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