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