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