Enforce uniqueness policy in IKEv1 main and aggressive modes
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / main_mode.c
1 /*
2 * Copyright (C) 2011-2012 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/ikev1/phase1.h>
25 #include <encoding/payloads/sa_payload.h>
26 #include <encoding/payloads/id_payload.h>
27 #include <encoding/payloads/hash_payload.h>
28 #include <sa/ikev1/tasks/xauth.h>
29 #include <sa/ikev1/tasks/mode_config.h>
30 #include <sa/ikev1/tasks/informational.h>
31 #include <sa/ikev1/tasks/isakmp_delete.h>
32 #include <processing/jobs/adopt_children_job.h>
33
34 typedef struct private_main_mode_t private_main_mode_t;
35
36 /**
37 * Private members of a main_mode_t task.
38 */
39 struct private_main_mode_t {
40
41 /**
42 * Public methods and task_t interface.
43 */
44 main_mode_t public;
45
46 /**
47 * Assigned IKE_SA.
48 */
49 ike_sa_t *ike_sa;
50
51 /**
52 * Are we the initiator?
53 */
54 bool initiator;
55
56 /**
57 * Common phase 1 helper class
58 */
59 phase1_t *ph1;
60
61 /**
62 * IKE config to establish
63 */
64 ike_cfg_t *ike_cfg;
65
66 /**
67 * Peer config to use
68 */
69 peer_cfg_t *peer_cfg;
70
71 /**
72 * selected IKE proposal
73 */
74 proposal_t *proposal;
75
76 /**
77 * Negotiated SA lifetime
78 */
79 u_int32_t lifetime;
80
81 /**
82 * Negotiated authentication method
83 */
84 auth_method_t method;
85
86 /** states of main mode */
87 enum {
88 MM_INIT,
89 MM_SA,
90 MM_KE,
91 MM_AUTH,
92 } state;
93 };
94
95 /**
96 * Set IKE_SA to established state
97 */
98 static bool establish(private_main_mode_t *this)
99 {
100 if (!charon->bus->authorize(charon->bus, TRUE))
101 {
102 DBG1(DBG_IKE, "final authorization hook forbids IKE_SA, cancelling");
103 return FALSE;
104 }
105
106 DBG0(DBG_IKE, "IKE_SA %s[%d] established between %H[%Y]...%H[%Y]",
107 this->ike_sa->get_name(this->ike_sa),
108 this->ike_sa->get_unique_id(this->ike_sa),
109 this->ike_sa->get_my_host(this->ike_sa),
110 this->ike_sa->get_my_id(this->ike_sa),
111 this->ike_sa->get_other_host(this->ike_sa),
112 this->ike_sa->get_other_id(this->ike_sa));
113
114 this->ike_sa->set_state(this->ike_sa, IKE_ESTABLISHED);
115 charon->bus->ike_updown(charon->bus, this->ike_sa, TRUE);
116
117 return TRUE;
118 }
119
120 /**
121 * Check for notify errors, return TRUE if error found
122 */
123 static bool has_notify_errors(private_main_mode_t *this, message_t *message)
124 {
125 enumerator_t *enumerator;
126 payload_t *payload;
127 bool err = FALSE;
128
129 enumerator = message->create_payload_enumerator(message);
130 while (enumerator->enumerate(enumerator, &payload))
131 {
132 if (payload->get_type(payload) == NOTIFY_V1)
133 {
134 notify_payload_t *notify;
135 notify_type_t type;
136
137 notify = (notify_payload_t*)payload;
138 type = notify->get_notify_type(notify);
139 if (type < 16384)
140 {
141 DBG1(DBG_IKE, "received %N error notify",
142 notify_type_names, type);
143 err = TRUE;
144 }
145 else if (type == INITIAL_CONTACT_IKEV1)
146 {
147 if (!this->initiator && this->state == MM_AUTH)
148 {
149 /* If authenticated and received INITIAL_CONTACT,
150 * delete any existing IKE_SAs with that peer.
151 * The delete takes place when the SA is checked in due
152 * to other id not known until the 3rd message.*/
153 this->ike_sa->set_condition(this->ike_sa,
154 COND_INIT_CONTACT_SEEN, TRUE);
155 }
156 }
157 else
158 {
159 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
160 }
161 }
162 }
163 enumerator->destroy(enumerator);
164
165 return err;
166 }
167
168 /**
169 * Queue a task sending a notify in an INFORMATIONAL exchange
170 */
171 static status_t send_notify(private_main_mode_t *this, notify_type_t type)
172 {
173 notify_payload_t *notify;
174 ike_sa_id_t *ike_sa_id;
175 u_int64_t spi_i, spi_r;
176 chunk_t spi;
177
178 notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1,
179 PROTO_IKE, type);
180 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
181 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
182 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
183 spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
184 notify->set_spi_data(notify, spi);
185
186 this->ike_sa->queue_task(this->ike_sa,
187 (task_t*)informational_create(this->ike_sa, notify));
188 /* cancel all active/passive tasks in favour of informational */
189 this->ike_sa->flush_queue(this->ike_sa,
190 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
191 return ALREADY_DONE;
192 }
193
194 /**
195 * Queue a delete task if authentication failed as initiator
196 */
197 static status_t send_delete(private_main_mode_t *this)
198 {
199 this->ike_sa->queue_task(this->ike_sa,
200 (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
201 /* cancel all active tasks in favour of informational */
202 this->ike_sa->flush_queue(this->ike_sa,
203 this->initiator ? TASK_QUEUE_ACTIVE : TASK_QUEUE_PASSIVE);
204 return ALREADY_DONE;
205 }
206
207 METHOD(task_t, build_i, status_t,
208 private_main_mode_t *this, message_t *message)
209 {
210 switch (this->state)
211 {
212 case MM_INIT:
213 {
214 sa_payload_t *sa_payload;
215 linked_list_t *proposals;
216 packet_t *packet;
217
218 DBG0(DBG_IKE, "initiating Main Mode IKE_SA %s[%d] to %H",
219 this->ike_sa->get_name(this->ike_sa),
220 this->ike_sa->get_unique_id(this->ike_sa),
221 this->ike_sa->get_other_host(this->ike_sa));
222 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
223
224 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
225 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
226 this->peer_cfg->get_ref(this->peer_cfg);
227
228 this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
229 if (this->method == AUTH_NONE)
230 {
231 DBG1(DBG_CFG, "configuration uses unsupported authentication");
232 return FAILED;
233 }
234 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
235 FALSE);
236 if (!this->lifetime)
237 { /* fall back to rekey time of no rekey time configured */
238 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
239 FALSE);
240 }
241 this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
242 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
243 sa_payload = sa_payload_create_from_proposals_v1(proposals,
244 this->lifetime, 0, this->method, MODE_NONE, FALSE, 0);
245 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
246
247 message->add_payload(message, &sa_payload->payload_interface);
248
249 /* pregenerate message to store SA payload */
250 if (this->ike_sa->generate_message(this->ike_sa, message,
251 &packet) != SUCCESS)
252 {
253 DBG1(DBG_IKE, "pregenerating SA payload failed");
254 return FAILED;
255 }
256 packet->destroy(packet);
257 if (!this->ph1->save_sa_payload(this->ph1, message))
258 {
259 return FAILED;
260 }
261
262 this->state = MM_SA;
263 return NEED_MORE;
264 }
265 case MM_SA:
266 {
267 u_int16_t group;
268
269 if (!this->ph1->create_hasher(this->ph1))
270 {
271 return send_notify(this, NO_PROPOSAL_CHOSEN);
272 }
273 if (!this->proposal->get_algorithm(this->proposal,
274 DIFFIE_HELLMAN_GROUP, &group, NULL))
275 {
276 DBG1(DBG_IKE, "DH group selection failed");
277 return send_notify(this, NO_PROPOSAL_CHOSEN);
278 }
279 if (!this->ph1->create_dh(this->ph1, group))
280 {
281 DBG1(DBG_IKE, "negotiated DH group not supported");
282 return send_notify(this, INVALID_KEY_INFORMATION);
283 }
284 if (!this->ph1->add_nonce_ke(this->ph1, message))
285 {
286 return send_notify(this, INVALID_KEY_INFORMATION);
287 }
288 this->state = MM_KE;
289 return NEED_MORE;
290 }
291 case MM_KE:
292 {
293 id_payload_t *id_payload;
294 identification_t *id;
295
296 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
297 if (!id)
298 {
299 DBG1(DBG_CFG, "own identity not known");
300 return send_notify(this, INVALID_ID_INFORMATION);
301 }
302 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
303 id_payload = id_payload_create_from_identification(ID_V1, id);
304 message->add_payload(message, &id_payload->payload_interface);
305
306 if (!this->ph1->build_auth(this->ph1, this->method, message,
307 id_payload->get_encoded(id_payload)))
308 {
309 return send_notify(this, AUTHENTICATION_FAILED);
310 }
311
312 this->state = MM_AUTH;
313 return NEED_MORE;
314 }
315 default:
316 return FAILED;
317 }
318 }
319
320 METHOD(task_t, process_r, status_t,
321 private_main_mode_t *this, message_t *message)
322 {
323 switch (this->state)
324 {
325 case MM_INIT:
326 {
327 linked_list_t *list;
328 sa_payload_t *sa_payload;
329 bool private;
330
331 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
332 DBG0(DBG_IKE, "%H is initiating a Main Mode IKE_SA",
333 message->get_source(message));
334 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
335
336 this->ike_sa->update_hosts(this->ike_sa,
337 message->get_destination(message),
338 message->get_source(message), TRUE);
339
340 sa_payload = (sa_payload_t*)message->get_payload(message,
341 SECURITY_ASSOCIATION_V1);
342 if (!sa_payload)
343 {
344 DBG1(DBG_IKE, "SA payload missing");
345 return send_notify(this, INVALID_PAYLOAD_TYPE);
346 }
347 if (!this->ph1->save_sa_payload(this->ph1, message))
348 {
349 return send_notify(this, INVALID_PAYLOAD_TYPE);
350 }
351
352 list = sa_payload->get_proposals(sa_payload);
353 private = this->ike_sa->supports_extension(this->ike_sa,
354 EXT_STRONGSWAN);
355 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
356 list, private);
357 list->destroy_offset(list, offsetof(proposal_t, destroy));
358 if (!this->proposal)
359 {
360 DBG1(DBG_IKE, "no proposal found");
361 return send_notify(this, NO_PROPOSAL_CHOSEN);
362 }
363 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
364
365 this->method = sa_payload->get_auth_method(sa_payload);
366 this->lifetime = sa_payload->get_lifetime(sa_payload);
367
368 this->state = MM_SA;
369 return NEED_MORE;
370 }
371 case MM_SA:
372 {
373 u_int16_t group;
374
375 if (!this->ph1->create_hasher(this->ph1))
376 {
377 return send_notify(this, INVALID_KEY_INFORMATION);
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 send_notify(this, INVALID_KEY_INFORMATION);
384 }
385 if (!this->ph1->create_dh(this->ph1, group))
386 {
387 DBG1(DBG_IKE, "negotiated DH group not supported");
388 return send_notify(this, INVALID_KEY_INFORMATION);
389 }
390 if (!this->ph1->get_nonce_ke(this->ph1, message))
391 {
392 return send_notify(this, INVALID_PAYLOAD_TYPE);
393 }
394 this->state = MM_KE;
395 return NEED_MORE;
396 }
397 case MM_KE:
398 {
399 id_payload_t *id_payload;
400 identification_t *id;
401
402 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
403 if (!id_payload)
404 {
405 DBG1(DBG_IKE, "IDii payload missing");
406 return send_notify(this, INVALID_PAYLOAD_TYPE);
407 }
408 id = id_payload->get_identification(id_payload);
409 this->ike_sa->set_other_id(this->ike_sa, id);
410
411 while (TRUE)
412 {
413 DESTROY_IF(this->peer_cfg);
414 this->peer_cfg = this->ph1->select_config(this->ph1,
415 this->method, FALSE, id);
416 if (!this->peer_cfg)
417 {
418 return send_notify(this, AUTHENTICATION_FAILED);
419 }
420 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
421
422 if (this->ph1->verify_auth(this->ph1, this->method, message,
423 id_payload->get_encoded(id_payload)))
424 {
425 break;
426 }
427 }
428
429 if (!charon->bus->authorize(charon->bus, FALSE))
430 {
431 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
432 "cancelling");
433 return send_notify(this, AUTHENTICATION_FAILED);
434 }
435
436 this->state = MM_AUTH;
437 if (has_notify_errors(this, message))
438 {
439 return FAILED;
440 }
441 return NEED_MORE;
442 }
443 default:
444 return FAILED;
445 }
446 }
447
448 METHOD(task_t, build_r, status_t,
449 private_main_mode_t *this, message_t *message)
450 {
451 switch (this->state)
452 {
453 case MM_SA:
454 {
455 sa_payload_t *sa_payload;
456
457 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
458 this->lifetime, 0, this->method, MODE_NONE, FALSE, 0);
459 message->add_payload(message, &sa_payload->payload_interface);
460
461 return NEED_MORE;
462 }
463 case MM_KE:
464 {
465 if (!this->ph1->add_nonce_ke(this->ph1, message))
466 {
467 return send_notify(this, INVALID_KEY_INFORMATION);
468 }
469 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
470 {
471 return send_notify(this, INVALID_KEY_INFORMATION);
472 }
473 return NEED_MORE;
474 }
475 case MM_AUTH:
476 {
477 id_payload_t *id_payload;
478 identification_t *id;
479
480 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
481 if (!id)
482 {
483 DBG1(DBG_CFG, "own identity not known");
484 return send_notify(this, INVALID_ID_INFORMATION);
485 }
486 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
487
488 id_payload = id_payload_create_from_identification(ID_V1, id);
489 message->add_payload(message, &id_payload->payload_interface);
490
491 if (!this->ph1->build_auth(this->ph1, this->method, message,
492 id_payload->get_encoded(id_payload)))
493 {
494 return send_notify(this, AUTHENTICATION_FAILED);
495 }
496 if (charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
497 this->ike_sa, FALSE))
498 {
499 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness policy");
500 return send_notify(this, AUTHENTICATION_FAILED);
501 }
502
503 switch (this->method)
504 {
505 case AUTH_XAUTH_INIT_PSK:
506 case AUTH_XAUTH_INIT_RSA:
507 case AUTH_HYBRID_INIT_RSA:
508 this->ike_sa->queue_task(this->ike_sa,
509 (task_t*)xauth_create(this->ike_sa, TRUE));
510 return SUCCESS;
511 case AUTH_XAUTH_RESP_PSK:
512 case AUTH_XAUTH_RESP_RSA:
513 case AUTH_HYBRID_RESP_RSA:
514 /* TODO-IKEv1: not yet supported */
515 return FAILED;
516 default:
517 if (!establish(this))
518 {
519 return send_notify(this, AUTHENTICATION_FAILED);
520 }
521 lib->processor->queue_job(lib->processor, (job_t*)
522 adopt_children_job_create(
523 this->ike_sa->get_id(this->ike_sa)));
524 return SUCCESS;
525 }
526 }
527 default:
528 return FAILED;
529 }
530 }
531
532 METHOD(task_t, process_i, status_t,
533 private_main_mode_t *this, message_t *message)
534 {
535 switch (this->state)
536 {
537 case MM_SA:
538 {
539 linked_list_t *list;
540 sa_payload_t *sa_payload;
541 auth_method_t method;
542 u_int32_t lifetime;
543 bool private;
544
545 sa_payload = (sa_payload_t*)message->get_payload(message,
546 SECURITY_ASSOCIATION_V1);
547 if (!sa_payload)
548 {
549 DBG1(DBG_IKE, "SA payload missing");
550 return send_notify(this, INVALID_PAYLOAD_TYPE);
551 }
552 list = sa_payload->get_proposals(sa_payload);
553 private = this->ike_sa->supports_extension(this->ike_sa,
554 EXT_STRONGSWAN);
555 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
556 list, private);
557 list->destroy_offset(list, offsetof(proposal_t, destroy));
558 if (!this->proposal)
559 {
560 DBG1(DBG_IKE, "no proposal found");
561 return send_notify(this, NO_PROPOSAL_CHOSEN);
562 }
563 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
564
565 lifetime = sa_payload->get_lifetime(sa_payload);
566 if (lifetime != this->lifetime)
567 {
568 DBG1(DBG_IKE, "received lifetime %us does not match configured "
569 "lifetime %us", lifetime, this->lifetime);
570 }
571 this->lifetime = lifetime;
572 method = sa_payload->get_auth_method(sa_payload);
573 if (method != this->method)
574 {
575 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
576 "continue with configured", auth_method_names, method,
577 auth_method_names, this->method);
578 }
579 return NEED_MORE;
580 }
581 case MM_KE:
582 {
583 if (!this->ph1->get_nonce_ke(this->ph1, message))
584 {
585 return send_notify(this, INVALID_PAYLOAD_TYPE);
586 }
587 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
588 {
589 return send_notify(this, INVALID_KEY_INFORMATION);
590 }
591 return NEED_MORE;
592 }
593 case MM_AUTH:
594 {
595 id_payload_t *id_payload;
596 identification_t *id, *cid;
597
598 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
599 if (!id_payload)
600 {
601 DBG1(DBG_IKE, "IDir payload missing");
602 return send_delete(this);
603 }
604 id = id_payload->get_identification(id_payload);
605 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
606 if (cid && !id->matches(id, cid))
607 {
608 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
609 id->destroy(id);
610 return send_delete(this);
611 }
612 this->ike_sa->set_other_id(this->ike_sa, id);
613
614 if (!this->ph1->verify_auth(this->ph1, this->method, message,
615 id_payload->get_encoded(id_payload)))
616 {
617 return send_delete(this);
618 }
619 if (!charon->bus->authorize(charon->bus, FALSE))
620 {
621 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
622 "cancelling");
623 return send_delete(this);
624 }
625 if (charon->ike_sa_manager->check_uniqueness(charon->ike_sa_manager,
626 this->ike_sa, FALSE))
627 {
628 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness policy");
629 return send_delete(this);
630 }
631
632 switch (this->method)
633 {
634 case AUTH_XAUTH_INIT_PSK:
635 case AUTH_XAUTH_INIT_RSA:
636 case AUTH_HYBRID_INIT_RSA:
637 /* wait for XAUTH request */
638 break;
639 case AUTH_XAUTH_RESP_PSK:
640 case AUTH_XAUTH_RESP_RSA:
641 case AUTH_HYBRID_RESP_RSA:
642 /* TODO-IKEv1: not yet */
643 return FAILED;
644 default:
645 if (!establish(this))
646 {
647 return send_delete(this);
648 }
649 break;
650 }
651 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
652 {
653 this->ike_sa->queue_task(this->ike_sa,
654 (task_t*)mode_config_create(this->ike_sa, TRUE));
655 }
656 return SUCCESS;
657 }
658 default:
659 return FAILED;
660 }
661 }
662
663 METHOD(task_t, get_type, task_type_t,
664 private_main_mode_t *this)
665 {
666 return TASK_MAIN_MODE;
667 }
668
669 METHOD(task_t, migrate, void,
670 private_main_mode_t *this, ike_sa_t *ike_sa)
671 {
672 DESTROY_IF(this->peer_cfg);
673 DESTROY_IF(this->proposal);
674 this->ph1->destroy(this->ph1);
675
676 this->ike_sa = ike_sa;
677 this->state = MM_INIT;
678 this->peer_cfg = NULL;
679 this->proposal = NULL;
680 this->ph1 = phase1_create(ike_sa, this->initiator);
681 }
682
683 METHOD(task_t, destroy, void,
684 private_main_mode_t *this)
685 {
686 DESTROY_IF(this->peer_cfg);
687 DESTROY_IF(this->proposal);
688 this->ph1->destroy(this->ph1);
689 free(this);
690 }
691
692 /*
693 * Described in header.
694 */
695 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
696 {
697 private_main_mode_t *this;
698
699 INIT(this,
700 .public = {
701 .task = {
702 .get_type = _get_type,
703 .migrate = _migrate,
704 .destroy = _destroy,
705 },
706 },
707 .ike_sa = ike_sa,
708 .ph1 = phase1_create(ike_sa, initiator),
709 .initiator = initiator,
710 .state = MM_INIT,
711 );
712
713 if (initiator)
714 {
715 this->public.task.build = _build_i;
716 this->public.task.process = _process_i;
717 }
718 else
719 {
720 this->public.task.build = _build_r;
721 this->public.task.process = _process_r;
722 }
723
724 return &this->public;
725 }