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