Create and verify PSK HASH payloads in IKEv1 main mode
[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 /** states of main mode */
110 enum {
111 MM_INIT,
112 MM_SA,
113 MM_KE,
114 MM_AUTH,
115 } state;
116 };
117
118 /**
119 * Get the first authentcation config from peer config
120 */
121 static auth_cfg_t *get_auth_cfg(private_main_mode_t *this, bool local)
122 {
123 enumerator_t *enumerator;
124 auth_cfg_t *cfg = NULL;
125
126 enumerator = this->peer_cfg->create_auth_cfg_enumerator(this->peer_cfg,
127 local);
128 enumerator->enumerate(enumerator, &cfg);
129 enumerator->destroy(enumerator);
130 return cfg;
131 }
132
133 /**
134 * Save the encoded SA payload of a message
135 */
136 static bool save_sa_payload(private_main_mode_t *this, message_t *message,
137 sa_payload_t *sa_payload)
138 {
139 payload_t *payload;
140 chunk_t data;
141
142 /* TODO-IKEv1: handle other payloads in front of SA? */
143 payload = &sa_payload->payload_interface;
144 data = message->get_packet_data(message);
145 if (data.len >= IKE_HEADER_LENGTH + payload->get_length(payload))
146 {
147 /* Get SA payload without 4 byte fixed header */
148 data = chunk_skip(data, IKE_HEADER_LENGTH);
149 data.len = payload->get_length(payload);
150 data = chunk_skip(data, 4);
151 this->sa_payload = chunk_clone(data);
152 return TRUE;
153 }
154 return FALSE;
155 }
156
157 METHOD(task_t, build_i, status_t,
158 private_main_mode_t *this, message_t *message)
159 {
160 switch (this->state)
161 {
162 case MM_INIT:
163 {
164 sa_payload_t *sa_payload;
165 linked_list_t *proposals;
166 packet_t *packet;
167
168 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
169 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
170 this->ike_sa->get_name(this->ike_sa),
171 this->ike_sa->get_unique_id(this->ike_sa),
172 this->ike_sa->get_other_host(this->ike_sa));
173 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
174
175 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
176
177 sa_payload = sa_payload_create_from_proposal_list(
178 SECURITY_ASSOCIATION_V1, proposals);
179 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
180
181 message->add_payload(message, &sa_payload->payload_interface);
182
183 /* pregenerate message to store SA payload */
184 if (this->ike_sa->generate_message(this->ike_sa, message,
185 &packet) != SUCCESS)
186 {
187 DBG1(DBG_IKE, "pregenerating SA payload failed");
188 return FAILED;
189 }
190 packet->destroy(packet);
191 if (!save_sa_payload(this, message, sa_payload))
192 {
193 DBG1(DBG_IKE, "SA payload invalid");
194 return FAILED;
195 }
196
197 this->state = MM_SA;
198 return NEED_MORE;
199 }
200 case MM_SA:
201 {
202 ke_payload_t *ke_payload;
203 nonce_payload_t *nonce_payload;
204 u_int16_t group;
205 rng_t *rng;
206
207 if (!this->proposal->get_algorithm(this->proposal,
208 DIFFIE_HELLMAN_GROUP, &group, NULL))
209 {
210 DBG1(DBG_IKE, "DH group selection failed");
211 return FAILED;
212 }
213 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
214 group);
215 if (!this->dh)
216 {
217 DBG1(DBG_IKE, "negotiated DH group not supported");
218 return FAILED;
219 }
220 ke_payload = ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1,
221 this->dh);
222 message->add_payload(message, &ke_payload->payload_interface);
223
224 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
225 if (!rng)
226 {
227 DBG1(DBG_IKE, "no RNG found to create nonce");
228 return FAILED;
229 }
230 rng->allocate_bytes(rng, NONCE_SIZE, &this->nonce_i);
231 rng->destroy(rng);
232
233 nonce_payload = nonce_payload_create(NONCE_V1);
234 nonce_payload->set_nonce(nonce_payload, this->nonce_i);
235 message->add_payload(message, &nonce_payload->payload_interface);
236
237 this->state = MM_KE;
238 return NEED_MORE;
239 }
240 case MM_KE:
241 {
242 id_payload_t *id_payload;
243 hash_payload_t *hash_payload;
244 identification_t *id;
245 chunk_t hash, dh;
246
247 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
248 this->peer_cfg->get_ref(this->peer_cfg);
249
250 this->my_auth = get_auth_cfg(this, TRUE);
251 this->other_auth = get_auth_cfg(this, FALSE);
252 if (!this->my_auth || !this->other_auth)
253 {
254 DBG1(DBG_CFG, "no auth config found");
255 return FAILED;
256 }
257 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
258 if (!id)
259 {
260 DBG1(DBG_CFG, "own identity not known");
261 return FAILED;
262 }
263
264 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
265
266 id_payload = id_payload_create_from_identification(ID_V1, id);
267 message->add_payload(message, &id_payload->payload_interface);
268
269 this->dh->get_my_public_value(this->dh, &dh);
270 hash = this->keymat->get_hash(this->keymat, TRUE,
271 dh, this->dh_value, this->ike_sa->get_id(this->ike_sa),
272 this->sa_payload, id);
273 free(dh.ptr);
274 hash_payload = hash_payload_create();
275 hash_payload->set_hash(hash_payload, hash);
276 free(hash.ptr);
277 message->add_payload(message, &hash_payload->payload_interface);
278
279 this->state = MM_AUTH;
280 return NEED_MORE;
281 }
282 default:
283 return FAILED;
284 }
285 }
286
287 METHOD(task_t, process_r, status_t,
288 private_main_mode_t *this, message_t *message)
289 {
290 switch (this->state)
291 {
292 case MM_INIT:
293 {
294 linked_list_t *list;
295 sa_payload_t *sa_payload;
296
297 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
298 DBG0(DBG_IKE, "%H is initiating a Main Mode",
299 message->get_source(message));
300 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
301
302 this->ike_sa->update_hosts(this->ike_sa,
303 message->get_destination(message),
304 message->get_source(message), TRUE);
305
306 sa_payload = (sa_payload_t*)message->get_payload(message,
307 SECURITY_ASSOCIATION_V1);
308 if (!sa_payload || !save_sa_payload(this, message, sa_payload))
309 {
310 DBG1(DBG_IKE, "SA payload missing or invalid");
311 return FAILED;
312 }
313
314 list = sa_payload->get_proposals(sa_payload);
315 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
316 list, FALSE);
317 list->destroy_offset(list, offsetof(proposal_t, destroy));
318 if (!this->proposal)
319 {
320 DBG1(DBG_IKE, "no proposal found");
321 return FAILED;
322 }
323 this->state = MM_SA;
324 return NEED_MORE;
325 }
326 case MM_SA:
327 {
328 ke_payload_t *ke_payload;
329 nonce_payload_t *nonce_payload;
330 u_int16_t group;
331
332 ke_payload = (ke_payload_t*)message->get_payload(message,
333 KEY_EXCHANGE_V1);
334 if (!ke_payload)
335 {
336 DBG1(DBG_IKE, "KE payload missing");
337 return FAILED;
338 }
339 this->dh_value = ke_payload->get_key_exchange_data(ke_payload);
340 this->dh_value = chunk_clone(this->dh_value);
341
342 if (!this->proposal->get_algorithm(this->proposal,
343 DIFFIE_HELLMAN_GROUP, &group, NULL))
344 {
345 DBG1(DBG_IKE, "DH group selection failed");
346 return FAILED;
347 }
348 this->dh = lib->crypto->create_dh(lib->crypto, group);
349 if (!this->dh)
350 {
351 DBG1(DBG_IKE, "negotiated DH group not supported");
352 return FAILED;
353 }
354 this->dh->set_other_public_value(this->dh, this->dh_value);
355
356 nonce_payload = (nonce_payload_t*)message->get_payload(message,
357 NONCE_V1);
358 if (!nonce_payload)
359 {
360 DBG1(DBG_IKE, "Nonce payload missing");
361 return FAILED;
362 }
363 this->nonce_i = nonce_payload->get_nonce(nonce_payload);
364
365 this->state = MM_KE;
366 return NEED_MORE;
367 }
368 case MM_KE:
369 {
370 enumerator_t *enumerator;
371 id_payload_t *id_payload;
372 hash_payload_t *hash_payload;
373 identification_t *id, *any;
374 chunk_t hash, dh;
375
376 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
377 if (!id_payload)
378 {
379 DBG1(DBG_IKE, "IDii payload missing");
380 return FAILED;
381 }
382
383 id = id_payload->get_identification(id_payload);
384 any = identification_create_from_encoding(ID_ANY, chunk_empty);
385 enumerator = charon->backends->create_peer_cfg_enumerator(
386 charon->backends,
387 this->ike_sa->get_my_host(this->ike_sa),
388 this->ike_sa->get_other_host(this->ike_sa),
389 any, id);
390 if (!enumerator->enumerate(enumerator, &this->peer_cfg))
391 {
392 DBG1(DBG_IKE, "no peer config found");
393 id->destroy(id);
394 any->destroy(any);
395 enumerator->destroy(enumerator);
396 return FAILED;
397 }
398 this->peer_cfg->get_ref(this->peer_cfg);
399 enumerator->destroy(enumerator);
400 any->destroy(any);
401
402 this->ike_sa->set_other_id(this->ike_sa, id);
403
404 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
405
406 this->my_auth = get_auth_cfg(this, TRUE);
407 this->other_auth = get_auth_cfg(this, FALSE);
408 if (!this->my_auth || !this->other_auth)
409 {
410 DBG1(DBG_IKE, "auth config missing");
411 return FAILED;
412 }
413
414 hash_payload = (hash_payload_t*)message->get_payload(message,
415 HASH_V1);
416 if (!hash_payload)
417 {
418 DBG1(DBG_IKE, "hash payload missing");
419 return FAILED;
420 }
421 hash = hash_payload->get_hash(hash_payload);
422 this->dh->get_my_public_value(this->dh, &dh);
423 hash = this->keymat->get_hash(this->keymat, TRUE,
424 this->dh_value, dh, this->ike_sa->get_id(this->ike_sa),
425 this->sa_payload, id);
426 free(dh.ptr);
427 if (!chunk_equals(hash, hash_payload->get_hash(hash_payload)))
428 {
429 DBG1(DBG_IKE, "calculated hash does not match to hash payload");
430 free(hash.ptr);
431 return FAILED;
432 }
433 free(hash.ptr);
434
435 this->state = MM_AUTH;
436 return NEED_MORE;
437 }
438 default:
439 return FAILED;
440 }
441 }
442
443 /**
444 * Lookup a shared secret for this IKE_SA
445 */
446 static shared_key_t *lookup_shared_key(private_main_mode_t *this)
447 {
448 host_t *me, *other;
449 identification_t *my_id, *other_id;
450 shared_key_t *shared_key;
451
452 me = this->ike_sa->get_my_host(this->ike_sa);
453 other = this->ike_sa->get_other_host(this->ike_sa);
454 my_id = identification_create_from_sockaddr(me->get_sockaddr(me));
455 other_id = identification_create_from_sockaddr(other->get_sockaddr(other));
456 if (!my_id || !other_id)
457 {
458 DESTROY_IF(my_id);
459 DESTROY_IF(other_id);
460 return NULL;
461 }
462 shared_key = lib->credmgr->get_shared(lib->credmgr, SHARED_IKE, my_id,
463 other_id);
464 if (!shared_key)
465 {
466 DBG1(DBG_IKE, "no shared key found for %H - %H", me, other);
467 }
468 my_id->destroy(my_id);
469 other_id->destroy(other_id);
470 return shared_key;
471 }
472
473 /**
474 * Derive key material for this IKE_SA
475 */
476 static bool derive_keys(private_main_mode_t *this, chunk_t nonce_i,
477 chunk_t nonce_r)
478 {
479 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
480 shared_key_t *shared_key = NULL;
481 auth_class_t auth;
482
483 /* TODO-IKEv1: support other authentication classes */
484 auth = AUTH_CLASS_PSK;
485 switch (auth)
486 {
487 case AUTH_CLASS_PSK:
488 shared_key = lookup_shared_key(this);
489 break;
490 default:
491 break;
492 }
493 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
494 this->dh_value, nonce_i, nonce_r, id, auth, shared_key))
495 {
496 DESTROY_IF(shared_key);
497 return FALSE;
498 }
499 DESTROY_IF(shared_key);
500 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, nonce_i, nonce_r,
501 NULL);
502 return TRUE;
503 }
504
505 METHOD(task_t, build_r, status_t,
506 private_main_mode_t *this, message_t *message)
507 {
508 switch (this->state)
509 {
510 case MM_SA:
511 {
512 sa_payload_t *sa_payload;
513
514 sa_payload = sa_payload_create_from_proposal(SECURITY_ASSOCIATION_V1,
515 this->proposal);
516 message->add_payload(message, &sa_payload->payload_interface);
517
518 return NEED_MORE;
519 }
520 case MM_KE:
521 {
522 ke_payload_t *ke_payload;
523 nonce_payload_t *nonce_payload;
524 rng_t *rng;
525
526 ke_payload = ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1,
527 this->dh);
528 message->add_payload(message, &ke_payload->payload_interface);
529
530 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
531 if (!rng)
532 {
533 DBG1(DBG_IKE, "no RNG found to create nonce");
534 return FAILED;
535 }
536 rng->allocate_bytes(rng, NONCE_SIZE, &this->nonce_r);
537 rng->destroy(rng);
538
539 if (!derive_keys(this, this->nonce_i, this->nonce_r))
540 {
541 DBG1(DBG_IKE, "key derivation failed");
542 return FAILED;
543 }
544
545 nonce_payload = nonce_payload_create(NONCE_V1);
546 nonce_payload->set_nonce(nonce_payload, this->nonce_r);
547 message->add_payload(message, &nonce_payload->payload_interface);
548 return NEED_MORE;
549 }
550 case MM_AUTH:
551 {
552 id_payload_t *id_payload;
553 hash_payload_t *hash_payload;
554 identification_t *id;
555 chunk_t hash, dh;
556
557 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
558 if (!id)
559 {
560 DBG1(DBG_CFG, "own identity not known");
561 return FAILED;
562 }
563
564 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
565
566 id_payload = id_payload_create_from_identification(ID_V1, id);
567 message->add_payload(message, &id_payload->payload_interface);
568
569 this->dh->get_my_public_value(this->dh, &dh);
570 hash = this->keymat->get_hash(this->keymat, FALSE,
571 dh, this->dh_value, this->ike_sa->get_id(this->ike_sa),
572 this->sa_payload, id);
573 free(dh.ptr);
574 hash_payload = hash_payload_create();
575 hash_payload->set_hash(hash_payload, hash);
576 free(hash.ptr);
577 message->add_payload(message, &hash_payload->payload_interface);
578
579 /* TODO-IKEv1: check for XAUTH rounds, queue them */
580 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
581 this->ike_sa->get_name(this->ike_sa),
582 this->ike_sa->get_unique_id(this->ike_sa),
583 this->ike_sa->get_my_host(this->ike_sa),
584 this->ike_sa->get_my_id(this->ike_sa),
585 this->ike_sa->get_other_host(this->ike_sa),
586 this->ike_sa->get_other_id(this->ike_sa));
587 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
588 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
589 return SUCCESS;
590 }
591 default:
592 return FAILED;
593 }
594 }
595
596 METHOD(task_t, process_i, status_t,
597 private_main_mode_t *this, message_t *message)
598 {
599 switch (this->state)
600 {
601 case MM_SA:
602 {
603 linked_list_t *list;
604 sa_payload_t *sa_payload;
605
606 sa_payload = (sa_payload_t*)message->get_payload(message,
607 SECURITY_ASSOCIATION_V1);
608 if (!sa_payload)
609 {
610 DBG1(DBG_IKE, "SA payload missing");
611 return FAILED;
612 }
613 list = sa_payload->get_proposals(sa_payload);
614 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
615 list, FALSE);
616 list->destroy_offset(list, offsetof(proposal_t, destroy));
617 if (!this->proposal)
618 {
619 DBG1(DBG_IKE, "no proposal found");
620 return FAILED;
621 }
622 return NEED_MORE;
623 }
624 case MM_KE:
625 {
626 ke_payload_t *ke_payload;
627 nonce_payload_t *nonce_payload;
628
629 ke_payload = (ke_payload_t*)message->get_payload(message,
630 KEY_EXCHANGE_V1);
631 if (!ke_payload)
632 {
633 DBG1(DBG_IKE, "KE payload missing");
634 return FAILED;
635 }
636 this->dh_value = ke_payload->get_key_exchange_data(ke_payload);
637 this->dh_value = chunk_clone(this->dh_value);
638 this->dh->set_other_public_value(this->dh, this->dh_value);
639
640 nonce_payload = (nonce_payload_t*)message->get_payload(message,
641 NONCE_V1);
642 if (!nonce_payload)
643 {
644 DBG1(DBG_IKE, "Nonce payload missing");
645 return FAILED;
646 }
647 this->nonce_r = nonce_payload->get_nonce(nonce_payload);
648
649 if (!derive_keys(this, this->nonce_i, this->nonce_r))
650 {
651 DBG1(DBG_IKE, "key derivation failed");
652 return FAILED;
653 }
654
655 return NEED_MORE;
656 }
657 case MM_AUTH:
658 {
659 id_payload_t *id_payload;
660 hash_payload_t *hash_payload;
661 identification_t *id;
662 chunk_t hash, dh;
663
664 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
665 if (!id_payload)
666 {
667 DBG1(DBG_IKE, "IDir payload missing");
668 return FAILED;
669 }
670 id = id_payload->get_identification(id_payload);
671 if (!id->matches(id, this->other_auth->get(this->other_auth,
672 AUTH_RULE_IDENTITY)))
673 {
674 DBG1(DBG_IKE, "IDir does not match");
675 id->destroy(id);
676 return FAILED;
677 }
678 this->ike_sa->set_other_id(this->ike_sa, id);
679
680 hash_payload = (hash_payload_t*)message->get_payload(message,
681 HASH_V1);
682 if (!hash_payload)
683 {
684 DBG1(DBG_IKE, "hash payload missing");
685 return FAILED;
686 }
687 hash = hash_payload->get_hash(hash_payload);
688 this->dh->get_my_public_value(this->dh, &dh);
689 hash = this->keymat->get_hash(this->keymat, FALSE,
690 this->dh_value, dh, this->ike_sa->get_id(this->ike_sa),
691 this->sa_payload, id);
692 free(dh.ptr);
693 if (!chunk_equals(hash, hash_payload->get_hash(hash_payload)))
694 {
695 DBG1(DBG_IKE, "calculated hash does not match to hash payload");
696 free(hash.ptr);
697 return FAILED;
698 }
699 free(hash.ptr);
700
701 /* TODO-IKEv1: check for XAUTH rounds, queue them */
702 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
703 this->ike_sa->get_name(this->ike_sa),
704 this->ike_sa->get_unique_id(this->ike_sa),
705 this->ike_sa->get_my_host(this->ike_sa),
706 this->ike_sa->get_my_id(this->ike_sa),
707 this->ike_sa->get_other_host(this->ike_sa),
708 this->ike_sa->get_other_id(this->ike_sa));
709 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
710 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
711 return SUCCESS;
712 }
713 default:
714 return FAILED;
715 }
716 }
717
718 METHOD(task_t, get_type, task_type_t,
719 private_main_mode_t *this)
720 {
721 return TASK_MAIN_MODE;
722 }
723
724 METHOD(task_t, migrate, void,
725 private_main_mode_t *this, ike_sa_t *ike_sa)
726 {
727 this->ike_sa = ike_sa;
728 }
729
730 METHOD(task_t, destroy, void,
731 private_main_mode_t *this)
732 {
733 DESTROY_IF(this->peer_cfg);
734 DESTROY_IF(this->proposal);
735 DESTROY_IF(this->dh);
736 free(this->dh_value.ptr);
737 free(this->nonce_i.ptr);
738 free(this->nonce_r.ptr);
739 free(this->sa_payload.ptr);
740 free(this);
741 }
742
743 /*
744 * Described in header.
745 */
746 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
747 {
748 private_main_mode_t *this;
749
750 INIT(this,
751 .public = {
752 .task = {
753 .get_type = _get_type,
754 .migrate = _migrate,
755 .destroy = _destroy,
756 },
757 },
758 .ike_sa = ike_sa,
759 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
760 .initiator = initiator,
761 .state = MM_INIT,
762 );
763
764 if (initiator)
765 {
766 this->public.task.build = _build_i;
767 this->public.task.process = _process_i;
768 }
769 else
770 {
771 this->public.task.build = _build_r;
772 this->public.task.process = _process_r;
773 }
774
775 return &this->public;
776 }