]>
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 MC |
20 | /* |
21 | * Given a |secret|; a |label| of length |labellen|; and a |hash| of the | |
22 | * handshake messages, derive a new secret |outlen| bytes long and store it in | |
f5ca0b04 MC |
23 | * the location pointed to be |out|. The |hash| value may be NULL. Returns 1 on |
24 | * success 0 on failure. | |
34574f19 | 25 | */ |
ec15acb6 | 26 | int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret, |
34574f19 MC |
27 | const unsigned char *label, size_t labellen, |
28 | const unsigned char *hash, | |
29 | unsigned char *out, size_t outlen) | |
30 | { | |
31 | const unsigned char label_prefix[] = "TLS 1.3, "; | |
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) | |
56 | || !WPACKET_sub_memcpy_u8(&pkt, hash, (hash == NULL) ? 0 : hashlen) | |
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 MC |
86 | return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1, |
87 | NULL, 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 MC |
99 | return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1, |
100 | NULL, 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, |
6484776f MC |
110 | sizeof(finishedlabel) - 1, NULL, fin, finlen); |
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); | |
3e0458fb MC |
127 | const char *derived_secret_label = "derived secret"; |
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); | |
151 | return 0; | |
152 | } | |
153 | EVP_MD_CTX_free(mctx); | |
154 | ||
155 | /* Generate the pre-extract secret */ | |
156 | if (!tls13_hkdf_expand(s, md, prevsecret, | |
157 | (unsigned char *)derived_secret_label, | |
158 | sizeof(derived_secret_label) - 1, hash, | |
159 | preextractsec, mdlen)) | |
160 | return 0; | |
161 | ||
162 | prevsecret = preextractsec; | |
34574f19 MC |
163 | prevsecretlen = mdlen; |
164 | } | |
165 | ||
166 | ret = EVP_PKEY_derive_init(pctx) <= 0 | |
167 | || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY) | |
168 | <= 0 | |
169 | || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0 | |
170 | || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0 | |
171 | || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen) | |
172 | <= 0 | |
173 | || EVP_PKEY_derive(pctx, outsecret, &mdlen) | |
174 | <= 0; | |
175 | ||
176 | EVP_PKEY_CTX_free(pctx); | |
3e0458fb MC |
177 | if (prevsecret == preextractsec) |
178 | OPENSSL_cleanse(preextractsec, mdlen); | |
34574f19 MC |
179 | return ret == 0; |
180 | } | |
181 | ||
34574f19 MC |
182 | /* |
183 | * Given an input secret |insecret| of length |insecretlen| generate the | |
184 | * handshake secret. This requires the early secret to already have been | |
f5ca0b04 | 185 | * generated. Returns 1 on success 0 on failure. |
34574f19 MC |
186 | */ |
187 | int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret, | |
188 | size_t insecretlen) | |
189 | { | |
ec15acb6 MC |
190 | return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret, |
191 | insecret, insecretlen, | |
34574f19 MC |
192 | (unsigned char *)&s->handshake_secret); |
193 | } | |
194 | ||
195 | /* | |
196 | * Given the handshake secret |prev| of length |prevlen| generate the master | |
f5ca0b04 MC |
197 | * secret and store its length in |*secret_size|. Returns 1 on success 0 on |
198 | * failure. | |
34574f19 MC |
199 | */ |
200 | int tls13_generate_master_secret(SSL *s, unsigned char *out, | |
201 | unsigned char *prev, size_t prevlen, | |
202 | size_t *secret_size) | |
203 | { | |
ec15acb6 MC |
204 | const EVP_MD *md = ssl_handshake_md(s); |
205 | ||
206 | *secret_size = EVP_MD_size(md); | |
207 | return tls13_generate_secret(s, md, prev, NULL, 0, out); | |
34574f19 MC |
208 | } |
209 | ||
92760c21 | 210 | /* |
f5ca0b04 MC |
211 | * Generates the mac for the Finished message. Returns the length of the MAC or |
212 | * 0 on error. | |
92760c21 MC |
213 | */ |
214 | size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen, | |
215 | unsigned char *out) | |
216 | { | |
6484776f MC |
217 | const EVP_MD *md = ssl_handshake_md(s); |
218 | unsigned char hash[EVP_MAX_MD_SIZE]; | |
219 | size_t hashlen, ret = 0; | |
220 | EVP_PKEY *key = NULL; | |
221 | EVP_MD_CTX *ctx = EVP_MD_CTX_new(); | |
92760c21 | 222 | |
6484776f MC |
223 | if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen)) |
224 | goto err; | |
225 | ||
226 | if (str == s->method->ssl3_enc->server_finished_label) | |
227 | key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, | |
228 | s->server_finished_secret, hashlen); | |
229 | else | |
230 | key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, | |
231 | s->client_finished_secret, hashlen); | |
232 | ||
233 | if (key == NULL | |
234 | || ctx == NULL | |
235 | || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0 | |
236 | || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0 | |
237 | || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0) | |
238 | goto err; | |
92760c21 | 239 | |
6484776f MC |
240 | ret = hashlen; |
241 | err: | |
242 | EVP_PKEY_free(key); | |
243 | EVP_MD_CTX_free(ctx); | |
244 | return ret; | |
92760c21 MC |
245 | } |
246 | ||
247 | /* | |
248 | * There isn't really a key block in TLSv1.3, but we still need this function | |
f5ca0b04 | 249 | * for initialising the cipher and hash. Returns 1 on success or 0 on failure. |
92760c21 MC |
250 | */ |
251 | int tls13_setup_key_block(SSL *s) | |
252 | { | |
253 | const EVP_CIPHER *c; | |
254 | const EVP_MD *hash; | |
255 | int mac_type = NID_undef; | |
256 | ||
257 | s->session->cipher = s->s3->tmp.new_cipher; | |
258 | if (!ssl_cipher_get_evp | |
259 | (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) { | |
260 | SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); | |
261 | return 0; | |
262 | } | |
263 | ||
264 | s->s3->tmp.new_sym_enc = c; | |
265 | s->s3->tmp.new_hash = hash; | |
266 | ||
267 | return 1; | |
268 | } | |
269 | ||
d49e23ec MC |
270 | static int derive_secret_key_and_iv(SSL *s, int send, const EVP_MD *md, |
271 | const EVP_CIPHER *ciph, | |
57389a32 MC |
272 | const unsigned char *insecret, |
273 | const unsigned char *hash, | |
274 | const unsigned char *label, | |
275 | size_t labellen, unsigned char *secret, | |
276 | unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx) | |
277 | { | |
278 | unsigned char key[EVP_MAX_KEY_LENGTH]; | |
279 | size_t ivlen, keylen, taglen; | |
57389a32 | 280 | size_t hashlen = EVP_MD_size(md); |
57389a32 MC |
281 | |
282 | if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, secret, | |
283 | hashlen)) { | |
284 | SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR); | |
285 | goto err; | |
286 | } | |
287 | ||
288 | /* TODO(size_t): convert me */ | |
289 | keylen = EVP_CIPHER_key_length(ciph); | |
290 | if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) { | |
c117af67 MC |
291 | uint32_t algenc; |
292 | ||
57389a32 | 293 | ivlen = EVP_CCM_TLS_IV_LEN; |
c117af67 MC |
294 | if (s->s3->tmp.new_cipher == NULL) { |
295 | /* We've not selected a cipher yet - we must be doing early data */ | |
296 | algenc = s->session->cipher->algorithm_enc; | |
297 | } else { | |
298 | algenc = s->s3->tmp.new_cipher->algorithm_enc; | |
299 | } | |
300 | if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8)) | |
57389a32 MC |
301 | taglen = EVP_CCM8_TLS_TAG_LEN; |
302 | else | |
303 | taglen = EVP_CCM_TLS_TAG_LEN; | |
304 | } else { | |
305 | ivlen = EVP_CIPHER_iv_length(ciph); | |
306 | taglen = 0; | |
307 | } | |
308 | ||
d49e23ec MC |
309 | if (!tls13_derive_key(s, md, secret, key, keylen) |
310 | || !tls13_derive_iv(s, md, secret, iv, ivlen)) { | |
57389a32 MC |
311 | SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR); |
312 | goto err; | |
313 | } | |
314 | ||
c2fd15f6 | 315 | if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, send) <= 0 |
57389a32 MC |
316 | || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL) |
317 | || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG, | |
318 | taglen, NULL)) | |
319 | || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) { | |
320 | SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_EVP_LIB); | |
321 | goto err; | |
322 | } | |
323 | ||
324 | #ifdef OPENSSL_SSL_TRACE_CRYPTO | |
325 | if (s->msg_callback) { | |
c2fd15f6 | 326 | int wh = send ? TLS1_RT_CRYPTO_WRITE : 0; |
57389a32 MC |
327 | |
328 | if (ciph->key_len) | |
329 | s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY, | |
330 | key, ciph->key_len, s, s->msg_callback_arg); | |
331 | ||
332 | wh |= TLS1_RT_CRYPTO_IV; | |
333 | s->msg_callback(2, s->version, wh, iv, ivlen, s, | |
334 | s->msg_callback_arg); | |
335 | } | |
336 | #endif | |
337 | ||
338 | return 1; | |
339 | err: | |
340 | OPENSSL_cleanse(key, sizeof(key)); | |
341 | return 0; | |
342 | } | |
343 | ||
0d9824c1 MC |
344 | int tls13_change_cipher_state(SSL *s, int which) |
345 | { | |
d49e23ec MC |
346 | static const unsigned char client_early_traffic[] = |
347 | "client early traffic secret"; | |
f5ca0b04 MC |
348 | static const unsigned char client_handshake_traffic[] = |
349 | "client handshake traffic secret"; | |
350 | static const unsigned char client_application_traffic[] = | |
351 | "client application traffic secret"; | |
352 | static const unsigned char server_handshake_traffic[] = | |
353 | "server handshake traffic secret"; | |
354 | static const unsigned char server_application_traffic[] = | |
355 | "server application traffic secret"; | |
ec15acb6 MC |
356 | static const unsigned char resumption_master_secret[] = |
357 | "resumption master secret"; | |
bebc0c7d | 358 | unsigned char *iv; |
0d9824c1 | 359 | unsigned char secret[EVP_MAX_MD_SIZE]; |
ace081c1 MC |
360 | unsigned char hashval[EVP_MAX_MD_SIZE]; |
361 | unsigned char *hash = hashval; | |
0d9824c1 | 362 | unsigned char *insecret; |
6484776f | 363 | unsigned char *finsecret = NULL; |
2c7bd692 | 364 | const char *log_label = NULL; |
0d9824c1 | 365 | EVP_CIPHER_CTX *ciph_ctx; |
57389a32 | 366 | size_t finsecretlen = 0; |
0d9824c1 | 367 | const unsigned char *label; |
ace081c1 | 368 | size_t labellen, hashlen = 0; |
6530c490 | 369 | int ret = 0; |
42f50fdf MC |
370 | const EVP_MD *md = NULL; |
371 | const EVP_CIPHER *cipher = NULL; | |
0d9824c1 MC |
372 | |
373 | if (which & SSL3_CC_READ) { | |
374 | if (s->enc_read_ctx != NULL) { | |
375 | EVP_CIPHER_CTX_reset(s->enc_read_ctx); | |
376 | } else { | |
377 | s->enc_read_ctx = EVP_CIPHER_CTX_new(); | |
378 | if (s->enc_read_ctx == NULL) { | |
379 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
380 | goto err; | |
381 | } | |
382 | } | |
383 | ciph_ctx = s->enc_read_ctx; | |
bebc0c7d | 384 | iv = s->read_iv; |
0d9824c1 MC |
385 | |
386 | RECORD_LAYER_reset_read_sequence(&s->rlayer); | |
387 | } else { | |
388 | if (s->enc_write_ctx != NULL) { | |
389 | EVP_CIPHER_CTX_reset(s->enc_write_ctx); | |
390 | } else { | |
391 | s->enc_write_ctx = EVP_CIPHER_CTX_new(); | |
392 | if (s->enc_write_ctx == NULL) { | |
393 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
394 | goto err; | |
395 | } | |
396 | } | |
397 | ciph_ctx = s->enc_write_ctx; | |
bebc0c7d | 398 | iv = s->write_iv; |
0d9824c1 MC |
399 | |
400 | RECORD_LAYER_reset_write_sequence(&s->rlayer); | |
401 | } | |
402 | ||
403 | if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE)) | |
404 | || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) { | |
d49e23ec MC |
405 | if (which & SSL3_CC_EARLY) { |
406 | EVP_MD_CTX *mdctx = NULL; | |
407 | long handlen; | |
408 | void *hdata; | |
409 | unsigned int hashlenui; | |
410 | const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session); | |
411 | ||
412 | insecret = s->early_secret; | |
413 | label = client_early_traffic; | |
414 | labellen = sizeof(client_early_traffic) - 1; | |
415 | log_label = CLIENT_EARLY_LABEL; | |
416 | ||
417 | handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata); | |
418 | if (handlen <= 0) { | |
419 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, | |
420 | SSL_R_BAD_HANDSHAKE_LENGTH); | |
421 | goto err; | |
422 | } | |
423 | if (sslcipher == NULL) { | |
424 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
425 | goto err; | |
426 | } | |
427 | ||
428 | /* | |
429 | * We need to calculate the handshake digest using the digest from | |
430 | * the session. We haven't yet selected our ciphersuite so we can't | |
431 | * use ssl_handshake_md(). | |
432 | */ | |
433 | mdctx = EVP_MD_CTX_new(); | |
434 | if (mdctx == NULL) { | |
435 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE); | |
436 | goto err; | |
437 | } | |
438 | cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher)); | |
439 | md = ssl_md(sslcipher->algorithm2); | |
440 | if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL) | |
441 | || !EVP_DigestUpdate(mdctx, hdata, handlen) | |
442 | || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) { | |
443 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
444 | EVP_MD_CTX_free(mdctx); | |
445 | goto err; | |
446 | } | |
447 | hashlen = hashlenui; | |
448 | EVP_MD_CTX_free(mdctx); | |
449 | } else if (which & SSL3_CC_HANDSHAKE) { | |
0d9824c1 | 450 | insecret = s->handshake_secret; |
6484776f | 451 | finsecret = s->client_finished_secret; |
6612d87b | 452 | finsecretlen = EVP_MD_size(ssl_handshake_md(s)); |
0d9824c1 MC |
453 | label = client_handshake_traffic; |
454 | labellen = sizeof(client_handshake_traffic) - 1; | |
2c7bd692 | 455 | log_label = CLIENT_HANDSHAKE_LABEL; |
fe5e20fd | 456 | /* |
f7e393be MC |
457 | * The hanshake hash used for the server read/client write handshake |
458 | * traffic secret is the same as the hash for the server | |
459 | * write/client read handshake traffic secret. However, if we | |
460 | * processed early data then we delay changing the server | |
461 | * read/client write cipher state until later, and the handshake | |
462 | * hashes have moved on. Therefore we use the value saved earlier | |
463 | * when we did the server write/client read change cipher state. | |
fe5e20fd | 464 | */ |
f7e393be | 465 | hash = s->handshake_traffic_hash; |
0d9824c1 | 466 | } else { |
ec15acb6 | 467 | insecret = s->master_secret; |
0d9824c1 MC |
468 | label = client_application_traffic; |
469 | labellen = sizeof(client_application_traffic) - 1; | |
2c7bd692 | 470 | log_label = CLIENT_APPLICATION_LABEL; |
ace081c1 MC |
471 | /* |
472 | * For this we only use the handshake hashes up until the server | |
473 | * Finished hash. We do not include the client's Finished, which is | |
474 | * what ssl_handshake_hash() would give us. Instead we use the | |
475 | * previously saved value. | |
476 | */ | |
477 | hash = s->server_finished_hash; | |
0d9824c1 MC |
478 | } |
479 | } else { | |
d49e23ec | 480 | /* Early data never applies to client-read/server-write */ |
0d9824c1 MC |
481 | if (which & SSL3_CC_HANDSHAKE) { |
482 | insecret = s->handshake_secret; | |
6484776f | 483 | finsecret = s->server_finished_secret; |
6612d87b | 484 | finsecretlen = EVP_MD_size(ssl_handshake_md(s)); |
0d9824c1 MC |
485 | label = server_handshake_traffic; |
486 | labellen = sizeof(server_handshake_traffic) - 1; | |
2c7bd692 | 487 | log_label = SERVER_HANDSHAKE_LABEL; |
0d9824c1 | 488 | } else { |
ec15acb6 | 489 | insecret = s->master_secret; |
0d9824c1 MC |
490 | label = server_application_traffic; |
491 | labellen = sizeof(server_application_traffic) - 1; | |
2c7bd692 | 492 | log_label = SERVER_APPLICATION_LABEL; |
0d9824c1 MC |
493 | } |
494 | } | |
495 | ||
d49e23ec MC |
496 | if (!(which & SSL3_CC_EARLY)) { |
497 | md = ssl_handshake_md(s); | |
498 | cipher = s->s3->tmp.new_sym_enc; | |
499 | if (!ssl3_digest_cached_records(s, 1) | |
500 | || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) { | |
501 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
502 | goto err; | |
503 | } | |
ace081c1 MC |
504 | } |
505 | ||
ec15acb6 MC |
506 | /* |
507 | * Save the hash of handshakes up to now for use when we calculate the | |
508 | * client application traffic secret | |
509 | */ | |
510 | if (label == server_application_traffic) | |
511 | memcpy(s->server_finished_hash, hashval, hashlen); | |
512 | ||
f7e393be | 513 | if (label == server_handshake_traffic) |
fe5e20fd MC |
514 | memcpy(s->handshake_traffic_hash, hashval, hashlen); |
515 | ||
ec15acb6 MC |
516 | if (label == client_application_traffic) { |
517 | /* | |
518 | * We also create the resumption master secret, but this time use the | |
519 | * hash for the whole handshake including the Client Finished | |
520 | */ | |
521 | if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret, | |
522 | resumption_master_secret, | |
523 | sizeof(resumption_master_secret) - 1, | |
524 | hashval, s->session->master_key, hashlen)) { | |
525 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
526 | goto err; | |
527 | } | |
528 | s->session->master_key_length = hashlen; | |
529 | } | |
530 | ||
d49e23ec MC |
531 | if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher, |
532 | insecret, hash, label, labellen, secret, iv, | |
533 | ciph_ctx)) { | |
57389a32 | 534 | goto err; |
ec07b1d8 | 535 | } |
0d9824c1 | 536 | |
57389a32 MC |
537 | if (label == server_application_traffic) |
538 | memcpy(s->server_app_traffic_secret, secret, hashlen); | |
539 | else if (label == client_application_traffic) | |
540 | memcpy(s->client_app_traffic_secret, secret, hashlen); | |
541 | ||
2c7bd692 CB |
542 | if (!ssl_log_secret(s, log_label, secret, hashlen)) { |
543 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); | |
544 | goto err; | |
545 | } | |
546 | ||
57389a32 MC |
547 | if (finsecret != NULL |
548 | && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret, | |
549 | finsecret, finsecretlen)) { | |
0d9824c1 MC |
550 | SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR); |
551 | goto err; | |
552 | } | |
553 | ||
57389a32 MC |
554 | ret = 1; |
555 | err: | |
556 | OPENSSL_cleanse(secret, sizeof(secret)); | |
557 | return ret; | |
558 | } | |
0d9824c1 | 559 | |
c2fd15f6 | 560 | int tls13_update_key(SSL *s, int send) |
57389a32 MC |
561 | { |
562 | static const unsigned char application_traffic[] = | |
563 | "application traffic secret"; | |
564 | const EVP_MD *md = ssl_handshake_md(s); | |
565 | size_t hashlen = EVP_MD_size(md); | |
566 | unsigned char *insecret, *iv; | |
567 | unsigned char secret[EVP_MAX_MD_SIZE]; | |
568 | EVP_CIPHER_CTX *ciph_ctx; | |
569 | int ret = 0; | |
0d9824c1 | 570 | |
c2fd15f6 | 571 | if (s->server == send) |
57389a32 MC |
572 | insecret = s->server_app_traffic_secret; |
573 | else | |
574 | insecret = s->client_app_traffic_secret; | |
bebc0c7d | 575 | |
c2fd15f6 | 576 | if (send) { |
57389a32 MC |
577 | iv = s->write_iv; |
578 | ciph_ctx = s->enc_write_ctx; | |
579 | RECORD_LAYER_reset_write_sequence(&s->rlayer); | |
580 | } else { | |
581 | iv = s->read_iv; | |
582 | ciph_ctx = s->enc_read_ctx; | |
583 | RECORD_LAYER_reset_read_sequence(&s->rlayer); | |
0d9824c1 | 584 | } |
57389a32 | 585 | |
d49e23ec MC |
586 | if (!derive_secret_key_and_iv(s, send, ssl_handshake_md(s), |
587 | s->s3->tmp.new_sym_enc, insecret, NULL, | |
588 | application_traffic, | |
57389a32 MC |
589 | sizeof(application_traffic) - 1, secret, iv, |
590 | ciph_ctx)) | |
591 | goto err; | |
592 | ||
593 | memcpy(insecret, secret, hashlen); | |
0d9824c1 | 594 | |
6530c490 | 595 | ret = 1; |
0d9824c1 MC |
596 | err: |
597 | OPENSSL_cleanse(secret, sizeof(secret)); | |
6530c490 | 598 | return ret; |
0d9824c1 | 599 | } |
04904312 MC |
600 | |
601 | int tls13_alert_code(int code) | |
602 | { | |
49e7fe12 | 603 | if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_END_OF_EARLY_DATA) |
04904312 MC |
604 | return code; |
605 | ||
606 | return tls1_alert_code(code); | |
607 | } |