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