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