]>
Commit | Line | Data |
---|---|---|
34574f19 MC |
1 | /* |
2 | * Copyright 2016 The OpenSSL Project Authors. All Rights Reserved. | |
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" | |
12 | #include <openssl/evp.h> | |
13 | #include <openssl/kdf.h> | |
14 | ||
15 | #define TLS13_MAX_LABEL_LEN 246 | |
16 | ||
17 | /* Always filled with zeros */ | |
18 | static const unsigned char default_zeros[EVP_MAX_MD_SIZE]; | |
19 | ||
34574f19 | 20 | /* |
a19ae67d MC |
21 | * Given a |secret|; a |label| of length |labellen|; and |data| of length |
22 | * |datalen| (e.g. typically a hash of the handshake messages), derive a new | |
23 | * secret |outlen| bytes long and store it in the location pointed to be |out|. | |
24 | * The |data| value may be zero length. Returns 1 on success 0 on failure. | |
34574f19 | 25 | */ |
ec15acb6 | 26 | int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret, |
34574f19 | 27 | const unsigned char *label, size_t labellen, |
a19ae67d | 28 | const unsigned char *data, size_t datalen, |
34574f19 MC |
29 | unsigned char *out, size_t outlen) |
30 | { | |
17aa119e | 31 | const unsigned char label_prefix[] = "tls13 "; |
34574f19 MC |
32 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); |
33 | int ret; | |
34 | size_t hkdflabellen; | |
35 | size_t hashlen; | |
36 | /* | |
37 | * 2 bytes for length of whole HkdfLabel + 1 byte for length of combined | |
38 | * prefix and label + bytes for the label itself + bytes for the hash | |
39 | */ | |
40 | unsigned char hkdflabel[sizeof(uint16_t) + sizeof(uint8_t) + | |
41 | + sizeof(label_prefix) + TLS13_MAX_LABEL_LEN | |
42 | + EVP_MAX_MD_SIZE]; | |
43 | WPACKET pkt; | |
44 | ||
45 | if (pctx == NULL) | |
46 | return 0; | |
47 | ||
48 | hashlen = EVP_MD_size(md); | |
49 | ||
50 | if (!WPACKET_init_static_len(&pkt, hkdflabel, sizeof(hkdflabel), 0) | |
51 | || !WPACKET_put_bytes_u16(&pkt, outlen) | |
52 | || !WPACKET_start_sub_packet_u8(&pkt) | |
53 | || !WPACKET_memcpy(&pkt, label_prefix, sizeof(label_prefix) - 1) | |
54 | || !WPACKET_memcpy(&pkt, label, labellen) | |
55 | || !WPACKET_close(&pkt) | |
a19ae67d | 56 | || !WPACKET_sub_memcpy_u8(&pkt, data, (data == NULL) ? 0 : datalen) |
34574f19 MC |
57 | || !WPACKET_get_total_written(&pkt, &hkdflabellen) |
58 | || !WPACKET_finish(&pkt)) { | |
d49e23ec | 59 | EVP_PKEY_CTX_free(pctx); |
34574f19 MC |
60 | WPACKET_cleanup(&pkt); |
61 | return 0; | |
62 | } | |
63 | ||
64 | ret = EVP_PKEY_derive_init(pctx) <= 0 | |
65 | || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) | |
66 | <= 0 | |
67 | || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0 | |
68 | || EVP_PKEY_CTX_set1_hkdf_key(pctx, secret, hashlen) <= 0 | |
69 | || EVP_PKEY_CTX_add1_hkdf_info(pctx, hkdflabel, hkdflabellen) <= 0 | |
70 | || EVP_PKEY_derive(pctx, out, &outlen) <= 0; | |
71 | ||
72 | EVP_PKEY_CTX_free(pctx); | |
73 | ||
74 | return ret == 0; | |
75 | } | |
76 | ||
34574f19 | 77 | /* |
f5ca0b04 MC |
78 | * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on |
79 | * success 0 on failure. | |
34574f19 | 80 | */ |
d49e23ec MC |
81 | int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret, |
82 | unsigned char *key, size_t keylen) | |
34574f19 | 83 | { |
f5ca0b04 MC |
84 | static const unsigned char keylabel[] = "key"; |
85 | ||
d49e23ec | 86 | return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1, |
a19ae67d | 87 | NULL, 0, key, keylen); |
34574f19 MC |
88 | } |
89 | ||
90 | /* | |
f5ca0b04 MC |
91 | * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on |
92 | * success 0 on failure. | |
34574f19 | 93 | */ |
d49e23ec MC |
94 | int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret, |
95 | unsigned char *iv, size_t ivlen) | |
34574f19 | 96 | { |
f5ca0b04 MC |
97 | static const unsigned char ivlabel[] = "iv"; |
98 | ||
d49e23ec | 99 | return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1, |
a19ae67d | 100 | NULL, 0, iv, ivlen); |
34574f19 MC |
101 | } |
102 | ||
ec15acb6 MC |
103 | int tls13_derive_finishedkey(SSL *s, const EVP_MD *md, |
104 | const unsigned char *secret, | |
105 | unsigned char *fin, size_t finlen) | |
6484776f | 106 | { |
f5ca0b04 MC |
107 | static const unsigned char finishedlabel[] = "finished"; |
108 | ||
ec15acb6 | 109 | return tls13_hkdf_expand(s, md, secret, finishedlabel, |
a19ae67d | 110 | sizeof(finishedlabel) - 1, NULL, 0, fin, finlen); |
6484776f MC |
111 | } |
112 | ||
34574f19 MC |
113 | /* |
114 | * Given the previous secret |prevsecret| and a new input secret |insecret| of | |
115 | * length |insecretlen|, generate a new secret and store it in the location | |
f5ca0b04 | 116 | * pointed to by |outsecret|. Returns 1 on success 0 on failure. |
34574f19 | 117 | */ |
ec15acb6 MC |
118 | int tls13_generate_secret(SSL *s, const EVP_MD *md, |
119 | const unsigned char *prevsecret, | |
120 | const unsigned char *insecret, | |
121 | size_t insecretlen, | |
122 | unsigned char *outsecret) | |
34574f19 | 123 | { |
34574f19 MC |
124 | size_t mdlen, prevsecretlen; |
125 | int ret; | |
126 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL); | |
17aa119e | 127 | static const char derived_secret_label[] = "derived"; |
3e0458fb | 128 | unsigned char preextractsec[EVP_MAX_MD_SIZE]; |
34574f19 MC |
129 | |
130 | if (pctx == NULL) | |
131 | return 0; | |
132 | ||
133 | mdlen = EVP_MD_size(md); | |
134 | ||
135 | if (insecret == NULL) { | |
136 | insecret = default_zeros; | |
137 | insecretlen = mdlen; | |
138 | } | |
139 | if (prevsecret == NULL) { | |
140 | prevsecret = default_zeros; | |
141 | prevsecretlen = 0; | |
142 | } else { | |
3e0458fb MC |
143 | EVP_MD_CTX *mctx = EVP_MD_CTX_new(); |
144 | unsigned char hash[EVP_MAX_MD_SIZE]; | |
145 | ||
146 | /* The pre-extract derive step uses a hash of no messages */ | |
147 | if (mctx == NULL | |
148 | || EVP_DigestInit_ex(mctx, md, NULL) <= 0 | |
149 | || EVP_DigestFinal_ex(mctx, hash, NULL) <= 0) { | |
150 | EVP_MD_CTX_free(mctx); | |
71d8c138 | 151 | EVP_PKEY_CTX_free(pctx); |
3e0458fb MC |
152 | return 0; |
153 | } | |
154 | EVP_MD_CTX_free(mctx); | |
155 | ||
156 | /* Generate the pre-extract secret */ | |
157 | if (!tls13_hkdf_expand(s, md, prevsecret, | |
158 | (unsigned char *)derived_secret_label, | |
a19ae67d | 159 | sizeof(derived_secret_label) - 1, hash, mdlen, |
71d8c138 BE |
160 | preextractsec, mdlen)) { |
161 | EVP_PKEY_CTX_free(pctx); | |
3e0458fb | 162 | return 0; |
71d8c138 | 163 | } |
3e0458fb MC |
164 | |
165 | prevsecret = preextractsec; | |
34574f19 MC |
166 | prevsecretlen = mdlen; |
167 | } | |
168 | ||
169 | ret = EVP_PKEY_derive_init(pctx) <= 0 | |
170 | || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) | |
171 | <= 0 | |
172 | || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0 | |
173 | || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0 | |
174 | || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen) | |
175 | <= 0 | |
176 | || EVP_PKEY_derive(pctx, outsecret, &mdlen) | |
177 | <= 0; | |
178 | ||
179 | EVP_PKEY_CTX_free(pctx); | |
3e0458fb MC |
180 | if (prevsecret == preextractsec) |
181 | OPENSSL_cleanse(preextractsec, mdlen); | |
34574f19 MC |
182 | return ret == 0; |
183 | } | |
184 | ||
34574f19 MC |
185 | /* |
186 | * Given an input secret |insecret| of length |insecretlen| generate the | |
187 | * handshake secret. This requires the early secret to already have been | |
f5ca0b04 | 188 | * generated. Returns 1 on success 0 on failure. |
34574f19 MC |
189 | */ |
190 | int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret, | |
191 | size_t insecretlen) | |
192 | { | |
ec15acb6 MC |
193 | return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret, |
194 | insecret, insecretlen, | |
34574f19 MC |
195 | (unsigned char *)&s->handshake_secret); |
196 | } | |
197 | ||
198 | /* | |
199 | * Given the handshake secret |prev| of length |prevlen| generate the master | |
f5ca0b04 MC |
200 | * secret and store its length in |*secret_size|. Returns 1 on success 0 on |
201 | * failure. | |
34574f19 MC |
202 | */ |
203 | int tls13_generate_master_secret(SSL *s, unsigned char *out, | |
204 | unsigned char *prev, size_t prevlen, | |
205 | size_t *secret_size) | |
206 | { | |
ec15acb6 MC |
207 | const EVP_MD *md = ssl_handshake_md(s); |
208 | ||
209 | *secret_size = EVP_MD_size(md); | |
210 | return tls13_generate_secret(s, md, prev, NULL, 0, out); | |
34574f19 MC |
211 | } |
212 | ||
92760c21 | 213 | /* |
f5ca0b04 MC |
214 | * Generates the mac for the Finished message. Returns the length of the MAC or |
215 | * 0 on error. | |
92760c21 MC |
216 | */ |
217 | size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen, | |
218 | unsigned char *out) | |
219 | { | |
6484776f MC |
220 | const EVP_MD *md = ssl_handshake_md(s); |
221 | unsigned char hash[EVP_MAX_MD_SIZE]; | |
222 | size_t hashlen, ret = 0; | |
223 | EVP_PKEY *key = NULL; | |
224 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | |
92760c21 | 225 | |
6484776f MC |
226 | if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) |
227 | goto err; | |
228 | ||
229 | if (str == s->method->ssl3_enc->server_finished_label) | |
230 | key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, | |
231 | s->server_finished_secret, hashlen); | |
232 | else | |
233 | key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, | |
234 | s->client_finished_secret, hashlen); | |
235 | ||
236 | if (key == NULL | |
237 | || ctx == NULL | |
238 | || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0 | |
239 | || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0 | |
240 | || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) | |
241 | goto err; | |
92760c21 | 242 | |
6484776f MC |
243 | ret = hashlen; |
244 | err: | |
245 | EVP_PKEY_free(key); | |
246 | EVP_MD_CTX_free(ctx); | |
247 | return ret; | |
92760c21 MC |
248 | } |
249 | ||
250 | /* | |
251 | * There isn't really a key block in TLSv1.3, but we still need this function | |
f5ca0b04 | 252 | * for initialising the cipher and hash. Returns 1 on success or 0 on failure. |
92760c21 MC |
253 | */ |
254 | int tls13_setup_key_block(SSL *s) | |
255 | { | |
256 | const EVP_CIPHER *c; | |
257 | const EVP_MD *hash; | |
258 | int mac_type = NID_undef; | |
259 | ||
260 | s->session->cipher = s->s3->tmp.new_cipher; | |
261 | if (!ssl_cipher_get_evp | |
262 | (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) { | |
263 | SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); | |
264 | return 0; | |
265 | } | |
266 | ||
267 | s->s3->tmp.new_sym_enc = c; | |
268 | s->s3->tmp.new_hash = hash; | |
269 | ||
270 | return 1; | |
271 | } | |
272 | ||
d1186c30 | 273 | static int derive_secret_key_and_iv(SSL *s, int sending, const EVP_MD *md, |
d49e23ec | 274 | const EVP_CIPHER *ciph, |
57389a32 MC |
275 | const unsigned char *insecret, |
276 | const unsigned char *hash, | |
277 | const unsigned char *label, | |
278 | size_t labellen, unsigned char *secret, | |
279 | unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx) | |
280 | { | |
281 | unsigned char key[EVP_MAX_KEY_LENGTH]; | |
282 | size_t ivlen, keylen, taglen; | |
57389a32 | 283 | size_t hashlen = EVP_MD_size(md); |
57389a32 | 284 | |
a19ae67d MC |
285 | if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, hashlen, |
286 | secret, hashlen)) { | |
57389a32 MC |
287 | SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR); |
288 | goto err; | |
289 | } | |
290 | ||
291 | /* TODO(size_t): convert me */ | |
292 | keylen = EVP_CIPHER_key_length(ciph); | |
293 | if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) { | |
c117af67 MC |
294 | uint32_t algenc; |
295 | ||
57389a32 | 296 | ivlen = EVP_CCM_TLS_IV_LEN; |
c117af67 MC |
297 | if (s->s3->tmp.new_cipher == NULL) { |
298 | /* We've not selected a cipher yet - we must be doing early data */ | |
299 | algenc = s->session->cipher->algorithm_enc; | |
300 | } else { | |
301 | algenc = s->s3->tmp.new_cipher->algorithm_enc; | |
302 | } | |
303 | if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8)) | |
57389a32 MC |
304 | taglen = EVP_CCM8_TLS_TAG_LEN; |
305 | else | |
306 | taglen = EVP_CCM_TLS_TAG_LEN; | |
307 | } else { | |
308 | ivlen = EVP_CIPHER_iv_length(ciph); | |
309 | taglen = 0; | |
310 | } | |
311 | ||
d49e23ec MC |
312 | if (!tls13_derive_key(s, md, secret, key, keylen) |
313 | || !tls13_derive_iv(s, md, secret, iv, ivlen)) { | |
57389a32 MC |
314 | SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR); |
315 | goto err; | |
316 | } | |
317 | ||
d1186c30 | 318 | if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, sending) <= 0 |
57389a32 MC |
319 | || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) |
320 | || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, | |
321 | taglen, NULL)) | |
322 | || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) { | |
323 | SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_EVP_LIB); | |
324 | goto err; | |
325 | } | |
326 | ||
57389a32 MC |
327 | return 1; |
328 | err: | |
329 | OPENSSL_cleanse(key, sizeof(key)); | |
330 | return 0; | |
331 | } | |
332 | ||
0d9824c1 MC |
333 | int tls13_change_cipher_state(SSL *s, int which) |
334 | { | |
17aa119e MC |
335 | static const unsigned char client_early_traffic[] = "c e traffic"; |
336 | static const unsigned char client_handshake_traffic[] = "c hs traffic"; | |
337 | static const unsigned char client_application_traffic[] = "c ap traffic"; | |
338 | static const unsigned char server_handshake_traffic[] = "s hs traffic"; | |
339 | static const unsigned char server_application_traffic[] = "s ap traffic"; | |
0ca8d1ec | 340 | static const unsigned char exporter_master_secret[] = "exp master"; |
17aa119e | 341 | static const unsigned char resumption_master_secret[] = "res master"; |
bebc0c7d | 342 | unsigned char *iv; |
0d9824c1 | 343 | unsigned char secret[EVP_MAX_MD_SIZE]; |
ace081c1 MC |
344 | unsigned char hashval[EVP_MAX_MD_SIZE]; |
345 | unsigned char *hash = hashval; | |
0d9824c1 | 346 | unsigned char *insecret; |
6484776f | 347 | unsigned char *finsecret = NULL; |
2c7bd692 | 348 | const char *log_label = NULL; |
0d9824c1 | 349 | EVP_CIPHER_CTX *ciph_ctx; |
57389a32 | 350 | size_t finsecretlen = 0; |
0d9824c1 | 351 | const unsigned char *label; |
ace081c1 | 352 | size_t labellen, hashlen = 0; |
6530c490 | 353 | int ret = 0; |
42f50fdf MC |
354 | const EVP_MD *md = NULL; |
355 | const EVP_CIPHER *cipher = NULL; | |
0d9824c1 MC |
356 | |
357 | if (which & SSL3_CC_READ) { | |
358 | if (s->enc_read_ctx != NULL) { | |
359 | EVP_CIPHER_CTX_reset(s->enc_read_ctx); | |
360 | } else { | |
361 | s->enc_read_ctx = EVP_CIPHER_CTX_new(); | |
362 | if (s->enc_read_ctx == NULL) { | |
363 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
364 | goto err; | |
365 | } | |
366 | } | |
367 | ciph_ctx = s->enc_read_ctx; | |
bebc0c7d | 368 | iv = s->read_iv; |
0d9824c1 MC |
369 | |
370 | RECORD_LAYER_reset_read_sequence(&s->rlayer); | |
371 | } else { | |
372 | if (s->enc_write_ctx != NULL) { | |
373 | EVP_CIPHER_CTX_reset(s->enc_write_ctx); | |
374 | } else { | |
375 | s->enc_write_ctx = EVP_CIPHER_CTX_new(); | |
376 | if (s->enc_write_ctx == NULL) { | |
377 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
378 | goto err; | |
379 | } | |
380 | } | |
381 | ciph_ctx = s->enc_write_ctx; | |
bebc0c7d | 382 | iv = s->write_iv; |
0d9824c1 MC |
383 | |
384 | RECORD_LAYER_reset_write_sequence(&s->rlayer); | |
385 | } | |
386 | ||
387 | if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE)) | |
388 | || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) { | |
d49e23ec MC |
389 | if (which & SSL3_CC_EARLY) { |
390 | EVP_MD_CTX *mdctx = NULL; | |
391 | long handlen; | |
392 | void *hdata; | |
393 | unsigned int hashlenui; | |
394 | const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session); | |
395 | ||
396 | insecret = s->early_secret; | |
397 | label = client_early_traffic; | |
398 | labellen = sizeof(client_early_traffic) - 1; | |
399 | log_label = CLIENT_EARLY_LABEL; | |
400 | ||
401 | handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
402 | if (handlen <= 0) { | |
403 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, | |
404 | SSL_R_BAD_HANDSHAKE_LENGTH); | |
405 | goto err; | |
406 | } | |
407 | if (sslcipher == NULL) { | |
408 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
409 | goto err; | |
410 | } | |
411 | ||
412 | /* | |
413 | * We need to calculate the handshake digest using the digest from | |
414 | * the session. We haven't yet selected our ciphersuite so we can't | |
415 | * use ssl_handshake_md(). | |
416 | */ | |
417 | mdctx = EVP_MD_CTX_new(); | |
418 | if (mdctx == NULL) { | |
419 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
420 | goto err; | |
421 | } | |
422 | cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher)); | |
423 | md = ssl_md(sslcipher->algorithm2); | |
424 | if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL) | |
425 | || !EVP_DigestUpdate(mdctx, hdata, handlen) | |
426 | || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) { | |
427 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
428 | EVP_MD_CTX_free(mdctx); | |
429 | goto err; | |
430 | } | |
431 | hashlen = hashlenui; | |
432 | EVP_MD_CTX_free(mdctx); | |
433 | } else if (which & SSL3_CC_HANDSHAKE) { | |
0d9824c1 | 434 | insecret = s->handshake_secret; |
6484776f | 435 | finsecret = s->client_finished_secret; |
6612d87b | 436 | finsecretlen = EVP_MD_size(ssl_handshake_md(s)); |
0d9824c1 MC |
437 | label = client_handshake_traffic; |
438 | labellen = sizeof(client_handshake_traffic) - 1; | |
2c7bd692 | 439 | log_label = CLIENT_HANDSHAKE_LABEL; |
fe5e20fd | 440 | /* |
69687aa8 | 441 | * The handshake hash used for the server read/client write handshake |
f7e393be MC |
442 | * traffic secret is the same as the hash for the server |
443 | * write/client read handshake traffic secret. However, if we | |
444 | * processed early data then we delay changing the server | |
445 | * read/client write cipher state until later, and the handshake | |
446 | * hashes have moved on. Therefore we use the value saved earlier | |
447 | * when we did the server write/client read change cipher state. | |
fe5e20fd | 448 | */ |
f7e393be | 449 | hash = s->handshake_traffic_hash; |
0d9824c1 | 450 | } else { |
ec15acb6 | 451 | insecret = s->master_secret; |
0d9824c1 MC |
452 | label = client_application_traffic; |
453 | labellen = sizeof(client_application_traffic) - 1; | |
2c7bd692 | 454 | log_label = CLIENT_APPLICATION_LABEL; |
ace081c1 MC |
455 | /* |
456 | * For this we only use the handshake hashes up until the server | |
457 | * Finished hash. We do not include the client's Finished, which is | |
458 | * what ssl_handshake_hash() would give us. Instead we use the | |
459 | * previously saved value. | |
460 | */ | |
461 | hash = s->server_finished_hash; | |
0d9824c1 MC |
462 | } |
463 | } else { | |
d49e23ec | 464 | /* Early data never applies to client-read/server-write */ |
0d9824c1 MC |
465 | if (which & SSL3_CC_HANDSHAKE) { |
466 | insecret = s->handshake_secret; | |
6484776f | 467 | finsecret = s->server_finished_secret; |
6612d87b | 468 | finsecretlen = EVP_MD_size(ssl_handshake_md(s)); |
0d9824c1 MC |
469 | label = server_handshake_traffic; |
470 | labellen = sizeof(server_handshake_traffic) - 1; | |
2c7bd692 | 471 | log_label = SERVER_HANDSHAKE_LABEL; |
0d9824c1 | 472 | } else { |
ec15acb6 | 473 | insecret = s->master_secret; |
0d9824c1 MC |
474 | label = server_application_traffic; |
475 | labellen = sizeof(server_application_traffic) - 1; | |
2c7bd692 | 476 | log_label = SERVER_APPLICATION_LABEL; |
0d9824c1 MC |
477 | } |
478 | } | |
479 | ||
d49e23ec MC |
480 | if (!(which & SSL3_CC_EARLY)) { |
481 | md = ssl_handshake_md(s); | |
482 | cipher = s->s3->tmp.new_sym_enc; | |
483 | if (!ssl3_digest_cached_records(s, 1) | |
484 | || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) { | |
485 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
486 | goto err; | |
487 | } | |
ace081c1 MC |
488 | } |
489 | ||
ec15acb6 MC |
490 | /* |
491 | * Save the hash of handshakes up to now for use when we calculate the | |
492 | * client application traffic secret | |
493 | */ | |
494 | if (label == server_application_traffic) | |
495 | memcpy(s->server_finished_hash, hashval, hashlen); | |
496 | ||
f7e393be | 497 | if (label == server_handshake_traffic) |
fe5e20fd MC |
498 | memcpy(s->handshake_traffic_hash, hashval, hashlen); |
499 | ||
ec15acb6 MC |
500 | if (label == client_application_traffic) { |
501 | /* | |
502 | * We also create the resumption master secret, but this time use the | |
503 | * hash for the whole handshake including the Client Finished | |
504 | */ | |
505 | if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret, | |
506 | resumption_master_secret, | |
507 | sizeof(resumption_master_secret) - 1, | |
a19ae67d MC |
508 | hashval, hashlen, s->session->master_key, |
509 | hashlen)) { | |
ec15acb6 MC |
510 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); |
511 | goto err; | |
512 | } | |
513 | s->session->master_key_length = hashlen; | |
0ca8d1ec MC |
514 | |
515 | /* Now we create the exporter master secret */ | |
516 | if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret, | |
517 | exporter_master_secret, | |
518 | sizeof(exporter_master_secret) - 1, | |
a19ae67d MC |
519 | hash, hashlen, s->exporter_master_secret, |
520 | hashlen)) { | |
0ca8d1ec MC |
521 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); |
522 | goto err; | |
523 | } | |
ec15acb6 MC |
524 | } |
525 | ||
d49e23ec MC |
526 | if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher, |
527 | insecret, hash, label, labellen, secret, iv, | |
528 | ciph_ctx)) { | |
57389a32 | 529 | goto err; |
ec07b1d8 | 530 | } |
0d9824c1 | 531 | |
57389a32 MC |
532 | if (label == server_application_traffic) |
533 | memcpy(s->server_app_traffic_secret, secret, hashlen); | |
534 | else if (label == client_application_traffic) | |
535 | memcpy(s->client_app_traffic_secret, secret, hashlen); | |
536 | ||
2c7bd692 CB |
537 | if (!ssl_log_secret(s, log_label, secret, hashlen)) { |
538 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
539 | goto err; | |
540 | } | |
541 | ||
57389a32 MC |
542 | if (finsecret != NULL |
543 | && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret, | |
544 | finsecret, finsecretlen)) { | |
0d9824c1 MC |
545 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); |
546 | goto err; | |
547 | } | |
548 | ||
57389a32 MC |
549 | ret = 1; |
550 | err: | |
551 | OPENSSL_cleanse(secret, sizeof(secret)); | |
552 | return ret; | |
553 | } | |
0d9824c1 | 554 | |
d1186c30 | 555 | int tls13_update_key(SSL *s, int sending) |
57389a32 | 556 | { |
17aa119e | 557 | static const unsigned char application_traffic[] = "traffic upd"; |
57389a32 MC |
558 | const EVP_MD *md = ssl_handshake_md(s); |
559 | size_t hashlen = EVP_MD_size(md); | |
560 | unsigned char *insecret, *iv; | |
561 | unsigned char secret[EVP_MAX_MD_SIZE]; | |
562 | EVP_CIPHER_CTX *ciph_ctx; | |
563 | int ret = 0; | |
0d9824c1 | 564 | |
d1186c30 | 565 | if (s->server == sending) |
57389a32 MC |
566 | insecret = s->server_app_traffic_secret; |
567 | else | |
568 | insecret = s->client_app_traffic_secret; | |
bebc0c7d | 569 | |
d1186c30 | 570 | if (sending) { |
57389a32 MC |
571 | iv = s->write_iv; |
572 | ciph_ctx = s->enc_write_ctx; | |
573 | RECORD_LAYER_reset_write_sequence(&s->rlayer); | |
574 | } else { | |
575 | iv = s->read_iv; | |
576 | ciph_ctx = s->enc_read_ctx; | |
577 | RECORD_LAYER_reset_read_sequence(&s->rlayer); | |
0d9824c1 | 578 | } |
57389a32 | 579 | |
d1186c30 | 580 | if (!derive_secret_key_and_iv(s, sending, ssl_handshake_md(s), |
d49e23ec MC |
581 | s->s3->tmp.new_sym_enc, insecret, NULL, |
582 | application_traffic, | |
57389a32 MC |
583 | sizeof(application_traffic) - 1, secret, iv, |
584 | ciph_ctx)) | |
585 | goto err; | |
586 | ||
587 | memcpy(insecret, secret, hashlen); | |
0d9824c1 | 588 | |
6530c490 | 589 | ret = 1; |
0d9824c1 MC |
590 | err: |
591 | OPENSSL_cleanse(secret, sizeof(secret)); | |
6530c490 | 592 | return ret; |
0d9824c1 | 593 | } |
04904312 MC |
594 | |
595 | int tls13_alert_code(int code) | |
596 | { | |
ef6c191b | 597 | if (code == SSL_AD_MISSING_EXTENSION) |
04904312 MC |
598 | return code; |
599 | ||
600 | return tls1_alert_code(code); | |
601 | } | |
0ca8d1ec MC |
602 | |
603 | int tls13_export_keying_material(SSL *s, unsigned char *out, size_t olen, | |
604 | const char *label, size_t llen, | |
605 | const unsigned char *context, | |
606 | size_t contextlen, int use_context) | |
607 | { | |
608 | unsigned char exportsecret[EVP_MAX_MD_SIZE]; | |
609 | static const unsigned char exporterlabel[] = "exporter"; | |
c8b93876 | 610 | unsigned char hash[EVP_MAX_MD_SIZE], data[EVP_MAX_MD_SIZE]; |
0ca8d1ec MC |
611 | const EVP_MD *md = ssl_handshake_md(s); |
612 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | |
c8b93876 | 613 | unsigned int hashsize, datalen; |
0ca8d1ec MC |
614 | int ret = 0; |
615 | ||
a599574b | 616 | if (ctx == NULL || !SSL_is_init_finished(s)) |
0ca8d1ec MC |
617 | goto err; |
618 | ||
619 | if (!use_context) | |
620 | contextlen = 0; | |
621 | ||
622 | if (EVP_DigestInit_ex(ctx, md, NULL) <= 0 | |
623 | || EVP_DigestUpdate(ctx, context, contextlen) <= 0 | |
624 | || EVP_DigestFinal_ex(ctx, hash, &hashsize) <= 0 | |
c8b93876 TT |
625 | || EVP_DigestInit_ex(ctx, md, NULL) <= 0 |
626 | || EVP_DigestFinal_ex(ctx, data, &datalen) <= 0 | |
0ca8d1ec | 627 | || !tls13_hkdf_expand(s, md, s->exporter_master_secret, |
c8b93876 TT |
628 | (const unsigned char *)label, llen, |
629 | data, datalen, exportsecret, hashsize) | |
0ca8d1ec | 630 | || !tls13_hkdf_expand(s, md, exportsecret, exporterlabel, |
a19ae67d MC |
631 | sizeof(exporterlabel) - 1, hash, hashsize, |
632 | out, olen)) | |
0ca8d1ec MC |
633 | goto err; |
634 | ||
635 | ret = 1; | |
636 | err: | |
637 | EVP_MD_CTX_free(ctx); | |
638 | return ret; | |
639 | } |