]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/tls13_enc.c
Convert remaining functions in statem_srvr.c to use SSLfatal()
[thirdparty/openssl.git] / ssl / tls13_enc.c
CommitLineData
34574f19
MC
1/*
2 * Copyright 2016 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"
08717544 12#include "internal/cryptlib.h"
34574f19
MC
13#include <openssl/evp.h>
14#include <openssl/kdf.h>
15
16#define TLS13_MAX_LABEL_LEN 246
17
18/* Always filled with zeros */
19static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
20
34574f19 21/*
a19ae67d
MC
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.
34574f19 26 */
ec15acb6 27int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
34574f19 28 const unsigned char *label, size_t labellen,
a19ae67d 29 const unsigned char *data, size_t datalen,
34574f19
MC
30 unsigned char *out, size_t outlen)
31{
17aa119e 32 const unsigned char label_prefix[] = "tls13 ";
34574f19
MC
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)
a19ae67d 57 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
34574f19
MC
58 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
59 || !WPACKET_finish(&pkt)) {
d49e23ec 60 EVP_PKEY_CTX_free(pctx);
34574f19 61 WPACKET_cleanup(&pkt);
f63a17d6
MC
62 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
63 ERR_R_INTERNAL_ERROR);
34574f19
MC
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
f63a17d6
MC
77 if (ret != 0)
78 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
79 ERR_R_INTERNAL_ERROR);
80
34574f19
MC
81 return ret == 0;
82}
83
34574f19 84/*
f5ca0b04
MC
85 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
86 * success 0 on failure.
34574f19 87 */
d49e23ec
MC
88int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
89 unsigned char *key, size_t keylen)
34574f19 90{
f5ca0b04
MC
91 static const unsigned char keylabel[] = "key";
92
d49e23ec 93 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
a19ae67d 94 NULL, 0, key, keylen);
34574f19
MC
95}
96
97/*
f5ca0b04
MC
98 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
99 * success 0 on failure.
34574f19 100 */
d49e23ec
MC
101int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
102 unsigned char *iv, size_t ivlen)
34574f19 103{
f5ca0b04
MC
104 static const unsigned char ivlabel[] = "iv";
105
d49e23ec 106 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
a19ae67d 107 NULL, 0, iv, ivlen);
34574f19
MC
108}
109
ec15acb6
MC
110int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
111 const unsigned char *secret,
112 unsigned char *fin, size_t finlen)
6484776f 113{
f5ca0b04
MC
114 static const unsigned char finishedlabel[] = "finished";
115
ec15acb6 116 return tls13_hkdf_expand(s, md, secret, finishedlabel,
a19ae67d 117 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
6484776f
MC
118}
119
34574f19
MC
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
f5ca0b04 123 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
34574f19 124 */
ec15acb6
MC
125int 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)
34574f19 130{
34574f19
MC
131 size_t mdlen, prevsecretlen;
132 int ret;
133 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
17aa119e 134 static const char derived_secret_label[] = "derived";
3e0458fb 135 unsigned char preextractsec[EVP_MAX_MD_SIZE];
34574f19 136
f63a17d6
MC
137 if (pctx == NULL) {
138 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
139 ERR_R_INTERNAL_ERROR);
34574f19 140 return 0;
f63a17d6 141 }
34574f19
MC
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 {
3e0458fb
MC
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) {
f63a17d6
MC
160 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
161 ERR_R_INTERNAL_ERROR);
3e0458fb 162 EVP_MD_CTX_free(mctx);
71d8c138 163 EVP_PKEY_CTX_free(pctx);
3e0458fb
MC
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,
a19ae67d 171 sizeof(derived_secret_label) - 1, hash, mdlen,
71d8c138 172 preextractsec, mdlen)) {
f63a17d6 173 /* SSLfatal() already called */
71d8c138 174 EVP_PKEY_CTX_free(pctx);
3e0458fb 175 return 0;
71d8c138 176 }
3e0458fb
MC
177
178 prevsecret = preextractsec;
34574f19
MC
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
f63a17d6
MC
192 if (ret != 0)
193 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
194 ERR_R_INTERNAL_ERROR);
195
34574f19 196 EVP_PKEY_CTX_free(pctx);
3e0458fb
MC
197 if (prevsecret == preextractsec)
198 OPENSSL_cleanse(preextractsec, mdlen);
34574f19
MC
199 return ret == 0;
200}
201
34574f19
MC
202/*
203 * Given an input secret |insecret| of length |insecretlen| generate the
204 * handshake secret. This requires the early secret to already have been
f5ca0b04 205 * generated. Returns 1 on success 0 on failure.
34574f19
MC
206 */
207int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
208 size_t insecretlen)
209{
f63a17d6 210 /* Calls SSLfatal() if required */
ec15acb6
MC
211 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
212 insecret, insecretlen,
34574f19
MC
213 (unsigned char *)&s->handshake_secret);
214}
215
216/*
217 * Given the handshake secret |prev| of length |prevlen| generate the master
f5ca0b04
MC
218 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
219 * failure.
34574f19
MC
220 */
221int tls13_generate_master_secret(SSL *s, unsigned char *out,
222 unsigned char *prev, size_t prevlen,
223 size_t *secret_size)
224{
ec15acb6
MC
225 const EVP_MD *md = ssl_handshake_md(s);
226
227 *secret_size = EVP_MD_size(md);
f63a17d6 228 /* Calls SSLfatal() if required */
ec15acb6 229 return tls13_generate_secret(s, md, prev, NULL, 0, out);
34574f19
MC
230}
231
92760c21 232/*
f5ca0b04
MC
233 * Generates the mac for the Finished message. Returns the length of the MAC or
234 * 0 on error.
92760c21
MC
235 */
236size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
237 unsigned char *out)
238{
6484776f
MC
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();
92760c21 244
6484776f
MC
245 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
246 goto err;
247
248 if (str == s->method->ssl3_enc->server_finished_label)
249 key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
250 s->server_finished_secret, hashlen);
251 else
252 key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
253 s->client_finished_secret, hashlen);
254
255 if (key == NULL
256 || ctx == NULL
257 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
258 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
259 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0)
260 goto err;
92760c21 261
6484776f
MC
262 ret = hashlen;
263 err:
264 EVP_PKEY_free(key);
265 EVP_MD_CTX_free(ctx);
266 return ret;
92760c21
MC
267}
268
269/*
270 * There isn't really a key block in TLSv1.3, but we still need this function
f5ca0b04 271 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
92760c21
MC
272 */
273int tls13_setup_key_block(SSL *s)
274{
275 const EVP_CIPHER *c;
276 const EVP_MD *hash;
277 int mac_type = NID_undef;
278
279 s->session->cipher = s->s3->tmp.new_cipher;
280 if (!ssl_cipher_get_evp
281 (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
f63a17d6
MC
282 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
283 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
92760c21
MC
284 return 0;
285 }
286
287 s->s3->tmp.new_sym_enc = c;
288 s->s3->tmp.new_hash = hash;
289
290 return 1;
291}
292
d1186c30 293static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
d49e23ec 294 const EVP_CIPHER *ciph,
57389a32
MC
295 const unsigned char *insecret,
296 const unsigned char *hash,
297 const unsigned char *label,
298 size_t labellen, unsigned char *secret,
299 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
300{
301 unsigned char key[EVP_MAX_KEY_LENGTH];
302 size_t ivlen, keylen, taglen;
57389a32 303 size_t hashlen = EVP_MD_size(md);
57389a32 304
a19ae67d
MC
305 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
306 secret, hashlen)) {
f63a17d6 307 /* SSLfatal() already called */
57389a32
MC
308 goto err;
309 }
310
311 /* TODO(size_t): convert me */
312 keylen = EVP_CIPHER_key_length(ciph);
313 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
c117af67
MC
314 uint32_t algenc;
315
57389a32 316 ivlen = EVP_CCM_TLS_IV_LEN;
c117af67
MC
317 if (s->s3->tmp.new_cipher == NULL) {
318 /* We've not selected a cipher yet - we must be doing early data */
319 algenc = s->session->cipher->algorithm_enc;
320 } else {
321 algenc = s->s3->tmp.new_cipher->algorithm_enc;
322 }
323 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
57389a32
MC
324 taglen = EVP_CCM8_TLS_TAG_LEN;
325 else
326 taglen = EVP_CCM_TLS_TAG_LEN;
327 } else {
328 ivlen = EVP_CIPHER_iv_length(ciph);
329 taglen = 0;
330 }
331
d49e23ec
MC
332 if (!tls13_derive_key(s, md, secret, key, keylen)
333 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
f63a17d6 334 /* SSLfatal() already called */
57389a32
MC
335 goto err;
336 }
337
d1186c30 338 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
57389a32
MC
339 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
340 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
341 taglen, NULL))
342 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
f63a17d6
MC
343 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
344 ERR_R_EVP_LIB);
57389a32
MC
345 goto err;
346 }
347
57389a32
MC
348 return 1;
349 err:
350 OPENSSL_cleanse(key, sizeof(key));
351 return 0;
352}
353
0d9824c1
MC
354int tls13_change_cipher_state(SSL *s, int which)
355{
17aa119e
MC
356 static const unsigned char client_early_traffic[] = "c e traffic";
357 static const unsigned char client_handshake_traffic[] = "c hs traffic";
358 static const unsigned char client_application_traffic[] = "c ap traffic";
359 static const unsigned char server_handshake_traffic[] = "s hs traffic";
360 static const unsigned char server_application_traffic[] = "s ap traffic";
0ca8d1ec 361 static const unsigned char exporter_master_secret[] = "exp master";
17aa119e 362 static const unsigned char resumption_master_secret[] = "res master";
bebc0c7d 363 unsigned char *iv;
0d9824c1 364 unsigned char secret[EVP_MAX_MD_SIZE];
ace081c1
MC
365 unsigned char hashval[EVP_MAX_MD_SIZE];
366 unsigned char *hash = hashval;
0d9824c1 367 unsigned char *insecret;
6484776f 368 unsigned char *finsecret = NULL;
2c7bd692 369 const char *log_label = NULL;
0d9824c1 370 EVP_CIPHER_CTX *ciph_ctx;
57389a32 371 size_t finsecretlen = 0;
0d9824c1 372 const unsigned char *label;
ace081c1 373 size_t labellen, hashlen = 0;
6530c490 374 int ret = 0;
42f50fdf
MC
375 const EVP_MD *md = NULL;
376 const EVP_CIPHER *cipher = NULL;
0d9824c1
MC
377
378 if (which & SSL3_CC_READ) {
379 if (s->enc_read_ctx != NULL) {
380 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
381 } else {
382 s->enc_read_ctx = EVP_CIPHER_CTX_new();
383 if (s->enc_read_ctx == NULL) {
f63a17d6
MC
384 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
385 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
0d9824c1
MC
386 goto err;
387 }
388 }
389 ciph_ctx = s->enc_read_ctx;
bebc0c7d 390 iv = s->read_iv;
0d9824c1
MC
391
392 RECORD_LAYER_reset_read_sequence(&s->rlayer);
393 } else {
394 if (s->enc_write_ctx != NULL) {
395 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
396 } else {
397 s->enc_write_ctx = EVP_CIPHER_CTX_new();
398 if (s->enc_write_ctx == NULL) {
f63a17d6
MC
399 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
400 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
0d9824c1
MC
401 goto err;
402 }
403 }
404 ciph_ctx = s->enc_write_ctx;
bebc0c7d 405 iv = s->write_iv;
0d9824c1
MC
406
407 RECORD_LAYER_reset_write_sequence(&s->rlayer);
408 }
409
410 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
411 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
d49e23ec
MC
412 if (which & SSL3_CC_EARLY) {
413 EVP_MD_CTX *mdctx = NULL;
414 long handlen;
415 void *hdata;
416 unsigned int hashlenui;
417 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
418
419 insecret = s->early_secret;
420 label = client_early_traffic;
421 labellen = sizeof(client_early_traffic) - 1;
422 log_label = CLIENT_EARLY_LABEL;
423
424 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
425 if (handlen <= 0) {
f63a17d6
MC
426 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
427 SSL_F_TLS13_CHANGE_CIPHER_STATE,
428 SSL_R_BAD_HANDSHAKE_LENGTH);
d49e23ec
MC
429 goto err;
430 }
add8d0e9 431
08717544
MC
432 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
433 && s->max_early_data > 0
434 && s->session->ext.max_early_data == 0) {
435 /*
436 * If we are attempting to send early data, and we've decided to
437 * actually do it but max_early_data in s->session is 0 then we
438 * must be using an external PSK.
439 */
440 if (!ossl_assert(s->psksession != NULL
441 && s->max_early_data ==
442 s->psksession->ext.max_early_data)) {
f63a17d6
MC
443 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
444 SSL_F_TLS13_CHANGE_CIPHER_STATE,
445 ERR_R_INTERNAL_ERROR);
08717544
MC
446 goto err;
447 }
add8d0e9 448 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
08717544 449 }
d49e23ec 450 if (sslcipher == NULL) {
f63a17d6
MC
451 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
452 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
d49e23ec
MC
453 goto err;
454 }
455
456 /*
457 * We need to calculate the handshake digest using the digest from
458 * the session. We haven't yet selected our ciphersuite so we can't
459 * use ssl_handshake_md().
460 */
461 mdctx = EVP_MD_CTX_new();
462 if (mdctx == NULL) {
f63a17d6
MC
463 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
464 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
d49e23ec
MC
465 goto err;
466 }
467 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
468 md = ssl_md(sslcipher->algorithm2);
469 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
470 || !EVP_DigestUpdate(mdctx, hdata, handlen)
471 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
f63a17d6
MC
472 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
473 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
d49e23ec
MC
474 EVP_MD_CTX_free(mdctx);
475 goto err;
476 }
477 hashlen = hashlenui;
478 EVP_MD_CTX_free(mdctx);
479 } else if (which & SSL3_CC_HANDSHAKE) {
0d9824c1 480 insecret = s->handshake_secret;
6484776f 481 finsecret = s->client_finished_secret;
6612d87b 482 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
0d9824c1
MC
483 label = client_handshake_traffic;
484 labellen = sizeof(client_handshake_traffic) - 1;
2c7bd692 485 log_label = CLIENT_HANDSHAKE_LABEL;
fe5e20fd 486 /*
69687aa8 487 * The handshake hash used for the server read/client write handshake
f7e393be
MC
488 * traffic secret is the same as the hash for the server
489 * write/client read handshake traffic secret. However, if we
490 * processed early data then we delay changing the server
491 * read/client write cipher state until later, and the handshake
492 * hashes have moved on. Therefore we use the value saved earlier
493 * when we did the server write/client read change cipher state.
fe5e20fd 494 */
f7e393be 495 hash = s->handshake_traffic_hash;
0d9824c1 496 } else {
ec15acb6 497 insecret = s->master_secret;
0d9824c1
MC
498 label = client_application_traffic;
499 labellen = sizeof(client_application_traffic) - 1;
2c7bd692 500 log_label = CLIENT_APPLICATION_LABEL;
ace081c1
MC
501 /*
502 * For this we only use the handshake hashes up until the server
503 * Finished hash. We do not include the client's Finished, which is
504 * what ssl_handshake_hash() would give us. Instead we use the
505 * previously saved value.
506 */
507 hash = s->server_finished_hash;
0d9824c1
MC
508 }
509 } else {
d49e23ec 510 /* Early data never applies to client-read/server-write */
0d9824c1
MC
511 if (which & SSL3_CC_HANDSHAKE) {
512 insecret = s->handshake_secret;
6484776f 513 finsecret = s->server_finished_secret;
6612d87b 514 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
0d9824c1
MC
515 label = server_handshake_traffic;
516 labellen = sizeof(server_handshake_traffic) - 1;
2c7bd692 517 log_label = SERVER_HANDSHAKE_LABEL;
0d9824c1 518 } else {
ec15acb6 519 insecret = s->master_secret;
0d9824c1
MC
520 label = server_application_traffic;
521 labellen = sizeof(server_application_traffic) - 1;
2c7bd692 522 log_label = SERVER_APPLICATION_LABEL;
0d9824c1
MC
523 }
524 }
525
d49e23ec
MC
526 if (!(which & SSL3_CC_EARLY)) {
527 md = ssl_handshake_md(s);
528 cipher = s->s3->tmp.new_sym_enc;
529 if (!ssl3_digest_cached_records(s, 1)
530 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
f63a17d6 531 /* SSLfatal() already called */;
d49e23ec
MC
532 goto err;
533 }
ace081c1
MC
534 }
535
ec15acb6
MC
536 /*
537 * Save the hash of handshakes up to now for use when we calculate the
538 * client application traffic secret
539 */
540 if (label == server_application_traffic)
541 memcpy(s->server_finished_hash, hashval, hashlen);
542
f7e393be 543 if (label == server_handshake_traffic)
fe5e20fd
MC
544 memcpy(s->handshake_traffic_hash, hashval, hashlen);
545
ec15acb6
MC
546 if (label == client_application_traffic) {
547 /*
548 * We also create the resumption master secret, but this time use the
549 * hash for the whole handshake including the Client Finished
550 */
551 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
552 resumption_master_secret,
553 sizeof(resumption_master_secret) - 1,
a19ae67d
MC
554 hashval, hashlen, s->session->master_key,
555 hashlen)) {
f63a17d6 556 /* SSLfatal() already called */
ec15acb6
MC
557 goto err;
558 }
559 s->session->master_key_length = hashlen;
0ca8d1ec
MC
560
561 /* Now we create the exporter master secret */
562 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
563 exporter_master_secret,
564 sizeof(exporter_master_secret) - 1,
a19ae67d
MC
565 hash, hashlen, s->exporter_master_secret,
566 hashlen)) {
f63a17d6 567 /* SSLfatal() already called */
0ca8d1ec
MC
568 goto err;
569 }
ec15acb6
MC
570 }
571
d49e23ec
MC
572 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
573 insecret, hash, label, labellen, secret, iv,
574 ciph_ctx)) {
f63a17d6 575 /* SSLfatal() already called */
57389a32 576 goto err;
ec07b1d8 577 }
0d9824c1 578
57389a32
MC
579 if (label == server_application_traffic)
580 memcpy(s->server_app_traffic_secret, secret, hashlen);
581 else if (label == client_application_traffic)
582 memcpy(s->client_app_traffic_secret, secret, hashlen);
583
2c7bd692 584 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
f63a17d6 585 /* SSLfatal() already called */
2c7bd692
CB
586 goto err;
587 }
588
57389a32
MC
589 if (finsecret != NULL
590 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
591 finsecret, finsecretlen)) {
f63a17d6 592 /* SSLfatal() already called */
0d9824c1
MC
593 goto err;
594 }
595
57389a32
MC
596 ret = 1;
597 err:
598 OPENSSL_cleanse(secret, sizeof(secret));
599 return ret;
600}
0d9824c1 601
d1186c30 602int tls13_update_key(SSL *s, int sending)
57389a32 603{
17aa119e 604 static const unsigned char application_traffic[] = "traffic upd";
57389a32
MC
605 const EVP_MD *md = ssl_handshake_md(s);
606 size_t hashlen = EVP_MD_size(md);
607 unsigned char *insecret, *iv;
608 unsigned char secret[EVP_MAX_MD_SIZE];
609 EVP_CIPHER_CTX *ciph_ctx;
610 int ret = 0;
0d9824c1 611
d1186c30 612 if (s->server == sending)
57389a32
MC
613 insecret = s->server_app_traffic_secret;
614 else
615 insecret = s->client_app_traffic_secret;
bebc0c7d 616
d1186c30 617 if (sending) {
57389a32
MC
618 iv = s->write_iv;
619 ciph_ctx = s->enc_write_ctx;
620 RECORD_LAYER_reset_write_sequence(&s->rlayer);
621 } else {
622 iv = s->read_iv;
623 ciph_ctx = s->enc_read_ctx;
624 RECORD_LAYER_reset_read_sequence(&s->rlayer);
0d9824c1 625 }
57389a32 626
d1186c30 627 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
d49e23ec
MC
628 s->s3->tmp.new_sym_enc, insecret, NULL,
629 application_traffic,
57389a32 630 sizeof(application_traffic) - 1, secret, iv,
f63a17d6
MC
631 ciph_ctx)) {
632 /* SSLfatal() already called */
57389a32 633 goto err;
f63a17d6 634 }
57389a32
MC
635
636 memcpy(insecret, secret, hashlen);
0d9824c1 637
6530c490 638 ret = 1;
0d9824c1
MC
639 err:
640 OPENSSL_cleanse(secret, sizeof(secret));
6530c490 641 return ret;
0d9824c1 642}
04904312
MC
643
644int tls13_alert_code(int code)
645{
ef6c191b 646 if (code == SSL_AD_MISSING_EXTENSION)
04904312
MC
647 return code;
648
649 return tls1_alert_code(code);
650}
0ca8d1ec
MC
651
652int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
653 const char *label, size_t llen,
654 const unsigned char *context,
655 size_t contextlen, int use_context)
656{
657 unsigned char exportsecret[EVP_MAX_MD_SIZE];
658 static const unsigned char exporterlabel[] = "exporter";
c8b93876 659 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
0ca8d1ec
MC
660 const EVP_MD *md = ssl_handshake_md(s);
661 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
c8b93876 662 unsigned int hashsize, datalen;
0ca8d1ec
MC
663 int ret = 0;
664
a599574b 665 if (ctx == NULL || !SSL_is_init_finished(s))
0ca8d1ec
MC
666 goto err;
667
668 if (!use_context)
669 contextlen = 0;
670
671 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
672 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
673 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
c8b93876
TT
674 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
675 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
0ca8d1ec 676 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
c8b93876
TT
677 (const unsigned char *)label, llen,
678 data, datalen, exportsecret, hashsize)
0ca8d1ec 679 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
a19ae67d
MC
680 sizeof(exporterlabel) - 1, hash, hashsize,
681 out, olen))
0ca8d1ec
MC
682 goto err;
683
684 ret = 1;
685 err:
686 EVP_MD_CTX_free(ctx);
687 return ret;
688}