ikev2: Add a global option to disable RFC 7427 signature authentication
[strongswan.git] / src / libcharon / sa / ikev2 / tasks / ike_init.c
1 /*
2 * Copyright (C) 2008-2015 Tobias Brunner
3 * Copyright (C) 2005-2008 Martin Willi
4 * Copyright (C) 2005 Jan Hutter
5 * Hochschule fuer Technik Rapperswil
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 */
17
18 #include "ike_init.h"
19
20 #include <string.h>
21
22 #include <daemon.h>
23 #include <bio/bio_reader.h>
24 #include <bio/bio_writer.h>
25 #include <sa/ikev2/keymat_v2.h>
26 #include <crypto/diffie_hellman.h>
27 #include <encoding/payloads/sa_payload.h>
28 #include <encoding/payloads/ke_payload.h>
29 #include <encoding/payloads/nonce_payload.h>
30
31 /** maximum retries to do with cookies/other dh groups */
32 #define MAX_RETRIES 5
33
34 typedef struct private_ike_init_t private_ike_init_t;
35
36 /**
37 * Private members of a ike_init_t task.
38 */
39 struct private_ike_init_t {
40
41 /**
42 * Public methods and task_t interface.
43 */
44 ike_init_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 * IKE config to establish
58 */
59 ike_cfg_t *config;
60
61 /**
62 * diffie hellman group to use
63 */
64 diffie_hellman_group_t dh_group;
65
66 /**
67 * diffie hellman key exchange
68 */
69 diffie_hellman_t *dh;
70
71 /**
72 * Keymat derivation (from IKE_SA)
73 */
74 keymat_v2_t *keymat;
75
76 /**
77 * nonce chosen by us
78 */
79 chunk_t my_nonce;
80
81 /**
82 * nonce chosen by peer
83 */
84 chunk_t other_nonce;
85
86 /**
87 * Negotiated proposal used for IKE_SA
88 */
89 proposal_t *proposal;
90
91 /**
92 * Old IKE_SA which gets rekeyed
93 */
94 ike_sa_t *old_sa;
95
96 /**
97 * cookie received from responder
98 */
99 chunk_t cookie;
100
101 /**
102 * retries done so far after failure (cookie or bad dh group)
103 */
104 u_int retry;
105
106 /**
107 * Whether to use Signature Authentication as per RFC 7427
108 */
109 bool signature_authentication;
110 };
111
112 /**
113 * Notify the peer about the hash algorithms we support, as per RFC 7427
114 */
115 static void send_supported_hash_algorithms(message_t *message)
116 {
117 enumerator_t *enumerator;
118 bio_writer_t *writer;
119 hash_algorithm_t hash;
120 char *plugin_name;
121
122 /* TODO-SIG: As initiator we could send only the algorithms we expect based
123 * on e.g. rightauth config */
124 writer = bio_writer_create(0);
125 enumerator = lib->crypto->create_hasher_enumerator(lib->crypto);
126 while (enumerator->enumerate(enumerator, &hash, &plugin_name))
127 {
128 if (hasher_algorithm_for_ikev2(hash))
129 {
130 writer->write_uint16(writer, hash);
131 }
132 }
133 enumerator->destroy(enumerator);
134
135 if (writer->get_buf(writer).len)
136 {
137 message->add_notify(message, FALSE, SIGNATURE_HASH_ALGORITHMS,
138 writer->get_buf(writer));
139 }
140 writer->destroy(writer);
141 }
142
143 /**
144 * Store algorithms supported by other peer
145 */
146 static void handle_supported_hash_algorithms(private_ike_init_t *this,
147 notify_payload_t *notify)
148 {
149 bio_reader_t *reader;
150 u_int16_t algo;
151 bool added = FALSE;
152
153 reader = bio_reader_create(notify->get_notification_data(notify));
154 while (reader->remaining(reader) >= 2 && reader->read_uint16(reader, &algo))
155 {
156 if (hasher_algorithm_for_ikev2(algo))
157 {
158 this->keymat->add_hash_algorithm(this->keymat, algo);
159 added = TRUE;
160 }
161 }
162 reader->destroy(reader);
163
164 if (added)
165 {
166 this->ike_sa->enable_extension(this->ike_sa, EXT_SIGNATURE_AUTH);
167 }
168 }
169
170 /**
171 * build the payloads for the message
172 */
173 static void build_payloads(private_ike_init_t *this, message_t *message)
174 {
175 sa_payload_t *sa_payload;
176 ke_payload_t *ke_payload;
177 nonce_payload_t *nonce_payload;
178 linked_list_t *proposal_list;
179 ike_sa_id_t *id;
180 proposal_t *proposal;
181 enumerator_t *enumerator;
182
183 id = this->ike_sa->get_id(this->ike_sa);
184
185 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
186
187 if (this->initiator)
188 {
189 proposal_list = this->config->get_proposals(this->config);
190 if (this->old_sa)
191 {
192 /* include SPI of new IKE_SA when we are rekeying */
193 enumerator = proposal_list->create_enumerator(proposal_list);
194 while (enumerator->enumerate(enumerator, (void**)&proposal))
195 {
196 proposal->set_spi(proposal, id->get_initiator_spi(id));
197 }
198 enumerator->destroy(enumerator);
199 }
200
201 sa_payload = sa_payload_create_from_proposals_v2(proposal_list);
202 proposal_list->destroy_offset(proposal_list, offsetof(proposal_t, destroy));
203 }
204 else
205 {
206 if (this->old_sa)
207 {
208 /* include SPI of new IKE_SA when we are rekeying */
209 this->proposal->set_spi(this->proposal, id->get_responder_spi(id));
210 }
211 sa_payload = sa_payload_create_from_proposal_v2(this->proposal);
212 }
213 message->add_payload(message, (payload_t*)sa_payload);
214
215 nonce_payload = nonce_payload_create(PLV2_NONCE);
216 nonce_payload->set_nonce(nonce_payload, this->my_nonce);
217 ke_payload = ke_payload_create_from_diffie_hellman(PLV2_KEY_EXCHANGE, this->dh);
218
219 if (this->old_sa)
220 { /* payload order differs if we are rekeying */
221 message->add_payload(message, (payload_t*)nonce_payload);
222 message->add_payload(message, (payload_t*)ke_payload);
223 }
224 else
225 {
226 message->add_payload(message, (payload_t*)ke_payload);
227 message->add_payload(message, (payload_t*)nonce_payload);
228 }
229
230 /* negotiate fragmentation if we are not rekeying */
231 if (!this->old_sa &&
232 this->config->fragmentation(this->config) != FRAGMENTATION_NO)
233 {
234 if (this->initiator ||
235 this->ike_sa->supports_extension(this->ike_sa,
236 EXT_IKE_FRAGMENTATION))
237 {
238 message->add_notify(message, FALSE, FRAGMENTATION_SUPPORTED,
239 chunk_empty);
240 }
241 }
242 /* submit supported hash algorithms for signature authentication */
243 if (!this->old_sa && this->signature_authentication)
244 {
245 if (this->initiator ||
246 this->ike_sa->supports_extension(this->ike_sa,
247 EXT_SIGNATURE_AUTH))
248 {
249 send_supported_hash_algorithms(message);
250 }
251 }
252 }
253
254 /**
255 * Read payloads from message
256 */
257 static void process_payloads(private_ike_init_t *this, message_t *message)
258 {
259 enumerator_t *enumerator;
260 payload_t *payload;
261 ke_payload_t *ke_payload = NULL;
262
263 enumerator = message->create_payload_enumerator(message);
264 while (enumerator->enumerate(enumerator, &payload))
265 {
266 switch (payload->get_type(payload))
267 {
268 case PLV2_SECURITY_ASSOCIATION:
269 {
270 sa_payload_t *sa_payload = (sa_payload_t*)payload;
271 linked_list_t *proposal_list;
272 bool private;
273
274 proposal_list = sa_payload->get_proposals(sa_payload);
275 private = this->ike_sa->supports_extension(this->ike_sa,
276 EXT_STRONGSWAN);
277 this->proposal = this->config->select_proposal(this->config,
278 proposal_list, private);
279 if (!this->proposal)
280 {
281 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE,
282 proposal_list);
283 }
284 proposal_list->destroy_offset(proposal_list,
285 offsetof(proposal_t, destroy));
286 break;
287 }
288 case PLV2_KEY_EXCHANGE:
289 {
290 ke_payload = (ke_payload_t*)payload;
291
292 this->dh_group = ke_payload->get_dh_group_number(ke_payload);
293 break;
294 }
295 case PLV2_NONCE:
296 {
297 nonce_payload_t *nonce_payload = (nonce_payload_t*)payload;
298
299 this->other_nonce = nonce_payload->get_nonce(nonce_payload);
300 break;
301 }
302 case PLV2_NOTIFY:
303 {
304 notify_payload_t *notify = (notify_payload_t*)payload;
305
306 switch (notify->get_notify_type(notify))
307 {
308 case FRAGMENTATION_SUPPORTED:
309 this->ike_sa->enable_extension(this->ike_sa,
310 EXT_IKE_FRAGMENTATION);
311 break;
312 case SIGNATURE_HASH_ALGORITHMS:
313 if (this->signature_authentication)
314 {
315 handle_supported_hash_algorithms(this, notify);
316 }
317 break;
318 default:
319 /* other notifies are handled elsewhere */
320 break;
321 }
322
323 }
324 default:
325 break;
326 }
327 }
328 enumerator->destroy(enumerator);
329
330 if (ke_payload && this->proposal &&
331 this->proposal->has_dh_group(this->proposal, this->dh_group))
332 {
333 if (!this->initiator)
334 {
335 this->dh = this->keymat->keymat.create_dh(
336 &this->keymat->keymat, this->dh_group);
337 }
338 if (this->dh)
339 {
340 this->dh->set_other_public_value(this->dh,
341 ke_payload->get_key_exchange_data(ke_payload));
342 }
343 }
344 }
345
346 METHOD(task_t, build_i, status_t,
347 private_ike_init_t *this, message_t *message)
348 {
349 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
350 DBG0(DBG_IKE, "initiating IKE_SA %s[%d] to %H",
351 this->ike_sa->get_name(this->ike_sa),
352 this->ike_sa->get_unique_id(this->ike_sa),
353 this->ike_sa->get_other_host(this->ike_sa));
354 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
355
356 if (this->retry >= MAX_RETRIES)
357 {
358 DBG1(DBG_IKE, "giving up after %d retries", MAX_RETRIES);
359 return FAILED;
360 }
361
362 /* if the DH group is set via use_dh_group(), we already have a DH object */
363 if (!this->dh)
364 {
365 this->dh_group = this->config->get_dh_group(this->config);
366 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
367 this->dh_group);
368 if (!this->dh)
369 {
370 DBG1(DBG_IKE, "configured DH group %N not supported",
371 diffie_hellman_group_names, this->dh_group);
372 return FAILED;
373 }
374 }
375
376 /* generate nonce only when we are trying the first time */
377 if (this->my_nonce.ptr == NULL)
378 {
379 nonce_gen_t *nonceg;
380
381 nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
382 if (!nonceg)
383 {
384 DBG1(DBG_IKE, "no nonce generator found to create nonce");
385 return FAILED;
386 }
387 if (!nonceg->allocate_nonce(nonceg, NONCE_SIZE, &this->my_nonce))
388 {
389 DBG1(DBG_IKE, "nonce allocation failed");
390 nonceg->destroy(nonceg);
391 return FAILED;
392 }
393 nonceg->destroy(nonceg);
394 }
395
396 if (this->cookie.ptr)
397 {
398 message->add_notify(message, FALSE, COOKIE, this->cookie);
399 }
400
401 build_payloads(this, message);
402
403 #ifdef ME
404 {
405 chunk_t connect_id = this->ike_sa->get_connect_id(this->ike_sa);
406 if (connect_id.ptr)
407 {
408 message->add_notify(message, FALSE, ME_CONNECTID, connect_id);
409 }
410 }
411 #endif /* ME */
412
413 return NEED_MORE;
414 }
415
416 METHOD(task_t, process_r, status_t,
417 private_ike_init_t *this, message_t *message)
418 {
419 nonce_gen_t *nonceg;
420
421 this->config = this->ike_sa->get_ike_cfg(this->ike_sa);
422 DBG0(DBG_IKE, "%H is initiating an IKE_SA", message->get_source(message));
423 this->ike_sa->set_state(this->ike_sa, IKE_CONNECTING);
424
425 nonceg = this->keymat->keymat.create_nonce_gen(&this->keymat->keymat);
426 if (!nonceg)
427 {
428 DBG1(DBG_IKE, "no nonce generator found to create nonce");
429 return FAILED;
430 }
431 if (!nonceg->allocate_nonce(nonceg, NONCE_SIZE, &this->my_nonce))
432 {
433 DBG1(DBG_IKE, "nonce allocation failed");
434 nonceg->destroy(nonceg);
435 return FAILED;
436 }
437 nonceg->destroy(nonceg);
438
439 #ifdef ME
440 {
441 notify_payload_t *notify = message->get_notify(message, ME_CONNECTID);
442 if (notify)
443 {
444 chunk_t connect_id = notify->get_notification_data(notify);
445 DBG2(DBG_IKE, "received ME_CONNECTID %#B", &connect_id);
446 charon->connect_manager->stop_checks(charon->connect_manager,
447 connect_id);
448 }
449 }
450 #endif /* ME */
451
452 process_payloads(this, message);
453
454 return NEED_MORE;
455 }
456
457 /**
458 * Derive the keymat for the IKE_SA
459 */
460 static bool derive_keys(private_ike_init_t *this,
461 chunk_t nonce_i, chunk_t nonce_r)
462 {
463 keymat_v2_t *old_keymat;
464 pseudo_random_function_t prf_alg = PRF_UNDEFINED;
465 chunk_t skd = chunk_empty;
466 ike_sa_id_t *id;
467
468 id = this->ike_sa->get_id(this->ike_sa);
469 if (this->old_sa)
470 {
471 /* rekeying: Include old SKd, use old PRF, apply SPI */
472 old_keymat = (keymat_v2_t*)this->old_sa->get_keymat(this->old_sa);
473 prf_alg = old_keymat->get_skd(old_keymat, &skd);
474 if (this->initiator)
475 {
476 id->set_responder_spi(id, this->proposal->get_spi(this->proposal));
477 }
478 else
479 {
480 id->set_initiator_spi(id, this->proposal->get_spi(this->proposal));
481 }
482 }
483 if (!this->keymat->derive_ike_keys(this->keymat, this->proposal, this->dh,
484 nonce_i, nonce_r, id, prf_alg, skd))
485 {
486 return FALSE;
487 }
488 charon->bus->ike_keys(charon->bus, this->ike_sa, this->dh, chunk_empty,
489 nonce_i, nonce_r, this->old_sa, NULL);
490 return TRUE;
491 }
492
493 METHOD(task_t, build_r, status_t,
494 private_ike_init_t *this, message_t *message)
495 {
496 /* check if we have everything we need */
497 if (this->proposal == NULL ||
498 this->other_nonce.len == 0 || this->my_nonce.len == 0)
499 {
500 DBG1(DBG_IKE, "received proposals inacceptable");
501 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
502 return FAILED;
503 }
504 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
505
506 if (this->dh == NULL ||
507 !this->proposal->has_dh_group(this->proposal, this->dh_group))
508 {
509 u_int16_t group;
510
511 if (this->proposal->get_algorithm(this->proposal, DIFFIE_HELLMAN_GROUP,
512 &group, NULL))
513 {
514 DBG1(DBG_IKE, "DH group %N inacceptable, requesting %N",
515 diffie_hellman_group_names, this->dh_group,
516 diffie_hellman_group_names, group);
517 this->dh_group = group;
518 group = htons(group);
519 message->add_notify(message, FALSE, INVALID_KE_PAYLOAD,
520 chunk_from_thing(group));
521 }
522 else
523 {
524 DBG1(DBG_IKE, "no acceptable proposal found");
525 }
526 return FAILED;
527 }
528
529 if (!derive_keys(this, this->other_nonce, this->my_nonce))
530 {
531 DBG1(DBG_IKE, "key derivation failed");
532 message->add_notify(message, TRUE, NO_PROPOSAL_CHOSEN, chunk_empty);
533 return FAILED;
534 }
535 build_payloads(this, message);
536 return SUCCESS;
537 }
538
539 /**
540 * Raise alerts for received notify errors
541 */
542 static void raise_alerts(private_ike_init_t *this, notify_type_t type)
543 {
544 linked_list_t *list;
545
546 switch (type)
547 {
548 case NO_PROPOSAL_CHOSEN:
549 list = this->config->get_proposals(this->config);
550 charon->bus->alert(charon->bus, ALERT_PROPOSAL_MISMATCH_IKE, list);
551 list->destroy_offset(list, offsetof(proposal_t, destroy));
552 break;
553 default:
554 break;
555 }
556 }
557
558 METHOD(task_t, process_i, status_t,
559 private_ike_init_t *this, message_t *message)
560 {
561 enumerator_t *enumerator;
562 payload_t *payload;
563
564 /* check for erroneous notifies */
565 enumerator = message->create_payload_enumerator(message);
566 while (enumerator->enumerate(enumerator, &payload))
567 {
568 if (payload->get_type(payload) == PLV2_NOTIFY)
569 {
570 notify_payload_t *notify = (notify_payload_t*)payload;
571 notify_type_t type = notify->get_notify_type(notify);
572
573 switch (type)
574 {
575 case INVALID_KE_PAYLOAD:
576 {
577 chunk_t data;
578 diffie_hellman_group_t bad_group;
579
580 bad_group = this->dh_group;
581 data = notify->get_notification_data(notify);
582 this->dh_group = ntohs(*((u_int16_t*)data.ptr));
583 DBG1(DBG_IKE, "peer didn't accept DH group %N, "
584 "it requested %N", diffie_hellman_group_names,
585 bad_group, diffie_hellman_group_names, this->dh_group);
586
587 if (this->old_sa == NULL)
588 { /* reset the IKE_SA if we are not rekeying */
589 this->ike_sa->reset(this->ike_sa);
590 }
591
592 enumerator->destroy(enumerator);
593 this->retry++;
594 return NEED_MORE;
595 }
596 case NAT_DETECTION_SOURCE_IP:
597 case NAT_DETECTION_DESTINATION_IP:
598 /* skip, handled in ike_natd_t */
599 break;
600 case MULTIPLE_AUTH_SUPPORTED:
601 /* handled in ike_auth_t */
602 break;
603 case COOKIE:
604 {
605 chunk_free(&this->cookie);
606 this->cookie = chunk_clone(notify->get_notification_data(notify));
607 this->ike_sa->reset(this->ike_sa);
608 enumerator->destroy(enumerator);
609 DBG2(DBG_IKE, "received %N notify", notify_type_names, type);
610 this->retry++;
611 return NEED_MORE;
612 }
613 default:
614 {
615 if (type <= 16383)
616 {
617 DBG1(DBG_IKE, "received %N notify error",
618 notify_type_names, type);
619 enumerator->destroy(enumerator);
620 raise_alerts(this, type);
621 return FAILED;
622 }
623 DBG2(DBG_IKE, "received %N notify",
624 notify_type_names, type);
625 break;
626 }
627 }
628 }
629 }
630 enumerator->destroy(enumerator);
631
632 process_payloads(this, message);
633
634 /* check if we have everything */
635 if (this->proposal == NULL ||
636 this->other_nonce.len == 0 || this->my_nonce.len == 0)
637 {
638 DBG1(DBG_IKE, "peers proposal selection invalid");
639 return FAILED;
640 }
641 this->ike_sa->set_proposal(this->ike_sa, this->proposal);
642
643 if (this->dh == NULL ||
644 !this->proposal->has_dh_group(this->proposal, this->dh_group))
645 {
646 DBG1(DBG_IKE, "peer DH group selection invalid");
647 return FAILED;
648 }
649
650 if (!derive_keys(this, this->my_nonce, this->other_nonce))
651 {
652 DBG1(DBG_IKE, "key derivation failed");
653 return FAILED;
654 }
655 return SUCCESS;
656 }
657
658 METHOD(task_t, get_type, task_type_t,
659 private_ike_init_t *this)
660 {
661 return TASK_IKE_INIT;
662 }
663
664 METHOD(task_t, migrate, void,
665 private_ike_init_t *this, ike_sa_t *ike_sa)
666 {
667 DESTROY_IF(this->proposal);
668 chunk_free(&this->other_nonce);
669
670 this->ike_sa = ike_sa;
671 this->keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa);
672 this->proposal = NULL;
673 if (this->dh && this->dh->get_dh_group(this->dh) != this->dh_group)
674 { /* reset DH value only if group changed (INVALID_KE_PAYLOAD) */
675 this->dh->destroy(this->dh);
676 this->dh = this->keymat->keymat.create_dh(&this->keymat->keymat,
677 this->dh_group);
678 }
679 }
680
681 METHOD(task_t, destroy, void,
682 private_ike_init_t *this)
683 {
684 DESTROY_IF(this->dh);
685 DESTROY_IF(this->proposal);
686 chunk_free(&this->my_nonce);
687 chunk_free(&this->other_nonce);
688 chunk_free(&this->cookie);
689 free(this);
690 }
691
692 METHOD(ike_init_t, get_lower_nonce, chunk_t,
693 private_ike_init_t *this)
694 {
695 if (memcmp(this->my_nonce.ptr, this->other_nonce.ptr,
696 min(this->my_nonce.len, this->other_nonce.len)) < 0)
697 {
698 return this->my_nonce;
699 }
700 else
701 {
702 return this->other_nonce;
703 }
704 }
705
706 /*
707 * Described in header.
708 */
709 ike_init_t *ike_init_create(ike_sa_t *ike_sa, bool initiator, ike_sa_t *old_sa)
710 {
711 private_ike_init_t *this;
712
713 INIT(this,
714 .public = {
715 .task = {
716 .get_type = _get_type,
717 .migrate = _migrate,
718 .destroy = _destroy,
719 },
720 .get_lower_nonce = _get_lower_nonce,
721 },
722 .ike_sa = ike_sa,
723 .initiator = initiator,
724 .dh_group = MODP_NONE,
725 .keymat = (keymat_v2_t*)ike_sa->get_keymat(ike_sa),
726 .old_sa = old_sa,
727 .signature_authentication = lib->settings->get_bool(lib->settings,
728 "%s.signature_authentication", TRUE, lib->ns),
729 );
730
731 if (initiator)
732 {
733 this->public.task.build = _build_i;
734 this->public.task.process = _process_i;
735 }
736 else
737 {
738 this->public.task.build = _build_r;
739 this->public.task.process = _process_r;
740 }
741
742 return &this->public;
743 }