2 * Copyright 2016-2023 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 /* ASCII: "tls13 ", in hex for EBCDIC compatibility */
22 static const unsigned char label_prefix
[] = "\x74\x6C\x73\x31\x33\x20";
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.
30 * If |raise_error| is set, ERR_raise is called on failure.
32 int tls13_hkdf_expand_ex(OSSL_LIB_CTX
*libctx
, const char *propq
,
34 const unsigned char *secret
,
35 const unsigned char *label
, size_t labellen
,
36 const unsigned char *data
, size_t datalen
,
37 unsigned char *out
, size_t outlen
, int raise_error
)
39 EVP_KDF
*kdf
= EVP_KDF_fetch(libctx
, OSSL_KDF_NAME_TLS1_3_KDF
, propq
);
41 OSSL_PARAM params
[7], *p
= params
;
42 int mode
= EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
;
43 const char *mdname
= EVP_MD_get0_name(md
);
47 kctx
= EVP_KDF_CTX_new(kdf
);
52 if (labellen
> TLS13_MAX_LABEL_LEN
) {
55 * Probably we have been called from SSL_export_keying_material(),
56 * or SSL_export_keying_material_early().
58 ERR_raise(ERR_LIB_SSL
, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL
);
60 EVP_KDF_CTX_free(kctx
);
64 if ((ret
= EVP_MD_get_size(md
)) <= 0) {
65 EVP_KDF_CTX_free(kctx
);
67 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
70 hashlen
= (size_t)ret
;
72 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
73 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST
,
75 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY
,
76 (unsigned char *)secret
, hashlen
);
77 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX
,
78 (unsigned char *)label_prefix
,
79 sizeof(label_prefix
) - 1);
80 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL
,
81 (unsigned char *)label
, labellen
);
83 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA
,
84 (unsigned char *)data
,
86 *p
++ = OSSL_PARAM_construct_end();
88 ret
= EVP_KDF_derive(kctx
, out
, outlen
, params
) <= 0;
89 EVP_KDF_CTX_free(kctx
);
93 ERR_raise(ERR_LIB_SSL
, ERR_R_INTERNAL_ERROR
);
99 int tls13_hkdf_expand(SSL_CONNECTION
*s
, const EVP_MD
*md
,
100 const unsigned char *secret
,
101 const unsigned char *label
, size_t labellen
,
102 const unsigned char *data
, size_t datalen
,
103 unsigned char *out
, size_t outlen
, int fatal
)
106 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
108 ret
= tls13_hkdf_expand_ex(sctx
->libctx
, sctx
->propq
, md
,
109 secret
, label
, labellen
, data
, datalen
,
110 out
, outlen
, !fatal
);
111 if (ret
== 0 && fatal
)
112 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
118 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
119 * success 0 on failure.
121 int tls13_derive_key(SSL_CONNECTION
*s
, const EVP_MD
*md
,
122 const unsigned char *secret
,
123 unsigned char *key
, size_t keylen
)
125 /* ASCII: "key", in hex for EBCDIC compatibility */
126 static const unsigned char keylabel
[] = "\x6B\x65\x79";
128 return tls13_hkdf_expand(s
, md
, secret
, keylabel
, sizeof(keylabel
) - 1,
129 NULL
, 0, key
, keylen
, 1);
133 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
134 * success 0 on failure.
136 int tls13_derive_iv(SSL_CONNECTION
*s
, const EVP_MD
*md
,
137 const unsigned char *secret
,
138 unsigned char *iv
, size_t ivlen
)
140 /* ASCII: "iv", in hex for EBCDIC compatibility */
141 static const unsigned char ivlabel
[] = "\x69\x76";
143 return tls13_hkdf_expand(s
, md
, secret
, ivlabel
, sizeof(ivlabel
) - 1,
144 NULL
, 0, iv
, ivlen
, 1);
147 int tls13_derive_finishedkey(SSL_CONNECTION
*s
, const EVP_MD
*md
,
148 const unsigned char *secret
,
149 unsigned char *fin
, size_t finlen
)
151 /* ASCII: "finished", in hex for EBCDIC compatibility */
152 static const unsigned char finishedlabel
[] = "\x66\x69\x6E\x69\x73\x68\x65\x64";
154 return tls13_hkdf_expand(s
, md
, secret
, finishedlabel
,
155 sizeof(finishedlabel
) - 1, NULL
, 0, fin
, finlen
, 1);
159 * Given the previous secret |prevsecret| and a new input secret |insecret| of
160 * length |insecretlen|, generate a new secret and store it in the location
161 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
163 int tls13_generate_secret(SSL_CONNECTION
*s
, const EVP_MD
*md
,
164 const unsigned char *prevsecret
,
165 const unsigned char *insecret
,
167 unsigned char *outsecret
)
174 OSSL_PARAM params
[7], *p
= params
;
175 int mode
= EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
;
176 const char *mdname
= EVP_MD_get0_name(md
);
177 /* ASCII: "derived", in hex for EBCDIC compatibility */
178 static const char derived_secret_label
[] = "\x64\x65\x72\x69\x76\x65\x64";
179 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
181 kdf
= EVP_KDF_fetch(sctx
->libctx
, OSSL_KDF_NAME_TLS1_3_KDF
, sctx
->propq
);
182 kctx
= EVP_KDF_CTX_new(kdf
);
185 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
189 mdleni
= EVP_MD_get_size(md
);
190 /* Ensure cast to size_t is safe */
191 if (!ossl_assert(mdleni
>= 0)) {
192 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
193 EVP_KDF_CTX_free(kctx
);
196 mdlen
= (size_t)mdleni
;
198 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
199 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST
,
201 if (insecret
!= NULL
)
202 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY
,
203 (unsigned char *)insecret
,
205 if (prevsecret
!= NULL
)
206 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT
,
207 (unsigned char *)prevsecret
, mdlen
);
208 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX
,
209 (unsigned char *)label_prefix
,
210 sizeof(label_prefix
) - 1);
211 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL
,
212 (unsigned char *)derived_secret_label
,
213 sizeof(derived_secret_label
) - 1);
214 *p
++ = OSSL_PARAM_construct_end();
216 ret
= EVP_KDF_derive(kctx
, outsecret
, mdlen
, params
) <= 0;
219 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
221 EVP_KDF_CTX_free(kctx
);
226 * Given an input secret |insecret| of length |insecretlen| generate the
227 * handshake secret. This requires the early secret to already have been
228 * generated. Returns 1 on success 0 on failure.
230 int tls13_generate_handshake_secret(SSL_CONNECTION
*s
,
231 const unsigned char *insecret
,
234 /* Calls SSLfatal() if required */
235 return tls13_generate_secret(s
, ssl_handshake_md(s
), s
->early_secret
,
236 insecret
, insecretlen
,
237 (unsigned char *)&s
->handshake_secret
);
241 * Given the handshake secret |prev| of length |prevlen| generate the master
242 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
245 int tls13_generate_master_secret(SSL_CONNECTION
*s
, unsigned char *out
,
246 unsigned char *prev
, size_t prevlen
,
249 const EVP_MD
*md
= ssl_handshake_md(s
);
252 md_size
= EVP_MD_get_size(md
);
254 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
257 *secret_size
= (size_t)md_size
;
258 /* Calls SSLfatal() if required */
259 return tls13_generate_secret(s
, md
, prev
, NULL
, 0, out
);
263 * Generates the mac for the Finished message. Returns the length of the MAC or
266 size_t tls13_final_finish_mac(SSL_CONNECTION
*s
, const char *str
, size_t slen
,
269 const EVP_MD
*md
= ssl_handshake_md(s
);
270 const char *mdname
= EVP_MD_get0_name(md
);
271 unsigned char hash
[EVP_MAX_MD_SIZE
];
272 unsigned char finsecret
[EVP_MAX_MD_SIZE
];
273 unsigned char *key
= NULL
;
274 size_t len
= 0, hashlen
;
275 OSSL_PARAM params
[2], *p
= params
;
276 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
281 /* Safe to cast away const here since we're not "getting" any data */
282 if (sctx
->propq
!= NULL
)
283 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES
,
286 *p
= OSSL_PARAM_construct_end();
288 if (!ssl_handshake_hash(s
, hash
, sizeof(hash
), &hashlen
)) {
289 /* SSLfatal() already called */
293 if (str
== SSL_CONNECTION_GET_SSL(s
)->method
->ssl3_enc
->server_finished_label
) {
294 key
= s
->server_finished_secret
;
295 } else if (SSL_IS_FIRST_HANDSHAKE(s
)) {
296 key
= s
->client_finished_secret
;
298 if (!tls13_derive_finishedkey(s
, md
,
299 s
->client_app_traffic_secret
,
305 if (!EVP_Q_mac(sctx
->libctx
, "HMAC", sctx
->propq
, mdname
,
306 params
, key
, hashlen
, hash
, hashlen
,
307 /* outsize as per sizeof(peer_finish_md) */
308 out
, EVP_MAX_MD_SIZE
* 2, &len
)) {
309 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
314 OPENSSL_cleanse(finsecret
, sizeof(finsecret
));
319 * There isn't really a key block in TLSv1.3, but we still need this function
320 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
322 int tls13_setup_key_block(SSL_CONNECTION
*s
)
326 int mac_type
= NID_undef
;
327 size_t mac_secret_size
= 0;
329 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
330 if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s
), s
->session
, &c
, &hash
,
331 &mac_type
, &mac_secret_size
, NULL
, 0)) {
332 /* Error is already recorded */
333 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
337 ssl_evp_cipher_free(s
->s3
.tmp
.new_sym_enc
);
338 s
->s3
.tmp
.new_sym_enc
= c
;
339 ssl_evp_md_free(s
->s3
.tmp
.new_hash
);
340 s
->s3
.tmp
.new_hash
= hash
;
341 s
->s3
.tmp
.new_mac_pkey_type
= mac_type
;
342 s
->s3
.tmp
.new_mac_secret_size
= mac_secret_size
;
347 static int derive_secret_key_and_iv(SSL_CONNECTION
*s
, const EVP_MD
*md
,
348 const EVP_CIPHER
*ciph
,
350 const EVP_MD
*mac_md
,
351 const unsigned char *insecret
,
352 const unsigned char *hash
,
353 const unsigned char *label
,
354 size_t labellen
, unsigned char *secret
,
355 unsigned char *key
, size_t *keylen
,
356 unsigned char **iv
, size_t *ivlen
,
359 int hashleni
= EVP_MD_get_size(md
);
361 int mode
, mac_mdleni
;
363 /* Ensure cast to size_t is safe */
364 if (!ossl_assert(hashleni
>= 0)) {
365 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
368 hashlen
= (size_t)hashleni
;
370 if (!tls13_hkdf_expand(s
, md
, insecret
, label
, labellen
, hash
, hashlen
,
371 secret
, hashlen
, 1)) {
372 /* SSLfatal() already called */
376 /* if ciph is NULL cipher, then use new_hash to calculate keylen */
377 if (EVP_CIPHER_is_a(ciph
, "NULL")
379 && mac_type
== NID_hmac
) {
380 mac_mdleni
= EVP_MD_get_size(mac_md
);
382 if (mac_mdleni
< 0) {
383 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
386 *ivlen
= *taglen
= (size_t)mac_mdleni
;
387 *keylen
= s
->s3
.tmp
.new_mac_secret_size
;
390 *keylen
= EVP_CIPHER_get_key_length(ciph
);
392 mode
= EVP_CIPHER_get_mode(ciph
);
393 if (mode
== EVP_CIPH_CCM_MODE
) {
396 *ivlen
= EVP_CCM_TLS_IV_LEN
;
397 if (s
->s3
.tmp
.new_cipher
!= NULL
) {
398 algenc
= s
->s3
.tmp
.new_cipher
->algorithm_enc
;
399 } else if (s
->session
->cipher
!= NULL
) {
400 /* We've not selected a cipher yet - we must be doing early data */
401 algenc
= s
->session
->cipher
->algorithm_enc
;
402 } else if (s
->psksession
!= NULL
&& s
->psksession
->cipher
!= NULL
) {
403 /* We must be doing early data with out-of-band PSK */
404 algenc
= s
->psksession
->cipher
->algorithm_enc
;
406 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
409 if (algenc
& (SSL_AES128CCM8
| SSL_AES256CCM8
))
410 *taglen
= EVP_CCM8_TLS_TAG_LEN
;
412 *taglen
= EVP_CCM_TLS_TAG_LEN
;
416 if (mode
== EVP_CIPH_GCM_MODE
) {
417 *taglen
= EVP_GCM_TLS_TAG_LEN
;
419 /* CHACHA20P-POLY1305 */
420 *taglen
= EVP_CHACHAPOLY_TLS_TAG_LEN
;
422 iivlen
= EVP_CIPHER_get_iv_length(ciph
);
424 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
431 if (*ivlen
> EVP_MAX_IV_LENGTH
) {
432 *iv
= OPENSSL_malloc(*ivlen
);
434 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_MALLOC_FAILURE
);
439 if (!tls13_derive_key(s
, md
, secret
, key
, *keylen
)
440 || !tls13_derive_iv(s
, md
, secret
, *iv
, *ivlen
)) {
441 /* SSLfatal() already called */
448 int tls13_change_cipher_state(SSL_CONNECTION
*s
, int which
)
450 /* ASCII: "c e traffic", in hex for EBCDIC compatibility */
451 static const unsigned char client_early_traffic
[] = "\x63\x20\x65\x20\x74\x72\x61\x66\x66\x69\x63";
452 /* ASCII: "c hs traffic", in hex for EBCDIC compatibility */
453 static const unsigned char client_handshake_traffic
[] = "\x63\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
454 /* ASCII: "c ap traffic", in hex for EBCDIC compatibility */
455 static const unsigned char client_application_traffic
[] = "\x63\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
456 /* ASCII: "s hs traffic", in hex for EBCDIC compatibility */
457 static const unsigned char server_handshake_traffic
[] = "\x73\x20\x68\x73\x20\x74\x72\x61\x66\x66\x69\x63";
458 /* ASCII: "s ap traffic", in hex for EBCDIC compatibility */
459 static const unsigned char server_application_traffic
[] = "\x73\x20\x61\x70\x20\x74\x72\x61\x66\x66\x69\x63";
460 /* ASCII: "exp master", in hex for EBCDIC compatibility */
461 static const unsigned char exporter_master_secret
[] = "\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
462 /* ASCII: "res master", in hex for EBCDIC compatibility */
463 static const unsigned char resumption_master_secret
[] = "\x72\x65\x73\x20\x6D\x61\x73\x74\x65\x72";
464 /* ASCII: "e exp master", in hex for EBCDIC compatibility */
465 static const unsigned char early_exporter_master_secret
[] = "\x65\x20\x65\x78\x70\x20\x6D\x61\x73\x74\x65\x72";
466 unsigned char iv_intern
[EVP_MAX_IV_LENGTH
];
467 unsigned char *iv
= iv_intern
;
468 unsigned char key
[EVP_MAX_KEY_LENGTH
];
469 unsigned char secret
[EVP_MAX_MD_SIZE
];
470 unsigned char hashval
[EVP_MAX_MD_SIZE
];
471 unsigned char *hash
= hashval
;
472 unsigned char *insecret
;
473 unsigned char *finsecret
= NULL
;
474 const char *log_label
= NULL
;
475 int finsecretlen
= 0;
476 const unsigned char *label
;
477 size_t labellen
, hashlen
= 0;
479 const EVP_MD
*md
= NULL
, *mac_md
= NULL
;
480 const EVP_CIPHER
*cipher
= NULL
;
481 int mac_pkey_type
= NID_undef
;
482 SSL_CTX
*sctx
= SSL_CONNECTION_GET_CTX(s
);
483 size_t keylen
, ivlen
= EVP_MAX_IV_LENGTH
, taglen
;
485 int direction
= (which
& SSL3_CC_READ
) != 0 ? OSSL_RECORD_DIRECTION_READ
486 : OSSL_RECORD_DIRECTION_WRITE
;
488 if (((which
& SSL3_CC_CLIENT
) && (which
& SSL3_CC_WRITE
))
489 || ((which
& SSL3_CC_SERVER
) && (which
& SSL3_CC_READ
))) {
490 if ((which
& SSL3_CC_EARLY
) != 0) {
491 EVP_MD_CTX
*mdctx
= NULL
;
494 unsigned int hashlenui
;
495 const SSL_CIPHER
*sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
497 insecret
= s
->early_secret
;
498 label
= client_early_traffic
;
499 labellen
= sizeof(client_early_traffic
) - 1;
500 log_label
= CLIENT_EARLY_LABEL
;
502 handlen
= BIO_get_mem_data(s
->s3
.handshake_buffer
, &hdata
);
504 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_HANDSHAKE_LENGTH
);
508 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
509 && s
->max_early_data
> 0
510 && s
->session
->ext
.max_early_data
== 0) {
512 * If we are attempting to send early data, and we've decided to
513 * actually do it but max_early_data in s->session is 0 then we
514 * must be using an external PSK.
516 if (!ossl_assert(s
->psksession
!= NULL
517 && s
->max_early_data
==
518 s
->psksession
->ext
.max_early_data
)) {
519 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
522 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
524 if (sslcipher
== NULL
) {
525 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_R_BAD_PSK
);
530 * This ups the ref count on cipher so we better make sure we free
533 if (!ssl_cipher_get_evp_cipher(sctx
, sslcipher
, &cipher
)) {
534 /* Error is already recorded */
535 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
539 if (((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
) == 0)
540 && (!ssl_cipher_get_evp_md_mac(sctx
, sslcipher
, &mac_md
,
541 &mac_pkey_type
, NULL
))) {
542 SSLfatal_alert(s
, SSL_AD_INTERNAL_ERROR
);
547 * We need to calculate the handshake digest using the digest from
548 * the session. We haven't yet selected our ciphersuite so we can't
549 * use ssl_handshake_md().
551 mdctx
= EVP_MD_CTX_new();
553 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_EVP_LIB
);
557 md
= ssl_md(sctx
, sslcipher
->algorithm2
);
558 if (md
== NULL
|| !EVP_DigestInit_ex(mdctx
, md
, NULL
)
559 || !EVP_DigestUpdate(mdctx
, hdata
, handlen
)
560 || !EVP_DigestFinal_ex(mdctx
, hashval
, &hashlenui
)) {
561 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
562 EVP_MD_CTX_free(mdctx
);
566 EVP_MD_CTX_free(mdctx
);
568 if (!tls13_hkdf_expand(s
, md
, insecret
,
569 early_exporter_master_secret
,
570 sizeof(early_exporter_master_secret
) - 1,
572 s
->early_exporter_master_secret
, hashlen
,
574 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
578 if (!ssl_log_secret(s
, EARLY_EXPORTER_SECRET_LABEL
,
579 s
->early_exporter_master_secret
, hashlen
)) {
580 /* SSLfatal() already called */
583 } else if (which
& SSL3_CC_HANDSHAKE
) {
584 insecret
= s
->handshake_secret
;
585 finsecret
= s
->client_finished_secret
;
586 finsecretlen
= EVP_MD_get_size(ssl_handshake_md(s
));
587 if (finsecretlen
<= 0) {
588 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
591 label
= client_handshake_traffic
;
592 labellen
= sizeof(client_handshake_traffic
) - 1;
593 log_label
= CLIENT_HANDSHAKE_LABEL
;
595 * The handshake hash used for the server read/client write handshake
596 * traffic secret is the same as the hash for the server
597 * write/client read handshake traffic secret. However, if we
598 * processed early data then we delay changing the server
599 * read/client write cipher state until later, and the handshake
600 * hashes have moved on. Therefore we use the value saved earlier
601 * when we did the server write/client read change cipher state.
603 hash
= s
->handshake_traffic_hash
;
605 insecret
= s
->master_secret
;
606 label
= client_application_traffic
;
607 labellen
= sizeof(client_application_traffic
) - 1;
608 log_label
= CLIENT_APPLICATION_LABEL
;
610 * For this we only use the handshake hashes up until the server
611 * Finished hash. We do not include the client's Finished, which is
612 * what ssl_handshake_hash() would give us. Instead we use the
613 * previously saved value.
615 hash
= s
->server_finished_hash
;
618 /* Early data never applies to client-read/server-write */
619 if (which
& SSL3_CC_HANDSHAKE
) {
620 insecret
= s
->handshake_secret
;
621 finsecret
= s
->server_finished_secret
;
622 finsecretlen
= EVP_MD_get_size(ssl_handshake_md(s
));
623 if (finsecretlen
<= 0) {
624 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
627 label
= server_handshake_traffic
;
628 labellen
= sizeof(server_handshake_traffic
) - 1;
629 log_label
= SERVER_HANDSHAKE_LABEL
;
631 insecret
= s
->master_secret
;
632 label
= server_application_traffic
;
633 labellen
= sizeof(server_application_traffic
) - 1;
634 log_label
= SERVER_APPLICATION_LABEL
;
638 if ((which
& SSL3_CC_EARLY
) == 0) {
639 md
= ssl_handshake_md(s
);
640 cipher
= s
->s3
.tmp
.new_sym_enc
;
641 mac_md
= s
->s3
.tmp
.new_hash
;
642 mac_pkey_type
= s
->s3
.tmp
.new_mac_pkey_type
;
643 if (!ssl3_digest_cached_records(s
, 1)
644 || !ssl_handshake_hash(s
, hashval
, sizeof(hashval
), &hashlen
)) {
645 /* SSLfatal() already called */;
651 * Save the hash of handshakes up to now for use when we calculate the
652 * client application traffic secret
654 if (label
== server_application_traffic
)
655 memcpy(s
->server_finished_hash
, hashval
, hashlen
);
657 if (label
== server_handshake_traffic
)
658 memcpy(s
->handshake_traffic_hash
, hashval
, hashlen
);
660 if (label
== client_application_traffic
) {
662 * We also create the resumption master secret, but this time use the
663 * hash for the whole handshake including the Client Finished
665 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
666 resumption_master_secret
,
667 sizeof(resumption_master_secret
) - 1,
668 hashval
, hashlen
, s
->resumption_master_secret
,
670 /* SSLfatal() already called */
675 /* check whether cipher is known */
676 if (!ossl_assert(cipher
!= NULL
))
679 if (!derive_secret_key_and_iv(s
, md
, cipher
, mac_pkey_type
, mac_md
,
680 insecret
, hash
, label
, labellen
, secret
, key
,
681 &keylen
, &iv
, &ivlen
, &taglen
)) {
682 /* SSLfatal() already called */
686 if (label
== server_application_traffic
) {
687 memcpy(s
->server_app_traffic_secret
, secret
, hashlen
);
688 /* Now we create the exporter master secret */
689 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
690 exporter_master_secret
,
691 sizeof(exporter_master_secret
) - 1,
692 hash
, hashlen
, s
->exporter_master_secret
,
694 /* SSLfatal() already called */
698 if (!ssl_log_secret(s
, EXPORTER_SECRET_LABEL
, s
->exporter_master_secret
,
700 /* SSLfatal() already called */
703 } else if (label
== client_application_traffic
)
704 memcpy(s
->client_app_traffic_secret
, secret
, hashlen
);
706 if (!ssl_log_secret(s
, log_label
, secret
, hashlen
)) {
707 /* SSLfatal() already called */
711 if (finsecret
!= NULL
712 && !tls13_derive_finishedkey(s
, ssl_handshake_md(s
), secret
,
713 finsecret
, (size_t)finsecretlen
)) {
714 /* SSLfatal() already called */
718 if ((which
& SSL3_CC_WRITE
) != 0) {
719 if (!s
->server
&& label
== client_early_traffic
)
720 s
->rlayer
.wrlmethod
->set_plain_alerts(s
->rlayer
.wrl
, 1);
722 s
->rlayer
.wrlmethod
->set_plain_alerts(s
->rlayer
.wrl
, 0);
725 level
= (which
& SSL3_CC_EARLY
) != 0
726 ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
727 : ((which
&SSL3_CC_HANDSHAKE
) != 0
728 ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
729 : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
);
731 if (!ssl_set_new_record_layer(s
, s
->version
,
733 level
, secret
, hashlen
, key
, keylen
, iv
,
734 ivlen
, NULL
, 0, cipher
, taglen
,
735 mac_pkey_type
, mac_md
, NULL
, md
)) {
736 /* SSLfatal already called */
742 if ((which
& SSL3_CC_EARLY
) != 0) {
743 /* We up-refed this so now we need to down ref */
744 if ((EVP_CIPHER_flags(cipher
) & EVP_CIPH_FLAG_AEAD_CIPHER
) == 0)
745 ssl_evp_md_free(mac_md
);
746 ssl_evp_cipher_free(cipher
);
748 OPENSSL_cleanse(key
, sizeof(key
));
749 OPENSSL_cleanse(secret
, sizeof(secret
));
755 int tls13_update_key(SSL_CONNECTION
*s
, int sending
)
757 /* ASCII: "traffic upd", in hex for EBCDIC compatibility */
758 static const unsigned char application_traffic
[] = "\x74\x72\x61\x66\x66\x69\x63\x20\x75\x70\x64";
759 const EVP_MD
*md
= ssl_handshake_md(s
);
761 unsigned char key
[EVP_MAX_KEY_LENGTH
];
762 unsigned char *insecret
;
763 unsigned char secret
[EVP_MAX_MD_SIZE
];
765 size_t keylen
, ivlen
, taglen
;
767 int direction
= sending
? OSSL_RECORD_DIRECTION_WRITE
768 : OSSL_RECORD_DIRECTION_READ
;
769 unsigned char iv_intern
[EVP_MAX_IV_LENGTH
];
770 unsigned char *iv
= iv_intern
;
772 if ((l
= EVP_MD_get_size(md
)) <= 0) {
773 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, ERR_R_INTERNAL_ERROR
);
778 if (s
->server
== sending
)
779 insecret
= s
->server_app_traffic_secret
;
781 insecret
= s
->client_app_traffic_secret
;
783 if (!derive_secret_key_and_iv(s
, md
,
784 s
->s3
.tmp
.new_sym_enc
,
785 s
->s3
.tmp
.new_mac_pkey_type
, s
->s3
.tmp
.new_hash
,
788 sizeof(application_traffic
) - 1, secret
, key
,
789 &keylen
, &iv
, &ivlen
, &taglen
)) {
790 /* SSLfatal() already called */
794 memcpy(insecret
, secret
, hashlen
);
796 if (!ssl_set_new_record_layer(s
, s
->version
,
798 OSSL_RECORD_PROTECTION_LEVEL_APPLICATION
,
799 insecret
, hashlen
, key
, keylen
, iv
, ivlen
, NULL
, 0,
800 s
->s3
.tmp
.new_sym_enc
, taglen
, NID_undef
, NULL
,
802 /* SSLfatal already called */
806 /* Call Key log on successful traffic secret update */
807 log_label
= s
->server
== sending
? SERVER_APPLICATION_N_LABEL
: CLIENT_APPLICATION_N_LABEL
;
808 if (!ssl_log_secret(s
, log_label
, secret
, hashlen
)) {
809 /* SSLfatal() already called */
814 OPENSSL_cleanse(key
, sizeof(key
));
815 OPENSSL_cleanse(secret
, sizeof(secret
));
821 int tls13_alert_code(int code
)
823 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
824 if (code
== SSL_AD_MISSING_EXTENSION
|| code
== SSL_AD_CERTIFICATE_REQUIRED
)
827 return tls1_alert_code(code
);
830 int tls13_export_keying_material(SSL_CONNECTION
*s
,
831 unsigned char *out
, size_t olen
,
832 const char *label
, size_t llen
,
833 const unsigned char *context
,
834 size_t contextlen
, int use_context
)
836 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
837 /* ASCII: "exporter", in hex for EBCDIC compatibility */
838 static const unsigned char exporterlabel
[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
839 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
840 const EVP_MD
*md
= ssl_handshake_md(s
);
841 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
842 unsigned int hashsize
, datalen
;
845 if (ctx
== NULL
|| md
== NULL
|| !ossl_statem_export_allowed(s
))
851 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
852 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
853 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
854 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
855 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
856 || !tls13_hkdf_expand(s
, md
, s
->exporter_master_secret
,
857 (const unsigned char *)label
, llen
,
858 data
, datalen
, exportsecret
, hashsize
, 0)
859 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
860 sizeof(exporterlabel
) - 1, hash
, hashsize
,
866 EVP_MD_CTX_free(ctx
);
870 int tls13_export_keying_material_early(SSL_CONNECTION
*s
,
871 unsigned char *out
, size_t olen
,
872 const char *label
, size_t llen
,
873 const unsigned char *context
,
876 /* ASCII: "exporter", in hex for EBCDIC compatibility */
877 static const unsigned char exporterlabel
[] = "\x65\x78\x70\x6F\x72\x74\x65\x72";
878 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
879 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
881 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
882 unsigned int hashsize
, datalen
;
884 const SSL_CIPHER
*sslcipher
;
886 if (ctx
== NULL
|| !ossl_statem_export_early_allowed(s
))
889 if (!s
->server
&& s
->max_early_data
> 0
890 && s
->session
->ext
.max_early_data
== 0)
891 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
893 sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
895 md
= ssl_md(SSL_CONNECTION_GET_CTX(s
), sslcipher
->algorithm2
);
898 * Calculate the hash value and store it in |data|. The reason why
899 * the empty string is used is that the definition of TLS-Exporter
902 * TLS-Exporter(label, context_value, key_length) =
903 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
904 * "exporter", Hash(context_value), key_length)
906 * Derive-Secret(Secret, Label, Messages) =
907 * HKDF-Expand-Label(Secret, Label,
908 * Transcript-Hash(Messages), Hash.length)
910 * Here Transcript-Hash is the cipher suite hash algorithm.
913 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
914 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
915 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
916 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
917 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
918 || !tls13_hkdf_expand(s
, md
, s
->early_exporter_master_secret
,
919 (const unsigned char *)label
, llen
,
920 data
, datalen
, exportsecret
, hashsize
, 0)
921 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
922 sizeof(exporterlabel
) - 1, hash
, hashsize
,
928 EVP_MD_CTX_free(ctx
);