2 * Copyright 2016-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (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>
18 #define TLS13_MAX_LABEL_LEN 249
20 /* Always filled with zeros */
21 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
24 * Given a |secret|; a |label| of length |labellen|; and |data| of length
25 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
26 * secret |outlen| bytes long and store it in the location pointed to be |out|.
27 * The |data| value may be zero length. Any errors will be treated as fatal if
28 * |fatal| is set. Returns 1 on success 0 on failure.
30 int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
31 const unsigned char *label, size_t labellen,
32 const unsigned char *data, size_t datalen,
33 unsigned char *out, size_t outlen, int fatal)
36 static const unsigned char label_prefix[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 };
38 static const unsigned char label_prefix[] = "tls13 ";
40 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
45 * 2 bytes for length of derived secret + 1 byte for length of combined
46 * prefix and label + bytes for the label itself + 1 byte length of hash
47 * + bytes for the hash itself
49 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t)
50 + (sizeof(label_prefix) - 1) + TLS13_MAX_LABEL_LEN
51 + 1 + EVP_MAX_MD_SIZE];
57 if (labellen > TLS13_MAX_LABEL_LEN) {
59 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
60 ERR_R_INTERNAL_ERROR);
63 * Probably we have been called from SSL_export_keying_material(),
64 * or SSL_export_keying_material_early().
66 SSLerr(SSL_F_TLS13_HKDF_EXPAND, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
68 EVP_PKEY_CTX_free(pctx);
72 hashlen = EVP_MD_size(md);
74 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
75 || !WPACKET_put_bytes_u16(&pkt, outlen)
76 || !WPACKET_start_sub_packet_u8(&pkt)
77 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
78 || !WPACKET_memcpy(&pkt, label, labellen)
79 || !WPACKET_close(&pkt)
80 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
81 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
82 || !WPACKET_finish(&pkt)) {
83 EVP_PKEY_CTX_free(pctx);
84 WPACKET_cleanup(&pkt);
86 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
87 ERR_R_INTERNAL_ERROR);
89 SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
93 ret = EVP_PKEY_derive_init(pctx) <= 0
94 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
96 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
97 || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
98 || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
99 || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
101 EVP_PKEY_CTX_free(pctx);
105 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
106 ERR_R_INTERNAL_ERROR);
108 SSLerr(SSL_F_TLS13_HKDF_EXPAND, ERR_R_INTERNAL_ERROR);
115 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
116 * success 0 on failure.
118 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
119 unsigned char *key, size_t keylen)
121 #ifdef CHARSET_EBCDIC
122 static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
124 static const unsigned char keylabel[] = "key";
127 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
128 NULL, 0, key, keylen, 1);
132 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
133 * success 0 on failure.
135 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
136 unsigned char *iv, size_t ivlen)
138 #ifdef CHARSET_EBCDIC
139 static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
141 static const unsigned char ivlabel[] = "iv";
144 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
145 NULL, 0, iv, ivlen, 1);
148 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
149 const unsigned char *secret,
150 unsigned char *fin, size_t finlen)
152 #ifdef CHARSET_EBCDIC
153 static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
155 static const unsigned char finishedlabel[] = "finished";
158 return tls13_hkdf_expand(s, md, secret, finishedlabel,
159 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
163 * Given the previous secret |prevsecret| and a new input secret |insecret| of
164 * length |insecretlen|, generate a new secret and store it in the location
165 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
167 int tls13_generate_secret(SSL *s, const EVP_MD *md,
168 const unsigned char *prevsecret,
169 const unsigned char *insecret,
171 unsigned char *outsecret)
173 size_t mdlen, prevsecretlen;
176 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
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 unsigned char preextractsec[EVP_MAX_MD_SIZE];
185 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
186 ERR_R_INTERNAL_ERROR);
190 mdleni = EVP_MD_size(md);
191 /* Ensure cast to size_t is safe */
192 if (!ossl_assert(mdleni >= 0)) {
193 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
194 ERR_R_INTERNAL_ERROR);
197 mdlen = (size_t)mdleni;
199 if (insecret == NULL) {
200 insecret = default_zeros;
203 if (prevsecret == NULL) {
204 prevsecret = default_zeros;
207 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
208 unsigned char hash[EVP_MAX_MD_SIZE];
210 /* The pre-extract derive step uses a hash of no messages */
212 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
213 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
214 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
215 ERR_R_INTERNAL_ERROR);
216 EVP_MD_CTX_free(mctx);
217 EVP_PKEY_CTX_free(pctx);
220 EVP_MD_CTX_free(mctx);
222 /* Generate the pre-extract secret */
223 if (!tls13_hkdf_expand(s, md, prevsecret,
224 (unsigned char *)derived_secret_label,
225 sizeof(derived_secret_label) - 1, hash, mdlen,
226 preextractsec, mdlen, 1)) {
227 /* SSLfatal() already called */
228 EVP_PKEY_CTX_free(pctx);
232 prevsecret = preextractsec;
233 prevsecretlen = mdlen;
236 ret = EVP_PKEY_derive_init(pctx) <= 0
237 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
239 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
240 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
241 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
243 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
247 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
248 ERR_R_INTERNAL_ERROR);
250 EVP_PKEY_CTX_free(pctx);
251 if (prevsecret == preextractsec)
252 OPENSSL_cleanse(preextractsec, mdlen);
257 * Given an input secret |insecret| of length |insecretlen| generate the
258 * handshake secret. This requires the early secret to already have been
259 * generated. Returns 1 on success 0 on failure.
261 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
264 /* Calls SSLfatal() if required */
265 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
266 insecret, insecretlen,
267 (unsigned char *)&s->handshake_secret);
271 * Given the handshake secret |prev| of length |prevlen| generate the master
272 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
275 int tls13_generate_master_secret(SSL *s, unsigned char *out,
276 unsigned char *prev, size_t prevlen,
279 const EVP_MD *md = ssl_handshake_md(s);
281 *secret_size = EVP_MD_size(md);
282 /* Calls SSLfatal() if required */
283 return tls13_generate_secret(s, md, prev, NULL, 0, out);
287 * Generates the mac for the Finished message. Returns the length of the MAC or
290 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
293 const EVP_MD *md = ssl_handshake_md(s);
294 unsigned char hash[EVP_MAX_MD_SIZE];
295 size_t hashlen, ret = 0;
296 EVP_PKEY *key = NULL;
297 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
299 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
300 /* SSLfatal() already called */
304 if (str == s->method->ssl3_enc->server_finished_label) {
305 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
306 s->server_finished_secret, hashlen);
307 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
308 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
309 s->client_finished_secret, hashlen);
311 unsigned char finsecret[EVP_MAX_MD_SIZE];
313 if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
314 s->client_app_traffic_secret,
318 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
320 OPENSSL_cleanse(finsecret, sizeof(finsecret));
325 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
326 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
327 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
328 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
329 ERR_R_INTERNAL_ERROR);
336 EVP_MD_CTX_free(ctx);
341 * There isn't really a key block in TLSv1.3, but we still need this function
342 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
344 int tls13_setup_key_block(SSL *s)
349 s->session->cipher = s->s3->tmp.new_cipher;
350 if (!ssl_cipher_get_evp(s->session, &c, &hash, NULL, NULL, NULL, 0)) {
351 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
352 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
356 s->s3->tmp.new_sym_enc = c;
357 s->s3->tmp.new_hash = hash;
362 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
363 const EVP_CIPHER *ciph,
364 const unsigned char *insecret,
365 const unsigned char *hash,
366 const unsigned char *label,
367 size_t labellen, unsigned char *secret,
368 unsigned char *key, unsigned char *iv,
369 EVP_CIPHER_CTX *ciph_ctx)
371 size_t ivlen, keylen, taglen;
372 int hashleni = EVP_MD_size(md);
375 /* Ensure cast to size_t is safe */
376 if (!ossl_assert(hashleni >= 0)) {
377 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
381 hashlen = (size_t)hashleni;
383 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
384 secret, hashlen, 1)) {
385 /* SSLfatal() already called */
389 /* TODO(size_t): convert me */
390 keylen = EVP_CIPHER_key_length(ciph);
391 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
394 ivlen = EVP_CCM_TLS_IV_LEN;
395 if (s->s3->tmp.new_cipher != NULL) {
396 algenc = s->s3->tmp.new_cipher->algorithm_enc;
397 } else if (s->session->cipher != NULL) {
398 /* We've not selected a cipher yet - we must be doing early data */
399 algenc = s->session->cipher->algorithm_enc;
400 } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
401 /* We must be doing early data with out-of-band PSK */
402 algenc = s->psksession->cipher->algorithm_enc;
404 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
408 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
409 taglen = EVP_CCM8_TLS_TAG_LEN;
411 taglen = EVP_CCM_TLS_TAG_LEN;
413 ivlen = EVP_CIPHER_iv_length(ciph);
417 if (!tls13_derive_key(s, md, secret, key, keylen)
418 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
419 /* SSLfatal() already called */
423 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
424 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
425 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
427 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
428 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
436 int tls13_change_cipher_state(SSL *s, int which)
438 #ifdef CHARSET_EBCDIC
439 static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
440 static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
441 static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
442 static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
443 static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
444 static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
445 static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
446 static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
448 static const unsigned char client_early_traffic[] = "c e traffic";
449 static const unsigned char client_handshake_traffic[] = "c hs traffic";
450 static const unsigned char client_application_traffic[] = "c ap traffic";
451 static const unsigned char server_handshake_traffic[] = "s hs traffic";
452 static const unsigned char server_application_traffic[] = "s ap traffic";
453 static const unsigned char exporter_master_secret[] = "exp master";
454 static const unsigned char resumption_master_secret[] = "res master";
455 static const unsigned char early_exporter_master_secret[] = "e exp master";
458 unsigned char key[EVP_MAX_KEY_LENGTH];
459 unsigned char secret[EVP_MAX_MD_SIZE];
460 unsigned char hashval[EVP_MAX_MD_SIZE];
461 unsigned char *hash = hashval;
462 unsigned char *insecret;
463 unsigned char *finsecret = NULL;
464 const char *log_label = NULL;
465 EVP_CIPHER_CTX *ciph_ctx;
466 size_t finsecretlen = 0;
467 const unsigned char *label;
468 size_t labellen, hashlen = 0;
470 const EVP_MD *md = NULL;
471 const EVP_CIPHER *cipher = NULL;
472 #if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
473 ktls_crypto_info_t crypto_info;
478 if (which & SSL3_CC_READ) {
479 if (s->enc_read_ctx != NULL) {
480 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
482 s->enc_read_ctx = EVP_CIPHER_CTX_new();
483 if (s->enc_read_ctx == NULL) {
484 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
485 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
489 ciph_ctx = s->enc_read_ctx;
492 RECORD_LAYER_reset_read_sequence(&s->rlayer);
494 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
495 if (s->enc_write_ctx != NULL) {
496 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
498 s->enc_write_ctx = EVP_CIPHER_CTX_new();
499 if (s->enc_write_ctx == NULL) {
500 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
501 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
505 ciph_ctx = s->enc_write_ctx;
508 RECORD_LAYER_reset_write_sequence(&s->rlayer);
511 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
512 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
513 if (which & SSL3_CC_EARLY) {
514 EVP_MD_CTX *mdctx = NULL;
517 unsigned int hashlenui;
518 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
520 insecret = s->early_secret;
521 label = client_early_traffic;
522 labellen = sizeof(client_early_traffic) - 1;
523 log_label = CLIENT_EARLY_LABEL;
525 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
527 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
528 SSL_F_TLS13_CHANGE_CIPHER_STATE,
529 SSL_R_BAD_HANDSHAKE_LENGTH);
533 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
534 && s->max_early_data > 0
535 && s->session->ext.max_early_data == 0) {
537 * If we are attempting to send early data, and we've decided to
538 * actually do it but max_early_data in s->session is 0 then we
539 * must be using an external PSK.
541 if (!ossl_assert(s->psksession != NULL
542 && s->max_early_data ==
543 s->psksession->ext.max_early_data)) {
544 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
545 SSL_F_TLS13_CHANGE_CIPHER_STATE,
546 ERR_R_INTERNAL_ERROR);
549 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
551 if (sslcipher == NULL) {
552 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
553 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
558 * We need to calculate the handshake digest using the digest from
559 * the session. We haven't yet selected our ciphersuite so we can't
560 * use ssl_handshake_md().
562 mdctx = EVP_MD_CTX_new();
564 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
565 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
568 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
569 md = ssl_md(sslcipher->algorithm2);
570 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
571 || !EVP_DigestUpdate(mdctx, hdata, handlen)
572 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
573 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
574 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
575 EVP_MD_CTX_free(mdctx);
579 EVP_MD_CTX_free(mdctx);
581 if (!tls13_hkdf_expand(s, md, insecret,
582 early_exporter_master_secret,
583 sizeof(early_exporter_master_secret) - 1,
585 s->early_exporter_master_secret, hashlen,
587 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
588 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
592 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
593 s->early_exporter_master_secret, hashlen)) {
594 /* SSLfatal() already called */
597 } else if (which & SSL3_CC_HANDSHAKE) {
598 insecret = s->handshake_secret;
599 finsecret = s->client_finished_secret;
600 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
601 label = client_handshake_traffic;
602 labellen = sizeof(client_handshake_traffic) - 1;
603 log_label = CLIENT_HANDSHAKE_LABEL;
605 * The handshake hash used for the server read/client write handshake
606 * traffic secret is the same as the hash for the server
607 * write/client read handshake traffic secret. However, if we
608 * processed early data then we delay changing the server
609 * read/client write cipher state until later, and the handshake
610 * hashes have moved on. Therefore we use the value saved earlier
611 * when we did the server write/client read change cipher state.
613 hash = s->handshake_traffic_hash;
615 insecret = s->master_secret;
616 label = client_application_traffic;
617 labellen = sizeof(client_application_traffic) - 1;
618 log_label = CLIENT_APPLICATION_LABEL;
620 * For this we only use the handshake hashes up until the server
621 * Finished hash. We do not include the client's Finished, which is
622 * what ssl_handshake_hash() would give us. Instead we use the
623 * previously saved value.
625 hash = s->server_finished_hash;
628 /* Early data never applies to client-read/server-write */
629 if (which & SSL3_CC_HANDSHAKE) {
630 insecret = s->handshake_secret;
631 finsecret = s->server_finished_secret;
632 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
633 label = server_handshake_traffic;
634 labellen = sizeof(server_handshake_traffic) - 1;
635 log_label = SERVER_HANDSHAKE_LABEL;
637 insecret = s->master_secret;
638 label = server_application_traffic;
639 labellen = sizeof(server_application_traffic) - 1;
640 log_label = SERVER_APPLICATION_LABEL;
644 if (!(which & SSL3_CC_EARLY)) {
645 md = ssl_handshake_md(s);
646 cipher = s->s3->tmp.new_sym_enc;
647 if (!ssl3_digest_cached_records(s, 1)
648 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
649 /* SSLfatal() already called */;
655 * Save the hash of handshakes up to now for use when we calculate the
656 * client application traffic secret
658 if (label == server_application_traffic)
659 memcpy(s->server_finished_hash, hashval, hashlen);
661 if (label == server_handshake_traffic)
662 memcpy(s->handshake_traffic_hash, hashval, hashlen);
664 if (label == client_application_traffic) {
666 * We also create the resumption master secret, but this time use the
667 * hash for the whole handshake including the Client Finished
669 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
670 resumption_master_secret,
671 sizeof(resumption_master_secret) - 1,
672 hashval, hashlen, s->resumption_master_secret,
674 /* SSLfatal() already called */
679 /* check whether cipher is known */
680 if(!ossl_assert(cipher != NULL))
683 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
684 insecret, hash, label, labellen, secret, key,
686 /* SSLfatal() already called */
690 if (label == server_application_traffic) {
691 memcpy(s->server_app_traffic_secret, secret, hashlen);
692 /* Now we create the exporter master secret */
693 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
694 exporter_master_secret,
695 sizeof(exporter_master_secret) - 1,
696 hash, hashlen, s->exporter_master_secret,
698 /* SSLfatal() already called */
702 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
704 /* SSLfatal() already called */
707 } else if (label == client_application_traffic)
708 memcpy(s->client_app_traffic_secret, secret, hashlen);
710 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
711 /* SSLfatal() already called */
715 if (finsecret != NULL
716 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
717 finsecret, finsecretlen)) {
718 /* SSLfatal() already called */
722 if (!s->server && label == client_early_traffic)
723 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
725 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
726 #ifndef OPENSSL_NO_KTLS
727 # if defined(OPENSSL_KTLS_TLS13)
728 if (!(which & SSL3_CC_APPLICATION)
729 || (s->options & SSL_OP_ENABLE_KTLS) == 0)
732 /* ktls supports only the maximum fragment size */
733 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
736 /* ktls does not support record padding */
737 if (s->record_padding_cb != NULL)
740 /* check that cipher is supported */
741 if (!ktls_check_supported_cipher(s, cipher, ciph_ctx))
744 if (which & SSL3_CC_WRITE)
749 if (!ossl_assert(bio != NULL)) {
750 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_CHANGE_CIPHER_STATE,
751 ERR_R_INTERNAL_ERROR);
755 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
756 if (which & SSL3_CC_WRITE) {
757 if (BIO_flush(bio) <= 0)
761 /* configure kernel crypto structure */
762 if (which & SSL3_CC_WRITE)
763 rl_sequence = RECORD_LAYER_get_write_sequence(&s->rlayer);
765 rl_sequence = RECORD_LAYER_get_read_sequence(&s->rlayer);
767 if (!ktls_configure_crypto(s, cipher, ciph_ctx, rl_sequence, &crypto_info,
768 which & SSL3_CC_WRITE, iv, key, NULL, 0))
771 /* ktls works with user provided buffers directly */
772 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
773 if (which & SSL3_CC_WRITE)
774 ssl3_release_write_buffer(s);
781 OPENSSL_cleanse(key, sizeof(key));
782 OPENSSL_cleanse(secret, sizeof(secret));
786 int tls13_update_key(SSL *s, int sending)
788 #ifdef CHARSET_EBCDIC
789 static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
791 static const unsigned char application_traffic[] = "traffic upd";
793 const EVP_MD *md = ssl_handshake_md(s);
794 size_t hashlen = EVP_MD_size(md);
795 unsigned char key[EVP_MAX_KEY_LENGTH];
796 unsigned char *insecret, *iv;
797 unsigned char secret[EVP_MAX_MD_SIZE];
798 EVP_CIPHER_CTX *ciph_ctx;
801 if (s->server == sending)
802 insecret = s->server_app_traffic_secret;
804 insecret = s->client_app_traffic_secret;
807 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
809 ciph_ctx = s->enc_write_ctx;
810 RECORD_LAYER_reset_write_sequence(&s->rlayer);
813 ciph_ctx = s->enc_read_ctx;
814 RECORD_LAYER_reset_read_sequence(&s->rlayer);
817 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
818 s->s3->tmp.new_sym_enc, insecret, NULL,
820 sizeof(application_traffic) - 1, secret, key,
822 /* SSLfatal() already called */
826 memcpy(insecret, secret, hashlen);
828 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
831 OPENSSL_cleanse(key, sizeof(key));
832 OPENSSL_cleanse(secret, sizeof(secret));
836 int tls13_alert_code(int code)
838 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
839 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
842 return tls1_alert_code(code);
845 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
846 const char *label, size_t llen,
847 const unsigned char *context,
848 size_t contextlen, int use_context)
850 unsigned char exportsecret[EVP_MAX_MD_SIZE];
851 #ifdef CHARSET_EBCDIC
852 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
854 static const unsigned char exporterlabel[] = "exporter";
856 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
857 const EVP_MD *md = ssl_handshake_md(s);
858 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
859 unsigned int hashsize, datalen;
862 if (ctx == NULL || !ossl_statem_export_allowed(s))
868 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
869 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
870 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
871 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
872 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
873 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
874 (const unsigned char *)label, llen,
875 data, datalen, exportsecret, hashsize, 0)
876 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
877 sizeof(exporterlabel) - 1, hash, hashsize,
883 EVP_MD_CTX_free(ctx);
887 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
888 const char *label, size_t llen,
889 const unsigned char *context,
892 #ifdef CHARSET_EBCDIC
893 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
895 static const unsigned char exporterlabel[] = "exporter";
897 unsigned char exportsecret[EVP_MAX_MD_SIZE];
898 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
900 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
901 unsigned int hashsize, datalen;
903 const SSL_CIPHER *sslcipher;
905 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
908 if (!s->server && s->max_early_data > 0
909 && s->session->ext.max_early_data == 0)
910 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
912 sslcipher = SSL_SESSION_get0_cipher(s->session);
914 md = ssl_md(sslcipher->algorithm2);
917 * Calculate the hash value and store it in |data|. The reason why
918 * the empty string is used is that the definition of TLS-Exporter
921 * TLS-Exporter(label, context_value, key_length) =
922 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
923 * "exporter", Hash(context_value), key_length)
925 * Derive-Secret(Secret, Label, Messages) =
926 * HKDF-Expand-Label(Secret, Label,
927 * Transcript-Hash(Messages), Hash.length)
929 * Here Transcript-Hash is the cipher suite hash algorithm.
931 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
932 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
933 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
934 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
935 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
936 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
937 (const unsigned char *)label, llen,
938 data, datalen, exportsecret, hashsize, 0)
939 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
940 sizeof(exporterlabel) - 1, hash, hashsize,
946 EVP_MD_CTX_free(ctx);