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