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