0582c33d5360760285d9343b26ba0789e96d7685
[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 * Check if a peer skipped authentication by using Hybrid authentication
340 */
341 static bool skipped_auth(private_main_mode_t *this, bool local)
342 {
343 bool initiator;
344
345 initiator = local == this->initiator;
346 if (initiator && this->auth_method == AUTH_HYBRID_INIT_RSA)
347 {
348 return TRUE;
349 }
350 if (!initiator && this->auth_method == AUTH_HYBRID_RESP_RSA)
351 {
352 return TRUE;
353 }
354 return FALSE;
355 }
356
357 /**
358 * Check if remote authentication constraints fulfilled
359 */
360 static bool check_constraints(private_main_mode_t *this)
361 {
362 identification_t *id;
363 auth_cfg_t *auth;
364
365 auth = this->ike_sa->get_auth_cfg(this->ike_sa, FALSE);
366 /* auth identity to comply */
367 id = this->ike_sa->get_other_id(this->ike_sa);
368 auth->add(auth, AUTH_RULE_IDENTITY, id->clone(id));
369 if (skipped_auth(this, FALSE))
370 {
371 return TRUE;
372 }
373 return auth->complies(auth, this->other_auth, TRUE);
374 }
375
376 /**
377 * Save authentication information after authentication succeeded
378 */
379 static void save_auth_cfg(private_main_mode_t *this, bool local)
380 {
381 auth_cfg_t *auth;
382
383 if (skipped_auth(this, local))
384 {
385 return;
386 }
387 auth = auth_cfg_create();
388 /* for local config, we _copy_ entires from the config, as it contains
389 * certificates we must send later. */
390 auth->merge(auth, this->ike_sa->get_auth_cfg(this->ike_sa, local), local);
391 this->ike_sa->add_auth_cfg(this->ike_sa, local, auth);
392 }
393
394 /**
395 * Select the best configuration as responder
396 */
397 static peer_cfg_t *select_config(private_main_mode_t *this, identification_t *id)
398 {
399 enumerator_t *enumerator;
400 peer_cfg_t *current, *found = NULL;
401 host_t *me, *other;
402
403 me = this->ike_sa->get_my_host(this->ike_sa);
404 other = this->ike_sa->get_other_host(this->ike_sa);
405 DBG1(DBG_CFG, "looking for %N peer configs matching %H...%H[%Y]",
406 auth_method_names, this->auth_method, me, other, id);
407 enumerator = charon->backends->create_peer_cfg_enumerator(charon->backends,
408 me, other, NULL, id);
409 while (enumerator->enumerate(enumerator, &current))
410 {
411 if (get_auth_method(this, current) == this->auth_method)
412 {
413 found = current->get_ref(current);
414 break;
415 }
416 }
417 enumerator->destroy(enumerator);
418
419 if (found)
420 {
421 DBG2(DBG_CFG, "selected peer config \"%s\"", found->get_name(found));
422 }
423 return found;
424 }
425
426 /**
427 * Check for notify errors, return TRUE if error found
428 */
429 static bool has_notify_errors(private_main_mode_t *this, message_t *message)
430 {
431 enumerator_t *enumerator;
432 payload_t *payload;
433 bool err = FALSE;
434
435 enumerator = message->create_payload_enumerator(message);
436 while (enumerator->enumerate(enumerator, &payload))
437 {
438 if (payload->get_type(payload) == NOTIFY_V1)
439 {
440 notify_payload_t *notify;
441 notify_type_t type;
442
443 notify = (notify_payload_t*)payload;
444 type = notify->get_notify_type(notify);
445 if (type < 16384)
446 {
447 DBG1(DBG_IKE, "received %N error notify",
448 notify_type_names, type);
449 err = TRUE;
450 }
451 else if (type == INITIAL_CONTACT_IKEV1)
452 {
453 if (!this->initiator && this->state == MM_AUTH)
454 {
455 /* If authenticated and received INITIAL_CONTACT,
456 * delete any existing IKE_SAs with that peer.
457 * The delete takes place when the SA is checked in due
458 * to other id not known until the 3rd message.*/
459 this->ike_sa->set_condition(this->ike_sa,
460 COND_INIT_CONTACT_SEEN, TRUE);
461 }
462 }
463 else
464 {
465 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
466 }
467 }
468 }
469 enumerator->destroy(enumerator);
470
471 return err;
472 }
473
474 /**
475 * Queue a task sending a notify in an INFORMATIONAL exchange
476 */
477 static status_t send_notify(private_main_mode_t *this,
478 notify_type_t type, chunk_t data)
479 {
480 notify_payload_t *notify;
481 ike_sa_id_t *ike_sa_id;
482 u_int64_t spi_i, spi_r;
483 chunk_t spi;
484
485 notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1,
486 PROTO_IKE, type);
487 notify->set_notification_data(notify, data);
488 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
489 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
490 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
491 spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
492 notify->set_spi_data(notify, spi);
493
494 this->ike_sa->queue_task(this->ike_sa,
495 (task_t*)informational_create(this->ike_sa, notify));
496 /* cancel all active/passive tasks in favour of informational */
497 return ALREADY_DONE;
498 }
499
500 METHOD(task_t, build_i, status_t,
501 private_main_mode_t *this, message_t *message)
502 {
503 switch (this->state)
504 {
505 case MM_INIT:
506 {
507 sa_payload_t *sa_payload;
508 linked_list_t *proposals;
509 packet_t *packet;
510
511 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
512 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
513 this->ike_sa->get_name(this->ike_sa),
514 this->ike_sa->get_unique_id(this->ike_sa),
515 this->ike_sa->get_other_host(this->ike_sa));
516 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
517
518 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
519 this->peer_cfg->get_ref(this->peer_cfg);
520
521 this->my_auth = get_auth_cfg(this->peer_cfg, TRUE);
522 this->other_auth = get_auth_cfg(this->peer_cfg, FALSE);
523 if (!this->my_auth || !this->other_auth)
524 {
525 DBG1(DBG_CFG, "no auth config found");
526 return FAILED;
527 }
528 this->auth_method = get_auth_method(this, this->peer_cfg);
529 if (this->auth_method == AUTH_NONE)
530 {
531 DBG1(DBG_CFG, "configuration uses unsupported authentication");
532 return FAILED;
533 }
534 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
535 FALSE);
536 if (!this->lifetime)
537 { /* fall back to rekey time of no rekey time configured */
538 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
539 FALSE);
540 }
541 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
542 sa_payload = sa_payload_create_from_proposals_v1(proposals,
543 this->lifetime, 0, this->auth_method, MODE_NONE, FALSE);
544 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
545
546 message->add_payload(message, &sa_payload->payload_interface);
547
548 /* pregenerate message to store SA payload */
549 if (this->ike_sa->generate_message(this->ike_sa, message,
550 &packet) != SUCCESS)
551 {
552 DBG1(DBG_IKE, "pregenerating SA payload failed");
553 return FAILED;
554 }
555 packet->destroy(packet);
556 if (!save_sa_payload(this, message))
557 {
558 DBG1(DBG_IKE, "SA payload invalid");
559 return FAILED;
560 }
561
562 this->state = MM_SA;
563 return NEED_MORE;
564 }
565 case MM_SA:
566 {
567 u_int16_t group;
568
569 if (!this->keymat->create_hasher(this->keymat, this->proposal))
570 {
571 return FAILED;
572 }
573 if (!this->proposal->get_algorithm(this->proposal,
574 DIFFIE_HELLMAN_GROUP, &group, NULL))
575 {
576 DBG1(DBG_IKE, "DH group selection failed");
577 return FAILED;
578 }
579 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
580 group);
581 if (!this->dh)
582 {
583 DBG1(DBG_IKE, "negotiated DH group not supported");
584 return FAILED;
585 }
586 if (!add_nonce_ke(this, &this->nonce_i, message))
587 {
588 return FAILED;
589 }
590 this->state = MM_KE;
591 return NEED_MORE;
592 }
593 case MM_KE:
594 {
595 authenticator_t *authenticator;
596 id_payload_t *id_payload;
597 identification_t *id;
598
599 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
600 if (!id)
601 {
602 DBG1(DBG_CFG, "own identity not known");
603 return FAILED;
604 }
605
606 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
607
608 id_payload = id_payload_create_from_identification(ID_V1, id);
609 message->add_payload(message, &id_payload->payload_interface);
610
611 authenticator = create_authenticator(this, id_payload);
612 if (!authenticator || authenticator->build(authenticator,
613 message) != SUCCESS)
614 {
615 DESTROY_IF(authenticator);
616 return FAILED;
617 }
618 authenticator->destroy(authenticator);
619 save_auth_cfg(this, TRUE);
620
621 this->state = MM_AUTH;
622 return NEED_MORE;
623 }
624 default:
625 return FAILED;
626 }
627 }
628
629 METHOD(task_t, process_r, status_t,
630 private_main_mode_t *this, message_t *message)
631 {
632 switch (this->state)
633 {
634 case MM_INIT:
635 {
636 linked_list_t *list;
637 sa_payload_t *sa_payload;
638
639 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
640 DBG0(DBG_IKE, "%H is initiating a Main Mode",
641 message->get_source(message));
642 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
643
644 this->ike_sa->update_hosts(this->ike_sa,
645 message->get_destination(message),
646 message->get_source(message), TRUE);
647
648 sa_payload = (sa_payload_t*)message->get_payload(message,
649 SECURITY_ASSOCIATION_V1);
650 if (!sa_payload || !save_sa_payload(this, message))
651 {
652 DBG1(DBG_IKE, "SA payload missing or invalid");
653 return FAILED;
654 }
655
656 list = sa_payload->get_proposals(sa_payload);
657 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
658 list, FALSE);
659 list->destroy_offset(list, offsetof(proposal_t, destroy));
660 if (!this->proposal)
661 {
662 DBG1(DBG_IKE, "no proposal found");
663 return send_notify(this, NO_PROPOSAL_CHOSEN, chunk_empty);
664 }
665
666 this->auth_method = sa_payload->get_auth_method(sa_payload);
667 this->lifetime = sa_payload->get_lifetime(sa_payload);
668
669 this->state = MM_SA;
670 return NEED_MORE;
671 }
672 case MM_SA:
673 {
674 u_int16_t group;
675
676 if (!this->keymat->create_hasher(this->keymat, this->proposal))
677 {
678 return FAILED;
679 }
680 if (!this->proposal->get_algorithm(this->proposal,
681 DIFFIE_HELLMAN_GROUP, &group, NULL))
682 {
683 DBG1(DBG_IKE, "DH group selection failed");
684 return FAILED;
685 }
686 this->dh = lib->crypto->create_dh(lib->crypto, group);
687 if (!this->dh)
688 {
689 DBG1(DBG_IKE, "negotiated DH group not supported");
690 return FAILED;
691 }
692 if (!get_nonce_ke(this, &this->nonce_i, message))
693 {
694 return FAILED;
695 }
696 this->state = MM_KE;
697 return NEED_MORE;
698 }
699 case MM_KE:
700 {
701 authenticator_t *authenticator;
702 id_payload_t *id_payload;
703 identification_t *id;
704
705 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
706 if (!id_payload)
707 {
708 DBG1(DBG_IKE, "IDii payload missing");
709 return FAILED;
710 }
711
712 id = id_payload->get_identification(id_payload);
713 this->ike_sa->set_other_id(this->ike_sa, id);
714 this->peer_cfg = select_config(this, id);
715 if (!this->peer_cfg)
716 {
717 DBG1(DBG_IKE, "no peer config found");
718 return send_notify(this, AUTHENTICATION_FAILED, chunk_empty);
719 }
720 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
721
722 this->my_auth = get_auth_cfg(this->peer_cfg, TRUE);
723 this->other_auth = get_auth_cfg(this->peer_cfg, FALSE);
724 if (!this->my_auth || !this->other_auth)
725 {
726 DBG1(DBG_IKE, "auth config missing");
727 return send_notify(this, AUTHENTICATION_FAILED, chunk_empty);
728 }
729
730 authenticator = create_authenticator(this, id_payload);
731 if (!authenticator || authenticator->process(authenticator,
732 message) != SUCCESS)
733 {
734 DESTROY_IF(authenticator);
735 return send_notify(this, AUTHENTICATION_FAILED, chunk_empty);
736 }
737 authenticator->destroy(authenticator);
738 if (!check_constraints(this))
739 {
740 return FAILED;
741 }
742 save_auth_cfg(this, FALSE);
743
744 this->state = MM_AUTH;
745 if (has_notify_errors(this, message))
746 {
747 return FAILED;
748 }
749 return NEED_MORE;
750 }
751 default:
752 return FAILED;
753 }
754 }
755
756 /**
757 * Lookup a shared secret for this IKE_SA
758 */
759 static shared_key_t *lookup_shared_key(private_main_mode_t *this)
760 {
761 host_t *me, *other;
762 identification_t *my_id, *other_id;
763 shared_key_t *shared_key = NULL;
764
765 /* try to get a PSK for IP addresses */
766 me = this->ike_sa->get_my_host(this->ike_sa);
767 other = this->ike_sa->get_other_host(this->ike_sa);
768 my_id = identification_create_from_sockaddr(me->get_sockaddr(me));
769 other_id = identification_create_from_sockaddr(other->get_sockaddr(other));
770 if (my_id && other_id)
771 {
772 shared_key = lib->credmgr->get_shared(lib->credmgr, SHARED_IKE,
773 my_id, other_id);
774 }
775 DESTROY_IF(my_id);
776 DESTROY_IF(other_id);
777 if (shared_key)
778 {
779 return shared_key;
780 }
781
782 if (this->my_auth && this->other_auth)
783 { /* as initiator, use identities from configuraiton */
784 my_id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
785 other_id = this->other_auth->get(this->other_auth, AUTH_RULE_IDENTITY);
786 if (my_id && other_id)
787 {
788 shared_key = lib->credmgr->get_shared(lib->credmgr, SHARED_IKE,
789 my_id, other_id);
790 }
791 else
792 {
793 DBG1(DBG_IKE, "no shared key found for '%Y'[%H] - '%Y'[%H]",
794 my_id, me, other_id, other);
795 }
796 }
797 else
798 { /* as responder, we try to find a config by IP */
799 enumerator_t *enumerator;
800 auth_cfg_t *my_auth, *other_auth;
801 peer_cfg_t *peer_cfg = NULL;
802
803 enumerator = charon->backends->create_peer_cfg_enumerator(
804 charon->backends, me, other, NULL, NULL);
805 while (enumerator->enumerate(enumerator, &peer_cfg))
806 {
807 my_auth = get_auth_cfg(peer_cfg, TRUE);
808 other_auth = get_auth_cfg(peer_cfg, FALSE);
809 if (my_auth && other_auth)
810 {
811 my_id = my_auth->get(my_auth, AUTH_RULE_IDENTITY);
812 other_id = other_auth->get(other_auth, AUTH_RULE_IDENTITY);
813 if (my_id && other_id)
814 {
815 shared_key = lib->credmgr->get_shared(lib->credmgr,
816 SHARED_IKE, my_id, other_id);
817 if (shared_key)
818 {
819 break;
820 }
821 else
822 {
823 DBG1(DBG_IKE, "no shared key found for "
824 "'%Y'[%H] - '%Y'[%H]", my_id, me, other_id, other);
825 }
826 }
827 }
828 }
829 enumerator->destroy(enumerator);
830 if (!peer_cfg)
831 {
832 DBG1(DBG_IKE, "no shared key found for %H - %H", me, other);
833 }
834 }
835 return shared_key;
836 }
837
838 /**
839 * Derive key material for this IKE_SA
840 */
841 static bool derive_keys(private_main_mode_t *this, chunk_t nonce_i,
842 chunk_t nonce_r)
843 {
844 ike_sa_id_t *id = this->ike_sa->get_id(this->ike_sa);
845 shared_key_t *shared_key = NULL;
846
847 switch (this->auth_method)
848 {
849 case AUTH_PSK:
850 case AUTH_XAUTH_INIT_PSK:
851 case AUTH_XAUTH_RESP_PSK:
852 shared_key = lookup_shared_key(this);
853 break;
854 default:
855 break;
856 }
857 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
858 this->dh_value, nonce_i, nonce_r, id, this->auth_method, shared_key))
859 {
860 DESTROY_IF(shared_key);
861 DBG1(DBG_IKE, "key derivation for %N failed",
862 auth_method_names, this->auth_method);
863 return FALSE;
864 }
865 DESTROY_IF(shared_key);
866 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, nonce_i, nonce_r,
867 NULL);
868
869 return TRUE;
870 }
871
872 /**
873 * Set IKE_SA to established state
874 */
875 static void establish(private_main_mode_t *this)
876 {
877 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
878 this->ike_sa->get_name(this->ike_sa),
879 this->ike_sa->get_unique_id(this->ike_sa),
880 this->ike_sa->get_my_host(this->ike_sa),
881 this->ike_sa->get_my_id(this->ike_sa),
882 this->ike_sa->get_other_host(this->ike_sa),
883 this->ike_sa->get_other_id(this->ike_sa));
884
885 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
886 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
887 }
888
889 METHOD(task_t, build_r, status_t,
890 private_main_mode_t *this, message_t *message)
891 {
892 switch (this->state)
893 {
894 case MM_SA:
895 {
896 sa_payload_t *sa_payload;
897
898 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
899 this->lifetime, 0, this->auth_method, MODE_NONE, FALSE);
900 message->add_payload(message, &sa_payload->payload_interface);
901
902 return NEED_MORE;
903 }
904 case MM_KE:
905 {
906 if (!add_nonce_ke(this, &this->nonce_r, message))
907 {
908 return FAILED;
909 }
910 if (!derive_keys(this, this->nonce_i, this->nonce_r))
911 {
912 return FAILED;
913 }
914 return NEED_MORE;
915 }
916 case MM_AUTH:
917 {
918 authenticator_t *authenticator;
919 id_payload_t *id_payload;
920 identification_t *id;
921
922 id = this->my_auth->get(this->my_auth, AUTH_RULE_IDENTITY);
923 if (!id)
924 {
925 DBG1(DBG_CFG, "own identity not known");
926 return FAILED;
927 }
928
929 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
930
931 id_payload = id_payload_create_from_identification(ID_V1, id);
932 message->add_payload(message, &id_payload->payload_interface);
933
934 authenticator = create_authenticator(this, id_payload);
935 if (!authenticator || authenticator->build(authenticator,
936 message) != SUCCESS)
937 {
938 DESTROY_IF(authenticator);
939 return FAILED;
940 }
941 authenticator->destroy(authenticator);
942 save_auth_cfg(this, TRUE);
943
944 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
945 {
946 this->ike_sa->queue_task(this->ike_sa,
947 (task_t*)mode_config_create(this->ike_sa, TRUE));
948 }
949
950 switch (this->auth_method)
951 {
952 case AUTH_XAUTH_INIT_PSK:
953 case AUTH_XAUTH_INIT_RSA:
954 case AUTH_HYBRID_INIT_RSA:
955 this->ike_sa->queue_task(this->ike_sa,
956 (task_t*)xauth_create(this->ike_sa, TRUE));
957 return SUCCESS;
958 case AUTH_XAUTH_RESP_PSK:
959 case AUTH_XAUTH_RESP_RSA:
960 case AUTH_HYBRID_RESP_RSA:
961 /* TODO-IKEv1: not yet supported */
962 return FAILED;
963 default:
964 establish(this);
965 return SUCCESS;
966 }
967 }
968 default:
969 return FAILED;
970 }
971 }
972
973 METHOD(task_t, process_i, status_t,
974 private_main_mode_t *this, message_t *message)
975 {
976 switch (this->state)
977 {
978 case MM_SA:
979 {
980 linked_list_t *list;
981 sa_payload_t *sa_payload;
982 auth_method_t auth_method;
983 u_int32_t lifetime;
984
985 sa_payload = (sa_payload_t*)message->get_payload(message,
986 SECURITY_ASSOCIATION_V1);
987 if (!sa_payload)
988 {
989 DBG1(DBG_IKE, "SA payload missing");
990 return FAILED;
991 }
992 list = sa_payload->get_proposals(sa_payload);
993 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
994 list, FALSE);
995 list->destroy_offset(list, offsetof(proposal_t, destroy));
996 if (!this->proposal)
997 {
998 DBG1(DBG_IKE, "no proposal found");
999 return FAILED;
1000 }
1001
1002 lifetime = sa_payload->get_lifetime(sa_payload);
1003 if (lifetime != this->lifetime)
1004 {
1005 DBG1(DBG_IKE, "received lifetime %us does not match configured "
1006 "%us, using lower value", lifetime, this->lifetime);
1007 }
1008 this->lifetime = min(this->lifetime, lifetime);
1009 auth_method = sa_payload->get_auth_method(sa_payload);
1010 if (auth_method != this->auth_method)
1011 {
1012 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
1013 "continue with configured", auth_method_names, auth_method,
1014 auth_method_names, this->auth_method);
1015 }
1016 return NEED_MORE;
1017 }
1018 case MM_KE:
1019 {
1020 if (!get_nonce_ke(this, &this->nonce_r, message))
1021 {
1022 return FAILED;
1023 }
1024 if (!derive_keys(this, this->nonce_i, this->nonce_r))
1025 {
1026 return FAILED;
1027 }
1028 return NEED_MORE;
1029 }
1030 case MM_AUTH:
1031 {
1032 authenticator_t *authenticator;
1033 id_payload_t *id_payload;
1034 identification_t *id;
1035
1036 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
1037 if (!id_payload)
1038 {
1039 DBG1(DBG_IKE, "IDir payload missing");
1040 return FAILED;
1041 }
1042 id = id_payload->get_identification(id_payload);
1043 if (!id->matches(id, this->other_auth->get(this->other_auth,
1044 AUTH_RULE_IDENTITY)))
1045 {
1046 DBG1(DBG_IKE, "IDir does not match");
1047 id->destroy(id);
1048 return FAILED;
1049 }
1050 this->ike_sa->set_other_id(this->ike_sa, id);
1051
1052 authenticator = create_authenticator(this, id_payload);
1053 if (!authenticator || authenticator->process(authenticator,
1054 message) != SUCCESS)
1055 {
1056 DESTROY_IF(authenticator);
1057 return FAILED;
1058 }
1059 authenticator->destroy(authenticator);
1060 if (!check_constraints(this))
1061 {
1062 return FAILED;
1063 }
1064 save_auth_cfg(this, FALSE);
1065
1066 switch (this->auth_method)
1067 {
1068 case AUTH_XAUTH_INIT_PSK:
1069 case AUTH_XAUTH_INIT_RSA:
1070 case AUTH_HYBRID_INIT_RSA:
1071 /* wait for XAUTH request */
1072 return SUCCESS;
1073 case AUTH_XAUTH_RESP_PSK:
1074 case AUTH_XAUTH_RESP_RSA:
1075 case AUTH_HYBRID_RESP_RSA:
1076 /* TODO-IKEv1: not yet */
1077 return FAILED;
1078 default:
1079 establish(this);
1080 return SUCCESS;
1081 }
1082 }
1083 default:
1084 return FAILED;
1085 }
1086 }
1087
1088 METHOD(task_t, get_type, task_type_t,
1089 private_main_mode_t *this)
1090 {
1091 return TASK_MAIN_MODE;
1092 }
1093
1094 METHOD(task_t, migrate, void,
1095 private_main_mode_t *this, ike_sa_t *ike_sa)
1096 {
1097 this->ike_sa = ike_sa;
1098 }
1099
1100 METHOD(task_t, destroy, void,
1101 private_main_mode_t *this)
1102 {
1103 DESTROY_IF(this->peer_cfg);
1104 DESTROY_IF(this->proposal);
1105 DESTROY_IF(this->dh);
1106 free(this->dh_value.ptr);
1107 free(this->nonce_i.ptr);
1108 free(this->nonce_r.ptr);
1109 free(this->sa_payload.ptr);
1110 free(this);
1111 }
1112
1113 /*
1114 * Described in header.
1115 */
1116 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
1117 {
1118 private_main_mode_t *this;
1119
1120 INIT(this,
1121 .public = {
1122 .task = {
1123 .get_type = _get_type,
1124 .migrate = _migrate,
1125 .destroy = _destroy,
1126 },
1127 },
1128 .ike_sa = ike_sa,
1129 .keymat = (keymat_v1_t*)ike_sa->get_keymat(ike_sa),
1130 .initiator = initiator,
1131 .state = MM_INIT,
1132 );
1133
1134 if (initiator)
1135 {
1136 this->public.task.build = _build_i;
1137 this->public.task.process = _process_i;
1138 }
1139 else
1140 {
1141 this->public.task.build = _build_r;
1142 this->public.task.process = _process_r;
1143 }
1144
1145 return &this->public;
1146 }