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