Show auth method of config we are looking for in 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 #include <sa/tasks/xauth.h>
32 #include <sa/tasks/mode_config.h>
33 #include <sa/tasks/informational.h>
34
35 typedef struct private_main_mode_t private_main_mode_t;
36
37 /**
38 * Private members of a main_mode_t task.
39 */
40 struct private_main_mode_t {
41
42 /**
43 * Public methods and task_t interface.
44 */
45 main_mode_t public;
46
47 /**
48 * Assigned IKE_SA.
49 */
50 ike_sa_t *ike_sa;
51
52 /**
53 * Are we the initiator?
54 */
55 bool initiator;
56
57 /**
58 * IKE config to establish
59 */
60 ike_cfg_t *ike_cfg;
61
62 /**
63 * Peer config to use
64 */
65 peer_cfg_t *peer_cfg;
66
67 /**
68 * Local authentication configuration
69 */
70 auth_cfg_t *my_auth;
71
72 /**
73 * Remote authentication configuration
74 */
75 auth_cfg_t *other_auth;
76
77 /**
78 * selected IKE proposal
79 */
80 proposal_t *proposal;
81
82 /**
83 * DH exchange
84 */
85 diffie_hellman_t *dh;
86
87 /**
88 * Keymat derivation (from SA)
89 */
90 keymat_v1_t *keymat;
91
92 /**
93 * Received public DH value from peer
94 */
95 chunk_t dh_value;
96
97 /**
98 * Initiators nonce
99 */
100 chunk_t nonce_i;
101
102 /**
103 * Responder nonce
104 */
105 chunk_t nonce_r;
106
107 /**
108 * Encoded SA initiator payload used for authentication
109 */
110 chunk_t sa_payload;
111
112 /**
113 * Negotiated SA lifetime
114 */
115 u_int32_t lifetime;
116
117 /**
118 * Negotiated authentication method
119 */
120 auth_method_t auth_method;
121
122 /** states of main mode */
123 enum {
124 MM_INIT,
125 MM_SA,
126 MM_KE,
127 MM_AUTH,
128 } state;
129 };
130
131 /**
132 * Get the first authentcation config from peer config
133 */
134 static auth_cfg_t *get_auth_cfg(peer_cfg_t *peer_cfg, bool local)
135 {
136 enumerator_t *enumerator;
137 auth_cfg_t *cfg = NULL;
138
139 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
140 enumerator->enumerate(enumerator, &cfg);
141 enumerator->destroy(enumerator);
142 return cfg;
143 }
144
145 /**
146 * Create an authenticator, if supported
147 */
148 static authenticator_t *create_authenticator(private_main_mode_t *this,
149 id_payload_t *id)
150 {
151 authenticator_t *authenticator;
152 authenticator = authenticator_create_v1(this->ike_sa, this->initiator,
153 this->auth_method, this->dh,
154 this->dh_value, this->sa_payload,
155 id->get_encoded(id));
156 if (!authenticator)
157 {
158 DBG1(DBG_IKE, "negotiated authentication method %N not supported",
159 auth_method_names, this->auth_method);
160 }
161 return authenticator;
162 }
163
164 /**
165 * Save the encoded SA payload of a message
166 */
167 static bool save_sa_payload(private_main_mode_t *this, message_t *message)
168 {
169 enumerator_t *enumerator;
170 payload_t *payload, *sa = NULL;
171 chunk_t data;
172 size_t offset = IKE_HEADER_LENGTH;
173
174 enumerator = message->create_payload_enumerator(message);
175 while (enumerator->enumerate(enumerator, &payload))
176 {
177 if (payload->get_type(payload) == SECURITY_ASSOCIATION_V1)
178 {
179 sa = payload;
180 break;
181 }
182 else
183 {
184 offset += payload->get_length(payload);
185 }
186 }
187 enumerator->destroy(enumerator);
188
189 data = message->get_packet_data(message);
190 if (sa && data.len >= offset + sa->get_length(sa))
191 {
192 /* Get SA payload without 4 byte fixed header */
193 data = chunk_skip(data, offset);
194 data.len = sa->get_length(sa);
195 data = chunk_skip(data, 4);
196 this->sa_payload = chunk_clone(data);
197 return TRUE;
198 }
199 return FALSE;
200 }
201
202 /**
203 * Generate and add NONCE, KE payload
204 */
205 static bool add_nonce_ke(private_main_mode_t *this, chunk_t *nonce,
206 message_t *message)
207 {
208 nonce_payload_t *nonce_payload;
209 ke_payload_t *ke_payload;
210 rng_t *rng;
211
212 ke_payload = ke_payload_create_from_diffie_hellman(KEY_EXCHANGE_V1,
213 this->dh);
214 message->add_payload(message, &ke_payload->payload_interface);
215
216 rng = lib->crypto->create_rng(lib->crypto, RNG_WEAK);
217 if (!rng)
218 {
219 DBG1(DBG_IKE, "no RNG found to create nonce");
220 return FALSE;
221 }
222 rng->allocate_bytes(rng, NONCE_SIZE, nonce);
223 rng->destroy(rng);
224
225 nonce_payload = nonce_payload_create(NONCE_V1);
226 nonce_payload->set_nonce(nonce_payload, *nonce);
227 message->add_payload(message, &nonce_payload->payload_interface);
228
229 return TRUE;
230 }
231
232 /**
233 * Extract nonce from NONCE payload, process KE payload
234 */
235 static bool get_nonce_ke(private_main_mode_t *this, chunk_t *nonce,
236 message_t *message)
237 {
238 nonce_payload_t *nonce_payload;
239 ke_payload_t *ke_payload;
240
241 ke_payload = (ke_payload_t*)message->get_payload(message, KEY_EXCHANGE_V1);
242 if (!ke_payload)
243 {
244 DBG1(DBG_IKE, "KE payload missing in message");
245 return FALSE;
246 }
247 this->dh_value = chunk_clone(ke_payload->get_key_exchange_data(ke_payload));
248 this->dh->set_other_public_value(this->dh, this->dh_value);
249
250 nonce_payload = (nonce_payload_t*)message->get_payload(message, NONCE_V1);
251 if (!nonce_payload)
252 {
253 DBG1(DBG_IKE, "NONCE payload missing in message");
254 return FALSE;
255 }
256 *nonce = nonce_payload->get_nonce(nonce_payload);
257
258 return TRUE;
259 }
260
261 /**
262 * Get the two auth classes from local or remote config
263 */
264 static void get_auth_class(peer_cfg_t *peer_cfg, bool local,
265 auth_class_t *c1, auth_class_t *c2)
266 {
267 enumerator_t *enumerator;
268 auth_cfg_t *auth;
269
270 *c1 = *c2 = AUTH_CLASS_ANY;
271
272 enumerator = peer_cfg->create_auth_cfg_enumerator(peer_cfg, local);
273 while (enumerator->enumerate(enumerator, &auth))
274 {
275 if (*c1 == AUTH_CLASS_ANY)
276 {
277 *c1 = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
278 }
279 else
280 {
281 *c2 = (uintptr_t)auth->get(auth, AUTH_RULE_AUTH_CLASS);
282 break;
283 }
284 }
285 enumerator->destroy(enumerator);
286 }
287
288 /**
289 * Get auth method to use from a peer config
290 */
291 static auth_method_t get_auth_method(private_main_mode_t *this,
292 peer_cfg_t *peer_cfg)
293 {
294 auth_class_t i1, i2, r1, r2;
295
296 get_auth_class(peer_cfg, this->initiator, &i1, &i2);
297 get_auth_class(peer_cfg, !this->initiator, &r1, &r2);
298
299 if (i1 == AUTH_CLASS_PUBKEY && r1 == AUTH_CLASS_PUBKEY)
300 {
301 if (i2 == AUTH_CLASS_ANY && r2 == AUTH_CLASS_ANY)
302 {
303 /* TODO-IKEv1: ECDSA? */
304 return AUTH_RSA;
305 }
306 if (i2 == AUTH_CLASS_XAUTH)
307 {
308 return AUTH_XAUTH_INIT_RSA;
309 }
310 if (r2 == AUTH_CLASS_XAUTH)
311 {
312 return AUTH_XAUTH_RESP_RSA;
313 }
314 }
315 if (i1 == AUTH_CLASS_PSK && r1 == AUTH_CLASS_PSK)
316 {
317 if (i2 == AUTH_CLASS_ANY && r2 == AUTH_CLASS_ANY)
318 {
319 return AUTH_PSK;
320 }
321 if (i2 == AUTH_CLASS_XAUTH)
322 {
323 return AUTH_XAUTH_INIT_PSK;
324 }
325 if (r2 == AUTH_CLASS_XAUTH)
326 {
327 return AUTH_XAUTH_RESP_PSK;
328 }
329 }
330 if (i1 == AUTH_CLASS_XAUTH && r1 == AUTH_CLASS_PUBKEY &&
331 i2 == AUTH_CLASS_ANY && r2 == AUTH_CLASS_ANY)
332 {
333 return AUTH_HYBRID_INIT_RSA;
334 }
335 return AUTH_NONE;;
336 }
337
338 /**
339 * Select the best configuration as responder
340 */
341 static peer_cfg_t *select_config(private_main_mode_t *this, identification_t *id)
342 {
343 enumerator_t *enumerator;
344 peer_cfg_t *current, *found = NULL;
345 host_t *me, *other;
346
347 me = this->ike_sa->get_my_host(this->ike_sa);
348 other = this->ike_sa->get_other_host(this->ike_sa);
349 DBG1(DBG_CFG, "looking for %N peer configs matching %H...%H[%Y]",
350 auth_method_names, this->auth_method, me, other, id);
351 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends,
352 me, other, NULL, id);
353 while (enumerator->enumerate(enumerator, &current))
354 {
355 if (get_auth_method(this, current) == this->auth_method)
356 {
357 found = current->get_ref(current);
358 break;
359 }
360 }
361 enumerator->destroy(enumerator);
362
363 if (found)
364 {
365 DBG2(DBG_CFG, "selected peer config \"%s\"", found->get_name(found));
366 }
367 return found;
368 }
369
370 /**
371 * Check for notify errors, return TRUE if error found
372 */
373 static bool has_notify_errors(private_main_mode_t *this, message_t *message)
374 {
375 enumerator_t *enumerator;
376 payload_t *payload;
377 bool err = FALSE;
378
379 enumerator = message->create_payload_enumerator(message);
380 while (enumerator->enumerate(enumerator, &payload))
381 {
382 if (payload->get_type(payload) == NOTIFY_V1)
383 {
384 notify_payload_t *notify;
385 notify_type_t type;
386
387 notify = (notify_payload_t*)payload;
388 type = notify->get_notify_type(notify);
389 if (type < 16384)
390 {
391 DBG1(DBG_IKE, "received %N error notify",
392 notify_type_names, type);
393 err = TRUE;
394 }
395 else if (type == INITIAL_CONTACT_IKEV1)
396 {
397 if (!this->initiator && this->state == MM_AUTH)
398 {
399 /* If authenticated and received INITIAL_CONTACT,
400 * delete any existing IKE_SAs with that peer.
401 * The delete takes place when the SA is checked in due
402 * to other id not known until the 3rd message.*/
403 this->ike_sa->set_condition(this->ike_sa,
404 COND_INIT_CONTACT_SEEN, TRUE);
405 }
406 }
407 else
408 {
409 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
410 }
411 }
412 }
413 enumerator->destroy(enumerator);
414
415 return err;
416 }
417
418 /**
419 * Queue a task sending a notify in an INFORMATIONAL exchange
420 */
421 static status_t send_notify(private_main_mode_t *this,
422 notify_type_t type, chunk_t data)
423 {
424 notify_payload_t *notify;
425 ike_sa_id_t *ike_sa_id;
426 u_int64_t spi_i, spi_r;
427 chunk_t spi;
428
429 notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1,
430 PROTO_IKE, type);
431 notify->set_notification_data(notify, data);
432 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
433 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
434 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
435 spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
436 notify->set_spi_data(notify, spi);
437
438 this->ike_sa->queue_task(this->ike_sa,
439 (task_t*)informational_create(this->ike_sa, notify));
440 /* cancel all active/passive tasks in favour of informational */
441 return ALREADY_DONE;
442 }
443
444 METHOD(task_t, build_i, status_t,
445 private_main_mode_t *this, message_t *message)
446 {
447 switch (this->state)
448 {
449 case MM_INIT:
450 {
451 sa_payload_t *sa_payload;
452 linked_list_t *proposals;
453 packet_t *packet;
454
455 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
456 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
457 this->ike_sa->get_name(this->ike_sa),
458 this->ike_sa->get_unique_id(this->ike_sa),
459 this->ike_sa->get_other_host(this->ike_sa));
460 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
461
462 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
463 this->peer_cfg->get_ref(this->peer_cfg);
464
465 this->my_auth = get_auth_cfg(this->peer_cfg, TRUE);
466 this->other_auth = get_auth_cfg(this->peer_cfg, FALSE);
467 if (!this->my_auth || !this->other_auth)
468 {
469 DBG1(DBG_CFG, "no auth config found");
470 return FAILED;
471 }
472 this->auth_method = get_auth_method(this, this->peer_cfg);
473 if (this->auth_method == AUTH_NONE)
474 {
475 DBG1(DBG_CFG, "configuration uses unsupported authentication");
476 return FAILED;
477 }
478 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
479 FALSE);
480 if (!this->lifetime)
481 { /* fall back to rekey time of no rekey time configured */
482 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
483 FALSE);
484 }
485 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
486 sa_payload = sa_payload_create_from_proposals_v1(proposals,
487 this->lifetime, 0, this->auth_method, MODE_NONE, FALSE);
488 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
489
490 message->add_payload(message, &sa_payload->payload_interface);
491
492 /* pregenerate message to store SA payload */
493 if (this->ike_sa->generate_message(this->ike_sa, message,
494 &packet) != SUCCESS)
495 {
496 DBG1(DBG_IKE, "pregenerating SA payload failed");
497 return FAILED;
498 }
499 packet->destroy(packet);
500 if (!save_sa_payload(this, message))
501 {
502 DBG1(DBG_IKE, "SA payload invalid");
503 return FAILED;
504 }
505
506 this->state = MM_SA;
507 return NEED_MORE;
508 }
509 case MM_SA:
510 {
511 u_int16_t group;
512
513 if (!this->keymat->create_hasher(this->keymat, this->proposal))
514 {
515 return FAILED;
516 }
517 if (!this->proposal->get_algorithm(this->proposal,
518 DIFFIE_HELLMAN_GROUP, &group, NULL))
519 {
520 DBG1(DBG_IKE, "DH group selection failed");
521 return FAILED;
522 }
523 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
524 group);
525 if (!this->dh)
526 {
527 DBG1(DBG_IKE, "negotiated DH group not supported");
528 return FAILED;
529 }
530 if (!add_nonce_ke(this, &this->nonce_i, message))
531 {
532 return FAILED;
533 }
534 this->state = MM_KE;
535 return NEED_MORE;
536 }
537 case MM_KE:
538 {
539 authenticator_t *authenticator;
540 id_payload_t *id_payload;
541 identification_t *id;
542
543 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
544 if (!id)
545 {
546 DBG1(DBG_CFG, "own identity not known");
547 return FAILED;
548 }
549
550 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
551
552 id_payload = id_payload_create_from_identification(ID_V1, id);
553 message->add_payload(message, &id_payload->payload_interface);
554
555 authenticator = create_authenticator(this, id_payload);
556 if (!authenticator || authenticator->build(authenticator,
557 message) != SUCCESS)
558 {
559 DESTROY_IF(authenticator);
560 return FAILED;
561 }
562 authenticator->destroy(authenticator);
563
564 this->state = MM_AUTH;
565 return NEED_MORE;
566 }
567 default:
568 return FAILED;
569 }
570 }
571
572 METHOD(task_t, process_r, status_t,
573 private_main_mode_t *this, message_t *message)
574 {
575 switch (this->state)
576 {
577 case MM_INIT:
578 {
579 linked_list_t *list;
580 sa_payload_t *sa_payload;
581
582 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
583 DBG0(DBG_IKE, "%H is initiating a Main Mode",
584 message->get_source(message));
585 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
586
587 this->ike_sa->update_hosts(this->ike_sa,
588 message->get_destination(message),
589 message->get_source(message), TRUE);
590
591 sa_payload = (sa_payload_t*)message->get_payload(message,
592 SECURITY_ASSOCIATION_V1);
593 if (!sa_payload || !save_sa_payload(this, message))
594 {
595 DBG1(DBG_IKE, "SA payload missing or invalid");
596 return FAILED;
597 }
598
599 list = sa_payload->get_proposals(sa_payload);
600 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
601 list, FALSE);
602 list->destroy_offset(list, offsetof(proposal_t, destroy));
603 if (!this->proposal)
604 {
605 DBG1(DBG_IKE, "no proposal found");
606 return send_notify(this, NO_PROPOSAL_CHOSEN, chunk_empty);
607 }
608
609 this->auth_method = sa_payload->get_auth_method(sa_payload);
610 this->lifetime = sa_payload->get_lifetime(sa_payload);
611
612 this->state = MM_SA;
613 return NEED_MORE;
614 }
615 case MM_SA:
616 {
617 u_int16_t group;
618
619 if (!this->keymat->create_hasher(this->keymat, this->proposal))
620 {
621 return FAILED;
622 }
623 if (!this->proposal->get_algorithm(this->proposal,
624 DIFFIE_HELLMAN_GROUP, &group, NULL))
625 {
626 DBG1(DBG_IKE, "DH group selection failed");
627 return FAILED;
628 }
629 this->dh = lib->crypto->create_dh(lib->crypto, group);
630 if (!this->dh)
631 {
632 DBG1(DBG_IKE, "negotiated DH group not supported");
633 return FAILED;
634 }
635 if (!get_nonce_ke(this, &this->nonce_i, message))
636 {
637 return FAILED;
638 }
639 this->state = MM_KE;
640 return NEED_MORE;
641 }
642 case MM_KE:
643 {
644 authenticator_t *authenticator;
645 id_payload_t *id_payload;
646 identification_t *id;
647
648 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
649 if (!id_payload)
650 {
651 DBG1(DBG_IKE, "IDii payload missing");
652 return FAILED;
653 }
654
655 id = id_payload->get_identification(id_payload);
656 this->ike_sa->set_other_id(this->ike_sa, id);
657 this->peer_cfg = select_config(this, id);
658 if (!this->peer_cfg)
659 {
660 DBG1(DBG_IKE, "no peer config found");
661 return send_notify(this, AUTHENTICATION_FAILED, chunk_empty);
662 }
663 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
664
665 this->my_auth = get_auth_cfg(this->peer_cfg, TRUE);
666 this->other_auth = get_auth_cfg(this->peer_cfg, FALSE);
667 if (!this->my_auth || !this->other_auth)
668 {
669 DBG1(DBG_IKE, "auth config missing");
670 return send_notify(this, AUTHENTICATION_FAILED, chunk_empty);
671 }
672
673 authenticator = create_authenticator(this, id_payload);
674 if (!authenticator || authenticator->process(authenticator,
675 message) != SUCCESS)
676 {
677 DESTROY_IF(authenticator);
678 return send_notify(this, AUTHENTICATION_FAILED, chunk_empty);
679 }
680 authenticator->destroy(authenticator);
681
682 this->state = MM_AUTH;
683 if (has_notify_errors(this, message))
684 {
685 return FAILED;
686 }
687 return NEED_MORE;
688 }
689 default:
690 return FAILED;
691 }
692 }
693
694 /**
695 * Lookup a shared secret for this IKE_SA
696 */
697 static shared_key_t *lookup_shared_key(private_main_mode_t *this)
698 {
699 host_t *me, *other;
700 identification_t *my_id, *other_id;
701 shared_key_t *shared_key = NULL;
702
703 /* try to get a PSK for IP addresses */
704 me = this->ike_sa->get_my_host(this->ike_sa);
705 other = this->ike_sa->get_other_host(this->ike_sa);
706 my_id = identification_create_from_sockaddr(me->get_sockaddr(me));
707 other_id = identification_create_from_sockaddr(other->get_sockaddr(other));
708 if (my_id && other_id)
709 {
710 shared_key = lib->credmgr->get_shared(lib->credmgr, SHARED_IKE,
711 my_id, other_id);
712 }
713 DESTROY_IF(my_id);
714 DESTROY_IF(other_id);
715 if (shared_key)
716 {
717 return shared_key;
718 }
719
720 if (this->my_auth && this->other_auth)
721 { /* as initiator, use identities from configuraiton */
722 my_id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
723 other_id = this->other_auth->get(this->other_auth, AUTH_RULE_IDENTITY);
724 if (my_id && other_id)
725 {
726 shared_key = lib->credmgr->get_shared(lib->credmgr, SHARED_IKE,
727 my_id, other_id);
728 }
729 else
730 {
731 DBG1(DBG_IKE, "no shared key found for '%Y'[%H] - '%Y'[%H]",
732 my_id, me, other_id, other);
733 }
734 }
735 else
736 { /* as responder, we try to find a config by IP */
737 enumerator_t *enumerator;
738 auth_cfg_t *my_auth, *other_auth;
739 peer_cfg_t *peer_cfg = NULL;
740
741 enumerator = charon->backends->create_peer_cfg_enumerator(
742 charon->backends, me, other, NULL, NULL);
743 while (enumerator->enumerate(enumerator, &peer_cfg))
744 {
745 my_auth = get_auth_cfg(peer_cfg, TRUE);
746 other_auth = get_auth_cfg(peer_cfg, FALSE);
747 if (my_auth && other_auth)
748 {
749 my_id = my_auth->get(my_auth, AUTH_RULE_IDENTITY);
750 other_id = other_auth->get(other_auth, AUTH_RULE_IDENTITY);
751 if (my_id && other_id)
752 {
753 shared_key = lib->credmgr->get_shared(lib->credmgr,
754 SHARED_IKE, my_id, other_id);
755 if (shared_key)
756 {
757 break;
758 }
759 else
760 {
761 DBG1(DBG_IKE, "no shared key found for "
762 "'%Y'[%H] - '%Y'[%H]", my_id, me, other_id, other);
763 }
764 }
765 }
766 }
767 enumerator->destroy(enumerator);
768 if (!peer_cfg)
769 {
770 DBG1(DBG_IKE, "no shared key found for %H - %H", me, other);
771 }
772 }
773 return shared_key;
774 }
775
776 /**
777 * Derive key material for this IKE_SA
778 */
779 static bool derive_keys(private_main_mode_t *this, chunk_t nonce_i,
780 chunk_t nonce_r)
781 {
782 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
783 shared_key_t *shared_key = NULL;
784
785 switch (this->auth_method)
786 {
787 case AUTH_PSK:
788 case AUTH_XAUTH_INIT_PSK:
789 case AUTH_XAUTH_RESP_PSK:
790 shared_key = lookup_shared_key(this);
791 break;
792 default:
793 break;
794 }
795 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
796 this->dh_value, nonce_i, nonce_r, id, this->auth_method, shared_key))
797 {
798 DESTROY_IF(shared_key);
799 DBG1(DBG_IKE, "key derivation for %N failed",
800 auth_method_names, this->auth_method);
801 return FALSE;
802 }
803 DESTROY_IF(shared_key);
804 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, nonce_i, nonce_r,
805 NULL);
806
807 return TRUE;
808 }
809
810 /**
811 * Set IKE_SA to established state
812 */
813 static void establish(private_main_mode_t *this)
814 {
815 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
816 this->ike_sa->get_name(this->ike_sa),
817 this->ike_sa->get_unique_id(this->ike_sa),
818 this->ike_sa->get_my_host(this->ike_sa),
819 this->ike_sa->get_my_id(this->ike_sa),
820 this->ike_sa->get_other_host(this->ike_sa),
821 this->ike_sa->get_other_id(this->ike_sa));
822
823 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
824 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
825 }
826
827 METHOD(task_t, build_r, status_t,
828 private_main_mode_t *this, message_t *message)
829 {
830 switch (this->state)
831 {
832 case MM_SA:
833 {
834 sa_payload_t *sa_payload;
835
836 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
837 this->lifetime, 0, this->auth_method, MODE_NONE, FALSE);
838 message->add_payload(message, &sa_payload->payload_interface);
839
840 return NEED_MORE;
841 }
842 case MM_KE:
843 {
844 if (!add_nonce_ke(this, &this->nonce_r, message))
845 {
846 return FAILED;
847 }
848 if (!derive_keys(this, this->nonce_i, this->nonce_r))
849 {
850 return FAILED;
851 }
852 return NEED_MORE;
853 }
854 case MM_AUTH:
855 {
856 authenticator_t *authenticator;
857 id_payload_t *id_payload;
858 identification_t *id;
859
860 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
861 if (!id)
862 {
863 DBG1(DBG_CFG, "own identity not known");
864 return FAILED;
865 }
866
867 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
868
869 id_payload = id_payload_create_from_identification(ID_V1, id);
870 message->add_payload(message, &id_payload->payload_interface);
871
872 authenticator = create_authenticator(this, id_payload);
873 if (!authenticator || authenticator->build(authenticator,
874 message) != SUCCESS)
875 {
876 DESTROY_IF(authenticator);
877 return FAILED;
878 }
879 authenticator->destroy(authenticator);
880
881 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
882 {
883 this->ike_sa->queue_task(this->ike_sa,
884 (task_t*)mode_config_create(this->ike_sa, TRUE));
885 }
886
887 switch (this->auth_method)
888 {
889 case AUTH_XAUTH_INIT_PSK:
890 case AUTH_XAUTH_INIT_RSA:
891 case AUTH_HYBRID_INIT_RSA:
892 this->ike_sa->queue_task(this->ike_sa,
893 (task_t*)xauth_create(this->ike_sa, TRUE));
894 return SUCCESS;
895 case AUTH_XAUTH_RESP_PSK:
896 case AUTH_XAUTH_RESP_RSA:
897 case AUTH_HYBRID_RESP_RSA:
898 /* TODO-IKEv1: not yet supported */
899 return FAILED;
900 default:
901 establish(this);
902 return SUCCESS;
903 }
904 }
905 default:
906 return FAILED;
907 }
908 }
909
910 METHOD(task_t, process_i, status_t,
911 private_main_mode_t *this, message_t *message)
912 {
913 switch (this->state)
914 {
915 case MM_SA:
916 {
917 linked_list_t *list;
918 sa_payload_t *sa_payload;
919 auth_method_t auth_method;
920 u_int32_t lifetime;
921
922 sa_payload = (sa_payload_t*)message->get_payload(message,
923 SECURITY_ASSOCIATION_V1);
924 if (!sa_payload)
925 {
926 DBG1(DBG_IKE, "SA payload missing");
927 return FAILED;
928 }
929 list = sa_payload->get_proposals(sa_payload);
930 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
931 list, FALSE);
932 list->destroy_offset(list, offsetof(proposal_t, destroy));
933 if (!this->proposal)
934 {
935 DBG1(DBG_IKE, "no proposal found");
936 return FAILED;
937 }
938
939 lifetime = sa_payload->get_lifetime(sa_payload);
940 if (lifetime != this->lifetime)
941 {
942 DBG1(DBG_IKE, "received lifetime %us does not match configured "
943 "%us, using lower value", lifetime, this->lifetime);
944 }
945 this->lifetime = min(this->lifetime, lifetime);
946 auth_method = sa_payload->get_auth_method(sa_payload);
947 if (auth_method != this->auth_method)
948 {
949 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
950 "continue with configured", auth_method_names, auth_method,
951 auth_method_names, this->auth_method);
952 }
953 return NEED_MORE;
954 }
955 case MM_KE:
956 {
957 if (!get_nonce_ke(this, &this->nonce_r, message))
958 {
959 return FAILED;
960 }
961 if (!derive_keys(this, this->nonce_i, this->nonce_r))
962 {
963 return FAILED;
964 }
965 return NEED_MORE;
966 }
967 case MM_AUTH:
968 {
969 authenticator_t *authenticator;
970 id_payload_t *id_payload;
971 identification_t *id;
972
973 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
974 if (!id_payload)
975 {
976 DBG1(DBG_IKE, "IDir payload missing");
977 return FAILED;
978 }
979 id = id_payload->get_identification(id_payload);
980 if (!id->matches(id, this->other_auth->get(this->other_auth,
981 AUTH_RULE_IDENTITY)))
982 {
983 DBG1(DBG_IKE, "IDir does not match");
984 id->destroy(id);
985 return FAILED;
986 }
987 this->ike_sa->set_other_id(this->ike_sa, id);
988
989 authenticator = create_authenticator(this, id_payload);
990 if (!authenticator || authenticator->process(authenticator,
991 message) != SUCCESS)
992 {
993 DESTROY_IF(authenticator);
994 return FAILED;
995 }
996 authenticator->destroy(authenticator);
997
998 switch (this->auth_method)
999 {
1000 case AUTH_XAUTH_INIT_PSK:
1001 case AUTH_XAUTH_INIT_RSA:
1002 case AUTH_HYBRID_INIT_RSA:
1003 /* wait for XAUTH request */
1004 return SUCCESS;
1005 case AUTH_XAUTH_RESP_PSK:
1006 case AUTH_XAUTH_RESP_RSA:
1007 case AUTH_HYBRID_RESP_RSA:
1008 /* TODO-IKEv1: not yet */
1009 return FAILED;
1010 default:
1011 establish(this);
1012 return SUCCESS;
1013 }
1014 }
1015 default:
1016 return FAILED;
1017 }
1018 }
1019
1020 METHOD(task_t, get_type, task_type_t,
1021 private_main_mode_t *this)
1022 {
1023 return TASK_MAIN_MODE;
1024 }
1025
1026 METHOD(task_t, migrate, void,
1027 private_main_mode_t *this, ike_sa_t *ike_sa)
1028 {
1029 this->ike_sa = ike_sa;
1030 }
1031
1032 METHOD(task_t, destroy, void,
1033 private_main_mode_t *this)
1034 {
1035 DESTROY_IF(this->peer_cfg);
1036 DESTROY_IF(this->proposal);
1037 DESTROY_IF(this->dh);
1038 free(this->dh_value.ptr);
1039 free(this->nonce_i.ptr);
1040 free(this->nonce_r.ptr);
1041 free(this->sa_payload.ptr);
1042 free(this);
1043 }
1044
1045 /*
1046 * Described in header.
1047 */
1048 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
1049 {
1050 private_main_mode_t *this;
1051
1052 INIT(this,
1053 .public = {
1054 .task = {
1055 .get_type = _get_type,
1056 .migrate = _migrate,
1057 .destroy = _destroy,
1058 },
1059 },
1060 .ike_sa = ike_sa,
1061 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
1062 .initiator = initiator,
1063 .state = MM_INIT,
1064 );
1065
1066 if (initiator)
1067 {
1068 this->public.task.build = _build_i;
1069 this->public.task.process = _process_i;
1070 }
1071 else
1072 {
1073 this->public.task.build = _build_r;
1074 this->public.task.process = _process_r;
1075 }
1076
1077 return &this->public;
1078 }