53c38748e3866cd61242b54173c0143399d14526
[strongswan.git] / src / libcharon / sa / ikev1 / 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/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 return ALREADY_DONE;
190 }
191
192 /**
193 * Queue a delete task if authentication failed as initiator
194 */
195 static status_t send_delete(private_main_mode_t *this)
196 {
197 this->ike_sa->queue_task(this->ike_sa,
198 (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
199 /* cancel all active tasks in favour of informational */
200 return ALREADY_DONE;
201 }
202
203 METHOD(task_t, build_i, status_t,
204 private_main_mode_t *this, message_t *message)
205 {
206 switch (this->state)
207 {
208 case MM_INIT:
209 {
210 sa_payload_t *sa_payload;
211 linked_list_t *proposals;
212 packet_t *packet;
213
214 DBG0(DBG_IKE, "initiating Main Mode IKE_SA %s[%d] to %H",
215 this->ike_sa->get_name(this->ike_sa),
216 this->ike_sa->get_unique_id(this->ike_sa),
217 this->ike_sa->get_other_host(this->ike_sa));
218 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
219
220 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
221 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
222 this->peer_cfg->get_ref(this->peer_cfg);
223
224 this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
225 if (this->method == AUTH_NONE)
226 {
227 DBG1(DBG_CFG, "configuration uses unsupported authentication");
228 return FAILED;
229 }
230 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
231 FALSE);
232 if (!this->lifetime)
233 { /* fall back to rekey time of no rekey time configured */
234 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
235 FALSE);
236 }
237 this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
238 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
239 sa_payload = sa_payload_create_from_proposals_v1(proposals,
240 this->lifetime, 0, this->method, MODE_NONE, FALSE);
241 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
242
243 message->add_payload(message, &sa_payload->payload_interface);
244
245 /* pregenerate message to store SA payload */
246 if (this->ike_sa->generate_message(this->ike_sa, message,
247 &packet) != SUCCESS)
248 {
249 DBG1(DBG_IKE, "pregenerating SA payload failed");
250 return FAILED;
251 }
252 packet->destroy(packet);
253 if (!this->ph1->save_sa_payload(this->ph1, message))
254 {
255 return FAILED;
256 }
257
258 this->state = MM_SA;
259 return NEED_MORE;
260 }
261 case MM_SA:
262 {
263 u_int16_t group;
264
265 if (!this->ph1->create_hasher(this->ph1))
266 {
267 return send_notify(this, NO_PROPOSAL_CHOSEN);
268 }
269 if (!this->proposal->get_algorithm(this->proposal,
270 DIFFIE_HELLMAN_GROUP, &group, NULL))
271 {
272 DBG1(DBG_IKE, "DH group selection failed");
273 return send_notify(this, NO_PROPOSAL_CHOSEN);
274 }
275 if (!this->ph1->create_dh(this->ph1, group))
276 {
277 DBG1(DBG_IKE, "negotiated DH group not supported");
278 return send_notify(this, INVALID_KEY_INFORMATION);
279 }
280 if (!this->ph1->add_nonce_ke(this->ph1, message))
281 {
282 return send_notify(this, INVALID_KEY_INFORMATION);
283 }
284 this->state = MM_KE;
285 return NEED_MORE;
286 }
287 case MM_KE:
288 {
289 id_payload_t *id_payload;
290 identification_t *id;
291
292 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
293 if (!id)
294 {
295 DBG1(DBG_CFG, "own identity not known");
296 return send_notify(this, INVALID_ID_INFORMATION);
297 }
298 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
299 id_payload = id_payload_create_from_identification(ID_V1, id);
300 message->add_payload(message, &id_payload->payload_interface);
301
302 if (!this->ph1->build_auth(this->ph1, this->method, message,
303 id_payload->get_encoded(id_payload)))
304 {
305 return send_notify(this, AUTHENTICATION_FAILED);
306 }
307
308 this->state = MM_AUTH;
309 return NEED_MORE;
310 }
311 default:
312 return FAILED;
313 }
314 }
315
316 METHOD(task_t, process_r, status_t,
317 private_main_mode_t *this, message_t *message)
318 {
319 switch (this->state)
320 {
321 case MM_INIT:
322 {
323 linked_list_t *list;
324 sa_payload_t *sa_payload;
325 bool private;
326
327 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
328 DBG0(DBG_IKE, "%H is initiating a Main Mode IKE_SA",
329 message->get_source(message));
330 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
331
332 this->ike_sa->update_hosts(this->ike_sa,
333 message->get_destination(message),
334 message->get_source(message), TRUE);
335
336 sa_payload = (sa_payload_t*)message->get_payload(message,
337 SECURITY_ASSOCIATION_V1);
338 if (!sa_payload)
339 {
340 DBG1(DBG_IKE, "SA payload missing");
341 return send_notify(this, INVALID_PAYLOAD_TYPE);
342 }
343 if (!this->ph1->save_sa_payload(this->ph1, message))
344 {
345 return send_notify(this, INVALID_PAYLOAD_TYPE);
346 }
347
348 list = sa_payload->get_proposals(sa_payload);
349 private = this->ike_sa->supports_extension(this->ike_sa,
350 EXT_STRONGSWAN);
351 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
352 list, private);
353 list->destroy_offset(list, offsetof(proposal_t, destroy));
354 if (!this->proposal)
355 {
356 DBG1(DBG_IKE, "no proposal found");
357 return send_notify(this, NO_PROPOSAL_CHOSEN);
358 }
359 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
360
361 this->method = sa_payload->get_auth_method(sa_payload);
362 this->lifetime = sa_payload->get_lifetime(sa_payload);
363
364 this->state = MM_SA;
365 return NEED_MORE;
366 }
367 case MM_SA:
368 {
369 u_int16_t group;
370
371 if (!this->ph1->create_hasher(this->ph1))
372 {
373 return send_notify(this, INVALID_KEY_INFORMATION);
374 }
375 if (!this->proposal->get_algorithm(this->proposal,
376 DIFFIE_HELLMAN_GROUP, &group, NULL))
377 {
378 DBG1(DBG_IKE, "DH group selection failed");
379 return send_notify(this, INVALID_KEY_INFORMATION);
380 }
381 if (!this->ph1->create_dh(this->ph1, group))
382 {
383 DBG1(DBG_IKE, "negotiated DH group not supported");
384 return send_notify(this, INVALID_KEY_INFORMATION);
385 }
386 if (!this->ph1->get_nonce_ke(this->ph1, message))
387 {
388 return send_notify(this, INVALID_PAYLOAD_TYPE);
389 }
390 this->state = MM_KE;
391 return NEED_MORE;
392 }
393 case MM_KE:
394 {
395 id_payload_t *id_payload;
396 identification_t *id;
397
398 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
399 if (!id_payload)
400 {
401 DBG1(DBG_IKE, "IDii payload missing");
402 return send_notify(this, INVALID_PAYLOAD_TYPE);
403 }
404
405 id = id_payload->get_identification(id_payload);
406 this->ike_sa->set_other_id(this->ike_sa, id);
407 this->peer_cfg = this->ph1->select_config(this->ph1,
408 this->method, FALSE, id);
409 if (!this->peer_cfg)
410 {
411 DBG1(DBG_IKE, "no peer config found");
412 return send_notify(this, AUTHENTICATION_FAILED);
413 }
414 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
415
416 if (!this->ph1->verify_auth(this->ph1, this->method, message,
417 id_payload->get_encoded(id_payload)))
418 {
419 return send_notify(this, AUTHENTICATION_FAILED);
420 }
421 if (!charon->bus->authorize(charon->bus, FALSE))
422 {
423 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
424 "cancelling");
425 return send_notify(this, AUTHENTICATION_FAILED);
426 }
427
428 this->state = MM_AUTH;
429 if (has_notify_errors(this, message))
430 {
431 return FAILED;
432 }
433 return NEED_MORE;
434 }
435 default:
436 return FAILED;
437 }
438 }
439
440 METHOD(task_t, build_r, status_t,
441 private_main_mode_t *this, message_t *message)
442 {
443 switch (this->state)
444 {
445 case MM_SA:
446 {
447 sa_payload_t *sa_payload;
448
449 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
450 this->lifetime, 0, this->method, MODE_NONE, FALSE);
451 message->add_payload(message, &sa_payload->payload_interface);
452
453 return NEED_MORE;
454 }
455 case MM_KE:
456 {
457 if (!this->ph1->add_nonce_ke(this->ph1, message))
458 {
459 return send_notify(this, INVALID_KEY_INFORMATION);
460 }
461 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
462 {
463 return send_notify(this, INVALID_KEY_INFORMATION);
464 }
465 return NEED_MORE;
466 }
467 case MM_AUTH:
468 {
469 id_payload_t *id_payload;
470 identification_t *id;
471
472 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
473 if (!id)
474 {
475 DBG1(DBG_CFG, "own identity not known");
476 return send_notify(this, INVALID_ID_INFORMATION);
477 }
478 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
479
480 id_payload = id_payload_create_from_identification(ID_V1, id);
481 message->add_payload(message, &id_payload->payload_interface);
482
483 if (!this->ph1->build_auth(this->ph1, this->method, message,
484 id_payload->get_encoded(id_payload)))
485 {
486 return send_notify(this, AUTHENTICATION_FAILED);
487 }
488
489 switch (this->method)
490 {
491 case AUTH_XAUTH_INIT_PSK:
492 case AUTH_XAUTH_INIT_RSA:
493 case AUTH_HYBRID_INIT_RSA:
494 this->ike_sa->queue_task(this->ike_sa,
495 (task_t*)xauth_create(this->ike_sa, TRUE));
496 return SUCCESS;
497 case AUTH_XAUTH_RESP_PSK:
498 case AUTH_XAUTH_RESP_RSA:
499 case AUTH_HYBRID_RESP_RSA:
500 /* TODO-IKEv1: not yet supported */
501 return FAILED;
502 default:
503 if (!establish(this))
504 {
505 return send_notify(this, AUTHENTICATION_FAILED);
506 }
507 lib->processor->queue_job(lib->processor, (job_t*)
508 adopt_children_job_create(
509 this->ike_sa->get_id(this->ike_sa)));
510 return SUCCESS;
511 }
512 }
513 default:
514 return FAILED;
515 }
516 }
517
518 METHOD(task_t, process_i, status_t,
519 private_main_mode_t *this, message_t *message)
520 {
521 switch (this->state)
522 {
523 case MM_SA:
524 {
525 linked_list_t *list;
526 sa_payload_t *sa_payload;
527 auth_method_t method;
528 u_int32_t lifetime;
529 bool private;
530
531 sa_payload = (sa_payload_t*)message->get_payload(message,
532 SECURITY_ASSOCIATION_V1);
533 if (!sa_payload)
534 {
535 DBG1(DBG_IKE, "SA payload missing");
536 return send_notify(this, INVALID_PAYLOAD_TYPE);
537 }
538 list = sa_payload->get_proposals(sa_payload);
539 private = this->ike_sa->supports_extension(this->ike_sa,
540 EXT_STRONGSWAN);
541 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
542 list, private);
543 list->destroy_offset(list, offsetof(proposal_t, destroy));
544 if (!this->proposal)
545 {
546 DBG1(DBG_IKE, "no proposal found");
547 return send_notify(this, NO_PROPOSAL_CHOSEN);
548 }
549 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
550
551 lifetime = sa_payload->get_lifetime(sa_payload);
552 if (lifetime != this->lifetime)
553 {
554 DBG1(DBG_IKE, "received lifetime %us does not match configured "
555 "lifetime %us", lifetime, this->lifetime);
556 }
557 this->lifetime = lifetime;
558 method = sa_payload->get_auth_method(sa_payload);
559 if (method != this->method)
560 {
561 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
562 "continue with configured", auth_method_names, method,
563 auth_method_names, this->method);
564 }
565 return NEED_MORE;
566 }
567 case MM_KE:
568 {
569 if (!this->ph1->get_nonce_ke(this->ph1, message))
570 {
571 return send_notify(this, INVALID_PAYLOAD_TYPE);
572 }
573 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
574 {
575 return send_notify(this, INVALID_KEY_INFORMATION);
576 }
577 return NEED_MORE;
578 }
579 case MM_AUTH:
580 {
581 id_payload_t *id_payload;
582 identification_t *id, *cid;
583
584 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
585 if (!id_payload)
586 {
587 DBG1(DBG_IKE, "IDir payload missing");
588 return send_delete(this);
589 }
590 id = id_payload->get_identification(id_payload);
591 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
592 if (cid && !id->matches(id, cid))
593 {
594 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
595 id->destroy(id);
596 return send_delete(this);
597 }
598 this->ike_sa->set_other_id(this->ike_sa, id);
599
600 if (!this->ph1->verify_auth(this->ph1, this->method, message,
601 id_payload->get_encoded(id_payload)))
602 {
603 return send_delete(this);
604 }
605 if (!charon->bus->authorize(charon->bus, FALSE))
606 {
607 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
608 "cancelling");
609 return send_delete(this);
610 }
611 switch (this->method)
612 {
613 case AUTH_XAUTH_INIT_PSK:
614 case AUTH_XAUTH_INIT_RSA:
615 case AUTH_HYBRID_INIT_RSA:
616 /* wait for XAUTH request */
617 break;
618 case AUTH_XAUTH_RESP_PSK:
619 case AUTH_XAUTH_RESP_RSA:
620 case AUTH_HYBRID_RESP_RSA:
621 /* TODO-IKEv1: not yet */
622 return FAILED;
623 default:
624 if (!establish(this))
625 {
626 return send_delete(this);
627 }
628 break;
629 }
630 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
631 {
632 this->ike_sa->queue_task(this->ike_sa,
633 (task_t*)mode_config_create(this->ike_sa, TRUE));
634 }
635 return SUCCESS;
636 }
637 default:
638 return FAILED;
639 }
640 }
641
642 METHOD(task_t, get_type, task_type_t,
643 private_main_mode_t *this)
644 {
645 return TASK_MAIN_MODE;
646 }
647
648 METHOD(task_t, migrate, void,
649 private_main_mode_t *this, ike_sa_t *ike_sa)
650 {
651 DESTROY_IF(this->peer_cfg);
652 DESTROY_IF(this->proposal);
653 this->ph1->destroy(this->ph1);
654
655 this->ike_sa = ike_sa;
656 this->state = MM_INIT;
657 this->peer_cfg = NULL;
658 this->proposal = NULL;
659 this->ph1 = phase1_create(ike_sa, this->initiator);
660 }
661
662 METHOD(task_t, destroy, void,
663 private_main_mode_t *this)
664 {
665 DESTROY_IF(this->peer_cfg);
666 DESTROY_IF(this->proposal);
667 this->ph1->destroy(this->ph1);
668 free(this);
669 }
670
671 /*
672 * Described in header.
673 */
674 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
675 {
676 private_main_mode_t *this;
677
678 INIT(this,
679 .public = {
680 .task = {
681 .get_type = _get_type,
682 .migrate = _migrate,
683 .destroy = _destroy,
684 },
685 },
686 .ike_sa = ike_sa,
687 .ph1 = phase1_create(ike_sa, initiator),
688 .initiator = initiator,
689 .state = MM_INIT,
690 );
691
692 if (initiator)
693 {
694 this->public.task.build = _build_i;
695 this->public.task.process = _process_i;
696 }
697 else
698 {
699 this->public.task.build = _build_r;
700 this->public.task.process = _process_r;
701 }
702
703 return &this->public;
704 }