2 * Copyright 2016-2018 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
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
16 #define TLS13_MAX_LABEL_LEN 246
18 /* Always filled with zeros */
19 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
22 * Given a |secret|; a |label| of length |labellen|; and |data| of length
23 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
24 * secret |outlen| bytes long and store it in the location pointed to be |out|.
25 * The |data| value may be zero length. Returns 1 on success 0 on failure.
27 int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
28 const unsigned char *label, size_t labellen,
29 const unsigned char *data, size_t datalen,
30 unsigned char *out, size_t outlen)
32 const unsigned char label_prefix[] = "tls13 ";
33 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
38 * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined
39 * prefix and label + bytes for the label itself + bytes for the hash
41 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
42 + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
49 hashlen = EVP_MD_size(md);
51 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
52 || !WPACKET_put_bytes_u16(&pkt, outlen)
53 || !WPACKET_start_sub_packet_u8(&pkt)
54 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
55 || !WPACKET_memcpy(&pkt, label, labellen)
56 || !WPACKET_close(&pkt)
57 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
58 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
59 || !WPACKET_finish(&pkt)) {
60 EVP_PKEY_CTX_free(pctx);
61 WPACKET_cleanup(&pkt);
62 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
63 ERR_R_INTERNAL_ERROR);
67 ret = EVP_PKEY_derive_init(pctx) <= 0
68 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
70 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
71 || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
72 || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
73 || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
75 EVP_PKEY_CTX_free(pctx);
78 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
79 ERR_R_INTERNAL_ERROR);
85 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
86 * success 0 on failure.
88 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
89 unsigned char *key, size_t keylen)
91 static const unsigned char keylabel[] = "key";
93 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
94 NULL, 0, key, keylen);
98 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
99 * success 0 on failure.
101 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
102 unsigned char *iv, size_t ivlen)
104 static const unsigned char ivlabel[] = "iv";
106 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
110 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
111 const unsigned char *secret,
112 unsigned char *fin, size_t finlen)
114 static const unsigned char finishedlabel[] = "finished";
116 return tls13_hkdf_expand(s, md, secret, finishedlabel,
117 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
121 * Given the previous secret |prevsecret| and a new input secret |insecret| of
122 * length |insecretlen|, generate a new secret and store it in the location
123 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
125 int tls13_generate_secret(SSL *s, const EVP_MD *md,
126 const unsigned char *prevsecret,
127 const unsigned char *insecret,
129 unsigned char *outsecret)
131 size_t mdlen, prevsecretlen;
134 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
135 static const char derived_secret_label[] = "derived";
136 unsigned char preextractsec[EVP_MAX_MD_SIZE];
139 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
140 ERR_R_INTERNAL_ERROR);
144 mdleni = EVP_MD_size(md);
145 /* Ensure cast to size_t is safe */
146 if (!ossl_assert(mdleni >= 0)) {
147 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
148 ERR_R_INTERNAL_ERROR);
151 mdlen = (size_t)mdleni;
153 if (insecret == NULL) {
154 insecret = default_zeros;
157 if (prevsecret == NULL) {
158 prevsecret = default_zeros;
161 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
162 unsigned char hash[EVP_MAX_MD_SIZE];
164 /* The pre-extract derive step uses a hash of no messages */
166 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
167 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
168 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
169 ERR_R_INTERNAL_ERROR);
170 EVP_MD_CTX_free(mctx);
171 EVP_PKEY_CTX_free(pctx);
174 EVP_MD_CTX_free(mctx);
176 /* Generate the pre-extract secret */
177 if (!tls13_hkdf_expand(s, md, prevsecret,
178 (unsigned char *)derived_secret_label,
179 sizeof(derived_secret_label) - 1, hash, mdlen,
180 preextractsec, mdlen)) {
181 /* SSLfatal() already called */
182 EVP_PKEY_CTX_free(pctx);
186 prevsecret = preextractsec;
187 prevsecretlen = mdlen;
190 ret = EVP_PKEY_derive_init(pctx) <= 0
191 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
193 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
194 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
195 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
197 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
201 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
202 ERR_R_INTERNAL_ERROR);
204 EVP_PKEY_CTX_free(pctx);
205 if (prevsecret == preextractsec)
206 OPENSSL_cleanse(preextractsec, mdlen);
211 * Given an input secret |insecret| of length |insecretlen| generate the
212 * handshake secret. This requires the early secret to already have been
213 * generated. Returns 1 on success 0 on failure.
215 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
218 /* Calls SSLfatal() if required */
219 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
220 insecret, insecretlen,
221 (unsigned char *)&s->handshake_secret);
225 * Given the handshake secret |prev| of length |prevlen| generate the master
226 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
229 int tls13_generate_master_secret(SSL *s, unsigned char *out,
230 unsigned char *prev, size_t prevlen,
233 const EVP_MD *md = ssl_handshake_md(s);
235 *secret_size = EVP_MD_size(md);
236 /* Calls SSLfatal() if required */
237 return tls13_generate_secret(s, md, prev, NULL, 0, out);
241 * Generates the mac for the Finished message. Returns the length of the MAC or
244 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
247 const EVP_MD *md = ssl_handshake_md(s);
248 unsigned char hash[EVP_MAX_MD_SIZE];
249 size_t hashlen, ret = 0;
250 EVP_PKEY *key = NULL;
251 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
253 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
254 /* SSLfatal() already called */
258 if (str == s->method->ssl3_enc->server_finished_label) {
259 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
260 s->server_finished_secret, hashlen);
261 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
262 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
263 s->client_finished_secret, hashlen);
265 unsigned char finsecret[EVP_MAX_MD_SIZE];
267 if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
268 s->client_app_traffic_secret,
272 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
274 OPENSSL_cleanse(finsecret, sizeof(finsecret));
279 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
280 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
281 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
282 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
283 ERR_R_INTERNAL_ERROR);
290 EVP_MD_CTX_free(ctx);
295 * There isn't really a key block in TLSv1.3, but we still need this function
296 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
298 int tls13_setup_key_block(SSL *s)
302 int mac_type = NID_undef;
304 s->session->cipher = s->s3->tmp.new_cipher;
305 if (!ssl_cipher_get_evp
306 (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
307 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
308 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
312 s->s3->tmp.new_sym_enc = c;
313 s->s3->tmp.new_hash = hash;
318 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
319 const EVP_CIPHER *ciph,
320 const unsigned char *insecret,
321 const unsigned char *hash,
322 const unsigned char *label,
323 size_t labellen, unsigned char *secret,
324 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
326 unsigned char key[EVP_MAX_KEY_LENGTH];
327 size_t ivlen, keylen, taglen;
328 int hashleni = EVP_MD_size(md);
331 /* Ensure cast to size_t is safe */
332 if (!ossl_assert(hashleni >= 0)) {
333 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
337 hashlen = (size_t)hashleni;
339 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
341 /* SSLfatal() already called */
345 /* TODO(size_t): convert me */
346 keylen = EVP_CIPHER_key_length(ciph);
347 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
350 ivlen = EVP_CCM_TLS_IV_LEN;
351 if (s->s3->tmp.new_cipher == NULL) {
352 /* We've not selected a cipher yet - we must be doing early data */
353 algenc = s->session->cipher->algorithm_enc;
355 algenc = s->s3->tmp.new_cipher->algorithm_enc;
357 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
358 taglen = EVP_CCM8_TLS_TAG_LEN;
360 taglen = EVP_CCM_TLS_TAG_LEN;
362 ivlen = EVP_CIPHER_iv_length(ciph);
366 if (!tls13_derive_key(s, md, secret, key, keylen)
367 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
368 /* SSLfatal() already called */
372 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
373 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
374 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
376 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
377 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
384 OPENSSL_cleanse(key, sizeof(key));
388 int tls13_change_cipher_state(SSL *s, int which)
390 static const unsigned char client_early_traffic[] = "c e traffic";
391 static const unsigned char client_handshake_traffic[] = "c hs traffic";
392 static const unsigned char client_application_traffic[] = "c ap traffic";
393 static const unsigned char server_handshake_traffic[] = "s hs traffic";
394 static const unsigned char server_application_traffic[] = "s ap traffic";
395 static const unsigned char exporter_master_secret[] = "exp master";
396 static const unsigned char resumption_master_secret[] = "res master";
397 static const unsigned char early_exporter_master_secret[] = "e exp master";
399 unsigned char secret[EVP_MAX_MD_SIZE];
400 unsigned char hashval[EVP_MAX_MD_SIZE];
401 unsigned char *hash = hashval;
402 unsigned char *insecret;
403 unsigned char *finsecret = NULL;
404 const char *log_label = NULL;
405 EVP_CIPHER_CTX *ciph_ctx;
406 size_t finsecretlen = 0;
407 const unsigned char *label;
408 size_t labellen, hashlen = 0;
410 const EVP_MD *md = NULL;
411 const EVP_CIPHER *cipher = NULL;
413 if (which & SSL3_CC_READ) {
414 if (s->enc_read_ctx != NULL) {
415 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
417 s->enc_read_ctx = EVP_CIPHER_CTX_new();
418 if (s->enc_read_ctx == NULL) {
419 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
420 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
424 ciph_ctx = s->enc_read_ctx;
427 RECORD_LAYER_reset_read_sequence(&s->rlayer);
429 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
430 if (s->enc_write_ctx != NULL) {
431 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
433 s->enc_write_ctx = EVP_CIPHER_CTX_new();
434 if (s->enc_write_ctx == NULL) {
435 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
436 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
440 ciph_ctx = s->enc_write_ctx;
443 RECORD_LAYER_reset_write_sequence(&s->rlayer);
446 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
447 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
448 if (which & SSL3_CC_EARLY) {
449 EVP_MD_CTX *mdctx = NULL;
452 unsigned int hashlenui;
453 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
455 insecret = s->early_secret;
456 label = client_early_traffic;
457 labellen = sizeof(client_early_traffic) - 1;
458 log_label = CLIENT_EARLY_LABEL;
460 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
462 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
463 SSL_F_TLS13_CHANGE_CIPHER_STATE,
464 SSL_R_BAD_HANDSHAKE_LENGTH);
468 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
469 && s->max_early_data > 0
470 && s->session->ext.max_early_data == 0) {
472 * If we are attempting to send early data, and we've decided to
473 * actually do it but max_early_data in s->session is 0 then we
474 * must be using an external PSK.
476 if (!ossl_assert(s->psksession != NULL
477 && s->max_early_data ==
478 s->psksession->ext.max_early_data)) {
479 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
480 SSL_F_TLS13_CHANGE_CIPHER_STATE,
481 ERR_R_INTERNAL_ERROR);
484 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
486 if (sslcipher == NULL) {
487 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
488 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
493 * We need to calculate the handshake digest using the digest from
494 * the session. We haven't yet selected our ciphersuite so we can't
495 * use ssl_handshake_md().
497 mdctx = EVP_MD_CTX_new();
499 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
500 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
503 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
504 md = ssl_md(sslcipher->algorithm2);
505 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
506 || !EVP_DigestUpdate(mdctx, hdata, handlen)
507 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
508 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
509 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
510 EVP_MD_CTX_free(mdctx);
514 EVP_MD_CTX_free(mdctx);
516 if (!tls13_hkdf_expand(s, md, insecret,
517 early_exporter_master_secret,
518 sizeof(early_exporter_master_secret) - 1,
520 s->early_exporter_master_secret, hashlen)) {
521 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
522 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
526 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
527 s->early_exporter_master_secret, hashlen)) {
528 /* SSLfatal() already called */
531 } else if (which & SSL3_CC_HANDSHAKE) {
532 insecret = s->handshake_secret;
533 finsecret = s->client_finished_secret;
534 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
535 label = client_handshake_traffic;
536 labellen = sizeof(client_handshake_traffic) - 1;
537 log_label = CLIENT_HANDSHAKE_LABEL;
539 * The handshake hash used for the server read/client write handshake
540 * traffic secret is the same as the hash for the server
541 * write/client read handshake traffic secret. However, if we
542 * processed early data then we delay changing the server
543 * read/client write cipher state until later, and the handshake
544 * hashes have moved on. Therefore we use the value saved earlier
545 * when we did the server write/client read change cipher state.
547 hash = s->handshake_traffic_hash;
549 insecret = s->master_secret;
550 label = client_application_traffic;
551 labellen = sizeof(client_application_traffic) - 1;
552 log_label = CLIENT_APPLICATION_LABEL;
554 * For this we only use the handshake hashes up until the server
555 * Finished hash. We do not include the client's Finished, which is
556 * what ssl_handshake_hash() would give us. Instead we use the
557 * previously saved value.
559 hash = s->server_finished_hash;
562 /* Early data never applies to client-read/server-write */
563 if (which & SSL3_CC_HANDSHAKE) {
564 insecret = s->handshake_secret;
565 finsecret = s->server_finished_secret;
566 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
567 label = server_handshake_traffic;
568 labellen = sizeof(server_handshake_traffic) - 1;
569 log_label = SERVER_HANDSHAKE_LABEL;
571 insecret = s->master_secret;
572 label = server_application_traffic;
573 labellen = sizeof(server_application_traffic) - 1;
574 log_label = SERVER_APPLICATION_LABEL;
578 if (!(which & SSL3_CC_EARLY)) {
579 md = ssl_handshake_md(s);
580 cipher = s->s3->tmp.new_sym_enc;
581 if (!ssl3_digest_cached_records(s, 1)
582 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
583 /* SSLfatal() already called */;
589 * Save the hash of handshakes up to now for use when we calculate the
590 * client application traffic secret
592 if (label == server_application_traffic)
593 memcpy(s->server_finished_hash, hashval, hashlen);
595 if (label == server_handshake_traffic)
596 memcpy(s->handshake_traffic_hash, hashval, hashlen);
598 if (label == client_application_traffic) {
600 * We also create the resumption master secret, but this time use the
601 * hash for the whole handshake including the Client Finished
603 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
604 resumption_master_secret,
605 sizeof(resumption_master_secret) - 1,
606 hashval, hashlen, s->resumption_master_secret,
608 /* SSLfatal() already called */
613 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
614 insecret, hash, label, labellen, secret, iv,
616 /* SSLfatal() already called */
620 if (label == server_application_traffic) {
621 memcpy(s->server_app_traffic_secret, secret, hashlen);
622 /* Now we create the exporter master secret */
623 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
624 exporter_master_secret,
625 sizeof(exporter_master_secret) - 1,
626 hash, hashlen, s->exporter_master_secret,
628 /* SSLfatal() already called */
632 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
634 /* SSLfatal() already called */
637 } else if (label == client_application_traffic)
638 memcpy(s->client_app_traffic_secret, secret, hashlen);
640 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
641 /* SSLfatal() already called */
645 if (finsecret != NULL
646 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
647 finsecret, finsecretlen)) {
648 /* SSLfatal() already called */
652 if (!s->server && label == client_early_traffic)
653 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
655 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
658 OPENSSL_cleanse(secret, sizeof(secret));
662 int tls13_update_key(SSL *s, int sending)
664 static const unsigned char application_traffic[] = "traffic upd";
665 const EVP_MD *md = ssl_handshake_md(s);
666 size_t hashlen = EVP_MD_size(md);
667 unsigned char *insecret, *iv;
668 unsigned char secret[EVP_MAX_MD_SIZE];
669 EVP_CIPHER_CTX *ciph_ctx;
672 if (s->server == sending)
673 insecret = s->server_app_traffic_secret;
675 insecret = s->client_app_traffic_secret;
678 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
680 ciph_ctx = s->enc_write_ctx;
681 RECORD_LAYER_reset_write_sequence(&s->rlayer);
684 ciph_ctx = s->enc_read_ctx;
685 RECORD_LAYER_reset_read_sequence(&s->rlayer);
688 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
689 s->s3->tmp.new_sym_enc, insecret, NULL,
691 sizeof(application_traffic) - 1, secret, iv,
693 /* SSLfatal() already called */
697 memcpy(insecret, secret, hashlen);
699 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
702 OPENSSL_cleanse(secret, sizeof(secret));
706 int tls13_alert_code(int code)
708 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
709 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
712 return tls1_alert_code(code);
715 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
716 const char *label, size_t llen,
717 const unsigned char *context,
718 size_t contextlen, int use_context)
720 unsigned char exportsecret[EVP_MAX_MD_SIZE];
721 static const unsigned char exporterlabel[] = "exporter";
722 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
723 const EVP_MD *md = ssl_handshake_md(s);
724 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
725 unsigned int hashsize, datalen;
728 if (ctx == NULL || !ossl_statem_export_allowed(s))
734 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
735 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
736 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
737 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
738 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
739 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
740 (const unsigned char *)label, llen,
741 data, datalen, exportsecret, hashsize)
742 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
743 sizeof(exporterlabel) - 1, hash, hashsize,
749 EVP_MD_CTX_free(ctx);
753 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
754 const char *label, size_t llen,
755 const unsigned char *context,
758 static const unsigned char exporterlabel[] = "exporter";
759 unsigned char exportsecret[EVP_MAX_MD_SIZE];
760 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
762 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
763 unsigned int hashsize, datalen;
765 const SSL_CIPHER *sslcipher;
767 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
770 if (!s->server && s->max_early_data > 0
771 && s->session->ext.max_early_data == 0)
772 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
774 sslcipher = SSL_SESSION_get0_cipher(s->session);
776 md = ssl_md(sslcipher->algorithm2);
779 * Calculate the hash value and store it in |data|. The reason why
780 * the empty string is used is that the definition of TLS-Exporter
783 * TLS-Exporter(label, context_value, key_length) =
784 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
785 * "exporter", Hash(context_value), key_length)
787 * Derive-Secret(Secret, Label, Messages) =
788 * HKDF-Expand-Label(Secret, Label,
789 * Transcript-Hash(Messages), Hash.length)
791 * Here Transcript-Hash is the cipher suite hash algorithm.
793 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
794 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
795 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
796 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
797 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
798 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
799 (const unsigned char *)label, llen,
800 data, datalen, exportsecret, hashsize)
801 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
802 sizeof(exporterlabel) - 1, hash, hashsize,
808 EVP_MD_CTX_free(ctx);