2 * Copyright 1995-2021 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
12 #include "ssl_local.h"
13 #include "record/record_local.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
, ERR_R_INTERNAL_ERROR
);
45 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
48 kdf
= EVP_KDF_fetch(s
->ctx
->libctx
, OSSL_KDF_NAME_TLS1_PRF
, s
->ctx
->propq
);
51 kctx
= EVP_KDF_CTX_new(kdf
);
55 mdname
= EVP_MD_name(md
);
56 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST
,
58 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SECRET
,
61 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED
,
62 (void *)seed1
, (size_t)seed1_len
);
63 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED
,
64 (void *)seed2
, (size_t)seed2_len
);
65 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED
,
66 (void *)seed3
, (size_t)seed3_len
);
67 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED
,
68 (void *)seed4
, (size_t)seed4_len
);
69 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SEED
,
70 (void *)seed5
, (size_t)seed5_len
);
71 *p
= OSSL_PARAM_construct_end();
72 if (EVP_KDF_CTX_set_params(kctx
, params
)
73 && EVP_KDF_derive(kctx
, out
, olen
)) {
74 EVP_KDF_CTX_free(kctx
);
80 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
82 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
83 EVP_KDF_CTX_free(kctx
);
87 static int tls1_generate_key_block(SSL
*s
, unsigned char *km
, size_t num
)
91 /* Calls SSLfatal() as required */
93 TLS_MD_KEY_EXPANSION_CONST
,
94 TLS_MD_KEY_EXPANSION_CONST_SIZE
, s
->s3
.server_random
,
95 SSL3_RANDOM_SIZE
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
,
96 NULL
, 0, NULL
, 0, s
->session
->master_key
,
97 s
->session
->master_key_length
, km
, num
, 1);
102 #ifndef OPENSSL_NO_KTLS
104 * Count the number of records that were not processed yet from record boundary.
106 * This function assumes that there are only fully formed records read in the
107 * record layer. If read_ahead is enabled, then this might be false and this
108 * function will fail.
110 # ifndef OPENSSL_NO_KTLS_RX
111 static int count_unprocessed_records(SSL
*s
)
113 SSL3_BUFFER
*rbuf
= RECORD_LAYER_get_rbuf(&s
->rlayer
);
117 if (!PACKET_buf_init(&pkt
, rbuf
->buf
+ rbuf
->offset
, rbuf
->left
))
120 while (PACKET_remaining(&pkt
) > 0) {
121 /* Skip record type and version */
122 if (!PACKET_forward(&pkt
, 3))
125 /* Read until next record */
126 if (PACKET_get_length_prefixed_2(&pkt
, &subpkt
))
138 int tls_provider_set_tls_params(SSL
*s
, EVP_CIPHER_CTX
*ctx
,
139 const EVP_CIPHER
*ciph
,
143 * Provided cipher, the TLS padding/MAC removal is performed provider
144 * side so we need to tell the ctx about our TLS version and mac size
146 OSSL_PARAM params
[3], *pprm
= params
;
150 if ((EVP_CIPHER_flags(ciph
) & EVP_CIPH_FLAG_AEAD_CIPHER
) == 0
152 * We look at s->ext.use_etm instead of SSL_READ_ETM() or
153 * SSL_WRITE_ETM() because this test applies to both reading
157 imacsize
= EVP_MD_size(md
);
159 macsize
= (size_t)imacsize
;
161 *pprm
++ = OSSL_PARAM_construct_int(OSSL_CIPHER_PARAM_TLS_VERSION
,
163 *pprm
++ = OSSL_PARAM_construct_size_t(OSSL_CIPHER_PARAM_TLS_MAC_SIZE
,
165 *pprm
= OSSL_PARAM_construct_end();
167 if (!EVP_CIPHER_CTX_set_params(ctx
, params
)) {
168 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
176 static int tls_iv_length_within_key_block(const EVP_CIPHER
*c
)
178 /* If GCM/CCM mode only part of IV comes from PRF */
179 if (EVP_CIPHER_mode(c
) == EVP_CIPH_GCM_MODE
)
180 return EVP_GCM_TLS_FIXED_IV_LEN
;
181 else if (EVP_CIPHER_mode(c
) == EVP_CIPH_CCM_MODE
)
182 return EVP_CCM_TLS_FIXED_IV_LEN
;
184 return EVP_CIPHER_iv_length(c
);
187 int tls1_change_cipher_state(SSL
*s
, int which
)
189 unsigned char *p
, *mac_secret
;
190 unsigned char *ms
, *key
, *iv
;
193 #ifndef OPENSSL_NO_COMP
194 const SSL_COMP
*comp
;
198 size_t *mac_secret_size
;
201 size_t n
, i
, j
, k
, cl
;
203 #ifndef OPENSSL_NO_KTLS
204 ktls_crypto_info_t crypto_info
;
205 unsigned char *rec_seq
;
207 # ifndef OPENSSL_NO_KTLS_RX
208 int count_unprocessed
;
214 c
= s
->s3
.tmp
.new_sym_enc
;
215 m
= s
->s3
.tmp
.new_hash
;
216 mac_type
= s
->s3
.tmp
.new_mac_pkey_type
;
217 #ifndef OPENSSL_NO_COMP
218 comp
= s
->s3
.tmp
.new_compression
;
221 if (which
& SSL3_CC_READ
) {
223 s
->s3
.flags
|= TLS1_FLAGS_ENCRYPT_THEN_MAC_READ
;
225 s
->s3
.flags
&= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_READ
;
227 if (s
->s3
.tmp
.new_cipher
->algorithm2
& TLS1_STREAM_MAC
)
228 s
->mac_flags
|= SSL_MAC_FLAG_READ_MAC_STREAM
;
230 s
->mac_flags
&= ~SSL_MAC_FLAG_READ_MAC_STREAM
;
232 if (s
->s3
.tmp
.new_cipher
->algorithm2
& TLS1_TLSTREE
)
233 s
->mac_flags
|= SSL_MAC_FLAG_READ_MAC_TLSTREE
;
235 s
->mac_flags
&= ~SSL_MAC_FLAG_READ_MAC_TLSTREE
;
237 if (s
->enc_read_ctx
!= NULL
) {
239 } else if ((s
->enc_read_ctx
= EVP_CIPHER_CTX_new()) == NULL
) {
240 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
244 * make sure it's initialised in case we exit later with an error
246 EVP_CIPHER_CTX_reset(s
->enc_read_ctx
);
248 dd
= s
->enc_read_ctx
;
249 mac_ctx
= ssl_replace_hash(&s
->read_hash
, NULL
);
250 if (mac_ctx
== NULL
) {
251 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
254 #ifndef OPENSSL_NO_COMP
255 COMP_CTX_free(s
->expand
);
258 s
->expand
= COMP_CTX_new(comp
->method
);
259 if (s
->expand
== NULL
) {
260 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
261 SSL_R_COMPRESSION_LIBRARY_ERROR
);
267 * this is done by dtls1_reset_seq_numbers for DTLS
270 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
271 mac_secret
= &(s
->s3
.read_mac_secret
[0]);
272 mac_secret_size
= &(s
->s3
.read_mac_secret_size
);
274 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
276 s
->s3
.flags
|= TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE
;
278 s
->s3
.flags
&= ~TLS1_FLAGS_ENCRYPT_THEN_MAC_WRITE
;
280 if (s
->s3
.tmp
.new_cipher
->algorithm2
& TLS1_STREAM_MAC
)
281 s
->mac_flags
|= SSL_MAC_FLAG_WRITE_MAC_STREAM
;
283 s
->mac_flags
&= ~SSL_MAC_FLAG_WRITE_MAC_STREAM
;
285 if (s
->s3
.tmp
.new_cipher
->algorithm2
& TLS1_TLSTREE
)
286 s
->mac_flags
|= SSL_MAC_FLAG_WRITE_MAC_TLSTREE
;
288 s
->mac_flags
&= ~SSL_MAC_FLAG_WRITE_MAC_TLSTREE
;
289 if (s
->enc_write_ctx
!= NULL
&& !SSL_IS_DTLS(s
)) {
291 } else if ((s
->enc_write_ctx
= EVP_CIPHER_CTX_new()) == NULL
) {
292 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
295 dd
= s
->enc_write_ctx
;
296 if (SSL_IS_DTLS(s
)) {
297 mac_ctx
= EVP_MD_CTX_new();
298 if (mac_ctx
== NULL
) {
299 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
302 s
->write_hash
= mac_ctx
;
304 mac_ctx
= ssl_replace_hash(&s
->write_hash
, NULL
);
305 if (mac_ctx
== NULL
) {
306 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
310 #ifndef OPENSSL_NO_COMP
311 COMP_CTX_free(s
->compress
);
314 s
->compress
= COMP_CTX_new(comp
->method
);
315 if (s
->compress
== NULL
) {
316 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
317 SSL_R_COMPRESSION_LIBRARY_ERROR
);
323 * this is done by dtls1_reset_seq_numbers for DTLS
326 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
327 mac_secret
= &(s
->s3
.write_mac_secret
[0]);
328 mac_secret_size
= &(s
->s3
.write_mac_secret_size
);
332 EVP_CIPHER_CTX_reset(dd
);
334 p
= s
->s3
.tmp
.key_block
;
335 i
= *mac_secret_size
= s
->s3
.tmp
.new_mac_secret_size
;
337 /* TODO(size_t): convert me */
338 cl
= EVP_CIPHER_key_length(c
);
340 k
= tls_iv_length_within_key_block(c
);
341 if ((which
== SSL3_CHANGE_CIPHER_CLIENT_WRITE
) ||
342 (which
== SSL3_CHANGE_CIPHER_SERVER_READ
)) {
359 if (n
> s
->s3
.tmp
.key_block_length
) {
360 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
364 memcpy(mac_secret
, ms
, i
);
366 if (!(EVP_CIPHER_flags(c
) & EVP_CIPH_FLAG_AEAD_CIPHER
)) {
367 if (mac_type
== EVP_PKEY_HMAC
) {
368 mac_key
= EVP_PKEY_new_raw_private_key_ex(s
->ctx
->libctx
, "HMAC",
369 s
->ctx
->propq
, mac_secret
,
373 * If its not HMAC then the only other types of MAC we support are
374 * the GOST MACs, so we need to use the old style way of creating
377 mac_key
= EVP_PKEY_new_mac_key(mac_type
, NULL
, mac_secret
,
378 (int)*mac_secret_size
);
381 || EVP_DigestSignInit_ex(mac_ctx
, NULL
, EVP_MD_name(m
),
382 s
->ctx
->libctx
, s
->ctx
->propq
, mac_key
) <= 0) {
383 EVP_PKEY_free(mac_key
);
384 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
387 EVP_PKEY_free(mac_key
);
390 OSSL_TRACE_BEGIN(TLS
) {
391 BIO_printf(trc_out
, "which = %04X, mac key:\n", which
);
392 BIO_dump_indent(trc_out
, ms
, i
, 4);
393 } OSSL_TRACE_END(TLS
);
395 if (EVP_CIPHER_mode(c
) == EVP_CIPH_GCM_MODE
) {
396 if (!EVP_CipherInit_ex(dd
, c
, NULL
, key
, NULL
, (which
& SSL3_CC_WRITE
))
397 || !EVP_CIPHER_CTX_ctrl(dd
, EVP_CTRL_GCM_SET_IV_FIXED
, (int)k
,
399 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
402 } else if (EVP_CIPHER_mode(c
) == EVP_CIPH_CCM_MODE
) {
405 new_cipher
->algorithm_enc
& (SSL_AES128CCM8
| SSL_AES256CCM8
))
406 taglen
= EVP_CCM8_TLS_TAG_LEN
;
408 taglen
= EVP_CCM_TLS_TAG_LEN
;
409 if (!EVP_CipherInit_ex(dd
, c
, NULL
, NULL
, NULL
, (which
& SSL3_CC_WRITE
))
410 || !EVP_CIPHER_CTX_ctrl(dd
, EVP_CTRL_AEAD_SET_IVLEN
, 12, NULL
)
411 || !EVP_CIPHER_CTX_ctrl(dd
, EVP_CTRL_AEAD_SET_TAG
, taglen
, NULL
)
412 || !EVP_CIPHER_CTX_ctrl(dd
, EVP_CTRL_CCM_SET_IV_FIXED
, (int)k
, iv
)
413 || !EVP_CipherInit_ex(dd
, NULL
, NULL
, key
, NULL
, -1)) {
414 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
418 if (!EVP_CipherInit_ex(dd
, c
, NULL
, key
, iv
, (which
& SSL3_CC_WRITE
))) {
419 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
423 /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
424 if ((EVP_CIPHER_flags(c
) & EVP_CIPH_FLAG_AEAD_CIPHER
) && *mac_secret_size
425 && !EVP_CIPHER_CTX_ctrl(dd
, EVP_CTRL_AEAD_SET_MAC_KEY
,
426 (int)*mac_secret_size
, mac_secret
)) {
427 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
430 if (EVP_CIPHER_provider(c
) != NULL
431 && !tls_provider_set_tls_params(s
, dd
, c
, m
)) {
432 /* SSLfatal already called */
436 #ifndef OPENSSL_NO_KTLS
440 if (((which
& SSL3_CC_READ
) && (s
->mode
& SSL_MODE_NO_KTLS_RX
))
441 || ((which
& SSL3_CC_WRITE
) && (s
->mode
& SSL_MODE_NO_KTLS_TX
)))
444 /* ktls supports only the maximum fragment size */
445 if (ssl_get_max_send_fragment(s
) != SSL3_RT_MAX_PLAIN_LENGTH
)
448 /* check that cipher is supported */
449 if (!ktls_check_supported_cipher(s
, c
, dd
))
452 if (which
& SSL3_CC_WRITE
)
457 if (!ossl_assert(bio
!= NULL
)) {
458 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
462 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
463 if (which
& SSL3_CC_WRITE
) {
464 if (BIO_flush(bio
) <= 0)
468 /* ktls doesn't support renegotiation */
469 if ((BIO_get_ktls_send(s
->wbio
) && (which
& SSL3_CC_WRITE
)) ||
470 (BIO_get_ktls_recv(s
->rbio
) && (which
& SSL3_CC_READ
))) {
471 SSLfatal(s
, SSL_AD_NO_RENEGOTIATION
, ERR_R_INTERNAL_ERROR
);
475 if (which
& SSL3_CC_WRITE
)
476 rl_sequence
= RECORD_LAYER_get_write_sequence(&s
->rlayer
);
478 rl_sequence
= RECORD_LAYER_get_read_sequence(&s
->rlayer
);
480 if (!ktls_configure_crypto(s
, c
, dd
, rl_sequence
, &crypto_info
, &rec_seq
,
481 iv
, key
, ms
, *mac_secret_size
))
484 if (which
& SSL3_CC_READ
) {
485 # ifndef OPENSSL_NO_KTLS_RX
486 count_unprocessed
= count_unprocessed_records(s
);
487 if (count_unprocessed
< 0)
490 /* increment the crypto_info record sequence */
491 while (count_unprocessed
) {
492 for (bit
= 7; bit
>= 0; bit
--) { /* increment */
494 if (rec_seq
[bit
] != 0)
504 /* ktls works with user provided buffers directly */
505 if (BIO_set_ktls(bio
, &crypto_info
, which
& SSL3_CC_WRITE
)) {
506 if (which
& SSL3_CC_WRITE
)
507 ssl3_release_write_buffer(s
);
508 SSL_set_options(s
, SSL_OP_NO_RENEGOTIATION
);
512 #endif /* OPENSSL_NO_KTLS */
513 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
515 OSSL_TRACE_BEGIN(TLS
) {
516 BIO_printf(trc_out
, "which = %04X, key:\n", which
);
517 BIO_dump_indent(trc_out
, key
, EVP_CIPHER_key_length(c
), 4);
518 BIO_printf(trc_out
, "iv:\n");
519 BIO_dump_indent(trc_out
, iv
, k
, 4);
520 } OSSL_TRACE_END(TLS
);
527 int tls1_setup_key_block(SSL
*s
)
533 int mac_type
= NID_undef
;
534 size_t num
, mac_secret_size
= 0;
537 if (s
->s3
.tmp
.key_block_length
!= 0)
540 if (!ssl_cipher_get_evp(s
->ctx
, s
->session
, &c
, &hash
, &mac_type
,
541 &mac_secret_size
, &comp
, s
->ext
.use_etm
)) {
542 /* Error is already recorded */
543 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
547 ssl_evp_cipher_free(s
->s3
.tmp
.new_sym_enc
);
548 s
->s3
.tmp
.new_sym_enc
= c
;
549 ssl_evp_md_free(s
->s3
.tmp
.new_hash
);
550 s
->s3
.tmp
.new_hash
= hash
;
551 s
->s3
.tmp
.new_mac_pkey_type
= mac_type
;
552 s
->s3
.tmp
.new_mac_secret_size
= mac_secret_size
;
553 num
= mac_secret_size
+ EVP_CIPHER_key_length(c
) + tls_iv_length_within_key_block(c
);
556 ssl3_cleanup_key_block(s
);
558 if ((p
= OPENSSL_malloc(num
)) == NULL
) {
559 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
563 s
->s3
.tmp
.key_block_length
= num
;
564 s
->s3
.tmp
.key_block
= p
;
566 OSSL_TRACE_BEGIN(TLS
) {
567 BIO_printf(trc_out
, "key block length: %ld\n", num
);
568 BIO_printf(trc_out
, "client random\n");
569 BIO_dump_indent(trc_out
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
, 4);
570 BIO_printf(trc_out
, "server random\n");
571 BIO_dump_indent(trc_out
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
, 4);
572 BIO_printf(trc_out
, "master key\n");
573 BIO_dump_indent(trc_out
,
574 s
->session
->master_key
,
575 s
->session
->master_key_length
, 4);
576 } OSSL_TRACE_END(TLS
);
578 if (!tls1_generate_key_block(s
, p
, num
)) {
579 /* SSLfatal() already called */
583 OSSL_TRACE_BEGIN(TLS
) {
584 BIO_printf(trc_out
, "key block\n");
585 BIO_dump_indent(trc_out
, p
, num
, 4);
586 } OSSL_TRACE_END(TLS
);
588 if (!(s
->options
& SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS
)
589 && s
->method
->version
<= TLS1_VERSION
) {
591 * enable vulnerability countermeasure for CBC ciphers with known-IV
592 * problem (http://www.openssl.org/~bodo/tls-cbc.txt)
594 s
->s3
.need_empty_fragments
= 1;
596 if (s
->session
->cipher
!= NULL
) {
597 if (s
->session
->cipher
->algorithm_enc
== SSL_eNULL
)
598 s
->s3
.need_empty_fragments
= 0;
600 if (s
->session
->cipher
->algorithm_enc
== SSL_RC4
)
601 s
->s3
.need_empty_fragments
= 0;
610 size_t tls1_final_finish_mac(SSL
*s
, const char *str
, size_t slen
,
614 unsigned char hash
[EVP_MAX_MD_SIZE
];
615 size_t finished_size
= TLS1_FINISH_MAC_LENGTH
;
617 if (s
->s3
.tmp
.new_cipher
->algorithm_mkey
& SSL_kGOST18
)
620 if (!ssl3_digest_cached_records(s
, 0)) {
621 /* SSLfatal() already called */
625 if (!ssl_handshake_hash(s
, hash
, sizeof(hash
), &hashlen
)) {
626 /* SSLfatal() already called */
630 if (!tls1_PRF(s
, str
, slen
, hash
, hashlen
, NULL
, 0, NULL
, 0, NULL
, 0,
631 s
->session
->master_key
, s
->session
->master_key_length
,
632 out
, finished_size
, 1)) {
633 /* SSLfatal() already called */
636 OPENSSL_cleanse(hash
, hashlen
);
637 return finished_size
;
640 int tls1_generate_master_secret(SSL
*s
, unsigned char *out
, unsigned char *p
,
641 size_t len
, size_t *secret_size
)
643 if (s
->session
->flags
& SSL_SESS_FLAG_EXTMS
) {
644 unsigned char hash
[EVP_MAX_MD_SIZE
* 2];
647 * Digest cached records keeping record buffer (if present): this won't
648 * affect client auth because we're freezing the buffer at the same
649 * point (after client key exchange and before certificate verify)
651 if (!ssl3_digest_cached_records(s
, 1)
652 || !ssl_handshake_hash(s
, hash
, sizeof(hash
), &hashlen
)) {
653 /* SSLfatal() already called */
656 OSSL_TRACE_BEGIN(TLS
) {
657 BIO_printf(trc_out
, "Handshake hashes:\n");
658 BIO_dump(trc_out
, (char *)hash
, hashlen
);
659 } OSSL_TRACE_END(TLS
);
661 TLS_MD_EXTENDED_MASTER_SECRET_CONST
,
662 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE
,
666 NULL
, 0, p
, len
, out
,
667 SSL3_MASTER_SECRET_SIZE
, 1)) {
668 /* SSLfatal() already called */
671 OPENSSL_cleanse(hash
, hashlen
);
674 TLS_MD_MASTER_SECRET_CONST
,
675 TLS_MD_MASTER_SECRET_CONST_SIZE
,
676 s
->s3
.client_random
, SSL3_RANDOM_SIZE
,
678 s
->s3
.server_random
, SSL3_RANDOM_SIZE
,
679 NULL
, 0, p
, len
, out
,
680 SSL3_MASTER_SECRET_SIZE
, 1)) {
681 /* SSLfatal() already called */
686 OSSL_TRACE_BEGIN(TLS
) {
687 BIO_printf(trc_out
, "Premaster Secret:\n");
688 BIO_dump_indent(trc_out
, p
, len
, 4);
689 BIO_printf(trc_out
, "Client Random:\n");
690 BIO_dump_indent(trc_out
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
, 4);
691 BIO_printf(trc_out
, "Server Random:\n");
692 BIO_dump_indent(trc_out
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
, 4);
693 BIO_printf(trc_out
, "Master Secret:\n");
694 BIO_dump_indent(trc_out
,
695 s
->session
->master_key
,
696 SSL3_MASTER_SECRET_SIZE
, 4);
697 } OSSL_TRACE_END(TLS
);
699 *secret_size
= SSL3_MASTER_SECRET_SIZE
;
703 int tls1_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
704 const char *label
, size_t llen
,
705 const unsigned char *context
,
706 size_t contextlen
, int use_context
)
708 unsigned char *val
= NULL
;
709 size_t vallen
= 0, currentvalpos
;
713 * construct PRF arguments we construct the PRF argument ourself rather
714 * than passing separate values into the TLS PRF to ensure that the
715 * concatenation of values does not create a prohibited label.
717 vallen
= llen
+ SSL3_RANDOM_SIZE
* 2;
719 vallen
+= 2 + contextlen
;
722 val
= OPENSSL_malloc(vallen
);
726 memcpy(val
+ currentvalpos
, (unsigned char *)label
, llen
);
727 currentvalpos
+= llen
;
728 memcpy(val
+ currentvalpos
, s
->s3
.client_random
, SSL3_RANDOM_SIZE
);
729 currentvalpos
+= SSL3_RANDOM_SIZE
;
730 memcpy(val
+ currentvalpos
, s
->s3
.server_random
, SSL3_RANDOM_SIZE
);
731 currentvalpos
+= SSL3_RANDOM_SIZE
;
734 val
[currentvalpos
] = (contextlen
>> 8) & 0xff;
736 val
[currentvalpos
] = contextlen
& 0xff;
738 if ((contextlen
> 0) || (context
!= NULL
)) {
739 memcpy(val
+ currentvalpos
, context
, contextlen
);
744 * disallow prohibited labels note that SSL3_RANDOM_SIZE > max(prohibited
745 * label len) = 15, so size of val > max(prohibited label len) = 15 and
746 * the comparisons won't have buffer overflow
748 if (memcmp(val
, TLS_MD_CLIENT_FINISH_CONST
,
749 TLS_MD_CLIENT_FINISH_CONST_SIZE
) == 0)
751 if (memcmp(val
, TLS_MD_SERVER_FINISH_CONST
,
752 TLS_MD_SERVER_FINISH_CONST_SIZE
) == 0)
754 if (memcmp(val
, TLS_MD_MASTER_SECRET_CONST
,
755 TLS_MD_MASTER_SECRET_CONST_SIZE
) == 0)
757 if (memcmp(val
, TLS_MD_EXTENDED_MASTER_SECRET_CONST
,
758 TLS_MD_EXTENDED_MASTER_SECRET_CONST_SIZE
) == 0)
760 if (memcmp(val
, TLS_MD_KEY_EXPANSION_CONST
,
761 TLS_MD_KEY_EXPANSION_CONST_SIZE
) == 0)
770 s
->session
->master_key
, s
->session
->master_key_length
,
775 ERR_raise(ERR_LIB_SSL
, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL
);
779 ERR_raise(ERR_LIB_SSL
, ERR_R_MALLOC_FAILURE
);
782 OPENSSL_clear_free(val
, vallen
);
786 int tls1_alert_code(int code
)
789 case SSL_AD_CLOSE_NOTIFY
:
790 return SSL3_AD_CLOSE_NOTIFY
;
791 case SSL_AD_UNEXPECTED_MESSAGE
:
792 return SSL3_AD_UNEXPECTED_MESSAGE
;
793 case SSL_AD_BAD_RECORD_MAC
:
794 return SSL3_AD_BAD_RECORD_MAC
;
795 case SSL_AD_DECRYPTION_FAILED
:
796 return TLS1_AD_DECRYPTION_FAILED
;
797 case SSL_AD_RECORD_OVERFLOW
:
798 return TLS1_AD_RECORD_OVERFLOW
;
799 case SSL_AD_DECOMPRESSION_FAILURE
:
800 return SSL3_AD_DECOMPRESSION_FAILURE
;
801 case SSL_AD_HANDSHAKE_FAILURE
:
802 return SSL3_AD_HANDSHAKE_FAILURE
;
803 case SSL_AD_NO_CERTIFICATE
:
805 case SSL_AD_BAD_CERTIFICATE
:
806 return SSL3_AD_BAD_CERTIFICATE
;
807 case SSL_AD_UNSUPPORTED_CERTIFICATE
:
808 return SSL3_AD_UNSUPPORTED_CERTIFICATE
;
809 case SSL_AD_CERTIFICATE_REVOKED
:
810 return SSL3_AD_CERTIFICATE_REVOKED
;
811 case SSL_AD_CERTIFICATE_EXPIRED
:
812 return SSL3_AD_CERTIFICATE_EXPIRED
;
813 case SSL_AD_CERTIFICATE_UNKNOWN
:
814 return SSL3_AD_CERTIFICATE_UNKNOWN
;
815 case SSL_AD_ILLEGAL_PARAMETER
:
816 return SSL3_AD_ILLEGAL_PARAMETER
;
817 case SSL_AD_UNKNOWN_CA
:
818 return TLS1_AD_UNKNOWN_CA
;
819 case SSL_AD_ACCESS_DENIED
:
820 return TLS1_AD_ACCESS_DENIED
;
821 case SSL_AD_DECODE_ERROR
:
822 return TLS1_AD_DECODE_ERROR
;
823 case SSL_AD_DECRYPT_ERROR
:
824 return TLS1_AD_DECRYPT_ERROR
;
825 case SSL_AD_EXPORT_RESTRICTION
:
826 return TLS1_AD_EXPORT_RESTRICTION
;
827 case SSL_AD_PROTOCOL_VERSION
:
828 return TLS1_AD_PROTOCOL_VERSION
;
829 case SSL_AD_INSUFFICIENT_SECURITY
:
830 return TLS1_AD_INSUFFICIENT_SECURITY
;
831 case SSL_AD_INTERNAL_ERROR
:
832 return TLS1_AD_INTERNAL_ERROR
;
833 case SSL_AD_USER_CANCELLED
:
834 return TLS1_AD_USER_CANCELLED
;
835 case SSL_AD_NO_RENEGOTIATION
:
836 return TLS1_AD_NO_RENEGOTIATION
;
837 case SSL_AD_UNSUPPORTED_EXTENSION
:
838 return TLS1_AD_UNSUPPORTED_EXTENSION
;
839 case SSL_AD_CERTIFICATE_UNOBTAINABLE
:
840 return TLS1_AD_CERTIFICATE_UNOBTAINABLE
;
841 case SSL_AD_UNRECOGNIZED_NAME
:
842 return TLS1_AD_UNRECOGNIZED_NAME
;
843 case SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE
:
844 return TLS1_AD_BAD_CERTIFICATE_STATUS_RESPONSE
;
845 case SSL_AD_BAD_CERTIFICATE_HASH_VALUE
:
846 return TLS1_AD_BAD_CERTIFICATE_HASH_VALUE
;
847 case SSL_AD_UNKNOWN_PSK_IDENTITY
:
848 return TLS1_AD_UNKNOWN_PSK_IDENTITY
;
849 case SSL_AD_INAPPROPRIATE_FALLBACK
:
850 return TLS1_AD_INAPPROPRIATE_FALLBACK
;
851 case SSL_AD_NO_APPLICATION_PROTOCOL
:
852 return TLS1_AD_NO_APPLICATION_PROTOCOL
;
853 case SSL_AD_CERTIFICATE_REQUIRED
:
854 return SSL_AD_HANDSHAKE_FAILURE
;