e2db13a135c0bc40332be81bbea82022ff0abf76
[strongswan.git] / src / libcharon / sa / ikev1 / keymat_v1.c
1 /*
2 * Copyright (C) 2011 Tobias Brunner
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_v1.h"
17
18 #include <daemon.h>
19 #include <encoding/generator.h>
20 #include <encoding/payloads/nonce_payload.h>
21 #include <utils/linked_list.h>
22
23 typedef struct private_keymat_v1_t private_keymat_v1_t;
24
25 /**
26 * Max. number of IVs to track.
27 */
28 #define MAX_IV 3
29
30 /**
31 * Max. number of Quick Modes to track.
32 */
33 #define MAX_QM 2
34
35 /**
36 * Data stored for IVs
37 */
38 typedef struct {
39 /** message ID */
40 u_int32_t mid;
41 /** current IV */
42 chunk_t iv;
43 /** last block of encrypted message */
44 chunk_t last_block;
45 } iv_data_t;
46
47 /**
48 * Private data of an keymat_t object.
49 */
50 struct private_keymat_v1_t {
51
52 /**
53 * Public keymat_v1_t interface.
54 */
55 keymat_v1_t public;
56
57 /**
58 * IKE_SA Role, initiator or responder
59 */
60 bool initiator;
61
62 /**
63 * General purpose PRF
64 */
65 prf_t *prf;
66
67 /**
68 * PRF to create Phase 1 HASH payloads
69 */
70 prf_t *prf_auth;
71
72 /**
73 * Crypter wrapped in an aead_t interface
74 */
75 aead_t *aead;
76
77 /**
78 * Hasher used for IV generation (and other things like e.g. NAT-T)
79 */
80 hasher_t *hasher;
81
82 /**
83 * Key used for authentication during main mode
84 */
85 chunk_t skeyid;
86
87 /**
88 * Key to derive key material from for non-ISAKMP SAs, rekeying
89 */
90 chunk_t skeyid_d;
91
92 /**
93 * Key used for authentication after main mode
94 */
95 chunk_t skeyid_a;
96
97 /**
98 * Phase 1 IV
99 */
100 iv_data_t phase1_iv;
101
102 /**
103 * Keep track of IVs for exchanges after phase 1. We store only a limited
104 * number of IVs in an MRU sort of way. Stores iv_data_t objects.
105 */
106 linked_list_t *ivs;
107
108 /**
109 * Keep track of Nonces during Quick Mode exchanges. Only a limited number
110 * of QMs are tracked at the same time. Stores qm_data_t objects.
111 */
112 linked_list_t *qms;
113 };
114
115
116 /**
117 * Destroy an iv_data_t object.
118 */
119 static void iv_data_destroy(iv_data_t *this)
120 {
121 chunk_free(&this->last_block);
122 chunk_free(&this->iv);
123 free(this);
124 }
125
126 /**
127 * Data stored for Quick Mode exchanges
128 */
129 typedef struct {
130 /** message ID */
131 u_int32_t mid;
132 /** Ni_b (Nonce from first message) */
133 chunk_t n_i;
134 /** Nr_b (Nonce from second message) */
135 chunk_t n_r;
136 } qm_data_t;
137
138 /**
139 * Destroy a qm_data_t object.
140 */
141 static void qm_data_destroy(qm_data_t *this)
142 {
143 chunk_free(&this->n_i);
144 chunk_free(&this->n_r);
145 free(this);
146 }
147
148 /**
149 * Constants used in key derivation.
150 */
151 static const chunk_t octet_0 = chunk_from_chars(0x00);
152 static const chunk_t octet_1 = chunk_from_chars(0x01);
153 static const chunk_t octet_2 = chunk_from_chars(0x02);
154
155 /**
156 * Simple aead_t implementation without support for authentication.
157 */
158 typedef struct {
159 /** implements aead_t interface */
160 aead_t aead;
161 /** crypter to be used */
162 crypter_t *crypter;
163 } private_aead_t;
164
165
166 METHOD(aead_t, encrypt, bool,
167 private_aead_t *this, chunk_t plain, chunk_t assoc, chunk_t iv,
168 chunk_t *encrypted)
169 {
170 return this->crypter->encrypt(this->crypter, plain, iv, encrypted);
171 }
172
173 METHOD(aead_t, decrypt, bool,
174 private_aead_t *this, chunk_t encrypted, chunk_t assoc, chunk_t iv,
175 chunk_t *plain)
176 {
177 return this->crypter->decrypt(this->crypter, encrypted, iv, plain);
178 }
179
180 METHOD(aead_t, get_block_size, size_t,
181 private_aead_t *this)
182 {
183 return this->crypter->get_block_size(this->crypter);
184 }
185
186 METHOD(aead_t, get_icv_size, size_t,
187 private_aead_t *this)
188 {
189 return 0;
190 }
191
192 METHOD(aead_t, get_iv_size, size_t,
193 private_aead_t *this)
194 {
195 /* in order to create the messages properly we return 0 here */
196 return 0;
197 }
198
199 METHOD(aead_t, get_key_size, size_t,
200 private_aead_t *this)
201 {
202 return this->crypter->get_key_size(this->crypter);
203 }
204
205 METHOD(aead_t, set_key, bool,
206 private_aead_t *this, chunk_t key)
207 {
208 return this->crypter->set_key(this->crypter, key);
209 }
210
211 METHOD(aead_t, aead_destroy, void,
212 private_aead_t *this)
213 {
214 this->crypter->destroy(this->crypter);
215 free(this);
216 }
217
218 /**
219 * Expand SKEYID_e according to Appendix B in RFC 2409.
220 * TODO-IKEv1: verify keys (e.g. for weak keys, see Appendix B)
221 */
222 static bool expand_skeyid_e(chunk_t skeyid_e, size_t key_size, prf_t *prf,
223 chunk_t *ka)
224 {
225 size_t block_size;
226 chunk_t seed;
227 int i;
228
229 if (skeyid_e.len >= key_size)
230 { /* no expansion required, reduce to key_size */
231 skeyid_e.len = key_size;
232 *ka = skeyid_e;
233 return TRUE;
234 }
235 block_size = prf->get_block_size(prf);
236 *ka = chunk_alloc((key_size / block_size + 1) * block_size);
237 ka->len = key_size;
238
239 /* Ka = K1 | K2 | ..., K1 = prf(SKEYID_e, 0), K2 = prf(SKEYID_e, K1) ... */
240 if (!prf->set_key(prf, skeyid_e))
241 {
242 chunk_clear(ka);
243 chunk_clear(&skeyid_e);
244 return FALSE;
245 }
246 seed = octet_0;
247 for (i = 0; i < key_size; i += block_size)
248 {
249 if (!prf->get_bytes(prf, seed, ka->ptr + i))
250 {
251 chunk_clear(ka);
252 chunk_clear(&skeyid_e);
253 return FALSE;
254 }
255 seed = chunk_create(ka->ptr + i, block_size);
256 }
257 chunk_clear(&skeyid_e);
258 return TRUE;
259 }
260
261 /**
262 * Create a simple implementation of the aead_t interface which only encrypts
263 * or decrypts data.
264 */
265 static aead_t *create_aead(proposal_t *proposal, prf_t *prf, chunk_t skeyid_e)
266 {
267 private_aead_t *this;
268 u_int16_t alg, key_size;
269 crypter_t *crypter;
270 chunk_t ka;
271
272 if (!proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM, &alg,
273 &key_size))
274 {
275 DBG1(DBG_IKE, "no %N selected",
276 transform_type_names, ENCRYPTION_ALGORITHM);
277 return NULL;
278 }
279 crypter = lib->crypto->create_crypter(lib->crypto, alg, key_size / 8);
280 if (!crypter)
281 {
282 DBG1(DBG_IKE, "%N %N (key size %d) not supported!",
283 transform_type_names, ENCRYPTION_ALGORITHM,
284 encryption_algorithm_names, alg, key_size);
285 return NULL;
286 }
287 key_size = crypter->get_key_size(crypter);
288 if (!expand_skeyid_e(skeyid_e, crypter->get_key_size(crypter), prf, &ka))
289 {
290 return NULL;
291 }
292 DBG4(DBG_IKE, "encryption key Ka %B", &ka);
293 if (!crypter->set_key(crypter, ka))
294 {
295 chunk_clear(&ka);
296 return NULL;
297 }
298 chunk_clear(&ka);
299
300 INIT(this,
301 .aead = {
302 .encrypt = _encrypt,
303 .decrypt = _decrypt,
304 .get_block_size = _get_block_size,
305 .get_icv_size = _get_icv_size,
306 .get_iv_size = _get_iv_size,
307 .get_key_size = _get_key_size,
308 .set_key = _set_key,
309 .destroy = _aead_destroy,
310 },
311 .crypter = crypter,
312 );
313 return &this->aead;
314 }
315
316 /**
317 * Converts integrity algorithm to PRF algorithm
318 */
319 static u_int16_t auth_to_prf(u_int16_t alg)
320 {
321 switch (alg)
322 {
323 case AUTH_HMAC_SHA1_96:
324 return PRF_HMAC_SHA1;
325 case AUTH_HMAC_SHA2_256_128:
326 return PRF_HMAC_SHA2_256;
327 case AUTH_HMAC_SHA2_384_192:
328 return PRF_HMAC_SHA2_384;
329 case AUTH_HMAC_SHA2_512_256:
330 return PRF_HMAC_SHA2_512;
331 case AUTH_HMAC_MD5_96:
332 return PRF_HMAC_MD5;
333 case AUTH_AES_XCBC_96:
334 return PRF_AES128_XCBC;
335 default:
336 return PRF_UNDEFINED;
337 }
338 }
339
340 /**
341 * Converts integrity algorithm to hash algorithm
342 */
343 static u_int16_t auth_to_hash(u_int16_t alg)
344 {
345 switch (alg)
346 {
347 case AUTH_HMAC_SHA1_96:
348 return HASH_SHA1;
349 case AUTH_HMAC_SHA2_256_128:
350 return HASH_SHA256;
351 case AUTH_HMAC_SHA2_384_192:
352 return HASH_SHA384;
353 case AUTH_HMAC_SHA2_512_256:
354 return HASH_SHA512;
355 case AUTH_HMAC_MD5_96:
356 return HASH_MD5;
357 default:
358 return HASH_UNKNOWN;
359 }
360 }
361
362 /**
363 * Adjust the key length for PRF algorithms that expect a fixed key length.
364 */
365 static void adjust_keylen(u_int16_t alg, chunk_t *key)
366 {
367 switch (alg)
368 {
369 case PRF_AES128_XCBC:
370 /* while rfc4434 defines variable keys for AES-XCBC, rfc3664 does
371 * not and therefore fixed key semantics apply to XCBC for key
372 * derivation. */
373 key->len = min(key->len, 16);
374 break;
375 default:
376 /* all other algorithms use variable key length */
377 break;
378 }
379 }
380
381 METHOD(keymat_v1_t, derive_ike_keys, bool,
382 private_keymat_v1_t *this, proposal_t *proposal, diffie_hellman_t *dh,
383 chunk_t dh_other, chunk_t nonce_i, chunk_t nonce_r, ike_sa_id_t *id,
384 auth_method_t auth, shared_key_t *shared_key)
385 {
386 chunk_t g_xy, g_xi, g_xr, dh_me, spi_i, spi_r, nonces, data, skeyid_e;
387 chunk_t skeyid;
388 u_int16_t alg;
389
390 spi_i = chunk_alloca(sizeof(u_int64_t));
391 spi_r = chunk_alloca(sizeof(u_int64_t));
392
393 if (!proposal->get_algorithm(proposal, PSEUDO_RANDOM_FUNCTION, &alg, NULL))
394 { /* no PRF negotiated, use HMAC version of integrity algorithm instead */
395 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, NULL)
396 || (alg = auth_to_prf(alg)) == PRF_UNDEFINED)
397 {
398 DBG1(DBG_IKE, "no %N selected",
399 transform_type_names, PSEUDO_RANDOM_FUNCTION);
400 return FALSE;
401 }
402 }
403 this->prf = lib->crypto->create_prf(lib->crypto, alg);
404 if (!this->prf)
405 {
406 DBG1(DBG_IKE, "%N %N not supported!",
407 transform_type_names, PSEUDO_RANDOM_FUNCTION,
408 pseudo_random_function_names, alg);
409 return FALSE;
410 }
411 if (this->prf->get_block_size(this->prf) <
412 this->prf->get_key_size(this->prf))
413 { /* TODO-IKEv1: support PRF output expansion (RFC 2409, Appendix B) */
414 DBG1(DBG_IKE, "expansion of %N %N output not supported!",
415 transform_type_names, PSEUDO_RANDOM_FUNCTION,
416 pseudo_random_function_names, alg);
417 return FALSE;
418 }
419
420 if (dh->get_shared_secret(dh, &g_xy) != SUCCESS)
421 {
422 return FALSE;
423 }
424 DBG4(DBG_IKE, "shared Diffie Hellman secret %B", &g_xy);
425
426 *((u_int64_t*)spi_i.ptr) = id->get_initiator_spi(id);
427 *((u_int64_t*)spi_r.ptr) = id->get_responder_spi(id);
428 nonces = chunk_cata("cc", nonce_i, nonce_r);
429
430 switch (auth)
431 {
432 case AUTH_PSK:
433 case AUTH_XAUTH_INIT_PSK:
434 { /* SKEYID = prf(pre-shared-key, Ni_b | Nr_b) */
435 chunk_t psk;
436 if (!shared_key)
437 {
438 chunk_clear(&g_xy);
439 return FALSE;
440 }
441 psk = shared_key->get_key(shared_key);
442 adjust_keylen(alg, &psk);
443 if (!this->prf->set_key(this->prf, psk) ||
444 !this->prf->allocate_bytes(this->prf, nonces, &skeyid))
445 {
446 chunk_clear(&g_xy);
447 return FALSE;
448 }
449 break;
450 }
451 case AUTH_RSA:
452 case AUTH_ECDSA_256:
453 case AUTH_ECDSA_384:
454 case AUTH_ECDSA_521:
455 case AUTH_XAUTH_INIT_RSA:
456 case AUTH_XAUTH_RESP_RSA:
457 case AUTH_HYBRID_INIT_RSA:
458 case AUTH_HYBRID_RESP_RSA:
459 {
460 if (!this->prf->set_key(this->prf, nonces) ||
461 !this->prf->allocate_bytes(this->prf, g_xy, &skeyid))
462 {
463 chunk_clear(&g_xy);
464 return FALSE;
465 }
466 break;
467 }
468 default:
469 /* TODO-IKEv1: implement key derivation for other schemes */
470 /* authentication class not supported */
471 chunk_clear(&g_xy);
472 return FALSE;
473 }
474 adjust_keylen(alg, &skeyid);
475 DBG4(DBG_IKE, "SKEYID %B", &skeyid);
476
477 /* SKEYID_d = prf(SKEYID, g^xy | CKY-I | CKY-R | 0) */
478 data = chunk_cat("cccc", g_xy, spi_i, spi_r, octet_0);
479 if (!this->prf->set_key(this->prf, skeyid) ||
480 !this->prf->allocate_bytes(this->prf, data, &this->skeyid_d))
481 {
482 chunk_clear(&g_xy);
483 chunk_clear(&data);
484 }
485 chunk_clear(&data);
486 DBG4(DBG_IKE, "SKEYID_d %B", &this->skeyid_d);
487
488 /* SKEYID_a = prf(SKEYID, SKEYID_d | g^xy | CKY-I | CKY-R | 1) */
489 data = chunk_cat("ccccc", this->skeyid_d, g_xy, spi_i, spi_r, octet_1);
490 if (!this->prf->allocate_bytes(this->prf, data, &this->skeyid_a))
491 {
492 chunk_clear(&g_xy);
493 chunk_clear(&data);
494 }
495 chunk_clear(&data);
496 DBG4(DBG_IKE, "SKEYID_a %B", &this->skeyid_a);
497
498 /* SKEYID_e = prf(SKEYID, SKEYID_a | g^xy | CKY-I | CKY-R | 2) */
499 data = chunk_cat("ccccc", this->skeyid_a, g_xy, spi_i, spi_r, octet_2);
500 if (!this->prf->allocate_bytes(this->prf, data, &skeyid_e))
501 {
502 chunk_clear(&g_xy);
503 chunk_clear(&data);
504 }
505 chunk_clear(&data);
506 DBG4(DBG_IKE, "SKEYID_e %B", &skeyid_e);
507
508 chunk_clear(&g_xy);
509
510 switch (auth)
511 {
512 case AUTH_ECDSA_256:
513 alg = PRF_HMAC_SHA2_256;
514 break;
515 case AUTH_ECDSA_384:
516 alg = PRF_HMAC_SHA2_384;
517 break;
518 case AUTH_ECDSA_521:
519 alg = PRF_HMAC_SHA2_512;
520 break;
521 default:
522 /* use proposal algorithm */
523 break;
524 }
525 this->prf_auth = lib->crypto->create_prf(lib->crypto, alg);
526 if (!this->prf_auth)
527 {
528 DBG1(DBG_IKE, "%N %N not supported!",
529 transform_type_names, PSEUDO_RANDOM_FUNCTION,
530 pseudo_random_function_names, alg);
531 chunk_clear(&skeyid);
532 return FALSE;
533 }
534 if (!this->prf_auth->set_key(this->prf_auth, skeyid))
535 {
536 chunk_clear(&skeyid);
537 return FALSE;
538 }
539 chunk_clear(&skeyid);
540
541 this->aead = create_aead(proposal, this->prf, skeyid_e);
542 if (!this->aead)
543 {
544 return FALSE;
545 }
546 if (!this->hasher && !this->public.create_hasher(&this->public, proposal))
547 {
548 return FALSE;
549 }
550
551 dh->get_my_public_value(dh, &dh_me);
552 g_xi = this->initiator ? dh_me : dh_other;
553 g_xr = this->initiator ? dh_other : dh_me;
554
555 /* initial IV = hash(g^xi | g^xr) */
556 data = chunk_cata("cc", g_xi, g_xr);
557 if (!this->hasher->allocate_hash(this->hasher, data, &this->phase1_iv.iv))
558 {
559 chunk_free(&dh_me);
560 return FALSE;
561 }
562 if (this->phase1_iv.iv.len > this->aead->get_block_size(this->aead))
563 {
564 this->phase1_iv.iv.len = this->aead->get_block_size(this->aead);
565 }
566 chunk_free(&dh_me);
567 DBG4(DBG_IKE, "initial IV %B", &this->phase1_iv.iv);
568
569 return TRUE;
570 }
571
572 METHOD(keymat_v1_t, derive_child_keys, bool,
573 private_keymat_v1_t *this, proposal_t *proposal, diffie_hellman_t *dh,
574 u_int32_t spi_i, u_int32_t spi_r, chunk_t nonce_i, chunk_t nonce_r,
575 chunk_t *encr_i, chunk_t *integ_i, chunk_t *encr_r, chunk_t *integ_r)
576 {
577 u_int16_t enc_alg, int_alg, enc_size = 0, int_size = 0;
578 u_int8_t protocol;
579 prf_plus_t *prf_plus;
580 chunk_t seed, secret = chunk_empty;
581
582 if (proposal->get_algorithm(proposal, ENCRYPTION_ALGORITHM,
583 &enc_alg, &enc_size))
584 {
585 DBG2(DBG_CHD, " using %N for encryption",
586 encryption_algorithm_names, enc_alg);
587
588 if (!enc_size)
589 {
590 enc_size = keymat_get_keylen_encr(enc_alg);
591 }
592 if (enc_alg != ENCR_NULL && !enc_size)
593 {
594 DBG1(DBG_CHD, "no keylength defined for %N",
595 encryption_algorithm_names, enc_alg);
596 return FALSE;
597 }
598 /* to bytes */
599 enc_size /= 8;
600
601 /* CCM/GCM/CTR/GMAC needs additional bytes */
602 switch (enc_alg)
603 {
604 case ENCR_AES_CCM_ICV8:
605 case ENCR_AES_CCM_ICV12:
606 case ENCR_AES_CCM_ICV16:
607 case ENCR_CAMELLIA_CCM_ICV8:
608 case ENCR_CAMELLIA_CCM_ICV12:
609 case ENCR_CAMELLIA_CCM_ICV16:
610 enc_size += 3;
611 break;
612 case ENCR_AES_GCM_ICV8:
613 case ENCR_AES_GCM_ICV12:
614 case ENCR_AES_GCM_ICV16:
615 case ENCR_AES_CTR:
616 case ENCR_NULL_AUTH_AES_GMAC:
617 enc_size += 4;
618 break;
619 default:
620 break;
621 }
622 }
623
624 if (proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM,
625 &int_alg, &int_size))
626 {
627 DBG2(DBG_CHD, " using %N for integrity",
628 integrity_algorithm_names, int_alg);
629
630 if (!int_size)
631 {
632 int_size = keymat_get_keylen_integ(int_alg);
633 }
634 if (!int_size)
635 {
636 DBG1(DBG_CHD, "no keylength defined for %N",
637 integrity_algorithm_names, int_alg);
638 return FALSE;
639 }
640 /* to bytes */
641 int_size /= 8;
642 }
643
644 /* KEYMAT = prf+(SKEYID_d, [ g(qm)^xy | ] protocol | SPI | Ni_b | Nr_b) */
645 if (!this->prf->set_key(this->prf, this->skeyid_d))
646 {
647 return FALSE;
648 }
649 protocol = proposal->get_protocol(proposal);
650 if (dh)
651 {
652 if (dh->get_shared_secret(dh, &secret) != SUCCESS)
653 {
654 return FALSE;
655 }
656 DBG4(DBG_CHD, "DH secret %B", &secret);
657 }
658
659 seed = chunk_cata("ccccc", secret, chunk_from_thing(protocol),
660 chunk_from_thing(spi_r), nonce_i, nonce_r);
661 DBG4(DBG_CHD, "initiator SA seed %B", &seed);
662
663 prf_plus = prf_plus_create(this->prf, FALSE, seed);
664 if (!prf_plus)
665 {
666 chunk_clear(&secret);
667 return FALSE;
668 }
669 if (!prf_plus->allocate_bytes(prf_plus, enc_size, encr_i) ||
670 !prf_plus->allocate_bytes(prf_plus, int_size, integ_i))
671 {
672 prf_plus->destroy(prf_plus);
673 chunk_clear(&secret);
674 return FALSE;
675 }
676 prf_plus->destroy(prf_plus);
677
678 seed = chunk_cata("ccccc", secret, chunk_from_thing(protocol),
679 chunk_from_thing(spi_i), nonce_i, nonce_r);
680 DBG4(DBG_CHD, "responder SA seed %B", &seed);
681 prf_plus = prf_plus_create(this->prf, FALSE, seed);
682 if (!prf_plus)
683 {
684 chunk_clear(&secret);
685 return FALSE;
686 }
687 if (!prf_plus->allocate_bytes(prf_plus, enc_size, encr_r) ||
688 !prf_plus->allocate_bytes(prf_plus, int_size, integ_r))
689 {
690 prf_plus->destroy(prf_plus);
691 chunk_clear(&secret);
692 return FALSE;
693 }
694 prf_plus->destroy(prf_plus);
695
696 chunk_clear(&secret);
697
698 if (enc_size)
699 {
700 DBG4(DBG_CHD, "encryption initiator key %B", encr_i);
701 DBG4(DBG_CHD, "encryption responder key %B", encr_r);
702 }
703 if (int_size)
704 {
705 DBG4(DBG_CHD, "integrity initiator key %B", integ_i);
706 DBG4(DBG_CHD, "integrity responder key %B", integ_r);
707 }
708 return TRUE;
709 }
710
711 METHOD(keymat_v1_t, create_hasher, bool,
712 private_keymat_v1_t *this, proposal_t *proposal)
713 {
714 u_int16_t alg;
715 if (!proposal->get_algorithm(proposal, INTEGRITY_ALGORITHM, &alg, NULL) ||
716 (alg = auth_to_hash(alg)) == HASH_UNKNOWN)
717 {
718 DBG1(DBG_IKE, "no %N selected", transform_type_names, HASH_ALGORITHM);
719 return FALSE;
720 }
721 this->hasher = lib->crypto->create_hasher(lib->crypto, alg);
722 if (!this->hasher)
723 {
724 DBG1(DBG_IKE, "%N %N not supported!",
725 transform_type_names, HASH_ALGORITHM,
726 hash_algorithm_names, alg);
727 return FALSE;
728 }
729 return TRUE;
730 }
731
732 METHOD(keymat_v1_t, get_hasher, hasher_t*,
733 private_keymat_v1_t *this)
734 {
735 return this->hasher;
736 }
737
738 METHOD(keymat_v1_t, get_hash, bool,
739 private_keymat_v1_t *this, bool initiator, chunk_t dh, chunk_t dh_other,
740 ike_sa_id_t *ike_sa_id, chunk_t sa_i, chunk_t id, chunk_t *hash)
741 {
742 chunk_t data;
743 u_int64_t spi, spi_other;
744
745 /* HASH_I = prf(SKEYID, g^xi | g^xr | CKY-I | CKY-R | SAi_b | IDii_b )
746 * HASH_R = prf(SKEYID, g^xr | g^xi | CKY-R | CKY-I | SAi_b | IDir_b )
747 */
748 if (initiator)
749 {
750 spi = ike_sa_id->get_initiator_spi(ike_sa_id);
751 spi_other = ike_sa_id->get_responder_spi(ike_sa_id);
752 }
753 else
754 {
755 spi_other = ike_sa_id->get_initiator_spi(ike_sa_id);
756 spi = ike_sa_id->get_responder_spi(ike_sa_id);
757 }
758 data = chunk_cat("cccccc", dh, dh_other,
759 chunk_from_thing(spi), chunk_from_thing(spi_other),
760 sa_i, id);
761
762 DBG3(DBG_IKE, "HASH_%c data %B", initiator ? 'I' : 'R', &data);
763
764 if (!this->prf_auth->allocate_bytes(this->prf_auth, data, hash))
765 {
766 free(data.ptr);
767 return FALSE;
768 }
769
770 DBG3(DBG_IKE, "HASH_%c %B", initiator ? 'I' : 'R', hash);
771
772 free(data.ptr);
773 return TRUE;
774 }
775
776 /**
777 * Get the nonce value found in the given message.
778 * Returns FALSE if none is found.
779 */
780 static bool get_nonce(message_t *message, chunk_t *n)
781 {
782 nonce_payload_t *nonce;
783 nonce = (nonce_payload_t*)message->get_payload(message, NONCE_V1);
784 if (nonce)
785 {
786 *n = nonce->get_nonce(nonce);
787 return TRUE;
788 }
789 return FALSE;
790 }
791
792 /**
793 * Generate the message data in order to generate the hashes.
794 */
795 static chunk_t get_message_data(message_t *message, generator_t *generator)
796 {
797 payload_t *payload, *next;
798 enumerator_t *enumerator;
799 u_int32_t *lenpos;
800
801 if (message->is_encoded(message))
802 { /* inbound, although the message is generated, we cannot access the
803 * cleartext message data, so generate it anyway */
804 enumerator = message->create_payload_enumerator(message);
805 while (enumerator->enumerate(enumerator, &payload))
806 {
807 if (payload->get_type(payload) == HASH_V1)
808 {
809 continue;
810 }
811 generator->generate_payload(generator, payload);
812 }
813 enumerator->destroy(enumerator);
814 }
815 else
816 {
817 /* outbound, generate the payloads (there is no HASH payload yet) */
818 enumerator = message->create_payload_enumerator(message);
819 if (enumerator->enumerate(enumerator, &payload))
820 {
821 while (enumerator->enumerate(enumerator, &next))
822 {
823 payload->set_next_type(payload, next->get_type(next));
824 generator->generate_payload(generator, payload);
825 payload = next;
826 }
827 payload->set_next_type(payload, NO_PAYLOAD);
828 generator->generate_payload(generator, payload);
829 }
830 enumerator->destroy(enumerator);
831 }
832 return generator->get_chunk(generator, &lenpos);
833 }
834
835 /**
836 * Try to find data about a Quick Mode with the given message ID,
837 * if none is found, state is generated.
838 */
839 static qm_data_t *lookup_quick_mode(private_keymat_v1_t *this, u_int32_t mid)
840 {
841 enumerator_t *enumerator;
842 qm_data_t *qm, *found = NULL;
843
844 enumerator = this->qms->create_enumerator(this->qms);
845 while (enumerator->enumerate(enumerator, &qm))
846 {
847 if (qm->mid == mid)
848 { /* state gets moved to the front of the list */
849 this->qms->remove_at(this->qms, enumerator);
850 found = qm;
851 break;
852 }
853 }
854 enumerator->destroy(enumerator);
855 if (!found)
856 {
857 INIT(found,
858 .mid = mid,
859 );
860 }
861 this->qms->insert_first(this->qms, found);
862 /* remove least recently used state if maximum reached */
863 if (this->qms->get_count(this->qms) > MAX_QM &&
864 this->qms->remove_last(this->qms, (void**)&qm) == SUCCESS)
865 {
866 qm_data_destroy(qm);
867 }
868 return found;
869 }
870
871 METHOD(keymat_v1_t, get_hash_phase2, bool,
872 private_keymat_v1_t *this, message_t *message, chunk_t *hash)
873 {
874 u_int32_t mid, mid_n;
875 chunk_t data = chunk_empty;
876 bool add_message = TRUE;
877 char *name = "Hash";
878
879 if (!this->prf)
880 { /* no keys derived yet */
881 return FALSE;
882 }
883
884 mid = message->get_message_id(message);
885 mid_n = htonl(mid);
886
887 /* Hashes are simple for most exchanges in Phase 2:
888 * Hash = prf(SKEYID_a, M-ID | Complete message after HASH payload)
889 * For Quick Mode there are three hashes:
890 * Hash(1) = same as above
891 * Hash(2) = prf(SKEYID_a, M-ID | Ni_b | Message after HASH payload)
892 * Hash(3) = prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b)
893 * So, for Quick Mode we keep track of the nonce values.
894 */
895 switch (message->get_exchange_type(message))
896 {
897 case QUICK_MODE:
898 {
899 qm_data_t *qm = lookup_quick_mode(this, mid);
900 if (!qm->n_i.ptr)
901 { /* Hash(1) = prf(SKEYID_a, M-ID | Message after HASH payload) */
902 name = "Hash(1)";
903 if (!get_nonce(message, &qm->n_i))
904 {
905 return FALSE;
906 }
907 data = chunk_from_thing(mid_n);
908 }
909 else if (!qm->n_r.ptr)
910 { /* Hash(2) = prf(SKEYID_a, M-ID | Ni_b | Message after HASH) */
911 name = "Hash(2)";
912 if (!get_nonce(message, &qm->n_r))
913 {
914 return FALSE;
915 }
916 data = chunk_cata("cc", chunk_from_thing(mid_n), qm->n_i);
917 }
918 else
919 { /* Hash(3) = prf(SKEYID_a, 0 | M-ID | Ni_b | Nr_b) */
920 name = "Hash(3)";
921 data = chunk_cata("cccc", octet_0, chunk_from_thing(mid_n),
922 qm->n_i, qm->n_r);
923 add_message = FALSE;
924 /* we don't need the state anymore */
925 this->qms->remove(this->qms, qm, NULL);
926 qm_data_destroy(qm);
927 }
928 break;
929 }
930 case TRANSACTION:
931 case INFORMATIONAL_V1:
932 /* Hash = prf(SKEYID_a, M-ID | Message after HASH payload) */
933 data = chunk_from_thing(mid_n);
934 break;
935 default:
936 return FALSE;
937 }
938 if (!this->prf->set_key(this->prf, this->skeyid_a))
939 {
940 return FALSE;
941 }
942 if (add_message)
943 {
944 generator_t *generator;
945 chunk_t msg;
946
947 generator = generator_create_no_dbg();
948 msg = get_message_data(message, generator);
949 if (!this->prf->allocate_bytes(this->prf, data, NULL) ||
950 !this->prf->allocate_bytes(this->prf, msg, hash))
951 {
952 generator->destroy(generator);
953 return FALSE;
954 }
955 generator->destroy(generator);
956 }
957 else
958 {
959 if (!this->prf->allocate_bytes(this->prf, data, hash))
960 {
961 return FALSE;
962 }
963 }
964 DBG3(DBG_IKE, "%s %B", name, hash);
965 return TRUE;
966 }
967
968 /**
969 * Generate an IV
970 */
971 static bool generate_iv(private_keymat_v1_t *this, iv_data_t *iv)
972 {
973 if (iv->mid == 0 || iv->iv.ptr)
974 { /* use last block of previous encrypted message */
975 chunk_free(&iv->iv);
976 iv->iv = iv->last_block;
977 iv->last_block = chunk_empty;
978 }
979 else
980 {
981 /* initial phase 2 IV = hash(last_phase1_block | mid) */
982 u_int32_t net;;
983 chunk_t data;
984
985 net = htonl(iv->mid);
986 data = chunk_cata("cc", this->phase1_iv.iv, chunk_from_thing(net));
987 if (!this->hasher->allocate_hash(this->hasher, data, &iv->iv))
988 {
989 return FALSE;
990 }
991 if (iv->iv.len > this->aead->get_block_size(this->aead))
992 {
993 iv->iv.len = this->aead->get_block_size(this->aead);
994 }
995 }
996 DBG4(DBG_IKE, "next IV for MID %u %B", iv->mid, &iv->iv);
997 return TRUE;
998 }
999
1000 /**
1001 * Try to find an IV for the given message ID, if not found, generate it.
1002 */
1003 static iv_data_t *lookup_iv(private_keymat_v1_t *this, u_int32_t mid)
1004 {
1005 enumerator_t *enumerator;
1006 iv_data_t *iv, *found = NULL;
1007
1008 if (mid == 0)
1009 {
1010 return &this->phase1_iv;
1011 }
1012
1013 enumerator = this->ivs->create_enumerator(this->ivs);
1014 while (enumerator->enumerate(enumerator, &iv))
1015 {
1016 if (iv->mid == mid)
1017 { /* IV gets moved to the front of the list */
1018 this->ivs->remove_at(this->ivs, enumerator);
1019 found = iv;
1020 break;
1021 }
1022 }
1023 enumerator->destroy(enumerator);
1024 if (!found)
1025 {
1026 INIT(found,
1027 .mid = mid,
1028 );
1029 if (!generate_iv(this, found))
1030 {
1031 iv_data_destroy(found);
1032 return NULL;
1033 }
1034 }
1035 this->ivs->insert_first(this->ivs, found);
1036 /* remove least recently used IV if maximum reached */
1037 if (this->ivs->get_count(this->ivs) > MAX_IV &&
1038 this->ivs->remove_last(this->ivs, (void**)&iv) == SUCCESS)
1039 {
1040 iv_data_destroy(iv);
1041 }
1042 return found;
1043 }
1044
1045 METHOD(keymat_v1_t, get_iv, bool,
1046 private_keymat_v1_t *this, u_int32_t mid, chunk_t *out)
1047 {
1048 iv_data_t *iv;
1049
1050 iv = lookup_iv(this, mid);
1051 if (iv)
1052 {
1053 *out = iv->iv;
1054 return TRUE;
1055 }
1056 return FALSE;
1057 }
1058
1059 METHOD(keymat_v1_t, update_iv, bool,
1060 private_keymat_v1_t *this, u_int32_t mid, chunk_t last_block)
1061 {
1062 iv_data_t *iv = lookup_iv(this, mid);
1063 if (iv)
1064 { /* update last block */
1065 chunk_free(&iv->last_block);
1066 iv->last_block = chunk_clone(last_block);
1067 return TRUE;
1068 }
1069 return FALSE;
1070 }
1071
1072 METHOD(keymat_v1_t, confirm_iv, bool,
1073 private_keymat_v1_t *this, u_int32_t mid)
1074 {
1075 iv_data_t *iv = lookup_iv(this, mid);
1076 if (iv)
1077 {
1078 return generate_iv(this, iv);
1079 }
1080 return FALSE;
1081 }
1082
1083 METHOD(keymat_t, get_version, ike_version_t,
1084 private_keymat_v1_t *this)
1085 {
1086 return IKEV1;
1087 }
1088
1089 METHOD(keymat_t, create_dh, diffie_hellman_t*,
1090 private_keymat_v1_t *this, diffie_hellman_group_t group)
1091 {
1092 return lib->crypto->create_dh(lib->crypto, group);
1093 }
1094
1095 METHOD(keymat_t, create_nonce_gen, nonce_gen_t*,
1096 private_keymat_v1_t *this)
1097 {
1098 return lib->crypto->create_nonce_gen(lib->crypto);
1099 }
1100
1101 METHOD(keymat_t, get_aead, aead_t*,
1102 private_keymat_v1_t *this, bool in)
1103 {
1104 return this->aead;
1105 }
1106
1107 METHOD(keymat_t, destroy, void,
1108 private_keymat_v1_t *this)
1109 {
1110 DESTROY_IF(this->prf);
1111 DESTROY_IF(this->prf_auth);
1112 DESTROY_IF(this->aead);
1113 DESTROY_IF(this->hasher);
1114 chunk_clear(&this->skeyid_d);
1115 chunk_clear(&this->skeyid_a);
1116 chunk_free(&this->phase1_iv.iv);
1117 chunk_free(&this->phase1_iv.last_block);
1118 this->ivs->destroy_function(this->ivs, (void*)iv_data_destroy);
1119 this->qms->destroy_function(this->qms, (void*)qm_data_destroy);
1120 free(this);
1121 }
1122
1123 /**
1124 * See header
1125 */
1126 keymat_v1_t *keymat_v1_create(bool initiator)
1127 {
1128 private_keymat_v1_t *this;
1129
1130 INIT(this,
1131 .public = {
1132 .keymat = {
1133 .get_version = _get_version,
1134 .create_dh = _create_dh,
1135 .create_nonce_gen = _create_nonce_gen,
1136 .get_aead = _get_aead,
1137 .destroy = _destroy,
1138 },
1139 .derive_ike_keys = _derive_ike_keys,
1140 .derive_child_keys = _derive_child_keys,
1141 .create_hasher = _create_hasher,
1142 .get_hasher = _get_hasher,
1143 .get_hash = _get_hash,
1144 .get_hash_phase2 = _get_hash_phase2,
1145 .get_iv = _get_iv,
1146 .update_iv = _update_iv,
1147 .confirm_iv = _confirm_iv,
1148 },
1149 .ivs = linked_list_create(),
1150 .qms = linked_list_create(),
1151 .initiator = initiator,
1152 );
1153
1154 return &this->public;
1155 }