]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/tls13_enc.c
QUIC Demuxer and Record Layer (RX Side)
[thirdparty/openssl.git] / ssl / tls13_enc.c
CommitLineData
34574f19 1/*
fecb3aae 2 * Copyright 2016-2022 The OpenSSL Project Authors. All Rights Reserved.
34574f19 3 *
2c18d164 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
34574f19
MC
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>
706457b7 11#include "ssl_local.h"
cc10b56d
VF
12#include "internal/ktls.h"
13#include "record/record_local.h"
08717544 14#include "internal/cryptlib.h"
34574f19
MC
15#include <openssl/evp.h>
16#include <openssl/kdf.h>
ce3b1bb4 17#include <openssl/core_names.h>
34574f19 18
0fb2815b 19#define TLS13_MAX_LABEL_LEN 249
34574f19 20
f7d998a2
P
21#ifdef CHARSET_EBCDIC
22static const unsigned char label_prefix[] = { 0x74, 0x6C, 0x73, 0x31, 0x33, 0x20, 0x00 };
23#else
24static const unsigned char label_prefix[] = "tls13 ";
25#endif
34574f19 26
34574f19 27/*
a19ae67d
MC
28 * Given a |secret|; a |label| of length |labellen|; and |data| of length
29 * |datalen| (e.g. typically a hash of the handshake messages), derive a new
30 * secret |outlen| bytes long and store it in the location pointed to be |out|.
0fb2815b
MC
31 * The |data| value may be zero length. Any errors will be treated as fatal if
32 * |fatal| is set. Returns 1 on success 0 on failure.
ec279ac2 33 * If |raise_error| is set, ERR_raise is called on failure.
34574f19 34 */
ec279ac2
HL
35int tls13_hkdf_expand_ex(OSSL_LIB_CTX *libctx, const char *propq,
36 const EVP_MD *md,
37 const unsigned char *secret,
38 const unsigned char *label, size_t labellen,
39 const unsigned char *data, size_t datalen,
40 unsigned char *out, size_t outlen, int raise_error)
34574f19 41{
ec279ac2 42 EVP_KDF *kdf = EVP_KDF_fetch(libctx, OSSL_KDF_NAME_TLS1_3_KDF, propq);
ce3b1bb4 43 EVP_KDF_CTX *kctx;
f7d998a2 44 OSSL_PARAM params[7], *p = params;
ce3b1bb4 45 int mode = EVP_PKEY_HKDEF_MODE_EXPAND_ONLY;
ed576acd 46 const char *mdname = EVP_MD_get0_name(md);
34574f19 47 int ret;
34574f19 48 size_t hashlen;
34574f19 49
660c5344 50 kctx = EVP_KDF_CTX_new(kdf);
ce3b1bb4 51 EVP_KDF_free(kdf);
32495464 52 if (kctx == NULL)
34574f19
MC
53 return 0;
54
0fb2815b 55 if (labellen > TLS13_MAX_LABEL_LEN) {
ec279ac2 56 if (raise_error)
0fb2815b
MC
57 /*
58 * Probably we have been called from SSL_export_keying_material(),
59 * or SSL_export_keying_material_early().
60 */
6849b73c 61 ERR_raise(ERR_LIB_SSL, SSL_R_TLS_ILLEGAL_EXPORTER_LABEL);
ec279ac2 62
660c5344 63 EVP_KDF_CTX_free(kctx);
0fb2815b
MC
64 return 0;
65 }
66
f7d998a2 67 if ((ret = EVP_MD_get_size(md)) <= 0) {
660c5344 68 EVP_KDF_CTX_free(kctx);
ec279ac2 69 if (raise_error)
6849b73c 70 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
34574f19
MC
71 return 0;
72 }
f7d998a2 73 hashlen = (size_t)ret;
34574f19 74
ce3b1bb4
P
75 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
76 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
8b6ffd40 77 (char *)mdname, 0);
ce3b1bb4
P
78 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
79 (unsigned char *)secret, hashlen);
f7d998a2
P
80 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
81 (unsigned char *)label_prefix,
82 sizeof(label_prefix) - 1);
83 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
84 (unsigned char *)label, labellen);
85 if (data != NULL)
86 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_DATA,
87 (unsigned char *)data,
88 datalen);
ce3b1bb4
P
89 *p++ = OSSL_PARAM_construct_end();
90
5cceedb5 91 ret = EVP_KDF_derive(kctx, out, outlen, params) <= 0;
660c5344 92 EVP_KDF_CTX_free(kctx);
34574f19 93
0fb2815b 94 if (ret != 0) {
ec279ac2 95 if (raise_error)
6849b73c 96 ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
0fb2815b 97 }
f63a17d6 98
34574f19
MC
99 return ret == 0;
100}
101
ec279ac2
HL
102int tls13_hkdf_expand(SSL_CONNECTION *s, const EVP_MD *md,
103 const unsigned char *secret,
104 const unsigned char *label, size_t labellen,
105 const unsigned char *data, size_t datalen,
106 unsigned char *out, size_t outlen, int fatal)
107{
108 int ret;
109 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
110
111 ret = tls13_hkdf_expand_ex(sctx->libctx, sctx->propq, md,
112 secret, label, labellen, data, datalen,
113 out, outlen, !fatal);
114 if (ret == 0 && fatal)
115 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
116
117 return ret;
118}
119
34574f19 120/*
f5ca0b04
MC
121 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
122 * success 0 on failure.
34574f19 123 */
38b051a1
TM
124int tls13_derive_key(SSL_CONNECTION *s, const EVP_MD *md,
125 const unsigned char *secret,
d49e23ec 126 unsigned char *key, size_t keylen)
34574f19 127{
48102247 128#ifdef CHARSET_EBCDIC
129 static const unsigned char keylabel[] ={ 0x6B, 0x65, 0x79, 0x00 };
130#else
131 static const unsigned char keylabel[] = "key";
132#endif
f5ca0b04 133
d49e23ec 134 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
0fb2815b 135 NULL, 0, key, keylen, 1);
34574f19
MC
136}
137
138/*
f5ca0b04
MC
139 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
140 * success 0 on failure.
34574f19 141 */
38b051a1
TM
142int tls13_derive_iv(SSL_CONNECTION *s, const EVP_MD *md,
143 const unsigned char *secret,
d49e23ec 144 unsigned char *iv, size_t ivlen)
34574f19 145{
48102247 146#ifdef CHARSET_EBCDIC
147 static const unsigned char ivlabel[] = { 0x69, 0x76, 0x00 };
148#else
149 static const unsigned char ivlabel[] = "iv";
150#endif
f5ca0b04 151
d49e23ec 152 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
0fb2815b 153 NULL, 0, iv, ivlen, 1);
34574f19
MC
154}
155
38b051a1 156int tls13_derive_finishedkey(SSL_CONNECTION *s, const EVP_MD *md,
ec15acb6
MC
157 const unsigned char *secret,
158 unsigned char *fin, size_t finlen)
6484776f 159{
48102247 160#ifdef CHARSET_EBCDIC
161 static const unsigned char finishedlabel[] = { 0x66, 0x69, 0x6E, 0x69, 0x73, 0x68, 0x65, 0x64, 0x00 };
162#else
163 static const unsigned char finishedlabel[] = "finished";
164#endif
f5ca0b04 165
ec15acb6 166 return tls13_hkdf_expand(s, md, secret, finishedlabel,
0fb2815b 167 sizeof(finishedlabel) - 1, NULL, 0, fin, finlen, 1);
6484776f
MC
168}
169
34574f19
MC
170/*
171 * Given the previous secret |prevsecret| and a new input secret |insecret| of
172 * length |insecretlen|, generate a new secret and store it in the location
f5ca0b04 173 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
34574f19 174 */
38b051a1 175int tls13_generate_secret(SSL_CONNECTION *s, const EVP_MD *md,
ec15acb6
MC
176 const unsigned char *prevsecret,
177 const unsigned char *insecret,
178 size_t insecretlen,
179 unsigned char *outsecret)
34574f19 180{
f7d998a2 181 size_t mdlen;
bceae201 182 int mdleni;
34574f19 183 int ret;
ce3b1bb4
P
184 EVP_KDF *kdf;
185 EVP_KDF_CTX *kctx;
f7d998a2 186 OSSL_PARAM params[7], *p = params;
ce3b1bb4 187 int mode = EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY;
ed576acd 188 const char *mdname = EVP_MD_get0_name(md);
48102247 189#ifdef CHARSET_EBCDIC
190 static const char derived_secret_label[] = { 0x64, 0x65, 0x72, 0x69, 0x76, 0x65, 0x64, 0x00 };
191#else
17aa119e 192 static const char derived_secret_label[] = "derived";
48102247 193#endif
38b051a1 194 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
34574f19 195
38b051a1 196 kdf = EVP_KDF_fetch(sctx->libctx, OSSL_KDF_NAME_TLS1_3_KDF, sctx->propq);
660c5344 197 kctx = EVP_KDF_CTX_new(kdf);
ce3b1bb4 198 EVP_KDF_free(kdf);
32495464 199 if (kctx == NULL) {
c48ffbcc 200 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
34574f19 201 return 0;
f63a17d6 202 }
34574f19 203
ed576acd 204 mdleni = EVP_MD_get_size(md);
bceae201
MC
205 /* Ensure cast to size_t is safe */
206 if (!ossl_assert(mdleni >= 0)) {
c48ffbcc 207 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
660c5344 208 EVP_KDF_CTX_free(kctx);
bceae201
MC
209 return 0;
210 }
211 mdlen = (size_t)mdleni;
34574f19 212
ce3b1bb4
P
213 *p++ = OSSL_PARAM_construct_int(OSSL_KDF_PARAM_MODE, &mode);
214 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_KDF_PARAM_DIGEST,
8b6ffd40 215 (char *)mdname, 0);
f7d998a2
P
216 if (insecret != NULL)
217 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_KEY,
218 (unsigned char *)insecret,
219 insecretlen);
220 if (prevsecret != NULL)
221 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_SALT,
222 (unsigned char *)prevsecret, mdlen);
223 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_PREFIX,
224 (unsigned char *)label_prefix,
225 sizeof(label_prefix) - 1);
226 *p++ = OSSL_PARAM_construct_octet_string(OSSL_KDF_PARAM_LABEL,
227 (unsigned char *)derived_secret_label,
228 sizeof(derived_secret_label) - 1);
ce3b1bb4
P
229 *p++ = OSSL_PARAM_construct_end();
230
5cceedb5 231 ret = EVP_KDF_derive(kctx, outsecret, mdlen, params) <= 0;
34574f19 232
f63a17d6 233 if (ret != 0)
c48ffbcc 234 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
f63a17d6 235
660c5344 236 EVP_KDF_CTX_free(kctx);
34574f19
MC
237 return ret == 0;
238}
239
34574f19
MC
240/*
241 * Given an input secret |insecret| of length |insecretlen| generate the
242 * handshake secret. This requires the early secret to already have been
f5ca0b04 243 * generated. Returns 1 on success 0 on failure.
34574f19 244 */
38b051a1
TM
245int tls13_generate_handshake_secret(SSL_CONNECTION *s,
246 const unsigned char *insecret,
247 size_t insecretlen)
34574f19 248{
f63a17d6 249 /* Calls SSLfatal() if required */
ec15acb6
MC
250 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
251 insecret, insecretlen,
34574f19
MC
252 (unsigned char *)&s->handshake_secret);
253}
254
255/*
256 * Given the handshake secret |prev| of length |prevlen| generate the master
f5ca0b04
MC
257 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
258 * failure.
34574f19 259 */
38b051a1 260int tls13_generate_master_secret(SSL_CONNECTION *s, unsigned char *out,
34574f19
MC
261 unsigned char *prev, size_t prevlen,
262 size_t *secret_size)
263{
ec15acb6
MC
264 const EVP_MD *md = ssl_handshake_md(s);
265
ed576acd 266 *secret_size = EVP_MD_get_size(md);
f63a17d6 267 /* Calls SSLfatal() if required */
ec15acb6 268 return tls13_generate_secret(s, md, prev, NULL, 0, out);
34574f19
MC
269}
270
92760c21 271/*
f5ca0b04
MC
272 * Generates the mac for the Finished message. Returns the length of the MAC or
273 * 0 on error.
92760c21 274 */
38b051a1 275size_t tls13_final_finish_mac(SSL_CONNECTION *s, const char *str, size_t slen,
92760c21
MC
276 unsigned char *out)
277{
b740012f 278 const EVP_MD *md = ssl_handshake_md(s);
279 const char *mdname = EVP_MD_get0_name(md);
6484776f 280 unsigned char hash[EVP_MAX_MD_SIZE];
c8f6c28a 281 unsigned char finsecret[EVP_MAX_MD_SIZE];
0edb8194 282 unsigned char *key = NULL;
21dfdbef 283 size_t len = 0, hashlen;
0a8a6afd 284 OSSL_PARAM params[2], *p = params;
38b051a1 285 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
c8f6c28a 286
b740012f 287 if (md == NULL)
288 return 0;
289
c8f6c28a 290 /* Safe to cast away const here since we're not "getting" any data */
38b051a1 291 if (sctx->propq != NULL)
c8f6c28a 292 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_PROPERTIES,
38b051a1 293 (char *)sctx->propq,
c8f6c28a 294 0);
0edb8194 295 *p = OSSL_PARAM_construct_end();
92760c21 296
d4d2f3a4
MC
297 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) {
298 /* SSLfatal() already called */
6484776f 299 goto err;
d4d2f3a4 300 }
6484776f 301
38b051a1 302 if (str == SSL_CONNECTION_GET_SSL(s)->method->ssl3_enc->server_finished_label) {
0edb8194 303 key = s->server_finished_secret;
de9f5b35 304 } else if (SSL_IS_FIRST_HANDSHAKE(s)) {
0edb8194 305 key = s->client_finished_secret;
de9f5b35 306 } else {
b740012f 307 if (!tls13_derive_finishedkey(s, md,
de9f5b35
MC
308 s->client_app_traffic_secret,
309 finsecret, hashlen))
310 goto err;
0edb8194 311 key = finsecret;
de9f5b35 312 }
6484776f 313
38b051a1 314 if (!EVP_Q_mac(sctx->libctx, "HMAC", sctx->propq, mdname,
0a8a6afd
DDO
315 params, key, hashlen, hash, hashlen,
316 /* outsize as per sizeof(peer_finish_md) */
317 out, EVP_MAX_MD_SIZE * 2, &len)) {
c48ffbcc 318 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
6484776f 319 goto err;
d4d2f3a4 320 }
92760c21 321
6484776f 322 err:
c8f6c28a 323 OPENSSL_cleanse(finsecret, sizeof(finsecret));
21dfdbef 324 return len;
92760c21
MC
325}
326
327/*
328 * There isn't really a key block in TLSv1.3, but we still need this function
f5ca0b04 329 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
92760c21 330 */
38b051a1 331int tls13_setup_key_block(SSL_CONNECTION *s)
92760c21
MC
332{
333 const EVP_CIPHER *c;
334 const EVP_MD *hash;
92760c21 335
555cbb32 336 s->session->cipher = s->s3.tmp.new_cipher;
38b051a1
TM
337 if (!ssl_cipher_get_evp(SSL_CONNECTION_GET_CTX(s), s->session, &c, &hash,
338 NULL, NULL, NULL, 0)) {
5a2d0ef3
RL
339 /* Error is already recorded */
340 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
92760c21
MC
341 return 0;
342 }
343
c8f6c28a 344 ssl_evp_cipher_free(s->s3.tmp.new_sym_enc);
555cbb32 345 s->s3.tmp.new_sym_enc = c;
c8f6c28a 346 ssl_evp_md_free(s->s3.tmp.new_hash);
555cbb32 347 s->s3.tmp.new_hash = hash;
92760c21
MC
348
349 return 1;
350}
351
38b051a1
TM
352static int derive_secret_key_and_iv(SSL_CONNECTION *s, int sending,
353 const EVP_MD *md,
d49e23ec 354 const EVP_CIPHER *ciph,
57389a32
MC
355 const unsigned char *insecret,
356 const unsigned char *hash,
357 const unsigned char *label,
358 size_t labellen, unsigned char *secret,
2b891e30
MC
359 unsigned char *key, size_t *keylen,
360 unsigned char *iv, size_t *ivlen,
361 size_t *taglen,
cc10b56d 362 EVP_CIPHER_CTX *ciph_ctx)
57389a32 363{
ed576acd 364 int hashleni = EVP_MD_get_size(md);
bceae201 365 size_t hashlen;
2b891e30 366 int mode;
bceae201
MC
367
368 /* Ensure cast to size_t is safe */
369 if (!ossl_assert(hashleni >= 0)) {
c48ffbcc 370 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
cc10b56d 371 return 0;
bceae201
MC
372 }
373 hashlen = (size_t)hashleni;
57389a32 374
a19ae67d 375 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen,
0fb2815b 376 secret, hashlen, 1)) {
f63a17d6 377 /* SSLfatal() already called */
cc10b56d 378 return 0;
57389a32
MC
379 }
380
2b891e30
MC
381 *keylen = EVP_CIPHER_get_key_length(ciph);
382
383 mode = EVP_CIPHER_get_mode(ciph);
384 if (mode == EVP_CIPH_CCM_MODE) {
c117af67
MC
385 uint32_t algenc;
386
2b891e30 387 *ivlen = EVP_CCM_TLS_IV_LEN;
2e1a4f6a 388 if (s->s3.tmp.new_cipher != NULL) {
389 algenc = s->s3.tmp.new_cipher->algorithm_enc;
390 } else if (s->session->cipher != NULL) {
c117af67
MC
391 /* We've not selected a cipher yet - we must be doing early data */
392 algenc = s->session->cipher->algorithm_enc;
2e1a4f6a 393 } else if (s->psksession != NULL && s->psksession->cipher != NULL) {
394 /* We must be doing early data with out-of-band PSK */
395 algenc = s->psksession->cipher->algorithm_enc;
c117af67 396 } else {
c48ffbcc 397 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
cc10b56d 398 return 0;
c117af67
MC
399 }
400 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
2b891e30 401 *taglen = EVP_CCM8_TLS_TAG_LEN;
57389a32 402 else
2b891e30 403 *taglen = EVP_CCM_TLS_TAG_LEN;
57389a32 404 } else {
cc110a0a
MC
405 int iivlen;
406
2b891e30
MC
407 if (mode == EVP_CIPH_GCM_MODE) {
408 *taglen = EVP_GCM_TLS_TAG_LEN;
409 } else {
410 /* CHACHA20P-POLY1305 */
411 *taglen = EVP_CHACHAPOLY_TLS_TAG_LEN;
412 }
cc110a0a
MC
413 iivlen = EVP_CIPHER_get_iv_length(ciph);
414 if (iivlen < 0) {
415 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
416 return 0;
417 }
418 *ivlen = iivlen;
57389a32
MC
419 }
420
2b891e30
MC
421 if (!tls13_derive_key(s, md, secret, key, *keylen)
422 || !tls13_derive_iv(s, md, secret, iv, *ivlen)) {
f63a17d6 423 /* SSLfatal() already called */
cc10b56d 424 return 0;
57389a32
MC
425 }
426
2b891e30
MC
427 if (sending) {
428 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0
429 || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, *ivlen, NULL) <= 0
430 || (mode == EVP_CIPH_CCM_MODE
431 && EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, *taglen, NULL) <= 0)
432 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
433 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
434 return 0;
435 }
57389a32
MC
436 }
437
57389a32 438 return 1;
57389a32
MC
439}
440
38b051a1 441int tls13_change_cipher_state(SSL_CONNECTION *s, int which)
0d9824c1 442{
48102247 443#ifdef CHARSET_EBCDIC
2b891e30
MC
444 static const unsigned char client_early_traffic[] = {0x63, 0x20, 0x65, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
445 static const unsigned char client_handshake_traffic[] = {0x63, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
446 static const unsigned char client_application_traffic[] = {0x63, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
447 static const unsigned char server_handshake_traffic[] = {0x73, 0x20, 0x68, 0x73, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
448 static const unsigned char server_application_traffic[] = {0x73, 0x20, 0x61, 0x70, 0x20, /*traffic*/0x74, 0x72, 0x61, 0x66, 0x66, 0x69, 0x63, 0x00};
449 static const unsigned char exporter_master_secret[] = {0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
450 static const unsigned char resumption_master_secret[] = {0x72, 0x65, 0x73, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
451 static const unsigned char early_exporter_master_secret[] = {0x65, 0x20, 0x65, 0x78, 0x70, 0x20, /* master*/ 0x6D, 0x61, 0x73, 0x74, 0x65, 0x72, 0x00};
48102247 452#else
17aa119e
MC
453 static const unsigned char client_early_traffic[] = "c e traffic";
454 static const unsigned char client_handshake_traffic[] = "c hs traffic";
455 static const unsigned char client_application_traffic[] = "c ap traffic";
456 static const unsigned char server_handshake_traffic[] = "s hs traffic";
457 static const unsigned char server_application_traffic[] = "s ap traffic";
0ca8d1ec 458 static const unsigned char exporter_master_secret[] = "exp master";
17aa119e 459 static const unsigned char resumption_master_secret[] = "res master";
b38ede80 460 static const unsigned char early_exporter_master_secret[] = "e exp master";
48102247 461#endif
bebc0c7d 462 unsigned char *iv;
cc10b56d 463 unsigned char key[EVP_MAX_KEY_LENGTH];
0d9824c1 464 unsigned char secret[EVP_MAX_MD_SIZE];
ace081c1
MC
465 unsigned char hashval[EVP_MAX_MD_SIZE];
466 unsigned char *hash = hashval;
0d9824c1 467 unsigned char *insecret;
6484776f 468 unsigned char *finsecret = NULL;
2c7bd692 469 const char *log_label = NULL;
2b891e30 470 EVP_CIPHER_CTX *ciph_ctx = NULL;
57389a32 471 size_t finsecretlen = 0;
0d9824c1 472 const unsigned char *label;
ace081c1 473 size_t labellen, hashlen = 0;
6530c490 474 int ret = 0;
42f50fdf
MC
475 const EVP_MD *md = NULL;
476 const EVP_CIPHER *cipher = NULL;
38b051a1 477 SSL_CTX *sctx = SSL_CONNECTION_GET_CTX(s);
2b891e30 478 size_t keylen, ivlen, taglen;
cc10b56d 479#if !defined(OPENSSL_NO_KTLS) && defined(OPENSSL_KTLS_TLS13)
c34ca13a 480 ktls_crypto_info_t crypto_info;
7c78932b 481 void *rl_sequence;
cc10b56d 482 BIO *bio;
cc10b56d 483#endif
0d9824c1
MC
484
485 if (which & SSL3_CC_READ) {
bebc0c7d 486 iv = s->read_iv;
0d9824c1 487 } else {
7426cd34 488 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
0d9824c1
MC
489 if (s->enc_write_ctx != NULL) {
490 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
491 } else {
492 s->enc_write_ctx = EVP_CIPHER_CTX_new();
493 if (s->enc_write_ctx == NULL) {
c48ffbcc 494 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
0d9824c1
MC
495 goto err;
496 }
497 }
498 ciph_ctx = s->enc_write_ctx;
bebc0c7d 499 iv = s->write_iv;
0d9824c1
MC
500
501 RECORD_LAYER_reset_write_sequence(&s->rlayer);
502 }
503
504 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
505 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
d49e23ec
MC
506 if (which & SSL3_CC_EARLY) {
507 EVP_MD_CTX *mdctx = NULL;
508 long handlen;
509 void *hdata;
510 unsigned int hashlenui;
511 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
512
513 insecret = s->early_secret;
514 label = client_early_traffic;
515 labellen = sizeof(client_early_traffic) - 1;
516 log_label = CLIENT_EARLY_LABEL;
517
555cbb32 518 handlen = BIO_get_mem_data(s->s3.handshake_buffer, &hdata);
d49e23ec 519 if (handlen <= 0) {
c48ffbcc 520 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_HANDSHAKE_LENGTH);
d49e23ec
MC
521 goto err;
522 }
add8d0e9 523
08717544
MC
524 if (s->early_data_state == SSL_EARLY_DATA_CONNECTING
525 && s->max_early_data > 0
526 && s->session->ext.max_early_data == 0) {
527 /*
528 * If we are attempting to send early data, and we've decided to
529 * actually do it but max_early_data in s->session is 0 then we
530 * must be using an external PSK.
531 */
532 if (!ossl_assert(s->psksession != NULL
533 && s->max_early_data ==
534 s->psksession->ext.max_early_data)) {
c48ffbcc 535 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
08717544
MC
536 goto err;
537 }
add8d0e9 538 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
08717544 539 }
d49e23ec 540 if (sslcipher == NULL) {
c48ffbcc 541 SSLfatal(s, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_PSK);
d49e23ec
MC
542 goto err;
543 }
544
545 /*
546 * We need to calculate the handshake digest using the digest from
547 * the session. We haven't yet selected our ciphersuite so we can't
548 * use ssl_handshake_md().
549 */
550 mdctx = EVP_MD_CTX_new();
551 if (mdctx == NULL) {
c48ffbcc 552 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_MALLOC_FAILURE);
d49e23ec
MC
553 goto err;
554 }
9727f4e7
MC
555
556 /*
557 * This ups the ref count on cipher so we better make sure we free
558 * it again
559 */
38b051a1 560 if (!ssl_cipher_get_evp_cipher(sctx, sslcipher, &cipher)) {
5a2d0ef3
RL
561 /* Error is already recorded */
562 SSLfatal_alert(s, SSL_AD_INTERNAL_ERROR);
67d744b9 563 EVP_MD_CTX_free(mdctx);
9727f4e7
MC
564 goto err;
565 }
566
38b051a1 567 md = ssl_md(sctx, sslcipher->algorithm2);
d49e23ec
MC
568 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
569 || !EVP_DigestUpdate(mdctx, hdata, handlen)
570 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
c48ffbcc 571 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
d49e23ec
MC
572 EVP_MD_CTX_free(mdctx);
573 goto err;
574 }
575 hashlen = hashlenui;
576 EVP_MD_CTX_free(mdctx);
b38ede80
TT
577
578 if (!tls13_hkdf_expand(s, md, insecret,
579 early_exporter_master_secret,
580 sizeof(early_exporter_master_secret) - 1,
581 hashval, hashlen,
0fb2815b
MC
582 s->early_exporter_master_secret, hashlen,
583 1)) {
c48ffbcc 584 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
b38ede80
TT
585 goto err;
586 }
01a2a654
PW
587
588 if (!ssl_log_secret(s, EARLY_EXPORTER_SECRET_LABEL,
589 s->early_exporter_master_secret, hashlen)) {
590 /* SSLfatal() already called */
591 goto err;
592 }
d49e23ec 593 } else if (which & SSL3_CC_HANDSHAKE) {
0d9824c1 594 insecret = s->handshake_secret;
6484776f 595 finsecret = s->client_finished_secret;
ed576acd 596 finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
0d9824c1
MC
597 label = client_handshake_traffic;
598 labellen = sizeof(client_handshake_traffic) - 1;
2c7bd692 599 log_label = CLIENT_HANDSHAKE_LABEL;
fe5e20fd 600 /*
69687aa8 601 * The handshake hash used for the server read/client write handshake
f7e393be
MC
602 * traffic secret is the same as the hash for the server
603 * write/client read handshake traffic secret. However, if we
604 * processed early data then we delay changing the server
605 * read/client write cipher state until later, and the handshake
606 * hashes have moved on. Therefore we use the value saved earlier
607 * when we did the server write/client read change cipher state.
fe5e20fd 608 */
f7e393be 609 hash = s->handshake_traffic_hash;
0d9824c1 610 } else {
ec15acb6 611 insecret = s->master_secret;
0d9824c1
MC
612 label = client_application_traffic;
613 labellen = sizeof(client_application_traffic) - 1;
2c7bd692 614 log_label = CLIENT_APPLICATION_LABEL;
ace081c1
MC
615 /*
616 * For this we only use the handshake hashes up until the server
617 * Finished hash. We do not include the client's Finished, which is
618 * what ssl_handshake_hash() would give us. Instead we use the
619 * previously saved value.
620 */
621 hash = s->server_finished_hash;
0d9824c1
MC
622 }
623 } else {
d49e23ec 624 /* Early data never applies to client-read/server-write */
0d9824c1
MC
625 if (which & SSL3_CC_HANDSHAKE) {
626 insecret = s->handshake_secret;
6484776f 627 finsecret = s->server_finished_secret;
ed576acd 628 finsecretlen = EVP_MD_get_size(ssl_handshake_md(s));
0d9824c1
MC
629 label = server_handshake_traffic;
630 labellen = sizeof(server_handshake_traffic) - 1;
2c7bd692 631 log_label = SERVER_HANDSHAKE_LABEL;
0d9824c1 632 } else {
ec15acb6 633 insecret = s->master_secret;
0d9824c1
MC
634 label = server_application_traffic;
635 labellen = sizeof(server_application_traffic) - 1;
2c7bd692 636 log_label = SERVER_APPLICATION_LABEL;
0d9824c1
MC
637 }
638 }
639
d49e23ec
MC
640 if (!(which & SSL3_CC_EARLY)) {
641 md = ssl_handshake_md(s);
555cbb32 642 cipher = s->s3.tmp.new_sym_enc;
d49e23ec
MC
643 if (!ssl3_digest_cached_records(s, 1)
644 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
f63a17d6 645 /* SSLfatal() already called */;
d49e23ec
MC
646 goto err;
647 }
ace081c1
MC
648 }
649
ec15acb6
MC
650 /*
651 * Save the hash of handshakes up to now for use when we calculate the
652 * client application traffic secret
653 */
654 if (label == server_application_traffic)
655 memcpy(s->server_finished_hash, hashval, hashlen);
656
f7e393be 657 if (label == server_handshake_traffic)
fe5e20fd
MC
658 memcpy(s->handshake_traffic_hash, hashval, hashlen);
659
ec15acb6
MC
660 if (label == client_application_traffic) {
661 /*
662 * We also create the resumption master secret, but this time use the
663 * hash for the whole handshake including the Client Finished
664 */
665 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
666 resumption_master_secret,
667 sizeof(resumption_master_secret) - 1,
4ff1a526 668 hashval, hashlen, s->resumption_master_secret,
0fb2815b 669 hashlen, 1)) {
f63a17d6 670 /* SSLfatal() already called */
ec15acb6
MC
671 goto err;
672 }
ec15acb6
MC
673 }
674
cc10b56d 675 /* check whether cipher is known */
1287dabd 676 if (!ossl_assert(cipher != NULL))
cc10b56d
VF
677 goto err;
678
d49e23ec 679 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
cc10b56d 680 insecret, hash, label, labellen, secret, key,
2b891e30 681 &keylen, iv, &ivlen, &taglen, ciph_ctx)) {
f63a17d6 682 /* SSLfatal() already called */
57389a32 683 goto err;
ec07b1d8 684 }
0d9824c1 685
2221ec10 686 if (label == server_application_traffic) {
57389a32 687 memcpy(s->server_app_traffic_secret, secret, hashlen);
2221ec10
TT
688 /* Now we create the exporter master secret */
689 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
690 exporter_master_secret,
691 sizeof(exporter_master_secret) - 1,
692 hash, hashlen, s->exporter_master_secret,
0fb2815b 693 hashlen, 1)) {
2221ec10
TT
694 /* SSLfatal() already called */
695 goto err;
696 }
6329ce8f
PW
697
698 if (!ssl_log_secret(s, EXPORTER_SECRET_LABEL, s->exporter_master_secret,
699 hashlen)) {
700 /* SSLfatal() already called */
701 goto err;
702 }
2221ec10 703 } else if (label == client_application_traffic)
57389a32
MC
704 memcpy(s->client_app_traffic_secret, secret, hashlen);
705
2c7bd692 706 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
f63a17d6 707 /* SSLfatal() already called */
2c7bd692
CB
708 goto err;
709 }
710
57389a32
MC
711 if (finsecret != NULL
712 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
713 finsecret, finsecretlen)) {
f63a17d6 714 /* SSLfatal() already called */
0d9824c1
MC
715 goto err;
716 }
717
7426cd34
MC
718 if (!s->server && label == client_early_traffic)
719 s->statem.enc_write_state = ENC_WRITE_STATE_WRITE_PLAIN_ALERTS;
720 else
721 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
2b891e30
MC
722
723 if ((which & SSL3_CC_READ) != 0) {
724 int level = (which & SSL3_CC_EARLY) != 0
725 ? OSSL_RECORD_PROTECTION_LEVEL_EARLY
726 : ((which &SSL3_CC_HANDSHAKE) != 0
727 ? OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE
728 : OSSL_RECORD_PROTECTION_LEVEL_APPLICATION);
79eebb08 729
cc110a0a 730 if (!ssl_set_new_record_layer(s, s->version,
79eebb08
MC
731 OSSL_RECORD_DIRECTION_READ,
732 level, key, keylen, iv, ivlen, NULL, 0,
733 cipher, taglen, NID_undef, NULL, NULL)) {
7c293999 734 /* SSLfatal already called */
2b891e30
MC
735 goto err;
736 }
4564b47d 737 /* TODO(RECLAYER): Remove me when write rlayer done */
cc110a0a 738 goto skip_ktls;
2b891e30
MC
739 }
740
cc10b56d
VF
741#ifndef OPENSSL_NO_KTLS
742# if defined(OPENSSL_KTLS_TLS13)
7c78932b 743 if (!(which & SSL3_CC_APPLICATION)
a3a54179 744 || (s->options & SSL_OP_ENABLE_KTLS) == 0)
cc10b56d
VF
745 goto skip_ktls;
746
747 /* ktls supports only the maximum fragment size */
748 if (ssl_get_max_send_fragment(s) != SSL3_RT_MAX_PLAIN_LENGTH)
749 goto skip_ktls;
750
751 /* ktls does not support record padding */
752 if (s->record_padding_cb != NULL)
753 goto skip_ktls;
754
755 /* check that cipher is supported */
7f2f0ac7 756 if (!ktls_check_supported_cipher(s, cipher, NULL, taglen))
cc10b56d
VF
757 goto skip_ktls;
758
7c78932b
DU
759 if (which & SSL3_CC_WRITE)
760 bio = s->wbio;
761 else
762 bio = s->rbio;
cc10b56d
VF
763
764 if (!ossl_assert(bio != NULL)) {
c48ffbcc 765 SSLfatal(s, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
cc10b56d
VF
766 goto err;
767 }
768
769 /* All future data will get encrypted by ktls. Flush the BIO or skip ktls */
7c78932b
DU
770 if (which & SSL3_CC_WRITE) {
771 if (BIO_flush(bio) <= 0)
772 goto skip_ktls;
773 }
cc10b56d
VF
774
775 /* configure kernel crypto structure */
19d00444
MC
776 /*
777 * If we get here we are only doing the write side. The read side goes
778 * through the new record layer code.
779 */
780 rl_sequence = RECORD_LAYER_get_write_sequence(&s->rlayer);
7c78932b 781
8124ab56
MC
782 if (!ktls_configure_crypto(sctx->libctx, s->version, cipher, NULL,
783 rl_sequence, &crypto_info, which & SSL3_CC_WRITE,
784 iv, ivlen, key, keylen, NULL, 0))
cc10b56d
VF
785 goto skip_ktls;
786
787 /* ktls works with user provided buffers directly */
7c78932b
DU
788 if (BIO_set_ktls(bio, &crypto_info, which & SSL3_CC_WRITE)) {
789 if (which & SSL3_CC_WRITE)
790 ssl3_release_write_buffer(s);
791 }
cc10b56d
VF
792# endif
793#endif
cc110a0a 794skip_ktls:
57389a32
MC
795 ret = 1;
796 err:
9727f4e7
MC
797 if ((which & SSL3_CC_EARLY) != 0) {
798 /* We up-refed this so now we need to down ref */
799 ssl_evp_cipher_free(cipher);
800 }
cc10b56d 801 OPENSSL_cleanse(key, sizeof(key));
57389a32
MC
802 OPENSSL_cleanse(secret, sizeof(secret));
803 return ret;
804}
0d9824c1 805
38b051a1 806int tls13_update_key(SSL_CONNECTION *s, int sending)
57389a32 807{
48102247 808#ifdef CHARSET_EBCDIC
809 static const unsigned char application_traffic[] = { 0x74, 0x72 ,0x61 ,0x66 ,0x66 ,0x69 ,0x63 ,0x20 ,0x75 ,0x70 ,0x64, 0x00};
810#else
811 static const unsigned char application_traffic[] = "traffic upd";
812#endif
57389a32 813 const EVP_MD *md = ssl_handshake_md(s);
ed576acd 814 size_t hashlen = EVP_MD_get_size(md);
cc10b56d 815 unsigned char key[EVP_MAX_KEY_LENGTH];
57389a32
MC
816 unsigned char *insecret, *iv;
817 unsigned char secret[EVP_MAX_MD_SIZE];
818 EVP_CIPHER_CTX *ciph_ctx;
2b891e30 819 size_t keylen, ivlen, taglen;
57389a32 820 int ret = 0;
0d9824c1 821
d1186c30 822 if (s->server == sending)
57389a32
MC
823 insecret = s->server_app_traffic_secret;
824 else
825 insecret = s->client_app_traffic_secret;
bebc0c7d 826
d1186c30 827 if (sending) {
7426cd34 828 s->statem.enc_write_state = ENC_WRITE_STATE_INVALID;
57389a32
MC
829 iv = s->write_iv;
830 ciph_ctx = s->enc_write_ctx;
831 RECORD_LAYER_reset_write_sequence(&s->rlayer);
832 } else {
833 iv = s->read_iv;
834 ciph_ctx = s->enc_read_ctx;
0d9824c1 835 }
57389a32 836
b740012f 837 if (!derive_secret_key_and_iv(s, sending, md,
555cbb32 838 s->s3.tmp.new_sym_enc, insecret, NULL,
d49e23ec 839 application_traffic,
cc10b56d 840 sizeof(application_traffic) - 1, secret, key,
2b891e30 841 &keylen, iv, &ivlen, &taglen, ciph_ctx)) {
f63a17d6 842 /* SSLfatal() already called */
57389a32 843 goto err;
f63a17d6 844 }
57389a32
MC
845
846 memcpy(insecret, secret, hashlen);
0d9824c1 847
2b891e30 848 if (!sending) {
cc110a0a 849 if (!ssl_set_new_record_layer(s, s->version,
79eebb08
MC
850 OSSL_RECORD_DIRECTION_READ,
851 OSSL_RECORD_PROTECTION_LEVEL_APPLICATION,
852 key, keylen, iv, ivlen, NULL, 0,
853 s->s3.tmp.new_sym_enc, taglen, NID_undef, NULL,
854 NULL)) {
7c293999 855 /* SSLfatal already called */
2b891e30
MC
856 goto err;
857 }
858 }
859
7426cd34 860 s->statem.enc_write_state = ENC_WRITE_STATE_VALID;
6530c490 861 ret = 1;
0d9824c1 862 err:
cc10b56d 863 OPENSSL_cleanse(key, sizeof(key));
0d9824c1 864 OPENSSL_cleanse(secret, sizeof(secret));
6530c490 865 return ret;
0d9824c1 866}
04904312
MC
867
868int tls13_alert_code(int code)
869{
43a0f273
MC
870 /* There are 2 additional alerts in TLSv1.3 compared to TLSv1.2 */
871 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_CERTIFICATE_REQUIRED)
04904312
MC
872 return code;
873
874 return tls1_alert_code(code);
875}
0ca8d1ec 876
38b051a1
TM
877int tls13_export_keying_material(SSL_CONNECTION *s,
878 unsigned char *out, size_t olen,
0ca8d1ec
MC
879 const char *label, size_t llen,
880 const unsigned char *context,
881 size_t contextlen, int use_context)
882{
883 unsigned char exportsecret[EVP_MAX_MD_SIZE];
48102247 884#ifdef CHARSET_EBCDIC
885 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
886#else
0ca8d1ec 887 static const unsigned char exporterlabel[] = "exporter";
48102247 888#endif
c8b93876 889 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
0ca8d1ec
MC
890 const EVP_MD *md = ssl_handshake_md(s);
891 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
c8b93876 892 unsigned int hashsize, datalen;
0ca8d1ec
MC
893 int ret = 0;
894
b740012f 895 if (ctx == NULL || md == NULL || !ossl_statem_export_allowed(s))
0ca8d1ec
MC
896 goto err;
897
898 if (!use_context)
899 contextlen = 0;
900
901 if (EVP_DigestInit_ex(ctx, md, NULL) <= 0
902 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
903 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
c8b93876
TT
904 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
905 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
0ca8d1ec 906 || !tls13_hkdf_expand(s, md, s->exporter_master_secret,
c8b93876 907 (const unsigned char *)label, llen,
0fb2815b 908 data, datalen, exportsecret, hashsize, 0)
0ca8d1ec 909 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
a19ae67d 910 sizeof(exporterlabel) - 1, hash, hashsize,
0fb2815b 911 out, olen, 0))
0ca8d1ec
MC
912 goto err;
913
914 ret = 1;
915 err:
916 EVP_MD_CTX_free(ctx);
917 return ret;
918}
b38ede80 919
38b051a1
TM
920int tls13_export_keying_material_early(SSL_CONNECTION *s,
921 unsigned char *out, size_t olen,
b38ede80
TT
922 const char *label, size_t llen,
923 const unsigned char *context,
924 size_t contextlen)
925{
48102247 926#ifdef CHARSET_EBCDIC
927 static const unsigned char exporterlabel[] = {0x65, 0x78, 0x70, 0x6F, 0x72, 0x74, 0x65, 0x72, 0x00};
928#else
929 static const unsigned char exporterlabel[] = "exporter";
930#endif
b38ede80
TT
931 unsigned char exportsecret[EVP_MAX_MD_SIZE];
932 unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE];
933 const EVP_MD *md;
934 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
935 unsigned int hashsize, datalen;
936 int ret = 0;
937 const SSL_CIPHER *sslcipher;
938
939 if (ctx == NULL || !ossl_statem_export_early_allowed(s))
940 goto err;
941
942 if (!s->server && s->max_early_data > 0
943 && s->session->ext.max_early_data == 0)
944 sslcipher = SSL_SESSION_get0_cipher(s->psksession);
945 else
946 sslcipher = SSL_SESSION_get0_cipher(s->session);
947
38b051a1 948 md = ssl_md(SSL_CONNECTION_GET_CTX(s), sslcipher->algorithm2);
b38ede80
TT
949
950 /*
951 * Calculate the hash value and store it in |data|. The reason why
952 * the empty string is used is that the definition of TLS-Exporter
953 * is like so:
954 *
955 * TLS-Exporter(label, context_value, key_length) =
956 * HKDF-Expand-Label(Derive-Secret(Secret, label, ""),
957 * "exporter", Hash(context_value), key_length)
958 *
959 * Derive-Secret(Secret, Label, Messages) =
960 * HKDF-Expand-Label(Secret, Label,
961 * Transcript-Hash(Messages), Hash.length)
962 *
963 * Here Transcript-Hash is the cipher suite hash algorithm.
964 */
b740012f 965 if (md == NULL
966 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
b38ede80
TT
967 || EVP_DigestUpdate(ctx, context, contextlen) <= 0
968 || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0
969 || EVP_DigestInit_ex(ctx, md, NULL) <= 0
970 || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0
971 || !tls13_hkdf_expand(s, md, s->early_exporter_master_secret,
972 (const unsigned char *)label, llen,
0fb2815b 973 data, datalen, exportsecret, hashsize, 0)
b38ede80
TT
974 || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel,
975 sizeof(exporterlabel) - 1, hash, hashsize,
0fb2815b 976 out, olen, 0))
b38ede80
TT
977 goto err;
978
979 ret = 1;
980 err:
981 EVP_MD_CTX_free(ctx);
982 return ret;
983}