2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "ssl_local.h"
12 #include "internal/ktls.h"
13 #include "record/record_local.h"
14 #include "internal/cryptlib.h"
15 #include <openssl/evp.h>
16 #include <openssl/kdf.h>
17 #include <openssl/core_names.h>
19 #define TLS13_MAX_LABEL_LEN 249
21 /* Always filled with zeros */
22 static const unsigned char default_zeros
[EVP_MAX_MD_SIZE
];
25 * Given a |secret|; a |label| of length |labellen|; and |data| of length
26 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
27 * secret |outlen| bytes long and store it in the location pointed to be |out|.
28 * The |data| value may be zero length. Any errors will be treated as fatal if
29 * |fatal| is set. Returns 1 on success 0 on failure.
31 int tls13_hkdf_expand(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
32 const unsigned char *label
, size_t labellen
,
33 const unsigned char *data
, size_t datalen
,
34 unsigned char *out
, size_t outlen
, int fatal
)
37 static const unsigned char label_prefix
[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 };
39 static const unsigned char label_prefix
[] = "tls13 ";
41 EVP_KDF
*kdf
= EVP_KDF_fetch(s
->ctx
->libctx
, OSSL_KDF_NAME_HKDF
,
44 OSSL_PARAM params
[5], *p
= params
;
45 int mode
= EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
;
46 const char *mdname
= EVP_MD_get0_name(md
);
51 * 2 bytes for length of derived secret + 1 byte for length of combined
52 * prefix and label + bytes for the label itself + 1 byte length of hash
53 * + bytes for the hash itself
55 unsigned char hkdflabel
[sizeof(uint16_t) + sizeof(uint8_t)
56 + (sizeof(label_prefix
) - 1) + TLS13_MAX_LABEL_LEN
57 + 1 + EVP_MAX_MD_SIZE
];
60 kctx
= EVP_KDF_CTX_new(kdf
);
65 if (labellen
> TLS13_MAX_LABEL_LEN
) {
67 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
70 * Probably we have been called from SSL_export_keying_material(),
71 * or SSL_export_keying_material_early().
73 ERR_raise(ERR_LIB_SSL
, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL
);
75 EVP_KDF_CTX_free(kctx
);
79 hashlen
= EVP_MD_get_size(md
);
81 if (!WPACKET_init_static_len(&pkt
, hkdflabel
, sizeof(hkdflabel
), 0)
82 || !WPACKET_put_bytes_u16(&pkt
, outlen
)
83 || !WPACKET_start_sub_packet_u8(&pkt
)
84 || !WPACKET_memcpy(&pkt
, label_prefix
, sizeof(label_prefix
) - 1)
85 || !WPACKET_memcpy(&pkt
, label
, labellen
)
86 || !WPACKET_close(&pkt
)
87 || !WPACKET_sub_memcpy_u8(&pkt
, data
, (data
== NULL
) ? 0 : datalen
)
88 || !WPACKET_get_total_written(&pkt
, &hkdflabellen
)
89 || !WPACKET_finish(&pkt
)) {
90 EVP_KDF_CTX_free(kctx
);
91 WPACKET_cleanup(&pkt
);
93 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
95 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
99 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
100 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST
,
102 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY
,
103 (unsigned char *)secret
, hashlen
);
104 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO
,
105 hkdflabel
, hkdflabellen
);
106 *p
++ = OSSL_PARAM_construct_end();
108 ret
= EVP_KDF_derive(kctx
, out
, outlen
, params
) <= 0;
110 EVP_KDF_CTX_free(kctx
);
114 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
116 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
123 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
124 * success 0 on failure.
126 int tls13_derive_key(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
127 unsigned char *key
, size_t keylen
)
129 #ifdef CHARSET_EBCDIC
130 static const unsigned char keylabel
[] ={ 0x6B, 0x65, 0x79, 0x00 };
132 static const unsigned char keylabel
[] = "key";
135 return tls13_hkdf_expand(s
, md
, secret
, keylabel
, sizeof(keylabel
) - 1,
136 NULL
, 0, key
, keylen
, 1);
140 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
141 * success 0 on failure.
143 int tls13_derive_iv(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
144 unsigned char *iv
, size_t ivlen
)
146 #ifdef CHARSET_EBCDIC
147 static const unsigned char ivlabel
[] = { 0x69, 0x76, 0x00 };
149 static const unsigned char ivlabel
[] = "iv";
152 return tls13_hkdf_expand(s
, md
, secret
, ivlabel
, sizeof(ivlabel
) - 1,
153 NULL
, 0, iv
, ivlen
, 1);
156 int tls13_derive_finishedkey(SSL
*s
, const EVP_MD
*md
,
157 const unsigned char *secret
,
158 unsigned char *fin
, size_t finlen
)
160 #ifdef CHARSET_EBCDIC
161 static const unsigned char finishedlabel
[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
163 static const unsigned char finishedlabel
[] = "finished";
166 return tls13_hkdf_expand(s
, md
, secret
, finishedlabel
,
167 sizeof(finishedlabel
) - 1, NULL
, 0, fin
, finlen
, 1);
171 * Given the previous secret |prevsecret| and a new input secret |insecret| of
172 * length |insecretlen|, generate a new secret and store it in the location
173 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
175 int tls13_generate_secret(SSL
*s
, const EVP_MD
*md
,
176 const unsigned char *prevsecret
,
177 const unsigned char *insecret
,
179 unsigned char *outsecret
)
181 size_t mdlen
, prevsecretlen
;
186 OSSL_PARAM params
[5], *p
= params
;
187 int mode
= EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
;
188 const char *mdname
= EVP_MD_get0_name(md
);
189 #ifdef CHARSET_EBCDIC
190 static const char derived_secret_label
[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
192 static const char derived_secret_label
[] = "derived";
194 unsigned char preextractsec
[EVP_MAX_MD_SIZE
];
196 kdf
= EVP_KDF_fetch(s
->ctx
->libctx
, OSSL_KDF_NAME_HKDF
, s
->ctx
->propq
);
197 kctx
= EVP_KDF_CTX_new(kdf
);
200 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
204 mdleni
= EVP_MD_get_size(md
);
205 /* Ensure cast to size_t is safe */
206 if (!ossl_assert(mdleni
>= 0)) {
207 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
208 EVP_KDF_CTX_free(kctx
);
211 mdlen
= (size_t)mdleni
;
213 if (insecret
== NULL
) {
214 insecret
= default_zeros
;
217 if (prevsecret
== NULL
) {
218 prevsecret
= default_zeros
;
221 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
222 unsigned char hash
[EVP_MAX_MD_SIZE
];
224 /* The pre-extract derive step uses a hash of no messages */
226 || EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0
227 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
228 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
229 EVP_MD_CTX_free(mctx
);
230 EVP_KDF_CTX_free(kctx
);
233 EVP_MD_CTX_free(mctx
);
235 /* Generate the pre-extract secret */
236 if (!tls13_hkdf_expand(s
, md
, prevsecret
,
237 (unsigned char *)derived_secret_label
,
238 sizeof(derived_secret_label
) - 1, hash
, mdlen
,
239 preextractsec
, mdlen
, 1)) {
240 /* SSLfatal() already called */
241 EVP_KDF_CTX_free(kctx
);
245 prevsecret
= preextractsec
;
246 prevsecretlen
= mdlen
;
249 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
250 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST
,
252 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY
,
253 (unsigned char *)insecret
,
255 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT
,
256 (unsigned char *)prevsecret
,
258 *p
++ = OSSL_PARAM_construct_end();
260 ret
= EVP_KDF_derive(kctx
, outsecret
, mdlen
, params
) <= 0;
263 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
265 EVP_KDF_CTX_free(kctx
);
266 if (prevsecret
== preextractsec
)
267 OPENSSL_cleanse(preextractsec
, mdlen
);
272 * Given an input secret |insecret| of length |insecretlen| generate the
273 * handshake secret. This requires the early secret to already have been
274 * generated. Returns 1 on success 0 on failure.
276 int tls13_generate_handshake_secret(SSL
*s
, const unsigned char *insecret
,
279 /* Calls SSLfatal() if required */
280 return tls13_generate_secret(s
, ssl_handshake_md(s
), s
->early_secret
,
281 insecret
, insecretlen
,
282 (unsigned char *)&s
->handshake_secret
);
286 * Given the handshake secret |prev| of length |prevlen| generate the master
287 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
290 int tls13_generate_master_secret(SSL
*s
, unsigned char *out
,
291 unsigned char *prev
, size_t prevlen
,
294 const EVP_MD
*md
= ssl_handshake_md(s
);
296 *secret_size
= EVP_MD_get_size(md
);
297 /* Calls SSLfatal() if required */
298 return tls13_generate_secret(s
, md
, prev
, NULL
, 0, out
);
302 * Generates the mac for the Finished message. Returns the length of the MAC or
305 size_t tls13_final_finish_mac(SSL
*s
, const char *str
, size_t slen
,
308 const char *mdname
= EVP_MD_get0_name(ssl_handshake_md(s
));
309 unsigned char hash
[EVP_MAX_MD_SIZE
];
310 unsigned char finsecret
[EVP_MAX_MD_SIZE
];
311 unsigned char *key
= NULL
;
312 unsigned int len
= 0;
313 size_t hashlen
, ret
= 0;
314 OSSL_PARAM params
[2], *p
= params
;
316 /* Safe to cast away const here since we're not "getting" any data */
317 if (s
->ctx
->propq
!= NULL
)
318 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES
,
319 (char *)s
->ctx
->propq
,
321 *p
= OSSL_PARAM_construct_end();
323 if (!ssl_handshake_hash(s
, hash
, sizeof(hash
), &hashlen
)) {
324 /* SSLfatal() already called */
328 if (str
== s
->method
->ssl3_enc
->server_finished_label
) {
329 key
= s
->server_finished_secret
;
330 } else if (SSL_IS_FIRST_HANDSHAKE(s
)) {
331 key
= s
->client_finished_secret
;
333 if (!tls13_derive_finishedkey(s
, ssl_handshake_md(s
),
334 s
->client_app_traffic_secret
,
340 if (!EVP_Q_mac(s
->ctx
->libctx
, "HMAC", s
->ctx
->propq
, mdname
,
341 params
, key
, hashlen
, hash
, hashlen
,
342 /* outsize as per sizeof(peer_finish_md) */
343 out
, EVP_MAX_MD_SIZE
* 2, &len
)) {
344 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
350 OPENSSL_cleanse(finsecret
, sizeof(finsecret
));
355 * There isn't really a key block in TLSv1.3, but we still need this function
356 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
358 int tls13_setup_key_block(SSL
*s
)
363 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
364 if (!ssl_cipher_get_evp(s
->ctx
, s
->session
, &c
, &hash
, NULL
, NULL
, NULL
,
366 /* Error is already recorded */
367 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
371 ssl_evp_cipher_free(s
->s3
.tmp
.new_sym_enc
);
372 s
->s3
.tmp
.new_sym_enc
= c
;
373 ssl_evp_md_free(s
->s3
.tmp
.new_hash
);
374 s
->s3
.tmp
.new_hash
= hash
;
379 static int derive_secret_key_and_iv(SSL
*s
, int sending
, const EVP_MD
*md
,
380 const EVP_CIPHER
*ciph
,
381 const unsigned char *insecret
,
382 const unsigned char *hash
,
383 const unsigned char *label
,
384 size_t labellen
, unsigned char *secret
,
385 unsigned char *key
, unsigned char *iv
,
386 EVP_CIPHER_CTX
*ciph_ctx
)
388 size_t ivlen
, keylen
, taglen
;
389 int hashleni
= EVP_MD_get_size(md
);
392 /* Ensure cast to size_t is safe */
393 if (!ossl_assert(hashleni
>= 0)) {
394 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
397 hashlen
= (size_t)hashleni
;
399 if (!tls13_hkdf_expand(s
, md
, insecret
, label
, labellen
, hash
, hashlen
,
400 secret
, hashlen
, 1)) {
401 /* SSLfatal() already called */
405 /* TODO(size_t): convert me */
406 keylen
= EVP_CIPHER_get_key_length(ciph
);
407 if (EVP_CIPHER_get_mode(ciph
) == EVP_CIPH_CCM_MODE
) {
410 ivlen
= EVP_CCM_TLS_IV_LEN
;
411 if (s
->s3
.tmp
.new_cipher
!= NULL
) {
412 algenc
= s
->s3
.tmp
.new_cipher
->algorithm_enc
;
413 } else if (s
->session
->cipher
!= NULL
) {
414 /* We've not selected a cipher yet - we must be doing early data */
415 algenc
= s
->session
->cipher
->algorithm_enc
;
416 } else if (s
->psksession
!= NULL
&& s
->psksession
->cipher
!= NULL
) {
417 /* We must be doing early data with out-of-band PSK */
418 algenc
= s
->psksession
->cipher
->algorithm_enc
;
420 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
423 if (algenc
& (SSL_AES128CCM8
| SSL_AES256CCM8
))
424 taglen
= EVP_CCM8_TLS_TAG_LEN
;
426 taglen
= EVP_CCM_TLS_TAG_LEN
;
428 ivlen
= EVP_CIPHER_get_iv_length(ciph
);
432 if (!tls13_derive_key(s
, md
, secret
, key
, keylen
)
433 || !tls13_derive_iv(s
, md
, secret
, iv
, ivlen
)) {
434 /* SSLfatal() already called */
438 if (EVP_CipherInit_ex(ciph_ctx
, ciph
, NULL
, NULL
, NULL
, sending
) <= 0
439 || !EVP_CIPHER_CTX_ctrl(ciph_ctx
, EVP_CTRL_AEAD_SET_IVLEN
, ivlen
, NULL
)
440 || (taglen
!= 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx
, EVP_CTRL_AEAD_SET_TAG
,
442 || EVP_CipherInit_ex(ciph_ctx
, NULL
, NULL
, key
, NULL
, -1) <= 0) {
443 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
450 int tls13_change_cipher_state(SSL
*s
, int which
)
452 #ifdef CHARSET_EBCDIC
453 static const unsigned char client_early_traffic
[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
454 static const unsigned char client_handshake_traffic
[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
455 static const unsigned char client_application_traffic
[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
456 static const unsigned char server_handshake_traffic
[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
457 static const unsigned char server_application_traffic
[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
458 static const unsigned char exporter_master_secret
[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
459 static const unsigned char resumption_master_secret
[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
460 static const unsigned char early_exporter_master_secret
[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
462 static const unsigned char client_early_traffic
[] = "c e traffic";
463 static const unsigned char client_handshake_traffic
[] = "c hs traffic";
464 static const unsigned char client_application_traffic
[] = "c ap traffic";
465 static const unsigned char server_handshake_traffic
[] = "s hs traffic";
466 static const unsigned char server_application_traffic
[] = "s ap traffic";
467 static const unsigned char exporter_master_secret
[] = "exp master";
468 static const unsigned char resumption_master_secret
[] = "res master";
469 static const unsigned char early_exporter_master_secret
[] = "e exp master";
472 unsigned char key
[EVP_MAX_KEY_LENGTH
];
473 unsigned char secret
[EVP_MAX_MD_SIZE
];
474 unsigned char hashval
[EVP_MAX_MD_SIZE
];
475 unsigned char *hash
= hashval
;
476 unsigned char *insecret
;
477 unsigned char *finsecret
= NULL
;
478 const char *log_label
= NULL
;
479 EVP_CIPHER_CTX
*ciph_ctx
;
480 size_t finsecretlen
= 0;
481 const unsigned char *label
;
482 size_t labellen
, hashlen
= 0;
484 const EVP_MD
*md
= NULL
;
485 const EVP_CIPHER
*cipher
= NULL
;
486 #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
487 ktls_crypto_info_t crypto_info
;
491 if (which
& SSL3_CC_READ
) {
492 if (s
->enc_read_ctx
!= NULL
) {
493 EVP_CIPHER_CTX_reset(s
->enc_read_ctx
);
495 s
->enc_read_ctx
= EVP_CIPHER_CTX_new();
496 if (s
->enc_read_ctx
== NULL
) {
497 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
501 ciph_ctx
= s
->enc_read_ctx
;
504 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
506 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
507 if (s
->enc_write_ctx
!= NULL
) {
508 EVP_CIPHER_CTX_reset(s
->enc_write_ctx
);
510 s
->enc_write_ctx
= EVP_CIPHER_CTX_new();
511 if (s
->enc_write_ctx
== NULL
) {
512 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
516 ciph_ctx
= s
->enc_write_ctx
;
519 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
522 if (((which
& SSL3_CC_CLIENT
) && (which
& SSL3_CC_WRITE
))
523 || ((which
& SSL3_CC_SERVER
) && (which
& SSL3_CC_READ
))) {
524 if (which
& SSL3_CC_EARLY
) {
525 EVP_MD_CTX
*mdctx
= NULL
;
528 unsigned int hashlenui
;
529 const SSL_CIPHER
*sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
531 insecret
= s
->early_secret
;
532 label
= client_early_traffic
;
533 labellen
= sizeof(client_early_traffic
) - 1;
534 log_label
= CLIENT_EARLY_LABEL
;
536 handlen
= BIO_get_mem_data(s
->s3
.handshake_buffer
, &hdata
);
538 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_LENGTH
);
542 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
543 && s
->max_early_data
> 0
544 && s
->session
->ext
.max_early_data
== 0) {
546 * If we are attempting to send early data, and we've decided to
547 * actually do it but max_early_data in s->session is 0 then we
548 * must be using an external PSK.
550 if (!ossl_assert(s
->psksession
!= NULL
551 && s
->max_early_data
==
552 s
->psksession
->ext
.max_early_data
)) {
553 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
556 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
558 if (sslcipher
== NULL
) {
559 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_PSK
);
564 * We need to calculate the handshake digest using the digest from
565 * the session. We haven't yet selected our ciphersuite so we can't
566 * use ssl_handshake_md().
568 mdctx
= EVP_MD_CTX_new();
570 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
575 * This ups the ref count on cipher so we better make sure we free
578 if (!ssl_cipher_get_evp_cipher(s
->ctx
, sslcipher
, &cipher
)) {
579 /* Error is already recorded */
580 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
581 EVP_MD_CTX_free(mdctx
);
585 md
= ssl_md(s
->ctx
, sslcipher
->algorithm2
);
586 if (md
== NULL
|| !EVP_DigestInit_ex(mdctx
, md
, NULL
)
587 || !EVP_DigestUpdate(mdctx
, hdata
, handlen
)
588 || !EVP_DigestFinal_ex(mdctx
, hashval
, &hashlenui
)) {
589 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
590 EVP_MD_CTX_free(mdctx
);
594 EVP_MD_CTX_free(mdctx
);
596 if (!tls13_hkdf_expand(s
, md
, insecret
,
597 early_exporter_master_secret
,
598 sizeof(early_exporter_master_secret
) - 1,
600 s
->early_exporter_master_secret
, hashlen
,
602 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
606 if (!ssl_log_secret(s
, EARLY_EXPORTER_SECRET_LABEL
,
607 s
->early_exporter_master_secret
, hashlen
)) {
608 /* SSLfatal() already called */
611 } else if (which
& SSL3_CC_HANDSHAKE
) {
612 insecret
= s
->handshake_secret
;
613 finsecret
= s
->client_finished_secret
;
614 finsecretlen
= EVP_MD_get_size(ssl_handshake_md(s
));
615 label
= client_handshake_traffic
;
616 labellen
= sizeof(client_handshake_traffic
) - 1;
617 log_label
= CLIENT_HANDSHAKE_LABEL
;
619 * The handshake hash used for the server read/client write handshake
620 * traffic secret is the same as the hash for the server
621 * write/client read handshake traffic secret. However, if we
622 * processed early data then we delay changing the server
623 * read/client write cipher state until later, and the handshake
624 * hashes have moved on. Therefore we use the value saved earlier
625 * when we did the server write/client read change cipher state.
627 hash
= s
->handshake_traffic_hash
;
629 insecret
= s
->master_secret
;
630 label
= client_application_traffic
;
631 labellen
= sizeof(client_application_traffic
) - 1;
632 log_label
= CLIENT_APPLICATION_LABEL
;
634 * For this we only use the handshake hashes up until the server
635 * Finished hash. We do not include the client's Finished, which is
636 * what ssl_handshake_hash() would give us. Instead we use the
637 * previously saved value.
639 hash
= s
->server_finished_hash
;
642 /* Early data never applies to client-read/server-write */
643 if (which
& SSL3_CC_HANDSHAKE
) {
644 insecret
= s
->handshake_secret
;
645 finsecret
= s
->server_finished_secret
;
646 finsecretlen
= EVP_MD_get_size(ssl_handshake_md(s
));
647 label
= server_handshake_traffic
;
648 labellen
= sizeof(server_handshake_traffic
) - 1;
649 log_label
= SERVER_HANDSHAKE_LABEL
;
651 insecret
= s
->master_secret
;
652 label
= server_application_traffic
;
653 labellen
= sizeof(server_application_traffic
) - 1;
654 log_label
= SERVER_APPLICATION_LABEL
;
658 if (!(which
& SSL3_CC_EARLY
)) {
659 md
= ssl_handshake_md(s
);
660 cipher
= s
->s3
.tmp
.new_sym_enc
;
661 if (!ssl3_digest_cached_records(s
, 1)
662 || !ssl_handshake_hash(s
, hashval
, sizeof(hashval
), &hashlen
)) {
663 /* SSLfatal() already called */;
669 * Save the hash of handshakes up to now for use when we calculate the
670 * client application traffic secret
672 if (label
== server_application_traffic
)
673 memcpy(s
->server_finished_hash
, hashval
, hashlen
);
675 if (label
== server_handshake_traffic
)
676 memcpy(s
->handshake_traffic_hash
, hashval
, hashlen
);
678 if (label
== client_application_traffic
) {
680 * We also create the resumption master secret, but this time use the
681 * hash for the whole handshake including the Client Finished
683 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
684 resumption_master_secret
,
685 sizeof(resumption_master_secret
) - 1,
686 hashval
, hashlen
, s
->resumption_master_secret
,
688 /* SSLfatal() already called */
693 /* check whether cipher is known */
694 if(!ossl_assert(cipher
!= NULL
))
697 if (!derive_secret_key_and_iv(s
, which
& SSL3_CC_WRITE
, md
, cipher
,
698 insecret
, hash
, label
, labellen
, secret
, key
,
700 /* SSLfatal() already called */
704 if (label
== server_application_traffic
) {
705 memcpy(s
->server_app_traffic_secret
, secret
, hashlen
);
706 /* Now we create the exporter master secret */
707 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
708 exporter_master_secret
,
709 sizeof(exporter_master_secret
) - 1,
710 hash
, hashlen
, s
->exporter_master_secret
,
712 /* SSLfatal() already called */
716 if (!ssl_log_secret(s
, EXPORTER_SECRET_LABEL
, s
->exporter_master_secret
,
718 /* SSLfatal() already called */
721 } else if (label
== client_application_traffic
)
722 memcpy(s
->client_app_traffic_secret
, secret
, hashlen
);
724 if (!ssl_log_secret(s
, log_label
, secret
, hashlen
)) {
725 /* SSLfatal() already called */
729 if (finsecret
!= NULL
730 && !tls13_derive_finishedkey(s
, ssl_handshake_md(s
), secret
,
731 finsecret
, finsecretlen
)) {
732 /* SSLfatal() already called */
736 if (!s
->server
&& label
== client_early_traffic
)
737 s
->statem
.enc_write_state
= ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
;
739 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
740 #ifndef OPENSSL_NO_KTLS
741 # if defined(OPENSSL_KTLS_TLS13)
742 if (!(which
& SSL3_CC_WRITE
)
743 || !(which
& SSL3_CC_APPLICATION
)
744 || (s
->options
& SSL_OP_ENABLE_KTLS
) == 0)
747 /* ktls supports only the maximum fragment size */
748 if (ssl_get_max_send_fragment(s
) != SSL3_RT_MAX_PLAIN_LENGTH
)
751 /* ktls does not support record padding */
752 if (s
->record_padding_cb
!= NULL
)
755 /* check that cipher is supported */
756 if (!ktls_check_supported_cipher(s
, cipher
, ciph_ctx
))
761 if (!ossl_assert(bio
!= NULL
)) {
762 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
766 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
767 if (BIO_flush(bio
) <= 0)
770 /* configure kernel crypto structure */
771 if (!ktls_configure_crypto(s
, cipher
, ciph_ctx
,
772 RECORD_LAYER_get_write_sequence(&s
->rlayer
),
773 &crypto_info
, NULL
, iv
, key
, NULL
, 0))
776 /* ktls works with user provided buffers directly */
777 if (BIO_set_ktls(bio
, &crypto_info
, which
& SSL3_CC_WRITE
))
778 ssl3_release_write_buffer(s
);
784 if ((which
& SSL3_CC_EARLY
) != 0) {
785 /* We up-refed this so now we need to down ref */
786 ssl_evp_cipher_free(cipher
);
788 OPENSSL_cleanse(key
, sizeof(key
));
789 OPENSSL_cleanse(secret
, sizeof(secret
));
793 int tls13_update_key(SSL
*s
, int sending
)
795 #ifdef CHARSET_EBCDIC
796 static const unsigned char application_traffic
[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
798 static const unsigned char application_traffic
[] = "traffic upd";
800 const EVP_MD
*md
= ssl_handshake_md(s
);
801 size_t hashlen
= EVP_MD_get_size(md
);
802 unsigned char key
[EVP_MAX_KEY_LENGTH
];
803 unsigned char *insecret
, *iv
;
804 unsigned char secret
[EVP_MAX_MD_SIZE
];
805 EVP_CIPHER_CTX
*ciph_ctx
;
808 if (s
->server
== sending
)
809 insecret
= s
->server_app_traffic_secret
;
811 insecret
= s
->client_app_traffic_secret
;
814 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
816 ciph_ctx
= s
->enc_write_ctx
;
817 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
820 ciph_ctx
= s
->enc_read_ctx
;
821 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
824 if (!derive_secret_key_and_iv(s
, sending
, ssl_handshake_md(s
),
825 s
->s3
.tmp
.new_sym_enc
, insecret
, NULL
,
827 sizeof(application_traffic
) - 1, secret
, key
,
829 /* SSLfatal() already called */
833 memcpy(insecret
, secret
, hashlen
);
835 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
838 OPENSSL_cleanse(key
, sizeof(key
));
839 OPENSSL_cleanse(secret
, sizeof(secret
));
843 int tls13_alert_code(int code
)
845 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
846 if (code
== SSL_AD_MISSING_EXTENSION
|| code
== SSL_AD_CERTIFICATE_REQUIRED
)
849 return tls1_alert_code(code
);
852 int tls13_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
853 const char *label
, size_t llen
,
854 const unsigned char *context
,
855 size_t contextlen
, int use_context
)
857 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
858 #ifdef CHARSET_EBCDIC
859 static const unsigned char exporterlabel
[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
861 static const unsigned char exporterlabel
[] = "exporter";
863 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
864 const EVP_MD
*md
= ssl_handshake_md(s
);
865 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
866 unsigned int hashsize
, datalen
;
869 if (ctx
== NULL
|| !ossl_statem_export_allowed(s
))
875 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
876 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
877 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
878 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
879 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
880 || !tls13_hkdf_expand(s
, md
, s
->exporter_master_secret
,
881 (const unsigned char *)label
, llen
,
882 data
, datalen
, exportsecret
, hashsize
, 0)
883 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
884 sizeof(exporterlabel
) - 1, hash
, hashsize
,
890 EVP_MD_CTX_free(ctx
);
894 int tls13_export_keying_material_early(SSL
*s
, unsigned char *out
, size_t olen
,
895 const char *label
, size_t llen
,
896 const unsigned char *context
,
899 #ifdef CHARSET_EBCDIC
900 static const unsigned char exporterlabel
[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
902 static const unsigned char exporterlabel
[] = "exporter";
904 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
905 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
907 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
908 unsigned int hashsize
, datalen
;
910 const SSL_CIPHER
*sslcipher
;
912 if (ctx
== NULL
|| !ossl_statem_export_early_allowed(s
))
915 if (!s
->server
&& s
->max_early_data
> 0
916 && s
->session
->ext
.max_early_data
== 0)
917 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
919 sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
921 md
= ssl_md(s
->ctx
, sslcipher
->algorithm2
);
924 * Calculate the hash value and store it in |data|. The reason why
925 * the empty string is used is that the definition of TLS-Exporter
928 * TLS-Exporter(label, context_value, key_length) =
929 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
930 * "exporter", Hash(context_value), key_length)
932 * Derive-Secret(Secret, Label, Messages) =
933 * HKDF-Expand-Label(Secret, Label,
934 * Transcript-Hash(Messages), Hash.length)
936 * Here Transcript-Hash is the cipher suite hash algorithm.
938 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
939 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
940 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
941 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
942 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
943 || !tls13_hkdf_expand(s
, md
, s
->early_exporter_master_secret
,
944 (const unsigned char *)label
, llen
,
945 data
, datalen
, exportsecret
, hashsize
, 0)
946 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
947 sizeof(exporterlabel
) - 1, hash
, hashsize
,
953 EVP_MD_CTX_free(ctx
);