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;
133 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
134 static const char derived_secret_label[] = "derived";
135 unsigned char preextractsec[EVP_MAX_MD_SIZE];
138 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
139 ERR_R_INTERNAL_ERROR);
143 mdlen = EVP_MD_size(md);
145 if (insecret == NULL) {
146 insecret = default_zeros;
149 if (prevsecret == NULL) {
150 prevsecret = default_zeros;
153 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
154 unsigned char hash[EVP_MAX_MD_SIZE];
156 /* The pre-extract derive step uses a hash of no messages */
158 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
159 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
160 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
161 ERR_R_INTERNAL_ERROR);
162 EVP_MD_CTX_free(mctx);
163 EVP_PKEY_CTX_free(pctx);
166 EVP_MD_CTX_free(mctx);
168 /* Generate the pre-extract secret */
169 if (!tls13_hkdf_expand(s, md, prevsecret,
170 (unsigned char *)derived_secret_label,
171 sizeof(derived_secret_label) - 1, hash, mdlen,
172 preextractsec, mdlen)) {
173 /* SSLfatal() already called */
174 EVP_PKEY_CTX_free(pctx);
178 prevsecret = preextractsec;
179 prevsecretlen = mdlen;
182 ret = EVP_PKEY_derive_init(pctx) <= 0
183 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
185 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
186 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
187 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
189 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
193 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
194 ERR_R_INTERNAL_ERROR);
196 EVP_PKEY_CTX_free(pctx);
197 if (prevsecret == preextractsec)
198 OPENSSL_cleanse(preextractsec, mdlen);
203 * Given an input secret |insecret| of length |insecretlen| generate the
204 * handshake secret. This requires the early secret to already have been
205 * generated. Returns 1 on success 0 on failure.
207 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
210 /* Calls SSLfatal() if required */
211 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
212 insecret, insecretlen,
213 (unsigned char *)&s->handshake_secret);
217 * Given the handshake secret |prev| of length |prevlen| generate the master
218 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
221 int tls13_generate_master_secret(SSL *s, unsigned char *out,
222 unsigned char *prev, size_t prevlen,
225 const EVP_MD *md = ssl_handshake_md(s);
227 *secret_size = EVP_MD_size(md);
228 /* Calls SSLfatal() if required */
229 return tls13_generate_secret(s, md, prev, NULL, 0, out);
233 * Generates the mac for the Finished message. Returns the length of the MAC or
236 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
239 const EVP_MD *md = ssl_handshake_md(s);
240 unsigned char hash[EVP_MAX_MD_SIZE];
241 size_t hashlen, ret = 0;
242 EVP_PKEY *key = NULL;
243 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
245 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
246 /* SSLfatal() already called */
250 if (str == s->method->ssl3_enc->server_finished_label)
251 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
252 s->server_finished_secret, hashlen);
254 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
255 s->client_finished_secret, hashlen);
259 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
260 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
261 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
262 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
263 ERR_R_INTERNAL_ERROR);
270 EVP_MD_CTX_free(ctx);
275 * There isn't really a key block in TLSv1.3, but we still need this function
276 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
278 int tls13_setup_key_block(SSL *s)
282 int mac_type = NID_undef;
284 s->session->cipher = s->s3->tmp.new_cipher;
285 if (!ssl_cipher_get_evp
286 (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
287 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
288 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
292 s->s3->tmp.new_sym_enc = c;
293 s->s3->tmp.new_hash = hash;
298 static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
299 const EVP_CIPHER *ciph,
300 const unsigned char *insecret,
301 const unsigned char *hash,
302 const unsigned char *label,
303 size_t labellen, unsigned char *secret,
304 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
306 unsigned char key[EVP_MAX_KEY_LENGTH];
307 size_t ivlen, keylen, taglen;
308 size_t hashlen = EVP_MD_size(md);
310 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
312 /* SSLfatal() already called */
316 /* TODO(size_t): convert me */
317 keylen = EVP_CIPHER_key_length(ciph);
318 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
321 ivlen = EVP_CCM_TLS_IV_LEN;
322 if (s->s3->tmp.new_cipher == NULL) {
323 /* We've not selected a cipher yet - we must be doing early data */
324 algenc = s->session->cipher->algorithm_enc;
326 algenc = s->s3->tmp.new_cipher->algorithm_enc;
328 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
329 taglen = EVP_CCM8_TLS_TAG_LEN;
331 taglen = EVP_CCM_TLS_TAG_LEN;
333 ivlen = EVP_CIPHER_iv_length(ciph);
337 if (!tls13_derive_key(s, md, secret, key, keylen)
338 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
339 /* SSLfatal() already called */
343 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
344 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
345 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
347 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
348 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
355 OPENSSL_cleanse(key, sizeof(key));
359 int tls13_change_cipher_state(SSL *s, int which)
361 static const unsigned char client_early_traffic[] = "c e traffic";
362 static const unsigned char client_handshake_traffic[] = "c hs traffic";
363 static const unsigned char client_application_traffic[] = "c ap traffic";
364 static const unsigned char server_handshake_traffic[] = "s hs traffic";
365 static const unsigned char server_application_traffic[] = "s ap traffic";
366 static const unsigned char exporter_master_secret[] = "exp master";
367 static const unsigned char resumption_master_secret[] = "res master";
368 static const unsigned char early_exporter_master_secret[] = "e exp master";
370 unsigned char secret[EVP_MAX_MD_SIZE];
371 unsigned char hashval[EVP_MAX_MD_SIZE];
372 unsigned char *hash = hashval;
373 unsigned char *insecret;
374 unsigned char *finsecret = NULL;
375 const char *log_label = NULL;
376 EVP_CIPHER_CTX *ciph_ctx;
377 size_t finsecretlen = 0;
378 const unsigned char *label;
379 size_t labellen, hashlen = 0;
381 const EVP_MD *md = NULL;
382 const EVP_CIPHER *cipher = NULL;
384 if (which & SSL3_CC_READ) {
385 if (s->enc_read_ctx != NULL) {
386 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
388 s->enc_read_ctx = EVP_CIPHER_CTX_new();
389 if (s->enc_read_ctx == NULL) {
390 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
391 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
395 ciph_ctx = s->enc_read_ctx;
398 RECORD_LAYER_reset_read_sequence(&s->rlayer);
400 s->statem.invalid_enc_write_ctx = 1;
401 if (s->enc_write_ctx != NULL) {
402 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
404 s->enc_write_ctx = EVP_CIPHER_CTX_new();
405 if (s->enc_write_ctx == NULL) {
406 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
407 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
411 ciph_ctx = s->enc_write_ctx;
414 RECORD_LAYER_reset_write_sequence(&s->rlayer);
417 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
418 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
419 if (which & SSL3_CC_EARLY) {
420 EVP_MD_CTX *mdctx = NULL;
423 unsigned int hashlenui;
424 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
426 insecret = s->early_secret;
427 label = client_early_traffic;
428 labellen = sizeof(client_early_traffic) - 1;
429 log_label = CLIENT_EARLY_LABEL;
431 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
433 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
434 SSL_F_TLS13_CHANGE_CIPHER_STATE,
435 SSL_R_BAD_HANDSHAKE_LENGTH);
439 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
440 && s->max_early_data > 0
441 && s->session->ext.max_early_data == 0) {
443 * If we are attempting to send early data, and we've decided to
444 * actually do it but max_early_data in s->session is 0 then we
445 * must be using an external PSK.
447 if (!ossl_assert(s->psksession != NULL
448 && s->max_early_data ==
449 s->psksession->ext.max_early_data)) {
450 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
451 SSL_F_TLS13_CHANGE_CIPHER_STATE,
452 ERR_R_INTERNAL_ERROR);
455 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
457 if (sslcipher == NULL) {
458 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
459 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
464 * We need to calculate the handshake digest using the digest from
465 * the session. We haven't yet selected our ciphersuite so we can't
466 * use ssl_handshake_md().
468 mdctx = EVP_MD_CTX_new();
470 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
471 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
474 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
475 md = ssl_md(sslcipher->algorithm2);
476 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
477 || !EVP_DigestUpdate(mdctx, hdata, handlen)
478 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
479 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
480 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
481 EVP_MD_CTX_free(mdctx);
485 EVP_MD_CTX_free(mdctx);
487 if (!tls13_hkdf_expand(s, md, insecret,
488 early_exporter_master_secret,
489 sizeof(early_exporter_master_secret) - 1,
491 s->early_exporter_master_secret, hashlen)) {
492 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
493 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
496 } else if (which & SSL3_CC_HANDSHAKE) {
497 insecret = s->handshake_secret;
498 finsecret = s->client_finished_secret;
499 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
500 label = client_handshake_traffic;
501 labellen = sizeof(client_handshake_traffic) - 1;
502 log_label = CLIENT_HANDSHAKE_LABEL;
504 * The handshake hash used for the server read/client write handshake
505 * traffic secret is the same as the hash for the server
506 * write/client read handshake traffic secret. However, if we
507 * processed early data then we delay changing the server
508 * read/client write cipher state until later, and the handshake
509 * hashes have moved on. Therefore we use the value saved earlier
510 * when we did the server write/client read change cipher state.
512 hash = s->handshake_traffic_hash;
514 insecret = s->master_secret;
515 label = client_application_traffic;
516 labellen = sizeof(client_application_traffic) - 1;
517 log_label = CLIENT_APPLICATION_LABEL;
519 * For this we only use the handshake hashes up until the server
520 * Finished hash. We do not include the client's Finished, which is
521 * what ssl_handshake_hash() would give us. Instead we use the
522 * previously saved value.
524 hash = s->server_finished_hash;
527 /* Early data never applies to client-read/server-write */
528 if (which & SSL3_CC_HANDSHAKE) {
529 insecret = s->handshake_secret;
530 finsecret = s->server_finished_secret;
531 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
532 label = server_handshake_traffic;
533 labellen = sizeof(server_handshake_traffic) - 1;
534 log_label = SERVER_HANDSHAKE_LABEL;
536 insecret = s->master_secret;
537 label = server_application_traffic;
538 labellen = sizeof(server_application_traffic) - 1;
539 log_label = SERVER_APPLICATION_LABEL;
543 if (!(which & SSL3_CC_EARLY)) {
544 md = ssl_handshake_md(s);
545 cipher = s->s3->tmp.new_sym_enc;
546 if (!ssl3_digest_cached_records(s, 1)
547 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
548 /* SSLfatal() already called */;
554 * Save the hash of handshakes up to now for use when we calculate the
555 * client application traffic secret
557 if (label == server_application_traffic)
558 memcpy(s->server_finished_hash, hashval, hashlen);
560 if (label == server_handshake_traffic)
561 memcpy(s->handshake_traffic_hash, hashval, hashlen);
563 if (label == client_application_traffic) {
565 * We also create the resumption master secret, but this time use the
566 * hash for the whole handshake including the Client Finished
568 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
569 resumption_master_secret,
570 sizeof(resumption_master_secret) - 1,
571 hashval, hashlen, s->session->master_key,
573 /* SSLfatal() already called */
576 s->session->master_key_length = hashlen;
579 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
580 insecret, hash, label, labellen, secret, iv,
582 /* SSLfatal() already called */
586 if (label == server_application_traffic) {
587 memcpy(s->server_app_traffic_secret, secret, hashlen);
588 /* Now we create the exporter master secret */
589 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
590 exporter_master_secret,
591 sizeof(exporter_master_secret) - 1,
592 hash, hashlen, s->exporter_master_secret,
594 /* SSLfatal() already called */
597 } else if (label == client_application_traffic)
598 memcpy(s->client_app_traffic_secret, secret, hashlen);
600 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
601 /* SSLfatal() already called */
605 if (finsecret != NULL
606 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
607 finsecret, finsecretlen)) {
608 /* SSLfatal() already called */
612 s->statem.invalid_enc_write_ctx = 0;
615 OPENSSL_cleanse(secret, sizeof(secret));
619 int tls13_update_key(SSL *s, int sending)
621 static const unsigned char application_traffic[] = "traffic upd";
622 const EVP_MD *md = ssl_handshake_md(s);
623 size_t hashlen = EVP_MD_size(md);
624 unsigned char *insecret, *iv;
625 unsigned char secret[EVP_MAX_MD_SIZE];
626 EVP_CIPHER_CTX *ciph_ctx;
629 if (s->server == sending)
630 insecret = s->server_app_traffic_secret;
632 insecret = s->client_app_traffic_secret;
635 s->statem.invalid_enc_write_ctx = 1;
637 ciph_ctx = s->enc_write_ctx;
638 RECORD_LAYER_reset_write_sequence(&s->rlayer);
641 ciph_ctx = s->enc_read_ctx;
642 RECORD_LAYER_reset_read_sequence(&s->rlayer);
645 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
646 s->s3->tmp.new_sym_enc, insecret, NULL,
648 sizeof(application_traffic) - 1, secret, iv,
650 /* SSLfatal() already called */
654 memcpy(insecret, secret, hashlen);
656 s->statem.invalid_enc_write_ctx = 0;
659 OPENSSL_cleanse(secret, sizeof(secret));
663 int tls13_alert_code(int code)
665 if (code == SSL_AD_MISSING_EXTENSION)
668 return tls1_alert_code(code);
671 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
672 const char *label, size_t llen,
673 const unsigned char *context,
674 size_t contextlen, int use_context)
676 unsigned char exportsecret[EVP_MAX_MD_SIZE];
677 static const unsigned char exporterlabel[] = "exporter";
678 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
679 const EVP_MD *md = ssl_handshake_md(s);
680 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
681 unsigned int hashsize, datalen;
684 if (ctx == NULL || !ossl_statem_export_allowed(s))
690 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
691 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
692 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
693 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
694 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
695 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
696 (const unsigned char *)label, llen,
697 data, datalen, exportsecret, hashsize)
698 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
699 sizeof(exporterlabel) - 1, hash, hashsize,
705 EVP_MD_CTX_free(ctx);
709 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
710 const char *label, size_t llen,
711 const unsigned char *context,
714 static const unsigned char exporterlabel[] = "exporter";
715 unsigned char exportsecret[EVP_MAX_MD_SIZE];
716 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
718 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
719 unsigned int hashsize, datalen;
721 const SSL_CIPHER *sslcipher;
723 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
726 if (!s->server && s->max_early_data > 0
727 && s->session->ext.max_early_data == 0)
728 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
730 sslcipher = SSL_SESSION_get0_cipher(s->session);
732 md = ssl_md(sslcipher->algorithm2);
735 * Calculate the hash value and store it in |data|. The reason why
736 * the empty string is used is that the definition of TLS-Exporter
739 * TLS-Exporter(label, context_value, key_length) =
740 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
741 * "exporter", Hash(context_value), key_length)
743 * Derive-Secret(Secret, Label, Messages) =
744 * HKDF-Expand-Label(Secret, Label,
745 * Transcript-Hash(Messages), Hash.length)
747 * Here Transcript-Hash is the cipher suite hash algorithm.
749 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
750 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
751 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
752 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
753 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
754 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
755 (const unsigned char *)label, llen,
756 data, datalen, exportsecret, hashsize)
757 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
758 sizeof(exporterlabel) - 1, hash, hashsize,
764 EVP_MD_CTX_free(ctx);