2 * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include "ssl_local.h"
12 #include "internal/ktls.h"
13 #include "record/record_local.h"
14 #include "internal/cryptlib.h"
15 #include <openssl/evp.h>
16 #include <openssl/kdf.h>
17 #include <openssl/core_names.h>
19 #define TLS13_MAX_LABEL_LEN 249
22 static const unsigned char label_prefix[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 };
24 static const unsigned char label_prefix[] = "tls13 ";
28 * Given a |secret|; a |label| of length |labellen|; and |data| of length
29 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
30 * secret |outlen| bytes long and store it in the location pointed to be |out|.
31 * The |data| value may be zero length. Any errors will be treated as fatal if
32 * |fatal| is set. Returns 1 on success 0 on failure.
34 int tls13_hkdf_expand(SSL *s, const EVP_MD *md, 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 fatal)
39 EVP_KDF *kdf = EVP_KDF_fetch(s->ctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF,
42 OSSL_PARAM params[7], *p = params;
43 int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
44 const char *mdname = EVP_MD_get0_name(md);
48 kctx = EVP_KDF_CTX_new(kdf);
53 if (labellen > TLS13_MAX_LABEL_LEN) {
55 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
58 * Probably we have been called from SSL_export_keying_material(),
59 * or SSL_export_keying_material_early().
61 ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
63 EVP_KDF_CTX_free(kctx);
67 if ((ret = EVP_MD_get_size(md)) <= 0) {
68 EVP_KDF_CTX_free(kctx);
70 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
72 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
75 hashlen = (size_t)ret;
77 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
78 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
80 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
81 (unsigned char *)secret, hashlen);
82 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
83 (unsigned char *)label_prefix,
84 sizeof(label_prefix) - 1);
85 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
86 (unsigned char *)label, labellen);
88 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA,
89 (unsigned char *)data,
91 *p++ = OSSL_PARAM_construct_end();
93 ret = EVP_KDF_derive(kctx, out, outlen, params) <= 0;
94 EVP_KDF_CTX_free(kctx);
98 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
100 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
107 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
108 * success 0 on failure.
110 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
111 unsigned char *key, size_t keylen)
113 #ifdef CHARSET_EBCDIC
114 static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
116 static const unsigned char keylabel[] = "key";
119 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
120 NULL, 0, key, keylen, 1);
124 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
125 * success 0 on failure.
127 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
128 unsigned char *iv, size_t ivlen)
130 #ifdef CHARSET_EBCDIC
131 static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
133 static const unsigned char ivlabel[] = "iv";
136 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
137 NULL, 0, iv, ivlen, 1);
140 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
141 const unsigned char *secret,
142 unsigned char *fin, size_t finlen)
144 #ifdef CHARSET_EBCDIC
145 static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
147 static const unsigned char finishedlabel[] = "finished";
150 return tls13_hkdf_expand(s, md, secret, finishedlabel,
151 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
155 * Given the previous secret |prevsecret| and a new input secret |insecret| of
156 * length |insecretlen|, generate a new secret and store it in the location
157 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
159 int tls13_generate_secret(SSL *s, const EVP_MD *md,
160 const unsigned char *prevsecret,
161 const unsigned char *insecret,
163 unsigned char *outsecret)
170 OSSL_PARAM params[7], *p = params;
171 int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
172 const char *mdname = EVP_MD_get0_name(md);
173 #ifdef CHARSET_EBCDIC
174 static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
176 static const char derived_secret_label[] = "derived";
179 kdf = EVP_KDF_fetch(s->ctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF, s->ctx->propq);
180 kctx = EVP_KDF_CTX_new(kdf);
183 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
187 mdleni = EVP_MD_get_size(md);
188 /* Ensure cast to size_t is safe */
189 if (!ossl_assert(mdleni >= 0)) {
190 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
191 EVP_KDF_CTX_free(kctx);
194 mdlen = (size_t)mdleni;
196 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
197 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
199 if (insecret != NULL)
200 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
201 (unsigned char *)insecret,
203 if (prevsecret != NULL)
204 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
205 (unsigned char *)prevsecret, mdlen);
206 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
207 (unsigned char *)label_prefix,
208 sizeof(label_prefix) - 1);
209 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
210 (unsigned char *)derived_secret_label,
211 sizeof(derived_secret_label) - 1);
212 *p++ = OSSL_PARAM_construct_end();
214 ret = EVP_KDF_derive(kctx, outsecret, mdlen, params) <= 0;
217 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
219 EVP_KDF_CTX_free(kctx);
224 * Given an input secret |insecret| of length |insecretlen| generate the
225 * handshake secret. This requires the early secret to already have been
226 * generated. Returns 1 on success 0 on failure.
228 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
231 /* Calls SSLfatal() if required */
232 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
233 insecret, insecretlen,
234 (unsigned char *)&s->handshake_secret);
238 * Given the handshake secret |prev| of length |prevlen| generate the master
239 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
242 int tls13_generate_master_secret(SSL *s, unsigned char *out,
243 unsigned char *prev, size_t prevlen,
246 const EVP_MD *md = ssl_handshake_md(s);
248 *secret_size = EVP_MD_get_size(md);
249 /* Calls SSLfatal() if required */
250 return tls13_generate_secret(s, md, prev, NULL, 0, out);
254 * Generates the mac for the Finished message. Returns the length of the MAC or
257 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
260 const char *mdname = EVP_MD_get0_name(ssl_handshake_md(s));
261 unsigned char hash[EVP_MAX_MD_SIZE];
262 unsigned char finsecret[EVP_MAX_MD_SIZE];
263 unsigned char *key = NULL;
264 size_t len = 0, hashlen;
265 OSSL_PARAM params[2], *p = params;
267 /* Safe to cast away const here since we're not "getting" any data */
268 if (s->ctx->propq != NULL)
269 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES,
270 (char *)s->ctx->propq,
272 *p = OSSL_PARAM_construct_end();
274 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
275 /* SSLfatal() already called */
279 if (str == s->method->ssl3_enc->server_finished_label) {
280 key = s->server_finished_secret;
281 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
282 key = s->client_finished_secret;
284 if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
285 s->client_app_traffic_secret,
291 if (!EVP_Q_mac(s->ctx->libctx, "HMAC", s->ctx->propq, mdname,
292 params, key, hashlen, hash, hashlen,
293 /* outsize as per sizeof(peer_finish_md) */
294 out, EVP_MAX_MD_SIZE * 2, &len)) {
295 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
300 OPENSSL_cleanse(finsecret, sizeof(finsecret));
305 * There isn't really a key block in TLSv1.3, but we still need this function
306 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
308 int tls13_setup_key_block(SSL *s)
313 s->session->cipher = s->s3.tmp.new_cipher;
314 if (!ssl_cipher_get_evp(s->ctx, s->session, &c, &hash, NULL, NULL, NULL,
316 /* Error is already recorded */
317 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
321 ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
322 s->s3.tmp.new_sym_enc = c;
323 ssl_evp_md_free(s->s3.tmp.new_hash);
324 s->s3.tmp.new_hash = hash;
329 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
330 const EVP_CIPHER *ciph,
331 const unsigned char *insecret,
332 const unsigned char *hash,
333 const unsigned char *label,
334 size_t labellen, unsigned char *secret,
335 unsigned char *key, unsigned char *iv,
336 EVP_CIPHER_CTX *ciph_ctx)
338 size_t ivlen, keylen, taglen;
339 int hashleni = EVP_MD_get_size(md);
342 /* Ensure cast to size_t is safe */
343 if (!ossl_assert(hashleni >= 0)) {
344 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
347 hashlen = (size_t)hashleni;
349 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
350 secret, hashlen, 1)) {
351 /* SSLfatal() already called */
355 keylen = EVP_CIPHER_get_key_length(ciph);
356 if (EVP_CIPHER_get_mode(ciph) == EVP_CIPH_CCM_MODE) {
359 ivlen = EVP_CCM_TLS_IV_LEN;
360 if (s->s3.tmp.new_cipher != NULL) {
361 algenc = s->s3.tmp.new_cipher->algorithm_enc;
362 } else if (s->session->cipher != NULL) {
363 /* We've not selected a cipher yet - we must be doing early data */
364 algenc = s->session->cipher->algorithm_enc;
365 } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
366 /* We must be doing early data with out-of-band PSK */
367 algenc = s->psksession->cipher->algorithm_enc;
369 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
372 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
373 taglen = EVP_CCM8_TLS_TAG_LEN;
375 taglen = EVP_CCM_TLS_TAG_LEN;
377 ivlen = EVP_CIPHER_get_iv_length(ciph);
381 if (!tls13_derive_key(s, md, secret, key, keylen)
382 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
383 /* SSLfatal() already called */
387 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
388 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
389 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
391 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
392 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
399 int tls13_change_cipher_state(SSL *s, int which)
401 #ifdef CHARSET_EBCDIC
402 static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
403 static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
404 static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
405 static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
406 static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
407 static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
408 static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
409 static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
411 static const unsigned char client_early_traffic[] = "c e traffic";
412 static const unsigned char client_handshake_traffic[] = "c hs traffic";
413 static const unsigned char client_application_traffic[] = "c ap traffic";
414 static const unsigned char server_handshake_traffic[] = "s hs traffic";
415 static const unsigned char server_application_traffic[] = "s ap traffic";
416 static const unsigned char exporter_master_secret[] = "exp master";
417 static const unsigned char resumption_master_secret[] = "res master";
418 static const unsigned char early_exporter_master_secret[] = "e exp master";
421 unsigned char key[EVP_MAX_KEY_LENGTH];
422 unsigned char secret[EVP_MAX_MD_SIZE];
423 unsigned char hashval[EVP_MAX_MD_SIZE];
424 unsigned char *hash = hashval;
425 unsigned char *insecret;
426 unsigned char *finsecret = NULL;
427 const char *log_label = NULL;
428 EVP_CIPHER_CTX *ciph_ctx;
429 size_t finsecretlen = 0;
430 const unsigned char *label;
431 size_t labellen, hashlen = 0;
433 const EVP_MD *md = NULL;
434 const EVP_CIPHER *cipher = NULL;
435 #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
436 ktls_crypto_info_t crypto_info;
440 if (which & SSL3_CC_READ) {
441 if (s->enc_read_ctx != NULL) {
442 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
444 s->enc_read_ctx = EVP_CIPHER_CTX_new();
445 if (s->enc_read_ctx == NULL) {
446 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
450 ciph_ctx = s->enc_read_ctx;
453 RECORD_LAYER_reset_read_sequence(&s->rlayer);
455 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
456 if (s->enc_write_ctx != NULL) {
457 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
459 s->enc_write_ctx = EVP_CIPHER_CTX_new();
460 if (s->enc_write_ctx == NULL) {
461 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
465 ciph_ctx = s->enc_write_ctx;
468 RECORD_LAYER_reset_write_sequence(&s->rlayer);
471 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
472 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
473 if (which & SSL3_CC_EARLY) {
474 EVP_MD_CTX *mdctx = NULL;
477 unsigned int hashlenui;
478 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
480 insecret = s->early_secret;
481 label = client_early_traffic;
482 labellen = sizeof(client_early_traffic) - 1;
483 log_label = CLIENT_EARLY_LABEL;
485 handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
487 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
491 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
492 && s->max_early_data > 0
493 && s->session->ext.max_early_data == 0) {
495 * If we are attempting to send early data, and we've decided to
496 * actually do it but max_early_data in s->session is 0 then we
497 * must be using an external PSK.
499 if (!ossl_assert(s->psksession != NULL
500 && s->max_early_data ==
501 s->psksession->ext.max_early_data)) {
502 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
505 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
507 if (sslcipher == NULL) {
508 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
513 * We need to calculate the handshake digest using the digest from
514 * the session. We haven't yet selected our ciphersuite so we can't
515 * use ssl_handshake_md().
517 mdctx = EVP_MD_CTX_new();
519 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
524 * This ups the ref count on cipher so we better make sure we free
527 if (!ssl_cipher_get_evp_cipher(s->ctx, sslcipher, &cipher)) {
528 /* Error is already recorded */
529 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
530 EVP_MD_CTX_free(mdctx);
534 md = ssl_md(s->ctx, sslcipher->algorithm2);
535 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
536 || !EVP_DigestUpdate(mdctx, hdata, handlen)
537 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
538 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
539 EVP_MD_CTX_free(mdctx);
543 EVP_MD_CTX_free(mdctx);
545 if (!tls13_hkdf_expand(s, md, insecret,
546 early_exporter_master_secret,
547 sizeof(early_exporter_master_secret) - 1,
549 s->early_exporter_master_secret, hashlen,
551 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
555 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
556 s->early_exporter_master_secret, hashlen)) {
557 /* SSLfatal() already called */
560 } else if (which & SSL3_CC_HANDSHAKE) {
561 insecret = s->handshake_secret;
562 finsecret = s->client_finished_secret;
563 finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
564 label = client_handshake_traffic;
565 labellen = sizeof(client_handshake_traffic) - 1;
566 log_label = CLIENT_HANDSHAKE_LABEL;
568 * The handshake hash used for the server read/client write handshake
569 * traffic secret is the same as the hash for the server
570 * write/client read handshake traffic secret. However, if we
571 * processed early data then we delay changing the server
572 * read/client write cipher state until later, and the handshake
573 * hashes have moved on. Therefore we use the value saved earlier
574 * when we did the server write/client read change cipher state.
576 hash = s->handshake_traffic_hash;
578 insecret = s->master_secret;
579 label = client_application_traffic;
580 labellen = sizeof(client_application_traffic) - 1;
581 log_label = CLIENT_APPLICATION_LABEL;
583 * For this we only use the handshake hashes up until the server
584 * Finished hash. We do not include the client's Finished, which is
585 * what ssl_handshake_hash() would give us. Instead we use the
586 * previously saved value.
588 hash = s->server_finished_hash;
591 /* Early data never applies to client-read/server-write */
592 if (which & SSL3_CC_HANDSHAKE) {
593 insecret = s->handshake_secret;
594 finsecret = s->server_finished_secret;
595 finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
596 label = server_handshake_traffic;
597 labellen = sizeof(server_handshake_traffic) - 1;
598 log_label = SERVER_HANDSHAKE_LABEL;
600 insecret = s->master_secret;
601 label = server_application_traffic;
602 labellen = sizeof(server_application_traffic) - 1;
603 log_label = SERVER_APPLICATION_LABEL;
607 if (!(which & SSL3_CC_EARLY)) {
608 md = ssl_handshake_md(s);
609 cipher = s->s3.tmp.new_sym_enc;
610 if (!ssl3_digest_cached_records(s, 1)
611 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
612 /* SSLfatal() already called */;
618 * Save the hash of handshakes up to now for use when we calculate the
619 * client application traffic secret
621 if (label == server_application_traffic)
622 memcpy(s->server_finished_hash, hashval, hashlen);
624 if (label == server_handshake_traffic)
625 memcpy(s->handshake_traffic_hash, hashval, hashlen);
627 if (label == client_application_traffic) {
629 * We also create the resumption master secret, but this time use the
630 * hash for the whole handshake including the Client Finished
632 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
633 resumption_master_secret,
634 sizeof(resumption_master_secret) - 1,
635 hashval, hashlen, s->resumption_master_secret,
637 /* SSLfatal() already called */
642 /* check whether cipher is known */
643 if (!ossl_assert(cipher != NULL))
646 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
647 insecret, hash, label, labellen, secret, key,
649 /* SSLfatal() already called */
653 if (label == server_application_traffic) {
654 memcpy(s->server_app_traffic_secret, secret, hashlen);
655 /* Now we create the exporter master secret */
656 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
657 exporter_master_secret,
658 sizeof(exporter_master_secret) - 1,
659 hash, hashlen, s->exporter_master_secret,
661 /* SSLfatal() already called */
665 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
667 /* SSLfatal() already called */
670 } else if (label == client_application_traffic)
671 memcpy(s->client_app_traffic_secret, secret, hashlen);
673 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
674 /* SSLfatal() already called */
678 if (finsecret != NULL
679 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
680 finsecret, finsecretlen)) {
681 /* SSLfatal() already called */
685 if (!s->server && label == client_early_traffic)
686 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
688 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
689 #ifndef OPENSSL_NO_KTLS
690 # if defined(OPENSSL_KTLS_TLS13)
691 if (!(which & SSL3_CC_WRITE)
692 || !(which & SSL3_CC_APPLICATION)
693 || (s->options & SSL_OP_ENABLE_KTLS) == 0)
696 /* ktls supports only the maximum fragment size */
697 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
700 /* ktls does not support record padding */
701 if (s->record_padding_cb != NULL)
704 /* check that cipher is supported */
705 if (!ktls_check_supported_cipher(s, cipher, ciph_ctx))
710 if (!ossl_assert(bio != NULL)) {
711 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
715 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
716 if (BIO_flush(bio) <= 0)
719 /* configure kernel crypto structure */
720 if (!ktls_configure_crypto(s, cipher, ciph_ctx,
721 RECORD_LAYER_get_write_sequence(&s->rlayer),
722 &crypto_info, NULL, iv, key, NULL, 0))
725 /* ktls works with user provided buffers directly */
726 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE))
727 ssl3_release_write_buffer(s);
733 if ((which & SSL3_CC_EARLY) != 0) {
734 /* We up-refed this so now we need to down ref */
735 ssl_evp_cipher_free(cipher);
737 OPENSSL_cleanse(key, sizeof(key));
738 OPENSSL_cleanse(secret, sizeof(secret));
742 int tls13_update_key(SSL *s, int sending)
744 #ifdef CHARSET_EBCDIC
745 static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
747 static const unsigned char application_traffic[] = "traffic upd";
749 const EVP_MD *md = ssl_handshake_md(s);
750 size_t hashlen = EVP_MD_get_size(md);
751 unsigned char key[EVP_MAX_KEY_LENGTH];
752 unsigned char *insecret, *iv;
753 unsigned char secret[EVP_MAX_MD_SIZE];
754 EVP_CIPHER_CTX *ciph_ctx;
757 if (s->server == sending)
758 insecret = s->server_app_traffic_secret;
760 insecret = s->client_app_traffic_secret;
763 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
765 ciph_ctx = s->enc_write_ctx;
766 RECORD_LAYER_reset_write_sequence(&s->rlayer);
769 ciph_ctx = s->enc_read_ctx;
770 RECORD_LAYER_reset_read_sequence(&s->rlayer);
773 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
774 s->s3.tmp.new_sym_enc, insecret, NULL,
776 sizeof(application_traffic) - 1, secret, key,
778 /* SSLfatal() already called */
782 memcpy(insecret, secret, hashlen);
784 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
787 OPENSSL_cleanse(key, sizeof(key));
788 OPENSSL_cleanse(secret, sizeof(secret));
792 int tls13_alert_code(int code)
794 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
795 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
798 return tls1_alert_code(code);
801 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
802 const char *label, size_t llen,
803 const unsigned char *context,
804 size_t contextlen, int use_context)
806 unsigned char exportsecret[EVP_MAX_MD_SIZE];
807 #ifdef CHARSET_EBCDIC
808 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
810 static const unsigned char exporterlabel[] = "exporter";
812 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
813 const EVP_MD *md = ssl_handshake_md(s);
814 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
815 unsigned int hashsize, datalen;
818 if (ctx == NULL || !ossl_statem_export_allowed(s))
824 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
825 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
826 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
827 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
828 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
829 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
830 (const unsigned char *)label, llen,
831 data, datalen, exportsecret, hashsize, 0)
832 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
833 sizeof(exporterlabel) - 1, hash, hashsize,
839 EVP_MD_CTX_free(ctx);
843 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
844 const char *label, size_t llen,
845 const unsigned char *context,
848 #ifdef CHARSET_EBCDIC
849 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
851 static const unsigned char exporterlabel[] = "exporter";
853 unsigned char exportsecret[EVP_MAX_MD_SIZE];
854 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
856 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
857 unsigned int hashsize, datalen;
859 const SSL_CIPHER *sslcipher;
861 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
864 if (!s->server && s->max_early_data > 0
865 && s->session->ext.max_early_data == 0)
866 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
868 sslcipher = SSL_SESSION_get0_cipher(s->session);
870 md = ssl_md(s->ctx, sslcipher->algorithm2);
873 * Calculate the hash value and store it in |data|. The reason why
874 * the empty string is used is that the definition of TLS-Exporter
877 * TLS-Exporter(label, context_value, key_length) =
878 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
879 * "exporter", Hash(context_value), key_length)
881 * Derive-Secret(Secret, Label, Messages) =
882 * HKDF-Expand-Label(Secret, Label,
883 * Transcript-Hash(Messages), Hash.length)
885 * Here Transcript-Hash is the cipher suite hash algorithm.
887 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
888 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
889 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
890 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
891 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
892 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
893 (const unsigned char *)label, llen,
894 data, datalen, exportsecret, hashsize, 0)
895 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
896 sizeof(exporterlabel) - 1, hash, hashsize,
902 EVP_MD_CTX_free(ctx);