2 * Copyright 2016-2022 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_CONNECTION *s, const EVP_MD *md,
35 const unsigned char *secret,
36 const unsigned char *label, size_t labellen,
37 const unsigned char *data, size_t datalen,
38 unsigned char *out, size_t outlen, int fatal)
40 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
41 EVP_KDF *kdf = EVP_KDF_fetch(sctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF,
44 OSSL_PARAM params[7], *p = params;
45 int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
46 const char *mdname = EVP_MD_get0_name(md);
50 kctx = EVP_KDF_CTX_new(kdf);
55 if (labellen > TLS13_MAX_LABEL_LEN) {
57 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
60 * Probably we have been called from SSL_export_keying_material(),
61 * or SSL_export_keying_material_early().
63 ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
65 EVP_KDF_CTX_free(kctx);
69 if ((ret = EVP_MD_get_size(md)) <= 0) {
70 EVP_KDF_CTX_free(kctx);
72 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
74 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
77 hashlen = (size_t)ret;
79 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
80 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
82 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
83 (unsigned char *)secret, hashlen);
84 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
85 (unsigned char *)label_prefix,
86 sizeof(label_prefix) - 1);
87 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
88 (unsigned char *)label, labellen);
90 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA,
91 (unsigned char *)data,
93 *p++ = OSSL_PARAM_construct_end();
95 ret = EVP_KDF_derive(kctx, out, outlen, params) <= 0;
96 EVP_KDF_CTX_free(kctx);
100 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
102 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
109 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
110 * success 0 on failure.
112 int tls13_derive_key(SSL_CONNECTION *s, const EVP_MD *md,
113 const unsigned char *secret,
114 unsigned char *key, size_t keylen)
116 #ifdef CHARSET_EBCDIC
117 static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
119 static const unsigned char keylabel[] = "key";
122 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
123 NULL, 0, key, keylen, 1);
127 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
128 * success 0 on failure.
130 int tls13_derive_iv(SSL_CONNECTION *s, const EVP_MD *md,
131 const unsigned char *secret,
132 unsigned char *iv, size_t ivlen)
134 #ifdef CHARSET_EBCDIC
135 static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
137 static const unsigned char ivlabel[] = "iv";
140 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
141 NULL, 0, iv, ivlen, 1);
144 int tls13_derive_finishedkey(SSL_CONNECTION *s, const EVP_MD *md,
145 const unsigned char *secret,
146 unsigned char *fin, size_t finlen)
148 #ifdef CHARSET_EBCDIC
149 static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
151 static const unsigned char finishedlabel[] = "finished";
154 return tls13_hkdf_expand(s, md, secret, finishedlabel,
155 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
159 * Given the previous secret |prevsecret| and a new input secret |insecret| of
160 * length |insecretlen|, generate a new secret and store it in the location
161 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
163 int tls13_generate_secret(SSL_CONNECTION *s, const EVP_MD *md,
164 const unsigned char *prevsecret,
165 const unsigned char *insecret,
167 unsigned char *outsecret)
174 OSSL_PARAM params[7], *p = params;
175 int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
176 const char *mdname = EVP_MD_get0_name(md);
177 #ifdef CHARSET_EBCDIC
178 static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
180 static const char derived_secret_label[] = "derived";
182 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
184 kdf = EVP_KDF_fetch(sctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF, sctx->propq);
185 kctx = EVP_KDF_CTX_new(kdf);
188 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
192 mdleni = EVP_MD_get_size(md);
193 /* Ensure cast to size_t is safe */
194 if (!ossl_assert(mdleni >= 0)) {
195 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
196 EVP_KDF_CTX_free(kctx);
199 mdlen = (size_t)mdleni;
201 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
202 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
204 if (insecret != NULL)
205 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
206 (unsigned char *)insecret,
208 if (prevsecret != NULL)
209 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
210 (unsigned char *)prevsecret, mdlen);
211 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
212 (unsigned char *)label_prefix,
213 sizeof(label_prefix) - 1);
214 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
215 (unsigned char *)derived_secret_label,
216 sizeof(derived_secret_label) - 1);
217 *p++ = OSSL_PARAM_construct_end();
219 ret = EVP_KDF_derive(kctx, outsecret, mdlen, params) <= 0;
222 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
224 EVP_KDF_CTX_free(kctx);
229 * Given an input secret |insecret| of length |insecretlen| generate the
230 * handshake secret. This requires the early secret to already have been
231 * generated. Returns 1 on success 0 on failure.
233 int tls13_generate_handshake_secret(SSL_CONNECTION *s,
234 const unsigned char *insecret,
237 /* Calls SSLfatal() if required */
238 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
239 insecret, insecretlen,
240 (unsigned char *)&s->handshake_secret);
244 * Given the handshake secret |prev| of length |prevlen| generate the master
245 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
248 int tls13_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
249 unsigned char *prev, size_t prevlen,
252 const EVP_MD *md = ssl_handshake_md(s);
254 *secret_size = EVP_MD_get_size(md);
255 /* Calls SSLfatal() if required */
256 return tls13_generate_secret(s, md, prev, NULL, 0, out);
260 * Generates the mac for the Finished message. Returns the length of the MAC or
263 size_t tls13_final_finish_mac(SSL_CONNECTION *s, const char *str, size_t slen,
266 const EVP_MD *md = ssl_handshake_md(s);
267 const char *mdname = EVP_MD_get0_name(md);
268 unsigned char hash[EVP_MAX_MD_SIZE];
269 unsigned char finsecret[EVP_MAX_MD_SIZE];
270 unsigned char *key = NULL;
271 size_t len = 0, hashlen;
272 OSSL_PARAM params[2], *p = params;
273 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
278 /* Safe to cast away const here since we're not "getting" any data */
279 if (sctx->propq != NULL)
280 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES,
283 *p = OSSL_PARAM_construct_end();
285 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
286 /* SSLfatal() already called */
290 if (str == SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->server_finished_label) {
291 key = s->server_finished_secret;
292 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
293 key = s->client_finished_secret;
295 if (!tls13_derive_finishedkey(s, md,
296 s->client_app_traffic_secret,
302 if (!EVP_Q_mac(sctx->libctx, "HMAC", sctx->propq, mdname,
303 params, key, hashlen, hash, hashlen,
304 /* outsize as per sizeof(peer_finish_md) */
305 out, EVP_MAX_MD_SIZE * 2, &len)) {
306 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
311 OPENSSL_cleanse(finsecret, sizeof(finsecret));
316 * There isn't really a key block in TLSv1.3, but we still need this function
317 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
319 int tls13_setup_key_block(SSL_CONNECTION *s)
324 s->session->cipher = s->s3.tmp.new_cipher;
325 if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
326 NULL, NULL, NULL, 0)) {
327 /* Error is already recorded */
328 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
332 ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
333 s->s3.tmp.new_sym_enc = c;
334 ssl_evp_md_free(s->s3.tmp.new_hash);
335 s->s3.tmp.new_hash = hash;
340 static int derive_secret_key_and_iv(SSL_CONNECTION *s, int sending,
342 const EVP_CIPHER *ciph,
343 const unsigned char *insecret,
344 const unsigned char *hash,
345 const unsigned char *label,
346 size_t labellen, unsigned char *secret,
347 unsigned char *key, size_t *keylen,
348 unsigned char *iv, size_t *ivlen,
350 EVP_CIPHER_CTX *ciph_ctx)
352 int hashleni = EVP_MD_get_size(md);
356 /* Ensure cast to size_t is safe */
357 if (!ossl_assert(hashleni >= 0)) {
358 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
361 hashlen = (size_t)hashleni;
363 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
364 secret, hashlen, 1)) {
365 /* SSLfatal() already called */
369 *keylen = EVP_CIPHER_get_key_length(ciph);
371 mode = EVP_CIPHER_get_mode(ciph);
372 if (mode == EVP_CIPH_CCM_MODE) {
375 *ivlen = EVP_CCM_TLS_IV_LEN;
376 if (s->s3.tmp.new_cipher != NULL) {
377 algenc = s->s3.tmp.new_cipher->algorithm_enc;
378 } else if (s->session->cipher != NULL) {
379 /* We've not selected a cipher yet - we must be doing early data */
380 algenc = s->session->cipher->algorithm_enc;
381 } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
382 /* We must be doing early data with out-of-band PSK */
383 algenc = s->psksession->cipher->algorithm_enc;
385 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
388 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
389 *taglen = EVP_CCM8_TLS_TAG_LEN;
391 *taglen = EVP_CCM_TLS_TAG_LEN;
395 if (mode == EVP_CIPH_GCM_MODE) {
396 *taglen = EVP_GCM_TLS_TAG_LEN;
398 /* CHACHA20P-POLY1305 */
399 *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
401 iivlen = EVP_CIPHER_get_iv_length(ciph);
403 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
409 if (!tls13_derive_key(s, md, secret, key, *keylen)
410 || !tls13_derive_iv(s, md, secret, iv, *ivlen)) {
411 /* SSLfatal() already called */
416 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
417 || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, *ivlen, NULL) <= 0
418 || (mode == EVP_CIPH_CCM_MODE
419 && EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, *taglen, NULL) <= 0)
420 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
421 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
429 int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
431 #ifdef CHARSET_EBCDIC
432 static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
433 static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
434 static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
435 static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
436 static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
437 static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
438 static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
439 static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
441 static const unsigned char client_early_traffic[] = "c e traffic";
442 static const unsigned char client_handshake_traffic[] = "c hs traffic";
443 static const unsigned char client_application_traffic[] = "c ap traffic";
444 static const unsigned char server_handshake_traffic[] = "s hs traffic";
445 static const unsigned char server_application_traffic[] = "s ap traffic";
446 static const unsigned char exporter_master_secret[] = "exp master";
447 static const unsigned char resumption_master_secret[] = "res master";
448 static const unsigned char early_exporter_master_secret[] = "e exp master";
451 unsigned char key[EVP_MAX_KEY_LENGTH];
452 unsigned char secret[EVP_MAX_MD_SIZE];
453 unsigned char hashval[EVP_MAX_MD_SIZE];
454 unsigned char *hash = hashval;
455 unsigned char *insecret;
456 unsigned char *finsecret = NULL;
457 const char *log_label = NULL;
458 EVP_CIPHER_CTX *ciph_ctx = NULL;
459 size_t finsecretlen = 0;
460 const unsigned char *label;
461 size_t labellen, hashlen = 0;
463 const EVP_MD *md = NULL;
464 const EVP_CIPHER *cipher = NULL;
465 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
466 size_t keylen, ivlen, taglen;
467 #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
468 ktls_crypto_info_t crypto_info;
473 if (which & SSL3_CC_READ) {
476 RECORD_LAYER_reset_read_sequence(&s->rlayer);
478 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
479 if (s->enc_write_ctx != NULL) {
480 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
482 s->enc_write_ctx = EVP_CIPHER_CTX_new();
483 if (s->enc_write_ctx == NULL) {
484 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
488 ciph_ctx = s->enc_write_ctx;
491 RECORD_LAYER_reset_write_sequence(&s->rlayer);
494 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
495 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
496 if (which & SSL3_CC_EARLY) {
497 EVP_MD_CTX *mdctx = NULL;
500 unsigned int hashlenui;
501 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
503 insecret = s->early_secret;
504 label = client_early_traffic;
505 labellen = sizeof(client_early_traffic) - 1;
506 log_label = CLIENT_EARLY_LABEL;
508 handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
510 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
514 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
515 && s->max_early_data > 0
516 && s->session->ext.max_early_data == 0) {
518 * If we are attempting to send early data, and we've decided to
519 * actually do it but max_early_data in s->session is 0 then we
520 * must be using an external PSK.
522 if (!ossl_assert(s->psksession != NULL
523 && s->max_early_data ==
524 s->psksession->ext.max_early_data)) {
525 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
528 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
530 if (sslcipher == NULL) {
531 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
536 * We need to calculate the handshake digest using the digest from
537 * the session. We haven't yet selected our ciphersuite so we can't
538 * use ssl_handshake_md().
540 mdctx = EVP_MD_CTX_new();
542 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
547 * This ups the ref count on cipher so we better make sure we free
550 if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)) {
551 /* Error is already recorded */
552 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
553 EVP_MD_CTX_free(mdctx);
557 md = ssl_md(sctx, sslcipher->algorithm2);
558 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
559 || !EVP_DigestUpdate(mdctx, hdata, handlen)
560 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
561 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
562 EVP_MD_CTX_free(mdctx);
566 EVP_MD_CTX_free(mdctx);
568 if (!tls13_hkdf_expand(s, md, insecret,
569 early_exporter_master_secret,
570 sizeof(early_exporter_master_secret) - 1,
572 s->early_exporter_master_secret, hashlen,
574 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
578 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
579 s->early_exporter_master_secret, hashlen)) {
580 /* SSLfatal() already called */
583 } else if (which & SSL3_CC_HANDSHAKE) {
584 insecret = s->handshake_secret;
585 finsecret = s->client_finished_secret;
586 finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
587 label = client_handshake_traffic;
588 labellen = sizeof(client_handshake_traffic) - 1;
589 log_label = CLIENT_HANDSHAKE_LABEL;
591 * The handshake hash used for the server read/client write handshake
592 * traffic secret is the same as the hash for the server
593 * write/client read handshake traffic secret. However, if we
594 * processed early data then we delay changing the server
595 * read/client write cipher state until later, and the handshake
596 * hashes have moved on. Therefore we use the value saved earlier
597 * when we did the server write/client read change cipher state.
599 hash = s->handshake_traffic_hash;
601 insecret = s->master_secret;
602 label = client_application_traffic;
603 labellen = sizeof(client_application_traffic) - 1;
604 log_label = CLIENT_APPLICATION_LABEL;
606 * For this we only use the handshake hashes up until the server
607 * Finished hash. We do not include the client's Finished, which is
608 * what ssl_handshake_hash() would give us. Instead we use the
609 * previously saved value.
611 hash = s->server_finished_hash;
614 /* Early data never applies to client-read/server-write */
615 if (which & SSL3_CC_HANDSHAKE) {
616 insecret = s->handshake_secret;
617 finsecret = s->server_finished_secret;
618 finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
619 label = server_handshake_traffic;
620 labellen = sizeof(server_handshake_traffic) - 1;
621 log_label = SERVER_HANDSHAKE_LABEL;
623 insecret = s->master_secret;
624 label = server_application_traffic;
625 labellen = sizeof(server_application_traffic) - 1;
626 log_label = SERVER_APPLICATION_LABEL;
630 if (!(which & SSL3_CC_EARLY)) {
631 md = ssl_handshake_md(s);
632 cipher = s->s3.tmp.new_sym_enc;
633 if (!ssl3_digest_cached_records(s, 1)
634 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
635 /* SSLfatal() already called */;
641 * Save the hash of handshakes up to now for use when we calculate the
642 * client application traffic secret
644 if (label == server_application_traffic)
645 memcpy(s->server_finished_hash, hashval, hashlen);
647 if (label == server_handshake_traffic)
648 memcpy(s->handshake_traffic_hash, hashval, hashlen);
650 if (label == client_application_traffic) {
652 * We also create the resumption master secret, but this time use the
653 * hash for the whole handshake including the Client Finished
655 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
656 resumption_master_secret,
657 sizeof(resumption_master_secret) - 1,
658 hashval, hashlen, s->resumption_master_secret,
660 /* SSLfatal() already called */
665 /* check whether cipher is known */
666 if (!ossl_assert(cipher != NULL))
669 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
670 insecret, hash, label, labellen, secret, key,
671 &keylen, iv, &ivlen, &taglen, ciph_ctx)) {
672 /* SSLfatal() already called */
676 if (label == server_application_traffic) {
677 memcpy(s->server_app_traffic_secret, secret, hashlen);
678 /* Now we create the exporter master secret */
679 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
680 exporter_master_secret,
681 sizeof(exporter_master_secret) - 1,
682 hash, hashlen, s->exporter_master_secret,
684 /* SSLfatal() already called */
688 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
690 /* SSLfatal() already called */
693 } else if (label == client_application_traffic)
694 memcpy(s->client_app_traffic_secret, secret, hashlen);
696 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
697 /* SSLfatal() already called */
701 if (finsecret != NULL
702 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
703 finsecret, finsecretlen)) {
704 /* SSLfatal() already called */
708 if (!s->server && label == client_early_traffic)
709 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
711 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
713 if ((which & SSL3_CC_READ) != 0) {
714 int level = (which & SSL3_CC_EARLY) != 0
715 ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
716 : ((which &SSL3_CC_HANDSHAKE) != 0
717 ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
718 : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
720 if (!ssl_set_new_record_layer(s, s->version,
721 OSSL_RECORD_DIRECTION_READ,
722 level, key, keylen, iv, ivlen, NULL, 0,
723 cipher, taglen, NID_undef, NULL, NULL)) {
724 /* SSLfatal already called */
727 /* TODO(RECLAYER): Remove me when write rlayer done */
731 #ifndef OPENSSL_NO_KTLS
732 # if defined(OPENSSL_KTLS_TLS13)
733 if (!(which & SSL3_CC_APPLICATION)
734 || (s->options & SSL_OP_ENABLE_KTLS) == 0)
737 /* ktls supports only the maximum fragment size */
738 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
741 /* ktls does not support record padding */
742 if (s->record_padding_cb != NULL)
745 /* check that cipher is supported */
746 if (!ktls_check_supported_cipher(s, cipher, NULL, taglen))
749 if (which & SSL3_CC_WRITE)
754 if (!ossl_assert(bio != NULL)) {
755 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
759 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
760 if (which & SSL3_CC_WRITE) {
761 if (BIO_flush(bio) <= 0)
765 /* configure kernel crypto structure */
766 if (which & SSL3_CC_WRITE)
767 rl_sequence = RECORD_LAYER_get_write_sequence(&s->rlayer);
769 rl_sequence = RECORD_LAYER_get_read_sequence(&s->rlayer);
771 if (!ktls_configure_crypto(sctx->libctx, s->version, cipher, NULL,
772 rl_sequence, &crypto_info, which & SSL3_CC_WRITE,
773 iv, ivlen, key, keylen, NULL, 0))
776 /* ktls works with user provided buffers directly */
777 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
778 if (which & SSL3_CC_WRITE)
779 ssl3_release_write_buffer(s);
786 if ((which & SSL3_CC_EARLY) != 0) {
787 /* We up-refed this so now we need to down ref */
788 ssl_evp_cipher_free(cipher);
790 OPENSSL_cleanse(key, sizeof(key));
791 OPENSSL_cleanse(secret, sizeof(secret));
795 int tls13_update_key(SSL_CONNECTION *s, int sending)
797 #ifdef CHARSET_EBCDIC
798 static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
800 static const unsigned char application_traffic[] = "traffic upd";
802 const EVP_MD *md = ssl_handshake_md(s);
803 size_t hashlen = EVP_MD_get_size(md);
804 unsigned char key[EVP_MAX_KEY_LENGTH];
805 unsigned char *insecret, *iv;
806 unsigned char secret[EVP_MAX_MD_SIZE];
807 EVP_CIPHER_CTX *ciph_ctx;
808 size_t keylen, ivlen, taglen;
811 if (s->server == sending)
812 insecret = s->server_app_traffic_secret;
814 insecret = s->client_app_traffic_secret;
817 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
819 ciph_ctx = s->enc_write_ctx;
820 RECORD_LAYER_reset_write_sequence(&s->rlayer);
823 ciph_ctx = s->enc_read_ctx;
824 RECORD_LAYER_reset_read_sequence(&s->rlayer);
827 if (!derive_secret_key_and_iv(s, sending, md,
828 s->s3.tmp.new_sym_enc, insecret, NULL,
830 sizeof(application_traffic) - 1, secret, key,
831 &keylen, iv, &ivlen, &taglen, ciph_ctx)) {
832 /* SSLfatal() already called */
836 memcpy(insecret, secret, hashlen);
839 if (!ssl_set_new_record_layer(s, s->version,
840 OSSL_RECORD_DIRECTION_READ,
841 OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
842 key, keylen, iv, ivlen, NULL, 0,
843 s->s3.tmp.new_sym_enc, taglen, NID_undef, NULL,
845 /* SSLfatal already called */
850 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
853 OPENSSL_cleanse(key, sizeof(key));
854 OPENSSL_cleanse(secret, sizeof(secret));
858 int tls13_alert_code(int code)
860 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
861 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
864 return tls1_alert_code(code);
867 int tls13_export_keying_material(SSL_CONNECTION *s,
868 unsigned char *out, size_t olen,
869 const char *label, size_t llen,
870 const unsigned char *context,
871 size_t contextlen, int use_context)
873 unsigned char exportsecret[EVP_MAX_MD_SIZE];
874 #ifdef CHARSET_EBCDIC
875 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
877 static const unsigned char exporterlabel[] = "exporter";
879 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
880 const EVP_MD *md = ssl_handshake_md(s);
881 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
882 unsigned int hashsize, datalen;
885 if (ctx == NULL || md == NULL || !ossl_statem_export_allowed(s))
891 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
892 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
893 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
894 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
895 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
896 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
897 (const unsigned char *)label, llen,
898 data, datalen, exportsecret, hashsize, 0)
899 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
900 sizeof(exporterlabel) - 1, hash, hashsize,
906 EVP_MD_CTX_free(ctx);
910 int tls13_export_keying_material_early(SSL_CONNECTION *s,
911 unsigned char *out, size_t olen,
912 const char *label, size_t llen,
913 const unsigned char *context,
916 #ifdef CHARSET_EBCDIC
917 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
919 static const unsigned char exporterlabel[] = "exporter";
921 unsigned char exportsecret[EVP_MAX_MD_SIZE];
922 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
924 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
925 unsigned int hashsize, datalen;
927 const SSL_CIPHER *sslcipher;
929 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
932 if (!s->server && s->max_early_data > 0
933 && s->session->ext.max_early_data == 0)
934 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
936 sslcipher = SSL_SESSION_get0_cipher(s->session);
938 md = ssl_md(SSL_CONNECTION_GET_CTX(s), sslcipher->algorithm2);
941 * Calculate the hash value and store it in |data|. The reason why
942 * the empty string is used is that the definition of TLS-Exporter
945 * TLS-Exporter(label, context_value, key_length) =
946 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
947 * "exporter", Hash(context_value), key_length)
949 * Derive-Secret(Secret, Label, Messages) =
950 * HKDF-Expand-Label(Secret, Label,
951 * Transcript-Hash(Messages), Hash.length)
953 * Here Transcript-Hash is the cipher suite hash algorithm.
956 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
957 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
958 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
959 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
960 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
961 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
962 (const unsigned char *)label, llen,
963 data, datalen, exportsecret, hashsize, 0)
964 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
965 sizeof(exporterlabel) - 1, hash, hashsize,
971 EVP_MD_CTX_free(ctx);