]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/tls13_enc.c
Ignore disabled ciphers when deciding if we are using ECC
[thirdparty/openssl.git] / ssl / tls13_enc.c
CommitLineData
34574f19 1/*
6738bf14 2 * Copyright 2016-2018 The OpenSSL Project Authors. All Rights Reserved.
34574f19
MC
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
ec0c5f56
BE
16/*
17 * RFC 8446, 7.1 Key Schedule, says:
18 * Note: With common hash functions, any label longer than 12 characters
19 * requires an additional iteration of the hash function to compute.
20 * The labels in this specification have all been chosen to fit within
21 * this limit.
22 */
23#define TLS13_MAX_LABEL_LEN 12
34574f19
MC
24
25/* Always filled with zeros */
26static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
27
34574f19 28/*
a19ae67d
MC
29 * Given a |secret|; a |label| of length |labellen|; and |data| of length
30 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
31 * secret |outlen| bytes long and store it in the location pointed to be |out|.
32 * The |data| value may be zero length. Returns 1 on success 0 on failure.
34574f19 33 */
ec15acb6 34int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
34574f19 35 const unsigned char *label, size_t labellen,
a19ae67d 36 const unsigned char *data, size_t datalen,
34574f19
MC
37 unsigned char *out, size_t outlen)
38{
ec0c5f56 39 static const unsigned char label_prefix[] = "tls13 ";
34574f19
MC
40 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
41 int ret;
42 size_t hkdflabellen;
43 size_t hashlen;
44 /*
ec0c5f56
BE
45 * 2 bytes for length of derived secret + 1 byte for length of combined
46 * prefix and label + bytes for the label itself + 1 byte length of hash
47 * + bytes for the hash itself
34574f19
MC
48 */
49 unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) +
50 + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN
51 + EVP_MAX_MD_SIZE];
52 WPACKET pkt;
53
54 if (pctx == NULL)
55 return 0;
56
57 hashlen = EVP_MD_size(md);
58
59 if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0)
60 || !WPACKET_put_bytes_u16(&pkt, outlen)
61 || !WPACKET_start_sub_packet_u8(&pkt)
62 || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1)
63 || !WPACKET_memcpy(&pkt, label, labellen)
64 || !WPACKET_close(&pkt)
a19ae67d 65 || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen)
34574f19
MC
66 || !WPACKET_get_total_written(&pkt, &hkdflabellen)
67 || !WPACKET_finish(&pkt)) {
d49e23ec 68 EVP_PKEY_CTX_free(pctx);
34574f19 69 WPACKET_cleanup(&pkt);
f63a17d6
MC
70 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
71 ERR_R_INTERNAL_ERROR);
34574f19
MC
72 return 0;
73 }
74
75 ret = EVP_PKEY_derive_init(pctx) <= 0
76 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY)
77 <= 0
78 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
79 || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0
80 || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0
81 || EVP_PKEY_derive(pctx, out, &outlen) <= 0;
82
83 EVP_PKEY_CTX_free(pctx);
84
f63a17d6
MC
85 if (ret != 0)
86 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_HKDF_EXPAND,
87 ERR_R_INTERNAL_ERROR);
88
34574f19
MC
89 return ret == 0;
90}
91
34574f19 92/*
f5ca0b04
MC
93 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
94 * success 0 on failure.
34574f19 95 */
d49e23ec
MC
96int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
97 unsigned char *key, size_t keylen)
34574f19 98{
f5ca0b04
MC
99 static const unsigned char keylabel[] = "key";
100
d49e23ec 101 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
a19ae67d 102 NULL, 0, key, keylen);
34574f19
MC
103}
104
105/*
f5ca0b04
MC
106 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
107 * success 0 on failure.
34574f19 108 */
d49e23ec
MC
109int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
110 unsigned char *iv, size_t ivlen)
34574f19 111{
f5ca0b04
MC
112 static const unsigned char ivlabel[] = "iv";
113
d49e23ec 114 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
a19ae67d 115 NULL, 0, iv, ivlen);
34574f19
MC
116}
117
ec15acb6
MC
118int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
119 const unsigned char *secret,
120 unsigned char *fin, size_t finlen)
6484776f 121{
f5ca0b04
MC
122 static const unsigned char finishedlabel[] = "finished";
123
ec15acb6 124 return tls13_hkdf_expand(s, md, secret, finishedlabel,
a19ae67d 125 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen);
6484776f
MC
126}
127
34574f19
MC
128/*
129 * Given the previous secret |prevsecret| and a new input secret |insecret| of
130 * length |insecretlen|, generate a new secret and store it in the location
f5ca0b04 131 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
34574f19 132 */
ec15acb6
MC
133int tls13_generate_secret(SSL *s, const EVP_MD *md,
134 const unsigned char *prevsecret,
135 const unsigned char *insecret,
136 size_t insecretlen,
137 unsigned char *outsecret)
34574f19 138{
34574f19 139 size_t mdlen, prevsecretlen;
bceae201 140 int mdleni;
34574f19
MC
141 int ret;
142 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
17aa119e 143 static const char derived_secret_label[] = "derived";
3e0458fb 144 unsigned char preextractsec[EVP_MAX_MD_SIZE];
34574f19 145
f63a17d6
MC
146 if (pctx == NULL) {
147 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
148 ERR_R_INTERNAL_ERROR);
34574f19 149 return 0;
f63a17d6 150 }
34574f19 151
bceae201
MC
152 mdleni = EVP_MD_size(md);
153 /* Ensure cast to size_t is safe */
154 if (!ossl_assert(mdleni >= 0)) {
155 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
156 ERR_R_INTERNAL_ERROR);
157 return 0;
158 }
159 mdlen = (size_t)mdleni;
34574f19
MC
160
161 if (insecret == NULL) {
162 insecret = default_zeros;
163 insecretlen = mdlen;
164 }
165 if (prevsecret == NULL) {
166 prevsecret = default_zeros;
167 prevsecretlen = 0;
168 } else {
3e0458fb
MC
169 EVP_MD_CTX *mctx = EVP_MD_CTX_new();
170 unsigned char hash[EVP_MAX_MD_SIZE];
171
172 /* The pre-extract derive step uses a hash of no messages */
173 if (mctx == NULL
174 || EVP_DigestInit_ex(mctx, md, NULL) <= 0
175 || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) {
f63a17d6
MC
176 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
177 ERR_R_INTERNAL_ERROR);
3e0458fb 178 EVP_MD_CTX_free(mctx);
71d8c138 179 EVP_PKEY_CTX_free(pctx);
3e0458fb
MC
180 return 0;
181 }
182 EVP_MD_CTX_free(mctx);
183
184 /* Generate the pre-extract secret */
185 if (!tls13_hkdf_expand(s, md, prevsecret,
186 (unsigned char *)derived_secret_label,
a19ae67d 187 sizeof(derived_secret_label) - 1, hash, mdlen,
71d8c138 188 preextractsec, mdlen)) {
f63a17d6 189 /* SSLfatal() already called */
71d8c138 190 EVP_PKEY_CTX_free(pctx);
3e0458fb 191 return 0;
71d8c138 192 }
3e0458fb
MC
193
194 prevsecret = preextractsec;
34574f19
MC
195 prevsecretlen = mdlen;
196 }
197
198 ret = EVP_PKEY_derive_init(pctx) <= 0
199 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
200 <= 0
201 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
202 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
203 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
204 <= 0
205 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
206 <= 0;
207
f63a17d6
MC
208 if (ret != 0)
209 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_GENERATE_SECRET,
210 ERR_R_INTERNAL_ERROR);
211
34574f19 212 EVP_PKEY_CTX_free(pctx);
3e0458fb
MC
213 if (prevsecret == preextractsec)
214 OPENSSL_cleanse(preextractsec, mdlen);
34574f19
MC
215 return ret == 0;
216}
217
34574f19
MC
218/*
219 * Given an input secret |insecret| of length |insecretlen| generate the
220 * handshake secret. This requires the early secret to already have been
f5ca0b04 221 * generated. Returns 1 on success 0 on failure.
34574f19
MC
222 */
223int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
224 size_t insecretlen)
225{
f63a17d6 226 /* Calls SSLfatal() if required */
ec15acb6
MC
227 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
228 insecret, insecretlen,
34574f19
MC
229 (unsigned char *)&s->handshake_secret);
230}
231
232/*
233 * Given the handshake secret |prev| of length |prevlen| generate the master
f5ca0b04
MC
234 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
235 * failure.
34574f19
MC
236 */
237int tls13_generate_master_secret(SSL *s, unsigned char *out,
238 unsigned char *prev, size_t prevlen,
239 size_t *secret_size)
240{
ec15acb6
MC
241 const EVP_MD *md = ssl_handshake_md(s);
242
243 *secret_size = EVP_MD_size(md);
f63a17d6 244 /* Calls SSLfatal() if required */
ec15acb6 245 return tls13_generate_secret(s, md, prev, NULL, 0, out);
34574f19
MC
246}
247
92760c21 248/*
f5ca0b04
MC
249 * Generates the mac for the Finished message. Returns the length of the MAC or
250 * 0 on error.
92760c21
MC
251 */
252size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
253 unsigned char *out)
254{
6484776f
MC
255 const EVP_MD *md = ssl_handshake_md(s);
256 unsigned char hash[EVP_MAX_MD_SIZE];
257 size_t hashlen, ret = 0;
258 EVP_PKEY *key = NULL;
259 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
92760c21 260
d4d2f3a4
MC
261 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
262 /* SSLfatal() already called */
6484776f 263 goto err;
d4d2f3a4 264 }
6484776f 265
de9f5b35 266 if (str == s->method->ssl3_enc->server_finished_label) {
f929439f
MC
267 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
268 s->server_finished_secret, hashlen);
de9f5b35 269 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
f929439f
MC
270 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL,
271 s->client_finished_secret, hashlen);
de9f5b35
MC
272 } else {
273 unsigned char finsecret[EVP_MAX_MD_SIZE];
274
275 if (!tls13_derive_finishedkey(s, ssl_handshake_md(s),
276 s->client_app_traffic_secret,
277 finsecret, hashlen))
278 goto err;
279
280 key = EVP_PKEY_new_raw_private_key(EVP_PKEY_HMAC, NULL, finsecret,
281 hashlen);
8f39d8af 282 OPENSSL_cleanse(finsecret, sizeof(finsecret));
de9f5b35 283 }
6484776f
MC
284
285 if (key == NULL
286 || ctx == NULL
287 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
288 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
d4d2f3a4
MC
289 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) {
290 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_FINAL_FINISH_MAC,
291 ERR_R_INTERNAL_ERROR);
6484776f 292 goto err;
d4d2f3a4 293 }
92760c21 294
6484776f
MC
295 ret = hashlen;
296 err:
297 EVP_PKEY_free(key);
298 EVP_MD_CTX_free(ctx);
299 return ret;
92760c21
MC
300}
301
302/*
303 * There isn't really a key block in TLSv1.3, but we still need this function
f5ca0b04 304 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
92760c21
MC
305 */
306int tls13_setup_key_block(SSL *s)
307{
308 const EVP_CIPHER *c;
309 const EVP_MD *hash;
310 int mac_type = NID_undef;
311
312 s->session->cipher = s->s3->tmp.new_cipher;
313 if (!ssl_cipher_get_evp
314 (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
f63a17d6
MC
315 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_TLS13_SETUP_KEY_BLOCK,
316 SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
92760c21
MC
317 return 0;
318 }
319
320 s->s3->tmp.new_sym_enc = c;
321 s->s3->tmp.new_hash = hash;
322
323 return 1;
324}
325
d1186c30 326static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md,
d49e23ec 327 const EVP_CIPHER *ciph,
57389a32
MC
328 const unsigned char *insecret,
329 const unsigned char *hash,
330 const unsigned char *label,
331 size_t labellen, unsigned char *secret,
332 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
333{
334 unsigned char key[EVP_MAX_KEY_LENGTH];
335 size_t ivlen, keylen, taglen;
bceae201
MC
336 int hashleni = EVP_MD_size(md);
337 size_t hashlen;
338
339 /* Ensure cast to size_t is safe */
340 if (!ossl_assert(hashleni >= 0)) {
341 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
342 ERR_R_EVP_LIB);
343 goto err;
344 }
345 hashlen = (size_t)hashleni;
57389a32 346
a19ae67d
MC
347 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
348 secret, hashlen)) {
f63a17d6 349 /* SSLfatal() already called */
57389a32
MC
350 goto err;
351 }
352
353 /* TODO(size_t): convert me */
354 keylen = EVP_CIPHER_key_length(ciph);
355 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
c117af67
MC
356 uint32_t algenc;
357
57389a32 358 ivlen = EVP_CCM_TLS_IV_LEN;
c117af67
MC
359 if (s->s3->tmp.new_cipher == NULL) {
360 /* We've not selected a cipher yet - we must be doing early data */
361 algenc = s->session->cipher->algorithm_enc;
362 } else {
363 algenc = s->s3->tmp.new_cipher->algorithm_enc;
364 }
365 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
57389a32
MC
366 taglen = EVP_CCM8_TLS_TAG_LEN;
367 else
368 taglen = EVP_CCM_TLS_TAG_LEN;
369 } else {
370 ivlen = EVP_CIPHER_iv_length(ciph);
371 taglen = 0;
372 }
373
d49e23ec
MC
374 if (!tls13_derive_key(s, md, secret, key, keylen)
375 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
f63a17d6 376 /* SSLfatal() already called */
57389a32
MC
377 goto err;
378 }
379
d1186c30 380 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
57389a32
MC
381 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
382 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
383 taglen, NULL))
384 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
f63a17d6
MC
385 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_F_DERIVE_SECRET_KEY_AND_IV,
386 ERR_R_EVP_LIB);
57389a32
MC
387 goto err;
388 }
389
57389a32
MC
390 return 1;
391 err:
392 OPENSSL_cleanse(key, sizeof(key));
393 return 0;
394}
395
0d9824c1
MC
396int tls13_change_cipher_state(SSL *s, int which)
397{
17aa119e
MC
398 static const unsigned char client_early_traffic[] = "c e traffic";
399 static const unsigned char client_handshake_traffic[] = "c hs traffic";
400 static const unsigned char client_application_traffic[] = "c ap traffic";
401 static const unsigned char server_handshake_traffic[] = "s hs traffic";
402 static const unsigned char server_application_traffic[] = "s ap traffic";
0ca8d1ec 403 static const unsigned char exporter_master_secret[] = "exp master";
17aa119e 404 static const unsigned char resumption_master_secret[] = "res master";
b38ede80 405 static const unsigned char early_exporter_master_secret[] = "e exp master";
bebc0c7d 406 unsigned char *iv;
0d9824c1 407 unsigned char secret[EVP_MAX_MD_SIZE];
ace081c1
MC
408 unsigned char hashval[EVP_MAX_MD_SIZE];
409 unsigned char *hash = hashval;
0d9824c1 410 unsigned char *insecret;
6484776f 411 unsigned char *finsecret = NULL;
2c7bd692 412 const char *log_label = NULL;
0d9824c1 413 EVP_CIPHER_CTX *ciph_ctx;
57389a32 414 size_t finsecretlen = 0;
0d9824c1 415 const unsigned char *label;
ace081c1 416 size_t labellen, hashlen = 0;
6530c490 417 int ret = 0;
42f50fdf
MC
418 const EVP_MD *md = NULL;
419 const EVP_CIPHER *cipher = NULL;
0d9824c1
MC
420
421 if (which & SSL3_CC_READ) {
422 if (s->enc_read_ctx != NULL) {
423 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
424 } else {
425 s->enc_read_ctx = EVP_CIPHER_CTX_new();
426 if (s->enc_read_ctx == NULL) {
f63a17d6
MC
427 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
428 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
0d9824c1
MC
429 goto err;
430 }
431 }
432 ciph_ctx = s->enc_read_ctx;
bebc0c7d 433 iv = s->read_iv;
0d9824c1
MC
434
435 RECORD_LAYER_reset_read_sequence(&s->rlayer);
436 } else {
7426cd34 437 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
0d9824c1
MC
438 if (s->enc_write_ctx != NULL) {
439 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
440 } else {
441 s->enc_write_ctx = EVP_CIPHER_CTX_new();
442 if (s->enc_write_ctx == NULL) {
f63a17d6
MC
443 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
444 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
0d9824c1
MC
445 goto err;
446 }
447 }
448 ciph_ctx = s->enc_write_ctx;
bebc0c7d 449 iv = s->write_iv;
0d9824c1
MC
450
451 RECORD_LAYER_reset_write_sequence(&s->rlayer);
452 }
453
454 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
455 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
d49e23ec
MC
456 if (which & SSL3_CC_EARLY) {
457 EVP_MD_CTX *mdctx = NULL;
458 long handlen;
459 void *hdata;
460 unsigned int hashlenui;
461 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
462
463 insecret = s->early_secret;
464 label = client_early_traffic;
465 labellen = sizeof(client_early_traffic) - 1;
466 log_label = CLIENT_EARLY_LABEL;
467
468 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
469 if (handlen <= 0) {
f63a17d6
MC
470 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
471 SSL_F_TLS13_CHANGE_CIPHER_STATE,
472 SSL_R_BAD_HANDSHAKE_LENGTH);
d49e23ec
MC
473 goto err;
474 }
add8d0e9 475
08717544
MC
476 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
477 && s->max_early_data > 0
478 && s->session->ext.max_early_data == 0) {
479 /*
480 * If we are attempting to send early data, and we've decided to
481 * actually do it but max_early_data in s->session is 0 then we
482 * must be using an external PSK.
483 */
484 if (!ossl_assert(s->psksession != NULL
485 && s->max_early_data ==
486 s->psksession->ext.max_early_data)) {
f63a17d6
MC
487 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
488 SSL_F_TLS13_CHANGE_CIPHER_STATE,
489 ERR_R_INTERNAL_ERROR);
08717544
MC
490 goto err;
491 }
add8d0e9 492 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
08717544 493 }
d49e23ec 494 if (sslcipher == NULL) {
f63a17d6
MC
495 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
496 SSL_F_TLS13_CHANGE_CIPHER_STATE, SSL_R_BAD_PSK);
d49e23ec
MC
497 goto err;
498 }
499
500 /*
501 * We need to calculate the handshake digest using the digest from
502 * the session. We haven't yet selected our ciphersuite so we can't
503 * use ssl_handshake_md().
504 */
505 mdctx = EVP_MD_CTX_new();
506 if (mdctx == NULL) {
f63a17d6
MC
507 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
508 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
d49e23ec
MC
509 goto err;
510 }
511 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
512 md = ssl_md(sslcipher->algorithm2);
513 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
514 || !EVP_DigestUpdate(mdctx, hdata, handlen)
515 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
f63a17d6
MC
516 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
517 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
d49e23ec
MC
518 EVP_MD_CTX_free(mdctx);
519 goto err;
520 }
521 hashlen = hashlenui;
522 EVP_MD_CTX_free(mdctx);
b38ede80
TT
523
524 if (!tls13_hkdf_expand(s, md, insecret,
525 early_exporter_master_secret,
526 sizeof(early_exporter_master_secret) - 1,
527 hashval, hashlen,
528 s->early_exporter_master_secret, hashlen)) {
529 SSLfatal(s, SSL_AD_INTERNAL_ERROR,
530 SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
531 goto err;
532 }
01a2a654
PW
533
534 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
535 s->early_exporter_master_secret, hashlen)) {
536 /* SSLfatal() already called */
537 goto err;
538 }
d49e23ec 539 } else if (which & SSL3_CC_HANDSHAKE) {
0d9824c1 540 insecret = s->handshake_secret;
6484776f 541 finsecret = s->client_finished_secret;
6612d87b 542 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
0d9824c1
MC
543 label = client_handshake_traffic;
544 labellen = sizeof(client_handshake_traffic) - 1;
2c7bd692 545 log_label = CLIENT_HANDSHAKE_LABEL;
fe5e20fd 546 /*
69687aa8 547 * The handshake hash used for the server read/client write handshake
f7e393be
MC
548 * traffic secret is the same as the hash for the server
549 * write/client read handshake traffic secret. However, if we
550 * processed early data then we delay changing the server
551 * read/client write cipher state until later, and the handshake
552 * hashes have moved on. Therefore we use the value saved earlier
553 * when we did the server write/client read change cipher state.
fe5e20fd 554 */
f7e393be 555 hash = s->handshake_traffic_hash;
0d9824c1 556 } else {
ec15acb6 557 insecret = s->master_secret;
0d9824c1
MC
558 label = client_application_traffic;
559 labellen = sizeof(client_application_traffic) - 1;
2c7bd692 560 log_label = CLIENT_APPLICATION_LABEL;
ace081c1
MC
561 /*
562 * For this we only use the handshake hashes up until the server
563 * Finished hash. We do not include the client's Finished, which is
564 * what ssl_handshake_hash() would give us. Instead we use the
565 * previously saved value.
566 */
567 hash = s->server_finished_hash;
0d9824c1
MC
568 }
569 } else {
d49e23ec 570 /* Early data never applies to client-read/server-write */
0d9824c1
MC
571 if (which & SSL3_CC_HANDSHAKE) {
572 insecret = s->handshake_secret;
6484776f 573 finsecret = s->server_finished_secret;
6612d87b 574 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
0d9824c1
MC
575 label = server_handshake_traffic;
576 labellen = sizeof(server_handshake_traffic) - 1;
2c7bd692 577 log_label = SERVER_HANDSHAKE_LABEL;
0d9824c1 578 } else {
ec15acb6 579 insecret = s->master_secret;
0d9824c1
MC
580 label = server_application_traffic;
581 labellen = sizeof(server_application_traffic) - 1;
2c7bd692 582 log_label = SERVER_APPLICATION_LABEL;
0d9824c1
MC
583 }
584 }
585
d49e23ec
MC
586 if (!(which & SSL3_CC_EARLY)) {
587 md = ssl_handshake_md(s);
588 cipher = s->s3->tmp.new_sym_enc;
589 if (!ssl3_digest_cached_records(s, 1)
590 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
f63a17d6 591 /* SSLfatal() already called */;
d49e23ec
MC
592 goto err;
593 }
ace081c1
MC
594 }
595
ec15acb6
MC
596 /*
597 * Save the hash of handshakes up to now for use when we calculate the
598 * client application traffic secret
599 */
600 if (label == server_application_traffic)
601 memcpy(s->server_finished_hash, hashval, hashlen);
602
f7e393be 603 if (label == server_handshake_traffic)
fe5e20fd
MC
604 memcpy(s->handshake_traffic_hash, hashval, hashlen);
605
ec15acb6
MC
606 if (label == client_application_traffic) {
607 /*
608 * We also create the resumption master secret, but this time use the
609 * hash for the whole handshake including the Client Finished
610 */
611 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
612 resumption_master_secret,
613 sizeof(resumption_master_secret) - 1,
4ff1a526 614 hashval, hashlen, s->resumption_master_secret,
a19ae67d 615 hashlen)) {
f63a17d6 616 /* SSLfatal() already called */
ec15acb6
MC
617 goto err;
618 }
ec15acb6
MC
619 }
620
d49e23ec
MC
621 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
622 insecret, hash, label, labellen, secret, iv,
623 ciph_ctx)) {
f63a17d6 624 /* SSLfatal() already called */
57389a32 625 goto err;
ec07b1d8 626 }
0d9824c1 627
2221ec10 628 if (label == server_application_traffic) {
57389a32 629 memcpy(s->server_app_traffic_secret, secret, hashlen);
2221ec10
TT
630 /* Now we create the exporter master secret */
631 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
632 exporter_master_secret,
633 sizeof(exporter_master_secret) - 1,
634 hash, hashlen, s->exporter_master_secret,
635 hashlen)) {
636 /* SSLfatal() already called */
637 goto err;
638 }
6329ce8f
PW
639
640 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
641 hashlen)) {
642 /* SSLfatal() already called */
643 goto err;
644 }
2221ec10 645 } else if (label == client_application_traffic)
57389a32
MC
646 memcpy(s->client_app_traffic_secret, secret, hashlen);
647
2c7bd692 648 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
f63a17d6 649 /* SSLfatal() already called */
2c7bd692
CB
650 goto err;
651 }
652
57389a32
MC
653 if (finsecret != NULL
654 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
655 finsecret, finsecretlen)) {
f63a17d6 656 /* SSLfatal() already called */
0d9824c1
MC
657 goto err;
658 }
659
7426cd34
MC
660 if (!s->server && label == client_early_traffic)
661 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
662 else
663 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
57389a32
MC
664 ret = 1;
665 err:
666 OPENSSL_cleanse(secret, sizeof(secret));
667 return ret;
668}
0d9824c1 669
d1186c30 670int tls13_update_key(SSL *s, int sending)
57389a32 671{
17aa119e 672 static const unsigned char application_traffic[] = "traffic upd";
57389a32
MC
673 const EVP_MD *md = ssl_handshake_md(s);
674 size_t hashlen = EVP_MD_size(md);
675 unsigned char *insecret, *iv;
676 unsigned char secret[EVP_MAX_MD_SIZE];
677 EVP_CIPHER_CTX *ciph_ctx;
678 int ret = 0;
0d9824c1 679
d1186c30 680 if (s->server == sending)
57389a32
MC
681 insecret = s->server_app_traffic_secret;
682 else
683 insecret = s->client_app_traffic_secret;
bebc0c7d 684
d1186c30 685 if (sending) {
7426cd34 686 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
57389a32
MC
687 iv = s->write_iv;
688 ciph_ctx = s->enc_write_ctx;
689 RECORD_LAYER_reset_write_sequence(&s->rlayer);
690 } else {
691 iv = s->read_iv;
692 ciph_ctx = s->enc_read_ctx;
693 RECORD_LAYER_reset_read_sequence(&s->rlayer);
0d9824c1 694 }
57389a32 695
d1186c30 696 if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s),
d49e23ec
MC
697 s->s3->tmp.new_sym_enc, insecret, NULL,
698 application_traffic,
57389a32 699 sizeof(application_traffic) - 1, secret, iv,
f63a17d6
MC
700 ciph_ctx)) {
701 /* SSLfatal() already called */
57389a32 702 goto err;
f63a17d6 703 }
57389a32
MC
704
705 memcpy(insecret, secret, hashlen);
0d9824c1 706
7426cd34 707 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
6530c490 708 ret = 1;
0d9824c1
MC
709 err:
710 OPENSSL_cleanse(secret, sizeof(secret));
6530c490 711 return ret;
0d9824c1 712}
04904312
MC
713
714int tls13_alert_code(int code)
715{
43a0f273
MC
716 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
717 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
04904312
MC
718 return code;
719
720 return tls1_alert_code(code);
721}
0ca8d1ec
MC
722
723int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen,
724 const char *label, size_t llen,
725 const unsigned char *context,
726 size_t contextlen, int use_context)
727{
728 unsigned char exportsecret[EVP_MAX_MD_SIZE];
729 static const unsigned char exporterlabel[] = "exporter";
c8b93876 730 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
0ca8d1ec
MC
731 const EVP_MD *md = ssl_handshake_md(s);
732 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
c8b93876 733 unsigned int hashsize, datalen;
0ca8d1ec
MC
734 int ret = 0;
735
1f5878b8 736 if (ctx == NULL || !ossl_statem_export_allowed(s))
0ca8d1ec
MC
737 goto err;
738
739 if (!use_context)
740 contextlen = 0;
741
742 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
743 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
744 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
c8b93876
TT
745 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
746 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
0ca8d1ec 747 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
c8b93876
TT
748 (const unsigned char *)label, llen,
749 data, datalen, exportsecret, hashsize)
0ca8d1ec 750 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
a19ae67d
MC
751 sizeof(exporterlabel) - 1, hash, hashsize,
752 out, olen))
0ca8d1ec
MC
753 goto err;
754
755 ret = 1;
756 err:
757 EVP_MD_CTX_free(ctx);
758 return ret;
759}
b38ede80
TT
760
761int tls13_export_keying_material_early(SSL *s, unsigned char *out, size_t olen,
762 const char *label, size_t llen,
763 const unsigned char *context,
764 size_t contextlen)
765{
766 static const unsigned char exporterlabel[] = "exporter";
767 unsigned char exportsecret[EVP_MAX_MD_SIZE];
768 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
769 const EVP_MD *md;
770 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
771 unsigned int hashsize, datalen;
772 int ret = 0;
773 const SSL_CIPHER *sslcipher;
774
775 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
776 goto err;
777
778 if (!s->server && s->max_early_data > 0
779 && s->session->ext.max_early_data == 0)
780 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
781 else
782 sslcipher = SSL_SESSION_get0_cipher(s->session);
783
784 md = ssl_md(sslcipher->algorithm2);
785
786 /*
787 * Calculate the hash value and store it in |data|. The reason why
788 * the empty string is used is that the definition of TLS-Exporter
789 * is like so:
790 *
791 * TLS-Exporter(label, context_value, key_length) =
792 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
793 * "exporter", Hash(context_value), key_length)
794 *
795 * Derive-Secret(Secret, Label, Messages) =
796 * HKDF-Expand-Label(Secret, Label,
797 * Transcript-Hash(Messages), Hash.length)
798 *
799 * Here Transcript-Hash is the cipher suite hash algorithm.
800 */
801 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
802 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
803 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
804 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
805 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
806 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
807 (const unsigned char *)label, llen,
808 data, datalen, exportsecret, hashsize)
809 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
810 sizeof(exporterlabel) - 1, hash, hashsize,
811 out, olen))
812 goto err;
813
814 ret = 1;
815 err:
816 EVP_MD_CTX_free(ctx);
817 return ret;
818}