summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/lib/libssl/Makefile3
-rw-r--r--src/lib/libssl/ssl_locl.h28
-rw-r--r--src/lib/libssl/t1_enc.c97
-rw-r--r--src/lib/libssl/tls12_key_schedule.c175
-rw-r--r--src/lib/libssl/tls12_record_layer.c78
5 files changed, 247 insertions, 134 deletions
diff --git a/src/lib/libssl/Makefile b/src/lib/libssl/Makefile
index d6730a5e04..6171194629 100644
--- a/src/lib/libssl/Makefile
+++ b/src/lib/libssl/Makefile
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile,v 1.69 2021/04/25 13:15:22 jsing Exp $ 1# $OpenBSD: Makefile,v 1.70 2021/05/05 10:05:27 jsing Exp $
2 2
3.include <bsd.own.mk> 3.include <bsd.own.mk>
4.ifndef NOMAN 4.ifndef NOMAN
@@ -67,6 +67,7 @@ SRCS= \
67 ssl_versions.c \ 67 ssl_versions.c \
68 t1_enc.c \ 68 t1_enc.c \
69 t1_lib.c \ 69 t1_lib.c \
70 tls12_key_schedule.c \
70 tls12_lib.c \ 71 tls12_lib.c \
71 tls12_record_layer.c \ 72 tls12_record_layer.c \
72 tls13_buffer.c \ 73 tls13_buffer.c \
diff --git a/src/lib/libssl/ssl_locl.h b/src/lib/libssl/ssl_locl.h
index 9dfa1243c9..1f7e1fa587 100644
--- a/src/lib/libssl/ssl_locl.h
+++ b/src/lib/libssl/ssl_locl.h
@@ -1,4 +1,4 @@
1/* $OpenBSD: ssl_locl.h,v 1.341 2021/05/02 17:46:58 jsing Exp $ */ 1/* $OpenBSD: ssl_locl.h,v 1.342 2021/05/05 10:05:27 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -431,12 +431,8 @@ typedef struct ssl_handshake_tls12_st {
431 int cert_request; 431 int cert_request;
432 STACK_OF(X509_NAME) *ca_names; 432 STACK_OF(X509_NAME) *ca_names;
433 433
434 /* Size of the MAC secret. */
435 int mac_secret_size;
436
437 /* Record-layer key block for TLS 1.2 and earlier. */ 434 /* Record-layer key block for TLS 1.2 and earlier. */
438 unsigned char *key_block; 435 struct tls12_key_block *key_block;
439 size_t key_block_len;
440 436
441 /* Transcript hash prior to sending certificate verify message. */ 437 /* Transcript hash prior to sending certificate verify message. */
442 uint8_t cert_verify[EVP_MAX_MD_SIZE]; 438 uint8_t cert_verify[EVP_MAX_MD_SIZE];
@@ -522,6 +518,17 @@ typedef struct ssl_handshake_st {
522 SSL_HANDSHAKE_TLS13 tls13; 518 SSL_HANDSHAKE_TLS13 tls13;
523} SSL_HANDSHAKE; 519} SSL_HANDSHAKE;
524 520
521struct tls12_key_block;
522
523struct tls12_key_block *tls12_key_block_new(void);
524void tls12_key_block_free(struct tls12_key_block *kb);
525void tls12_key_block_client_write(struct tls12_key_block *kb, CBS *mac_key,
526 CBS *key, CBS *iv);
527void tls12_key_block_server_write(struct tls12_key_block *kb, CBS *mac_key,
528 CBS *key, CBS *iv);
529int tls12_key_block_generate(struct tls12_key_block *kb, SSL *s,
530 const EVP_AEAD *aead, const EVP_CIPHER *cipher, const EVP_MD *mac_hash);
531
525struct tls12_record_layer; 532struct tls12_record_layer;
526 533
527struct tls12_record_layer *tls12_record_layer_new(void); 534struct tls12_record_layer *tls12_record_layer_new(void);
@@ -532,8 +539,6 @@ int tls12_record_layer_write_overhead(struct tls12_record_layer *rl,
532 size_t *overhead); 539 size_t *overhead);
533int tls12_record_layer_read_protected(struct tls12_record_layer *rl); 540int tls12_record_layer_read_protected(struct tls12_record_layer *rl);
534int tls12_record_layer_write_protected(struct tls12_record_layer *rl); 541int tls12_record_layer_write_protected(struct tls12_record_layer *rl);
535const EVP_AEAD *tls12_record_layer_aead(struct tls12_record_layer *rl);
536const EVP_CIPHER *tls12_record_layer_cipher(struct tls12_record_layer *rl);
537void tls12_record_layer_set_aead(struct tls12_record_layer *rl, 542void tls12_record_layer_set_aead(struct tls12_record_layer *rl,
538 const EVP_AEAD *aead); 543 const EVP_AEAD *aead);
539void tls12_record_layer_set_cipher_hash(struct tls12_record_layer *rl, 544void tls12_record_layer_set_cipher_hash(struct tls12_record_layer *rl,
@@ -553,11 +558,9 @@ void tls12_record_layer_reflect_seq_num(struct tls12_record_layer *rl);
553void tls12_record_layer_read_cipher_hash(struct tls12_record_layer *rl, 558void tls12_record_layer_read_cipher_hash(struct tls12_record_layer *rl,
554 EVP_CIPHER_CTX **cipher, EVP_MD_CTX **hash); 559 EVP_CIPHER_CTX **cipher, EVP_MD_CTX **hash);
555int tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl, 560int tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
556 const uint8_t *mac_key, size_t mac_key_len, const uint8_t *key, 561 CBS *mac_key, CBS *key, CBS *iv);
557 size_t key_len, const uint8_t *iv, size_t iv_len);
558int tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl, 562int tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl,
559 const uint8_t *mac_key, size_t mac_key_len, const uint8_t *key, 563 CBS *mac_key, CBS *key, CBS *iv);
560 size_t key_len, const uint8_t *iv, size_t iv_len);
561int tls12_record_layer_open_record(struct tls12_record_layer *rl, 564int tls12_record_layer_open_record(struct tls12_record_layer *rl,
562 uint8_t *buf, size_t buf_len, uint8_t **out, size_t *out_len); 565 uint8_t *buf, size_t buf_len, uint8_t **out, size_t *out_len);
563int tls12_record_layer_seal_record(struct tls12_record_layer *rl, 566int tls12_record_layer_seal_record(struct tls12_record_layer *rl,
@@ -1381,6 +1384,7 @@ void tls1_cleanup_key_block(SSL *s);
1381int tls1_change_read_cipher_state(SSL *s); 1384int tls1_change_read_cipher_state(SSL *s);
1382int tls1_change_write_cipher_state(SSL *s); 1385int tls1_change_write_cipher_state(SSL *s);
1383int tls1_setup_key_block(SSL *s); 1386int tls1_setup_key_block(SSL *s);
1387int tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len);
1384int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen, 1388int tls1_export_keying_material(SSL *s, unsigned char *out, size_t olen,
1385 const char *label, size_t llen, const unsigned char *p, size_t plen, 1389 const char *label, size_t llen, const unsigned char *p, size_t plen,
1386 int use_context); 1390 int use_context);
diff --git a/src/lib/libssl/t1_enc.c b/src/lib/libssl/t1_enc.c
index e3cdcc134b..5a626fb880 100644
--- a/src/lib/libssl/t1_enc.c
+++ b/src/lib/libssl/t1_enc.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: t1_enc.c,v 1.142 2021/05/02 17:46:58 jsing Exp $ */ 1/* $OpenBSD: t1_enc.c,v 1.143 2021/05/05 10:05:27 jsing Exp $ */
2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3 * All rights reserved. 3 * All rights reserved.
4 * 4 *
@@ -147,9 +147,8 @@
147void 147void
148tls1_cleanup_key_block(SSL *s) 148tls1_cleanup_key_block(SSL *s)
149{ 149{
150 freezero(S3I(s)->hs.tls12.key_block, S3I(s)->hs.tls12.key_block_len); 150 tls12_key_block_free(S3I(s)->hs.tls12.key_block);
151 S3I(s)->hs.tls12.key_block = NULL; 151 S3I(s)->hs.tls12.key_block = NULL;
152 S3I(s)->hs.tls12.key_block_len = 0;
153} 152}
154 153
155/* 154/*
@@ -283,7 +282,7 @@ tls1_PRF(SSL *s, const unsigned char *secret, size_t secret_len,
283 return (1); 282 return (1);
284} 283}
285 284
286static int 285int
287tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len) 286tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len)
288{ 287{
289 return tls1_PRF(s, 288 return tls1_PRF(s,
@@ -297,62 +296,20 @@ tls1_generate_key_block(SSL *s, uint8_t *key_block, size_t key_block_len)
297static int 296static int
298tls1_change_cipher_state(SSL *s, int is_write) 297tls1_change_cipher_state(SSL *s, int is_write)
299{ 298{
300 const unsigned char *client_write_mac_secret, *server_write_mac_secret; 299 CBS mac_key, key, iv;
301 const unsigned char *client_write_key, *server_write_key;
302 const unsigned char *client_write_iv, *server_write_iv;
303 const unsigned char *mac_secret, *key, *iv;
304 int mac_secret_size, key_len, iv_len;
305 unsigned char *key_block;
306 const EVP_CIPHER *cipher;
307 const EVP_AEAD *aead;
308
309 aead = tls12_record_layer_aead(s->internal->rl);
310 cipher = tls12_record_layer_cipher(s->internal->rl);
311
312 if (aead != NULL) {
313 key_len = EVP_AEAD_key_length(aead);
314 iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(S3I(s)->hs.cipher);
315 } else {
316 key_len = EVP_CIPHER_key_length(cipher);
317 iv_len = EVP_CIPHER_iv_length(cipher);
318 }
319
320 mac_secret_size = S3I(s)->hs.tls12.mac_secret_size;
321
322 key_block = S3I(s)->hs.tls12.key_block;
323 client_write_mac_secret = key_block;
324 key_block += mac_secret_size;
325 server_write_mac_secret = key_block;
326 key_block += mac_secret_size;
327 client_write_key = key_block;
328 key_block += key_len;
329 server_write_key = key_block;
330 key_block += key_len;
331 client_write_iv = key_block;
332 key_block += iv_len;
333 server_write_iv = key_block;
334 key_block += iv_len;
335 300
336 /* Use client write keys on client write and server read. */ 301 /* Use client write keys on client write and server read. */
337 if ((!s->server && is_write) || (s->server && !is_write)) { 302 if ((!s->server && is_write) || (s->server && !is_write)) {
338 mac_secret = client_write_mac_secret; 303 tls12_key_block_client_write(S3I(s)->hs.tls12.key_block,
339 key = client_write_key; 304 &mac_key, &key, &iv);
340 iv = client_write_iv;
341 } else { 305 } else {
342 mac_secret = server_write_mac_secret; 306 tls12_key_block_server_write(S3I(s)->hs.tls12.key_block,
343 key = server_write_key; 307 &mac_key, &key, &iv);
344 iv = server_write_iv;
345 }
346
347 if (key_block - S3I(s)->hs.tls12.key_block !=
348 S3I(s)->hs.tls12.key_block_len) {
349 SSLerror(s, ERR_R_INTERNAL_ERROR);
350 goto err;
351 } 308 }
352 309
353 if (!is_write) { 310 if (!is_write) {
354 if (!tls12_record_layer_change_read_cipher_state(s->internal->rl, 311 if (!tls12_record_layer_change_read_cipher_state(s->internal->rl,
355 mac_secret, mac_secret_size, key, key_len, iv, iv_len)) 312 &mac_key, &key, &iv))
356 goto err; 313 goto err;
357 if (SSL_is_dtls(s)) 314 if (SSL_is_dtls(s))
358 dtls1_reset_read_seq_numbers(s); 315 dtls1_reset_read_seq_numbers(s);
@@ -360,7 +317,7 @@ tls1_change_cipher_state(SSL *s, int is_write)
360 &s->enc_read_ctx, &s->read_hash); 317 &s->enc_read_ctx, &s->read_hash);
361 } else { 318 } else {
362 if (!tls12_record_layer_change_write_cipher_state(s->internal->rl, 319 if (!tls12_record_layer_change_write_cipher_state(s->internal->rl,
363 mac_secret, mac_secret_size, key, key_len, iv, iv_len)) 320 &mac_key, &key, &iv))
364 goto err; 321 goto err;
365 if (SSL_is_dtls(s)) 322 if (SSL_is_dtls(s))
366 dtls1_reset_write_seq_numbers(s); 323 dtls1_reset_write_seq_numbers(s);
@@ -386,17 +343,19 @@ tls1_change_write_cipher_state(SSL *s)
386int 343int
387tls1_setup_key_block(SSL *s) 344tls1_setup_key_block(SSL *s)
388{ 345{
389 unsigned char *key_block; 346 struct tls12_key_block *key_block;
390 int mac_type = NID_undef, mac_secret_size = 0; 347 int mac_type = NID_undef, mac_secret_size = 0;
391 size_t key_block_len;
392 int key_len, iv_len;
393 const EVP_CIPHER *cipher = NULL; 348 const EVP_CIPHER *cipher = NULL;
394 const EVP_AEAD *aead = NULL; 349 const EVP_AEAD *aead = NULL;
395 const EVP_MD *handshake_hash = NULL; 350 const EVP_MD *handshake_hash = NULL;
396 const EVP_MD *mac_hash = NULL; 351 const EVP_MD *mac_hash = NULL;
397 int ret = 0; 352 int ret = 0;
398 353
399 if (S3I(s)->hs.tls12.key_block_len != 0) 354 /*
355 * XXX - callers should be changed so that they only call this
356 * function once.
357 */
358 if (S3I(s)->hs.tls12.key_block != NULL)
400 return (1); 359 return (1);
401 360
402 if (s->session->cipher && 361 if (s->session->cipher &&
@@ -405,41 +364,29 @@ tls1_setup_key_block(SSL *s)
405 SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 364 SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
406 return (0); 365 return (0);
407 } 366 }
408 key_len = EVP_AEAD_key_length(aead);
409 iv_len = SSL_CIPHER_AEAD_FIXED_NONCE_LEN(s->session->cipher);
410 } else { 367 } else {
368 /* XXX - mac_type and mac_secret_size are now unused. */
411 if (!ssl_cipher_get_evp(s->session, &cipher, &mac_hash, 369 if (!ssl_cipher_get_evp(s->session, &cipher, &mac_hash,
412 &mac_type, &mac_secret_size)) { 370 &mac_type, &mac_secret_size)) {
413 SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE); 371 SSLerror(s, SSL_R_CIPHER_OR_HASH_UNAVAILABLE);
414 return (0); 372 return (0);
415 } 373 }
416 key_len = EVP_CIPHER_key_length(cipher);
417 iv_len = EVP_CIPHER_iv_length(cipher);
418 } 374 }
419 375
420 if (!ssl_get_handshake_evp_md(s, &handshake_hash)) 376 if (!ssl_get_handshake_evp_md(s, &handshake_hash))
421 return (0); 377 return (0);
422 378
423 S3I(s)->hs.tls12.mac_secret_size = mac_secret_size;
424
425 tls12_record_layer_set_aead(s->internal->rl, aead); 379 tls12_record_layer_set_aead(s->internal->rl, aead);
426 tls12_record_layer_set_cipher_hash(s->internal->rl, cipher, 380 tls12_record_layer_set_cipher_hash(s->internal->rl, cipher,
427 handshake_hash, mac_hash); 381 handshake_hash, mac_hash);
428 382
429 tls1_cleanup_key_block(s); 383 if ((key_block = tls12_key_block_new()) == NULL)
430 384 goto err;
431 if ((key_block = reallocarray(NULL, mac_secret_size + key_len + iv_len, 385 if (!tls12_key_block_generate(key_block, s, aead, cipher, mac_hash))
432 2)) == NULL) {
433 SSLerror(s, ERR_R_MALLOC_FAILURE);
434 goto err; 386 goto err;
435 }
436 key_block_len = (mac_secret_size + key_len + iv_len) * 2;
437 387
438 S3I(s)->hs.tls12.key_block_len = key_block_len;
439 S3I(s)->hs.tls12.key_block = key_block; 388 S3I(s)->hs.tls12.key_block = key_block;
440 389 key_block = NULL;
441 if (!tls1_generate_key_block(s, key_block, key_block_len))
442 goto err;
443 390
444 if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) && 391 if (!(s->internal->options & SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS) &&
445 s->method->internal->version <= TLS1_VERSION) { 392 s->method->internal->version <= TLS1_VERSION) {
@@ -463,6 +410,8 @@ tls1_setup_key_block(SSL *s)
463 ret = 1; 410 ret = 1;
464 411
465 err: 412 err:
413 tls12_key_block_free(key_block);
414
466 return (ret); 415 return (ret);
467} 416}
468 417
diff --git a/src/lib/libssl/tls12_key_schedule.c b/src/lib/libssl/tls12_key_schedule.c
new file mode 100644
index 0000000000..c206460d95
--- /dev/null
+++ b/src/lib/libssl/tls12_key_schedule.c
@@ -0,0 +1,175 @@
1/* $OpenBSD: tls12_key_schedule.c,v 1.1 2021/05/05 10:05:27 jsing Exp $ */
2/*
3 * Copyright (c) 2021 Joel Sing <jsing@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
17
18#include <stdlib.h>
19
20#include <openssl/evp.h>
21
22#include "bytestring.h"
23#include "ssl_locl.h"
24
25struct tls12_key_block {
26 CBS client_write_mac_key;
27 CBS server_write_mac_key;
28 CBS client_write_key;
29 CBS server_write_key;
30 CBS client_write_iv;
31 CBS server_write_iv;
32
33 uint8_t *key_block;
34 size_t key_block_len;
35};
36
37struct tls12_key_block *
38tls12_key_block_new(void)
39{
40 return calloc(1, sizeof(struct tls12_key_block));
41}
42
43static void
44tls12_key_block_clear(struct tls12_key_block *kb)
45{
46 CBS_init(&kb->client_write_mac_key, NULL, 0);
47 CBS_init(&kb->server_write_mac_key, NULL, 0);
48 CBS_init(&kb->client_write_key, NULL, 0);
49 CBS_init(&kb->server_write_key, NULL, 0);
50 CBS_init(&kb->client_write_iv, NULL, 0);
51 CBS_init(&kb->server_write_iv, NULL, 0);
52
53 freezero(kb->key_block, kb->key_block_len);
54 kb->key_block = NULL;
55 kb->key_block_len = 0;
56}
57
58void
59tls12_key_block_free(struct tls12_key_block *kb)
60{
61 if (kb == NULL)
62 return;
63
64 tls12_key_block_clear(kb);
65
66 freezero(kb, sizeof(struct tls12_key_block));
67}
68
69void
70tls12_key_block_client_write(struct tls12_key_block *kb, CBS *mac_key,
71 CBS *key, CBS *iv)
72{
73 CBS_dup(&kb->client_write_mac_key, mac_key);
74 CBS_dup(&kb->client_write_key, key);
75 CBS_dup(&kb->client_write_iv, iv);
76}
77
78void
79tls12_key_block_server_write(struct tls12_key_block *kb, CBS *mac_key,
80 CBS *key, CBS *iv)
81{
82 CBS_dup(&kb->server_write_mac_key, mac_key);
83 CBS_dup(&kb->server_write_key, key);
84 CBS_dup(&kb->server_write_iv, iv);
85}
86
87int
88tls12_key_block_generate(struct tls12_key_block *kb, SSL *s,
89 const EVP_AEAD *aead, const EVP_CIPHER *cipher, const EVP_MD *mac_hash)
90{
91 size_t mac_key_len = 0, key_len = 0, iv_len = 0;
92 uint8_t *key_block = NULL;
93 size_t key_block_len = 0;
94 CBS cbs;
95
96 /*
97 * Generate a TLSv1.2 key block and partition into individual secrets,
98 * as per RFC 5246 section 6.3.
99 */
100
101 tls12_key_block_clear(kb);
102
103 /* Must have AEAD or cipher/MAC pair. */
104 if (aead == NULL && (cipher == NULL || mac_hash == NULL))
105 goto err;
106
107 if (aead != NULL) {
108 key_len = EVP_AEAD_key_length(aead);
109
110 /* AEAD fixed nonce length. */
111 if (aead == EVP_aead_aes_128_gcm() ||
112 aead == EVP_aead_aes_256_gcm())
113 iv_len = 4;
114 else if (aead == EVP_aead_chacha20_poly1305())
115 iv_len = 12;
116 else
117 goto err;
118 } else if (cipher != NULL && mac_hash != NULL) {
119 /*
120 * A negative integer return value will be detected via the
121 * EVP_MAX_* checks against the size_t variables below.
122 */
123 mac_key_len = EVP_MD_size(mac_hash);
124 key_len = EVP_CIPHER_key_length(cipher);
125 iv_len = EVP_CIPHER_iv_length(cipher);
126
127 /* Special handling for GOST... */
128 if (EVP_MD_type(mac_hash) == NID_id_Gost28147_89_MAC)
129 mac_key_len = 32;
130 }
131
132 if (mac_key_len > EVP_MAX_MD_SIZE)
133 goto err;
134 if (key_len > EVP_MAX_KEY_LENGTH)
135 goto err;
136 if (iv_len > EVP_MAX_IV_LENGTH)
137 goto err;
138
139 key_block_len = 2 * mac_key_len + 2 * key_len + 2 * iv_len;
140 if ((key_block = calloc(1, key_block_len)) == NULL)
141 goto err;
142
143 if (!tls1_generate_key_block(s, key_block, key_block_len))
144 goto err;
145
146 kb->key_block = key_block;
147 kb->key_block_len = key_block_len;
148 key_block = NULL;
149 key_block_len = 0;
150
151 /* Partition key block into individual secrets. */
152 CBS_init(&cbs, kb->key_block, kb->key_block_len);
153 if (!CBS_get_bytes(&cbs, &kb->client_write_mac_key, mac_key_len))
154 goto err;
155 if (!CBS_get_bytes(&cbs, &kb->server_write_mac_key, mac_key_len))
156 goto err;
157 if (!CBS_get_bytes(&cbs, &kb->client_write_key, key_len))
158 goto err;
159 if (!CBS_get_bytes(&cbs, &kb->server_write_key, key_len))
160 goto err;
161 if (!CBS_get_bytes(&cbs, &kb->client_write_iv, iv_len))
162 goto err;
163 if (!CBS_get_bytes(&cbs, &kb->server_write_iv, iv_len))
164 goto err;
165 if (CBS_len(&cbs) != 0)
166 goto err;
167
168 return 1;
169
170 err:
171 tls12_key_block_clear(kb);
172 freezero(key_block, key_block_len);
173
174 return 0;
175}
diff --git a/src/lib/libssl/tls12_record_layer.c b/src/lib/libssl/tls12_record_layer.c
index 7e29f4ed65..b9a3320de8 100644
--- a/src/lib/libssl/tls12_record_layer.c
+++ b/src/lib/libssl/tls12_record_layer.c
@@ -1,4 +1,4 @@
1/* $OpenBSD: tls12_record_layer.c,v 1.26 2021/04/19 17:26:39 jsing Exp $ */ 1/* $OpenBSD: tls12_record_layer.c,v 1.27 2021/05/05 10:05:27 jsing Exp $ */
2/* 2/*
3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org> 3 * Copyright (c) 2020 Joel Sing <jsing@openbsd.org>
4 * 4 *
@@ -254,18 +254,6 @@ tls12_record_layer_write_protected(struct tls12_record_layer *rl)
254 return tls12_record_protection_engaged(rl->write); 254 return tls12_record_protection_engaged(rl->write);
255} 255}
256 256
257const EVP_AEAD *
258tls12_record_layer_aead(struct tls12_record_layer *rl)
259{
260 return rl->aead;
261}
262
263const EVP_CIPHER *
264tls12_record_layer_cipher(struct tls12_record_layer *rl)
265{
266 return rl->cipher;
267}
268
269void 257void
270tls12_record_layer_set_aead(struct tls12_record_layer *rl, const EVP_AEAD *aead) 258tls12_record_layer_set_aead(struct tls12_record_layer *rl, const EVP_AEAD *aead)
271{ 259{
@@ -410,11 +398,10 @@ tls12_record_layer_set_mac_key(struct tls12_record_protection *rp,
410 398
411static int 399static int
412tls12_record_layer_ccs_aead(struct tls12_record_layer *rl, 400tls12_record_layer_ccs_aead(struct tls12_record_layer *rl,
413 struct tls12_record_protection *rp, int is_write, const uint8_t *mac_key, 401 struct tls12_record_protection *rp, int is_write, CBS *mac_key, CBS *key,
414 size_t mac_key_len, const uint8_t *key, size_t key_len, const uint8_t *iv, 402 CBS *iv)
415 size_t iv_len)
416{ 403{
417 size_t aead_nonce_len; 404 size_t aead_nonce_len, fixed_nonce_len;
418 405
419 if (!tls12_record_protection_unused(rp)) 406 if (!tls12_record_protection_unused(rp))
420 return 0; 407 return 0;
@@ -431,11 +418,11 @@ tls12_record_layer_ccs_aead(struct tls12_record_layer *rl,
431 if (rl->aead == EVP_aead_chacha20_poly1305()) 418 if (rl->aead == EVP_aead_chacha20_poly1305())
432 rp->aead_ctx->xor_fixed_nonce = 1; 419 rp->aead_ctx->xor_fixed_nonce = 1;
433 420
434 if (iv_len > sizeof(rp->aead_ctx->fixed_nonce)) 421 if (!CBS_write_bytes(iv, rp->aead_ctx->fixed_nonce,
422 sizeof(rp->aead_ctx->fixed_nonce), &fixed_nonce_len))
435 return 0; 423 return 0;
436 424
437 memcpy(rp->aead_ctx->fixed_nonce, iv, iv_len); 425 rp->aead_ctx->fixed_nonce_len = fixed_nonce_len;
438 rp->aead_ctx->fixed_nonce_len = iv_len;
439 rp->aead_ctx->tag_len = EVP_AEAD_max_overhead(rl->aead); 426 rp->aead_ctx->tag_len = EVP_AEAD_max_overhead(rl->aead);
440 rp->aead_ctx->variable_nonce_len = 8; 427 rp->aead_ctx->variable_nonce_len = 8;
441 428
@@ -454,8 +441,8 @@ tls12_record_layer_ccs_aead(struct tls12_record_layer *rl,
454 return 0; 441 return 0;
455 } 442 }
456 443
457 if (!EVP_AEAD_CTX_init(&rp->aead_ctx->ctx, rl->aead, key, key_len, 444 if (!EVP_AEAD_CTX_init(&rp->aead_ctx->ctx, rl->aead, CBS_data(key),
458 EVP_AEAD_DEFAULT_TAG_LENGTH, NULL)) 445 CBS_len(key), EVP_AEAD_DEFAULT_TAG_LENGTH, NULL))
459 return 0; 446 return 0;
460 447
461 return 1; 448 return 1;
@@ -463,9 +450,8 @@ tls12_record_layer_ccs_aead(struct tls12_record_layer *rl,
463 450
464static int 451static int
465tls12_record_layer_ccs_cipher(struct tls12_record_layer *rl, 452tls12_record_layer_ccs_cipher(struct tls12_record_layer *rl,
466 struct tls12_record_protection *rp, int is_write, const uint8_t *mac_key, 453 struct tls12_record_protection *rp, int is_write, CBS *mac_key, CBS *key,
467 size_t mac_key_len, const uint8_t *key, size_t key_len, const uint8_t *iv, 454 CBS *iv)
468 size_t iv_len)
469{ 455{
470 EVP_PKEY *mac_pkey = NULL; 456 EVP_PKEY *mac_pkey = NULL;
471 int gost_param_nid; 457 int gost_param_nid;
@@ -478,23 +464,23 @@ tls12_record_layer_ccs_cipher(struct tls12_record_layer *rl,
478 mac_type = EVP_PKEY_HMAC; 464 mac_type = EVP_PKEY_HMAC;
479 rp->stream_mac = 0; 465 rp->stream_mac = 0;
480 466
481 if (iv_len > INT_MAX || key_len > INT_MAX) 467 if (CBS_len(iv) > INT_MAX || CBS_len(key) > INT_MAX)
482 goto err; 468 goto err;
483 if (EVP_CIPHER_iv_length(rl->cipher) != iv_len) 469 if (EVP_CIPHER_iv_length(rl->cipher) != CBS_len(iv))
484 goto err; 470 goto err;
485 if (EVP_CIPHER_key_length(rl->cipher) != key_len) 471 if (EVP_CIPHER_key_length(rl->cipher) != CBS_len(key))
486 goto err; 472 goto err;
487 473
488 /* Special handling for GOST... */ 474 /* Special handling for GOST... */
489 if (EVP_MD_type(rl->mac_hash) == NID_id_Gost28147_89_MAC) { 475 if (EVP_MD_type(rl->mac_hash) == NID_id_Gost28147_89_MAC) {
490 if (mac_key_len != 32) 476 if (CBS_len(mac_key) != 32)
491 goto err; 477 goto err;
492 mac_type = EVP_PKEY_GOSTIMIT; 478 mac_type = EVP_PKEY_GOSTIMIT;
493 rp->stream_mac = 1; 479 rp->stream_mac = 1;
494 } else { 480 } else {
495 if (mac_key_len > INT_MAX) 481 if (CBS_len(mac_key) > INT_MAX)
496 goto err; 482 goto err;
497 if (EVP_MD_size(rl->mac_hash) != mac_key_len) 483 if (EVP_MD_size(rl->mac_hash) != CBS_len(mac_key))
498 goto err; 484 goto err;
499 } 485 }
500 486
@@ -503,15 +489,16 @@ tls12_record_layer_ccs_cipher(struct tls12_record_layer *rl,
503 if ((rp->hash_ctx = EVP_MD_CTX_new()) == NULL) 489 if ((rp->hash_ctx = EVP_MD_CTX_new()) == NULL)
504 goto err; 490 goto err;
505 491
506 if (!tls12_record_layer_set_mac_key(rp, mac_key, mac_key_len)) 492 if (!tls12_record_layer_set_mac_key(rp, CBS_data(mac_key),
493 CBS_len(mac_key)))
507 goto err; 494 goto err;
508 495
509 if ((mac_pkey = EVP_PKEY_new_mac_key(mac_type, NULL, mac_key, 496 if ((mac_pkey = EVP_PKEY_new_mac_key(mac_type, NULL, CBS_data(mac_key),
510 mac_key_len)) == NULL) 497 CBS_len(mac_key))) == NULL)
511 goto err; 498 goto err;
512 499
513 if (!EVP_CipherInit_ex(rp->cipher_ctx, rl->cipher, NULL, key, iv, 500 if (!EVP_CipherInit_ex(rp->cipher_ctx, rl->cipher, NULL, CBS_data(key),
514 is_write)) 501 CBS_data(iv), is_write))
515 goto err; 502 goto err;
516 503
517 if (EVP_DigestSignInit(rp->hash_ctx, NULL, rl->mac_hash, NULL, 504 if (EVP_DigestSignInit(rp->hash_ctx, NULL, rl->mac_hash, NULL,
@@ -545,22 +532,20 @@ tls12_record_layer_ccs_cipher(struct tls12_record_layer *rl,
545 532
546static int 533static int
547tls12_record_layer_change_cipher_state(struct tls12_record_layer *rl, 534tls12_record_layer_change_cipher_state(struct tls12_record_layer *rl,
548 struct tls12_record_protection *rp, int is_write, const uint8_t *mac_key, 535 struct tls12_record_protection *rp, int is_write, CBS *mac_key, CBS *key,
549 size_t mac_key_len, const uint8_t *key, size_t key_len, const uint8_t *iv, 536 CBS *iv)
550 size_t iv_len)
551{ 537{
552 if (rl->aead != NULL) 538 if (rl->aead != NULL)
553 return tls12_record_layer_ccs_aead(rl, rp, is_write, mac_key, 539 return tls12_record_layer_ccs_aead(rl, rp, is_write, mac_key,
554 mac_key_len, key, key_len, iv, iv_len); 540 key, iv);
555 541
556 return tls12_record_layer_ccs_cipher(rl, rp, is_write, mac_key, 542 return tls12_record_layer_ccs_cipher(rl, rp, is_write, mac_key,
557 mac_key_len, key, key_len, iv, iv_len); 543 key, iv);
558} 544}
559 545
560int 546int
561tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl, 547tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
562 const uint8_t *mac_key, size_t mac_key_len, const uint8_t *key, 548 CBS *mac_key, CBS *key, CBS *iv)
563 size_t key_len, const uint8_t *iv, size_t iv_len)
564{ 549{
565 struct tls12_record_protection *read_new = NULL; 550 struct tls12_record_protection *read_new = NULL;
566 int ret = 0; 551 int ret = 0;
@@ -571,7 +556,7 @@ tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
571 /* Read sequence number gets reset to zero. */ 556 /* Read sequence number gets reset to zero. */
572 557
573 if (!tls12_record_layer_change_cipher_state(rl, read_new, 0, 558 if (!tls12_record_layer_change_cipher_state(rl, read_new, 0,
574 mac_key, mac_key_len, key, key_len, iv, iv_len)) 559 mac_key, key, iv))
575 goto err; 560 goto err;
576 561
577 tls12_record_protection_free(rl->read_current); 562 tls12_record_protection_free(rl->read_current);
@@ -588,8 +573,7 @@ tls12_record_layer_change_read_cipher_state(struct tls12_record_layer *rl,
588 573
589int 574int
590tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl, 575tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl,
591 const uint8_t *mac_key, size_t mac_key_len, const uint8_t *key, 576 CBS *mac_key, CBS *key, CBS *iv)
592 size_t key_len, const uint8_t *iv, size_t iv_len)
593{ 577{
594 struct tls12_record_protection *write_new; 578 struct tls12_record_protection *write_new;
595 int ret = 0; 579 int ret = 0;
@@ -600,7 +584,7 @@ tls12_record_layer_change_write_cipher_state(struct tls12_record_layer *rl,
600 /* Write sequence number gets reset to zero. */ 584 /* Write sequence number gets reset to zero. */
601 585
602 if (!tls12_record_layer_change_cipher_state(rl, write_new, 1, 586 if (!tls12_record_layer_change_cipher_state(rl, write_new, 1,
603 mac_key, mac_key_len, key, key_len, iv, iv_len)) 587 mac_key, key, iv))
604 goto err; 588 goto err;
605 589
606 if (rl->dtls) { 590 if (rl->dtls) {