]> git.ipfire.org Git - thirdparty/openssl.git/blob - ssl/tls13_enc.c
Fix a crash in SSLfatal due to invalid enc_write_ctx
[thirdparty/openssl.git] / ssl / tls13_enc.c
1 /*
2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
3 *
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
8 */
9
10 #include <stdlib.h>
11 #include "ssl_locl.h"
12 #include "internal/cryptlib.h"
13 #include <openssl/evp.h>
14 #include <openssl/kdf.h>
15
16 #define TLS13_MAX_LABEL_LEN 246
17
18 /* Always filled with zeros */
19 static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
20
21 /*
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.
26 */
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)
31 {
32 const unsigned char label_prefix[] = "tls13 ";
33 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
34 int ret;
35 size_t hkdflabellen;
36 size_t hashlen;
37 /*
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
40 */
41 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
42 + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
43 + EVP_MAX_MD_SIZE];
44 WPACKET pkt;
45
46 if (pctx == NULL)
47 return 0;
48
49 hashlen = EVP_MD_size(md);
50
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);
64 return 0;
65 }
66
67 ret = EVP_PKEY_derive_init(pctx) <= 0
68 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
69 <= 0
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;
74
75 EVP_PKEY_CTX_free(pctx);
76
77 if (ret != 0)
78 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
79 ERR_R_INTERNAL_ERROR);
80
81 return ret == 0;
82 }
83
84 /*
85 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
86 * success 0 on failure.
87 */
88 int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
89 unsigned char *key, size_t keylen)
90 {
91 static const unsigned char keylabel[] = "key";
92
93 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
94 NULL, 0, key, keylen);
95 }
96
97 /*
98 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
99 * success 0 on failure.
100 */
101 int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
102 unsigned char *iv, size_t ivlen)
103 {
104 static const unsigned char ivlabel[] = "iv";
105
106 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
107 NULL, 0, iv, ivlen);
108 }
109
110 int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
111 const unsigned char *secret,
112 unsigned char *fin, size_t finlen)
113 {
114 static const unsigned char finishedlabel[] = "finished";
115
116 return tls13_hkdf_expand(s, md, secret, finishedlabel,
117 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
118 }
119
120 /*
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.
124 */
125 int tls13_generate_secret(SSL *s, const EVP_MD *md,
126 const unsigned char *prevsecret,
127 const unsigned char *insecret,
128 size_t insecretlen,
129 unsigned char *outsecret)
130 {
131 size_t mdlen, prevsecretlen;
132 int ret;
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];
136
137 if (pctx == NULL) {
138 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
139 ERR_R_INTERNAL_ERROR);
140 return 0;
141 }
142
143 mdlen = EVP_MD_size(md);
144
145 if (insecret == NULL) {
146 insecret = default_zeros;
147 insecretlen = mdlen;
148 }
149 if (prevsecret == NULL) {
150 prevsecret = default_zeros;
151 prevsecretlen = 0;
152 } else {
153 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
154 unsigned char hash[EVP_MAX_MD_SIZE];
155
156 /* The pre-extract derive step uses a hash of no messages */
157 if (mctx == NULL
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);
164 return 0;
165 }
166 EVP_MD_CTX_free(mctx);
167
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);
175 return 0;
176 }
177
178 prevsecret = preextractsec;
179 prevsecretlen = mdlen;
180 }
181
182 ret = EVP_PKEY_derive_init(pctx) <= 0
183 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
184 <= 0
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)
188 <= 0
189 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
190 <= 0;
191
192 if (ret != 0)
193 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
194 ERR_R_INTERNAL_ERROR);
195
196 EVP_PKEY_CTX_free(pctx);
197 if (prevsecret == preextractsec)
198 OPENSSL_cleanse(preextractsec, mdlen);
199 return ret == 0;
200 }
201
202 /*
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.
206 */
207 int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
208 size_t insecretlen)
209 {
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);
214 }
215
216 /*
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
219 * failure.
220 */
221 int tls13_generate_master_secret(SSL *s, unsigned char *out,
222 unsigned char *prev, size_t prevlen,
223 size_t *secret_size)
224 {
225 const EVP_MD *md = ssl_handshake_md(s);
226
227 *secret_size = EVP_MD_size(md);
228 /* Calls SSLfatal() if required */
229 return tls13_generate_secret(s, md, prev, NULL, 0, out);
230 }
231
232 /*
233 * Generates the mac for the Finished message. Returns the length of the MAC or
234 * 0 on error.
235 */
236 size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
237 unsigned char *out)
238 {
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();
244
245 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
246 /* SSLfatal() already called */
247 goto err;
248 }
249
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);
253 else
254 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
255 s->client_finished_secret, hashlen);
256
257 if (key == NULL
258 || ctx == NULL
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);
264 goto err;
265 }
266
267 ret = hashlen;
268 err:
269 EVP_PKEY_free(key);
270 EVP_MD_CTX_free(ctx);
271 return ret;
272 }
273
274 /*
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.
277 */
278 int tls13_setup_key_block(SSL *s)
279 {
280 const EVP_CIPHER *c;
281 const EVP_MD *hash;
282 int mac_type = NID_undef;
283
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);
289 return 0;
290 }
291
292 s->s3->tmp.new_sym_enc = c;
293 s->s3->tmp.new_hash = hash;
294
295 return 1;
296 }
297
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)
305 {
306 unsigned char key[EVP_MAX_KEY_LENGTH];
307 size_t ivlen, keylen, taglen;
308 size_t hashlen = EVP_MD_size(md);
309
310 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
311 secret, hashlen)) {
312 /* SSLfatal() already called */
313 goto err;
314 }
315
316 /* TODO(size_t): convert me */
317 keylen = EVP_CIPHER_key_length(ciph);
318 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
319 uint32_t algenc;
320
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;
325 } else {
326 algenc = s->s3->tmp.new_cipher->algorithm_enc;
327 }
328 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
329 taglen = EVP_CCM8_TLS_TAG_LEN;
330 else
331 taglen = EVP_CCM_TLS_TAG_LEN;
332 } else {
333 ivlen = EVP_CIPHER_iv_length(ciph);
334 taglen = 0;
335 }
336
337 if (!tls13_derive_key(s, md, secret, key, keylen)
338 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
339 /* SSLfatal() already called */
340 goto err;
341 }
342
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,
346 taglen, NULL))
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,
349 ERR_R_EVP_LIB);
350 goto err;
351 }
352
353 return 1;
354 err:
355 OPENSSL_cleanse(key, sizeof(key));
356 return 0;
357 }
358
359 int tls13_change_cipher_state(SSL *s, int which)
360 {
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";
369 unsigned char *iv;
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;
380 int ret = 0;
381 const EVP_MD *md = NULL;
382 const EVP_CIPHER *cipher = NULL;
383
384 if (which & SSL3_CC_READ) {
385 if (s->enc_read_ctx != NULL) {
386 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
387 } else {
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);
392 goto err;
393 }
394 }
395 ciph_ctx = s->enc_read_ctx;
396 iv = s->read_iv;
397
398 RECORD_LAYER_reset_read_sequence(&s->rlayer);
399 } else {
400 s->statem.invalid_enc_write_ctx = 1;
401 if (s->enc_write_ctx != NULL) {
402 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
403 } else {
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);
408 goto err;
409 }
410 EVP_CIPHER_CTX_ctrl(s->enc_write_ctx, EVP_CTRL_SET_DRBG, 0, s->drbg);
411 }
412 ciph_ctx = s->enc_write_ctx;
413 iv = s->write_iv;
414
415 RECORD_LAYER_reset_write_sequence(&s->rlayer);
416 }
417
418 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
419 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
420 if (which & SSL3_CC_EARLY) {
421 EVP_MD_CTX *mdctx = NULL;
422 long handlen;
423 void *hdata;
424 unsigned int hashlenui;
425 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
426
427 insecret = s->early_secret;
428 label = client_early_traffic;
429 labellen = sizeof(client_early_traffic) - 1;
430 log_label = CLIENT_EARLY_LABEL;
431
432 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
433 if (handlen <= 0) {
434 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
435 SSL_F_TLS13_CHANGE_CIPHER_STATE,
436 SSL_R_BAD_HANDSHAKE_LENGTH);
437 goto err;
438 }
439
440 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
441 && s->max_early_data > 0
442 && s->session->ext.max_early_data == 0) {
443 /*
444 * If we are attempting to send early data, and we've decided to
445 * actually do it but max_early_data in s->session is 0 then we
446 * must be using an external PSK.
447 */
448 if (!ossl_assert(s->psksession != NULL
449 && s->max_early_data ==
450 s->psksession->ext.max_early_data)) {
451 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
452 SSL_F_TLS13_CHANGE_CIPHER_STATE,
453 ERR_R_INTERNAL_ERROR);
454 goto err;
455 }
456 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
457 }
458 if (sslcipher == NULL) {
459 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
460 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
461 goto err;
462 }
463
464 /*
465 * We need to calculate the handshake digest using the digest from
466 * the session. We haven't yet selected our ciphersuite so we can't
467 * use ssl_handshake_md().
468 */
469 mdctx = EVP_MD_CTX_new();
470 if (mdctx == NULL) {
471 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
472 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
473 goto err;
474 }
475 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
476 md = ssl_md(sslcipher->algorithm2);
477 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
478 || !EVP_DigestUpdate(mdctx, hdata, handlen)
479 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
480 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
481 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
482 EVP_MD_CTX_free(mdctx);
483 goto err;
484 }
485 hashlen = hashlenui;
486 EVP_MD_CTX_free(mdctx);
487
488 if (!tls13_hkdf_expand(s, md, insecret,
489 early_exporter_master_secret,
490 sizeof(early_exporter_master_secret) - 1,
491 hashval, hashlen,
492 s->early_exporter_master_secret, hashlen)) {
493 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
494 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
495 goto err;
496 }
497 } else if (which & SSL3_CC_HANDSHAKE) {
498 insecret = s->handshake_secret;
499 finsecret = s->client_finished_secret;
500 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
501 label = client_handshake_traffic;
502 labellen = sizeof(client_handshake_traffic) - 1;
503 log_label = CLIENT_HANDSHAKE_LABEL;
504 /*
505 * The handshake hash used for the server read/client write handshake
506 * traffic secret is the same as the hash for the server
507 * write/client read handshake traffic secret. However, if we
508 * processed early data then we delay changing the server
509 * read/client write cipher state until later, and the handshake
510 * hashes have moved on. Therefore we use the value saved earlier
511 * when we did the server write/client read change cipher state.
512 */
513 hash = s->handshake_traffic_hash;
514 } else {
515 insecret = s->master_secret;
516 label = client_application_traffic;
517 labellen = sizeof(client_application_traffic) - 1;
518 log_label = CLIENT_APPLICATION_LABEL;
519 /*
520 * For this we only use the handshake hashes up until the server
521 * Finished hash. We do not include the client's Finished, which is
522 * what ssl_handshake_hash() would give us. Instead we use the
523 * previously saved value.
524 */
525 hash = s->server_finished_hash;
526 }
527 } else {
528 /* Early data never applies to client-read/server-write */
529 if (which & SSL3_CC_HANDSHAKE) {
530 insecret = s->handshake_secret;
531 finsecret = s->server_finished_secret;
532 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
533 label = server_handshake_traffic;
534 labellen = sizeof(server_handshake_traffic) - 1;
535 log_label = SERVER_HANDSHAKE_LABEL;
536 } else {
537 insecret = s->master_secret;
538 label = server_application_traffic;
539 labellen = sizeof(server_application_traffic) - 1;
540 log_label = SERVER_APPLICATION_LABEL;
541 }
542 }
543
544 if (!(which & SSL3_CC_EARLY)) {
545 md = ssl_handshake_md(s);
546 cipher = s->s3->tmp.new_sym_enc;
547 if (!ssl3_digest_cached_records(s, 1)
548 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
549 /* SSLfatal() already called */;
550 goto err;
551 }
552 }
553
554 /*
555 * Save the hash of handshakes up to now for use when we calculate the
556 * client application traffic secret
557 */
558 if (label == server_application_traffic)
559 memcpy(s->server_finished_hash, hashval, hashlen);
560
561 if (label == server_handshake_traffic)
562 memcpy(s->handshake_traffic_hash, hashval, hashlen);
563
564 if (label == client_application_traffic) {
565 /*
566 * We also create the resumption master secret, but this time use the
567 * hash for the whole handshake including the Client Finished
568 */
569 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
570 resumption_master_secret,
571 sizeof(resumption_master_secret) - 1,
572 hashval, hashlen, s->session->master_key,
573 hashlen)) {
574 /* SSLfatal() already called */
575 goto err;
576 }
577 s->session->master_key_length = hashlen;
578 }
579
580 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
581 insecret, hash, label, labellen, secret, iv,
582 ciph_ctx)) {
583 /* SSLfatal() already called */
584 goto err;
585 }
586
587 if (label == server_application_traffic) {
588 memcpy(s->server_app_traffic_secret, secret, hashlen);
589 /* Now we create the exporter master secret */
590 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
591 exporter_master_secret,
592 sizeof(exporter_master_secret) - 1,
593 hash, hashlen, s->exporter_master_secret,
594 hashlen)) {
595 /* SSLfatal() already called */
596 goto err;
597 }
598 } else if (label == client_application_traffic)
599 memcpy(s->client_app_traffic_secret, secret, hashlen);
600
601 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
602 /* SSLfatal() already called */
603 goto err;
604 }
605
606 if (finsecret != NULL
607 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
608 finsecret, finsecretlen)) {
609 /* SSLfatal() already called */
610 goto err;
611 }
612
613 s->statem.invalid_enc_write_ctx = 0;
614 ret = 1;
615 err:
616 OPENSSL_cleanse(secret, sizeof(secret));
617 return ret;
618 }
619
620 int tls13_update_key(SSL *s, int sending)
621 {
622 static const unsigned char application_traffic[] = "traffic upd";
623 const EVP_MD *md = ssl_handshake_md(s);
624 size_t hashlen = EVP_MD_size(md);
625 unsigned char *insecret, *iv;
626 unsigned char secret[EVP_MAX_MD_SIZE];
627 EVP_CIPHER_CTX *ciph_ctx;
628 int ret = 0;
629
630 if (s->server == sending)
631 insecret = s->server_app_traffic_secret;
632 else
633 insecret = s->client_app_traffic_secret;
634
635 if (sending) {
636 s->statem.invalid_enc_write_ctx = 1;
637 iv = s->write_iv;
638 ciph_ctx = s->enc_write_ctx;
639 RECORD_LAYER_reset_write_sequence(&s->rlayer);
640 } else {
641 iv = s->read_iv;
642 ciph_ctx = s->enc_read_ctx;
643 RECORD_LAYER_reset_read_sequence(&s->rlayer);
644 }
645
646 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
647 s->s3->tmp.new_sym_enc, insecret, NULL,
648 application_traffic,
649 sizeof(application_traffic) - 1, secret, iv,
650 ciph_ctx)) {
651 /* SSLfatal() already called */
652 goto err;
653 }
654
655 memcpy(insecret, secret, hashlen);
656
657 s->statem.invalid_enc_write_ctx = 0;
658 ret = 1;
659 err:
660 OPENSSL_cleanse(secret, sizeof(secret));
661 return ret;
662 }
663
664 int tls13_alert_code(int code)
665 {
666 if (code == SSL_AD_MISSING_EXTENSION)
667 return code;
668
669 return tls1_alert_code(code);
670 }
671
672 int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
673 const char *label, size_t llen,
674 const unsigned char *context,
675 size_t contextlen, int use_context)
676 {
677 unsigned char exportsecret[EVP_MAX_MD_SIZE];
678 static const unsigned char exporterlabel[] = "exporter";
679 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
680 const EVP_MD *md = ssl_handshake_md(s);
681 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
682 unsigned int hashsize, datalen;
683 int ret = 0;
684
685 if (ctx == NULL || !ossl_statem_export_allowed(s))
686 goto err;
687
688 if (!use_context)
689 contextlen = 0;
690
691 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
692 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
693 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
694 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
695 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
696 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
697 (const unsigned char *)label, llen,
698 data, datalen, exportsecret, hashsize)
699 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
700 sizeof(exporterlabel) - 1, hash, hashsize,
701 out, olen))
702 goto err;
703
704 ret = 1;
705 err:
706 EVP_MD_CTX_free(ctx);
707 return ret;
708 }
709
710 int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
711 const char *label, size_t llen,
712 const unsigned char *context,
713 size_t contextlen)
714 {
715 static const unsigned char exporterlabel[] = "exporter";
716 unsigned char exportsecret[EVP_MAX_MD_SIZE];
717 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
718 const EVP_MD *md;
719 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
720 unsigned int hashsize, datalen;
721 int ret = 0;
722 const SSL_CIPHER *sslcipher;
723
724 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
725 goto err;
726
727 if (!s->server && s->max_early_data > 0
728 && s->session->ext.max_early_data == 0)
729 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
730 else
731 sslcipher = SSL_SESSION_get0_cipher(s->session);
732
733 md = ssl_md(sslcipher->algorithm2);
734
735 /*
736 * Calculate the hash value and store it in |data|. The reason why
737 * the empty string is used is that the definition of TLS-Exporter
738 * is like so:
739 *
740 * TLS-Exporter(label, context_value, key_length) =
741 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
742 * "exporter", Hash(context_value), key_length)
743 *
744 * Derive-Secret(Secret, Label, Messages) =
745 * HKDF-Expand-Label(Secret, Label,
746 * Transcript-Hash(Messages), Hash.length)
747 *
748 * Here Transcript-Hash is the cipher suite hash algorithm.
749 */
750 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
751 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
752 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
753 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
754 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
755 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
756 (const unsigned char *)label, llen,
757 data, datalen, exportsecret, hashsize)
758 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
759 sizeof(exporterlabel) - 1, hash, hashsize,
760 out, olen))
761 goto err;
762
763 ret = 1;
764 err:
765 EVP_MD_CTX_free(ctx);
766 return ret;
767 }