Merge branch 'ikev1-clean' into ikev1-master
[strongswan.git] / src / libcharon / sa / ikev1 / tasks / aggressive_mode.c
1 /*
2 * Copyright (C) 2012 Martin Willi
3 * Copyright (C) 2012 revosec AG
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "aggressive_mode.h"
17
18 #include <string.h>
19
20 #include <daemon.h>
21 #include <sa/ikev1/phase1.h>
22 #include <encoding/payloads/sa_payload.h>
23 #include <encoding/payloads/id_payload.h>
24 #include <encoding/payloads/hash_payload.h>
25 #include <sa/ikev1/tasks/xauth.h>
26 #include <sa/ikev1/tasks/mode_config.h>
27 #include <sa/ikev1/tasks/informational.h>
28 #include <sa/ikev1/tasks/isakmp_delete.h>
29 #include <processing/jobs/adopt_children_job.h>
30
31 typedef struct private_aggressive_mode_t private_aggressive_mode_t;
32
33 /**
34 * Private members of a aggressive_mode_t task.
35 */
36 struct private_aggressive_mode_t {
37
38 /**
39 * Public methods and task_t interface.
40 */
41 aggressive_mode_t public;
42
43 /**
44 * Assigned IKE_SA.
45 */
46 ike_sa_t *ike_sa;
47
48 /**
49 * Are we the initiator?
50 */
51 bool initiator;
52
53 /**
54 * Common phase 1 helper class
55 */
56 phase1_t *ph1;
57
58 /**
59 * IKE config to establish
60 */
61 ike_cfg_t *ike_cfg;
62
63 /**
64 * Peer config to use
65 */
66 peer_cfg_t *peer_cfg;
67
68 /**
69 * selected IKE proposal
70 */
71 proposal_t *proposal;
72
73 /**
74 * Negotiated SA lifetime
75 */
76 u_int32_t lifetime;
77
78 /**
79 * Negotiated authentication method
80 */
81 auth_method_t method;
82
83 /**
84 * Encoded ID payload, without fixed header
85 */
86 chunk_t id_data;
87
88 /** states of aggressive mode */
89 enum {
90 AM_INIT,
91 AM_AUTH,
92 } state;
93 };
94
95 /**
96 * Set IKE_SA to established state
97 */
98 static bool establish(private_aggressive_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_aggressive_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
146 {
147 DBG1(DBG_IKE, "received %N notify", notify_type_names, type);
148 }
149 }
150 }
151 enumerator->destroy(enumerator);
152
153 return err;
154 }
155
156 /**
157 * Queue a task sending a notify in an INFORMATIONAL exchange
158 */
159 static status_t send_notify(private_aggressive_mode_t *this, notify_type_t type)
160 {
161 notify_payload_t *notify;
162 ike_sa_id_t *ike_sa_id;
163 u_int64_t spi_i, spi_r;
164 chunk_t spi;
165
166 notify = notify_payload_create_from_protocol_and_type(NOTIFY_V1,
167 PROTO_IKE, type);
168 ike_sa_id = this->ike_sa->get_id(this->ike_sa);
169 spi_i = ike_sa_id->get_initiator_spi(ike_sa_id);
170 spi_r = ike_sa_id->get_responder_spi(ike_sa_id);
171 spi = chunk_cata("cc", chunk_from_thing(spi_i), chunk_from_thing(spi_r));
172 notify->set_spi_data(notify, spi);
173
174 this->ike_sa->queue_task(this->ike_sa,
175 (task_t*)informational_create(this->ike_sa, notify));
176 /* cancel all active/passive tasks in favour of informational */
177 return ALREADY_DONE;
178 }
179
180 /**
181 * Queue a delete task if authentication failed as initiator
182 */
183 static status_t send_delete(private_aggressive_mode_t *this)
184 {
185 this->ike_sa->queue_task(this->ike_sa,
186 (task_t*)isakmp_delete_create(this->ike_sa, TRUE));
187 /* cancel all active tasks in favour of informational */
188 return ALREADY_DONE;
189 }
190
191 METHOD(task_t, build_i, status_t,
192 private_aggressive_mode_t *this, message_t *message)
193 {
194 switch (this->state)
195 {
196 case AM_INIT:
197 {
198 sa_payload_t *sa_payload;
199 id_payload_t *id_payload;
200 linked_list_t *proposals;
201 identification_t *id;
202 packet_t *packet;
203 u_int16_t group;
204
205 DBG0(DBG_IKE, "initiating Aggressive Mode IKE_SA %s[%d] to %H",
206 this->ike_sa->get_name(this->ike_sa),
207 this->ike_sa->get_unique_id(this->ike_sa),
208 this->ike_sa->get_other_host(this->ike_sa));
209 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
210
211 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
212 this->peer_cfg = this->ike_sa->get_peer_cfg(this->ike_sa);
213 this->peer_cfg->get_ref(this->peer_cfg);
214
215 this->method = this->ph1->get_auth_method(this->ph1, this->peer_cfg);
216 if (this->method == AUTH_NONE)
217 {
218 DBG1(DBG_CFG, "configuration uses unsupported authentication");
219 return FAILED;
220 }
221 this->lifetime = this->peer_cfg->get_reauth_time(this->peer_cfg,
222 FALSE);
223 if (!this->lifetime)
224 { /* fall back to rekey time of no rekey time configured */
225 this->lifetime = this->peer_cfg->get_rekey_time(this->peer_cfg,
226 FALSE);
227 }
228 this->lifetime += this->peer_cfg->get_over_time(this->peer_cfg);
229 proposals = this->ike_cfg->get_proposals(this->ike_cfg);
230 sa_payload = sa_payload_create_from_proposals_v1(proposals,
231 this->lifetime, 0, this->method, MODE_NONE, FALSE);
232 proposals->destroy_offset(proposals, offsetof(proposal_t, destroy));
233
234 message->add_payload(message, &sa_payload->payload_interface);
235
236 group = this->ike_cfg->get_dh_group(this->ike_cfg);
237 if (group == MODP_NONE)
238 {
239 DBG1(DBG_IKE, "DH group selection failed");
240 return FAILED;
241 }
242 if (!this->ph1->create_dh(this->ph1, group))
243 {
244 DBG1(DBG_IKE, "DH group %N not supported",
245 diffie_hellman_group_names, group);
246 return FAILED;
247 }
248 if (!this->ph1->add_nonce_ke(this->ph1, message))
249 {
250 return FAILED;
251 }
252 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
253 if (!id)
254 {
255 DBG1(DBG_CFG, "own identity not known");
256 return FAILED;
257 }
258 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
259 id_payload = id_payload_create_from_identification(ID_V1, id);
260 this->id_data = id_payload->get_encoded(id_payload);
261 message->add_payload(message, &id_payload->payload_interface);
262
263 /* pregenerate message to store SA payload */
264 if (this->ike_sa->generate_message(this->ike_sa, message,
265 &packet) != SUCCESS)
266 {
267 DBG1(DBG_IKE, "pregenerating SA payload failed");
268 return FAILED;
269 }
270 packet->destroy(packet);
271 if (!this->ph1->save_sa_payload(this->ph1, message))
272 {
273 DBG1(DBG_IKE, "SA payload invalid");
274 return FAILED;
275 }
276 this->state = AM_AUTH;
277 return NEED_MORE;
278 }
279 case AM_AUTH:
280 {
281 if (!this->ph1->build_auth(this->ph1, this->method, message,
282 this->id_data))
283 {
284 this->id_data = chunk_empty;
285 return send_notify(this, AUTHENTICATION_FAILED);
286 }
287 this->id_data = chunk_empty;
288
289 switch (this->method)
290 {
291 case AUTH_XAUTH_INIT_PSK:
292 case AUTH_XAUTH_INIT_RSA:
293 case AUTH_HYBRID_INIT_RSA:
294 /* wait for XAUTH request */
295 break;
296 case AUTH_XAUTH_RESP_PSK:
297 case AUTH_XAUTH_RESP_RSA:
298 case AUTH_HYBRID_RESP_RSA:
299 /* TODO-IKEv1: not yet */
300 return FAILED;
301 default:
302 if (!establish(this))
303 {
304 return send_notify(this, AUTHENTICATION_FAILED);
305 }
306 break;
307 }
308 if (this->peer_cfg->get_virtual_ip(this->peer_cfg))
309 {
310 this->ike_sa->queue_task(this->ike_sa,
311 (task_t*)mode_config_create(this->ike_sa, TRUE));
312 }
313 return SUCCESS;
314 }
315 default:
316 return FAILED;
317 }
318 }
319
320 METHOD(task_t, process_r, status_t,
321 private_aggressive_mode_t *this, message_t *message)
322 {
323 switch (this->state)
324 {
325 case AM_INIT:
326 {
327 sa_payload_t *sa_payload;
328 id_payload_t *id_payload;
329 identification_t *id;
330 linked_list_t *list;
331 u_int16_t group;
332
333 this->ike_cfg = this->ike_sa->get_ike_cfg(this->ike_sa);
334 DBG0(DBG_IKE, "%H is initiating a Aggressive Mode IKE_SA",
335 message->get_source(message));
336 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
337
338 this->ike_sa->update_hosts(this->ike_sa,
339 message->get_destination(message),
340 message->get_source(message), TRUE);
341
342 sa_payload = (sa_payload_t*)message->get_payload(message,
343 SECURITY_ASSOCIATION_V1);
344 if (!sa_payload)
345 {
346 DBG1(DBG_IKE, "SA payload missing");
347 return send_notify(this, INVALID_PAYLOAD_TYPE);
348 }
349 if (!this->ph1->save_sa_payload(this->ph1, message))
350 {
351 return send_notify(this, INVALID_PAYLOAD_TYPE);
352 }
353
354 list = sa_payload->get_proposals(sa_payload);
355 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
356 list, FALSE);
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 if (!this->proposal->get_algorithm(this->proposal,
369 DIFFIE_HELLMAN_GROUP, &group, NULL))
370 {
371 DBG1(DBG_IKE, "DH group selection failed");
372 return send_notify(this, INVALID_KEY_INFORMATION);
373 }
374 if (!this->ph1->create_dh(this->ph1, group))
375 {
376 DBG1(DBG_IKE, "negotiated DH group not supported");
377 return send_notify(this, INVALID_KEY_INFORMATION);
378 }
379 if (!this->ph1->get_nonce_ke(this->ph1, message))
380 {
381 return send_notify(this, INVALID_PAYLOAD_TYPE);
382 }
383
384 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
385 if (!id_payload)
386 {
387 DBG1(DBG_IKE, "IDii payload missing");
388 return send_notify(this, INVALID_PAYLOAD_TYPE);
389 }
390
391 id = id_payload->get_identification(id_payload);
392 this->id_data = id_payload->get_encoded(id_payload);
393 this->ike_sa->set_other_id(this->ike_sa, id);
394 this->peer_cfg = this->ph1->select_config(this->ph1,
395 this->method, TRUE, id);
396 if (!this->peer_cfg)
397 {
398 DBG1(DBG_IKE, "no peer config found");
399 return send_notify(this, AUTHENTICATION_FAILED);
400 }
401 this->ike_sa->set_peer_cfg(this->ike_sa, this->peer_cfg);
402
403 this->state = AM_AUTH;
404 if (has_notify_errors(this, message))
405 {
406 return FAILED;
407 }
408 return NEED_MORE;
409 }
410 case AM_AUTH:
411 {
412 if (!this->ph1->verify_auth(this->ph1, this->method, message,
413 this->id_data))
414 {
415 this->id_data = chunk_empty;
416 return send_delete(this);
417 }
418 this->id_data = chunk_empty;
419
420 if (!charon->bus->authorize(charon->bus, FALSE))
421 {
422 DBG1(DBG_IKE, "Aggressive Mode authorization hook forbids "
423 "IKE_SA, cancelling");
424 return send_delete(this);
425 }
426
427 switch (this->method)
428 {
429 case AUTH_XAUTH_INIT_PSK:
430 case AUTH_XAUTH_INIT_RSA:
431 case AUTH_HYBRID_INIT_RSA:
432 this->ike_sa->queue_task(this->ike_sa,
433 (task_t*)xauth_create(this->ike_sa, TRUE));
434 return SUCCESS;
435 case AUTH_XAUTH_RESP_PSK:
436 case AUTH_XAUTH_RESP_RSA:
437 case AUTH_HYBRID_RESP_RSA:
438 /* TODO-IKEv1: not yet supported */
439 return FAILED;
440 default:
441 if (!establish(this))
442 {
443 return send_delete(this);
444 }
445 lib->processor->queue_job(lib->processor, (job_t*)
446 adopt_children_job_create(
447 this->ike_sa->get_id(this->ike_sa)));
448 return SUCCESS;
449 }
450 }
451 default:
452 return FAILED;
453 }
454 }
455
456 METHOD(task_t, build_r, status_t,
457 private_aggressive_mode_t *this, message_t *message)
458 {
459 if (this->state == AM_AUTH)
460 {
461 sa_payload_t *sa_payload;
462 id_payload_t *id_payload;
463 identification_t *id;
464
465 sa_payload = sa_payload_create_from_proposal_v1(this->proposal,
466 this->lifetime, 0, this->method, MODE_NONE, FALSE);
467 message->add_payload(message, &sa_payload->payload_interface);
468
469 if (!this->ph1->add_nonce_ke(this->ph1, message))
470 {
471 return send_notify(this, INVALID_KEY_INFORMATION);
472 }
473 if (!this->ph1->create_hasher(this->ph1))
474 {
475 return send_notify(this, NO_PROPOSAL_CHOSEN);
476 }
477 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
478 {
479 return send_notify(this, INVALID_KEY_INFORMATION);
480 }
481
482 id = this->ph1->get_id(this->ph1, this->peer_cfg, TRUE);
483 if (!id)
484 {
485 DBG1(DBG_CFG, "own identity not known");
486 return send_notify(this, INVALID_ID_INFORMATION);
487 }
488 this->ike_sa->set_my_id(this->ike_sa, id->clone(id));
489
490 id_payload = id_payload_create_from_identification(ID_V1, id);
491 message->add_payload(message, &id_payload->payload_interface);
492
493 if (!this->ph1->build_auth(this->ph1, this->method, message,
494 id_payload->get_encoded(id_payload)))
495 {
496 return send_notify(this, AUTHENTICATION_FAILED);
497 }
498 return NEED_MORE;
499 }
500 return FAILED;
501 }
502
503 METHOD(task_t, process_i, status_t,
504 private_aggressive_mode_t *this, message_t *message)
505 {
506 if (this->state == AM_AUTH)
507 {
508 auth_method_t method;
509 sa_payload_t *sa_payload;
510 id_payload_t *id_payload;
511 identification_t *id, *cid;
512 linked_list_t *list;
513 u_int32_t lifetime;
514
515 sa_payload = (sa_payload_t*)message->get_payload(message,
516 SECURITY_ASSOCIATION_V1);
517 if (!sa_payload)
518 {
519 DBG1(DBG_IKE, "SA payload missing");
520 return send_notify(this, INVALID_PAYLOAD_TYPE);
521 }
522 list = sa_payload->get_proposals(sa_payload);
523 this->proposal = this->ike_cfg->select_proposal(this->ike_cfg,
524 list, FALSE);
525 list->destroy_offset(list, offsetof(proposal_t, destroy));
526 if (!this->proposal)
527 {
528 DBG1(DBG_IKE, "no proposal found");
529 return send_notify(this, NO_PROPOSAL_CHOSEN);
530 }
531 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
532
533 lifetime = sa_payload->get_lifetime(sa_payload);
534 if (lifetime != this->lifetime)
535 {
536 DBG1(DBG_IKE, "received lifetime %us does not match configured "
537 "lifetime %us", lifetime, this->lifetime);
538 }
539 this->lifetime = lifetime;
540 method = sa_payload->get_auth_method(sa_payload);
541 if (method != this->method)
542 {
543 DBG1(DBG_IKE, "received %N authentication, but configured %N, "
544 "continue with configured", auth_method_names, method,
545 auth_method_names, this->method);
546 }
547 if (!this->ph1->get_nonce_ke(this->ph1, message))
548 {
549 return send_notify(this, INVALID_PAYLOAD_TYPE);
550 }
551 if (!this->ph1->create_hasher(this->ph1))
552 {
553 return send_notify(this, NO_PROPOSAL_CHOSEN);
554 }
555 if (!this->ph1->derive_keys(this->ph1, this->peer_cfg, this->method))
556 {
557 return send_notify(this, INVALID_KEY_INFORMATION);
558 }
559
560 id_payload = (id_payload_t*)message->get_payload(message, ID_V1);
561 if (!id_payload)
562 {
563 DBG1(DBG_IKE, "IDir payload missing");
564 return send_delete(this);
565 }
566 id = id_payload->get_identification(id_payload);
567 cid = this->ph1->get_id(this->ph1, this->peer_cfg, FALSE);
568 if (cid && !id->matches(id, cid))
569 {
570 DBG1(DBG_IKE, "IDir '%Y' does not match to '%Y'", id, cid);
571 id->destroy(id);
572 return send_notify(this, INVALID_ID_INFORMATION);
573 }
574 this->ike_sa->set_other_id(this->ike_sa, id);
575
576 if (!this->ph1->verify_auth(this->ph1, this->method, message,
577 id_payload->get_encoded(id_payload)))
578 {
579 return send_notify(this, AUTHENTICATION_FAILED);
580 }
581 if (!charon->bus->authorize(charon->bus, FALSE))
582 {
583 DBG1(DBG_IKE, "Aggressive Mode authorization hook forbids IKE_SA, "
584 "cancelling");
585 return send_notify(this, AUTHENTICATION_FAILED);
586 }
587
588 return NEED_MORE;
589 }
590 return FAILED;
591 }
592
593 METHOD(task_t, get_type, task_type_t,
594 private_aggressive_mode_t *this)
595 {
596 return TASK_AGGRESSIVE_MODE;
597 }
598
599 METHOD(task_t, migrate, void,
600 private_aggressive_mode_t *this, ike_sa_t *ike_sa)
601 {
602 DESTROY_IF(this->peer_cfg);
603 DESTROY_IF(this->proposal);
604 this->ph1->destroy(this->ph1);
605 chunk_free(&this->id_data);
606
607 this->ike_sa = ike_sa;
608 this->state = AM_INIT;
609 this->peer_cfg = NULL;
610 this->proposal = NULL;
611 this->ph1 = phase1_create(ike_sa, this->initiator);
612 }
613
614 METHOD(task_t, destroy, void,
615 private_aggressive_mode_t *this)
616 {
617 DESTROY_IF(this->peer_cfg);
618 DESTROY_IF(this->proposal);
619 this->ph1->destroy(this->ph1);
620 chunk_free(&this->id_data);
621 free(this);
622 }
623
624 /*
625 * Described in header.
626 */
627 aggressive_mode_t *aggressive_mode_create(ike_sa_t *ike_sa, bool initiator)
628 {
629 private_aggressive_mode_t *this;
630
631 INIT(this,
632 .public = {
633 .task = {
634 .get_type = _get_type,
635 .migrate = _migrate,
636 .destroy = _destroy,
637 },
638 },
639 .ike_sa = ike_sa,
640 .ph1 = phase1_create(ike_sa, initiator),
641 .initiator = initiator,
642 .state = AM_INIT,
643 );
644
645 if (initiator)
646 {
647 this->public.task.build = _build_i;
648 this->public.task.process = _process_i;
649 }
650 else
651 {
652 this->public.task.build = _build_r;
653 this->public.task.process = _process_r;
654 }
655
656 return &this->public;
657 }