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