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