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