Add basic support for XAuth responder authentication
[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
497 switch (this->method)
498 {
499 case AUTH_XAUTH_INIT_PSK:
500 case AUTH_XAUTH_INIT_RSA:
501 case AUTH_HYBRID_INIT_RSA:
502 this->ike_sa->queue_task(this->ike_sa,
503 (task_t*)xauth_create(this->ike_sa, TRUE));
504 return SUCCESS;
505 case AUTH_XAUTH_RESP_PSK:
506 case AUTH_XAUTH_RESP_RSA:
507 case AUTH_HYBRID_RESP_RSA:
508 /* wait for XAUTH request */
509 return SUCCESS;
510 default:
511 if (charon->ike_sa_manager->check_uniqueness(
512 charon->ike_sa_manager, this->ike_sa, FALSE))
513 {
514 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
515 "policy");
516 return send_notify(this, AUTHENTICATION_FAILED);
517 }
518 if (!establish(this))
519 {
520 return send_notify(this, AUTHENTICATION_FAILED);
521 }
522 lib->processor->queue_job(lib->processor, (job_t*)
523 adopt_children_job_create(
524 this->ike_sa->get_id(this->ike_sa)));
525 return SUCCESS;
526 }
527 }
528 default:
529 return FAILED;
530 }
531 }
532
533 METHOD(task_t, process_i, status_t,
534 private_main_mode_t *this, message_t *message)
535 {
536 switch (this->state)
537 {
538 case MM_SA:
539 {
540 linked_list_t *list;
541 sa_payload_t *sa_payload;
542 auth_method_t method;
543 u_int32_t lifetime;
544 bool private;
545
546 sa_payload = (sa_payload_t*)message->get_payload(message,
547 SECURITY_ASSOCIATION_V1);
548 if (!sa_payload)
549 {
550 DBG1(DBG_IKE, "SA payload missing");
551 return send_notify(this, INVALID_PAYLOAD_TYPE);
552 }
553 list = sa_payload->get_proposals(sa_payload);
554 private = this->ike_sa->supports_extension(this->ike_sa,
555 EXT_STRONGSWAN);
556 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
557 list, private);
558 list->destroy_offset(list, offsetof(proposal_t, destroy));
559 if (!this->proposal)
560 {
561 DBG1(DBG_IKE, "no proposal found");
562 return send_notify(this, NO_PROPOSAL_CHOSEN);
563 }
564 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
565
566 lifetime = sa_payload->get_lifetime(sa_payload);
567 if (lifetime != this->lifetime)
568 {
569 DBG1(DBG_IKE, "received lifetime %us does not match configured "
570 "lifetime %us", lifetime, this->lifetime);
571 }
572 this->lifetime = lifetime;
573 method = sa_payload->get_auth_method(sa_payload);
574 if (method != this->method)
575 {
576 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
577 "continue with configured", auth_method_names, method,
578 auth_method_names, this->method);
579 }
580 return NEED_MORE;
581 }
582 case MM_KE:
583 {
584 if (!this->ph1->get_nonce_ke(this->ph1, message))
585 {
586 return send_notify(this, INVALID_PAYLOAD_TYPE);
587 }
588 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
589 {
590 return send_notify(this, INVALID_KEY_INFORMATION);
591 }
592 return NEED_MORE;
593 }
594 case MM_AUTH:
595 {
596 id_payload_t *id_payload;
597 identification_t *id, *cid;
598
599 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
600 if (!id_payload)
601 {
602 DBG1(DBG_IKE, "IDir payload missing");
603 return send_delete(this);
604 }
605 id = id_payload->get_identification(id_payload);
606 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
607 if (cid && !id->matches(id, cid))
608 {
609 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
610 id->destroy(id);
611 return send_delete(this);
612 }
613 this->ike_sa->set_other_id(this->ike_sa, id);
614
615 if (!this->ph1->verify_auth(this->ph1, this->method, message,
616 id_payload->get_encoded(id_payload)))
617 {
618 return send_delete(this);
619 }
620 if (!charon->bus->authorize(charon->bus, FALSE))
621 {
622 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
623 "cancelling");
624 return send_delete(this);
625 }
626
627 switch (this->method)
628 {
629 case AUTH_XAUTH_INIT_PSK:
630 case AUTH_XAUTH_INIT_RSA:
631 case AUTH_HYBRID_INIT_RSA:
632 /* wait for XAUTH request */
633 break;
634 case AUTH_XAUTH_RESP_PSK:
635 case AUTH_XAUTH_RESP_RSA:
636 case AUTH_HYBRID_RESP_RSA:
637 this->ike_sa->queue_task(this->ike_sa,
638 (task_t*)xauth_create(this->ike_sa, TRUE));
639 return SUCCESS;
640 default:
641 if (charon->ike_sa_manager->check_uniqueness(
642 charon->ike_sa_manager, this->ike_sa, FALSE))
643 {
644 DBG1(DBG_IKE, "cancelling Main Mode due to uniqueness "
645 "policy");
646 return send_delete(this);
647 }
648 if (!establish(this))
649 {
650 return send_delete(this);
651 }
652 break;
653 }
654 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
655 {
656 this->ike_sa->queue_task(this->ike_sa,
657 (task_t*)mode_config_create(this->ike_sa, TRUE));
658 }
659 return SUCCESS;
660 }
661 default:
662 return FAILED;
663 }
664 }
665
666 METHOD(task_t, get_type, task_type_t,
667 private_main_mode_t *this)
668 {
669 return TASK_MAIN_MODE;
670 }
671
672 METHOD(task_t, migrate, void,
673 private_main_mode_t *this, ike_sa_t *ike_sa)
674 {
675 DESTROY_IF(this->peer_cfg);
676 DESTROY_IF(this->proposal);
677 this->ph1->destroy(this->ph1);
678
679 this->ike_sa = ike_sa;
680 this->state = MM_INIT;
681 this->peer_cfg = NULL;
682 this->proposal = NULL;
683 this->ph1 = phase1_create(ike_sa, this->initiator);
684 }
685
686 METHOD(task_t, destroy, void,
687 private_main_mode_t *this)
688 {
689 DESTROY_IF(this->peer_cfg);
690 DESTROY_IF(this->proposal);
691 this->ph1->destroy(this->ph1);
692 free(this);
693 }
694
695 /*
696 * Described in header.
697 */
698 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
699 {
700 private_main_mode_t *this;
701
702 INIT(this,
703 .public = {
704 .task = {
705 .get_type = _get_type,
706 .migrate = _migrate,
707 .destroy = _destroy,
708 },
709 },
710 .ike_sa = ike_sa,
711 .ph1 = phase1_create(ike_sa, initiator),
712 .initiator = initiator,
713 .state = MM_INIT,
714 );
715
716 if (initiator)
717 {
718 this->public.task.build = _build_i;
719 this->public.task.process = _process_i;
720 }
721 else
722 {
723 this->public.task.build = _build_r;
724 this->public.task.process = _process_r;
725 }
726
727 return &this->public;
728 }