2 * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright 2005 Nokia. All rights reserved.
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
13 #include "record/record_locl.h"
14 #include "internal/ktls.h"
15 #include "internal/cryptlib.h"
16 #include <openssl/comp.h>
17 #include <openssl/evp.h>
18 #include <openssl/kdf.h>
19 #include <openssl/rand.h>
20 #include <openssl/obj_mac.h>
21 #include <openssl/core_names.h>
22 #include <openssl/trace.h>
24 /* seed1 through seed5 are concatenated */
25 static int tls1_PRF(SSL *s,
26 const void *seed1, size_t seed1_len,
27 const void *seed2, size_t seed2_len,
28 const void *seed3, size_t seed3_len,
29 const void *seed4, size_t seed4_len,
30 const void *seed5, size_t seed5_len,
31 const unsigned char *sec, size_t slen,
32 unsigned char *out, size_t olen, int fatal)
34 const EVP_MD *md = ssl_prf_md(s);
36 EVP_KDF_CTX *kctx = NULL;
37 OSSL_PARAM params[8], *p = params;
41 /* Should never happen */
43 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
44 ERR_R_INTERNAL_ERROR);
46 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
49 kdf = EVP_KDF_fetch(NULL, SN_tls1_prf, NULL);
52 kctx = EVP_KDF_CTX_new(kdf);
56 mdname = EVP_MD_name(md);
57 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
58 (char *)mdname, strlen(mdname) + 1);
59 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET,
62 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
63 (void *)seed1, (size_t)seed1_len);
64 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
65 (void *)seed2, (size_t)seed2_len);
66 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
67 (void *)seed3, (size_t)seed3_len);
68 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
69 (void *)seed4, (size_t)seed4_len);
70 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED,
71 (void *)seed5, (size_t)seed5_len);
72 *p = OSSL_PARAM_construct_end();
73 if (EVP_KDF_CTX_set_params(kctx, params)
74 && EVP_KDF_derive(kctx, out, olen)) {
75 EVP_KDF_CTX_free(kctx);
81 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_PRF,
82 ERR_R_INTERNAL_ERROR);
84 SSLerr(SSL_F_TLS1_PRF, ERR_R_INTERNAL_ERROR);
85 EVP_KDF_CTX_free(kctx);
89 static int tls1_generate_key_block(SSL *s, unsigned char *km, size_t num)
93 /* Calls SSLfatal() as required */
95 TLS_MD_KEY_EXPANSION_CONST,
96 TLS_MD_KEY_EXPANSION_CONST_SIZE, s->s3.server_random,
97 SSL3_RANDOM_SIZE, s->s3.client_random, SSL3_RANDOM_SIZE,
98 NULL, 0, NULL, 0, s->session->master_key,
99 s->session->master_key_length, km, num, 1);
104 #ifndef OPENSSL_NO_KTLS
106 * Count the number of records that were not processed yet from record boundary.
108 * This function assumes that there are only fully formed records read in the
109 * record layer. If read_ahead is enabled, then this might be false and this
110 * function will fail.
112 static int count_unprocessed_records(SSL *s)
114 SSL3_BUFFER *rbuf = RECORD_LAYER_get_rbuf(&s->rlayer);
118 if (!PACKET_buf_init(&pkt, rbuf->buf + rbuf->offset, rbuf->left))
121 while (PACKET_remaining(&pkt) > 0) {
122 /* Skip record type and version */
123 if (!PACKET_forward(&pkt, 3))
126 /* Read until next record */
127 if (PACKET_get_length_prefixed_2(&pkt, &subpkt))
137 int tls1_change_cipher_state(SSL *s, int which)
139 unsigned char *p, *mac_secret;
140 unsigned char *ms, *key, *iv;
143 #ifndef OPENSSL_NO_COMP
144 const SSL_COMP *comp;
148 size_t *mac_secret_size;
151 size_t n, i, j, k, cl;
153 #ifndef OPENSSL_NO_KTLS
154 struct tls12_crypto_info_aes_gcm_128 crypto_info;
156 unsigned char geniv[12];
157 int count_unprocessed;
161 c = s->s3.tmp.new_sym_enc;
162 m = s->s3.tmp.new_hash;
163 mac_type = s->s3.tmp.new_mac_pkey_type;
164 #ifndef OPENSSL_NO_COMP
165 comp = s->s3.tmp.new_compression;
168 if (which & SSL3_CC_READ) {
170 s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
172 s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ;
174 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
175 s->mac_flags |= SSL_MAC_FLAG_READ_MAC_STREAM;
177 s->mac_flags &= ~SSL_MAC_FLAG_READ_MAC_STREAM;
179 if (s->enc_read_ctx != NULL) {
181 } else if ((s->enc_read_ctx = EVP_CIPHER_CTX_new()) == NULL) {
182 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
183 ERR_R_MALLOC_FAILURE);
187 * make sure it's initialised in case we exit later with an error
189 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
191 dd = s->enc_read_ctx;
192 mac_ctx = ssl_replace_hash(&s->read_hash, NULL);
193 if (mac_ctx == NULL) {
194 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
195 ERR_R_INTERNAL_ERROR);
198 #ifndef OPENSSL_NO_COMP
199 COMP_CTX_free(s->expand);
202 s->expand = COMP_CTX_new(comp->method);
203 if (s->expand == NULL) {
204 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
205 SSL_F_TLS1_CHANGE_CIPHER_STATE,
206 SSL_R_COMPRESSION_LIBRARY_ERROR);
212 * this is done by dtls1_reset_seq_numbers for DTLS
215 RECORD_LAYER_reset_read_sequence(&s->rlayer);
216 mac_secret = &(s->s3.read_mac_secret[0]);
217 mac_secret_size = &(s->s3.read_mac_secret_size);
219 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
221 s->s3.flags |= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
223 s->s3.flags &= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE;
225 if (s->s3.tmp.new_cipher->algorithm2 & TLS1_STREAM_MAC)
226 s->mac_flags |= SSL_MAC_FLAG_WRITE_MAC_STREAM;
228 s->mac_flags &= ~SSL_MAC_FLAG_WRITE_MAC_STREAM;
229 if (s->enc_write_ctx != NULL && !SSL_IS_DTLS(s)) {
231 } else if ((s->enc_write_ctx = EVP_CIPHER_CTX_new()) == NULL) {
232 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
233 ERR_R_MALLOC_FAILURE);
236 dd = s->enc_write_ctx;
237 if (SSL_IS_DTLS(s)) {
238 mac_ctx = EVP_MD_CTX_new();
239 if (mac_ctx == NULL) {
240 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
241 SSL_F_TLS1_CHANGE_CIPHER_STATE,
242 ERR_R_MALLOC_FAILURE);
245 s->write_hash = mac_ctx;
247 mac_ctx = ssl_replace_hash(&s->write_hash, NULL);
248 if (mac_ctx == NULL) {
249 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
250 SSL_F_TLS1_CHANGE_CIPHER_STATE,
251 ERR_R_MALLOC_FAILURE);
255 #ifndef OPENSSL_NO_COMP
256 COMP_CTX_free(s->compress);
259 s->compress = COMP_CTX_new(comp->method);
260 if (s->compress == NULL) {
261 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
262 SSL_F_TLS1_CHANGE_CIPHER_STATE,
263 SSL_R_COMPRESSION_LIBRARY_ERROR);
269 * this is done by dtls1_reset_seq_numbers for DTLS
272 RECORD_LAYER_reset_write_sequence(&s->rlayer);
273 mac_secret = &(s->s3.write_mac_secret[0]);
274 mac_secret_size = &(s->s3.write_mac_secret_size);
278 EVP_CIPHER_CTX_reset(dd);
280 p = s->s3.tmp.key_block;
281 i = *mac_secret_size = s->s3.tmp.new_mac_secret_size;
283 /* TODO(size_t): convert me */
284 cl = EVP_CIPHER_key_length(c);
286 /* Was j=(exp)?5:EVP_CIPHER_key_length(c); */
287 /* If GCM/CCM mode only part of IV comes from PRF */
288 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE)
289 k = EVP_GCM_TLS_FIXED_IV_LEN;
290 else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE)
291 k = EVP_CCM_TLS_FIXED_IV_LEN;
293 k = EVP_CIPHER_iv_length(c);
294 if ((which == SSL3_CHANGE_CIPHER_CLIENT_WRITE) ||
295 (which == SSL3_CHANGE_CIPHER_SERVER_READ)) {
312 if (n > s->s3.tmp.key_block_length) {
313 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
314 ERR_R_INTERNAL_ERROR);
318 memcpy(mac_secret, ms, i);
320 if (!(EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER)) {
321 /* TODO(size_t): Convert this function */
322 mac_key = EVP_PKEY_new_mac_key(mac_type, NULL, mac_secret,
323 (int)*mac_secret_size);
325 || EVP_DigestSignInit(mac_ctx, NULL, m, NULL, mac_key) <= 0) {
326 EVP_PKEY_free(mac_key);
327 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
328 ERR_R_INTERNAL_ERROR);
331 EVP_PKEY_free(mac_key);
334 OSSL_TRACE_BEGIN(TLS) {
335 BIO_printf(trc_out, "which = %04X, mac key:\n", which);
336 BIO_dump_indent(trc_out, ms, i, 4);
337 } OSSL_TRACE_END(TLS);
339 if (EVP_CIPHER_mode(c) == EVP_CIPH_GCM_MODE) {
340 if (!EVP_CipherInit_ex(dd, c, NULL, key, NULL, (which & SSL3_CC_WRITE))
341 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GCM_SET_IV_FIXED, (int)k,
343 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
344 ERR_R_INTERNAL_ERROR);
347 } else if (EVP_CIPHER_mode(c) == EVP_CIPH_CCM_MODE) {
350 new_cipher->algorithm_enc & (SSL_AES128CCM8 | SSL_AES256CCM8))
351 taglen = EVP_CCM8_TLS_TAG_LEN;
353 taglen = EVP_CCM_TLS_TAG_LEN;
354 if (!EVP_CipherInit_ex(dd, c, NULL, NULL, NULL, (which & SSL3_CC_WRITE))
355 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_IVLEN, 12, NULL)
356 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_TAG, taglen, NULL)
357 || !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_CCM_SET_IV_FIXED, (int)k, iv)
358 || !EVP_CipherInit_ex(dd, NULL, NULL, key, NULL, -1)) {
359 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
360 ERR_R_INTERNAL_ERROR);
364 if (!EVP_CipherInit_ex(dd, c, NULL, key, iv, (which & SSL3_CC_WRITE))) {
365 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
366 ERR_R_INTERNAL_ERROR);
370 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
371 if ((EVP_CIPHER_flags(c) & EVP_CIPH_FLAG_AEAD_CIPHER) && *mac_secret_size
372 && !EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_AEAD_SET_MAC_KEY,
373 (int)*mac_secret_size, mac_secret)) {
374 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
375 ERR_R_INTERNAL_ERROR);
378 #ifndef OPENSSL_NO_KTLS
382 if (((which & SSL3_CC_READ) && (s->mode & SSL_MODE_NO_KTLS_RX))
383 || ((which & SSL3_CC_WRITE) && (s->mode & SSL_MODE_NO_KTLS_TX)))
386 /* ktls supports only the maximum fragment size */
387 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
390 /* check that cipher is AES_GCM_128 */
391 if (EVP_CIPHER_nid(c) != NID_aes_128_gcm
392 || EVP_CIPHER_mode(c) != EVP_CIPH_GCM_MODE
393 || EVP_CIPHER_key_length(c) != TLS_CIPHER_AES_GCM_128_KEY_SIZE)
396 /* check version is 1.2 */
397 if (s->version != TLS1_2_VERSION)
400 if (which & SSL3_CC_WRITE)
405 if (!ossl_assert(bio != NULL)) {
406 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_CHANGE_CIPHER_STATE,
407 ERR_R_INTERNAL_ERROR);
411 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
412 if (which & SSL3_CC_WRITE) {
413 if (BIO_flush(bio) <= 0)
417 /* ktls doesn't support renegotiation */
418 if ((BIO_get_ktls_send(s->wbio) && (which & SSL3_CC_WRITE)) ||
419 (BIO_get_ktls_recv(s->rbio) && (which & SSL3_CC_READ))) {
420 SSLfatal(s, SSL_AD_NO_RENEGOTIATION, SSL_F_TLS1_CHANGE_CIPHER_STATE,
421 ERR_R_INTERNAL_ERROR);
425 memset(&crypto_info, 0, sizeof(crypto_info));
426 crypto_info.info.cipher_type = TLS_CIPHER_AES_GCM_128;
427 crypto_info.info.version = s->version;
429 EVP_CIPHER_CTX_ctrl(dd, EVP_CTRL_GET_IV,
430 EVP_GCM_TLS_FIXED_IV_LEN + EVP_GCM_TLS_EXPLICIT_IV_LEN,
432 memcpy(crypto_info.iv, geniv + EVP_GCM_TLS_FIXED_IV_LEN,
433 TLS_CIPHER_AES_GCM_128_IV_SIZE);
434 memcpy(crypto_info.salt, geniv, TLS_CIPHER_AES_GCM_128_SALT_SIZE);
435 memcpy(crypto_info.key, key, EVP_CIPHER_key_length(c));
436 if (which & SSL3_CC_WRITE)
437 memcpy(crypto_info.rec_seq, &s->rlayer.write_sequence,
438 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
440 memcpy(crypto_info.rec_seq, &s->rlayer.read_sequence,
441 TLS_CIPHER_AES_GCM_128_REC_SEQ_SIZE);
443 if (which & SSL3_CC_READ) {
444 count_unprocessed = count_unprocessed_records(s);
445 if (count_unprocessed < 0)
448 /* increment the crypto_info record sequence */
449 while (count_unprocessed) {
450 for (bit = 7; bit >= 0; bit--) { /* increment */
451 ++crypto_info.rec_seq[bit];
452 if (crypto_info.rec_seq[bit] != 0)
459 /* ktls works with user provided buffers directly */
460 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
461 if (which & SSL3_CC_WRITE)
462 ssl3_release_write_buffer(s);
463 SSL_set_options(s, SSL_OP_NO_RENEGOTIATION);
467 #endif /* OPENSSL_NO_KTLS */
468 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
470 OSSL_TRACE_BEGIN(TLS) {
471 BIO_printf(trc_out, "which = %04X, key:\n", which);
472 BIO_dump_indent(trc_out, key, EVP_CIPHER_key_length(c), 4);
473 BIO_printf(trc_out, "iv:\n");
474 BIO_dump_indent(trc_out, iv, k, 4);
475 } OSSL_TRACE_END(TLS);
482 int tls1_setup_key_block(SSL *s)
488 int mac_type = NID_undef;
489 size_t num, mac_secret_size = 0;
492 if (s->s3.tmp.key_block_length != 0)
495 if (!ssl_cipher_get_evp(s->session, &c, &hash, &mac_type, &mac_secret_size,
496 &comp, s->ext.use_etm)) {
497 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
498 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
502 s->s3.tmp.new_sym_enc = c;
503 s->s3.tmp.new_hash = hash;
504 s->s3.tmp.new_mac_pkey_type = mac_type;
505 s->s3.tmp.new_mac_secret_size = mac_secret_size;
506 num = EVP_CIPHER_key_length(c) + mac_secret_size + EVP_CIPHER_iv_length(c);
509 ssl3_cleanup_key_block(s);
511 if ((p = OPENSSL_malloc(num)) == NULL) {
512 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS1_SETUP_KEY_BLOCK,
513 ERR_R_MALLOC_FAILURE);
517 s->s3.tmp.key_block_length = num;
518 s->s3.tmp.key_block = p;
520 OSSL_TRACE_BEGIN(TLS) {
521 BIO_printf(trc_out, "client random\n");
522 BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
523 BIO_printf(trc_out, "server random\n");
524 BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
525 BIO_printf(trc_out, "master key\n");
526 BIO_dump_indent(trc_out,
527 s->session->master_key,
528 s->session->master_key_length, 4);
529 } OSSL_TRACE_END(TLS);
531 if (!tls1_generate_key_block(s, p, num)) {
532 /* SSLfatal() already called */
536 OSSL_TRACE_BEGIN(TLS) {
537 BIO_printf(trc_out, "key block\n");
538 BIO_dump_indent(trc_out, p, num, 4);
539 } OSSL_TRACE_END(TLS);
541 if (!(s->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)
542 && s->method->version <= TLS1_VERSION) {
544 * enable vulnerability countermeasure for CBC ciphers with known-IV
545 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
547 s->s3.need_empty_fragments = 1;
549 if (s->session->cipher != NULL) {
550 if (s->session->cipher->algorithm_enc == SSL_eNULL)
551 s->s3.need_empty_fragments = 0;
553 #ifndef OPENSSL_NO_RC4
554 if (s->session->cipher->algorithm_enc == SSL_RC4)
555 s->s3.need_empty_fragments = 0;
565 size_t tls1_final_finish_mac(SSL *s, const char *str, size_t slen,
569 unsigned char hash[EVP_MAX_MD_SIZE];
571 if (!ssl3_digest_cached_records(s, 0)) {
572 /* SSLfatal() already called */
576 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
577 /* SSLfatal() already called */
581 if (!tls1_PRF(s, str, slen, hash, hashlen, NULL, 0, NULL, 0, NULL, 0,
582 s->session->master_key, s->session->master_key_length,
583 out, TLS1_FINISH_MAC_LENGTH, 1)) {
584 /* SSLfatal() already called */
587 OPENSSL_cleanse(hash, hashlen);
588 return TLS1_FINISH_MAC_LENGTH;
591 int tls1_generate_master_secret(SSL *s, unsigned char *out, unsigned char *p,
592 size_t len, size_t *secret_size)
594 if (s->session->flags & SSL_SESS_FLAG_EXTMS) {
595 unsigned char hash[EVP_MAX_MD_SIZE * 2];
598 * Digest cached records keeping record buffer (if present): this wont
599 * affect client auth because we're freezing the buffer at the same
600 * point (after client key exchange and before certificate verify)
602 if (!ssl3_digest_cached_records(s, 1)
603 || !ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
604 /* SSLfatal() already called */
607 OSSL_TRACE_BEGIN(TLS) {
608 BIO_printf(trc_out, "Handshake hashes:\n");
609 BIO_dump(trc_out, (char *)hash, hashlen);
610 } OSSL_TRACE_END(TLS);
612 TLS_MD_EXTENDED_MASTER_SECRET_CONST,
613 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE,
617 NULL, 0, p, len, out,
618 SSL3_MASTER_SECRET_SIZE, 1)) {
619 /* SSLfatal() already called */
622 OPENSSL_cleanse(hash, hashlen);
625 TLS_MD_MASTER_SECRET_CONST,
626 TLS_MD_MASTER_SECRET_CONST_SIZE,
627 s->s3.client_random, SSL3_RANDOM_SIZE,
629 s->s3.server_random, SSL3_RANDOM_SIZE,
630 NULL, 0, p, len, out,
631 SSL3_MASTER_SECRET_SIZE, 1)) {
632 /* SSLfatal() already called */
637 OSSL_TRACE_BEGIN(TLS) {
638 BIO_printf(trc_out, "Premaster Secret:\n");
639 BIO_dump_indent(trc_out, p, len, 4);
640 BIO_printf(trc_out, "Client Random:\n");
641 BIO_dump_indent(trc_out, s->s3.client_random, SSL3_RANDOM_SIZE, 4);
642 BIO_printf(trc_out, "Server Random:\n");
643 BIO_dump_indent(trc_out, s->s3.server_random, SSL3_RANDOM_SIZE, 4);
644 BIO_printf(trc_out, "Master Secret:\n");
645 BIO_dump_indent(trc_out,
646 s->session->master_key,
647 SSL3_MASTER_SECRET_SIZE, 4);
648 } OSSL_TRACE_END(TLS);
650 *secret_size = SSL3_MASTER_SECRET_SIZE;
654 int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
655 const char *label, size_t llen,
656 const unsigned char *context,
657 size_t contextlen, int use_context)
659 unsigned char *val = NULL;
660 size_t vallen = 0, currentvalpos;
664 * construct PRF arguments we construct the PRF argument ourself rather
665 * than passing separate values into the TLS PRF to ensure that the
666 * concatenation of values does not create a prohibited label.
668 vallen = llen + SSL3_RANDOM_SIZE * 2;
670 vallen += 2 + contextlen;
673 val = OPENSSL_malloc(vallen);
677 memcpy(val + currentvalpos, (unsigned char *)label, llen);
678 currentvalpos += llen;
679 memcpy(val + currentvalpos, s->s3.client_random, SSL3_RANDOM_SIZE);
680 currentvalpos += SSL3_RANDOM_SIZE;
681 memcpy(val + currentvalpos, s->s3.server_random, SSL3_RANDOM_SIZE);
682 currentvalpos += SSL3_RANDOM_SIZE;
685 val[currentvalpos] = (contextlen >> 8) & 0xff;
687 val[currentvalpos] = contextlen & 0xff;
689 if ((contextlen > 0) || (context != NULL)) {
690 memcpy(val + currentvalpos, context, contextlen);
695 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
696 * label len) = 15, so size of val > max(prohibited label len) = 15 and
697 * the comparisons won't have buffer overflow
699 if (memcmp(val, TLS_MD_CLIENT_FINISH_CONST,
700 TLS_MD_CLIENT_FINISH_CONST_SIZE) == 0)
702 if (memcmp(val, TLS_MD_SERVER_FINISH_CONST,
703 TLS_MD_SERVER_FINISH_CONST_SIZE) == 0)
705 if (memcmp(val, TLS_MD_MASTER_SECRET_CONST,
706 TLS_MD_MASTER_SECRET_CONST_SIZE) == 0)
708 if (memcmp(val, TLS_MD_EXTENDED_MASTER_SECRET_CONST,
709 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE) == 0)
711 if (memcmp(val, TLS_MD_KEY_EXPANSION_CONST,
712 TLS_MD_KEY_EXPANSION_CONST_SIZE) == 0)
721 s->session->master_key, s->session->master_key_length,
726 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
730 SSLerr(SSL_F_TLS1_EXPORT_KEYING_MATERIAL, ERR_R_MALLOC_FAILURE);
733 OPENSSL_clear_free(val, vallen);
737 int tls1_alert_code(int code)
740 case SSL_AD_CLOSE_NOTIFY:
741 return SSL3_AD_CLOSE_NOTIFY;
742 case SSL_AD_UNEXPECTED_MESSAGE:
743 return SSL3_AD_UNEXPECTED_MESSAGE;
744 case SSL_AD_BAD_RECORD_MAC:
745 return SSL3_AD_BAD_RECORD_MAC;
746 case SSL_AD_DECRYPTION_FAILED:
747 return TLS1_AD_DECRYPTION_FAILED;
748 case SSL_AD_RECORD_OVERFLOW:
749 return TLS1_AD_RECORD_OVERFLOW;
750 case SSL_AD_DECOMPRESSION_FAILURE:
751 return SSL3_AD_DECOMPRESSION_FAILURE;
752 case SSL_AD_HANDSHAKE_FAILURE:
753 return SSL3_AD_HANDSHAKE_FAILURE;
754 case SSL_AD_NO_CERTIFICATE:
756 case SSL_AD_BAD_CERTIFICATE:
757 return SSL3_AD_BAD_CERTIFICATE;
758 case SSL_AD_UNSUPPORTED_CERTIFICATE:
759 return SSL3_AD_UNSUPPORTED_CERTIFICATE;
760 case SSL_AD_CERTIFICATE_REVOKED:
761 return SSL3_AD_CERTIFICATE_REVOKED;
762 case SSL_AD_CERTIFICATE_EXPIRED:
763 return SSL3_AD_CERTIFICATE_EXPIRED;
764 case SSL_AD_CERTIFICATE_UNKNOWN:
765 return SSL3_AD_CERTIFICATE_UNKNOWN;
766 case SSL_AD_ILLEGAL_PARAMETER:
767 return SSL3_AD_ILLEGAL_PARAMETER;
768 case SSL_AD_UNKNOWN_CA:
769 return TLS1_AD_UNKNOWN_CA;
770 case SSL_AD_ACCESS_DENIED:
771 return TLS1_AD_ACCESS_DENIED;
772 case SSL_AD_DECODE_ERROR:
773 return TLS1_AD_DECODE_ERROR;
774 case SSL_AD_DECRYPT_ERROR:
775 return TLS1_AD_DECRYPT_ERROR;
776 case SSL_AD_EXPORT_RESTRICTION:
777 return TLS1_AD_EXPORT_RESTRICTION;
778 case SSL_AD_PROTOCOL_VERSION:
779 return TLS1_AD_PROTOCOL_VERSION;
780 case SSL_AD_INSUFFICIENT_SECURITY:
781 return TLS1_AD_INSUFFICIENT_SECURITY;
782 case SSL_AD_INTERNAL_ERROR:
783 return TLS1_AD_INTERNAL_ERROR;
784 case SSL_AD_USER_CANCELLED:
785 return TLS1_AD_USER_CANCELLED;
786 case SSL_AD_NO_RENEGOTIATION:
787 return TLS1_AD_NO_RENEGOTIATION;
788 case SSL_AD_UNSUPPORTED_EXTENSION:
789 return TLS1_AD_UNSUPPORTED_EXTENSION;
790 case SSL_AD_CERTIFICATE_UNOBTAINABLE:
791 return TLS1_AD_CERTIFICATE_UNOBTAINABLE;
792 case SSL_AD_UNRECOGNIZED_NAME:
793 return TLS1_AD_UNRECOGNIZED_NAME;
794 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE:
795 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE;
796 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE:
797 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE;
798 case SSL_AD_UNKNOWN_PSK_IDENTITY:
799 return TLS1_AD_UNKNOWN_PSK_IDENTITY;
800 case SSL_AD_INAPPROPRIATE_FALLBACK:
801 return TLS1_AD_INAPPROPRIATE_FALLBACK;
802 case SSL_AD_NO_APPLICATION_PROTOCOL:
803 return TLS1_AD_NO_APPLICATION_PROTOCOL;
804 case SSL_AD_CERTIFICATE_REQUIRED:
805 return SSL_AD_HANDSHAKE_FAILURE;