3a6b0998bab288589ef261f5080e0c59de99ec3e
[strongswan.git] / Source / charon / ike_sa.c
1 /**
2 * @file ike_sa.c
3 *
4 * @brief Class ike_sa_t. An object of this type is managed by an
5 * ike_sa_manager_t object and represents an IKE_SA
6 *
7 */
8
9 /*
10 * Copyright (C) 2005 Jan Hutter, Martin Willi
11 * Hochschule fuer Technik Rapperswil
12 *
13 * This program is free software; you can redistribute it and/or modify it
14 * under the terms of the GNU General Public License as published by the
15 * Free Software Foundation; either version 2 of the License, or (at your
16 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
22 */
23
24 #include "ike_sa.h"
25
26 #include "types.h"
27 #include "globals.h"
28 #include "definitions.h"
29 #include "utils/allocator.h"
30 #include "utils/linked_list.h"
31 #include "utils/logger_manager.h"
32 #include "utils/randomizer.h"
33 #include "transforms/diffie_hellman.h"
34 #include "payloads/sa_payload.h"
35 #include "payloads/nonce_payload.h"
36 #include "payloads/ke_payload.h"
37 #include "payloads/transform_substructure.h"
38 #include "payloads/transform_attribute.h"
39
40
41 /**
42 * States in which a IKE_SA can actually be
43 */
44 typedef enum ike_sa_state_e ike_sa_state_t;
45
46 enum ike_sa_state_e {
47
48 /**
49 * IKE_SA is is not in a state
50 */
51 NO_STATE = 1,
52
53 /**
54 * A IKE_SA_INIT-message was sent: role initiator
55 */
56 IKE_SA_INIT_REQUESTED = 2,
57
58 /**
59 * A IKE_SA_INIT-message was replied: role responder
60 */
61 IKE_SA_INIT_RESPONDED = 3,
62
63 /**
64 * An IKE_AUTH-message was sent after a successful
65 * IKE_SA_INIT-exchange: role initiator
66 */
67 IKE_AUTH_REQUESTED = 4,
68
69 /**
70 * An IKE_AUTH-message was replied: role responder.
71 * In this state, all the informations for an IKE_SA
72 * and one CHILD_SA are known.
73 */
74 IKE_SA_INITIALIZED = 5
75 };
76
77 /**
78 * string mappings for ike_sa_state
79 */
80 mapping_t ike_sa_state_m[] = {
81 {NO_STATE, "NO_STATE"},
82 {IKE_SA_INIT_REQUESTED, "IKE_SA_INIT_REQUESTED"},
83 {IKE_SA_INIT_RESPONDED, "IKE_SA_INIT_RESPONDED"},
84 {IKE_AUTH_REQUESTED, "IKE_AUTH_REQUESTED"},
85 {IKE_SA_INITIALIZED, "IKE_SA_INITIALIZED"},
86 {MAPPING_END, NULL}
87 };
88
89
90 /**
91 * Private data of an message_t object
92 */
93 typedef struct private_ike_sa_s private_ike_sa_t;
94
95 struct private_ike_sa_s {
96
97 /**
98 * Public part of a ike_sa_t object
99 */
100 ike_sa_t public;
101
102 status_t (*build_sa_payload) (private_ike_sa_t *this, sa_payload_t **payload);
103 status_t (*build_ke_payload) (private_ike_sa_t *this, ke_payload_t **payload);
104 status_t (*build_nonce_payload) (private_ike_sa_t *this, nonce_payload_t **payload);
105
106 status_t (*build_message) (private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message);
107
108
109 status_t (*transto_ike_sa_init_requested) (private_ike_sa_t *this, char *name);
110 status_t (*transto_ike_sa_init_responded) (private_ike_sa_t *this, message_t *message);
111 status_t (*transto_ike_auth_requested) (private_ike_sa_t *this, message_t *message);
112
113 /* Private values */
114 /**
115 * Identifier for the current IKE_SA
116 */
117 ike_sa_id_t *ike_sa_id;
118
119 /**
120 * Linked List containing the child sa's of the current IKE_SA
121 */
122 linked_list_t *child_sas;
123
124 /**
125 * Current state of the IKE_SA
126 */
127 ike_sa_state_t state;
128
129 /**
130 * this SA's source for random data
131 */
132 randomizer_t *randomizer;
133
134 /**
135 * contains the last responded message
136 *
137 */
138 message_t *last_responded_message;
139
140 /**
141 * contains the last requested message
142 *
143 */
144 message_t *last_requested_message;
145
146 /**
147 * Informations of this host
148 */
149 struct {
150 host_t *host;
151 } me;
152
153 /**
154 * Informations of the other host
155 */
156 struct {
157 host_t *host;
158 } other;
159
160
161 struct {
162 /**
163 * Diffie Hellman object used to compute shared secret
164 */
165 diffie_hellman_t *diffie_hellman;
166 /**
167 * Diffie Hellman group number
168 */
169 u_int16_t dh_group_number;
170
171 /**
172 * Priority used get matching dh_group number
173 */
174 u_int16_t dh_group_priority;
175
176 /**
177 * selected proposals
178 */
179 linked_list_t *proposals;
180 /**
181 * Sent nonce value
182 */
183 chunk_t sent_nonce;
184 /**
185 * received nonce value
186 */
187 chunk_t received_nonce;
188 } ike_sa_init_data;
189
190
191 /**
192 * next message id to receive
193 */
194 u_int32_t message_id_in;
195
196 /**
197 * next message id to send
198 */
199 u_int32_t message_id_out;
200
201 /**
202 * a logger for this IKE_SA
203 */
204 logger_t *logger;
205 };
206
207 /**
208 * @brief implements function process_message of private_ike_sa_t
209 */
210 static status_t process_message (private_ike_sa_t *this, message_t *message)
211 {
212 u_int32_t message_id;
213 bool is_request;
214 is_request = message->get_request(message);
215 this->logger->log(this->logger, CONTROL|MORE, "Process %s message of exchange type %s",(is_request) ? "REQUEST" : "RESPONSE",
216 mapping_find(exchange_type_m,message->get_exchange_type(message)));
217
218
219 //message->get_exchange_type(message);
220
221
222 switch (message->get_exchange_type(message))
223 {
224 case IKE_SA_INIT:
225 {
226 if (message->get_request(message)) {
227 if (this->state == NO_STATE)
228 {
229 /* state transission NO_STATE => IKE_SA_INIT_RESPONDED */
230 return this->transto_ike_sa_init_responded(this, message);
231 }
232 }
233 else
234 {
235 if (this->state == IKE_SA_INIT_REQUESTED)
236 {
237 /* state transission IKE_SA_INIT_REQUESTED => IKE_AUTH_REQUESTED*/
238 return this->transto_ike_auth_requested(this, message);
239 }
240 }
241 break;
242 }
243 case IKE_AUTH:
244 {
245 /* break; */
246 }
247 case CREATE_CHILD_SA:
248 {
249 /* break; */
250 }
251 case INFORMATIONAL:
252 {
253 /* break; */
254 }
255 default:
256 {
257 this->logger->log(this->logger, ERROR, "processing %s-message not supported.",
258 mapping_find(exchange_type_m,message->get_exchange_type(message)));
259 return NOT_SUPPORTED;
260 }
261 }
262 this->logger->log(this->logger, ERROR, "received %s-message in state %s, rejected.",
263 mapping_find(exchange_type_m, message->get_exchange_type(message)),
264 mapping_find(ike_sa_state_m, this->state));
265 return INVALID_STATE;
266 }
267
268
269 static status_t build_message(private_ike_sa_t *this, exchange_type_t type, bool request, message_t **message)
270 {
271 status_t status;
272 message_t *new_message;
273 host_t *source, *destination;
274
275 new_message = message_create();
276 if (new_message == NULL)
277 {
278 return OUT_OF_RES;
279 }
280
281 status = this->me.host->clone(this->me.host, &source);
282 status |= this->other.host->clone(this->other.host, &destination);
283 if (status != SUCCESS)
284 {
285 new_message->destroy(new_message);
286 return status;
287 }
288 new_message->set_source(new_message, source);
289 new_message->set_destination(new_message, destination);
290
291 new_message->set_exchange_type(new_message, type);
292 new_message->set_request(new_message, request);
293
294 if (request)
295 {
296 new_message->set_message_id(new_message, this->message_id_out);
297 }else
298 {
299 new_message->set_message_id(new_message, this->message_id_in);
300 }
301
302 new_message->set_ike_sa_id(new_message, this->ike_sa_id);
303
304 *message = new_message;
305
306 return SUCCESS;
307 }
308
309
310 static status_t transto_ike_sa_init_requested(private_ike_sa_t *this, char *name)
311 {
312 message_t *message;
313 payload_t *payload;
314 packet_t *packet;
315 status_t status;
316
317 this->logger->log(this->logger, CONTROL, "initializing connection");
318
319 status = global_configuration_manager->get_local_host(global_configuration_manager, name, &(this->me.host));
320 if (status != SUCCESS)
321 {
322 return INVALID_ARG;
323 }
324
325 status = global_configuration_manager->get_remote_host(global_configuration_manager, name, &(this->other.host));
326 if (status != SUCCESS)
327 {
328 return INVALID_ARG;
329 }
330
331 status = global_configuration_manager->get_dh_group_number(global_configuration_manager, name, &(this->ike_sa_init_data.dh_group_number), this->ike_sa_init_data.dh_group_priority);
332 if (status != SUCCESS)
333 {
334 return INVALID_ARG;
335 }
336
337 this ->logger->log(this->logger, CONTROL|MORE, "create diffie hellman object");
338 if (this->ike_sa_init_data.diffie_hellman != NULL)
339 {
340 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t already existing!");
341 return FAILED;
342 }
343 this->ike_sa_init_data.diffie_hellman = diffie_hellman_create(this->ike_sa_init_data.dh_group_number);
344 if (this->ike_sa_init_data.diffie_hellman == NULL)
345 {
346 this->logger->log(this->logger, ERROR, "Object of type diffie_hellman_t could not be created!");
347 return FAILED;
348 }
349
350 if (this->ike_sa_init_data.sent_nonce.ptr != NULL)
351 {
352 this->logger->log(this->logger, ERROR, "Nonce for IKE_SA_INIT phase already existing!");
353 return FAILED;
354 }
355
356 if (this->randomizer->allocate_pseudo_random_bytes(this->randomizer, 16, &(this->ike_sa_init_data.sent_nonce)) != SUCCESS)
357 {
358 this->logger->log(this->logger, ERROR, "Could not create nonce!");
359 return OUT_OF_RES;
360 }
361
362
363 /* going to build message */
364
365 status = this->build_message(this, IKE_SA_INIT, TRUE, &message);
366 if (status != SUCCESS)
367 {
368 this->logger->log(this->logger, ERROR, "Could not build message");
369 return status;
370 }
371
372 /* build SA payload */
373 status = this->build_sa_payload(this, (sa_payload_t**)&payload);
374 if (status != SUCCESS)
375 {
376 this->logger->log(this->logger, ERROR, "Could not build SA payload");
377 message->destroy(message);
378 return status;
379 }
380 message->add_payload(message, payload);
381
382 /* build KE payload */
383 status = this->build_ke_payload(this,(ke_payload_t **) &payload);
384 if (status != SUCCESS)
385 {
386 this->logger->log(this->logger, ERROR, "Could not build KE payload");
387 message->destroy(message);
388 return status;
389 }
390 message->add_payload(message, payload);
391
392 /* build Nonce payload */
393 status = this->build_nonce_payload(this, (nonce_payload_t**)&payload);
394 if (status != SUCCESS)
395 {
396 this->logger->log(this->logger, ERROR, "Could not build NONCE payload");
397 message->destroy(message);
398 return status;
399 }
400 message->add_payload(message, payload);
401
402
403 status = message->generate(message, &packet);
404 if (status != SUCCESS)
405 {
406 this->logger->log(this->logger, ERROR, "Could not generate message");
407 message->destroy(message);
408 return status;
409 }
410
411 status = global_send_queue->add(global_send_queue, packet);
412 if (status != SUCCESS)
413 {
414 this->logger->log(this->logger, ERROR, "Could not add packet to send queue");
415 message->destroy(message);
416 return status;
417 }
418
419 if ( this->last_requested_message != NULL)
420 {
421 /* destroy message */
422 this->last_requested_message->destroy(this->last_requested_message);
423 }
424
425 this->last_requested_message = message;
426
427 /* message counter can now be increased */
428 this->message_id_out++;
429
430 /* states has NOW changed :-) */
431 this->state = IKE_SA_INIT_REQUESTED;
432
433 return SUCCESS;
434 }
435
436 static status_t transto_ike_sa_init_responded(private_ike_sa_t *this, message_t *request)
437 {
438 status_t status;
439 linked_list_iterator_t *payloads;
440 message_t *response;
441 host_t *source, *destination;
442
443 /* this is the first message we process, so copy host infos */
444 request->get_source(request, &source);
445 request->get_destination(request, &destination);
446 /* we need to clone them, since we destroy the message later */
447 destination->clone(destination, &(this->me.host));
448 source->clone(source, &(this->other.host));
449
450 /* parse incoming message */
451 status = request->parse_body(request);
452 if (status != SUCCESS)
453 {
454 this->logger->log(this->logger, ERROR, "Could not parse body");
455 return status;
456 }
457 /* iterate over incoming payloads */
458 status = request->get_payload_iterator(request, &payloads);
459 if (status != SUCCESS)
460 {
461 request->destroy(request);
462 return status;
463 }
464 while (payloads->has_next(payloads))
465 {
466 payload_t *payload;
467 payloads->current(payloads, (void**)&payload);
468
469 this->logger->log(this->logger, CONTROL|MORE, "Processing payload %s", mapping_find(payload_type_m, payload->get_type(payload)));
470 switch (payload->get_type(payload))
471 {
472 case SECURITY_ASSOCIATION:
473 {
474 sa_payload_t *sa_payload = (sa_payload_t*)payload;
475 linked_list_iterator_t *suggested_proposals, *accepted_proposals;
476 /* create a list for accepted proposals */
477 if (this->ike_sa_init_data.proposals == NULL) {
478 this->ike_sa_init_data.proposals = linked_list_create();
479 }
480 else
481 {
482 /** @todo destroy list contents */
483 }
484 if (this->ike_sa_init_data.proposals == NULL)
485 {
486 payloads->destroy(payloads);
487 return OUT_OF_RES;
488 }
489 status = this->ike_sa_init_data.proposals->create_iterator(this->ike_sa_init_data.proposals, &accepted_proposals, FALSE);
490 if (status != SUCCESS)
491 {
492 payloads->destroy(payloads);
493 return status;
494 }
495
496 /* get the list of suggested proposals */
497 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
498 if (status != SUCCESS)
499 {
500 accepted_proposals->destroy(accepted_proposals);
501 payloads->destroy(payloads);
502 return status;
503 }
504
505 /* now let the configuration-manager select a subset of the proposals */
506 status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
507 this->other.host, suggested_proposals, accepted_proposals);
508 if (status != SUCCESS)
509 {
510 suggested_proposals->destroy(suggested_proposals);
511 accepted_proposals->destroy(accepted_proposals);
512 payloads->destroy(payloads);
513 return status;
514 }
515
516 suggested_proposals->destroy(suggested_proposals);
517 accepted_proposals->destroy(accepted_proposals);
518
519 /* ok, we have what we need for sa_payload */
520 break;
521 }
522 case KEY_EXCHANGE:
523 {
524 ke_payload_t *ke_payload = (ke_payload_t*)payload;
525 diffie_hellman_t *dh;
526 diffie_hellman_group_t group;
527 bool allowed_group;
528
529 group = ke_payload->get_dh_group_number(ke_payload);
530
531 status = global_configuration_manager->is_dh_group_allowed_for_host(global_configuration_manager,
532 this->other.host, group, &allowed_group);
533 if (status != SUCCESS)
534 {
535 payloads->destroy(payloads);
536 return status;
537 }
538 if (!allowed_group)
539 {
540 /** @todo info reply */
541 }
542
543 dh = diffie_hellman_create(group);
544 if (dh == NULL)
545 {
546 payloads->destroy(payloads);
547 return OUT_OF_RES;
548 }
549
550 status = dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
551 if (status != SUCCESS)
552 {
553 dh->destroy(dh);
554 payloads->destroy(payloads);
555 return OUT_OF_RES;
556 }
557 /** @todo destroy if there is already one */
558 this->ike_sa_init_data.diffie_hellman = dh;
559 break;
560 }
561 case NONCE:
562 {
563 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
564 chunk_t nonce;
565
566 nonce_payload->get_nonce(nonce_payload, &nonce);
567 /** @todo free if there is already one */
568 this->ike_sa_init_data.received_nonce.ptr = allocator_clone_bytes(nonce.ptr, nonce.len);
569 this->ike_sa_init_data.received_nonce.len = nonce.len;
570 if (this->ike_sa_init_data.received_nonce.ptr == NULL)
571 {
572 payloads->destroy(payloads);
573 return OUT_OF_RES;
574 }
575 break;
576 }
577 default:
578 {
579 /** @todo handle */
580 }
581
582 }
583
584 }
585 payloads->destroy(payloads);
586
587 printf("done.\n");
588
589 /* set up the reply */
590 status = this->build_message(this, IKE_SA_INIT, FALSE, &response);
591 if (status != SUCCESS)
592 {
593 return status;
594 }
595
596
597
598
599 /*
600 job_t *delete_job;
601 delete_job = (job_t *) delete_ike_sa_job_create(this->ike_sa_id);
602 if (delete_job == NULL)
603 {
604 this->logger->log(this->logger, ERROR, "Job to delete IKE SA could not be created");
605 }
606
607 status = global_job_queue->add(global_job_queue,delete_job);
608 if (status != SUCCESS)
609 {
610 this->logger->log(this->logger, ERROR, "%s Job to delete IKE SA could not be added to job queue",mapping_find(status_m,status));
611 delete_job->destroy_all(delete_job);
612 }*/
613 return SUCCESS;
614 }
615
616 static status_t transto_ike_auth_requested(private_ike_sa_t *this, message_t *response)
617 {
618 status_t status;
619 linked_list_iterator_t *payloads;
620
621
622 /* parse incoming message */
623 status = response->parse_body(response);
624 if (status != SUCCESS)
625 {
626 this->logger->log(this->logger, ERROR, "Could not parse body");
627 return status;
628 }
629 /* iterate over incoming payloads */
630 status = response->get_payload_iterator(response, &payloads);
631 if (status != SUCCESS)
632 {
633 response->destroy(response);
634 return status;
635 }
636 while (payloads->has_next(payloads))
637 {
638 payload_t *payload;
639 payloads->current(payloads, (void**)&payload);
640
641 this->logger->log(this->logger, CONTROL|MORE, "Processing payload %s", mapping_find(payload_type_m, payload->get_type(payload)));
642 switch (payload->get_type(payload))
643 {
644 // case SECURITY_ASSOCIATION:
645 // {
646 // sa_payload_t *sa_payload = (sa_payload_t*)payload;
647 // linked_list_iterator_t *suggested_proposals, *accepted_proposals;
648 // /* create a list for accepted proposals */
649 // if (this->ike_sa_init_data.proposals == NULL) {
650 // this->ike_sa_init_data.proposals = linked_list_create();
651 // }
652 // else
653 // {
654 // /** @todo destroy list contents */
655 // }
656 // if (this->ike_sa_init_data.proposals == NULL)
657 // {
658 // payloads->destroy(payloads);
659 // return OUT_OF_RES;
660 // }
661 // status = this->ike_sa_init_data.proposals->create_iterator(this->ike_sa_init_data.proposals, &accepted_proposals, FALSE);
662 // if (status != SUCCESS)
663 // {
664 // payloads->destroy(payloads);
665 // return status;
666 // }
667 //
668 // /* get the list of suggested proposals */
669 // status = sa_payload->create_proposal_substructure_iterator(sa_payload, &suggested_proposals, TRUE);
670 // if (status != SUCCESS)
671 // {
672 // accepted_proposals->destroy(accepted_proposals);
673 // payloads->destroy(payloads);
674 // return status;
675 // }
676 //
677 // /* now let the configuration-manager select a subset of the proposals */
678 // status = global_configuration_manager->select_proposals_for_host(global_configuration_manager,
679 // this->other.host, suggested_proposals, accepted_proposals);
680 // if (status != SUCCESS)
681 // {
682 // suggested_proposals->destroy(suggested_proposals);
683 // accepted_proposals->destroy(accepted_proposals);
684 // payloads->destroy(payloads);
685 // return status;
686 // }
687 //
688 // suggested_proposals->destroy(suggested_proposals);
689 // accepted_proposals->destroy(accepted_proposals);
690 //
691 // /* ok, we have what we need for sa_payload */
692 // break;
693 // }
694 case KEY_EXCHANGE:
695 {
696 ke_payload_t *ke_payload = (ke_payload_t*)payload;
697 diffie_hellman_t *dh;
698 chunk_t shared_secret;
699
700 dh = this->ike_sa_init_data.diffie_hellman;
701
702
703
704
705 status = dh->set_other_public_value(dh, ke_payload->get_key_exchange_data(ke_payload));
706 if (status != SUCCESS)
707 {
708 dh->destroy(dh);
709 payloads->destroy(payloads);
710 return OUT_OF_RES;
711 }
712
713 status = dh->get_shared_secret(dh, &shared_secret);
714
715 this->logger->log_chunk(this->logger, RAW, "Shared secret", &shared_secret);
716
717 break;
718 }
719 case NONCE:
720 {
721 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
722 chunk_t nonce;
723
724 nonce_payload->get_nonce(nonce_payload, &nonce);
725 /** @todo free if there is already one */
726 this->ike_sa_init_data.received_nonce.ptr = allocator_clone_bytes(nonce.ptr, nonce.len);
727 this->ike_sa_init_data.received_nonce.len = nonce.len;
728 if (this->ike_sa_init_data.received_nonce.ptr == NULL)
729 {
730 payloads->destroy(payloads);
731 return OUT_OF_RES;
732 }
733 break;
734 }
735 default:
736 {
737 /** @todo handle */
738 }
739
740 }
741
742 }
743 payloads->destroy(payloads);
744
745 printf("done.\n");
746
747 /* set up the reply */
748 status = this->build_message(this, IKE_SA_INIT, FALSE, &response);
749 if (status != SUCCESS)
750 {
751 return status;
752 }
753
754
755
756 }
757
758 /**
759 * @brief implements function process_configuration of private_ike_sa_t
760 */
761 static status_t initialize_connection(private_ike_sa_t *this, char *name)
762 {
763 /* work is done in transto_ike_sa_init_requested */
764 return (this->transto_ike_sa_init_requested(this,name));
765 }
766
767 /**
768 * @brief implements function private_ike_sa_t.get_id
769 */
770 static ike_sa_id_t* get_id(private_ike_sa_t *this)
771 {
772 return this->ike_sa_id;
773 }
774
775 /**
776 * implements private_ike_sa_t.build_sa_payload
777 */
778 static status_t build_sa_payload(private_ike_sa_t *this, sa_payload_t **payload)
779 {
780 sa_payload_t* sa_payload;
781 linked_list_iterator_t *iterator;
782 status_t status;
783
784 this->logger->log(this->logger, CONTROL|MORE, "building sa payload");
785
786 sa_payload = sa_payload_create();
787 if (sa_payload == NULL)
788 {
789 return OUT_OF_RES;
790 }
791 status = sa_payload->create_proposal_substructure_iterator(sa_payload, &iterator, FALSE);
792 if (status != SUCCESS)
793 {
794 sa_payload->destroy(sa_payload);
795 return status;
796 }
797 status = global_configuration_manager->get_proposals_for_host(global_configuration_manager, this->other.host, iterator);
798 if (status != SUCCESS)
799 {
800 sa_payload->destroy(sa_payload);
801 return status;
802 }
803
804 *payload = sa_payload;
805
806 return SUCCESS;
807 }
808
809 static status_t build_ke_payload(private_ike_sa_t *this, ke_payload_t **payload)
810 {
811 ke_payload_t *ke_payload;
812 chunk_t key_data;
813 status_t status;
814
815 this->logger->log(this->logger, CONTROL|MORE, "building ke payload");
816
817 if (this->state != NO_STATE)
818 {
819 this->logger->log(this->logger, ERROR, "KE payload in state %s not supported",mapping_find(ike_sa_state_m,this->state));
820 return FALSE;
821 }
822
823 switch(this->ike_sa_id->is_initiator(this->ike_sa_id))
824 {
825 case TRUE:
826 {
827 this ->logger->log(this->logger, CONTROL|MORE, "get public dh value to send in ke payload");
828 status = this->ike_sa_init_data.diffie_hellman->get_my_public_value(this->ike_sa_init_data.diffie_hellman,&key_data);
829 if (status != SUCCESS)
830 {
831 this->logger->log(this->logger, ERROR, "Could not get my DH public value");
832 return status;
833 }
834
835 ke_payload = ke_payload_create();
836 if (ke_payload == NULL)
837 {
838 this->logger->log(this->logger, ERROR, "Could not create KE payload");
839 allocator_free_chunk(key_data);
840 return OUT_OF_RES;
841 }
842 ke_payload->set_dh_group_number(ke_payload, MODP_1024_BIT);
843 if (ke_payload->set_key_exchange_data(ke_payload, key_data) != SUCCESS)
844 {
845 this->logger->log(this->logger, ERROR, "Could not set key exchange data of KE payload");
846 ke_payload->destroy(ke_payload);
847 allocator_free_chunk(key_data);
848 return OUT_OF_RES;
849 }
850 allocator_free_chunk(key_data);
851
852 *payload = ke_payload;
853 return SUCCESS;
854 }
855 default: /* FALSE */
856 {
857 break;
858 }
859 }
860
861 return FAILED;
862 }
863
864 /**
865 * implements private_ike_sa_t.build_nonce_payload
866 */
867 static status_t build_nonce_payload(private_ike_sa_t *this, nonce_payload_t **payload)
868 {
869 nonce_payload_t *nonce_payload;
870
871 this->logger->log(this->logger, CONTROL|MORE, "building nonce payload");
872 nonce_payload = nonce_payload_create();
873 if (nonce_payload == NULL)
874 {
875 return OUT_OF_RES;
876 }
877
878 nonce_payload->set_nonce(nonce_payload, this->ike_sa_init_data.sent_nonce);
879
880 *payload = nonce_payload;
881
882 return SUCCESS;
883 }
884
885 /**
886 * @brief implements function destroy of private_ike_sa_t
887 */
888 static status_t destroy (private_ike_sa_t *this)
889 {
890
891 this->logger->log(this->logger, CONTROL | MORE, "Going to destroy IKE_SA");
892
893 /* destroy child sa's */
894 this->logger->log(this->logger, CONTROL | MOST, "Destroy all child_sa's");
895 while (this->child_sas->get_count(this->child_sas) > 0)
896 {
897 void *child_sa;
898 if (this->child_sas->remove_first(this->child_sas,&child_sa) != SUCCESS)
899 {
900 break;
901 }
902 /* destroy child sa */
903 }
904 this->child_sas->destroy(this->child_sas);
905
906 /* destroy ike_sa_id */
907 this->logger->log(this->logger, CONTROL | MOST, "Destroy assigned ike_sa_id");
908 this->ike_sa_id->destroy(this->ike_sa_id);
909
910 /* destroy stored requested message */
911 if (this->last_requested_message != NULL)
912 {
913 this->logger->log(this->logger, CONTROL | MOST, "Destroy last requested message");
914 this->last_requested_message->destroy(this->last_requested_message);
915 }
916
917 /* destroy stored responded messages */
918 if (this->last_responded_message != NULL)
919 {
920 this->logger->log(this->logger, CONTROL | MOST, "Destroy last responded message");
921 this->last_responded_message->destroy(this->last_responded_message);
922 }
923
924 this->logger->log(this->logger, CONTROL | MOST, "Destroy randomizer");
925 this->randomizer->destroy(this->randomizer);
926
927 this->logger->log(this->logger, CONTROL | MOST, "Going to destroy ike_sa_init data");
928 if (this->ike_sa_init_data.diffie_hellman != NULL)
929 {
930 this->logger->log(this->logger, CONTROL | MOST, "Destroy diffie hellman object");
931 this->ike_sa_init_data.diffie_hellman->destroy(this->ike_sa_init_data.diffie_hellman);
932 }
933 if (this->ike_sa_init_data.sent_nonce.ptr != NULL)
934 {
935 this->logger->log(this->logger, CONTROL | MOST, "Destroy sent nonce data");
936 allocator_free_chunk(this->ike_sa_init_data.sent_nonce);
937 }
938 if (this->ike_sa_init_data.received_nonce.ptr != NULL)
939 {
940 this->logger->log(this->logger, CONTROL | MOST, "Destroy received nonce data");
941 allocator_free_chunk(this->ike_sa_init_data.received_nonce);
942 }
943
944 this->logger->log(this->logger, CONTROL | MOST, "Destroy logger of IKE_SA");
945 global_logger_manager->destroy_logger(global_logger_manager, this->logger);
946
947 allocator_free(this);
948
949 return SUCCESS;
950 }
951
952 /*
953 * Described in Header
954 */
955 ike_sa_t * ike_sa_create(ike_sa_id_t *ike_sa_id)
956 {
957 private_ike_sa_t *this = allocator_alloc_thing(private_ike_sa_t);
958 if (this == NULL)
959 {
960 return NULL;
961 }
962
963
964 /* Public functions */
965 this->public.process_message = (status_t(*)(ike_sa_t*, message_t*)) process_message;
966 this->public.initialize_connection = (status_t(*)(ike_sa_t*, char*)) initialize_connection;
967 this->public.get_id = (ike_sa_id_t*(*)(ike_sa_t*)) get_id;
968 this->public.destroy = (status_t(*)(ike_sa_t*))destroy;
969
970 /* private functions */
971 this->build_sa_payload = build_sa_payload;
972 this->build_nonce_payload = build_nonce_payload;
973 this->build_ke_payload = build_ke_payload;
974 this->build_message = build_message;
975 this->transto_ike_sa_init_requested = transto_ike_sa_init_requested;
976 this->transto_ike_sa_init_responded = transto_ike_sa_init_responded;
977 this->transto_ike_auth_requested = transto_ike_auth_requested;
978
979
980
981 /* initialize private fields */
982 this->logger = global_logger_manager->create_logger(global_logger_manager, IKE_SA, NULL);
983 if (this->logger == NULL)
984 {
985 allocator_free(this);
986 }
987
988 if (ike_sa_id->clone(ike_sa_id,&(this->ike_sa_id)) != SUCCESS)
989 {
990 this->logger->log(this->logger, ERROR, "Fatal error: Could not clone ike_sa_id");
991 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
992 allocator_free(this);
993 return NULL;
994 }
995 this->child_sas = linked_list_create();
996 if (this->child_sas == NULL)
997 {
998 this->logger->log(this->logger, ERROR, "Fatal error: Could not create list for child_sa's");
999 this->ike_sa_id->destroy(this->ike_sa_id);
1000 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
1001 allocator_free(this);
1002 return NULL;
1003 }
1004 this->randomizer = randomizer_create();
1005 if (this->randomizer == NULL)
1006 {
1007 this->logger->log(this->logger, ERROR, "Fatal error: Could not create list for child_sa's");
1008 this->child_sas->destroy(this->child_sas);
1009 this->ike_sa_id->destroy(this->ike_sa_id);
1010 global_logger_manager->destroy_logger(global_logger_manager,this->logger);
1011 allocator_free(this);
1012 }
1013
1014 this->me.host = NULL;
1015 this->other.host = NULL;
1016 this->ike_sa_init_data.diffie_hellman = NULL;
1017 this->ike_sa_init_data.dh_group_number = 0;
1018 /* 1 means highest priority */
1019 this->ike_sa_init_data.dh_group_priority = 1;
1020 this->ike_sa_init_data.sent_nonce.len = 0;
1021 this->ike_sa_init_data.sent_nonce.ptr = NULL;
1022 this->ike_sa_init_data.received_nonce.len = 0;
1023 this->ike_sa_init_data.received_nonce.ptr = NULL;
1024 this->ike_sa_init_data.proposals = NULL;
1025 this->last_requested_message = NULL;
1026 this->last_responded_message = NULL;
1027 this->message_id_out = 0;
1028 this->message_id_in = 0;
1029
1030
1031 /* at creation time, IKE_SA isn't in a specific state */
1032 this->state = NO_STATE;
1033
1034 return (&this->public);
1035 }