blob: 47d23bd365fb5577c21c69317a23b37fa74bd9dd [file] [log] [blame]
Matt Caswell34574f12016-11-08 10:34:28 +00001/*
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 */
18static const unsigned char default_zeros[EVP_MAX_MD_SIZE];
19
Matt Caswell34574f12016-11-08 10:34:28 +000020/*
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
Matt Caswellf5ca0b02016-11-21 12:10:35 +000023 * the location pointed to be |out|. The |hash| value may be NULL. Returns 1 on
24 * success 0 on failure.
Matt Caswell34574f12016-11-08 10:34:28 +000025 */
Matt Caswellec15acb2017-01-13 17:00:49 +000026int tls13_hkdf_expand(SSL *s, const EVP_MD *md, const unsigned char *secret,
Matt Caswell34574f12016-11-08 10:34:28 +000027 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, ";
Matt Caswell34574f12016-11-08 10:34:28 +000032 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)) {
Matt Caswelld49e23e2017-02-21 16:39:43 +000059 EVP_PKEY_CTX_free(pctx);
Matt Caswell34574f12016-11-08 10:34:28 +000060 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
77/*
Matt Caswellf5ca0b02016-11-21 12:10:35 +000078 * Given a |secret| generate a |key| of length |keylen| bytes. Returns 1 on
79 * success 0 on failure.
Matt Caswell34574f12016-11-08 10:34:28 +000080 */
Matt Caswelld49e23e2017-02-21 16:39:43 +000081int tls13_derive_key(SSL *s, const EVP_MD *md, const unsigned char *secret,
82 unsigned char *key, size_t keylen)
Matt Caswell34574f12016-11-08 10:34:28 +000083{
Matt Caswellf5ca0b02016-11-21 12:10:35 +000084 static const unsigned char keylabel[] = "key";
85
Matt Caswelld49e23e2017-02-21 16:39:43 +000086 return tls13_hkdf_expand(s, md, secret, keylabel, sizeof(keylabel) - 1,
87 NULL, key, keylen);
Matt Caswell34574f12016-11-08 10:34:28 +000088}
89
90/*
Matt Caswellf5ca0b02016-11-21 12:10:35 +000091 * Given a |secret| generate an |iv| of length |ivlen| bytes. Returns 1 on
92 * success 0 on failure.
Matt Caswell34574f12016-11-08 10:34:28 +000093 */
Matt Caswelld49e23e2017-02-21 16:39:43 +000094int tls13_derive_iv(SSL *s, const EVP_MD *md, const unsigned char *secret,
95 unsigned char *iv, size_t ivlen)
Matt Caswell34574f12016-11-08 10:34:28 +000096{
Matt Caswellf5ca0b02016-11-21 12:10:35 +000097 static const unsigned char ivlabel[] = "iv";
98
Matt Caswelld49e23e2017-02-21 16:39:43 +000099 return tls13_hkdf_expand(s, md, secret, ivlabel, sizeof(ivlabel) - 1,
100 NULL, iv, ivlen);
Matt Caswell34574f12016-11-08 10:34:28 +0000101}
102
Matt Caswellec15acb2017-01-13 17:00:49 +0000103int tls13_derive_finishedkey(SSL *s, const EVP_MD *md,
104 const unsigned char *secret,
105 unsigned char *fin, size_t finlen)
Matt Caswell64847762016-11-11 00:20:19 +0000106{
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000107 static const unsigned char finishedlabel[] = "finished";
108
Matt Caswellec15acb2017-01-13 17:00:49 +0000109 return tls13_hkdf_expand(s, md, secret, finishedlabel,
Matt Caswell64847762016-11-11 00:20:19 +0000110 sizeof(finishedlabel) - 1, NULL, fin, finlen);
111}
112
Matt Caswell34574f12016-11-08 10:34:28 +0000113/*
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
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000116 * pointed to by |outsecret|. Returns 1 on success 0 on failure.
Matt Caswell34574f12016-11-08 10:34:28 +0000117 */
Matt Caswellec15acb2017-01-13 17:00:49 +0000118int 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)
Matt Caswell34574f12016-11-08 10:34:28 +0000123{
Matt Caswell34574f12016-11-08 10:34:28 +0000124 size_t mdlen, prevsecretlen;
125 int ret;
126 EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, NULL);
127
128 if (pctx == NULL)
129 return 0;
130
131 mdlen = EVP_MD_size(md);
132
133 if (insecret == NULL) {
134 insecret = default_zeros;
135 insecretlen = mdlen;
136 }
137 if (prevsecret == NULL) {
138 prevsecret = default_zeros;
139 prevsecretlen = 0;
140 } else {
141 prevsecretlen = mdlen;
142 }
143
144 ret = EVP_PKEY_derive_init(pctx) <= 0
145 || EVP_PKEY_CTX_hkdf_mode(pctx, EVP_PKEY_HKDEF_MODE_EXTRACT_ONLY)
146 <= 0
147 || EVP_PKEY_CTX_set_hkdf_md(pctx, md) <= 0
148 || EVP_PKEY_CTX_set1_hkdf_key(pctx, insecret, insecretlen) <= 0
149 || EVP_PKEY_CTX_set1_hkdf_salt(pctx, prevsecret, prevsecretlen)
150 <= 0
151 || EVP_PKEY_derive(pctx, outsecret, &mdlen)
152 <= 0;
153
154 EVP_PKEY_CTX_free(pctx);
155 return ret == 0;
156}
157
158/*
Matt Caswell34574f12016-11-08 10:34:28 +0000159 * Given an input secret |insecret| of length |insecretlen| generate the
160 * handshake secret. This requires the early secret to already have been
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000161 * generated. Returns 1 on success 0 on failure.
Matt Caswell34574f12016-11-08 10:34:28 +0000162 */
163int tls13_generate_handshake_secret(SSL *s, const unsigned char *insecret,
164 size_t insecretlen)
165{
Matt Caswellec15acb2017-01-13 17:00:49 +0000166 return tls13_generate_secret(s, ssl_handshake_md(s), s->early_secret,
167 insecret, insecretlen,
Matt Caswell34574f12016-11-08 10:34:28 +0000168 (unsigned char *)&s->handshake_secret);
169}
170
171/*
172 * Given the handshake secret |prev| of length |prevlen| generate the master
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000173 * secret and store its length in |*secret_size|. Returns 1 on success 0 on
174 * failure.
Matt Caswell34574f12016-11-08 10:34:28 +0000175 */
176int tls13_generate_master_secret(SSL *s, unsigned char *out,
177 unsigned char *prev, size_t prevlen,
178 size_t *secret_size)
179{
Matt Caswellec15acb2017-01-13 17:00:49 +0000180 const EVP_MD *md = ssl_handshake_md(s);
181
182 *secret_size = EVP_MD_size(md);
183 return tls13_generate_secret(s, md, prev, NULL, 0, out);
Matt Caswell34574f12016-11-08 10:34:28 +0000184}
185
Matt Caswell92760c22016-11-09 14:06:12 +0000186/*
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000187 * Generates the mac for the Finished message. Returns the length of the MAC or
188 * 0 on error.
Matt Caswell92760c22016-11-09 14:06:12 +0000189 */
190size_t tls13_final_finish_mac(SSL *s, const char *str, size_t slen,
191 unsigned char *out)
192{
Matt Caswell64847762016-11-11 00:20:19 +0000193 const EVP_MD *md = ssl_handshake_md(s);
194 unsigned char hash[EVP_MAX_MD_SIZE];
195 size_t hashlen, ret = 0;
196 EVP_PKEY *key = NULL;
197 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
Matt Caswell92760c22016-11-09 14:06:12 +0000198
Matt Caswell64847762016-11-11 00:20:19 +0000199 if (!ssl_handshake_hash(s, hash, sizeof(hash), &hashlen))
200 goto err;
Matt Caswell92760c22016-11-09 14:06:12 +0000201
Matt Caswell64847762016-11-11 00:20:19 +0000202 if (str == s->method->ssl3_enc->server_finished_label)
203 key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
204 s->server_finished_secret, hashlen);
205 else
206 key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL,
207 s->client_finished_secret, hashlen);
208
209 if (key == NULL
210 || ctx == NULL
211 || EVP_DigestSignInit(ctx, NULL, md, NULL, key) <= 0
212 || EVP_DigestSignUpdate(ctx, hash, hashlen) <= 0
213 || EVP_DigestSignFinal(ctx, out, &hashlen) <= 0)
214 goto err;
215
216 ret = hashlen;
217 err:
218 EVP_PKEY_free(key);
219 EVP_MD_CTX_free(ctx);
220 return ret;
Matt Caswell92760c22016-11-09 14:06:12 +0000221}
222
223/*
224 * There isn't really a key block in TLSv1.3, but we still need this function
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000225 * for initialising the cipher and hash. Returns 1 on success or 0 on failure.
Matt Caswell92760c22016-11-09 14:06:12 +0000226 */
227int tls13_setup_key_block(SSL *s)
228{
229 const EVP_CIPHER *c;
230 const EVP_MD *hash;
231 int mac_type = NID_undef;
232
233 s->session->cipher = s->s3->tmp.new_cipher;
234 if (!ssl_cipher_get_evp
235 (s->session, &c, &hash, &mac_type, NULL, NULL, 0)) {
236 SSLerr(SSL_F_TLS13_SETUP_KEY_BLOCK, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
237 return 0;
238 }
239
240 s->s3->tmp.new_sym_enc = c;
241 s->s3->tmp.new_hash = hash;
242
243 return 1;
244}
245
Matt Caswelld49e23e2017-02-21 16:39:43 +0000246static int derive_secret_key_and_iv(SSL *s, int send, const EVP_MD *md,
247 const EVP_CIPHER *ciph,
Matt Caswell57389a32017-02-10 17:43:09 +0000248 const unsigned char *insecret,
249 const unsigned char *hash,
250 const unsigned char *label,
251 size_t labellen, unsigned char *secret,
252 unsigned char *iv, EVP_CIPHER_CTX *ciph_ctx)
253{
254 unsigned char key[EVP_MAX_KEY_LENGTH];
255 size_t ivlen, keylen, taglen;
Matt Caswell57389a32017-02-10 17:43:09 +0000256 size_t hashlen = EVP_MD_size(md);
Matt Caswell57389a32017-02-10 17:43:09 +0000257
258 if (!tls13_hkdf_expand(s, md, insecret, label, labellen, hash, secret,
259 hashlen)) {
260 SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
261 goto err;
262 }
263
264 /* TODO(size_t): convert me */
265 keylen = EVP_CIPHER_key_length(ciph);
266 if (EVP_CIPHER_mode(ciph) == EVP_CIPH_CCM_MODE) {
Matt Caswellc117af62017-02-23 12:25:21 +0000267 uint32_t algenc;
268
Matt Caswell57389a32017-02-10 17:43:09 +0000269 ivlen = EVP_CCM_TLS_IV_LEN;
Matt Caswellc117af62017-02-23 12:25:21 +0000270 if (s->s3->tmp.new_cipher == NULL) {
271 /* We've not selected a cipher yet - we must be doing early data */
272 algenc = s->session->cipher->algorithm_enc;
273 } else {
274 algenc = s->s3->tmp.new_cipher->algorithm_enc;
275 }
276 if (algenc & (SSL_AES128CCM8 | SSL_AES256CCM8))
Matt Caswell57389a32017-02-10 17:43:09 +0000277 taglen = EVP_CCM8_TLS_TAG_LEN;
278 else
279 taglen = EVP_CCM_TLS_TAG_LEN;
280 } else {
281 ivlen = EVP_CIPHER_iv_length(ciph);
282 taglen = 0;
283 }
284
Matt Caswelld49e23e2017-02-21 16:39:43 +0000285 if (!tls13_derive_key(s, md, secret, key, keylen)
286 || !tls13_derive_iv(s, md, secret, iv, ivlen)) {
Matt Caswell57389a32017-02-10 17:43:09 +0000287 SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_INTERNAL_ERROR);
288 goto err;
289 }
290
Matt Caswellc2fd15f2017-02-14 11:48:24 +0000291 if (EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, send) <= 0
Matt Caswell57389a32017-02-10 17:43:09 +0000292 || !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, ivlen, NULL)
293 || (taglen != 0 && !EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
294 taglen, NULL))
295 || EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, -1) <= 0) {
296 SSLerr(SSL_F_DERIVE_SECRET_KEY_AND_IV, ERR_R_EVP_LIB);
297 goto err;
298 }
299
300#ifdef OPENSSL_SSL_TRACE_CRYPTO
301 if (s->msg_callback) {
Matt Caswellc2fd15f2017-02-14 11:48:24 +0000302 int wh = send ? TLS1_RT_CRYPTO_WRITE : 0;
Matt Caswell57389a32017-02-10 17:43:09 +0000303
304 if (ciph->key_len)
305 s->msg_callback(2, s->version, wh | TLS1_RT_CRYPTO_KEY,
306 key, ciph->key_len, s, s->msg_callback_arg);
307
308 wh |= TLS1_RT_CRYPTO_IV;
309 s->msg_callback(2, s->version, wh, iv, ivlen, s,
310 s->msg_callback_arg);
311 }
312#endif
313
314 return 1;
315 err:
316 OPENSSL_cleanse(key, sizeof(key));
317 return 0;
318}
319
Matt Caswell0d9824c2016-11-08 23:20:31 +0000320int tls13_change_cipher_state(SSL *s, int which)
321{
Matt Caswelld49e23e2017-02-21 16:39:43 +0000322 static const unsigned char client_early_traffic[] =
323 "client early traffic secret";
Matt Caswellf5ca0b02016-11-21 12:10:35 +0000324 static const unsigned char client_handshake_traffic[] =
325 "client handshake traffic secret";
326 static const unsigned char client_application_traffic[] =
327 "client application traffic secret";
328 static const unsigned char server_handshake_traffic[] =
329 "server handshake traffic secret";
330 static const unsigned char server_application_traffic[] =
331 "server application traffic secret";
Matt Caswellec15acb2017-01-13 17:00:49 +0000332 static const unsigned char resumption_master_secret[] =
333 "resumption master secret";
Matt Caswellbebc0c72016-11-17 18:00:17 +0000334 unsigned char *iv;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000335 unsigned char secret[EVP_MAX_MD_SIZE];
Matt Caswellace081c2016-12-29 17:11:27 +0000336 unsigned char hashval[EVP_MAX_MD_SIZE];
337 unsigned char *hash = hashval;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000338 unsigned char *insecret;
Matt Caswell64847762016-11-11 00:20:19 +0000339 unsigned char *finsecret = NULL;
Cory Benfield2c7bd692017-01-31 14:56:15 +0000340 const char *log_label = NULL;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000341 EVP_CIPHER_CTX *ciph_ctx;
Matt Caswell57389a32017-02-10 17:43:09 +0000342 size_t finsecretlen = 0;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000343 const unsigned char *label;
Matt Caswellace081c2016-12-29 17:11:27 +0000344 size_t labellen, hashlen = 0;
Matt Caswell6530c492016-11-23 15:38:32 +0000345 int ret = 0;
Matt Caswelld49e23e2017-02-21 16:39:43 +0000346 const EVP_MD *md;
347 const EVP_CIPHER *cipher;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000348
349 if (which & SSL3_CC_READ) {
350 if (s->enc_read_ctx != NULL) {
351 EVP_CIPHER_CTX_reset(s->enc_read_ctx);
352 } else {
353 s->enc_read_ctx = EVP_CIPHER_CTX_new();
354 if (s->enc_read_ctx == NULL) {
355 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
356 goto err;
357 }
358 }
359 ciph_ctx = s->enc_read_ctx;
Matt Caswellbebc0c72016-11-17 18:00:17 +0000360 iv = s->read_iv;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000361
362 RECORD_LAYER_reset_read_sequence(&s->rlayer);
363 } else {
364 if (s->enc_write_ctx != NULL) {
365 EVP_CIPHER_CTX_reset(s->enc_write_ctx);
366 } else {
367 s->enc_write_ctx = EVP_CIPHER_CTX_new();
368 if (s->enc_write_ctx == NULL) {
369 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
370 goto err;
371 }
372 }
373 ciph_ctx = s->enc_write_ctx;
Matt Caswellbebc0c72016-11-17 18:00:17 +0000374 iv = s->write_iv;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000375
376 RECORD_LAYER_reset_write_sequence(&s->rlayer);
377 }
378
379 if (((which & SSL3_CC_CLIENT) && (which & SSL3_CC_WRITE))
380 || ((which & SSL3_CC_SERVER) && (which & SSL3_CC_READ))) {
Matt Caswelld49e23e2017-02-21 16:39:43 +0000381 if (which & SSL3_CC_EARLY) {
382 EVP_MD_CTX *mdctx = NULL;
383 long handlen;
384 void *hdata;
385 unsigned int hashlenui;
386 const SSL_CIPHER *sslcipher = SSL_SESSION_get0_cipher(s->session);
387
388 insecret = s->early_secret;
389 label = client_early_traffic;
390 labellen = sizeof(client_early_traffic) - 1;
391 log_label = CLIENT_EARLY_LABEL;
392
393 handlen = BIO_get_mem_data(s->s3->handshake_buffer, &hdata);
394 if (handlen <= 0) {
395 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE,
396 SSL_R_BAD_HANDSHAKE_LENGTH);
397 goto err;
398 }
399 if (sslcipher == NULL) {
400 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
401 goto err;
402 }
403
404 /*
405 * We need to calculate the handshake digest using the digest from
406 * the session. We haven't yet selected our ciphersuite so we can't
407 * use ssl_handshake_md().
408 */
409 mdctx = EVP_MD_CTX_new();
410 if (mdctx == NULL) {
411 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_MALLOC_FAILURE);
412 goto err;
413 }
414 cipher = EVP_get_cipherbynid(SSL_CIPHER_get_cipher_nid(sslcipher));
415 md = ssl_md(sslcipher->algorithm2);
416 if (md == NULL || !EVP_DigestInit_ex(mdctx, md, NULL)
417 || !EVP_DigestUpdate(mdctx, hdata, handlen)
418 || !EVP_DigestFinal_ex(mdctx, hashval, &hashlenui)) {
419 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
420 EVP_MD_CTX_free(mdctx);
421 goto err;
422 }
423 hashlen = hashlenui;
424 EVP_MD_CTX_free(mdctx);
425 } else if (which & SSL3_CC_HANDSHAKE) {
Matt Caswell0d9824c2016-11-08 23:20:31 +0000426 insecret = s->handshake_secret;
Matt Caswell64847762016-11-11 00:20:19 +0000427 finsecret = s->client_finished_secret;
Matt Caswell6612d872016-12-15 00:28:47 +0000428 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
Matt Caswell0d9824c2016-11-08 23:20:31 +0000429 label = client_handshake_traffic;
430 labellen = sizeof(client_handshake_traffic) - 1;
Cory Benfield2c7bd692017-01-31 14:56:15 +0000431 log_label = CLIENT_HANDSHAKE_LABEL;
Matt Caswellfe5e20f2017-02-22 14:09:42 +0000432 /*
Matt Caswellf7e393b2017-02-27 11:19:57 +0000433 * The hanshake hash used for the server read/client write handshake
434 * traffic secret is the same as the hash for the server
435 * write/client read handshake traffic secret. However, if we
436 * processed early data then we delay changing the server
437 * read/client write cipher state until later, and the handshake
438 * hashes have moved on. Therefore we use the value saved earlier
439 * when we did the server write/client read change cipher state.
Matt Caswellfe5e20f2017-02-22 14:09:42 +0000440 */
Matt Caswellf7e393b2017-02-27 11:19:57 +0000441 hash = s->handshake_traffic_hash;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000442 } else {
Matt Caswellec15acb2017-01-13 17:00:49 +0000443 insecret = s->master_secret;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000444 label = client_application_traffic;
445 labellen = sizeof(client_application_traffic) - 1;
Cory Benfield2c7bd692017-01-31 14:56:15 +0000446 log_label = CLIENT_APPLICATION_LABEL;
Matt Caswellace081c2016-12-29 17:11:27 +0000447 /*
448 * For this we only use the handshake hashes up until the server
449 * Finished hash. We do not include the client's Finished, which is
450 * what ssl_handshake_hash() would give us. Instead we use the
451 * previously saved value.
452 */
453 hash = s->server_finished_hash;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000454 }
455 } else {
Matt Caswelld49e23e2017-02-21 16:39:43 +0000456 /* Early data never applies to client-read/server-write */
Matt Caswell0d9824c2016-11-08 23:20:31 +0000457 if (which & SSL3_CC_HANDSHAKE) {
458 insecret = s->handshake_secret;
Matt Caswell64847762016-11-11 00:20:19 +0000459 finsecret = s->server_finished_secret;
Matt Caswell6612d872016-12-15 00:28:47 +0000460 finsecretlen = EVP_MD_size(ssl_handshake_md(s));
Matt Caswell0d9824c2016-11-08 23:20:31 +0000461 label = server_handshake_traffic;
462 labellen = sizeof(server_handshake_traffic) - 1;
Cory Benfield2c7bd692017-01-31 14:56:15 +0000463 log_label = SERVER_HANDSHAKE_LABEL;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000464 } else {
Matt Caswellec15acb2017-01-13 17:00:49 +0000465 insecret = s->master_secret;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000466 label = server_application_traffic;
467 labellen = sizeof(server_application_traffic) - 1;
Cory Benfield2c7bd692017-01-31 14:56:15 +0000468 log_label = SERVER_APPLICATION_LABEL;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000469 }
470 }
471
Matt Caswelld49e23e2017-02-21 16:39:43 +0000472 if (!(which & SSL3_CC_EARLY)) {
473 md = ssl_handshake_md(s);
474 cipher = s->s3->tmp.new_sym_enc;
475 if (!ssl3_digest_cached_records(s, 1)
476 || !ssl_handshake_hash(s, hashval, sizeof(hashval), &hashlen)) {
477 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
478 goto err;
479 }
Matt Caswellec15acb2017-01-13 17:00:49 +0000480 }
481
482 /*
483 * Save the hash of handshakes up to now for use when we calculate the
484 * client application traffic secret
485 */
486 if (label == server_application_traffic)
487 memcpy(s->server_finished_hash, hashval, hashlen);
488
Matt Caswellf7e393b2017-02-27 11:19:57 +0000489 if (label == server_handshake_traffic)
Matt Caswellfe5e20f2017-02-22 14:09:42 +0000490 memcpy(s->handshake_traffic_hash, hashval, hashlen);
491
Matt Caswellec15acb2017-01-13 17:00:49 +0000492 if (label == client_application_traffic) {
493 /*
494 * We also create the resumption master secret, but this time use the
495 * hash for the whole handshake including the Client Finished
496 */
497 if (!tls13_hkdf_expand(s, ssl_handshake_md(s), insecret,
498 resumption_master_secret,
499 sizeof(resumption_master_secret) - 1,
500 hashval, s->session->master_key, hashlen)) {
Matt Caswellace081c2016-12-29 17:11:27 +0000501 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
502 goto err;
503 }
Matt Caswellec15acb2017-01-13 17:00:49 +0000504 s->session->master_key_length = hashlen;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000505 }
506
Matt Caswelld49e23e2017-02-21 16:39:43 +0000507 if (!derive_secret_key_and_iv(s, which & SSL3_CC_WRITE, md, cipher,
508 insecret, hash, label, labellen, secret, iv,
509 ciph_ctx)) {
Matt Caswell57389a32017-02-10 17:43:09 +0000510 goto err;
Dr. Stephen Hensonec07b1d2017-02-03 02:44:15 +0000511 }
Matt Caswell0d9824c2016-11-08 23:20:31 +0000512
Matt Caswell57389a32017-02-10 17:43:09 +0000513 if (label == server_application_traffic)
514 memcpy(s->server_app_traffic_secret, secret, hashlen);
515 else if (label == client_application_traffic)
516 memcpy(s->client_app_traffic_secret, secret, hashlen);
517
Cory Benfield2c7bd692017-01-31 14:56:15 +0000518 if (!ssl_log_secret(s, log_label, secret, hashlen)) {
519 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
520 goto err;
521 }
522
Matt Caswell57389a32017-02-10 17:43:09 +0000523 if (finsecret != NULL
524 && !tls13_derive_finishedkey(s, ssl_handshake_md(s), secret,
525 finsecret, finsecretlen)) {
Matt Caswell0d9824c2016-11-08 23:20:31 +0000526 SSLerr(SSL_F_TLS13_CHANGE_CIPHER_STATE, ERR_R_INTERNAL_ERROR);
527 goto err;
528 }
529
Matt Caswell57389a32017-02-10 17:43:09 +0000530 ret = 1;
531 err:
532 OPENSSL_cleanse(secret, sizeof(secret));
533 return ret;
534}
535
Matt Caswellc2fd15f2017-02-14 11:48:24 +0000536int tls13_update_key(SSL *s, int send)
Matt Caswell57389a32017-02-10 17:43:09 +0000537{
538 static const unsigned char application_traffic[] =
539 "application traffic secret";
540 const EVP_MD *md = ssl_handshake_md(s);
541 size_t hashlen = EVP_MD_size(md);
542 unsigned char *insecret, *iv;
543 unsigned char secret[EVP_MAX_MD_SIZE];
544 EVP_CIPHER_CTX *ciph_ctx;
545 int ret = 0;
546
Matt Caswellc2fd15f2017-02-14 11:48:24 +0000547 if (s->server == send)
Matt Caswell57389a32017-02-10 17:43:09 +0000548 insecret = s->server_app_traffic_secret;
549 else
550 insecret = s->client_app_traffic_secret;
551
Matt Caswellc2fd15f2017-02-14 11:48:24 +0000552 if (send) {
Matt Caswell57389a32017-02-10 17:43:09 +0000553 iv = s->write_iv;
554 ciph_ctx = s->enc_write_ctx;
555 RECORD_LAYER_reset_write_sequence(&s->rlayer);
556 } else {
557 iv = s->read_iv;
558 ciph_ctx = s->enc_read_ctx;
559 RECORD_LAYER_reset_read_sequence(&s->rlayer);
560 }
561
Matt Caswelld49e23e2017-02-21 16:39:43 +0000562 if (!derive_secret_key_and_iv(s, send, ssl_handshake_md(s),
563 s->s3->tmp.new_sym_enc, insecret, NULL,
564 application_traffic,
Matt Caswell57389a32017-02-10 17:43:09 +0000565 sizeof(application_traffic) - 1, secret, iv,
566 ciph_ctx))
Matt Caswellbebc0c72016-11-17 18:00:17 +0000567 goto err;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000568
Matt Caswell57389a32017-02-10 17:43:09 +0000569 memcpy(insecret, secret, hashlen);
Matt Caswell0d9824c2016-11-08 23:20:31 +0000570
Matt Caswell6530c492016-11-23 15:38:32 +0000571 ret = 1;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000572 err:
573 OPENSSL_cleanse(secret, sizeof(secret));
Matt Caswell6530c492016-11-23 15:38:32 +0000574 return ret;
Matt Caswell0d9824c2016-11-08 23:20:31 +0000575}
Matt Caswell04904312016-12-30 11:26:39 +0000576
577int tls13_alert_code(int code)
578{
Matt Caswell49e7fe12017-02-21 09:22:22 +0000579 if (code == SSL_AD_MISSING_EXTENSION || code == SSL_AD_END_OF_EARLY_DATA)
Matt Caswell04904312016-12-30 11:26:39 +0000580 return code;
581
582 return tls1_alert_code(code);
583}