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