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