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