a7be22916e7534a359009083811c273257cefb75
[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
326 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
327 DBG0(DBG_IKE, "%H is initiating a Main Mode IKE_SA",
328 message->get_source(message));
329 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
330
331 this->ike_sa->update_hosts(this->ike_sa,
332 message->get_destination(message),
333 message->get_source(message), TRUE);
334
335 sa_payload = (sa_payload_t*)message->get_payload(message,
336 SECURITY_ASSOCIATION_V1);
337 if (!sa_payload)
338 {
339 DBG1(DBG_IKE, "SA payload missing");
340 return send_notify(this, INVALID_PAYLOAD_TYPE);
341 }
342 if (!this->ph1->save_sa_payload(this->ph1, message))
343 {
344 return send_notify(this, INVALID_PAYLOAD_TYPE);
345 }
346
347 list = sa_payload->get_proposals(sa_payload);
348 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
349 list, FALSE);
350 list->destroy_offset(list, offsetof(proposal_t, destroy));
351 if (!this->proposal)
352 {
353 DBG1(DBG_IKE, "no proposal found");
354 return send_notify(this, NO_PROPOSAL_CHOSEN);
355 }
356 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
357
358 this->method = sa_payload->get_auth_method(sa_payload);
359 this->lifetime = sa_payload->get_lifetime(sa_payload);
360
361 this->state = MM_SA;
362 return NEED_MORE;
363 }
364 case MM_SA:
365 {
366 u_int16_t group;
367
368 if (!this->ph1->create_hasher(this->ph1))
369 {
370 return send_notify(this, INVALID_KEY_INFORMATION);
371 }
372 if (!this->proposal->get_algorithm(this->proposal,
373 DIFFIE_HELLMAN_GROUP, &group, NULL))
374 {
375 DBG1(DBG_IKE, "DH group selection failed");
376 return send_notify(this, INVALID_KEY_INFORMATION);
377 }
378 if (!this->ph1->create_dh(this->ph1, group))
379 {
380 DBG1(DBG_IKE, "negotiated DH group not supported");
381 return send_notify(this, INVALID_KEY_INFORMATION);
382 }
383 if (!this->ph1->get_nonce_ke(this->ph1, message))
384 {
385 return send_notify(this, INVALID_PAYLOAD_TYPE);
386 }
387 this->state = MM_KE;
388 return NEED_MORE;
389 }
390 case MM_KE:
391 {
392 id_payload_t *id_payload;
393 identification_t *id;
394
395 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
396 if (!id_payload)
397 {
398 DBG1(DBG_IKE, "IDii payload missing");
399 return send_notify(this, INVALID_PAYLOAD_TYPE);
400 }
401
402 id = id_payload->get_identification(id_payload);
403 this->ike_sa->set_other_id(this->ike_sa, id);
404 this->peer_cfg = this->ph1->select_config(this->ph1,
405 this->method, FALSE, id);
406 if (!this->peer_cfg)
407 {
408 DBG1(DBG_IKE, "no peer config found");
409 return send_notify(this, AUTHENTICATION_FAILED);
410 }
411 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
412
413 if (!this->ph1->verify_auth(this->ph1, this->method, message,
414 id_payload->get_encoded(id_payload)))
415 {
416 return send_notify(this, AUTHENTICATION_FAILED);
417 }
418 if (!charon->bus->authorize(charon->bus, FALSE))
419 {
420 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
421 "cancelling");
422 return send_notify(this, AUTHENTICATION_FAILED);
423 }
424
425 this->state = MM_AUTH;
426 if (has_notify_errors(this, message))
427 {
428 return FAILED;
429 }
430 return NEED_MORE;
431 }
432 default:
433 return FAILED;
434 }
435 }
436
437 METHOD(task_t, build_r, status_t,
438 private_main_mode_t *this, message_t *message)
439 {
440 switch (this->state)
441 {
442 case MM_SA:
443 {
444 sa_payload_t *sa_payload;
445
446 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
447 this->lifetime, 0, this->method, MODE_NONE, FALSE);
448 message->add_payload(message, &sa_payload->payload_interface);
449
450 return NEED_MORE;
451 }
452 case MM_KE:
453 {
454 if (!this->ph1->add_nonce_ke(this->ph1, message))
455 {
456 return send_notify(this, INVALID_KEY_INFORMATION);
457 }
458 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
459 {
460 return send_notify(this, INVALID_KEY_INFORMATION);
461 }
462 return NEED_MORE;
463 }
464 case MM_AUTH:
465 {
466 id_payload_t *id_payload;
467 identification_t *id;
468
469 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
470 if (!id)
471 {
472 DBG1(DBG_CFG, "own identity not known");
473 return send_notify(this, INVALID_ID_INFORMATION);
474 }
475 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
476
477 id_payload = id_payload_create_from_identification(ID_V1, id);
478 message->add_payload(message, &id_payload->payload_interface);
479
480 if (!this->ph1->build_auth(this->ph1, this->method, message,
481 id_payload->get_encoded(id_payload)))
482 {
483 return send_notify(this, AUTHENTICATION_FAILED);
484 }
485
486 switch (this->method)
487 {
488 case AUTH_XAUTH_INIT_PSK:
489 case AUTH_XAUTH_INIT_RSA:
490 case AUTH_HYBRID_INIT_RSA:
491 this->ike_sa->queue_task(this->ike_sa,
492 (task_t*)xauth_create(this->ike_sa, TRUE));
493 return SUCCESS;
494 case AUTH_XAUTH_RESP_PSK:
495 case AUTH_XAUTH_RESP_RSA:
496 case AUTH_HYBRID_RESP_RSA:
497 /* TODO-IKEv1: not yet supported */
498 return FAILED;
499 default:
500 if (!establish(this))
501 {
502 return send_notify(this, AUTHENTICATION_FAILED);
503 }
504 lib->processor->queue_job(lib->processor, (job_t*)
505 adopt_children_job_create(
506 this->ike_sa->get_id(this->ike_sa)));
507 return SUCCESS;
508 }
509 }
510 default:
511 return FAILED;
512 }
513 }
514
515 METHOD(task_t, process_i, status_t,
516 private_main_mode_t *this, message_t *message)
517 {
518 switch (this->state)
519 {
520 case MM_SA:
521 {
522 linked_list_t *list;
523 sa_payload_t *sa_payload;
524 auth_method_t method;
525 u_int32_t lifetime;
526
527 sa_payload = (sa_payload_t*)message->get_payload(message,
528 SECURITY_ASSOCIATION_V1);
529 if (!sa_payload)
530 {
531 DBG1(DBG_IKE, "SA payload missing");
532 return send_notify(this, INVALID_PAYLOAD_TYPE);
533 }
534 list = sa_payload->get_proposals(sa_payload);
535 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
536 list, FALSE);
537 list->destroy_offset(list, offsetof(proposal_t, destroy));
538 if (!this->proposal)
539 {
540 DBG1(DBG_IKE, "no proposal found");
541 return send_notify(this, NO_PROPOSAL_CHOSEN);
542 }
543 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
544
545 lifetime = sa_payload->get_lifetime(sa_payload);
546 if (lifetime != this->lifetime)
547 {
548 DBG1(DBG_IKE, "received lifetime %us does not match configured "
549 "lifetime %us", lifetime, this->lifetime);
550 }
551 this->lifetime = lifetime;
552 method = sa_payload->get_auth_method(sa_payload);
553 if (method != this->method)
554 {
555 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
556 "continue with configured", auth_method_names, method,
557 auth_method_names, this->method);
558 }
559 return NEED_MORE;
560 }
561 case MM_KE:
562 {
563 if (!this->ph1->get_nonce_ke(this->ph1, message))
564 {
565 return send_notify(this, INVALID_PAYLOAD_TYPE);
566 }
567 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
568 {
569 return send_notify(this, INVALID_KEY_INFORMATION);
570 }
571 return NEED_MORE;
572 }
573 case MM_AUTH:
574 {
575 id_payload_t *id_payload;
576 identification_t *id, *cid;
577
578 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
579 if (!id_payload)
580 {
581 DBG1(DBG_IKE, "IDir payload missing");
582 return send_delete(this);
583 }
584 id = id_payload->get_identification(id_payload);
585 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
586 if (cid && !id->matches(id, cid))
587 {
588 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
589 id->destroy(id);
590 return send_delete(this);
591 }
592 this->ike_sa->set_other_id(this->ike_sa, id);
593
594 if (!this->ph1->verify_auth(this->ph1, this->method, message,
595 id_payload->get_encoded(id_payload)))
596 {
597 return send_delete(this);
598 }
599 if (!charon->bus->authorize(charon->bus, FALSE))
600 {
601 DBG1(DBG_IKE, "Main Mode authorization hook forbids IKE_SA, "
602 "cancelling");
603 return send_delete(this);
604 }
605 switch (this->method)
606 {
607 case AUTH_XAUTH_INIT_PSK:
608 case AUTH_XAUTH_INIT_RSA:
609 case AUTH_HYBRID_INIT_RSA:
610 /* wait for XAUTH request */
611 break;
612 case AUTH_XAUTH_RESP_PSK:
613 case AUTH_XAUTH_RESP_RSA:
614 case AUTH_HYBRID_RESP_RSA:
615 /* TODO-IKEv1: not yet */
616 return FAILED;
617 default:
618 if (!establish(this))
619 {
620 return send_delete(this);
621 }
622 break;
623 }
624 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
625 {
626 this->ike_sa->queue_task(this->ike_sa,
627 (task_t*)mode_config_create(this->ike_sa, TRUE));
628 }
629 return SUCCESS;
630 }
631 default:
632 return FAILED;
633 }
634 }
635
636 METHOD(task_t, get_type, task_type_t,
637 private_main_mode_t *this)
638 {
639 return TASK_MAIN_MODE;
640 }
641
642 METHOD(task_t, migrate, void,
643 private_main_mode_t *this, ike_sa_t *ike_sa)
644 {
645 DESTROY_IF(this->peer_cfg);
646 DESTROY_IF(this->proposal);
647 this->ph1->destroy(this->ph1);
648
649 this->ike_sa = ike_sa;
650 this->state = MM_INIT;
651 this->peer_cfg = NULL;
652 this->proposal = NULL;
653 this->ph1 = phase1_create(ike_sa, this->initiator);
654 }
655
656 METHOD(task_t, destroy, void,
657 private_main_mode_t *this)
658 {
659 DESTROY_IF(this->peer_cfg);
660 DESTROY_IF(this->proposal);
661 this->ph1->destroy(this->ph1);
662 free(this);
663 }
664
665 /*
666 * Described in header.
667 */
668 main_mode_t *main_mode_create(ike_sa_t *ike_sa, bool initiator)
669 {
670 private_main_mode_t *this;
671
672 INIT(this,
673 .public = {
674 .task = {
675 .get_type = _get_type,
676 .migrate = _migrate,
677 .destroy = _destroy,
678 },
679 },
680 .ike_sa = ike_sa,
681 .ph1 = phase1_create(ike_sa, initiator),
682 .initiator = initiator,
683 .state = MM_INIT,
684 );
685
686 if (initiator)
687 {
688 this->public.task.build = _build_i;
689 this->public.task.process = _process_i;
690 }
691 else
692 {
693 this->public.task.build = _build_r;
694 this->public.task.process = _process_r;
695 }
696
697 return &this->public;
698 }