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