2 * Copyright 2016-2018 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/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
15 #include <openssl/core_names.h>
17 #define TLS13_MAX_LABEL_LEN 249
19 /* Always filled with zeros */
20 static const unsigned char default_zeros
[EVP_MAX_MD_SIZE
];
23 * Given a |secret|; a |label| of length |labellen|; and |data| of length
24 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
25 * secret |outlen| bytes long and store it in the location pointed to be |out|.
26 * The |data| value may be zero length. Any errors will be treated as fatal if
27 * |fatal| is set. Returns 1 on success 0 on failure.
29 int tls13_hkdf_expand(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
30 const unsigned char *label
, size_t labellen
,
31 const unsigned char *data
, size_t datalen
,
32 unsigned char *out
, size_t outlen
, int fatal
)
35 static const unsigned char label_prefix
[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 };
37 static const unsigned char label_prefix
[] = "tls13 ";
39 EVP_KDF
*kdf
= EVP_KDF_fetch(s
->ctx
->libctx
, OSSL_KDF_NAME_HKDF
,
42 OSSL_PARAM params
[5], *p
= params
;
43 int mode
= EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
;
44 const char *mdname
= EVP_MD_name(md
);
49 * 2 bytes for length of derived secret + 1 byte for length of combined
50 * prefix and label + bytes for the label itself + 1 byte length of hash
51 * + bytes for the hash itself
53 unsigned char hkdflabel
[sizeof(uint16_t) + sizeof(uint8_t)
54 + (sizeof(label_prefix
) - 1) + TLS13_MAX_LABEL_LEN
55 + 1 + EVP_MAX_MD_SIZE
];
58 kctx
= EVP_KDF_CTX_new(kdf
);
63 if (labellen
> TLS13_MAX_LABEL_LEN
) {
65 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_HKDF_EXPAND
,
66 ERR_R_INTERNAL_ERROR
);
69 * Probably we have been called from SSL_export_keying_material(),
70 * or SSL_export_keying_material_early().
72 SSLerr(SSL_F_TLS13_HKDF_EXPAND
, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL
);
74 EVP_KDF_CTX_free(kctx
);
78 hashlen
= EVP_MD_size(md
);
80 if (!WPACKET_init_static_len(&pkt
, hkdflabel
, sizeof(hkdflabel
), 0)
81 || !WPACKET_put_bytes_u16(&pkt
, outlen
)
82 || !WPACKET_start_sub_packet_u8(&pkt
)
83 || !WPACKET_memcpy(&pkt
, label_prefix
, sizeof(label_prefix
) - 1)
84 || !WPACKET_memcpy(&pkt
, label
, labellen
)
85 || !WPACKET_close(&pkt
)
86 || !WPACKET_sub_memcpy_u8(&pkt
, data
, (data
== NULL
) ? 0 : datalen
)
87 || !WPACKET_get_total_written(&pkt
, &hkdflabellen
)
88 || !WPACKET_finish(&pkt
)) {
89 EVP_KDF_CTX_free(kctx
);
90 WPACKET_cleanup(&pkt
);
92 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_HKDF_EXPAND
,
93 ERR_R_INTERNAL_ERROR
);
95 SSLerr(SSL_F_TLS13_HKDF_EXPAND
, 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_CTX_set_params(kctx
, params
) <= 0
109 || EVP_KDF_derive(kctx
, out
, outlen
) <= 0;
111 EVP_KDF_CTX_free(kctx
);
115 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_HKDF_EXPAND
,
116 ERR_R_INTERNAL_ERROR
);
118 SSLerr(SSL_F_TLS13_HKDF_EXPAND
, ERR_R_INTERNAL_ERROR
);
125 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
126 * success 0 on failure.
128 int tls13_derive_key(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
129 unsigned char *key
, size_t keylen
)
131 #ifdef CHARSET_EBCDIC
132 static const unsigned char keylabel
[] ={ 0x6B, 0x65, 0x79, 0x00 };
134 static const unsigned char keylabel
[] = "key";
137 return tls13_hkdf_expand(s
, md
, secret
, keylabel
, sizeof(keylabel
) - 1,
138 NULL
, 0, key
, keylen
, 1);
142 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
143 * success 0 on failure.
145 int tls13_derive_iv(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
146 unsigned char *iv
, size_t ivlen
)
148 #ifdef CHARSET_EBCDIC
149 static const unsigned char ivlabel
[] = { 0x69, 0x76, 0x00 };
151 static const unsigned char ivlabel
[] = "iv";
154 return tls13_hkdf_expand(s
, md
, secret
, ivlabel
, sizeof(ivlabel
) - 1,
155 NULL
, 0, iv
, ivlen
, 1);
158 int tls13_derive_finishedkey(SSL
*s
, const EVP_MD
*md
,
159 const unsigned char *secret
,
160 unsigned char *fin
, size_t finlen
)
162 #ifdef CHARSET_EBCDIC
163 static const unsigned char finishedlabel
[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
165 static const unsigned char finishedlabel
[] = "finished";
168 return tls13_hkdf_expand(s
, md
, secret
, finishedlabel
,
169 sizeof(finishedlabel
) - 1, NULL
, 0, fin
, finlen
, 1);
173 * Given the previous secret |prevsecret| and a new input secret |insecret| of
174 * length |insecretlen|, generate a new secret and store it in the location
175 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
177 int tls13_generate_secret(SSL
*s
, const EVP_MD
*md
,
178 const unsigned char *prevsecret
,
179 const unsigned char *insecret
,
181 unsigned char *outsecret
)
183 size_t mdlen
, prevsecretlen
;
188 OSSL_PARAM params
[5], *p
= params
;
189 int mode
= EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
;
190 const char *mdname
= EVP_MD_name(md
);
191 #ifdef CHARSET_EBCDIC
192 static const char derived_secret_label
[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
194 static const char derived_secret_label
[] = "derived";
196 unsigned char preextractsec
[EVP_MAX_MD_SIZE
];
198 kdf
= EVP_KDF_fetch(s
->ctx
->libctx
, OSSL_KDF_NAME_HKDF
, s
->ctx
->propq
);
199 kctx
= EVP_KDF_CTX_new(kdf
);
202 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
203 ERR_R_INTERNAL_ERROR
);
207 mdleni
= EVP_MD_size(md
);
208 /* Ensure cast to size_t is safe */
209 if (!ossl_assert(mdleni
>= 0)) {
210 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
211 ERR_R_INTERNAL_ERROR
);
212 EVP_KDF_CTX_free(kctx
);
215 mdlen
= (size_t)mdleni
;
217 if (insecret
== NULL
) {
218 insecret
= default_zeros
;
221 if (prevsecret
== NULL
) {
222 prevsecret
= default_zeros
;
225 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
226 unsigned char hash
[EVP_MAX_MD_SIZE
];
228 /* The pre-extract derive step uses a hash of no messages */
230 || EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0
231 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
232 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
233 ERR_R_INTERNAL_ERROR
);
234 EVP_MD_CTX_free(mctx
);
235 EVP_KDF_CTX_free(kctx
);
238 EVP_MD_CTX_free(mctx
);
240 /* Generate the pre-extract secret */
241 if (!tls13_hkdf_expand(s
, md
, prevsecret
,
242 (unsigned char *)derived_secret_label
,
243 sizeof(derived_secret_label
) - 1, hash
, mdlen
,
244 preextractsec
, mdlen
, 1)) {
245 /* SSLfatal() already called */
246 EVP_KDF_CTX_free(kctx
);
250 prevsecret
= preextractsec
;
251 prevsecretlen
= mdlen
;
254 *p
++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE
, &mode
);
255 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST
,
257 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY
,
258 (unsigned char *)insecret
,
260 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT
,
261 (unsigned char *)prevsecret
,
263 *p
++ = OSSL_PARAM_construct_end();
265 ret
= EVP_KDF_CTX_set_params(kctx
, params
) <= 0
266 || EVP_KDF_derive(kctx
, outsecret
, mdlen
) <= 0;
269 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
270 ERR_R_INTERNAL_ERROR
);
272 EVP_KDF_CTX_free(kctx
);
273 if (prevsecret
== preextractsec
)
274 OPENSSL_cleanse(preextractsec
, mdlen
);
279 * Given an input secret |insecret| of length |insecretlen| generate the
280 * handshake secret. This requires the early secret to already have been
281 * generated. Returns 1 on success 0 on failure.
283 int tls13_generate_handshake_secret(SSL
*s
, const unsigned char *insecret
,
286 /* Calls SSLfatal() if required */
287 return tls13_generate_secret(s
, ssl_handshake_md(s
), s
->early_secret
,
288 insecret
, insecretlen
,
289 (unsigned char *)&s
->handshake_secret
);
293 * Given the handshake secret |prev| of length |prevlen| generate the master
294 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
297 int tls13_generate_master_secret(SSL
*s
, unsigned char *out
,
298 unsigned char *prev
, size_t prevlen
,
301 const EVP_MD
*md
= ssl_handshake_md(s
);
303 *secret_size
= EVP_MD_size(md
);
304 /* Calls SSLfatal() if required */
305 return tls13_generate_secret(s
, md
, prev
, NULL
, 0, out
);
309 * Generates the mac for the Finished message. Returns the length of the MAC or
312 size_t tls13_final_finish_mac(SSL
*s
, const char *str
, size_t slen
,
315 const char *mdname
= EVP_MD_name(ssl_handshake_md(s
));
316 EVP_MAC
*hmac
= EVP_MAC_fetch(s
->ctx
->libctx
, "HMAC", s
->ctx
->propq
);
317 unsigned char hash
[EVP_MAX_MD_SIZE
];
318 unsigned char finsecret
[EVP_MAX_MD_SIZE
];
319 size_t hashlen
, ret
= 0;
320 EVP_MAC_CTX
*ctx
= NULL
;
321 OSSL_PARAM params
[4], *p
= params
;
324 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_FINAL_FINISH_MAC
,
325 ERR_R_INTERNAL_ERROR
);
329 /* Safe to cast away const here since we're not "getting" any data */
330 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST
,
332 if (s
->ctx
->propq
!= NULL
)
333 *p
++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES
,
334 (char *)s
->ctx
->propq
,
337 if (!ssl_handshake_hash(s
, hash
, sizeof(hash
), &hashlen
)) {
338 /* SSLfatal() already called */
342 if (str
== s
->method
->ssl3_enc
->server_finished_label
) {
343 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
344 s
->server_finished_secret
,
346 } else if (SSL_IS_FIRST_HANDSHAKE(s
)) {
347 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
,
348 s
->client_finished_secret
,
351 if (!tls13_derive_finishedkey(s
, ssl_handshake_md(s
),
352 s
->client_app_traffic_secret
,
356 *p
++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY
, finsecret
,
359 *p
++ = OSSL_PARAM_construct_end();
361 ctx
= EVP_MAC_CTX_new(hmac
);
363 || !EVP_MAC_CTX_set_params(ctx
, params
)
364 || !EVP_MAC_init(ctx
)
365 || !EVP_MAC_update(ctx
, hash
, hashlen
)
366 /* outsize as per sizeof(peer_finish_md) */
367 || !EVP_MAC_final(ctx
, out
, &hashlen
, EVP_MAX_MD_SIZE
* 2)) {
368 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_FINAL_FINISH_MAC
,
369 ERR_R_INTERNAL_ERROR
);
375 OPENSSL_cleanse(finsecret
, sizeof(finsecret
));
376 EVP_MAC_CTX_free(ctx
);
382 * There isn't really a key block in TLSv1.3, but we still need this function
383 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
385 int tls13_setup_key_block(SSL
*s
)
390 s
->session
->cipher
= s
->s3
.tmp
.new_cipher
;
391 if (!ssl_cipher_get_evp(s
->ctx
, s
->session
, &c
, &hash
, NULL
, NULL
, NULL
,
393 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_SETUP_KEY_BLOCK
,
394 SSL_R_CIPHER_OR_HASH_UNAVAILABLE
);
398 ssl_evp_cipher_free(s
->s3
.tmp
.new_sym_enc
);
399 s
->s3
.tmp
.new_sym_enc
= c
;
400 ssl_evp_md_free(s
->s3
.tmp
.new_hash
);
401 s
->s3
.tmp
.new_hash
= hash
;
406 static int derive_secret_key_and_iv(SSL
*s
, int sending
, const EVP_MD
*md
,
407 const EVP_CIPHER
*ciph
,
408 const unsigned char *insecret
,
409 const unsigned char *hash
,
410 const unsigned char *label
,
411 size_t labellen
, unsigned char *secret
,
412 unsigned char *iv
, EVP_CIPHER_CTX
*ciph_ctx
)
414 unsigned char key
[EVP_MAX_KEY_LENGTH
];
415 size_t ivlen
, keylen
, taglen
;
416 int hashleni
= EVP_MD_size(md
);
419 /* Ensure cast to size_t is safe */
420 if (!ossl_assert(hashleni
>= 0)) {
421 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DERIVE_SECRET_KEY_AND_IV
,
425 hashlen
= (size_t)hashleni
;
427 if (!tls13_hkdf_expand(s
, md
, insecret
, label
, labellen
, hash
, hashlen
,
428 secret
, hashlen
, 1)) {
429 /* SSLfatal() already called */
433 /* TODO(size_t): convert me */
434 keylen
= EVP_CIPHER_key_length(ciph
);
435 if (EVP_CIPHER_mode(ciph
) == EVP_CIPH_CCM_MODE
) {
438 ivlen
= EVP_CCM_TLS_IV_LEN
;
439 if (s
->s3
.tmp
.new_cipher
== NULL
) {
440 /* We've not selected a cipher yet - we must be doing early data */
441 algenc
= s
->session
->cipher
->algorithm_enc
;
443 algenc
= s
->s3
.tmp
.new_cipher
->algorithm_enc
;
445 if (algenc
& (SSL_AES128CCM8
| SSL_AES256CCM8
))
446 taglen
= EVP_CCM8_TLS_TAG_LEN
;
448 taglen
= EVP_CCM_TLS_TAG_LEN
;
450 ivlen
= EVP_CIPHER_iv_length(ciph
);
454 if (!tls13_derive_key(s
, md
, secret
, key
, keylen
)
455 || !tls13_derive_iv(s
, md
, secret
, iv
, ivlen
)) {
456 /* SSLfatal() already called */
460 if (EVP_CipherInit_ex(ciph_ctx
, ciph
, NULL
, NULL
, NULL
, sending
) <= 0
461 || !EVP_CIPHER_CTX_ctrl(ciph_ctx
, EVP_CTRL_AEAD_SET_IVLEN
, ivlen
, NULL
)
462 || (taglen
!= 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx
, EVP_CTRL_AEAD_SET_TAG
,
464 || EVP_CipherInit_ex(ciph_ctx
, NULL
, NULL
, key
, NULL
, -1) <= 0) {
465 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DERIVE_SECRET_KEY_AND_IV
,
472 OPENSSL_cleanse(key
, sizeof(key
));
476 int tls13_change_cipher_state(SSL
*s
, int which
)
478 #ifdef CHARSET_EBCDIC
479 static const unsigned char client_early_traffic
[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
480 static const unsigned char client_handshake_traffic
[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
481 static const unsigned char client_application_traffic
[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
482 static const unsigned char server_handshake_traffic
[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
483 static const unsigned char server_application_traffic
[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
484 static const unsigned char exporter_master_secret
[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
485 static const unsigned char resumption_master_secret
[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
486 static const unsigned char early_exporter_master_secret
[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
488 static const unsigned char client_early_traffic
[] = "c e traffic";
489 static const unsigned char client_handshake_traffic
[] = "c hs traffic";
490 static const unsigned char client_application_traffic
[] = "c ap traffic";
491 static const unsigned char server_handshake_traffic
[] = "s hs traffic";
492 static const unsigned char server_application_traffic
[] = "s ap traffic";
493 static const unsigned char exporter_master_secret
[] = "exp master";
494 static const unsigned char resumption_master_secret
[] = "res master";
495 static const unsigned char early_exporter_master_secret
[] = "e exp master";
498 unsigned char secret
[EVP_MAX_MD_SIZE
];
499 unsigned char hashval
[EVP_MAX_MD_SIZE
];
500 unsigned char *hash
= hashval
;
501 unsigned char *insecret
;
502 unsigned char *finsecret
= NULL
;
503 const char *log_label
= NULL
;
504 EVP_CIPHER_CTX
*ciph_ctx
;
505 size_t finsecretlen
= 0;
506 const unsigned char *label
;
507 size_t labellen
, hashlen
= 0;
509 const EVP_MD
*md
= NULL
;
510 const EVP_CIPHER
*cipher
= NULL
;
512 if (which
& SSL3_CC_READ
) {
513 if (s
->enc_read_ctx
!= NULL
) {
514 EVP_CIPHER_CTX_reset(s
->enc_read_ctx
);
516 s
->enc_read_ctx
= EVP_CIPHER_CTX_new();
517 if (s
->enc_read_ctx
== NULL
) {
518 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
519 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
523 ciph_ctx
= s
->enc_read_ctx
;
526 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
528 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
529 if (s
->enc_write_ctx
!= NULL
) {
530 EVP_CIPHER_CTX_reset(s
->enc_write_ctx
);
532 s
->enc_write_ctx
= EVP_CIPHER_CTX_new();
533 if (s
->enc_write_ctx
== NULL
) {
534 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
535 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
539 ciph_ctx
= s
->enc_write_ctx
;
542 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
545 if (((which
& SSL3_CC_CLIENT
) && (which
& SSL3_CC_WRITE
))
546 || ((which
& SSL3_CC_SERVER
) && (which
& SSL3_CC_READ
))) {
547 if (which
& SSL3_CC_EARLY
) {
548 EVP_MD_CTX
*mdctx
= NULL
;
551 unsigned int hashlenui
;
552 const SSL_CIPHER
*sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
554 insecret
= s
->early_secret
;
555 label
= client_early_traffic
;
556 labellen
= sizeof(client_early_traffic
) - 1;
557 log_label
= CLIENT_EARLY_LABEL
;
559 handlen
= BIO_get_mem_data(s
->s3
.handshake_buffer
, &hdata
);
561 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
562 SSL_F_TLS13_CHANGE_CIPHER_STATE
,
563 SSL_R_BAD_HANDSHAKE_LENGTH
);
567 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
568 && s
->max_early_data
> 0
569 && s
->session
->ext
.max_early_data
== 0) {
571 * If we are attempting to send early data, and we've decided to
572 * actually do it but max_early_data in s->session is 0 then we
573 * must be using an external PSK.
575 if (!ossl_assert(s
->psksession
!= NULL
576 && s
->max_early_data
==
577 s
->psksession
->ext
.max_early_data
)) {
578 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
579 SSL_F_TLS13_CHANGE_CIPHER_STATE
,
580 ERR_R_INTERNAL_ERROR
);
583 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
585 if (sslcipher
== NULL
) {
586 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
587 SSL_F_TLS13_CHANGE_CIPHER_STATE
, SSL_R_BAD_PSK
);
592 * We need to calculate the handshake digest using the digest from
593 * the session. We haven't yet selected our ciphersuite so we can't
594 * use ssl_handshake_md().
596 mdctx
= EVP_MD_CTX_new();
598 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
599 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
602 cipher
= EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher
));
603 md
= ssl_md(s
->ctx
, sslcipher
->algorithm2
);
604 if (md
== NULL
|| !EVP_DigestInit_ex(mdctx
, md
, NULL
)
605 || !EVP_DigestUpdate(mdctx
, hdata
, handlen
)
606 || !EVP_DigestFinal_ex(mdctx
, hashval
, &hashlenui
)) {
607 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
608 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
609 EVP_MD_CTX_free(mdctx
);
613 EVP_MD_CTX_free(mdctx
);
615 if (!tls13_hkdf_expand(s
, md
, insecret
,
616 early_exporter_master_secret
,
617 sizeof(early_exporter_master_secret
) - 1,
619 s
->early_exporter_master_secret
, hashlen
,
621 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
622 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
626 if (!ssl_log_secret(s
, EARLY_EXPORTER_SECRET_LABEL
,
627 s
->early_exporter_master_secret
, hashlen
)) {
628 /* SSLfatal() already called */
631 } else if (which
& SSL3_CC_HANDSHAKE
) {
632 insecret
= s
->handshake_secret
;
633 finsecret
= s
->client_finished_secret
;
634 finsecretlen
= EVP_MD_size(ssl_handshake_md(s
));
635 label
= client_handshake_traffic
;
636 labellen
= sizeof(client_handshake_traffic
) - 1;
637 log_label
= CLIENT_HANDSHAKE_LABEL
;
639 * The handshake hash used for the server read/client write handshake
640 * traffic secret is the same as the hash for the server
641 * write/client read handshake traffic secret. However, if we
642 * processed early data then we delay changing the server
643 * read/client write cipher state until later, and the handshake
644 * hashes have moved on. Therefore we use the value saved earlier
645 * when we did the server write/client read change cipher state.
647 hash
= s
->handshake_traffic_hash
;
649 insecret
= s
->master_secret
;
650 label
= client_application_traffic
;
651 labellen
= sizeof(client_application_traffic
) - 1;
652 log_label
= CLIENT_APPLICATION_LABEL
;
654 * For this we only use the handshake hashes up until the server
655 * Finished hash. We do not include the client's Finished, which is
656 * what ssl_handshake_hash() would give us. Instead we use the
657 * previously saved value.
659 hash
= s
->server_finished_hash
;
662 /* Early data never applies to client-read/server-write */
663 if (which
& SSL3_CC_HANDSHAKE
) {
664 insecret
= s
->handshake_secret
;
665 finsecret
= s
->server_finished_secret
;
666 finsecretlen
= EVP_MD_size(ssl_handshake_md(s
));
667 label
= server_handshake_traffic
;
668 labellen
= sizeof(server_handshake_traffic
) - 1;
669 log_label
= SERVER_HANDSHAKE_LABEL
;
671 insecret
= s
->master_secret
;
672 label
= server_application_traffic
;
673 labellen
= sizeof(server_application_traffic
) - 1;
674 log_label
= SERVER_APPLICATION_LABEL
;
678 if (!(which
& SSL3_CC_EARLY
)) {
679 md
= ssl_handshake_md(s
);
680 cipher
= s
->s3
.tmp
.new_sym_enc
;
681 if (!ssl3_digest_cached_records(s
, 1)
682 || !ssl_handshake_hash(s
, hashval
, sizeof(hashval
), &hashlen
)) {
683 /* SSLfatal() already called */;
689 * Save the hash of handshakes up to now for use when we calculate the
690 * client application traffic secret
692 if (label
== server_application_traffic
)
693 memcpy(s
->server_finished_hash
, hashval
, hashlen
);
695 if (label
== server_handshake_traffic
)
696 memcpy(s
->handshake_traffic_hash
, hashval
, hashlen
);
698 if (label
== client_application_traffic
) {
700 * We also create the resumption master secret, but this time use the
701 * hash for the whole handshake including the Client Finished
703 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
704 resumption_master_secret
,
705 sizeof(resumption_master_secret
) - 1,
706 hashval
, hashlen
, s
->resumption_master_secret
,
708 /* SSLfatal() already called */
713 if (!derive_secret_key_and_iv(s
, which
& SSL3_CC_WRITE
, md
, cipher
,
714 insecret
, hash
, label
, labellen
, secret
, iv
,
716 /* SSLfatal() already called */
720 if (label
== server_application_traffic
) {
721 memcpy(s
->server_app_traffic_secret
, secret
, hashlen
);
722 /* Now we create the exporter master secret */
723 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
724 exporter_master_secret
,
725 sizeof(exporter_master_secret
) - 1,
726 hash
, hashlen
, s
->exporter_master_secret
,
728 /* SSLfatal() already called */
732 if (!ssl_log_secret(s
, EXPORTER_SECRET_LABEL
, s
->exporter_master_secret
,
734 /* SSLfatal() already called */
737 } else if (label
== client_application_traffic
)
738 memcpy(s
->client_app_traffic_secret
, secret
, hashlen
);
740 if (!ssl_log_secret(s
, log_label
, secret
, hashlen
)) {
741 /* SSLfatal() already called */
745 if (finsecret
!= NULL
746 && !tls13_derive_finishedkey(s
, ssl_handshake_md(s
), secret
,
747 finsecret
, finsecretlen
)) {
748 /* SSLfatal() already called */
752 if (!s
->server
&& label
== client_early_traffic
)
753 s
->statem
.enc_write_state
= ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
;
755 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
758 OPENSSL_cleanse(secret
, sizeof(secret
));
762 int tls13_update_key(SSL
*s
, int sending
)
764 #ifdef CHARSET_EBCDIC
765 static const unsigned char application_traffic
[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
767 static const unsigned char application_traffic
[] = "traffic upd";
769 const EVP_MD
*md
= ssl_handshake_md(s
);
770 size_t hashlen
= EVP_MD_size(md
);
771 unsigned char *insecret
, *iv
;
772 unsigned char secret
[EVP_MAX_MD_SIZE
];
773 EVP_CIPHER_CTX
*ciph_ctx
;
776 if (s
->server
== sending
)
777 insecret
= s
->server_app_traffic_secret
;
779 insecret
= s
->client_app_traffic_secret
;
782 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
784 ciph_ctx
= s
->enc_write_ctx
;
785 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
788 ciph_ctx
= s
->enc_read_ctx
;
789 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
792 if (!derive_secret_key_and_iv(s
, sending
, ssl_handshake_md(s
),
793 s
->s3
.tmp
.new_sym_enc
, insecret
, NULL
,
795 sizeof(application_traffic
) - 1, secret
, iv
,
797 /* SSLfatal() already called */
801 memcpy(insecret
, secret
, hashlen
);
803 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
806 OPENSSL_cleanse(secret
, sizeof(secret
));
810 int tls13_alert_code(int code
)
812 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
813 if (code
== SSL_AD_MISSING_EXTENSION
|| code
== SSL_AD_CERTIFICATE_REQUIRED
)
816 return tls1_alert_code(code
);
819 int tls13_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
820 const char *label
, size_t llen
,
821 const unsigned char *context
,
822 size_t contextlen
, int use_context
)
824 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
825 #ifdef CHARSET_EBCDIC
826 static const unsigned char exporterlabel
[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
828 static const unsigned char exporterlabel
[] = "exporter";
830 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
831 const EVP_MD
*md
= ssl_handshake_md(s
);
832 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
833 unsigned int hashsize
, datalen
;
836 if (ctx
== NULL
|| !ossl_statem_export_allowed(s
))
842 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
843 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
844 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
845 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
846 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
847 || !tls13_hkdf_expand(s
, md
, s
->exporter_master_secret
,
848 (const unsigned char *)label
, llen
,
849 data
, datalen
, exportsecret
, hashsize
, 0)
850 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
851 sizeof(exporterlabel
) - 1, hash
, hashsize
,
857 EVP_MD_CTX_free(ctx
);
861 int tls13_export_keying_material_early(SSL
*s
, unsigned char *out
, size_t olen
,
862 const char *label
, size_t llen
,
863 const unsigned char *context
,
866 #ifdef CHARSET_EBCDIC
867 static const unsigned char exporterlabel
[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
869 static const unsigned char exporterlabel
[] = "exporter";
871 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
872 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
874 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
875 unsigned int hashsize
, datalen
;
877 const SSL_CIPHER
*sslcipher
;
879 if (ctx
== NULL
|| !ossl_statem_export_early_allowed(s
))
882 if (!s
->server
&& s
->max_early_data
> 0
883 && s
->session
->ext
.max_early_data
== 0)
884 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
886 sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
888 md
= ssl_md(s
->ctx
, sslcipher
->algorithm2
);
891 * Calculate the hash value and store it in |data|. The reason why
892 * the empty string is used is that the definition of TLS-Exporter
895 * TLS-Exporter(label, context_value, key_length) =
896 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
897 * "exporter", Hash(context_value), key_length)
899 * Derive-Secret(Secret, Label, Messages) =
900 * HKDF-Expand-Label(Secret, Label,
901 * Transcript-Hash(Messages), Hash.length)
903 * Here Transcript-Hash is the cipher suite hash algorithm.
905 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
906 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
907 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
908 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
909 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
910 || !tls13_hkdf_expand(s
, md
, s
->early_exporter_master_secret
,
911 (const unsigned char *)label
, llen
,
912 data
, datalen
, exportsecret
, hashsize
, 0)
913 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
914 sizeof(exporterlabel
) - 1, hash
, hashsize
,
920 EVP_MD_CTX_free(ctx
);