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(NULL, OSSL_KDF_NAME_HKDF, NULL);
41 OSSL_PARAM params[5], *p = params;
42 int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
43 const char *mdname = EVP_MD_name(md);
48 * 2 bytes for length of derived secret + 1 byte for length of combined
49 * prefix and label + bytes for the label itself + 1 byte length of hash
50 * + bytes for the hash itself
52 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
53 + (sizeof(label_prefix) - 1) + TLS13_MAX_LABEL_LEN
54 + 1 + EVP_MAX_MD_SIZE];
57 kctx = EVP_KDF_CTX_new(kdf);
62 if (labellen > TLS13_MAX_LABEL_LEN) {
64 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
65 ERR_R_INTERNAL_ERROR);
68 * Probably we have been called from SSL_export_keying_material(),
69 * or SSL_export_keying_material_early().
71 SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
73 EVP_KDF_CTX_free(kctx);
77 hashlen = EVP_MD_size(md);
79 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
80 || !WPACKET_put_bytes_u16(&pkt, outlen)
81 || !WPACKET_start_sub_packet_u8(&pkt)
82 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
83 || !WPACKET_memcpy(&pkt, label, labellen)
84 || !WPACKET_close(&pkt)
85 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
86 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
87 || !WPACKET_finish(&pkt)) {
88 EVP_KDF_CTX_free(kctx);
89 WPACKET_cleanup(&pkt);
91 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
92 ERR_R_INTERNAL_ERROR);
94 SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
98 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
99 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
100 (char *)mdname, strlen(mdname) + 1);
101 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
102 (unsigned char *)secret, hashlen);
103 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
104 hkdflabel, hkdflabellen);
105 *p++ = OSSL_PARAM_construct_end();
107 ret = EVP_KDF_CTX_set_params(kctx, params) <= 0
108 || EVP_KDF_derive(kctx, out, outlen) <= 0;
110 EVP_KDF_CTX_free(kctx);
114 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
115 ERR_R_INTERNAL_ERROR);
117 SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
124 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
125 * success 0 on failure.
127 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
128 unsigned char *key, size_t keylen)
130 #ifdef CHARSET_EBCDIC
131 static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
133 static const unsigned char keylabel[] = "key";
136 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
137 NULL, 0, key, keylen, 1);
141 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
142 * success 0 on failure.
144 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
145 unsigned char *iv, size_t ivlen)
147 #ifdef CHARSET_EBCDIC
148 static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
150 static const unsigned char ivlabel[] = "iv";
153 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
154 NULL, 0, iv, ivlen, 1);
157 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
158 const unsigned char *secret,
159 unsigned char *fin, size_t finlen)
161 #ifdef CHARSET_EBCDIC
162 static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
164 static const unsigned char finishedlabel[] = "finished";
167 return tls13_hkdf_expand(s, md, secret, finishedlabel,
168 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
172 * Given the previous secret |prevsecret| and a new input secret |insecret| of
173 * length |insecretlen|, generate a new secret and store it in the location
174 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
176 int tls13_generate_secret(SSL *s, const EVP_MD *md,
177 const unsigned char *prevsecret,
178 const unsigned char *insecret,
180 unsigned char *outsecret)
182 size_t mdlen, prevsecretlen;
187 OSSL_PARAM params[5], *p = params;
188 int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
189 const char *mdname = EVP_MD_name(md);
190 #ifdef CHARSET_EBCDIC
191 static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
193 static const char derived_secret_label[] = "derived";
195 unsigned char preextractsec[EVP_MAX_MD_SIZE];
197 kdf = EVP_KDF_fetch(NULL, OSSL_KDF_NAME_HKDF, NULL);
198 kctx = EVP_KDF_CTX_new(kdf);
201 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
202 ERR_R_INTERNAL_ERROR);
206 mdleni = EVP_MD_size(md);
207 /* Ensure cast to size_t is safe */
208 if (!ossl_assert(mdleni >= 0)) {
209 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
210 ERR_R_INTERNAL_ERROR);
211 EVP_KDF_CTX_free(kctx);
214 mdlen = (size_t)mdleni;
216 if (insecret == NULL) {
217 insecret = default_zeros;
220 if (prevsecret == NULL) {
221 prevsecret = default_zeros;
224 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
225 unsigned char hash[EVP_MAX_MD_SIZE];
227 /* The pre-extract derive step uses a hash of no messages */
229 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
230 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
231 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
232 ERR_R_INTERNAL_ERROR);
233 EVP_MD_CTX_free(mctx);
234 EVP_KDF_CTX_free(kctx);
237 EVP_MD_CTX_free(mctx);
239 /* Generate the pre-extract secret */
240 if (!tls13_hkdf_expand(s, md, prevsecret,
241 (unsigned char *)derived_secret_label,
242 sizeof(derived_secret_label) - 1, hash, mdlen,
243 preextractsec, mdlen, 1)) {
244 /* SSLfatal() already called */
245 EVP_KDF_CTX_free(kctx);
249 prevsecret = preextractsec;
250 prevsecretlen = mdlen;
253 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
254 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
255 (char *)mdname, strlen(mdname) + 1);
256 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
257 (unsigned char *)insecret,
259 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
260 (unsigned char *)prevsecret,
262 *p++ = OSSL_PARAM_construct_end();
264 ret = EVP_KDF_CTX_set_params(kctx, params) <= 0
265 || EVP_KDF_derive(kctx, outsecret, mdlen) <= 0;
268 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
269 ERR_R_INTERNAL_ERROR);
271 EVP_KDF_CTX_free(kctx);
272 if (prevsecret == preextractsec)
273 OPENSSL_cleanse(preextractsec, mdlen);
278 * Given an input secret |insecret| of length |insecretlen| generate the
279 * handshake secret. This requires the early secret to already have been
280 * generated. Returns 1 on success 0 on failure.
282 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
285 /* Calls SSLfatal() if required */
286 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
287 insecret, insecretlen,
288 (unsigned char *)&s->handshake_secret);
292 * Given the handshake secret |prev| of length |prevlen| generate the master
293 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
296 int tls13_generate_master_secret(SSL *s, unsigned char *out,
297 unsigned char *prev, size_t prevlen,
300 const EVP_MD *md = ssl_handshake_md(s);
302 *secret_size = EVP_MD_size(md);
303 /* Calls SSLfatal() if required */
304 return tls13_generate_secret(s, md, prev, NULL, 0, out);
308 * Generates the mac for the Finished message. Returns the length of the MAC or
311 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
314 const EVP_MD *md = ssl_handshake_md(s);
315 unsigned char hash[EVP_MAX_MD_SIZE];
316 size_t hashlen, ret = 0;
317 EVP_PKEY *key = NULL;
318 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
320 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
321 /* SSLfatal() already called */
325 if (str == s->method->ssl3_enc->server_finished_label) {
326 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
327 s->server_finished_secret, hashlen);
328 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
329 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
330 s->client_finished_secret, hashlen);
332 unsigned char finsecret[EVP_MAX_MD_SIZE];
334 if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
335 s->client_app_traffic_secret,
339 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
341 OPENSSL_cleanse(finsecret, sizeof(finsecret));
346 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
347 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
348 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
349 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
350 ERR_R_INTERNAL_ERROR);
357 EVP_MD_CTX_free(ctx);
362 * There isn't really a key block in TLSv1.3, but we still need this function
363 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
365 int tls13_setup_key_block(SSL *s)
370 s->session->cipher = s->s3.tmp.new_cipher;
371 if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, NULL, 0)) {
372 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
373 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
377 s->s3.tmp.new_sym_enc = c;
378 s->s3.tmp.new_hash = hash;
383 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
384 const EVP_CIPHER *ciph,
385 const unsigned char *insecret,
386 const unsigned char *hash,
387 const unsigned char *label,
388 size_t labellen, unsigned char *secret,
389 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
391 unsigned char key[EVP_MAX_KEY_LENGTH];
392 size_t ivlen, keylen, taglen;
393 int hashleni = EVP_MD_size(md);
396 /* Ensure cast to size_t is safe */
397 if (!ossl_assert(hashleni >= 0)) {
398 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
402 hashlen = (size_t)hashleni;
404 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
405 secret, hashlen, 1)) {
406 /* SSLfatal() already called */
410 /* TODO(size_t): convert me */
411 keylen = EVP_CIPHER_key_length(ciph);
412 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
415 ivlen = EVP_CCM_TLS_IV_LEN;
416 if (s->s3.tmp.new_cipher == NULL) {
417 /* We've not selected a cipher yet - we must be doing early data */
418 algenc = s->session->cipher->algorithm_enc;
420 algenc = s->s3.tmp.new_cipher->algorithm_enc;
422 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
423 taglen = EVP_CCM8_TLS_TAG_LEN;
425 taglen = EVP_CCM_TLS_TAG_LEN;
427 ivlen = EVP_CIPHER_iv_length(ciph);
431 if (!tls13_derive_key(s, md, secret, key, keylen)
432 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
433 /* SSLfatal() already called */
437 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
438 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
439 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
441 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
442 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
449 OPENSSL_cleanse(key, sizeof(key));
453 int tls13_change_cipher_state(SSL *s, int which)
455 #ifdef CHARSET_EBCDIC
456 static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
457 static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
458 static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
459 static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
460 static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
461 static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
462 static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
463 static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
465 static const unsigned char client_early_traffic[] = "c e traffic";
466 static const unsigned char client_handshake_traffic[] = "c hs traffic";
467 static const unsigned char client_application_traffic[] = "c ap traffic";
468 static const unsigned char server_handshake_traffic[] = "s hs traffic";
469 static const unsigned char server_application_traffic[] = "s ap traffic";
470 static const unsigned char exporter_master_secret[] = "exp master";
471 static const unsigned char resumption_master_secret[] = "res master";
472 static const unsigned char early_exporter_master_secret[] = "e exp master";
475 unsigned char secret[EVP_MAX_MD_SIZE];
476 unsigned char hashval[EVP_MAX_MD_SIZE];
477 unsigned char *hash = hashval;
478 unsigned char *insecret;
479 unsigned char *finsecret = NULL;
480 const char *log_label = NULL;
481 EVP_CIPHER_CTX *ciph_ctx;
482 size_t finsecretlen = 0;
483 const unsigned char *label;
484 size_t labellen, hashlen = 0;
486 const EVP_MD *md = NULL;
487 const EVP_CIPHER *cipher = NULL;
489 if (which & SSL3_CC_READ) {
490 if (s->enc_read_ctx != NULL) {
491 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
493 s->enc_read_ctx = EVP_CIPHER_CTX_new();
494 if (s->enc_read_ctx == NULL) {
495 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
496 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
500 ciph_ctx = s->enc_read_ctx;
503 RECORD_LAYER_reset_read_sequence(&s->rlayer);
505 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
506 if (s->enc_write_ctx != NULL) {
507 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
509 s->enc_write_ctx = EVP_CIPHER_CTX_new();
510 if (s->enc_write_ctx == NULL) {
511 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
512 SSL_F_TLS13_CHANGE_CIPHER_STATE, 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,
539 SSL_F_TLS13_CHANGE_CIPHER_STATE,
540 SSL_R_BAD_HANDSHAKE_LENGTH);
544 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
545 && s->max_early_data > 0
546 && s->session->ext.max_early_data == 0) {
548 * If we are attempting to send early data, and we've decided to
549 * actually do it but max_early_data in s->session is 0 then we
550 * must be using an external PSK.
552 if (!ossl_assert(s->psksession != NULL
553 && s->max_early_data ==
554 s->psksession->ext.max_early_data)) {
555 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
556 SSL_F_TLS13_CHANGE_CIPHER_STATE,
557 ERR_R_INTERNAL_ERROR);
560 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
562 if (sslcipher == NULL) {
563 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
564 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
569 * We need to calculate the handshake digest using the digest from
570 * the session. We haven't yet selected our ciphersuite so we can't
571 * use ssl_handshake_md().
573 mdctx = EVP_MD_CTX_new();
575 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
576 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
579 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
580 md = ssl_md(sslcipher->algorithm2);
581 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
582 || !EVP_DigestUpdate(mdctx, hdata, handlen)
583 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
584 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
585 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
586 EVP_MD_CTX_free(mdctx);
590 EVP_MD_CTX_free(mdctx);
592 if (!tls13_hkdf_expand(s, md, insecret,
593 early_exporter_master_secret,
594 sizeof(early_exporter_master_secret) - 1,
596 s->early_exporter_master_secret, hashlen,
598 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
599 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
603 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
604 s->early_exporter_master_secret, hashlen)) {
605 /* SSLfatal() already called */
608 } else if (which & SSL3_CC_HANDSHAKE) {
609 insecret = s->handshake_secret;
610 finsecret = s->client_finished_secret;
611 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
612 label = client_handshake_traffic;
613 labellen = sizeof(client_handshake_traffic) - 1;
614 log_label = CLIENT_HANDSHAKE_LABEL;
616 * The handshake hash used for the server read/client write handshake
617 * traffic secret is the same as the hash for the server
618 * write/client read handshake traffic secret. However, if we
619 * processed early data then we delay changing the server
620 * read/client write cipher state until later, and the handshake
621 * hashes have moved on. Therefore we use the value saved earlier
622 * when we did the server write/client read change cipher state.
624 hash = s->handshake_traffic_hash;
626 insecret = s->master_secret;
627 label = client_application_traffic;
628 labellen = sizeof(client_application_traffic) - 1;
629 log_label = CLIENT_APPLICATION_LABEL;
631 * For this we only use the handshake hashes up until the server
632 * Finished hash. We do not include the client's Finished, which is
633 * what ssl_handshake_hash() would give us. Instead we use the
634 * previously saved value.
636 hash = s->server_finished_hash;
639 /* Early data never applies to client-read/server-write */
640 if (which & SSL3_CC_HANDSHAKE) {
641 insecret = s->handshake_secret;
642 finsecret = s->server_finished_secret;
643 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
644 label = server_handshake_traffic;
645 labellen = sizeof(server_handshake_traffic) - 1;
646 log_label = SERVER_HANDSHAKE_LABEL;
648 insecret = s->master_secret;
649 label = server_application_traffic;
650 labellen = sizeof(server_application_traffic) - 1;
651 log_label = SERVER_APPLICATION_LABEL;
655 if (!(which & SSL3_CC_EARLY)) {
656 md = ssl_handshake_md(s);
657 cipher = s->s3.tmp.new_sym_enc;
658 if (!ssl3_digest_cached_records(s, 1)
659 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
660 /* SSLfatal() already called */;
666 * Save the hash of handshakes up to now for use when we calculate the
667 * client application traffic secret
669 if (label == server_application_traffic)
670 memcpy(s->server_finished_hash, hashval, hashlen);
672 if (label == server_handshake_traffic)
673 memcpy(s->handshake_traffic_hash, hashval, hashlen);
675 if (label == client_application_traffic) {
677 * We also create the resumption master secret, but this time use the
678 * hash for the whole handshake including the Client Finished
680 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
681 resumption_master_secret,
682 sizeof(resumption_master_secret) - 1,
683 hashval, hashlen, s->resumption_master_secret,
685 /* SSLfatal() already called */
690 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
691 insecret, hash, label, labellen, secret, iv,
693 /* SSLfatal() already called */
697 if (label == server_application_traffic) {
698 memcpy(s->server_app_traffic_secret, secret, hashlen);
699 /* Now we create the exporter master secret */
700 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
701 exporter_master_secret,
702 sizeof(exporter_master_secret) - 1,
703 hash, hashlen, s->exporter_master_secret,
705 /* SSLfatal() already called */
709 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
711 /* SSLfatal() already called */
714 } else if (label == client_application_traffic)
715 memcpy(s->client_app_traffic_secret, secret, hashlen);
717 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
718 /* SSLfatal() already called */
722 if (finsecret != NULL
723 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
724 finsecret, finsecretlen)) {
725 /* SSLfatal() already called */
729 if (!s->server && label == client_early_traffic)
730 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
732 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
735 OPENSSL_cleanse(secret, sizeof(secret));
739 int tls13_update_key(SSL *s, int sending)
741 #ifdef CHARSET_EBCDIC
742 static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
744 static const unsigned char application_traffic[] = "traffic upd";
746 const EVP_MD *md = ssl_handshake_md(s);
747 size_t hashlen = EVP_MD_size(md);
748 unsigned char *insecret, *iv;
749 unsigned char secret[EVP_MAX_MD_SIZE];
750 EVP_CIPHER_CTX *ciph_ctx;
753 if (s->server == sending)
754 insecret = s->server_app_traffic_secret;
756 insecret = s->client_app_traffic_secret;
759 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
761 ciph_ctx = s->enc_write_ctx;
762 RECORD_LAYER_reset_write_sequence(&s->rlayer);
765 ciph_ctx = s->enc_read_ctx;
766 RECORD_LAYER_reset_read_sequence(&s->rlayer);
769 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
770 s->s3.tmp.new_sym_enc, insecret, NULL,
772 sizeof(application_traffic) - 1, secret, iv,
774 /* SSLfatal() already called */
778 memcpy(insecret, secret, hashlen);
780 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
783 OPENSSL_cleanse(secret, sizeof(secret));
787 int tls13_alert_code(int code)
789 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
790 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
793 return tls1_alert_code(code);
796 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
797 const char *label, size_t llen,
798 const unsigned char *context,
799 size_t contextlen, int use_context)
801 unsigned char exportsecret[EVP_MAX_MD_SIZE];
802 #ifdef CHARSET_EBCDIC
803 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
805 static const unsigned char exporterlabel[] = "exporter";
807 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
808 const EVP_MD *md = ssl_handshake_md(s);
809 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
810 unsigned int hashsize, datalen;
813 if (ctx == NULL || !ossl_statem_export_allowed(s))
819 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
820 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
821 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
822 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
823 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
824 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
825 (const unsigned char *)label, llen,
826 data, datalen, exportsecret, hashsize, 0)
827 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
828 sizeof(exporterlabel) - 1, hash, hashsize,
834 EVP_MD_CTX_free(ctx);
838 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
839 const char *label, size_t llen,
840 const unsigned char *context,
843 #ifdef CHARSET_EBCDIC
844 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
846 static const unsigned char exporterlabel[] = "exporter";
848 unsigned char exportsecret[EVP_MAX_MD_SIZE];
849 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
851 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
852 unsigned int hashsize, datalen;
854 const SSL_CIPHER *sslcipher;
856 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
859 if (!s->server && s->max_early_data > 0
860 && s->session->ext.max_early_data == 0)
861 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
863 sslcipher = SSL_SESSION_get0_cipher(s->session);
865 md = ssl_md(sslcipher->algorithm2);
868 * Calculate the hash value and store it in |data|. The reason why
869 * the empty string is used is that the definition of TLS-Exporter
872 * TLS-Exporter(label, context_value, key_length) =
873 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
874 * "exporter", Hash(context_value), key_length)
876 * Derive-Secret(Secret, Label, Messages) =
877 * HKDF-Expand-Label(Secret, Label,
878 * Transcript-Hash(Messages), Hash.length)
880 * Here Transcript-Hash is the cipher suite hash algorithm.
882 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
883 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
884 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
885 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
886 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
887 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
888 (const unsigned char *)label, llen,
889 data, datalen, exportsecret, hashsize, 0)
890 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
891 sizeof(exporterlabel) - 1, hash, hashsize,
897 EVP_MD_CTX_free(ctx);