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