2 * Copyright 2016-2020 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_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, SSL_F_TLS13_HKDF_EXPAND,
68 ERR_R_INTERNAL_ERROR);
71 * Probably we have been called from SSL_export_keying_material(),
72 * or SSL_export_keying_material_early().
74 SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
76 EVP_KDF_CTX_free(kctx);
80 hashlen = EVP_MD_size(md);
82 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
83 || !WPACKET_put_bytes_u16(&pkt, outlen)
84 || !WPACKET_start_sub_packet_u8(&pkt)
85 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
86 || !WPACKET_memcpy(&pkt, label, labellen)
87 || !WPACKET_close(&pkt)
88 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
89 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
90 || !WPACKET_finish(&pkt)) {
91 EVP_KDF_CTX_free(kctx);
92 WPACKET_cleanup(&pkt);
94 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
95 ERR_R_INTERNAL_ERROR);
97 SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
101 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
102 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
104 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
105 (unsigned char *)secret, hashlen);
106 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_INFO,
107 hkdflabel, hkdflabellen);
108 *p++ = OSSL_PARAM_construct_end();
110 ret = EVP_KDF_CTX_set_params(kctx, params) <= 0
111 || EVP_KDF_derive(kctx, out, outlen) <= 0;
113 EVP_KDF_CTX_free(kctx);
117 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
118 ERR_R_INTERNAL_ERROR);
120 SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
127 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
128 * success 0 on failure.
130 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
131 unsigned char *key, size_t keylen)
133 #ifdef CHARSET_EBCDIC
134 static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
136 static const unsigned char keylabel[] = "key";
139 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
140 NULL, 0, key, keylen, 1);
144 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
145 * success 0 on failure.
147 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
148 unsigned char *iv, size_t ivlen)
150 #ifdef CHARSET_EBCDIC
151 static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
153 static const unsigned char ivlabel[] = "iv";
156 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
157 NULL, 0, iv, ivlen, 1);
160 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
161 const unsigned char *secret,
162 unsigned char *fin, size_t finlen)
164 #ifdef CHARSET_EBCDIC
165 static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
167 static const unsigned char finishedlabel[] = "finished";
170 return tls13_hkdf_expand(s, md, secret, finishedlabel,
171 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
175 * Given the previous secret |prevsecret| and a new input secret |insecret| of
176 * length |insecretlen|, generate a new secret and store it in the location
177 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
179 int tls13_generate_secret(SSL *s, const EVP_MD *md,
180 const unsigned char *prevsecret,
181 const unsigned char *insecret,
183 unsigned char *outsecret)
185 size_t mdlen, prevsecretlen;
190 OSSL_PARAM params[5], *p = params;
191 int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
192 const char *mdname = EVP_MD_name(md);
193 #ifdef CHARSET_EBCDIC
194 static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
196 static const char derived_secret_label[] = "derived";
198 unsigned char preextractsec[EVP_MAX_MD_SIZE];
200 kdf = EVP_KDF_fetch(s->ctx->libctx, OSSL_KDF_NAME_HKDF, s->ctx->propq);
201 kctx = EVP_KDF_CTX_new(kdf);
204 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
205 ERR_R_INTERNAL_ERROR);
209 mdleni = EVP_MD_size(md);
210 /* Ensure cast to size_t is safe */
211 if (!ossl_assert(mdleni >= 0)) {
212 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
213 ERR_R_INTERNAL_ERROR);
214 EVP_KDF_CTX_free(kctx);
217 mdlen = (size_t)mdleni;
219 if (insecret == NULL) {
220 insecret = default_zeros;
223 if (prevsecret == NULL) {
224 prevsecret = default_zeros;
227 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
228 unsigned char hash[EVP_MAX_MD_SIZE];
230 /* The pre-extract derive step uses a hash of no messages */
232 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
233 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
234 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
235 ERR_R_INTERNAL_ERROR);
236 EVP_MD_CTX_free(mctx);
237 EVP_KDF_CTX_free(kctx);
240 EVP_MD_CTX_free(mctx);
242 /* Generate the pre-extract secret */
243 if (!tls13_hkdf_expand(s, md, prevsecret,
244 (unsigned char *)derived_secret_label,
245 sizeof(derived_secret_label) - 1, hash, mdlen,
246 preextractsec, mdlen, 1)) {
247 /* SSLfatal() already called */
248 EVP_KDF_CTX_free(kctx);
252 prevsecret = preextractsec;
253 prevsecretlen = mdlen;
256 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
257 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
259 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
260 (unsigned char *)insecret,
262 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
263 (unsigned char *)prevsecret,
265 *p++ = OSSL_PARAM_construct_end();
267 ret = EVP_KDF_CTX_set_params(kctx, params) <= 0
268 || EVP_KDF_derive(kctx, outsecret, mdlen) <= 0;
271 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
272 ERR_R_INTERNAL_ERROR);
274 EVP_KDF_CTX_free(kctx);
275 if (prevsecret == preextractsec)
276 OPENSSL_cleanse(preextractsec, mdlen);
281 * Given an input secret |insecret| of length |insecretlen| generate the
282 * handshake secret. This requires the early secret to already have been
283 * generated. Returns 1 on success 0 on failure.
285 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
288 /* Calls SSLfatal() if required */
289 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
290 insecret, insecretlen,
291 (unsigned char *)&s->handshake_secret);
295 * Given the handshake secret |prev| of length |prevlen| generate the master
296 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
299 int tls13_generate_master_secret(SSL *s, unsigned char *out,
300 unsigned char *prev, size_t prevlen,
303 const EVP_MD *md = ssl_handshake_md(s);
305 *secret_size = EVP_MD_size(md);
306 /* Calls SSLfatal() if required */
307 return tls13_generate_secret(s, md, prev, NULL, 0, out);
311 * Generates the mac for the Finished message. Returns the length of the MAC or
314 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
317 const char *mdname = EVP_MD_name(ssl_handshake_md(s));
318 EVP_MAC *hmac = EVP_MAC_fetch(s->ctx->libctx, "HMAC", s->ctx->propq);
319 unsigned char hash[EVP_MAX_MD_SIZE];
320 unsigned char finsecret[EVP_MAX_MD_SIZE];
321 size_t hashlen, ret = 0;
322 EVP_MAC_CTX *ctx = NULL;
323 OSSL_PARAM params[4], *p = params;
326 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
327 ERR_R_INTERNAL_ERROR);
331 /* Safe to cast away const here since we're not "getting" any data */
332 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_DIGEST,
334 if (s->ctx->propq != NULL)
335 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES,
336 (char *)s->ctx->propq,
339 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
340 /* SSLfatal() already called */
344 if (str == s->method->ssl3_enc->server_finished_label) {
345 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
346 s->server_finished_secret,
348 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
349 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
350 s->client_finished_secret,
353 if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
354 s->client_app_traffic_secret,
358 *p++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, finsecret,
361 *p++ = OSSL_PARAM_construct_end();
363 ctx = EVP_MAC_CTX_new(hmac);
365 || !EVP_MAC_CTX_set_params(ctx, params)
366 || !EVP_MAC_init(ctx)
367 || !EVP_MAC_update(ctx, hash, hashlen)
368 /* outsize as per sizeof(peer_finish_md) */
369 || !EVP_MAC_final(ctx, out, &hashlen, EVP_MAX_MD_SIZE * 2)) {
370 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
371 ERR_R_INTERNAL_ERROR);
377 OPENSSL_cleanse(finsecret, sizeof(finsecret));
378 EVP_MAC_CTX_free(ctx);
384 * There isn't really a key block in TLSv1.3, but we still need this function
385 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
387 int tls13_setup_key_block(SSL *s)
392 s->session->cipher = s->s3.tmp.new_cipher;
393 if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, NULL, NULL, NULL,
395 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
396 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
400 ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
401 s->s3.tmp.new_sym_enc = c;
402 ssl_evp_md_free(s->s3.tmp.new_hash);
403 s->s3.tmp.new_hash = hash;
408 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
409 const EVP_CIPHER *ciph,
410 const unsigned char *insecret,
411 const unsigned char *hash,
412 const unsigned char *label,
413 size_t labellen, unsigned char *secret,
414 unsigned char *key, unsigned char *iv,
415 EVP_CIPHER_CTX *ciph_ctx)
417 size_t ivlen, keylen, taglen;
418 int hashleni = EVP_MD_size(md);
421 /* Ensure cast to size_t is safe */
422 if (!ossl_assert(hashleni >= 0)) {
423 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
427 hashlen = (size_t)hashleni;
429 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
430 secret, hashlen, 1)) {
431 /* SSLfatal() already called */
435 /* TODO(size_t): convert me */
436 keylen = EVP_CIPHER_key_length(ciph);
437 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
440 ivlen = EVP_CCM_TLS_IV_LEN;
441 if (s->s3.tmp.new_cipher != NULL) {
442 algenc = s->s3.tmp.new_cipher->algorithm_enc;
443 } else if (s->session->cipher != NULL) {
444 /* We've not selected a cipher yet - we must be doing early data */
445 algenc = s->session->cipher->algorithm_enc;
446 } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
447 /* We must be doing early data with out-of-band PSK */
448 algenc = s->psksession->cipher->algorithm_enc;
450 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
454 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
455 taglen = EVP_CCM8_TLS_TAG_LEN;
457 taglen = EVP_CCM_TLS_TAG_LEN;
459 ivlen = EVP_CIPHER_iv_length(ciph);
463 if (!tls13_derive_key(s, md, secret, key, keylen)
464 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
465 /* SSLfatal() already called */
469 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
470 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
471 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
473 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
474 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
482 int tls13_change_cipher_state(SSL *s, int which)
484 #ifdef CHARSET_EBCDIC
485 static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
486 static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
487 static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
488 static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
489 static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
490 static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
491 static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
492 static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
494 static const unsigned char client_early_traffic[] = "c e traffic";
495 static const unsigned char client_handshake_traffic[] = "c hs traffic";
496 static const unsigned char client_application_traffic[] = "c ap traffic";
497 static const unsigned char server_handshake_traffic[] = "s hs traffic";
498 static const unsigned char server_application_traffic[] = "s ap traffic";
499 static const unsigned char exporter_master_secret[] = "exp master";
500 static const unsigned char resumption_master_secret[] = "res master";
501 static const unsigned char early_exporter_master_secret[] = "e exp master";
504 unsigned char key[EVP_MAX_KEY_LENGTH];
505 unsigned char secret[EVP_MAX_MD_SIZE];
506 unsigned char hashval[EVP_MAX_MD_SIZE];
507 unsigned char *hash = hashval;
508 unsigned char *insecret;
509 unsigned char *finsecret = NULL;
510 const char *log_label = NULL;
511 EVP_CIPHER_CTX *ciph_ctx;
512 size_t finsecretlen = 0;
513 const unsigned char *label;
514 size_t labellen, hashlen = 0;
516 const EVP_MD *md = NULL;
517 const EVP_CIPHER *cipher = NULL;
518 #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
519 ktls_crypto_info_t crypto_info;
523 if (which & SSL3_CC_READ) {
524 if (s->enc_read_ctx != NULL) {
525 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
527 s->enc_read_ctx = EVP_CIPHER_CTX_new();
528 if (s->enc_read_ctx == NULL) {
529 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
530 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
534 ciph_ctx = s->enc_read_ctx;
537 RECORD_LAYER_reset_read_sequence(&s->rlayer);
539 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
540 if (s->enc_write_ctx != NULL) {
541 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
543 s->enc_write_ctx = EVP_CIPHER_CTX_new();
544 if (s->enc_write_ctx == NULL) {
545 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
546 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
550 ciph_ctx = s->enc_write_ctx;
553 RECORD_LAYER_reset_write_sequence(&s->rlayer);
556 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
557 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
558 if (which & SSL3_CC_EARLY) {
559 EVP_MD_CTX *mdctx = NULL;
562 unsigned int hashlenui;
563 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
565 insecret = s->early_secret;
566 label = client_early_traffic;
567 labellen = sizeof(client_early_traffic) - 1;
568 log_label = CLIENT_EARLY_LABEL;
570 handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
572 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
573 SSL_F_TLS13_CHANGE_CIPHER_STATE,
574 SSL_R_BAD_HANDSHAKE_LENGTH);
578 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
579 && s->max_early_data > 0
580 && s->session->ext.max_early_data == 0) {
582 * If we are attempting to send early data, and we've decided to
583 * actually do it but max_early_data in s->session is 0 then we
584 * must be using an external PSK.
586 if (!ossl_assert(s->psksession != NULL
587 && s->max_early_data ==
588 s->psksession->ext.max_early_data)) {
589 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
590 SSL_F_TLS13_CHANGE_CIPHER_STATE,
591 ERR_R_INTERNAL_ERROR);
594 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
596 if (sslcipher == NULL) {
597 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
598 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
603 * We need to calculate the handshake digest using the digest from
604 * the session. We haven't yet selected our ciphersuite so we can't
605 * use ssl_handshake_md().
607 mdctx = EVP_MD_CTX_new();
609 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
610 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
615 * This ups the ref count on cipher so we better make sure we free
618 if (!ssl_cipher_get_evp_cipher(s->ctx, sslcipher, &cipher)) {
619 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
620 SSL_F_TLS13_CHANGE_CIPHER_STATE,
621 SSL_R_ALGORITHM_FETCH_FAILED);
622 EVP_MD_CTX_free(mdctx);
626 md = ssl_md(s->ctx, sslcipher->algorithm2);
627 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
628 || !EVP_DigestUpdate(mdctx, hdata, handlen)
629 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
630 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
631 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
632 EVP_MD_CTX_free(mdctx);
636 EVP_MD_CTX_free(mdctx);
638 if (!tls13_hkdf_expand(s, md, insecret,
639 early_exporter_master_secret,
640 sizeof(early_exporter_master_secret) - 1,
642 s->early_exporter_master_secret, hashlen,
644 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
645 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
649 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
650 s->early_exporter_master_secret, hashlen)) {
651 /* SSLfatal() already called */
654 } else if (which & SSL3_CC_HANDSHAKE) {
655 insecret = s->handshake_secret;
656 finsecret = s->client_finished_secret;
657 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
658 label = client_handshake_traffic;
659 labellen = sizeof(client_handshake_traffic) - 1;
660 log_label = CLIENT_HANDSHAKE_LABEL;
662 * The handshake hash used for the server read/client write handshake
663 * traffic secret is the same as the hash for the server
664 * write/client read handshake traffic secret. However, if we
665 * processed early data then we delay changing the server
666 * read/client write cipher state until later, and the handshake
667 * hashes have moved on. Therefore we use the value saved earlier
668 * when we did the server write/client read change cipher state.
670 hash = s->handshake_traffic_hash;
672 insecret = s->master_secret;
673 label = client_application_traffic;
674 labellen = sizeof(client_application_traffic) - 1;
675 log_label = CLIENT_APPLICATION_LABEL;
677 * For this we only use the handshake hashes up until the server
678 * Finished hash. We do not include the client's Finished, which is
679 * what ssl_handshake_hash() would give us. Instead we use the
680 * previously saved value.
682 hash = s->server_finished_hash;
685 /* Early data never applies to client-read/server-write */
686 if (which & SSL3_CC_HANDSHAKE) {
687 insecret = s->handshake_secret;
688 finsecret = s->server_finished_secret;
689 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
690 label = server_handshake_traffic;
691 labellen = sizeof(server_handshake_traffic) - 1;
692 log_label = SERVER_HANDSHAKE_LABEL;
694 insecret = s->master_secret;
695 label = server_application_traffic;
696 labellen = sizeof(server_application_traffic) - 1;
697 log_label = SERVER_APPLICATION_LABEL;
701 if (!(which & SSL3_CC_EARLY)) {
702 md = ssl_handshake_md(s);
703 cipher = s->s3.tmp.new_sym_enc;
704 if (!ssl3_digest_cached_records(s, 1)
705 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
706 /* SSLfatal() already called */;
712 * Save the hash of handshakes up to now for use when we calculate the
713 * client application traffic secret
715 if (label == server_application_traffic)
716 memcpy(s->server_finished_hash, hashval, hashlen);
718 if (label == server_handshake_traffic)
719 memcpy(s->handshake_traffic_hash, hashval, hashlen);
721 if (label == client_application_traffic) {
723 * We also create the resumption master secret, but this time use the
724 * hash for the whole handshake including the Client Finished
726 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
727 resumption_master_secret,
728 sizeof(resumption_master_secret) - 1,
729 hashval, hashlen, s->resumption_master_secret,
731 /* SSLfatal() already called */
736 /* check whether cipher is known */
737 if(!ossl_assert(cipher != NULL))
740 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
741 insecret, hash, label, labellen, secret, key,
743 /* SSLfatal() already called */
747 if (label == server_application_traffic) {
748 memcpy(s->server_app_traffic_secret, secret, hashlen);
749 /* Now we create the exporter master secret */
750 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
751 exporter_master_secret,
752 sizeof(exporter_master_secret) - 1,
753 hash, hashlen, s->exporter_master_secret,
755 /* SSLfatal() already called */
759 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
761 /* SSLfatal() already called */
764 } else if (label == client_application_traffic)
765 memcpy(s->client_app_traffic_secret, secret, hashlen);
767 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
768 /* SSLfatal() already called */
772 if (finsecret != NULL
773 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
774 finsecret, finsecretlen)) {
775 /* SSLfatal() already called */
779 if (!s->server && label == client_early_traffic)
780 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
782 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
783 #ifndef OPENSSL_NO_KTLS
784 # if defined(OPENSSL_KTLS_TLS13)
785 if (!(which & SSL3_CC_WRITE) || !(which & SSL3_CC_APPLICATION)
786 || ((which & SSL3_CC_WRITE) && (s->mode & SSL_MODE_NO_KTLS_TX)))
789 /* ktls supports only the maximum fragment size */
790 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
793 /* ktls does not support record padding */
794 if (s->record_padding_cb != NULL)
797 /* check that cipher is supported */
798 if (!ktls_check_supported_cipher(s, cipher, ciph_ctx))
803 if (!ossl_assert(bio != NULL)) {
804 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_CHANGE_CIPHER_STATE,
805 ERR_R_INTERNAL_ERROR);
809 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
810 if (BIO_flush(bio) <= 0)
813 /* configure kernel crypto structure */
814 if (!ktls_configure_crypto(s, cipher, ciph_ctx,
815 RECORD_LAYER_get_write_sequence(&s->rlayer),
816 &crypto_info, NULL, iv, key, NULL, 0))
819 /* ktls works with user provided buffers directly */
820 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE))
821 ssl3_release_write_buffer(s);
827 if ((which & SSL3_CC_EARLY) != 0) {
828 /* We up-refed this so now we need to down ref */
829 ssl_evp_cipher_free(cipher);
831 OPENSSL_cleanse(key, sizeof(key));
832 OPENSSL_cleanse(secret, sizeof(secret));
836 int tls13_update_key(SSL *s, int sending)
838 #ifdef CHARSET_EBCDIC
839 static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
841 static const unsigned char application_traffic[] = "traffic upd";
843 const EVP_MD *md = ssl_handshake_md(s);
844 size_t hashlen = EVP_MD_size(md);
845 unsigned char key[EVP_MAX_KEY_LENGTH];
846 unsigned char *insecret, *iv;
847 unsigned char secret[EVP_MAX_MD_SIZE];
848 EVP_CIPHER_CTX *ciph_ctx;
851 if (s->server == sending)
852 insecret = s->server_app_traffic_secret;
854 insecret = s->client_app_traffic_secret;
857 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
859 ciph_ctx = s->enc_write_ctx;
860 RECORD_LAYER_reset_write_sequence(&s->rlayer);
863 ciph_ctx = s->enc_read_ctx;
864 RECORD_LAYER_reset_read_sequence(&s->rlayer);
867 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
868 s->s3.tmp.new_sym_enc, insecret, NULL,
870 sizeof(application_traffic) - 1, secret, key,
872 /* SSLfatal() already called */
876 memcpy(insecret, secret, hashlen);
878 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
881 OPENSSL_cleanse(key, sizeof(key));
882 OPENSSL_cleanse(secret, sizeof(secret));
886 int tls13_alert_code(int code)
888 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
889 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
892 return tls1_alert_code(code);
895 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
896 const char *label, size_t llen,
897 const unsigned char *context,
898 size_t contextlen, int use_context)
900 unsigned char exportsecret[EVP_MAX_MD_SIZE];
901 #ifdef CHARSET_EBCDIC
902 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
904 static const unsigned char exporterlabel[] = "exporter";
906 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
907 const EVP_MD *md = ssl_handshake_md(s);
908 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
909 unsigned int hashsize, datalen;
912 if (ctx == NULL || !ossl_statem_export_allowed(s))
918 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
919 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
920 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
921 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
922 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
923 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
924 (const unsigned char *)label, llen,
925 data, datalen, exportsecret, hashsize, 0)
926 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
927 sizeof(exporterlabel) - 1, hash, hashsize,
933 EVP_MD_CTX_free(ctx);
937 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
938 const char *label, size_t llen,
939 const unsigned char *context,
942 #ifdef CHARSET_EBCDIC
943 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
945 static const unsigned char exporterlabel[] = "exporter";
947 unsigned char exportsecret[EVP_MAX_MD_SIZE];
948 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
950 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
951 unsigned int hashsize, datalen;
953 const SSL_CIPHER *sslcipher;
955 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
958 if (!s->server && s->max_early_data > 0
959 && s->session->ext.max_early_data == 0)
960 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
962 sslcipher = SSL_SESSION_get0_cipher(s->session);
964 md = ssl_md(s->ctx, sslcipher->algorithm2);
967 * Calculate the hash value and store it in |data|. The reason why
968 * the empty string is used is that the definition of TLS-Exporter
971 * TLS-Exporter(label, context_value, key_length) =
972 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
973 * "exporter", Hash(context_value), key_length)
975 * Derive-Secret(Secret, Label, Messages) =
976 * HKDF-Expand-Label(Secret, Label,
977 * Transcript-Hash(Messages), Hash.length)
979 * Here Transcript-Hash is the cipher suite hash algorithm.
981 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
982 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
983 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
984 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
985 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
986 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
987 (const unsigned char *)label, llen,
988 data, datalen, exportsecret, hashsize, 0)
989 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
990 sizeof(exporterlabel) - 1, hash, hashsize,
996 EVP_MD_CTX_free(ctx);