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