2 * Copyright (C) 2010 Martin Willi
3 * Copyright (C) 2010 revosec AG
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>.
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
16 #include "tls_crypto.h"
20 typedef struct private_tls_crypto_t private_tls_crypto_t
;
23 * Private data of an tls_crypto_t object.
25 struct private_tls_crypto_t
{
28 * Public tls_crypto_t interface.
33 * List of supported/acceptable cipher suites
35 tls_cipher_suite_t
*suites
;
38 * Number of supported suites
43 * Selected cipher suite
45 tls_cipher_suite_t suite
;
53 * All handshake data concatentated
58 * Connection state TLS PRF
63 * Signer instance for inbound traffic
68 * Signer instance for outbound traffic
73 * Crypter instance for inbound traffic
75 crypter_t
*crypter_in
;
78 * Crypter instance for outbound traffic
80 crypter_t
*crypter_out
;
83 * IV for input decryption, if < TLSv1.2
88 * IV for output decryption, if < TLSv1.2
99 tls_cipher_suite_t suite
;
100 hash_algorithm_t hash
;
101 pseudo_random_function_t prf
;
102 integrity_algorithm_t mac
;
103 encryption_algorithm_t encr
;
108 * Mapping suites to a set of algorithms
110 static suite_algs_t suite_algs
[] = {
111 { TLS_RSA_WITH_NULL_MD5
,
117 { TLS_RSA_WITH_NULL_SHA
,
123 { TLS_RSA_WITH_NULL_SHA256
,
126 AUTH_HMAC_SHA2_256_256
,
129 { TLS_RSA_WITH_AES_128_CBC_SHA
,
135 { TLS_RSA_WITH_AES_256_CBC_SHA
,
141 { TLS_RSA_WITH_3DES_EDE_CBC_SHA
,
147 { TLS_RSA_WITH_AES_128_CBC_SHA256
,
150 AUTH_HMAC_SHA2_256_256
,
156 * Look up algoritms by a suite
158 static suite_algs_t
*find_suite(tls_cipher_suite_t suite
)
162 for (i
= 0; i
< countof(suite_algs
); i
++)
164 if (suite_algs
[i
].suite
== suite
)
166 return &suite_algs
[i
];
173 * Initialize the cipher suite list
175 static void build_cipher_suite_list(private_tls_crypto_t
*this)
177 encryption_algorithm_t encr
;
178 integrity_algorithm_t mac
;
179 enumerator_t
*encrs
, *macs
;
180 tls_cipher_suite_t supported
[64], unique
[64];
183 /* we assume that we support RSA, but no DHE yet */
184 macs
= lib
->crypto
->create_signer_enumerator(lib
->crypto
);
185 while (macs
->enumerate(macs
, &mac
))
189 case AUTH_HMAC_SHA1_160
:
190 supported
[count
++] = TLS_RSA_WITH_NULL_SHA
;
192 case AUTH_HMAC_SHA2_256_256
:
193 supported
[count
++] = TLS_RSA_WITH_NULL_SHA256
;
195 case AUTH_HMAC_MD5_128
:
196 supported
[count
++] = TLS_RSA_WITH_NULL_MD5
;
201 encrs
= lib
->crypto
->create_crypter_enumerator(lib
->crypto
);
202 while (encrs
->enumerate(encrs
, &encr
))
209 case AUTH_HMAC_SHA1_160
:
210 supported
[count
++] = TLS_RSA_WITH_AES_128_CBC_SHA
;
211 supported
[count
++] = TLS_RSA_WITH_AES_256_CBC_SHA
;
213 case AUTH_HMAC_SHA2_256_256
:
214 supported
[count
++] = TLS_RSA_WITH_AES_128_CBC_SHA256
;
215 supported
[count
++] = TLS_RSA_WITH_AES_128_CBC_SHA256
;
224 case AUTH_HMAC_SHA1_160
:
225 supported
[count
++] = TLS_RSA_WITH_3DES_EDE_CBC_SHA
;
235 encrs
->destroy(encrs
);
239 /* remove duplicates */
240 this->suite_count
= 0;
241 for (i
= 0; i
< count
; i
++)
245 for (j
= 0; j
< this->suite_count
; j
++)
247 if (supported
[i
] == unique
[j
])
255 unique
[this->suite_count
++] = supported
[i
];
259 this->suites
= malloc(sizeof(tls_cipher_suite_t
) * this->suite_count
);
260 memcpy(this->suites
, unique
, sizeof(tls_cipher_suite_t
) * this->suite_count
);
263 METHOD(tls_crypto_t
, get_cipher_suites
, int,
264 private_tls_crypto_t
*this, tls_cipher_suite_t
**suites
)
266 *suites
= this->suites
;
267 return this->suite_count
;
271 * Create crypto primitives
273 static bool create_ciphers(private_tls_crypto_t
*this, tls_cipher_suite_t suite
)
277 algs
= find_suite(suite
);
280 DBG1(DBG_IKE
, "selected TLS suite not supported");
284 DESTROY_IF(this->prf
);
285 if (this->tls
->get_version(this->tls
) < TLS_1_2
)
287 this->prf
= tls_prf_create_10();
291 this->prf
= tls_prf_create_12(algs
->prf
);
295 DBG1(DBG_IKE
, "selected TLS PRF not supported");
299 DESTROY_IF(this->signer_in
);
300 DESTROY_IF(this->signer_out
);
301 this->signer_in
= lib
->crypto
->create_signer(lib
->crypto
, algs
->mac
);
302 this->signer_out
= lib
->crypto
->create_signer(lib
->crypto
, algs
->mac
);
303 if (!this->signer_in
|| !this->signer_out
)
305 DBG1(DBG_IKE
, "selected TLS MAC %N not supported",
306 integrity_algorithm_names
, algs
->mac
);
310 DESTROY_IF(this->crypter_in
);
311 DESTROY_IF(this->crypter_out
);
312 if (algs
->encr
== ENCR_NULL
)
314 this->crypter_in
= this->crypter_out
= NULL
;
318 this->crypter_in
= lib
->crypto
->create_crypter(lib
->crypto
,
319 algs
->encr
, algs
->encr_size
);
320 this->crypter_out
= lib
->crypto
->create_crypter(lib
->crypto
,
321 algs
->encr
, algs
->encr_size
);
322 if (!this->crypter_in
|| !this->crypter_out
)
324 DBG1(DBG_IKE
, "selected TLS crypter %N not supported",
325 encryption_algorithm_names
, algs
->encr
);
332 METHOD(tls_crypto_t
, select_cipher_suite
, tls_cipher_suite_t
,
333 private_tls_crypto_t
*this, tls_cipher_suite_t
*suites
, int count
)
337 for (i
= 0; i
< this->suite_count
; i
++)
339 for (j
= 0; j
< count
; j
++)
341 if (this->suites
[i
] == suites
[j
])
343 if (create_ciphers(this, this->suites
[i
]))
345 this->suite
= this->suites
[i
];
354 METHOD(tls_crypto_t
, append_handshake
, void,
355 private_tls_crypto_t
*this, tls_handshake_type_t type
, chunk_t data
)
359 /* reconstruct handshake header */
360 header
= htonl(data
.len
| (type
<< 24));
361 this->handshake
= chunk_cat("mcc", this->handshake
,
362 chunk_from_thing(header
), data
);
366 * Create a hash of the stored handshake data
368 static bool hash_handshake(private_tls_crypto_t
*this, chunk_t
*hash
)
370 if (this->tls
->get_version(this->tls
) >= TLS_1_2
)
375 alg
= find_suite(this->suite
);
380 hasher
= lib
->crypto
->create_hasher(lib
->crypto
, alg
->hash
);
383 DBG1(DBG_IKE
, "%N not supported", hash_algorithm_names
, alg
->hash
);
386 hasher
->allocate_hash(hasher
, this->handshake
, hash
);
387 hasher
->destroy(hasher
);
391 hasher_t
*md5
, *sha1
;
392 char buf
[HASH_SIZE_MD5
+ HASH_SIZE_SHA1
];
394 md5
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_MD5
);
397 DBG1(DBG_IKE
, "%N not supported", hash_algorithm_names
, HASH_MD5
);
400 md5
->get_hash(md5
, this->handshake
, buf
);
402 sha1
= lib
->crypto
->create_hasher(lib
->crypto
, HASH_SHA1
);
405 DBG1(DBG_IKE
, "%N not supported", hash_algorithm_names
, HASH_SHA1
);
408 sha1
->get_hash(sha1
, this->handshake
, buf
+ HASH_SIZE_MD5
);
411 *hash
= chunk_clone(chunk_from_thing(buf
));
416 METHOD(tls_crypto_t
, sign_handshake
, bool,
417 private_tls_crypto_t
*this, private_key_t
*key
, chunk_t
*sig
)
419 if (this->tls
->get_version(this->tls
) >= TLS_1_2
)
425 if (!key
->sign(key
, SIGN_RSA_EMSA_PKCS1_SHA1
, this->handshake
, sig
))
429 /* TODO: signature scheme to hashsign algorithm mapping */
430 hash_alg
= 2; /* sha1 */
431 sig_alg
= 1; /* RSA */
432 length
= htons(sig
->len
);
433 *sig
= chunk_cat("cccm", chunk_from_thing(hash_alg
),
434 chunk_from_thing(sig_alg
), chunk_from_thing(length
), *sig
);
441 if (!hash_handshake(this, &hash
))
445 if (!key
->sign(key
, SIGN_RSA_EMSA_PKCS1_NULL
, hash
, sig
))
451 length
= htons(sig
->len
);
452 *sig
= chunk_cat("cm", chunk_from_thing(length
), *sig
);
457 METHOD(tls_crypto_t
, calculate_finished
, bool,
458 private_tls_crypto_t
*this, char *label
, char out
[12])
466 if (!hash_handshake(this, &seed
))
470 this->prf
->get_bytes(this->prf
, label
, seed
, 12, out
);
475 METHOD(tls_crypto_t
, derive_secrets
, void,
476 private_tls_crypto_t
*this, chunk_t premaster
,
477 chunk_t client_random
, chunk_t server_random
)
480 chunk_t seed
, block
, client_write
, server_write
;
481 int mks
, eks
= 0, ivs
= 0;
483 /* derive master secret */
484 seed
= chunk_cata("cc", client_random
, server_random
);
485 this->prf
->set_key(this->prf
, premaster
);
486 this->prf
->get_bytes(this->prf
, "master secret", seed
,
487 sizeof(master
), master
);
489 this->prf
->set_key(this->prf
, chunk_from_thing(master
));
490 memset(master
, 0, sizeof(master
));
492 /* derive key block for key expansion */
493 mks
= this->signer_out
->get_key_size(this->signer_out
);
494 if (this->crypter_out
)
496 eks
= this->crypter_out
->get_key_size(this->crypter_out
);
497 if (this->tls
->get_version(this->tls
) < TLS_1_1
)
499 ivs
= this->crypter_out
->get_block_size(this->crypter_out
);
502 seed
= chunk_cata("cc", server_random
, client_random
);
503 block
= chunk_alloca((mks
+ eks
+ ivs
) * 2);
504 this->prf
->get_bytes(this->prf
, "key expansion", seed
, block
.len
, block
.ptr
);
507 client_write
= chunk_create(block
.ptr
, mks
);
508 block
= chunk_skip(block
, mks
);
509 server_write
= chunk_create(block
.ptr
, mks
);
510 block
= chunk_skip(block
, mks
);
511 if (this->tls
->is_server(this->tls
))
513 this->signer_in
->set_key(this->signer_in
, client_write
);
514 this->signer_out
->set_key(this->signer_out
, server_write
);
518 this->signer_out
->set_key(this->signer_out
, client_write
);
519 this->signer_in
->set_key(this->signer_in
, server_write
);
522 /* crypter keys, and IVs if < TLSv1.2 */
523 if (this->crypter_out
&& this->crypter_in
)
525 client_write
= chunk_create(block
.ptr
, eks
);
526 block
= chunk_skip(block
, eks
);
527 server_write
= chunk_create(block
.ptr
, eks
);
528 block
= chunk_skip(block
, eks
);
530 if (this->tls
->is_server(this->tls
))
532 this->crypter_in
->set_key(this->crypter_in
, client_write
);
533 this->crypter_out
->set_key(this->crypter_out
, server_write
);
537 this->crypter_out
->set_key(this->crypter_out
, client_write
);
538 this->crypter_in
->set_key(this->crypter_in
, server_write
);
542 client_write
= chunk_create(block
.ptr
, ivs
);
543 block
= chunk_skip(block
, ivs
);
544 server_write
= chunk_create(block
.ptr
, ivs
);
545 block
= chunk_skip(block
, ivs
);
547 if (this->tls
->is_server(this->tls
))
549 this->iv_in
= chunk_clone(client_write
);
550 this->iv_out
= chunk_clone(server_write
);
554 this->iv_out
= chunk_clone(client_write
);
555 this->iv_in
= chunk_clone(server_write
);
561 METHOD(tls_crypto_t
, change_cipher
, void,
562 private_tls_crypto_t
*this, bool inbound
)
566 this->tls
->change_cipher(this->tls
, TRUE
, this->signer_in
,
567 this->crypter_in
, this->iv_in
);
571 this->tls
->change_cipher(this->tls
, FALSE
, this->signer_out
,
572 this->crypter_out
, this->iv_out
);
576 METHOD(tls_crypto_t
, derive_eap_msk
, void,
577 private_tls_crypto_t
*this, chunk_t client_random
, chunk_t server_random
)
581 seed
= chunk_cata("cc", client_random
, server_random
);
583 this->msk
= chunk_alloc(64);
584 this->prf
->get_bytes(this->prf
, "client EAP encryption", seed
,
585 this->msk
.len
, this->msk
.ptr
);
588 METHOD(tls_crypto_t
, get_eap_msk
, chunk_t
,
589 private_tls_crypto_t
*this)
594 METHOD(tls_crypto_t
, destroy
, void,
595 private_tls_crypto_t
*this)
597 DESTROY_IF(this->signer_in
);
598 DESTROY_IF(this->signer_out
);
599 DESTROY_IF(this->crypter_in
);
600 DESTROY_IF(this->crypter_out
);
601 free(this->iv_in
.ptr
);
602 free(this->iv_out
.ptr
);
603 free(this->handshake
.ptr
);
605 DESTROY_IF(this->prf
);
613 tls_crypto_t
*tls_crypto_create(tls_t
*tls
)
615 private_tls_crypto_t
*this;
619 .get_cipher_suites
= _get_cipher_suites
,
620 .select_cipher_suite
= _select_cipher_suite
,
621 .append_handshake
= _append_handshake
,
622 .sign_handshake
= _sign_handshake
,
623 .calculate_finished
= _calculate_finished
,
624 .derive_secrets
= _derive_secrets
,
625 .change_cipher
= _change_cipher
,
626 .derive_eap_msk
= _derive_eap_msk
,
627 .get_eap_msk
= _get_eap_msk
,
633 build_cipher_suite_list(this);
635 return &this->public;