Exchange IKEv1 SA specific proposal data with SA payload
[strongswan.git] / src / libcharon / sa / tasks / main_mode.c
1 /*
2 * Copyright (C) 2011 Tobias Brunner
3 * Hochschule fuer Technik Rapperswil
4 *
5 * Copyright (C) 2011 Martin Willi
6 * Copyright (C) 2011 revosec AG
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; either version 2 of the License, or (at your
11 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 */
18
19 #include "main_mode.h"
20
21 #include <string.h>
22
23 #include <daemon.h>
24 #include <sa/keymat_v1.h>
25 #include <crypto/diffie_hellman.h>
26 #include <encoding/payloads/sa_payload.h>
27 #include <encoding/payloads/ke_payload.h>
28 #include <encoding/payloads/nonce_payload.h>
29 #include <encoding/payloads/id_payload.h>
30 #include <encoding/payloads/hash_payload.h>
31
32 typedef struct private_main_mode_t private_main_mode_t;
33
34 /**
35 * Private members of a main_mode_t task.
36 */
37 struct private_main_mode_t {
38
39 /**
40 * Public methods and task_t interface.
41 */
42 main_mode_t public;
43
44 /**
45 * Assigned IKE_SA.
46 */
47 ike_sa_t *ike_sa;
48
49 /**
50 * Are we the initiator?
51 */
52 bool initiator;
53
54 /**
55 * IKE config to establish
56 */
57 ike_cfg_t *ike_cfg;
58
59 /**
60 * Peer config to use
61 */
62 peer_cfg_t *peer_cfg;
63
64 /**
65 * Local authentication configuration
66 */
67 auth_cfg_t *my_auth;
68
69 /**
70 * Remote authentication configuration
71 */
72 auth_cfg_t *other_auth;
73
74 /**
75 * selected IKE proposal
76 */
77 proposal_t *proposal;
78
79 /**
80 * DH exchange
81 */
82 diffie_hellman_t *dh;
83
84 /**
85 * Keymat derivation (from SA)
86 */
87 keymat_v1_t *keymat;
88
89 /**
90 * Received public DH value from peer
91 */
92 chunk_t dh_value;
93
94 /**
95 * Initiators nonce
96 */
97 chunk_t nonce_i;
98
99 /**
100 * Responder nonce
101 */
102 chunk_t nonce_r;
103
104 /**
105 * Encoded SA initiator payload used for authentication
106 */
107 chunk_t sa_payload;
108
109 /**
110 * Negotiated SA lifetime
111 */
112 u_int32_t lifetime;
113
114 /**
115 * Negotiated authentication method
116 */
117 auth_method_t auth_method;
118
119 /** states of main mode */
120 enum {
121 MM_INIT,
122 MM_SA,
123 MM_KE,
124 MM_AUTH,
125 } state;
126 };
127
128 /**
129 * Get the first authentcation config from peer config
130 */
131 static auth_cfg_t *get_auth_cfg(private_main_mode_t *this, bool local)
132 {
133 enumerator_t *enumerator;
134 auth_cfg_t *cfg = NULL;
135
136 enumerator = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg,
137 local);
138 enumerator->enumerate(enumerator, &cfg);
139 enumerator->destroy(enumerator);
140 return cfg;
141 }
142
143 /**
144 * Save the encoded SA payload of a message
145 */
146 static bool save_sa_payload(private_main_mode_t *this, message_t *message)
147 {
148 enumerator_t *enumerator;
149 payload_t *payload, *sa = NULL;
150 chunk_t data;
151 size_t offset = IKE_HEADER_LENGTH;
152
153 enumerator = message->create_payload_enumerator(message);
154 while (enumerator->enumerate(enumerator, &payload))
155 {
156 if (payload->get_type(payload) == SECURITY_ASSOCIATION_V1)
157 {
158 sa = payload;
159 break;
160 }
161 else
162 {
163 offset += payload->get_length(payload);
164 }
165 }
166 enumerator->destroy(enumerator);
167
168 data = message->get_packet_data(message);
169 if (sa && data.len >= offset + sa->get_length(sa))
170 {
171 /* Get SA payload without 4 byte fixed header */
172 data = chunk_skip(data, offset);
173 data.len = sa->get_length(sa);
174 data = chunk_skip(data, 4);
175 this->sa_payload = chunk_clone(data);
176 return TRUE;
177 }
178 return FALSE;
179 }
180
181 /**
182 * Build main mode hash payloads
183 */
184 static void build_hash(private_main_mode_t *this, bool initiator,
185 message_t *message, identification_t *id)
186 {
187 hash_payload_t *hash_payload;
188 chunk_t hash, dh;
189
190 this->dh->get_my_public_value(this->dh, &dh);
191 hash = this->keymat->get_hash(this->keymat, initiator, dh, this->dh_value,
192 this->ike_sa->get_id(this->ike_sa), this->sa_payload, id);
193 free(dh.ptr);
194
195 hash_payload = hash_payload_create();
196 hash_payload->set_hash(hash_payload, hash);
197 free(hash.ptr);
198
199 message->add_payload(message, &hash_payload->payload_interface);
200 }
201
202 /**
203 * Verify main mode hash payload
204 */
205 static bool verify_hash(private_main_mode_t *this, bool initiator,
206 message_t *message, identification_t *id)
207 {
208 hash_payload_t *hash_payload;
209 chunk_t hash, dh;
210 bool equal;
211
212 hash_payload = (hash_payload_t*)message->get_payload(message,
213 HASH_V1);
214 if (!hash_payload)
215 {
216 DBG1(DBG_IKE, "HASH payload missing in message");
217 return FALSE;
218 }
219 hash = hash_payload->get_hash(hash_payload);
220 this->dh->get_my_public_value(this->dh, &dh);
221 hash = this->keymat->get_hash(this->keymat, initiator, this->dh_value, dh,
222 this->ike_sa->get_id(this->ike_sa), this->sa_payload, id);
223 free(dh.ptr);
224 equal = chunk_equals(hash, hash_payload->get_hash(hash_payload));
225 free(hash.ptr);
226 if (!equal)
227 {
228 DBG1(DBG_IKE, "calculated HASH does not match HASH payload");
229 }
230 return equal;
231 }
232
233 /**
234 * Generate and add NONCE, KE payload
235 */
236 static bool add_nonce_ke(private_main_mode_t *this, chunk_t *nonce,
237 message_t *message)
238 {
239 nonce_payload_t *nonce_payload;
240 ke_payload_t *ke_payload;
241 rng_t *rng;
242
243 ke_payload = ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1,
244 this->dh);
245 message->add_payload(message, &ke_payload->payload_interface);
246
247 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
248 if (!rng)
249 {
250 DBG1(DBG_IKE, "no RNG found to create nonce");
251 return FALSE;
252 }
253 rng->allocate_bytes(rng, NONCE_SIZE, nonce);
254 rng->destroy(rng);
255
256 nonce_payload = nonce_payload_create(NONCE_V1);
257 nonce_payload->set_nonce(nonce_payload, *nonce);
258 message->add_payload(message, &nonce_payload->payload_interface);
259
260 return TRUE;
261 }
262
263 /**
264 * Extract nonce from NONCE payload, process KE payload
265 */
266 static bool get_nonce_ke(private_main_mode_t *this, chunk_t *nonce,
267 message_t *message)
268 {
269 nonce_payload_t *nonce_payload;
270 ke_payload_t *ke_payload;
271
272 ke_payload = (ke_payload_t*)message->get_payload(message, KEY_EXCHANGE_V1);
273 if (!ke_payload)
274 {
275 DBG1(DBG_IKE, "KE payload missing in message");
276 return FALSE;
277 }
278 this->dh_value = chunk_clone(ke_payload->get_key_exchange_data(ke_payload));
279 this->dh->set_other_public_value(this->dh, this->dh_value);
280
281 nonce_payload = (nonce_payload_t*)message->get_payload(message, NONCE_V1);
282 if (!nonce_payload)
283 {
284 DBG1(DBG_IKE, "NONCE payload missing in message");
285 return FALSE;
286 }
287 *nonce = nonce_payload->get_nonce(nonce_payload);
288
289 return TRUE;
290 }
291
292 /**
293 * Get auth method to use
294 */
295 static auth_method_t get_auth_method(private_main_mode_t *this)
296 {
297 switch ((uintptr_t)this->my_auth->get(this->my_auth, AUTH_RULE_AUTH_CLASS))
298 {
299 case AUTH_CLASS_PSK:
300 return AUTH_PSK;
301 case AUTH_CLASS_PUBKEY:
302 /* TODO-IKEv1: look for a key, return RSA or ECDSA */
303 default:
304 /* TODO-IKEv1: XAUTH methods */
305 return AUTH_RSA;
306 }
307 }
308
309 METHOD(task_t, build_i, status_t,
310 private_main_mode_t *this, message_t *message)
311 {
312 switch (this->state)
313 {
314 case MM_INIT:
315 {
316 sa_payload_t *sa_payload;
317 linked_list_t *proposals;
318 packet_t *packet;
319
320 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
321 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
322 this->ike_sa->get_name(this->ike_sa),
323 this->ike_sa->get_unique_id(this->ike_sa),
324 this->ike_sa->get_other_host(this->ike_sa));
325 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
326
327 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
328 this->peer_cfg->get_ref(this->peer_cfg);
329
330 this->my_auth = get_auth_cfg(this, TRUE);
331 this->other_auth = get_auth_cfg(this, FALSE);
332 if (!this->my_auth || !this->other_auth)
333 {
334 DBG1(DBG_CFG, "no auth config found");
335 return FAILED;
336 }
337
338 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
339 this->auth_method = get_auth_method(this);
340 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
341 FALSE);
342 if (!this->lifetime)
343 { /* fall back to rekey time of no rekey time configured */
344 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
345 FALSE);
346 }
347 sa_payload = sa_payload_create_from_proposals_v1(proposals,
348 this->lifetime, 0, this->auth_method, MODE_NONE, FALSE);
349 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
350
351 message->add_payload(message, &sa_payload->payload_interface);
352
353 /* pregenerate message to store SA payload */
354 if (this->ike_sa->generate_message(this->ike_sa, message,
355 &packet) != SUCCESS)
356 {
357 DBG1(DBG_IKE, "pregenerating SA payload failed");
358 return FAILED;
359 }
360 packet->destroy(packet);
361 if (!save_sa_payload(this, message))
362 {
363 DBG1(DBG_IKE, "SA payload invalid");
364 return FAILED;
365 }
366
367 this->state = MM_SA;
368 return NEED_MORE;
369 }
370 case MM_SA:
371 {
372 u_int16_t group;
373
374 if (!this->proposal->get_algorithm(this->proposal,
375 DIFFIE_HELLMAN_GROUP, &group, NULL))
376 {
377 DBG1(DBG_IKE, "DH group selection failed");
378 return FAILED;
379 }
380 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
381 group);
382 if (!this->dh)
383 {
384 DBG1(DBG_IKE, "negotiated DH group not supported");
385 return FAILED;
386 }
387 if (!add_nonce_ke(this, &this->nonce_i, message))
388 {
389 return FAILED;
390 }
391 this->state = MM_KE;
392 return NEED_MORE;
393 }
394 case MM_KE:
395 {
396 id_payload_t *id_payload;
397 identification_t *id;
398
399 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
400 if (!id)
401 {
402 DBG1(DBG_CFG, "own identity not known");
403 return FAILED;
404 }
405
406 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
407
408 id_payload = id_payload_create_from_identification(ID_V1, id);
409 message->add_payload(message, &id_payload->payload_interface);
410
411 build_hash(this, TRUE, message, id);
412
413 this->state = MM_AUTH;
414 return NEED_MORE;
415 }
416 default:
417 return FAILED;
418 }
419 }
420
421 METHOD(task_t, process_r, status_t,
422 private_main_mode_t *this, message_t *message)
423 {
424 switch (this->state)
425 {
426 case MM_INIT:
427 {
428 linked_list_t *list;
429 sa_payload_t *sa_payload;
430
431 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
432 DBG0(DBG_IKE, "%H is initiating a Main Mode",
433 message->get_source(message));
434 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
435
436 this->ike_sa->update_hosts(this->ike_sa,
437 message->get_destination(message),
438 message->get_source(message), TRUE);
439
440 sa_payload = (sa_payload_t*)message->get_payload(message,
441 SECURITY_ASSOCIATION_V1);
442 if (!sa_payload || !save_sa_payload(this, message))
443 {
444 DBG1(DBG_IKE, "SA payload missing or invalid");
445 return FAILED;
446 }
447
448 list = sa_payload->get_proposals(sa_payload);
449 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
450 list, FALSE);
451 list->destroy_offset(list, offsetof(proposal_t, destroy));
452 if (!this->proposal)
453 {
454 DBG1(DBG_IKE, "no proposal found");
455 return FAILED;
456 }
457
458 this->auth_method = sa_payload->get_auth_method(sa_payload);
459 this->lifetime = sa_payload->get_lifetime(sa_payload);
460
461 this->state = MM_SA;
462 return NEED_MORE;
463 }
464 case MM_SA:
465 {
466 u_int16_t group;
467
468 if (!this->proposal->get_algorithm(this->proposal,
469 DIFFIE_HELLMAN_GROUP, &group, NULL))
470 {
471 DBG1(DBG_IKE, "DH group selection failed");
472 return FAILED;
473 }
474 this->dh = lib->crypto->create_dh(lib->crypto, group);
475 if (!this->dh)
476 {
477 DBG1(DBG_IKE, "negotiated DH group not supported");
478 return FAILED;
479 }
480 if (!get_nonce_ke(this, &this->nonce_i, message))
481 {
482 return FAILED;
483 }
484 this->state = MM_KE;
485 return NEED_MORE;
486 }
487 case MM_KE:
488 {
489 enumerator_t *enumerator;
490 id_payload_t *id_payload;
491 identification_t *id, *any;
492
493 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
494 if (!id_payload)
495 {
496 DBG1(DBG_IKE, "IDii payload missing");
497 return FAILED;
498 }
499
500 id = id_payload->get_identification(id_payload);
501 any = identification_create_from_encoding(ID_ANY, chunk_empty);
502 enumerator = charon->backends->create_peer_cfg_enumerator(
503 charon->backends,
504 this->ike_sa->get_my_host(this->ike_sa),
505 this->ike_sa->get_other_host(this->ike_sa),
506 any, id);
507 if (!enumerator->enumerate(enumerator, &this->peer_cfg))
508 {
509 DBG1(DBG_IKE, "no peer config found");
510 id->destroy(id);
511 any->destroy(any);
512 enumerator->destroy(enumerator);
513 return FAILED;
514 }
515 this->peer_cfg->get_ref(this->peer_cfg);
516 enumerator->destroy(enumerator);
517 any->destroy(any);
518
519 this->ike_sa->set_other_id(this->ike_sa, id);
520
521 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
522
523 this->my_auth = get_auth_cfg(this, TRUE);
524 this->other_auth = get_auth_cfg(this, FALSE);
525 if (!this->my_auth || !this->other_auth)
526 {
527 DBG1(DBG_IKE, "auth config missing");
528 return FAILED;
529 }
530
531 if (!verify_hash(this, TRUE, message, id))
532 {
533 return FAILED;
534 }
535
536 this->state = MM_AUTH;
537 return NEED_MORE;
538 }
539 default:
540 return FAILED;
541 }
542 }
543
544 /**
545 * Lookup a shared secret for this IKE_SA
546 */
547 static shared_key_t *lookup_shared_key(private_main_mode_t *this)
548 {
549 host_t *me, *other;
550 identification_t *my_id, *other_id;
551 shared_key_t *shared_key;
552
553 me = this->ike_sa->get_my_host(this->ike_sa);
554 other = this->ike_sa->get_other_host(this->ike_sa);
555 my_id = identification_create_from_sockaddr(me->get_sockaddr(me));
556 other_id = identification_create_from_sockaddr(other->get_sockaddr(other));
557 if (!my_id || !other_id)
558 {
559 DESTROY_IF(my_id);
560 DESTROY_IF(other_id);
561 return NULL;
562 }
563 shared_key = lib->credmgr->get_shared(lib->credmgr, SHARED_IKE, my_id,
564 other_id);
565 if (!shared_key)
566 {
567 DBG1(DBG_IKE, "no shared key found for %H - %H", me, other);
568 }
569 my_id->destroy(my_id);
570 other_id->destroy(other_id);
571 return shared_key;
572 }
573
574 /**
575 * Derive key material for this IKE_SA
576 */
577 static bool derive_keys(private_main_mode_t *this, chunk_t nonce_i,
578 chunk_t nonce_r)
579 {
580 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
581 shared_key_t *shared_key = NULL;
582 auth_class_t auth;
583
584 /* TODO-IKEv1: support other authentication classes */
585 auth = AUTH_CLASS_PSK;
586 switch (auth)
587 {
588 case AUTH_CLASS_PSK:
589 shared_key = lookup_shared_key(this);
590 break;
591 default:
592 break;
593 }
594 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
595 this->dh_value, nonce_i, nonce_r, id, auth, shared_key))
596 {
597 DESTROY_IF(shared_key);
598 return FALSE;
599 }
600 DESTROY_IF(shared_key);
601 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, nonce_i, nonce_r,
602 NULL);
603 return TRUE;
604 }
605
606 METHOD(task_t, build_r, status_t,
607 private_main_mode_t *this, message_t *message)
608 {
609 switch (this->state)
610 {
611 case MM_SA:
612 {
613 sa_payload_t *sa_payload;
614
615 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
616 this->lifetime, 0, this->auth_method, MODE_NONE, FALSE);
617 message->add_payload(message, &sa_payload->payload_interface);
618
619 return NEED_MORE;
620 }
621 case MM_KE:
622 {
623 if (!add_nonce_ke(this, &this->nonce_r, message))
624 {
625 return FAILED;
626 }
627 if (!derive_keys(this, this->nonce_i, this->nonce_r))
628 {
629 DBG1(DBG_IKE, "key derivation failed");
630 return FAILED;
631 }
632 return NEED_MORE;
633 }
634 case MM_AUTH:
635 {
636 id_payload_t *id_payload;
637 identification_t *id;
638
639 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
640 if (!id)
641 {
642 DBG1(DBG_CFG, "own identity not known");
643 return FAILED;
644 }
645
646 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
647
648 id_payload = id_payload_create_from_identification(ID_V1, id);
649 message->add_payload(message, &id_payload->payload_interface);
650
651 build_hash(this, FALSE, message, id);
652
653 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
654 this->ike_sa->get_name(this->ike_sa),
655 this->ike_sa->get_unique_id(this->ike_sa),
656 this->ike_sa->get_my_host(this->ike_sa),
657 this->ike_sa->get_my_id(this->ike_sa),
658 this->ike_sa->get_other_host(this->ike_sa),
659 this->ike_sa->get_other_id(this->ike_sa));
660 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
661 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
662
663 /* TODO-IKEv1: Check the proposal for XAuthInit* auth modes */
664 /* TODO-IKEv1: check for XAUTH rounds, queue them */
665 if(0) /* TODO-IKEv1: Change to 1 if XAUTH is desired. */
666 return MIGRATE;
667 return SUCCESS;
668 }
669 default:
670 return FAILED;
671 }
672 }
673
674 METHOD(task_t, process_i, status_t,
675 private_main_mode_t *this, message_t *message)
676 {
677 switch (this->state)
678 {
679 case MM_SA:
680 {
681 linked_list_t *list;
682 sa_payload_t *sa_payload;
683 auth_method_t auth_method;
684 u_int32_t lifetime;
685
686 sa_payload = (sa_payload_t*)message->get_payload(message,
687 SECURITY_ASSOCIATION_V1);
688 if (!sa_payload)
689 {
690 DBG1(DBG_IKE, "SA payload missing");
691 return FAILED;
692 }
693 list = sa_payload->get_proposals(sa_payload);
694 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
695 list, FALSE);
696 list->destroy_offset(list, offsetof(proposal_t, destroy));
697 if (!this->proposal)
698 {
699 DBG1(DBG_IKE, "no proposal found");
700 return FAILED;
701 }
702
703 lifetime = sa_payload->get_lifetime(sa_payload);
704 if (lifetime != this->lifetime)
705 {
706 DBG1(DBG_IKE, "received lifetime %us does not match configured "
707 "%us, using lower value", lifetime, this->lifetime);
708 }
709 this->lifetime = min(this->lifetime, lifetime);
710 auth_method = sa_payload->get_auth_method(sa_payload);
711 if (auth_method != this->auth_method)
712 {
713 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
714 "continue with configured", auth_method_names, auth_method,
715 auth_method_names, this->auth_method);
716 }
717 return NEED_MORE;
718 }
719 case MM_KE:
720 {
721 if (!get_nonce_ke(this, &this->nonce_r, message))
722 {
723 return FAILED;
724 }
725 if (!derive_keys(this, this->nonce_i, this->nonce_r))
726 {
727 DBG1(DBG_IKE, "key derivation failed");
728 return FAILED;
729 }
730 return NEED_MORE;
731 }
732 case MM_AUTH:
733 {
734 id_payload_t *id_payload;
735 identification_t *id;
736
737 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
738 if (!id_payload)
739 {
740 DBG1(DBG_IKE, "IDir payload missing");
741 return FAILED;
742 }
743 id = id_payload->get_identification(id_payload);
744 if (!id->matches(id, this->other_auth->get(this->other_auth,
745 AUTH_RULE_IDENTITY)))
746 {
747 DBG1(DBG_IKE, "IDir does not match");
748 id->destroy(id);
749 return FAILED;
750 }
751 this->ike_sa->set_other_id(this->ike_sa, id);
752
753 if (!verify_hash(this, FALSE, message, id))
754 {
755 return FAILED;
756 }
757
758 /* TODO-IKEv1: check for XAUTH rounds, queue them */
759 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
760 this->ike_sa->get_name(this->ike_sa),
761 this->ike_sa->get_unique_id(this->ike_sa),
762 this->ike_sa->get_my_host(this->ike_sa),
763 this->ike_sa->get_my_id(this->ike_sa),
764 this->ike_sa->get_other_host(this->ike_sa),
765 this->ike_sa->get_other_id(this->ike_sa));
766 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
767 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
768 return SUCCESS;
769 }
770 default:
771 return FAILED;
772 }
773 }
774
775 METHOD(task_t, get_type, task_type_t,
776 private_main_mode_t *this)
777 {
778 return TASK_MAIN_MODE;
779 }
780
781 METHOD(task_t, migrate, void,
782 private_main_mode_t *this, ike_sa_t *ike_sa)
783 {
784 this->ike_sa = ike_sa;
785 }
786
787 METHOD(task_t, destroy, void,
788 private_main_mode_t *this)
789 {
790 DESTROY_IF(this->peer_cfg);
791 DESTROY_IF(this->proposal);
792 DESTROY_IF(this->dh);
793 free(this->dh_value.ptr);
794 free(this->nonce_i.ptr);
795 free(this->nonce_r.ptr);
796 free(this->sa_payload.ptr);
797 free(this);
798 }
799
800 /*
801 * Described in header.
802 */
803 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
804 {
805 private_main_mode_t *this;
806
807 INIT(this,
808 .public = {
809 .task = {
810 .get_type = _get_type,
811 .migrate = _migrate,
812 .destroy = _destroy,
813 },
814 },
815 .ike_sa = ike_sa,
816 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
817 .initiator = initiator,
818 .state = MM_INIT,
819 );
820
821 if (initiator)
822 {
823 this->public.task.build = _build_i;
824 this->public.task.process = _process_i;
825 }
826 else
827 {
828 this->public.task.build = _build_r;
829 this->public.task.process = _process_r;
830 }
831
832 return &this->public;
833 }