2 * Copyright 2016-2018 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
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
16 #define TLS13_MAX_LABEL_LEN 249
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. Any errors will be treated as fatal if
26 * |fatal| is set. Returns 1 on success 0 on failure.
28 int tls13_hkdf_expand(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
29 const unsigned char *label
, size_t labellen
,
30 const unsigned char *data
, size_t datalen
,
31 unsigned char *out
, size_t outlen
, int fatal
)
33 static const unsigned char label_prefix
[] = "tls13 ";
34 EVP_PKEY_CTX
*pctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF
, NULL
);
39 * 2 bytes for length of derived secret + 1 byte for length of combined
40 * prefix and label + bytes for the label itself + 1 byte length of hash
41 * + bytes for the hash itself
43 unsigned char hkdflabel
[sizeof(uint16_t) + sizeof(uint8_t) +
44 + sizeof(label_prefix
) + TLS13_MAX_LABEL_LEN
45 + 1 + EVP_MAX_MD_SIZE
];
51 if (labellen
> TLS13_MAX_LABEL_LEN
) {
53 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_HKDF_EXPAND
,
54 ERR_R_INTERNAL_ERROR
);
57 * Probably we have been called from SSL_export_keying_material(),
58 * or SSL_export_keying_material_early().
60 SSLerr(SSL_F_TLS13_HKDF_EXPAND
, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL
);
62 EVP_PKEY_CTX_free(pctx
);
66 hashlen
= EVP_MD_size(md
);
68 if (!WPACKET_init_static_len(&pkt
, hkdflabel
, sizeof(hkdflabel
), 0)
69 || !WPACKET_put_bytes_u16(&pkt
, outlen
)
70 || !WPACKET_start_sub_packet_u8(&pkt
)
71 || !WPACKET_memcpy(&pkt
, label_prefix
, sizeof(label_prefix
) - 1)
72 || !WPACKET_memcpy(&pkt
, label
, labellen
)
73 || !WPACKET_close(&pkt
)
74 || !WPACKET_sub_memcpy_u8(&pkt
, data
, (data
== NULL
) ? 0 : datalen
)
75 || !WPACKET_get_total_written(&pkt
, &hkdflabellen
)
76 || !WPACKET_finish(&pkt
)) {
77 EVP_PKEY_CTX_free(pctx
);
78 WPACKET_cleanup(&pkt
);
80 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_HKDF_EXPAND
,
81 ERR_R_INTERNAL_ERROR
);
83 SSLerr(SSL_F_TLS13_HKDF_EXPAND
, ERR_R_INTERNAL_ERROR
);
87 ret
= EVP_PKEY_derive_init(pctx
) <= 0
88 || EVP_PKEY_CTX_hkdf_mode(pctx
, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY
)
90 || EVP_PKEY_CTX_set_hkdf_md(pctx
, md
) <= 0
91 || EVP_PKEY_CTX_set1_hkdf_key(pctx
, secret
, hashlen
) <= 0
92 || EVP_PKEY_CTX_add1_hkdf_info(pctx
, hkdflabel
, hkdflabellen
) <= 0
93 || EVP_PKEY_derive(pctx
, out
, &outlen
) <= 0;
95 EVP_PKEY_CTX_free(pctx
);
99 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_HKDF_EXPAND
,
100 ERR_R_INTERNAL_ERROR
);
102 SSLerr(SSL_F_TLS13_HKDF_EXPAND
, 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
*s
, const EVP_MD
*md
, const unsigned char *secret
,
113 unsigned char *key
, size_t keylen
)
115 static const unsigned char keylabel
[] = "key";
117 return tls13_hkdf_expand(s
, md
, secret
, keylabel
, sizeof(keylabel
) - 1,
118 NULL
, 0, key
, keylen
, 1);
122 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
123 * success 0 on failure.
125 int tls13_derive_iv(SSL
*s
, const EVP_MD
*md
, const unsigned char *secret
,
126 unsigned char *iv
, size_t ivlen
)
128 static const unsigned char ivlabel
[] = "iv";
130 return tls13_hkdf_expand(s
, md
, secret
, ivlabel
, sizeof(ivlabel
) - 1,
131 NULL
, 0, iv
, ivlen
, 1);
134 int tls13_derive_finishedkey(SSL
*s
, const EVP_MD
*md
,
135 const unsigned char *secret
,
136 unsigned char *fin
, size_t finlen
)
138 static const unsigned char finishedlabel
[] = "finished";
140 return tls13_hkdf_expand(s
, md
, secret
, finishedlabel
,
141 sizeof(finishedlabel
) - 1, NULL
, 0, fin
, finlen
, 1);
145 * Given the previous secret |prevsecret| and a new input secret |insecret| of
146 * length |insecretlen|, generate a new secret and store it in the location
147 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
149 int tls13_generate_secret(SSL
*s
, const EVP_MD
*md
,
150 const unsigned char *prevsecret
,
151 const unsigned char *insecret
,
153 unsigned char *outsecret
)
155 size_t mdlen
, prevsecretlen
;
158 EVP_PKEY_CTX
*pctx
= EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF
, NULL
);
159 static const char derived_secret_label
[] = "derived";
160 unsigned char preextractsec
[EVP_MAX_MD_SIZE
];
163 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
164 ERR_R_INTERNAL_ERROR
);
168 mdleni
= EVP_MD_size(md
);
169 /* Ensure cast to size_t is safe */
170 if (!ossl_assert(mdleni
>= 0)) {
171 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
172 ERR_R_INTERNAL_ERROR
);
175 mdlen
= (size_t)mdleni
;
177 if (insecret
== NULL
) {
178 insecret
= default_zeros
;
181 if (prevsecret
== NULL
) {
182 prevsecret
= default_zeros
;
185 EVP_MD_CTX
*mctx
= EVP_MD_CTX_new();
186 unsigned char hash
[EVP_MAX_MD_SIZE
];
188 /* The pre-extract derive step uses a hash of no messages */
190 || EVP_DigestInit_ex(mctx
, md
, NULL
) <= 0
191 || EVP_DigestFinal_ex(mctx
, hash
, NULL
) <= 0) {
192 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
193 ERR_R_INTERNAL_ERROR
);
194 EVP_MD_CTX_free(mctx
);
195 EVP_PKEY_CTX_free(pctx
);
198 EVP_MD_CTX_free(mctx
);
200 /* Generate the pre-extract secret */
201 if (!tls13_hkdf_expand(s
, md
, prevsecret
,
202 (unsigned char *)derived_secret_label
,
203 sizeof(derived_secret_label
) - 1, hash
, mdlen
,
204 preextractsec
, mdlen
, 1)) {
205 /* SSLfatal() already called */
206 EVP_PKEY_CTX_free(pctx
);
210 prevsecret
= preextractsec
;
211 prevsecretlen
= mdlen
;
214 ret
= EVP_PKEY_derive_init(pctx
) <= 0
215 || EVP_PKEY_CTX_hkdf_mode(pctx
, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY
)
217 || EVP_PKEY_CTX_set_hkdf_md(pctx
, md
) <= 0
218 || EVP_PKEY_CTX_set1_hkdf_key(pctx
, insecret
, insecretlen
) <= 0
219 || EVP_PKEY_CTX_set1_hkdf_salt(pctx
, prevsecret
, prevsecretlen
)
221 || EVP_PKEY_derive(pctx
, outsecret
, &mdlen
)
225 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_GENERATE_SECRET
,
226 ERR_R_INTERNAL_ERROR
);
228 EVP_PKEY_CTX_free(pctx
);
229 if (prevsecret
== preextractsec
)
230 OPENSSL_cleanse(preextractsec
, mdlen
);
235 * Given an input secret |insecret| of length |insecretlen| generate the
236 * handshake secret. This requires the early secret to already have been
237 * generated. Returns 1 on success 0 on failure.
239 int tls13_generate_handshake_secret(SSL
*s
, const unsigned char *insecret
,
242 /* Calls SSLfatal() if required */
243 return tls13_generate_secret(s
, ssl_handshake_md(s
), s
->early_secret
,
244 insecret
, insecretlen
,
245 (unsigned char *)&s
->handshake_secret
);
249 * Given the handshake secret |prev| of length |prevlen| generate the master
250 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
253 int tls13_generate_master_secret(SSL
*s
, unsigned char *out
,
254 unsigned char *prev
, size_t prevlen
,
257 const EVP_MD
*md
= ssl_handshake_md(s
);
259 *secret_size
= EVP_MD_size(md
);
260 /* Calls SSLfatal() if required */
261 return tls13_generate_secret(s
, md
, prev
, NULL
, 0, out
);
265 * Generates the mac for the Finished message. Returns the length of the MAC or
268 size_t tls13_final_finish_mac(SSL
*s
, const char *str
, size_t slen
,
271 const EVP_MD
*md
= ssl_handshake_md(s
);
272 unsigned char hash
[EVP_MAX_MD_SIZE
];
273 size_t hashlen
, ret
= 0;
274 EVP_PKEY
*key
= NULL
;
275 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
277 if (!ssl_handshake_hash(s
, hash
, sizeof(hash
), &hashlen
)) {
278 /* SSLfatal() already called */
282 if (str
== s
->method
->ssl3_enc
->server_finished_label
) {
283 key
= EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC
, NULL
,
284 s
->server_finished_secret
, hashlen
);
285 } else if (SSL_IS_FIRST_HANDSHAKE(s
)) {
286 key
= EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC
, NULL
,
287 s
->client_finished_secret
, hashlen
);
289 unsigned char finsecret
[EVP_MAX_MD_SIZE
];
291 if (!tls13_derive_finishedkey(s
, ssl_handshake_md(s
),
292 s
->client_app_traffic_secret
,
296 key
= EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC
, NULL
, finsecret
,
298 OPENSSL_cleanse(finsecret
, sizeof(finsecret
));
303 || EVP_DigestSignInit(ctx
, NULL
, md
, NULL
, key
) <= 0
304 || EVP_DigestSignUpdate(ctx
, hash
, hashlen
) <= 0
305 || EVP_DigestSignFinal(ctx
, out
, &hashlen
) <= 0) {
306 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_FINAL_FINISH_MAC
,
307 ERR_R_INTERNAL_ERROR
);
314 EVP_MD_CTX_free(ctx
);
319 * There isn't really a key block in TLSv1.3, but we still need this function
320 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
322 int tls13_setup_key_block(SSL
*s
)
326 int mac_type
= NID_undef
;
328 s
->session
->cipher
= s
->s3
->tmp
.new_cipher
;
329 if (!ssl_cipher_get_evp
330 (s
->session
, &c
, &hash
, &mac_type
, NULL
, NULL
, 0)) {
331 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_TLS13_SETUP_KEY_BLOCK
,
332 SSL_R_CIPHER_OR_HASH_UNAVAILABLE
);
336 s
->s3
->tmp
.new_sym_enc
= c
;
337 s
->s3
->tmp
.new_hash
= hash
;
342 static int derive_secret_key_and_iv(SSL
*s
, int sending
, const EVP_MD
*md
,
343 const EVP_CIPHER
*ciph
,
344 const unsigned char *insecret
,
345 const unsigned char *hash
,
346 const unsigned char *label
,
347 size_t labellen
, unsigned char *secret
,
348 unsigned char *iv
, EVP_CIPHER_CTX
*ciph_ctx
)
350 unsigned char key
[EVP_MAX_KEY_LENGTH
];
351 size_t ivlen
, keylen
, taglen
;
352 int hashleni
= EVP_MD_size(md
);
355 /* Ensure cast to size_t is safe */
356 if (!ossl_assert(hashleni
>= 0)) {
357 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DERIVE_SECRET_KEY_AND_IV
,
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 /* TODO(size_t): convert me */
370 keylen
= EVP_CIPHER_key_length(ciph
);
371 if (EVP_CIPHER_mode(ciph
) == EVP_CIPH_CCM_MODE
) {
374 ivlen
= EVP_CCM_TLS_IV_LEN
;
375 if (s
->s3
->tmp
.new_cipher
== NULL
) {
376 /* We've not selected a cipher yet - we must be doing early data */
377 algenc
= s
->session
->cipher
->algorithm_enc
;
379 algenc
= s
->s3
->tmp
.new_cipher
->algorithm_enc
;
381 if (algenc
& (SSL_AES128CCM8
| SSL_AES256CCM8
))
382 taglen
= EVP_CCM8_TLS_TAG_LEN
;
384 taglen
= EVP_CCM_TLS_TAG_LEN
;
386 ivlen
= EVP_CIPHER_iv_length(ciph
);
390 if (!tls13_derive_key(s
, md
, secret
, key
, keylen
)
391 || !tls13_derive_iv(s
, md
, secret
, iv
, ivlen
)) {
392 /* SSLfatal() already called */
396 if (EVP_CipherInit_ex(ciph_ctx
, ciph
, NULL
, NULL
, NULL
, sending
) <= 0
397 || !EVP_CIPHER_CTX_ctrl(ciph_ctx
, EVP_CTRL_AEAD_SET_IVLEN
, ivlen
, NULL
)
398 || (taglen
!= 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx
, EVP_CTRL_AEAD_SET_TAG
,
400 || EVP_CipherInit_ex(ciph_ctx
, NULL
, NULL
, key
, NULL
, -1) <= 0) {
401 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
, SSL_F_DERIVE_SECRET_KEY_AND_IV
,
408 OPENSSL_cleanse(key
, sizeof(key
));
412 int tls13_change_cipher_state(SSL
*s
, int which
)
414 static const unsigned char client_early_traffic
[] = "c e traffic";
415 static const unsigned char client_handshake_traffic
[] = "c hs traffic";
416 static const unsigned char client_application_traffic
[] = "c ap traffic";
417 static const unsigned char server_handshake_traffic
[] = "s hs traffic";
418 static const unsigned char server_application_traffic
[] = "s ap traffic";
419 static const unsigned char exporter_master_secret
[] = "exp master";
420 static const unsigned char resumption_master_secret
[] = "res master";
421 static const unsigned char early_exporter_master_secret
[] = "e exp master";
423 unsigned char secret
[EVP_MAX_MD_SIZE
];
424 unsigned char hashval
[EVP_MAX_MD_SIZE
];
425 unsigned char *hash
= hashval
;
426 unsigned char *insecret
;
427 unsigned char *finsecret
= NULL
;
428 const char *log_label
= NULL
;
429 EVP_CIPHER_CTX
*ciph_ctx
;
430 size_t finsecretlen
= 0;
431 const unsigned char *label
;
432 size_t labellen
, hashlen
= 0;
434 const EVP_MD
*md
= NULL
;
435 const EVP_CIPHER
*cipher
= NULL
;
437 if (which
& SSL3_CC_READ
) {
438 if (s
->enc_read_ctx
!= NULL
) {
439 EVP_CIPHER_CTX_reset(s
->enc_read_ctx
);
441 s
->enc_read_ctx
= EVP_CIPHER_CTX_new();
442 if (s
->enc_read_ctx
== NULL
) {
443 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
444 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
448 ciph_ctx
= s
->enc_read_ctx
;
451 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
453 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
454 if (s
->enc_write_ctx
!= NULL
) {
455 EVP_CIPHER_CTX_reset(s
->enc_write_ctx
);
457 s
->enc_write_ctx
= EVP_CIPHER_CTX_new();
458 if (s
->enc_write_ctx
== NULL
) {
459 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
460 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
464 ciph_ctx
= s
->enc_write_ctx
;
467 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
470 if (((which
& SSL3_CC_CLIENT
) && (which
& SSL3_CC_WRITE
))
471 || ((which
& SSL3_CC_SERVER
) && (which
& SSL3_CC_READ
))) {
472 if (which
& SSL3_CC_EARLY
) {
473 EVP_MD_CTX
*mdctx
= NULL
;
476 unsigned int hashlenui
;
477 const SSL_CIPHER
*sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
479 insecret
= s
->early_secret
;
480 label
= client_early_traffic
;
481 labellen
= sizeof(client_early_traffic
) - 1;
482 log_label
= CLIENT_EARLY_LABEL
;
484 handlen
= BIO_get_mem_data(s
->s3
->handshake_buffer
, &hdata
);
486 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
487 SSL_F_TLS13_CHANGE_CIPHER_STATE
,
488 SSL_R_BAD_HANDSHAKE_LENGTH
);
492 if (s
->early_data_state
== SSL_EARLY_DATA_CONNECTING
493 && s
->max_early_data
> 0
494 && s
->session
->ext
.max_early_data
== 0) {
496 * If we are attempting to send early data, and we've decided to
497 * actually do it but max_early_data in s->session is 0 then we
498 * must be using an external PSK.
500 if (!ossl_assert(s
->psksession
!= NULL
501 && s
->max_early_data
==
502 s
->psksession
->ext
.max_early_data
)) {
503 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
504 SSL_F_TLS13_CHANGE_CIPHER_STATE
,
505 ERR_R_INTERNAL_ERROR
);
508 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
510 if (sslcipher
== NULL
) {
511 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
512 SSL_F_TLS13_CHANGE_CIPHER_STATE
, SSL_R_BAD_PSK
);
517 * We need to calculate the handshake digest using the digest from
518 * the session. We haven't yet selected our ciphersuite so we can't
519 * use ssl_handshake_md().
521 mdctx
= EVP_MD_CTX_new();
523 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
524 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_MALLOC_FAILURE
);
527 cipher
= EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher
));
528 md
= ssl_md(sslcipher
->algorithm2
);
529 if (md
== NULL
|| !EVP_DigestInit_ex(mdctx
, md
, NULL
)
530 || !EVP_DigestUpdate(mdctx
, hdata
, handlen
)
531 || !EVP_DigestFinal_ex(mdctx
, hashval
, &hashlenui
)) {
532 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
533 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
534 EVP_MD_CTX_free(mdctx
);
538 EVP_MD_CTX_free(mdctx
);
540 if (!tls13_hkdf_expand(s
, md
, insecret
,
541 early_exporter_master_secret
,
542 sizeof(early_exporter_master_secret
) - 1,
544 s
->early_exporter_master_secret
, hashlen
,
546 SSLfatal(s
, SSL_AD_INTERNAL_ERROR
,
547 SSL_F_TLS13_CHANGE_CIPHER_STATE
, ERR_R_INTERNAL_ERROR
);
551 if (!ssl_log_secret(s
, EARLY_EXPORTER_SECRET_LABEL
,
552 s
->early_exporter_master_secret
, hashlen
)) {
553 /* SSLfatal() already called */
556 } else if (which
& SSL3_CC_HANDSHAKE
) {
557 insecret
= s
->handshake_secret
;
558 finsecret
= s
->client_finished_secret
;
559 finsecretlen
= EVP_MD_size(ssl_handshake_md(s
));
560 label
= client_handshake_traffic
;
561 labellen
= sizeof(client_handshake_traffic
) - 1;
562 log_label
= CLIENT_HANDSHAKE_LABEL
;
564 * The handshake hash used for the server read/client write handshake
565 * traffic secret is the same as the hash for the server
566 * write/client read handshake traffic secret. However, if we
567 * processed early data then we delay changing the server
568 * read/client write cipher state until later, and the handshake
569 * hashes have moved on. Therefore we use the value saved earlier
570 * when we did the server write/client read change cipher state.
572 hash
= s
->handshake_traffic_hash
;
574 insecret
= s
->master_secret
;
575 label
= client_application_traffic
;
576 labellen
= sizeof(client_application_traffic
) - 1;
577 log_label
= CLIENT_APPLICATION_LABEL
;
579 * For this we only use the handshake hashes up until the server
580 * Finished hash. We do not include the client's Finished, which is
581 * what ssl_handshake_hash() would give us. Instead we use the
582 * previously saved value.
584 hash
= s
->server_finished_hash
;
587 /* Early data never applies to client-read/server-write */
588 if (which
& SSL3_CC_HANDSHAKE
) {
589 insecret
= s
->handshake_secret
;
590 finsecret
= s
->server_finished_secret
;
591 finsecretlen
= EVP_MD_size(ssl_handshake_md(s
));
592 label
= server_handshake_traffic
;
593 labellen
= sizeof(server_handshake_traffic
) - 1;
594 log_label
= SERVER_HANDSHAKE_LABEL
;
596 insecret
= s
->master_secret
;
597 label
= server_application_traffic
;
598 labellen
= sizeof(server_application_traffic
) - 1;
599 log_label
= SERVER_APPLICATION_LABEL
;
603 if (!(which
& SSL3_CC_EARLY
)) {
604 md
= ssl_handshake_md(s
);
605 cipher
= s
->s3
->tmp
.new_sym_enc
;
606 if (!ssl3_digest_cached_records(s
, 1)
607 || !ssl_handshake_hash(s
, hashval
, sizeof(hashval
), &hashlen
)) {
608 /* SSLfatal() already called */;
614 * Save the hash of handshakes up to now for use when we calculate the
615 * client application traffic secret
617 if (label
== server_application_traffic
)
618 memcpy(s
->server_finished_hash
, hashval
, hashlen
);
620 if (label
== server_handshake_traffic
)
621 memcpy(s
->handshake_traffic_hash
, hashval
, hashlen
);
623 if (label
== client_application_traffic
) {
625 * We also create the resumption master secret, but this time use the
626 * hash for the whole handshake including the Client Finished
628 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
629 resumption_master_secret
,
630 sizeof(resumption_master_secret
) - 1,
631 hashval
, hashlen
, s
->resumption_master_secret
,
633 /* SSLfatal() already called */
638 if (!derive_secret_key_and_iv(s
, which
& SSL3_CC_WRITE
, md
, cipher
,
639 insecret
, hash
, label
, labellen
, secret
, iv
,
641 /* SSLfatal() already called */
645 if (label
== server_application_traffic
) {
646 memcpy(s
->server_app_traffic_secret
, secret
, hashlen
);
647 /* Now we create the exporter master secret */
648 if (!tls13_hkdf_expand(s
, ssl_handshake_md(s
), insecret
,
649 exporter_master_secret
,
650 sizeof(exporter_master_secret
) - 1,
651 hash
, hashlen
, s
->exporter_master_secret
,
653 /* SSLfatal() already called */
657 if (!ssl_log_secret(s
, EXPORTER_SECRET_LABEL
, s
->exporter_master_secret
,
659 /* SSLfatal() already called */
662 } else if (label
== client_application_traffic
)
663 memcpy(s
->client_app_traffic_secret
, secret
, hashlen
);
665 if (!ssl_log_secret(s
, log_label
, secret
, hashlen
)) {
666 /* SSLfatal() already called */
670 if (finsecret
!= NULL
671 && !tls13_derive_finishedkey(s
, ssl_handshake_md(s
), secret
,
672 finsecret
, finsecretlen
)) {
673 /* SSLfatal() already called */
677 if (!s
->server
&& label
== client_early_traffic
)
678 s
->statem
.enc_write_state
= ENC_WRITE_STATE_WRITE_PLAIN_ALERTS
;
680 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
683 OPENSSL_cleanse(secret
, sizeof(secret
));
687 int tls13_update_key(SSL
*s
, int sending
)
689 static const unsigned char application_traffic
[] = "traffic upd";
690 const EVP_MD
*md
= ssl_handshake_md(s
);
691 size_t hashlen
= EVP_MD_size(md
);
692 unsigned char *insecret
, *iv
;
693 unsigned char secret
[EVP_MAX_MD_SIZE
];
694 EVP_CIPHER_CTX
*ciph_ctx
;
697 if (s
->server
== sending
)
698 insecret
= s
->server_app_traffic_secret
;
700 insecret
= s
->client_app_traffic_secret
;
703 s
->statem
.enc_write_state
= ENC_WRITE_STATE_INVALID
;
705 ciph_ctx
= s
->enc_write_ctx
;
706 RECORD_LAYER_reset_write_sequence(&s
->rlayer
);
709 ciph_ctx
= s
->enc_read_ctx
;
710 RECORD_LAYER_reset_read_sequence(&s
->rlayer
);
713 if (!derive_secret_key_and_iv(s
, sending
, ssl_handshake_md(s
),
714 s
->s3
->tmp
.new_sym_enc
, insecret
, NULL
,
716 sizeof(application_traffic
) - 1, secret
, iv
,
718 /* SSLfatal() already called */
722 memcpy(insecret
, secret
, hashlen
);
724 s
->statem
.enc_write_state
= ENC_WRITE_STATE_VALID
;
727 OPENSSL_cleanse(secret
, sizeof(secret
));
731 int tls13_alert_code(int code
)
733 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
734 if (code
== SSL_AD_MISSING_EXTENSION
|| code
== SSL_AD_CERTIFICATE_REQUIRED
)
737 return tls1_alert_code(code
);
740 int tls13_export_keying_material(SSL
*s
, unsigned char *out
, size_t olen
,
741 const char *label
, size_t llen
,
742 const unsigned char *context
,
743 size_t contextlen
, int use_context
)
745 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
746 static const unsigned char exporterlabel
[] = "exporter";
747 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
748 const EVP_MD
*md
= ssl_handshake_md(s
);
749 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
750 unsigned int hashsize
, datalen
;
753 if (ctx
== NULL
|| !ossl_statem_export_allowed(s
))
759 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
760 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
761 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
762 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
763 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
764 || !tls13_hkdf_expand(s
, md
, s
->exporter_master_secret
,
765 (const unsigned char *)label
, llen
,
766 data
, datalen
, exportsecret
, hashsize
, 0)
767 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
768 sizeof(exporterlabel
) - 1, hash
, hashsize
,
774 EVP_MD_CTX_free(ctx
);
778 int tls13_export_keying_material_early(SSL
*s
, unsigned char *out
, size_t olen
,
779 const char *label
, size_t llen
,
780 const unsigned char *context
,
783 static const unsigned char exporterlabel
[] = "exporter";
784 unsigned char exportsecret
[EVP_MAX_MD_SIZE
];
785 unsigned char hash
[EVP_MAX_MD_SIZE
], data
[EVP_MAX_MD_SIZE
];
787 EVP_MD_CTX
*ctx
= EVP_MD_CTX_new();
788 unsigned int hashsize
, datalen
;
790 const SSL_CIPHER
*sslcipher
;
792 if (ctx
== NULL
|| !ossl_statem_export_early_allowed(s
))
795 if (!s
->server
&& s
->max_early_data
> 0
796 && s
->session
->ext
.max_early_data
== 0)
797 sslcipher
= SSL_SESSION_get0_cipher(s
->psksession
);
799 sslcipher
= SSL_SESSION_get0_cipher(s
->session
);
801 md
= ssl_md(sslcipher
->algorithm2
);
804 * Calculate the hash value and store it in |data|. The reason why
805 * the empty string is used is that the definition of TLS-Exporter
808 * TLS-Exporter(label, context_value, key_length) =
809 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
810 * "exporter", Hash(context_value), key_length)
812 * Derive-Secret(Secret, Label, Messages) =
813 * HKDF-Expand-Label(Secret, Label,
814 * Transcript-Hash(Messages), Hash.length)
816 * Here Transcript-Hash is the cipher suite hash algorithm.
818 if (EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
819 || EVP_DigestUpdate(ctx
, context
, contextlen
) <= 0
820 || EVP_DigestFinal_ex(ctx
, hash
, &hashsize
) <= 0
821 || EVP_DigestInit_ex(ctx
, md
, NULL
) <= 0
822 || EVP_DigestFinal_ex(ctx
, data
, &datalen
) <= 0
823 || !tls13_hkdf_expand(s
, md
, s
->early_exporter_master_secret
,
824 (const unsigned char *)label
, llen
,
825 data
, datalen
, exportsecret
, hashsize
, 0)
826 || !tls13_hkdf_expand(s
, md
, exportsecret
, exporterlabel
,
827 sizeof(exporterlabel
) - 1, hash
, hashsize
,
833 EVP_MD_CTX_free(ctx
);