Send EAP-Nak with supported types if requested type is unsupported
[strongswan.git] / src / libcharon / sa / ikev2 / keymat_v2.c
1 /*
2 * Copyright (C) 2008 Martin Willi
3 * Hochschule fuer Technik Rapperswil
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 "keymat_v2.h"
17
18 #include <daemon.h>
19 #include <crypto/prf_plus.h>
20
21 typedef struct private_keymat_v2_t private_keymat_v2_t;
22
23 /**
24 * Private data of an keymat_t object.
25 */
26 struct private_keymat_v2_t {
27
28 /**
29 * Public keymat_v2_t interface.
30 */
31 keymat_v2_t public;
32
33 /**
34 * IKE_SA Role, initiator or responder
35 */
36 bool initiator;
37
38 /**
39 * inbound AEAD
40 */
41 aead_t *aead_in;
42
43 /**
44 * outbound AEAD
45 */
46 aead_t *aead_out;
47
48 /**
49 * General purpose PRF
50 */
51 prf_t *prf;
52
53 /**
54 * Negotiated PRF algorithm
55 */
56 pseudo_random_function_t prf_alg;
57
58 /**
59 * Key to derive key material from for CHILD_SAs, rekeying
60 */
61 chunk_t skd;
62
63 /**
64 * Key to build outging authentication data (SKp)
65 */
66 chunk_t skp_build;
67
68 /**
69 * Key to verify incoming authentication data (SKp)
70 */
71 chunk_t skp_verify;
72 };
73
74 METHOD(keymat_t, get_version, ike_version_t,
75 private_keymat_v2_t *this)
76 {
77 return IKEV2;
78 }
79
80 METHOD(keymat_t, create_dh, diffie_hellman_t*,
81 private_keymat_v2_t *this, diffie_hellman_group_t group)
82 {
83 return lib->crypto->create_dh(lib->crypto, group);
84 }
85
86 METHOD(keymat_t, create_nonce_gen, nonce_gen_t*,
87 private_keymat_v2_t *this)
88 {
89 return lib->crypto->create_nonce_gen(lib->crypto);
90 }
91
92 /**
93 * Derive IKE keys for a combined AEAD algorithm
94 */
95 static bool derive_ike_aead(private_keymat_v2_t *this, u_int16_t alg,
96 u_int16_t key_size, prf_plus_t *prf_plus)
97 {
98 aead_t *aead_i, *aead_r;
99 chunk_t key = chunk_empty;
100
101 /* SK_ei/SK_er used for encryption */
102 aead_i = lib->crypto->create_aead(lib->crypto, alg, key_size / 8);
103 aead_r = lib->crypto->create_aead(lib->crypto, alg, key_size / 8);
104 if (aead_i == NULL || aead_r == NULL)
105 {
106 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
107 transform_type_names, ENCRYPTION_ALGORITHM,
108 encryption_algorithm_names, alg, key_size);
109 goto failure;
110 }
111 key_size = aead_i->get_key_size(aead_i);
112 if (key_size != aead_r->get_key_size(aead_r))
113 {
114 goto failure;
115 }
116 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
117 {
118 goto failure;
119 }
120 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
121 if (!aead_i->set_key(aead_i, key))
122 {
123 goto failure;
124 }
125 chunk_clear(&key);
126
127 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
128 {
129 goto failure;
130 }
131 DBG4(DBG_IKE, "Sk_er secret %B", &key);
132 if (!aead_r->set_key(aead_r, key))
133 {
134 goto failure;
135 }
136
137 if (this->initiator)
138 {
139 this->aead_in = aead_r;
140 this->aead_out = aead_i;
141 }
142 else
143 {
144 this->aead_in = aead_i;
145 this->aead_out = aead_r;
146 }
147 aead_i = aead_r = NULL;
148
149 failure:
150 DESTROY_IF(aead_i);
151 DESTROY_IF(aead_r);
152 chunk_clear(&key);
153 return this->aead_in && this->aead_out;
154 }
155
156 /**
157 * Derive IKE keys for traditional encryption and MAC algorithms
158 */
159 static bool derive_ike_traditional(private_keymat_v2_t *this, u_int16_t enc_alg,
160 u_int16_t enc_size, u_int16_t int_alg, prf_plus_t *prf_plus)
161 {
162 crypter_t *crypter_i = NULL, *crypter_r = NULL;
163 signer_t *signer_i, *signer_r;
164 size_t key_size;
165 chunk_t key = chunk_empty;
166
167 signer_i = lib->crypto->create_signer(lib->crypto, int_alg);
168 signer_r = lib->crypto->create_signer(lib->crypto, int_alg);
169 crypter_i = lib->crypto->create_crypter(lib->crypto, enc_alg, enc_size / 8);
170 crypter_r = lib->crypto->create_crypter(lib->crypto, enc_alg, enc_size / 8);
171 if (signer_i == NULL || signer_r == NULL)
172 {
173 DBG1(DBG_IKE, "%N %N not supported!",
174 transform_type_names, INTEGRITY_ALGORITHM,
175 integrity_algorithm_names, int_alg);
176 goto failure;
177 }
178 if (crypter_i == NULL || crypter_r == NULL)
179 {
180 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
181 transform_type_names, ENCRYPTION_ALGORITHM,
182 encryption_algorithm_names, enc_alg, enc_size);
183 goto failure;
184 }
185
186 /* SK_ai/SK_ar used for integrity protection */
187 key_size = signer_i->get_key_size(signer_i);
188
189 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
190 {
191 goto failure;
192 }
193 DBG4(DBG_IKE, "Sk_ai secret %B", &key);
194 if (!signer_i->set_key(signer_i, key))
195 {
196 goto failure;
197 }
198 chunk_clear(&key);
199
200 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
201 {
202 goto failure;
203 }
204 DBG4(DBG_IKE, "Sk_ar secret %B", &key);
205 if (!signer_r->set_key(signer_r, key))
206 {
207 goto failure;
208 }
209 chunk_clear(&key);
210
211 /* SK_ei/SK_er used for encryption */
212 key_size = crypter_i->get_key_size(crypter_i);
213
214 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
215 {
216 goto failure;
217 }
218 DBG4(DBG_IKE, "Sk_ei secret %B", &key);
219 if (!crypter_i->set_key(crypter_i, key))
220 {
221 goto failure;
222 }
223 chunk_clear(&key);
224
225 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
226 {
227 goto failure;
228 }
229 DBG4(DBG_IKE, "Sk_er secret %B", &key);
230 if (!crypter_r->set_key(crypter_r, key))
231 {
232 goto failure;
233 }
234
235 if (this->initiator)
236 {
237 this->aead_in = aead_create(crypter_r, signer_r);
238 this->aead_out = aead_create(crypter_i, signer_i);
239 }
240 else
241 {
242 this->aead_in = aead_create(crypter_i, signer_i);
243 this->aead_out = aead_create(crypter_r, signer_r);
244 }
245 signer_i = signer_r = NULL;
246 crypter_i = crypter_r = NULL;
247
248 failure:
249 chunk_clear(&key);
250 DESTROY_IF(signer_i);
251 DESTROY_IF(signer_r);
252 DESTROY_IF(crypter_i);
253 DESTROY_IF(crypter_r);
254 return this->aead_in && this->aead_out;
255 }
256
257 METHOD(keymat_v2_t, derive_ike_keys, bool,
258 private_keymat_v2_t *this, proposal_t *proposal, diffie_hellman_t *dh,
259 chunk_t nonce_i, chunk_t nonce_r, ike_sa_id_t *id,
260 pseudo_random_function_t rekey_function, chunk_t rekey_skd)
261 {
262 chunk_t skeyseed, key, secret, full_nonce, fixed_nonce, prf_plus_seed;
263 chunk_t spi_i, spi_r;
264 prf_plus_t *prf_plus = NULL;
265 u_int16_t alg, key_size, int_alg;
266 prf_t *rekey_prf = NULL;
267
268 spi_i = chunk_alloca(sizeof(u_int64_t));
269 spi_r = chunk_alloca(sizeof(u_int64_t));
270
271 if (dh->get_shared_secret(dh, &secret) != SUCCESS)
272 {
273 return FALSE;
274 }
275
276 /* Create SAs general purpose PRF first, we may use it here */
277 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL))
278 {
279 DBG1(DBG_IKE, "no %N selected",
280 transform_type_names, PSEUDO_RANDOM_FUNCTION);
281 return FALSE;
282 }
283 this->prf_alg = alg;
284 this->prf = lib->crypto->create_prf(lib->crypto, alg);
285 if (this->prf == NULL)
286 {
287 DBG1(DBG_IKE, "%N %N not supported!",
288 transform_type_names, PSEUDO_RANDOM_FUNCTION,
289 pseudo_random_function_names, alg);
290 return FALSE;
291 }
292 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &secret);
293 /* full nonce is used as seed for PRF+ ... */
294 full_nonce = chunk_cat("cc", nonce_i, nonce_r);
295 /* but the PRF may need a fixed key which only uses the first bytes of
296 * the nonces. */
297 switch (alg)
298 {
299 case PRF_AES128_XCBC:
300 /* while rfc4434 defines variable keys for AES-XCBC, rfc3664 does
301 * not and therefore fixed key semantics apply to XCBC for key
302 * derivation. */
303 case PRF_CAMELLIA128_XCBC:
304 /* draft-kanno-ipsecme-camellia-xcbc refers to rfc 4434, we
305 * assume fixed key length. */
306 key_size = this->prf->get_key_size(this->prf)/2;
307 nonce_i.len = min(nonce_i.len, key_size);
308 nonce_r.len = min(nonce_r.len, key_size);
309 break;
310 default:
311 /* all other algorithms use variable key length, full nonce */
312 break;
313 }
314 fixed_nonce = chunk_cat("cc", nonce_i, nonce_r);
315 *((u_int64_t*)spi_i.ptr) = id->get_initiator_spi(id);
316 *((u_int64_t*)spi_r.ptr) = id->get_responder_spi(id);
317 prf_plus_seed = chunk_cat("ccc", full_nonce, spi_i, spi_r);
318
319 /* KEYMAT = prf+ (SKEYSEED, Ni | Nr | SPIi | SPIr)
320 *
321 * if we are rekeying, SKEYSEED is built on another way
322 */
323 if (rekey_function == PRF_UNDEFINED) /* not rekeying */
324 {
325 /* SKEYSEED = prf(Ni | Nr, g^ir) */
326 if (this->prf->set_key(this->prf, fixed_nonce) &&
327 this->prf->allocate_bytes(this->prf, secret, &skeyseed) &&
328 this->prf->set_key(this->prf, skeyseed))
329 {
330 prf_plus = prf_plus_create(this->prf, TRUE, prf_plus_seed);
331 }
332 }
333 else
334 {
335 /* SKEYSEED = prf(SK_d (old), [g^ir (new)] | Ni | Nr)
336 * use OLD SAs PRF functions for both prf_plus and prf */
337 rekey_prf = lib->crypto->create_prf(lib->crypto, rekey_function);
338 if (!rekey_prf)
339 {
340 DBG1(DBG_IKE, "PRF of old SA %N not supported!",
341 pseudo_random_function_names, rekey_function);
342 chunk_free(&full_nonce);
343 chunk_free(&fixed_nonce);
344 chunk_clear(&prf_plus_seed);
345 return FALSE;
346 }
347 secret = chunk_cat("mc", secret, full_nonce);
348 if (rekey_prf->set_key(rekey_prf, rekey_skd) &&
349 rekey_prf->allocate_bytes(rekey_prf, secret, &skeyseed) &&
350 rekey_prf->set_key(rekey_prf, skeyseed))
351 {
352 prf_plus = prf_plus_create(rekey_prf, TRUE, prf_plus_seed);
353 }
354 }
355 DBG4(DBG_IKE, "SKEYSEED %B", &skeyseed);
356
357 chunk_clear(&skeyseed);
358 chunk_clear(&secret);
359 chunk_free(&full_nonce);
360 chunk_free(&fixed_nonce);
361 chunk_clear(&prf_plus_seed);
362
363 if (!prf_plus)
364 {
365 goto failure;
366 }
367
368 /* KEYMAT = SK_d | SK_ai | SK_ar | SK_ei | SK_er | SK_pi | SK_pr */
369
370 /* SK_d is used for generating CHILD_SA key mat => store for later use */
371 key_size = this->prf->get_key_size(this->prf);
372 if (!prf_plus->allocate_bytes(prf_plus, key_size, &this->skd))
373 {
374 goto failure;
375 }
376 DBG4(DBG_IKE, "Sk_d secret %B", &this->skd);
377
378 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg, &key_size))
379 {
380 DBG1(DBG_IKE, "no %N selected",
381 transform_type_names, ENCRYPTION_ALGORITHM);
382 goto failure;
383 }
384
385 if (encryption_algorithm_is_aead(alg))
386 {
387 if (!derive_ike_aead(this, alg, key_size, prf_plus))
388 {
389 goto failure;
390 }
391 }
392 else
393 {
394 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
395 &int_alg, NULL))
396 {
397 DBG1(DBG_IKE, "no %N selected",
398 transform_type_names, INTEGRITY_ALGORITHM);
399 goto failure;
400 }
401 if (!derive_ike_traditional(this, alg, key_size, int_alg, prf_plus))
402 {
403 goto failure;
404 }
405 }
406
407 /* SK_pi/SK_pr used for authentication => stored for later */
408 key_size = this->prf->get_key_size(this->prf);
409 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
410 {
411 goto failure;
412 }
413 DBG4(DBG_IKE, "Sk_pi secret %B", &key);
414 if (this->initiator)
415 {
416 this->skp_build = key;
417 }
418 else
419 {
420 this->skp_verify = key;
421 }
422 if (!prf_plus->allocate_bytes(prf_plus, key_size, &key))
423 {
424 goto failure;
425 }
426 DBG4(DBG_IKE, "Sk_pr secret %B", &key);
427 if (this->initiator)
428 {
429 this->skp_verify = key;
430 }
431 else
432 {
433 this->skp_build = key;
434 }
435
436 /* all done, prf_plus not needed anymore */
437 failure:
438 DESTROY_IF(prf_plus);
439 DESTROY_IF(rekey_prf);
440
441 return this->skp_build.len && this->skp_verify.len;
442 }
443
444 METHOD(keymat_v2_t, derive_child_keys, bool,
445 private_keymat_v2_t *this, proposal_t *proposal, diffie_hellman_t *dh,
446 chunk_t nonce_i, chunk_t nonce_r, chunk_t *encr_i, chunk_t *integ_i,
447 chunk_t *encr_r, chunk_t *integ_r)
448 {
449 u_int16_t enc_alg, int_alg, enc_size = 0, int_size = 0;
450 chunk_t seed, secret = chunk_empty;
451 prf_plus_t *prf_plus;
452
453 if (dh)
454 {
455 if (dh->get_shared_secret(dh, &secret) != SUCCESS)
456 {
457 return FALSE;
458 }
459 DBG4(DBG_CHD, "DH secret %B", &secret);
460 }
461 seed = chunk_cata("mcc", secret, nonce_i, nonce_r);
462 DBG4(DBG_CHD, "seed %B", &seed);
463
464 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
465 &enc_alg, &enc_size))
466 {
467 DBG2(DBG_CHD, " using %N for encryption",
468 encryption_algorithm_names, enc_alg);
469
470 if (!enc_size)
471 {
472 enc_size = keymat_get_keylen_encr(enc_alg);
473 }
474 if (enc_alg != ENCR_NULL && !enc_size)
475 {
476 DBG1(DBG_CHD, "no keylength defined for %N",
477 encryption_algorithm_names, enc_alg);
478 return FALSE;
479 }
480 /* to bytes */
481 enc_size /= 8;
482
483 /* CCM/GCM/CTR/GMAC needs additional bytes */
484 switch (enc_alg)
485 {
486 case ENCR_AES_CCM_ICV8:
487 case ENCR_AES_CCM_ICV12:
488 case ENCR_AES_CCM_ICV16:
489 case ENCR_CAMELLIA_CCM_ICV8:
490 case ENCR_CAMELLIA_CCM_ICV12:
491 case ENCR_CAMELLIA_CCM_ICV16:
492 enc_size += 3;
493 break;
494 case ENCR_AES_GCM_ICV8:
495 case ENCR_AES_GCM_ICV12:
496 case ENCR_AES_GCM_ICV16:
497 case ENCR_AES_CTR:
498 case ENCR_NULL_AUTH_AES_GMAC:
499 enc_size += 4;
500 break;
501 default:
502 break;
503 }
504 }
505
506 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
507 &int_alg, &int_size))
508 {
509 DBG2(DBG_CHD, " using %N for integrity",
510 integrity_algorithm_names, int_alg);
511
512 if (!int_size)
513 {
514 int_size = keymat_get_keylen_integ(int_alg);
515 }
516 if (!int_size)
517 {
518 DBG1(DBG_CHD, "no keylength defined for %N",
519 integrity_algorithm_names, int_alg);
520 return FALSE;
521 }
522 /* to bytes */
523 int_size /= 8;
524 }
525
526 if (!this->prf->set_key(this->prf, this->skd))
527 {
528 return FALSE;
529 }
530 prf_plus = prf_plus_create(this->prf, TRUE, seed);
531 if (!prf_plus)
532 {
533 return FALSE;
534 }
535
536 *encr_i = *integ_i = *encr_r = *integ_r = chunk_empty;
537 if (!prf_plus->allocate_bytes(prf_plus, enc_size, encr_i) ||
538 !prf_plus->allocate_bytes(prf_plus, int_size, integ_i) ||
539 !prf_plus->allocate_bytes(prf_plus, enc_size, encr_r) ||
540 !prf_plus->allocate_bytes(prf_plus, int_size, integ_r))
541 {
542 chunk_free(encr_i);
543 chunk_free(integ_i);
544 chunk_free(encr_r);
545 chunk_free(integ_r);
546 prf_plus->destroy(prf_plus);
547 return FALSE;
548 }
549
550 prf_plus->destroy(prf_plus);
551
552 if (enc_size)
553 {
554 DBG4(DBG_CHD, "encryption initiator key %B", encr_i);
555 DBG4(DBG_CHD, "encryption responder key %B", encr_r);
556 }
557 if (int_size)
558 {
559 DBG4(DBG_CHD, "integrity initiator key %B", integ_i);
560 DBG4(DBG_CHD, "integrity responder key %B", integ_r);
561 }
562 return TRUE;
563 }
564
565 METHOD(keymat_v2_t, get_skd, pseudo_random_function_t,
566 private_keymat_v2_t *this, chunk_t *skd)
567 {
568 *skd = this->skd;
569 return this->prf_alg;
570 }
571
572 METHOD(keymat_t, get_aead, aead_t*,
573 private_keymat_v2_t *this, bool in)
574 {
575 return in ? this->aead_in : this->aead_out;
576 }
577
578 METHOD(keymat_v2_t, get_auth_octets, bool,
579 private_keymat_v2_t *this, bool verify, chunk_t ike_sa_init,
580 chunk_t nonce, identification_t *id, char reserved[3], chunk_t *octets)
581 {
582 chunk_t chunk, idx;
583 chunk_t skp;
584
585 skp = verify ? this->skp_verify : this->skp_build;
586
587 chunk = chunk_alloca(4);
588 chunk.ptr[0] = id->get_type(id);
589 memcpy(chunk.ptr + 1, reserved, 3);
590 idx = chunk_cata("cc", chunk, id->get_encoding(id));
591
592 DBG3(DBG_IKE, "IDx' %B", &idx);
593 DBG3(DBG_IKE, "SK_p %B", &skp);
594 if (!this->prf->set_key(this->prf, skp) ||
595 !this->prf->allocate_bytes(this->prf, idx, &chunk))
596 {
597 return FALSE;
598 }
599 *octets = chunk_cat("ccm", ike_sa_init, nonce, chunk);
600 DBG3(DBG_IKE, "octets = message + nonce + prf(Sk_px, IDx') %B", octets);
601 return TRUE;
602 }
603
604 /**
605 * Key pad for the AUTH method SHARED_KEY_MESSAGE_INTEGRITY_CODE.
606 */
607 #define IKEV2_KEY_PAD "Key Pad for IKEv2"
608 #define IKEV2_KEY_PAD_LENGTH 17
609
610 METHOD(keymat_v2_t, get_psk_sig, bool,
611 private_keymat_v2_t *this, bool verify, chunk_t ike_sa_init, chunk_t nonce,
612 chunk_t secret, identification_t *id, char reserved[3], chunk_t *sig)
613 {
614 chunk_t key_pad, key, octets;
615
616 if (!secret.len)
617 { /* EAP uses SK_p if no MSK has been established */
618 secret = verify ? this->skp_verify : this->skp_build;
619 }
620 if (!get_auth_octets(this, verify, ike_sa_init, nonce, id, reserved, &octets))
621 {
622 return FALSE;
623 }
624 /* AUTH = prf(prf(Shared Secret,"Key Pad for IKEv2"), <msg octets>) */
625 key_pad = chunk_create(IKEV2_KEY_PAD, IKEV2_KEY_PAD_LENGTH);
626 if (!this->prf->set_key(this->prf, secret) ||
627 !this->prf->allocate_bytes(this->prf, key_pad, &key))
628 {
629 chunk_free(&octets);
630 return FALSE;
631 }
632 if (!this->prf->set_key(this->prf, key) ||
633 !this->prf->allocate_bytes(this->prf, octets, sig))
634 {
635 chunk_free(&key);
636 chunk_free(&octets);
637 return FALSE;
638 }
639 DBG4(DBG_IKE, "secret %B", &secret);
640 DBG4(DBG_IKE, "prf(secret, keypad) %B", &key);
641 DBG3(DBG_IKE, "AUTH = prf(prf(secret, keypad), octets) %B", sig);
642 chunk_free(&octets);
643 chunk_free(&key);
644
645 return TRUE;
646 }
647
648 METHOD(keymat_t, destroy, void,
649 private_keymat_v2_t *this)
650 {
651 DESTROY_IF(this->aead_in);
652 DESTROY_IF(this->aead_out);
653 DESTROY_IF(this->prf);
654 chunk_clear(&this->skd);
655 chunk_clear(&this->skp_verify);
656 chunk_clear(&this->skp_build);
657 free(this);
658 }
659
660 /**
661 * See header
662 */
663 keymat_v2_t *keymat_v2_create(bool initiator)
664 {
665 private_keymat_v2_t *this;
666
667 INIT(this,
668 .public = {
669 .keymat = {
670 .get_version = _get_version,
671 .create_dh = _create_dh,
672 .create_nonce_gen = _create_nonce_gen,
673 .get_aead = _get_aead,
674 .destroy = _destroy,
675 },
676 .derive_ike_keys = _derive_ike_keys,
677 .derive_child_keys = _derive_child_keys,
678 .get_skd = _get_skd,
679 .get_auth_octets = _get_auth_octets,
680 .get_psk_sig = _get_psk_sig,
681 },
682 .initiator = initiator,
683 .prf_alg = PRF_UNDEFINED,
684 );
685
686 return &this->public;
687 }