xref: /dflybsd-src/crypto/libressl/ssl/tls13_client.c (revision 8edacedfc4a3bc9ac4f463b53f61cb4a6eb8d031)
1*8edacedfSDaniel Fojt /* $OpenBSD: tls13_client.c,v 1.67 2020/09/11 17:36:27 jsing Exp $ */
2cca6fc52SDaniel Fojt /*
3cca6fc52SDaniel Fojt  * Copyright (c) 2018, 2019 Joel Sing <jsing@openbsd.org>
4cca6fc52SDaniel Fojt  *
5cca6fc52SDaniel Fojt  * Permission to use, copy, modify, and distribute this software for any
6cca6fc52SDaniel Fojt  * purpose with or without fee is hereby granted, provided that the above
7cca6fc52SDaniel Fojt  * copyright notice and this permission notice appear in all copies.
8cca6fc52SDaniel Fojt  *
9cca6fc52SDaniel Fojt  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10cca6fc52SDaniel Fojt  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11cca6fc52SDaniel Fojt  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12cca6fc52SDaniel Fojt  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13cca6fc52SDaniel Fojt  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14cca6fc52SDaniel Fojt  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15cca6fc52SDaniel Fojt  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16cca6fc52SDaniel Fojt  */
17cca6fc52SDaniel Fojt 
18cca6fc52SDaniel Fojt #include "ssl_locl.h"
19cca6fc52SDaniel Fojt 
20cca6fc52SDaniel Fojt #include <openssl/ssl3.h>
21cca6fc52SDaniel Fojt 
22cca6fc52SDaniel Fojt #include "bytestring.h"
23cca6fc52SDaniel Fojt #include "ssl_tlsext.h"
24cca6fc52SDaniel Fojt #include "tls13_handshake.h"
25cca6fc52SDaniel Fojt #include "tls13_internal.h"
26cca6fc52SDaniel Fojt 
27cca6fc52SDaniel Fojt int
28cca6fc52SDaniel Fojt tls13_client_init(struct tls13_ctx *ctx)
29cca6fc52SDaniel Fojt {
30cca6fc52SDaniel Fojt 	const uint16_t *groups;
31cca6fc52SDaniel Fojt 	size_t groups_len;
32cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
33cca6fc52SDaniel Fojt 
34cca6fc52SDaniel Fojt 	if (!ssl_supported_version_range(s, &ctx->hs->min_version,
35cca6fc52SDaniel Fojt 	    &ctx->hs->max_version)) {
36cca6fc52SDaniel Fojt 		SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE);
37cca6fc52SDaniel Fojt 		return 0;
38cca6fc52SDaniel Fojt 	}
39cca6fc52SDaniel Fojt 	s->client_version = s->version = ctx->hs->max_version;
40cca6fc52SDaniel Fojt 
41*8edacedfSDaniel Fojt 	tls13_record_layer_set_retry_after_phh(ctx->rl,
42*8edacedfSDaniel Fojt 	    (s->internal->mode & SSL_MODE_AUTO_RETRY) != 0);
43*8edacedfSDaniel Fojt 
44cca6fc52SDaniel Fojt 	if (!ssl_get_new_session(s, 0)) /* XXX */
45cca6fc52SDaniel Fojt 		return 0;
46cca6fc52SDaniel Fojt 
47cca6fc52SDaniel Fojt 	if (!tls1_transcript_init(s))
48cca6fc52SDaniel Fojt 		return 0;
49cca6fc52SDaniel Fojt 
50cca6fc52SDaniel Fojt 	/* Generate a key share using our preferred group. */
51cca6fc52SDaniel Fojt 	tls1_get_group_list(s, 0, &groups, &groups_len);
52cca6fc52SDaniel Fojt 	if (groups_len < 1)
53cca6fc52SDaniel Fojt 		return 0;
54cca6fc52SDaniel Fojt 	if ((ctx->hs->key_share = tls13_key_share_new(groups[0])) == NULL)
55cca6fc52SDaniel Fojt 		return 0;
56cca6fc52SDaniel Fojt 	if (!tls13_key_share_generate(ctx->hs->key_share))
57cca6fc52SDaniel Fojt 		return 0;
58cca6fc52SDaniel Fojt 
59cca6fc52SDaniel Fojt 	arc4random_buf(s->s3->client_random, SSL3_RANDOM_SIZE);
60cca6fc52SDaniel Fojt 
61cca6fc52SDaniel Fojt 	/*
62cca6fc52SDaniel Fojt 	 * The legacy session identifier should either be set to an
63cca6fc52SDaniel Fojt 	 * unpredictable 32-byte value or zero length... a non-zero length
64cca6fc52SDaniel Fojt 	 * legacy session identifier triggers compatibility mode (see RFC 8446
65cca6fc52SDaniel Fojt 	 * Appendix D.4). In the pre-TLSv1.3 case a zero length value is used.
66cca6fc52SDaniel Fojt 	 */
67*8edacedfSDaniel Fojt 	if (ctx->middlebox_compat && ctx->hs->max_version >= TLS1_3_VERSION) {
68cca6fc52SDaniel Fojt 		arc4random_buf(ctx->hs->legacy_session_id,
69cca6fc52SDaniel Fojt 		    sizeof(ctx->hs->legacy_session_id));
70cca6fc52SDaniel Fojt 		ctx->hs->legacy_session_id_len =
71cca6fc52SDaniel Fojt 		    sizeof(ctx->hs->legacy_session_id);
72cca6fc52SDaniel Fojt 	}
73cca6fc52SDaniel Fojt 
74cca6fc52SDaniel Fojt 	return 1;
75cca6fc52SDaniel Fojt }
76cca6fc52SDaniel Fojt 
77cca6fc52SDaniel Fojt int
78cca6fc52SDaniel Fojt tls13_client_connect(struct tls13_ctx *ctx)
79cca6fc52SDaniel Fojt {
80cca6fc52SDaniel Fojt 	if (ctx->mode != TLS13_HS_CLIENT)
81cca6fc52SDaniel Fojt 		return TLS13_IO_FAILURE;
82cca6fc52SDaniel Fojt 
83cca6fc52SDaniel Fojt 	return tls13_handshake_perform(ctx);
84cca6fc52SDaniel Fojt }
85cca6fc52SDaniel Fojt 
86cca6fc52SDaniel Fojt static int
87cca6fc52SDaniel Fojt tls13_client_hello_build(struct tls13_ctx *ctx, CBB *cbb)
88cca6fc52SDaniel Fojt {
89cca6fc52SDaniel Fojt 	CBB cipher_suites, compression_methods, session_id;
90cca6fc52SDaniel Fojt 	uint16_t client_version;
91cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
92cca6fc52SDaniel Fojt 
93cca6fc52SDaniel Fojt 	/* Legacy client version is capped at TLS 1.2. */
94cca6fc52SDaniel Fojt 	client_version = ctx->hs->max_version;
95cca6fc52SDaniel Fojt 	if (client_version > TLS1_2_VERSION)
96cca6fc52SDaniel Fojt 		client_version = TLS1_2_VERSION;
97cca6fc52SDaniel Fojt 
98cca6fc52SDaniel Fojt 	if (!CBB_add_u16(cbb, client_version))
99cca6fc52SDaniel Fojt 		goto err;
100cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(cbb, s->s3->client_random, SSL3_RANDOM_SIZE))
101cca6fc52SDaniel Fojt 		goto err;
102cca6fc52SDaniel Fojt 
103cca6fc52SDaniel Fojt 	if (!CBB_add_u8_length_prefixed(cbb, &session_id))
104cca6fc52SDaniel Fojt 		goto err;
105cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&session_id, ctx->hs->legacy_session_id,
106cca6fc52SDaniel Fojt 	    ctx->hs->legacy_session_id_len))
107cca6fc52SDaniel Fojt 		goto err;
108cca6fc52SDaniel Fojt 
109cca6fc52SDaniel Fojt 	if (!CBB_add_u16_length_prefixed(cbb, &cipher_suites))
110cca6fc52SDaniel Fojt 		goto err;
111cca6fc52SDaniel Fojt 	if (!ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &cipher_suites)) {
112cca6fc52SDaniel Fojt 		SSLerror(s, SSL_R_NO_CIPHERS_AVAILABLE);
113cca6fc52SDaniel Fojt 		goto err;
114cca6fc52SDaniel Fojt 	}
115cca6fc52SDaniel Fojt 
116cca6fc52SDaniel Fojt 	if (!CBB_add_u8_length_prefixed(cbb, &compression_methods))
117cca6fc52SDaniel Fojt 		goto err;
118cca6fc52SDaniel Fojt 	if (!CBB_add_u8(&compression_methods, 0))
119cca6fc52SDaniel Fojt 		goto err;
120cca6fc52SDaniel Fojt 
121*8edacedfSDaniel Fojt 	if (!tlsext_client_build(s, SSL_TLSEXT_MSG_CH, cbb))
122cca6fc52SDaniel Fojt 		goto err;
123cca6fc52SDaniel Fojt 
124cca6fc52SDaniel Fojt 	if (!CBB_flush(cbb))
125cca6fc52SDaniel Fojt 		goto err;
126cca6fc52SDaniel Fojt 
127cca6fc52SDaniel Fojt 	return 1;
128cca6fc52SDaniel Fojt 
129cca6fc52SDaniel Fojt  err:
130cca6fc52SDaniel Fojt 	return 0;
131cca6fc52SDaniel Fojt }
132cca6fc52SDaniel Fojt 
133cca6fc52SDaniel Fojt int
134cca6fc52SDaniel Fojt tls13_client_hello_send(struct tls13_ctx *ctx, CBB *cbb)
135cca6fc52SDaniel Fojt {
136cca6fc52SDaniel Fojt 	if (ctx->hs->min_version < TLS1_2_VERSION)
137cca6fc52SDaniel Fojt 		tls13_record_layer_set_legacy_version(ctx->rl, TLS1_VERSION);
138cca6fc52SDaniel Fojt 
139cca6fc52SDaniel Fojt 	/* We may receive a pre-TLSv1.3 alert in response to the client hello. */
140cca6fc52SDaniel Fojt 	tls13_record_layer_allow_legacy_alerts(ctx->rl, 1);
141cca6fc52SDaniel Fojt 
142cca6fc52SDaniel Fojt 	if (!tls13_client_hello_build(ctx, cbb))
143cca6fc52SDaniel Fojt 		return 0;
144cca6fc52SDaniel Fojt 
145cca6fc52SDaniel Fojt 	return 1;
146cca6fc52SDaniel Fojt }
147cca6fc52SDaniel Fojt 
148cca6fc52SDaniel Fojt int
149cca6fc52SDaniel Fojt tls13_client_hello_sent(struct tls13_ctx *ctx)
150cca6fc52SDaniel Fojt {
151cca6fc52SDaniel Fojt 	tls13_record_layer_allow_ccs(ctx->rl, 1);
152cca6fc52SDaniel Fojt 
153cca6fc52SDaniel Fojt 	tls1_transcript_freeze(ctx->ssl);
154cca6fc52SDaniel Fojt 
155*8edacedfSDaniel Fojt 	if (ctx->middlebox_compat)
156*8edacedfSDaniel Fojt 		ctx->send_dummy_ccs = 1;
157*8edacedfSDaniel Fojt 
158cca6fc52SDaniel Fojt 	return 1;
159cca6fc52SDaniel Fojt }
160cca6fc52SDaniel Fojt 
161cca6fc52SDaniel Fojt static int
162cca6fc52SDaniel Fojt tls13_server_hello_is_legacy(CBS *cbs)
163cca6fc52SDaniel Fojt {
164cca6fc52SDaniel Fojt 	CBS extensions_block, extensions, extension_data;
165cca6fc52SDaniel Fojt 	uint16_t selected_version = 0;
166cca6fc52SDaniel Fojt 	uint16_t type;
167cca6fc52SDaniel Fojt 
168cca6fc52SDaniel Fojt 	CBS_dup(cbs, &extensions_block);
169cca6fc52SDaniel Fojt 
170cca6fc52SDaniel Fojt 	if (!CBS_get_u16_length_prefixed(&extensions_block, &extensions))
171cca6fc52SDaniel Fojt 		return 1;
172cca6fc52SDaniel Fojt 
173cca6fc52SDaniel Fojt 	while (CBS_len(&extensions) > 0) {
174cca6fc52SDaniel Fojt 		if (!CBS_get_u16(&extensions, &type))
175cca6fc52SDaniel Fojt 			return 1;
176cca6fc52SDaniel Fojt 		if (!CBS_get_u16_length_prefixed(&extensions, &extension_data))
177cca6fc52SDaniel Fojt 			return 1;
178cca6fc52SDaniel Fojt 
179cca6fc52SDaniel Fojt 		if (type != TLSEXT_TYPE_supported_versions)
180cca6fc52SDaniel Fojt 			continue;
181cca6fc52SDaniel Fojt 		if (!CBS_get_u16(&extension_data, &selected_version))
182cca6fc52SDaniel Fojt 			return 1;
183cca6fc52SDaniel Fojt 		if (CBS_len(&extension_data) != 0)
184cca6fc52SDaniel Fojt 			return 1;
185cca6fc52SDaniel Fojt 	}
186cca6fc52SDaniel Fojt 
187cca6fc52SDaniel Fojt 	return (selected_version < TLS1_3_VERSION);
188cca6fc52SDaniel Fojt }
189cca6fc52SDaniel Fojt 
190cca6fc52SDaniel Fojt static int
191cca6fc52SDaniel Fojt tls13_server_hello_is_retry(CBS *cbs)
192cca6fc52SDaniel Fojt {
193cca6fc52SDaniel Fojt 	CBS server_hello, server_random;
194cca6fc52SDaniel Fojt 	uint16_t legacy_version;
195cca6fc52SDaniel Fojt 
196cca6fc52SDaniel Fojt 	CBS_dup(cbs, &server_hello);
197cca6fc52SDaniel Fojt 
198cca6fc52SDaniel Fojt 	if (!CBS_get_u16(&server_hello, &legacy_version))
199cca6fc52SDaniel Fojt 		return 0;
200cca6fc52SDaniel Fojt 	if (!CBS_get_bytes(&server_hello, &server_random, SSL3_RANDOM_SIZE))
201cca6fc52SDaniel Fojt 		return 0;
202cca6fc52SDaniel Fojt 
203cca6fc52SDaniel Fojt 	/* See if this is a HelloRetryRequest. */
204cca6fc52SDaniel Fojt 	return CBS_mem_equal(&server_random, tls13_hello_retry_request_hash,
205cca6fc52SDaniel Fojt 	    sizeof(tls13_hello_retry_request_hash));
206cca6fc52SDaniel Fojt }
207cca6fc52SDaniel Fojt 
208cca6fc52SDaniel Fojt static int
209cca6fc52SDaniel Fojt tls13_server_hello_process(struct tls13_ctx *ctx, CBS *cbs)
210cca6fc52SDaniel Fojt {
211cca6fc52SDaniel Fojt 	CBS server_random, session_id;
212cca6fc52SDaniel Fojt 	uint16_t tlsext_msg_type = SSL_TLSEXT_MSG_SH;
213cca6fc52SDaniel Fojt 	uint16_t cipher_suite, legacy_version;
214cca6fc52SDaniel Fojt 	uint8_t compression_method;
215cca6fc52SDaniel Fojt 	const SSL_CIPHER *cipher;
216cca6fc52SDaniel Fojt 	int alert_desc;
217cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
218cca6fc52SDaniel Fojt 
219cca6fc52SDaniel Fojt 	if (!CBS_get_u16(cbs, &legacy_version))
220cca6fc52SDaniel Fojt 		goto err;
221cca6fc52SDaniel Fojt 	if (!CBS_get_bytes(cbs, &server_random, SSL3_RANDOM_SIZE))
222cca6fc52SDaniel Fojt 		goto err;
223cca6fc52SDaniel Fojt 	if (!CBS_get_u8_length_prefixed(cbs, &session_id))
224cca6fc52SDaniel Fojt 		goto err;
225cca6fc52SDaniel Fojt 	if (!CBS_get_u16(cbs, &cipher_suite))
226cca6fc52SDaniel Fojt 		goto err;
227cca6fc52SDaniel Fojt 	if (!CBS_get_u8(cbs, &compression_method))
228cca6fc52SDaniel Fojt 		goto err;
229cca6fc52SDaniel Fojt 
230cca6fc52SDaniel Fojt 	if (tls13_server_hello_is_legacy(cbs)) {
231cca6fc52SDaniel Fojt 		if (ctx->hs->max_version >= TLS1_3_VERSION) {
232cca6fc52SDaniel Fojt 			/*
233cca6fc52SDaniel Fojt 			 * RFC 8446 section 4.1.3, We must not downgrade if
234cca6fc52SDaniel Fojt 			 * the server random value contains the TLS 1.2 or 1.1
235cca6fc52SDaniel Fojt 			 * magical value.
236cca6fc52SDaniel Fojt 			 */
237cca6fc52SDaniel Fojt 			if (!CBS_skip(&server_random, CBS_len(&server_random) -
238cca6fc52SDaniel Fojt 			    sizeof(tls13_downgrade_12)))
239cca6fc52SDaniel Fojt 				goto err;
240cca6fc52SDaniel Fojt 			if (CBS_mem_equal(&server_random, tls13_downgrade_12,
241cca6fc52SDaniel Fojt 			    sizeof(tls13_downgrade_12)) ||
242cca6fc52SDaniel Fojt 			    CBS_mem_equal(&server_random, tls13_downgrade_11,
243cca6fc52SDaniel Fojt 			    sizeof(tls13_downgrade_11))) {
244*8edacedfSDaniel Fojt 				ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
245cca6fc52SDaniel Fojt 				goto err;
246cca6fc52SDaniel Fojt 			}
247cca6fc52SDaniel Fojt 		}
248cca6fc52SDaniel Fojt 
249cca6fc52SDaniel Fojt 		if (!CBS_skip(cbs, CBS_len(cbs)))
250cca6fc52SDaniel Fojt 			goto err;
251cca6fc52SDaniel Fojt 
252cca6fc52SDaniel Fojt 		ctx->hs->use_legacy = 1;
253cca6fc52SDaniel Fojt 		return 1;
254cca6fc52SDaniel Fojt 	}
255cca6fc52SDaniel Fojt 
256cca6fc52SDaniel Fojt 	/* From here on in we know we are doing TLSv1.3. */
257*8edacedfSDaniel Fojt 	tls13_record_layer_set_legacy_version(ctx->rl, TLS1_2_VERSION);
258cca6fc52SDaniel Fojt 	tls13_record_layer_allow_legacy_alerts(ctx->rl, 0);
259cca6fc52SDaniel Fojt 
260cca6fc52SDaniel Fojt 	/* See if this is a HelloRetryRequest. */
261cca6fc52SDaniel Fojt 	/* XXX - see if we can avoid doing this twice. */
262cca6fc52SDaniel Fojt 	if (CBS_mem_equal(&server_random, tls13_hello_retry_request_hash,
263cca6fc52SDaniel Fojt 	    sizeof(tls13_hello_retry_request_hash))) {
264cca6fc52SDaniel Fojt 		tlsext_msg_type = SSL_TLSEXT_MSG_HRR;
265cca6fc52SDaniel Fojt 		ctx->hs->hrr = 1;
266cca6fc52SDaniel Fojt 	}
267cca6fc52SDaniel Fojt 
268*8edacedfSDaniel Fojt 	if (!tlsext_client_parse(s, tlsext_msg_type, cbs, &alert_desc)) {
269cca6fc52SDaniel Fojt 		ctx->alert = alert_desc;
270cca6fc52SDaniel Fojt 		goto err;
271cca6fc52SDaniel Fojt 	}
272cca6fc52SDaniel Fojt 
273cca6fc52SDaniel Fojt 	/*
274cca6fc52SDaniel Fojt 	 * See if a supported versions extension was returned. If it was then
275cca6fc52SDaniel Fojt 	 * the legacy version must be set to 0x0303 (RFC 8446 section 4.1.3).
276cca6fc52SDaniel Fojt 	 * Otherwise, fallback to the legacy version, ensuring that it is both
277cca6fc52SDaniel Fojt 	 * within range and not TLS 1.3 or greater (which must use the
278cca6fc52SDaniel Fojt 	 * supported version extension.
279cca6fc52SDaniel Fojt 	 */
280cca6fc52SDaniel Fojt 	if (ctx->hs->server_version != 0) {
281cca6fc52SDaniel Fojt 		if (legacy_version != TLS1_2_VERSION) {
282*8edacedfSDaniel Fojt 			ctx->alert = TLS13_ALERT_PROTOCOL_VERSION;
283cca6fc52SDaniel Fojt 			goto err;
284cca6fc52SDaniel Fojt 		}
285cca6fc52SDaniel Fojt 	} else {
286cca6fc52SDaniel Fojt 		if (legacy_version < ctx->hs->min_version ||
287cca6fc52SDaniel Fojt 		    legacy_version > ctx->hs->max_version ||
288cca6fc52SDaniel Fojt 		    legacy_version > TLS1_2_VERSION) {
289*8edacedfSDaniel Fojt 			ctx->alert = TLS13_ALERT_PROTOCOL_VERSION;
290cca6fc52SDaniel Fojt 			goto err;
291cca6fc52SDaniel Fojt 		}
292cca6fc52SDaniel Fojt 		ctx->hs->server_version = legacy_version;
293cca6fc52SDaniel Fojt 	}
294cca6fc52SDaniel Fojt 
295cca6fc52SDaniel Fojt 	/* The session_id must match. */
296cca6fc52SDaniel Fojt 	if (!CBS_mem_equal(&session_id, ctx->hs->legacy_session_id,
297cca6fc52SDaniel Fojt 	    ctx->hs->legacy_session_id_len)) {
298*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
299cca6fc52SDaniel Fojt 		goto err;
300cca6fc52SDaniel Fojt 	}
301cca6fc52SDaniel Fojt 
302cca6fc52SDaniel Fojt 	/*
303cca6fc52SDaniel Fojt 	 * Ensure that the cipher suite is one that we offered in the client
304cca6fc52SDaniel Fojt 	 * hello and that it matches the TLS version selected.
305cca6fc52SDaniel Fojt 	 */
306cca6fc52SDaniel Fojt 	cipher = ssl3_get_cipher_by_value(cipher_suite);
307*8edacedfSDaniel Fojt 	if (cipher == NULL || !ssl_cipher_in_list(SSL_get_ciphers(s), cipher)) {
308*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
309cca6fc52SDaniel Fojt 		goto err;
310cca6fc52SDaniel Fojt 	}
311cca6fc52SDaniel Fojt 	if (ctx->hs->server_version == TLS1_3_VERSION &&
312cca6fc52SDaniel Fojt 	    cipher->algorithm_ssl != SSL_TLSV1_3) {
313*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
314cca6fc52SDaniel Fojt 		goto err;
315cca6fc52SDaniel Fojt 	}
316cca6fc52SDaniel Fojt 	/* XXX - move this to hs_tls13? */
317cca6fc52SDaniel Fojt 	S3I(s)->hs.new_cipher = cipher;
318cca6fc52SDaniel Fojt 
319cca6fc52SDaniel Fojt 	if (compression_method != 0) {
320*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
321cca6fc52SDaniel Fojt 		goto err;
322cca6fc52SDaniel Fojt 	}
323cca6fc52SDaniel Fojt 
324cca6fc52SDaniel Fojt 	return 1;
325cca6fc52SDaniel Fojt 
326cca6fc52SDaniel Fojt  err:
327cca6fc52SDaniel Fojt 	if (ctx->alert == 0)
328*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECODE_ERROR;
329cca6fc52SDaniel Fojt 
330cca6fc52SDaniel Fojt 	return 0;
331cca6fc52SDaniel Fojt }
332cca6fc52SDaniel Fojt 
333cca6fc52SDaniel Fojt static int
334cca6fc52SDaniel Fojt tls13_client_engage_record_protection(struct tls13_ctx *ctx)
335cca6fc52SDaniel Fojt {
336cca6fc52SDaniel Fojt 	struct tls13_secrets *secrets;
337cca6fc52SDaniel Fojt 	struct tls13_secret context;
338cca6fc52SDaniel Fojt 	unsigned char buf[EVP_MAX_MD_SIZE];
339cca6fc52SDaniel Fojt 	uint8_t *shared_key = NULL;
340cca6fc52SDaniel Fojt 	size_t shared_key_len = 0;
341cca6fc52SDaniel Fojt 	size_t hash_len;
342cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
343cca6fc52SDaniel Fojt 	int ret = 0;
344cca6fc52SDaniel Fojt 
345cca6fc52SDaniel Fojt 	/* Derive the shared key and engage record protection. */
346cca6fc52SDaniel Fojt 
347cca6fc52SDaniel Fojt 	if (!tls13_key_share_derive(ctx->hs->key_share, &shared_key,
348cca6fc52SDaniel Fojt 	    &shared_key_len))
349cca6fc52SDaniel Fojt 		goto err;
350cca6fc52SDaniel Fojt 
351cca6fc52SDaniel Fojt 	s->session->cipher = S3I(s)->hs.new_cipher;
352cca6fc52SDaniel Fojt 	s->session->ssl_version = ctx->hs->server_version;
353cca6fc52SDaniel Fojt 
354cca6fc52SDaniel Fojt 	if ((ctx->aead = tls13_cipher_aead(S3I(s)->hs.new_cipher)) == NULL)
355cca6fc52SDaniel Fojt 		goto err;
356cca6fc52SDaniel Fojt 	if ((ctx->hash = tls13_cipher_hash(S3I(s)->hs.new_cipher)) == NULL)
357cca6fc52SDaniel Fojt 		goto err;
358cca6fc52SDaniel Fojt 
359cca6fc52SDaniel Fojt 	if ((secrets = tls13_secrets_create(ctx->hash, 0)) == NULL)
360cca6fc52SDaniel Fojt 		goto err;
361cca6fc52SDaniel Fojt 	ctx->hs->secrets = secrets;
362cca6fc52SDaniel Fojt 
363cca6fc52SDaniel Fojt 	/* XXX - pass in hash. */
364cca6fc52SDaniel Fojt 	if (!tls1_transcript_hash_init(s))
365cca6fc52SDaniel Fojt 		goto err;
366cca6fc52SDaniel Fojt 	tls1_transcript_free(s);
367cca6fc52SDaniel Fojt 	if (!tls1_transcript_hash_value(s, buf, sizeof(buf), &hash_len))
368cca6fc52SDaniel Fojt 		goto err;
369cca6fc52SDaniel Fojt 	context.data = buf;
370cca6fc52SDaniel Fojt 	context.len = hash_len;
371cca6fc52SDaniel Fojt 
372cca6fc52SDaniel Fojt 	/* Early secrets. */
373cca6fc52SDaniel Fojt 	if (!tls13_derive_early_secrets(secrets, secrets->zeros.data,
374cca6fc52SDaniel Fojt 	    secrets->zeros.len, &context))
375cca6fc52SDaniel Fojt 		goto err;
376cca6fc52SDaniel Fojt 
377cca6fc52SDaniel Fojt 	/* Handshake secrets. */
378cca6fc52SDaniel Fojt 	if (!tls13_derive_handshake_secrets(ctx->hs->secrets, shared_key,
379cca6fc52SDaniel Fojt 	    shared_key_len, &context))
380cca6fc52SDaniel Fojt 		goto err;
381cca6fc52SDaniel Fojt 
382cca6fc52SDaniel Fojt 	tls13_record_layer_set_aead(ctx->rl, ctx->aead);
383cca6fc52SDaniel Fojt 	tls13_record_layer_set_hash(ctx->rl, ctx->hash);
384cca6fc52SDaniel Fojt 
385cca6fc52SDaniel Fojt 	if (!tls13_record_layer_set_read_traffic_key(ctx->rl,
386cca6fc52SDaniel Fojt 	    &secrets->server_handshake_traffic))
387cca6fc52SDaniel Fojt 		goto err;
388cca6fc52SDaniel Fojt 	if (!tls13_record_layer_set_write_traffic_key(ctx->rl,
389cca6fc52SDaniel Fojt 	    &secrets->client_handshake_traffic))
390cca6fc52SDaniel Fojt 		goto err;
391cca6fc52SDaniel Fojt 
392cca6fc52SDaniel Fojt 	ret = 1;
393cca6fc52SDaniel Fojt 
394cca6fc52SDaniel Fojt  err:
395cca6fc52SDaniel Fojt 	freezero(shared_key, shared_key_len);
396cca6fc52SDaniel Fojt 
397cca6fc52SDaniel Fojt 	return ret;
398cca6fc52SDaniel Fojt }
399cca6fc52SDaniel Fojt 
400cca6fc52SDaniel Fojt int
401cca6fc52SDaniel Fojt tls13_server_hello_retry_request_recv(struct tls13_ctx *ctx, CBS *cbs)
402cca6fc52SDaniel Fojt {
403cca6fc52SDaniel Fojt 	/*
404cca6fc52SDaniel Fojt 	 * The state machine has no way of knowing if we're going to receive a
405cca6fc52SDaniel Fojt 	 * HelloRetryRequest or a ServerHello. As such, we have to handle
406cca6fc52SDaniel Fojt 	 * this case here and hand off to the appropriate function.
407cca6fc52SDaniel Fojt 	 */
408cca6fc52SDaniel Fojt 	if (!tls13_server_hello_is_retry(cbs)) {
409cca6fc52SDaniel Fojt 		ctx->handshake_stage.hs_type |= WITHOUT_HRR;
410cca6fc52SDaniel Fojt 		return tls13_server_hello_recv(ctx, cbs);
411cca6fc52SDaniel Fojt 	}
412cca6fc52SDaniel Fojt 
413cca6fc52SDaniel Fojt 	if (!tls13_server_hello_process(ctx, cbs))
414cca6fc52SDaniel Fojt 		return 0;
415cca6fc52SDaniel Fojt 
416cca6fc52SDaniel Fojt 	/*
417cca6fc52SDaniel Fojt 	 * This may have been a TLSv1.2 or earlier ServerHello that just happened
418cca6fc52SDaniel Fojt 	 * to have matching server random...
419cca6fc52SDaniel Fojt 	 */
420cca6fc52SDaniel Fojt 	if (ctx->hs->use_legacy)
421cca6fc52SDaniel Fojt 		return tls13_use_legacy_client(ctx);
422cca6fc52SDaniel Fojt 
423cca6fc52SDaniel Fojt 	if (!ctx->hs->hrr)
424cca6fc52SDaniel Fojt 		return 0;
425cca6fc52SDaniel Fojt 
426cca6fc52SDaniel Fojt 	if (!tls13_synthetic_handshake_message(ctx))
427cca6fc52SDaniel Fojt 		return 0;
428cca6fc52SDaniel Fojt 	if (!tls13_handshake_msg_record(ctx))
429cca6fc52SDaniel Fojt 		return 0;
430cca6fc52SDaniel Fojt 
431cca6fc52SDaniel Fojt 	ctx->hs->hrr = 0;
432cca6fc52SDaniel Fojt 
433cca6fc52SDaniel Fojt 	return 1;
434cca6fc52SDaniel Fojt }
435cca6fc52SDaniel Fojt 
436cca6fc52SDaniel Fojt int
437cca6fc52SDaniel Fojt tls13_client_hello_retry_send(struct tls13_ctx *ctx, CBB *cbb)
438cca6fc52SDaniel Fojt {
439cca6fc52SDaniel Fojt 	/*
440cca6fc52SDaniel Fojt 	 * Ensure that the server supported group is one that we listed in our
441cca6fc52SDaniel Fojt 	 * supported groups and is not the same as the key share we previously
442cca6fc52SDaniel Fojt 	 * offered.
443cca6fc52SDaniel Fojt 	 */
444cca6fc52SDaniel Fojt 	if (!tls1_check_curve(ctx->ssl, ctx->hs->server_group))
445cca6fc52SDaniel Fojt 		return 0; /* XXX alert */
446cca6fc52SDaniel Fojt 	if (ctx->hs->server_group == tls13_key_share_group(ctx->hs->key_share))
447cca6fc52SDaniel Fojt 		return 0; /* XXX alert */
448cca6fc52SDaniel Fojt 
449cca6fc52SDaniel Fojt 	/* Switch to new key share. */
450cca6fc52SDaniel Fojt 	tls13_key_share_free(ctx->hs->key_share);
451cca6fc52SDaniel Fojt 	if ((ctx->hs->key_share =
452cca6fc52SDaniel Fojt 	    tls13_key_share_new(ctx->hs->server_group)) == NULL)
453cca6fc52SDaniel Fojt 		return 0;
454cca6fc52SDaniel Fojt 	if (!tls13_key_share_generate(ctx->hs->key_share))
455cca6fc52SDaniel Fojt 		return 0;
456cca6fc52SDaniel Fojt 
457cca6fc52SDaniel Fojt 	if (!tls13_client_hello_build(ctx, cbb))
458cca6fc52SDaniel Fojt 		return 0;
459cca6fc52SDaniel Fojt 
460cca6fc52SDaniel Fojt 	return 1;
461cca6fc52SDaniel Fojt }
462cca6fc52SDaniel Fojt 
463cca6fc52SDaniel Fojt int
464cca6fc52SDaniel Fojt tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs)
465cca6fc52SDaniel Fojt {
466cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
467cca6fc52SDaniel Fojt 
468cca6fc52SDaniel Fojt 	/*
469cca6fc52SDaniel Fojt 	 * We may have received a legacy (pre-TLSv1.3) ServerHello or a TLSv1.3
470cca6fc52SDaniel Fojt 	 * ServerHello. HelloRetryRequests have already been handled.
471cca6fc52SDaniel Fojt 	 */
472cca6fc52SDaniel Fojt 	if (!tls13_server_hello_process(ctx, cbs))
473cca6fc52SDaniel Fojt 		return 0;
474cca6fc52SDaniel Fojt 
475cca6fc52SDaniel Fojt 	if (ctx->handshake_stage.hs_type & WITHOUT_HRR) {
476cca6fc52SDaniel Fojt 		tls1_transcript_unfreeze(s);
477cca6fc52SDaniel Fojt 		if (!tls13_handshake_msg_record(ctx))
478cca6fc52SDaniel Fojt 			return 0;
479cca6fc52SDaniel Fojt 	}
480cca6fc52SDaniel Fojt 
481cca6fc52SDaniel Fojt 	if (ctx->hs->use_legacy) {
482cca6fc52SDaniel Fojt 		if (!(ctx->handshake_stage.hs_type & WITHOUT_HRR))
483cca6fc52SDaniel Fojt 			return 0;
484cca6fc52SDaniel Fojt 		return tls13_use_legacy_client(ctx);
485cca6fc52SDaniel Fojt 	}
486cca6fc52SDaniel Fojt 
487cca6fc52SDaniel Fojt 	if (ctx->hs->hrr) {
488cca6fc52SDaniel Fojt 		/* The server has sent two HelloRetryRequests. */
489*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
490cca6fc52SDaniel Fojt 		return 0;
491cca6fc52SDaniel Fojt 	}
492cca6fc52SDaniel Fojt 
493cca6fc52SDaniel Fojt 	if (!tls13_client_engage_record_protection(ctx))
494cca6fc52SDaniel Fojt 		return 0;
495cca6fc52SDaniel Fojt 
496cca6fc52SDaniel Fojt 	ctx->handshake_stage.hs_type |= NEGOTIATED;
497cca6fc52SDaniel Fojt 
498cca6fc52SDaniel Fojt 	return 1;
499cca6fc52SDaniel Fojt }
500cca6fc52SDaniel Fojt 
501cca6fc52SDaniel Fojt int
502cca6fc52SDaniel Fojt tls13_server_encrypted_extensions_recv(struct tls13_ctx *ctx, CBS *cbs)
503cca6fc52SDaniel Fojt {
504cca6fc52SDaniel Fojt 	int alert_desc;
505cca6fc52SDaniel Fojt 
506*8edacedfSDaniel Fojt 	if (!tlsext_client_parse(ctx->ssl, SSL_TLSEXT_MSG_EE, cbs, &alert_desc)) {
507cca6fc52SDaniel Fojt 		ctx->alert = alert_desc;
508cca6fc52SDaniel Fojt 		goto err;
509cca6fc52SDaniel Fojt 	}
510cca6fc52SDaniel Fojt 
511cca6fc52SDaniel Fojt 	return 1;
512cca6fc52SDaniel Fojt 
513cca6fc52SDaniel Fojt  err:
514cca6fc52SDaniel Fojt 	if (ctx->alert == 0)
515*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECODE_ERROR;
516cca6fc52SDaniel Fojt 
517cca6fc52SDaniel Fojt 	return 0;
518cca6fc52SDaniel Fojt }
519cca6fc52SDaniel Fojt 
520cca6fc52SDaniel Fojt int
521cca6fc52SDaniel Fojt tls13_server_certificate_request_recv(struct tls13_ctx *ctx, CBS *cbs)
522cca6fc52SDaniel Fojt {
523cca6fc52SDaniel Fojt 	CBS cert_request_context;
524cca6fc52SDaniel Fojt 	int alert_desc;
525cca6fc52SDaniel Fojt 
526cca6fc52SDaniel Fojt 	/*
527cca6fc52SDaniel Fojt 	 * Thanks to poor state design in the RFC, this function can be called
528cca6fc52SDaniel Fojt 	 * when we actually have a certificate message instead of a certificate
529cca6fc52SDaniel Fojt 	 * request... in that case we call the certificate handler after
530cca6fc52SDaniel Fojt 	 * switching state, to avoid advancing state.
531cca6fc52SDaniel Fojt 	 */
532cca6fc52SDaniel Fojt 	if (tls13_handshake_msg_type(ctx->hs_msg) == TLS13_MT_CERTIFICATE) {
533cca6fc52SDaniel Fojt 		ctx->handshake_stage.hs_type |= WITHOUT_CR;
534cca6fc52SDaniel Fojt 		return tls13_server_certificate_recv(ctx, cbs);
535cca6fc52SDaniel Fojt 	}
536cca6fc52SDaniel Fojt 
537cca6fc52SDaniel Fojt 	if (!CBS_get_u8_length_prefixed(cbs, &cert_request_context))
538cca6fc52SDaniel Fojt 		goto err;
539cca6fc52SDaniel Fojt 	if (CBS_len(&cert_request_context) != 0)
540cca6fc52SDaniel Fojt 		goto err;
541cca6fc52SDaniel Fojt 
542*8edacedfSDaniel Fojt 	if (!tlsext_client_parse(ctx->ssl, SSL_TLSEXT_MSG_CR, cbs, &alert_desc)) {
543cca6fc52SDaniel Fojt 		ctx->alert = alert_desc;
544cca6fc52SDaniel Fojt 		goto err;
545cca6fc52SDaniel Fojt 	}
546cca6fc52SDaniel Fojt 
547cca6fc52SDaniel Fojt 	return 1;
548cca6fc52SDaniel Fojt 
549cca6fc52SDaniel Fojt  err:
550cca6fc52SDaniel Fojt 	if (ctx->alert == 0)
551*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECODE_ERROR;
552*8edacedfSDaniel Fojt 
553cca6fc52SDaniel Fojt 	return 0;
554cca6fc52SDaniel Fojt }
555cca6fc52SDaniel Fojt 
556cca6fc52SDaniel Fojt int
557cca6fc52SDaniel Fojt tls13_server_certificate_recv(struct tls13_ctx *ctx, CBS *cbs)
558cca6fc52SDaniel Fojt {
559*8edacedfSDaniel Fojt 	CBS cert_request_context, cert_list, cert_data;
560cca6fc52SDaniel Fojt 	struct stack_st_X509 *certs = NULL;
561cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
562cca6fc52SDaniel Fojt 	X509 *cert = NULL;
563cca6fc52SDaniel Fojt 	EVP_PKEY *pkey;
564cca6fc52SDaniel Fojt 	const uint8_t *p;
565*8edacedfSDaniel Fojt 	int cert_idx, alert_desc;
566cca6fc52SDaniel Fojt 	int ret = 0;
567cca6fc52SDaniel Fojt 
568cca6fc52SDaniel Fojt 	if ((certs = sk_X509_new_null()) == NULL)
569cca6fc52SDaniel Fojt 		goto err;
570cca6fc52SDaniel Fojt 
571cca6fc52SDaniel Fojt 	if (!CBS_get_u8_length_prefixed(cbs, &cert_request_context))
572cca6fc52SDaniel Fojt 		goto err;
573cca6fc52SDaniel Fojt 	if (CBS_len(&cert_request_context) != 0)
574cca6fc52SDaniel Fojt 		goto err;
575cca6fc52SDaniel Fojt 	if (!CBS_get_u24_length_prefixed(cbs, &cert_list))
576cca6fc52SDaniel Fojt 		goto err;
577cca6fc52SDaniel Fojt 
578cca6fc52SDaniel Fojt 	while (CBS_len(&cert_list) > 0) {
579cca6fc52SDaniel Fojt 		if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data))
580cca6fc52SDaniel Fojt 			goto err;
581*8edacedfSDaniel Fojt 
582*8edacedfSDaniel Fojt 		if (!tlsext_client_parse(ctx->ssl, SSL_TLSEXT_MSG_CT,
583*8edacedfSDaniel Fojt 		    &cert_list, &alert_desc)) {
584*8edacedfSDaniel Fojt 			ctx->alert = alert_desc;
585cca6fc52SDaniel Fojt 			goto err;
586*8edacedfSDaniel Fojt 		}
587cca6fc52SDaniel Fojt 
588cca6fc52SDaniel Fojt 		p = CBS_data(&cert_data);
589cca6fc52SDaniel Fojt 		if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL)
590cca6fc52SDaniel Fojt 			goto err;
591cca6fc52SDaniel Fojt 		if (p != CBS_data(&cert_data) + CBS_len(&cert_data))
592cca6fc52SDaniel Fojt 			goto err;
593cca6fc52SDaniel Fojt 
594cca6fc52SDaniel Fojt 		if (!sk_X509_push(certs, cert))
595cca6fc52SDaniel Fojt 			goto err;
596cca6fc52SDaniel Fojt 
597cca6fc52SDaniel Fojt 		cert = NULL;
598cca6fc52SDaniel Fojt 	}
599cca6fc52SDaniel Fojt 
600cca6fc52SDaniel Fojt 	/* A server must always provide a non-empty certificate list. */
601cca6fc52SDaniel Fojt 	if (sk_X509_num(certs) < 1) {
602*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECODE_ERROR;
603cca6fc52SDaniel Fojt 		tls13_set_errorx(ctx, TLS13_ERR_NO_PEER_CERTIFICATE, 0,
604cca6fc52SDaniel Fojt 		    "peer failed to provide a certificate", NULL);
605cca6fc52SDaniel Fojt 		goto err;
606cca6fc52SDaniel Fojt 	}
607cca6fc52SDaniel Fojt 
608cca6fc52SDaniel Fojt 	/*
609cca6fc52SDaniel Fojt 	 * At this stage we still have no proof of possession. As such, it would
610cca6fc52SDaniel Fojt 	 * be preferable to keep the chain and verify once we have successfully
611cca6fc52SDaniel Fojt 	 * processed the CertificateVerify message.
612cca6fc52SDaniel Fojt 	 */
613cca6fc52SDaniel Fojt 	if (ssl_verify_cert_chain(s, certs) <= 0 &&
614cca6fc52SDaniel Fojt 	    s->verify_mode != SSL_VERIFY_NONE) {
615cca6fc52SDaniel Fojt 		ctx->alert = ssl_verify_alarm_type(s->verify_result);
616cca6fc52SDaniel Fojt 		tls13_set_errorx(ctx, TLS13_ERR_VERIFY_FAILED, 0,
617cca6fc52SDaniel Fojt 		    "failed to verify peer certificate", NULL);
618cca6fc52SDaniel Fojt 		goto err;
619cca6fc52SDaniel Fojt 	}
620cca6fc52SDaniel Fojt 	ERR_clear_error();
621cca6fc52SDaniel Fojt 
622cca6fc52SDaniel Fojt 	cert = sk_X509_value(certs, 0);
623cca6fc52SDaniel Fojt 	X509_up_ref(cert);
624cca6fc52SDaniel Fojt 
625cca6fc52SDaniel Fojt 	if ((pkey = X509_get0_pubkey(cert)) == NULL)
626cca6fc52SDaniel Fojt 		goto err;
627cca6fc52SDaniel Fojt 	if (EVP_PKEY_missing_parameters(pkey))
628cca6fc52SDaniel Fojt 		goto err;
629cca6fc52SDaniel Fojt 	if ((cert_idx = ssl_cert_type(cert, pkey)) < 0)
630cca6fc52SDaniel Fojt 		goto err;
631cca6fc52SDaniel Fojt 
632cca6fc52SDaniel Fojt 	ssl_sess_cert_free(SSI(s)->sess_cert);
633cca6fc52SDaniel Fojt 	if ((SSI(s)->sess_cert = ssl_sess_cert_new()) == NULL)
634cca6fc52SDaniel Fojt 		goto err;
635cca6fc52SDaniel Fojt 
636cca6fc52SDaniel Fojt 	SSI(s)->sess_cert->cert_chain = certs;
637cca6fc52SDaniel Fojt 	certs = NULL;
638cca6fc52SDaniel Fojt 
639cca6fc52SDaniel Fojt 	X509_up_ref(cert);
640cca6fc52SDaniel Fojt 	SSI(s)->sess_cert->peer_pkeys[cert_idx].x509 = cert;
641cca6fc52SDaniel Fojt 	SSI(s)->sess_cert->peer_key = &(SSI(s)->sess_cert->peer_pkeys[cert_idx]);
642cca6fc52SDaniel Fojt 
643cca6fc52SDaniel Fojt 	X509_free(s->session->peer);
644cca6fc52SDaniel Fojt 
645cca6fc52SDaniel Fojt 	X509_up_ref(cert);
646cca6fc52SDaniel Fojt 	s->session->peer = cert;
647cca6fc52SDaniel Fojt 	s->session->verify_result = s->verify_result;
648cca6fc52SDaniel Fojt 
649*8edacedfSDaniel Fojt 	if (ctx->ocsp_status_recv_cb != NULL &&
650*8edacedfSDaniel Fojt 	    !ctx->ocsp_status_recv_cb(ctx))
651*8edacedfSDaniel Fojt 		goto err;
652*8edacedfSDaniel Fojt 
653cca6fc52SDaniel Fojt 	ret = 1;
654cca6fc52SDaniel Fojt 
655cca6fc52SDaniel Fojt  err:
656cca6fc52SDaniel Fojt 	sk_X509_pop_free(certs, X509_free);
657cca6fc52SDaniel Fojt 	X509_free(cert);
658cca6fc52SDaniel Fojt 
659cca6fc52SDaniel Fojt 	return ret;
660cca6fc52SDaniel Fojt }
661cca6fc52SDaniel Fojt 
662cca6fc52SDaniel Fojt int
663cca6fc52SDaniel Fojt tls13_server_certificate_verify_recv(struct tls13_ctx *ctx, CBS *cbs)
664cca6fc52SDaniel Fojt {
665cca6fc52SDaniel Fojt 	const struct ssl_sigalg *sigalg;
666cca6fc52SDaniel Fojt 	uint16_t signature_scheme;
667cca6fc52SDaniel Fojt 	uint8_t *sig_content = NULL;
668cca6fc52SDaniel Fojt 	size_t sig_content_len;
669cca6fc52SDaniel Fojt 	EVP_MD_CTX *mdctx = NULL;
670cca6fc52SDaniel Fojt 	EVP_PKEY_CTX *pctx;
671cca6fc52SDaniel Fojt 	EVP_PKEY *pkey;
672cca6fc52SDaniel Fojt 	X509 *cert;
673cca6fc52SDaniel Fojt 	CBS signature;
674cca6fc52SDaniel Fojt 	CBB cbb;
675cca6fc52SDaniel Fojt 	int ret = 0;
676cca6fc52SDaniel Fojt 
677cca6fc52SDaniel Fojt 	memset(&cbb, 0, sizeof(cbb));
678cca6fc52SDaniel Fojt 
679cca6fc52SDaniel Fojt 	if (!CBS_get_u16(cbs, &signature_scheme))
680cca6fc52SDaniel Fojt 		goto err;
681cca6fc52SDaniel Fojt 	if (!CBS_get_u16_length_prefixed(cbs, &signature))
682cca6fc52SDaniel Fojt 		goto err;
683cca6fc52SDaniel Fojt 
684cca6fc52SDaniel Fojt 	if ((sigalg = ssl_sigalg(signature_scheme, tls13_sigalgs,
685cca6fc52SDaniel Fojt 	    tls13_sigalgs_len)) == NULL)
686cca6fc52SDaniel Fojt 		goto err;
687cca6fc52SDaniel Fojt 
688cca6fc52SDaniel Fojt 	if (!CBB_init(&cbb, 0))
689cca6fc52SDaniel Fojt 		goto err;
690cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&cbb, tls13_cert_verify_pad,
691cca6fc52SDaniel Fojt 	    sizeof(tls13_cert_verify_pad)))
692cca6fc52SDaniel Fojt 		goto err;
693cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&cbb, tls13_cert_server_verify_context,
694cca6fc52SDaniel Fojt 	    strlen(tls13_cert_server_verify_context)))
695cca6fc52SDaniel Fojt 		goto err;
696cca6fc52SDaniel Fojt 	if (!CBB_add_u8(&cbb, 0))
697cca6fc52SDaniel Fojt 		goto err;
698cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&cbb, ctx->hs->transcript_hash,
699cca6fc52SDaniel Fojt 	    ctx->hs->transcript_hash_len))
700cca6fc52SDaniel Fojt 		goto err;
701cca6fc52SDaniel Fojt 	if (!CBB_finish(&cbb, &sig_content, &sig_content_len))
702cca6fc52SDaniel Fojt 		goto err;
703cca6fc52SDaniel Fojt 
704cca6fc52SDaniel Fojt 	if ((cert = ctx->ssl->session->peer) == NULL)
705cca6fc52SDaniel Fojt 		goto err;
706cca6fc52SDaniel Fojt 	if ((pkey = X509_get0_pubkey(cert)) == NULL)
707cca6fc52SDaniel Fojt 		goto err;
708cca6fc52SDaniel Fojt 	if (!ssl_sigalg_pkey_ok(sigalg, pkey, 1))
709cca6fc52SDaniel Fojt 		goto err;
710cca6fc52SDaniel Fojt 
711cca6fc52SDaniel Fojt 	if (CBS_len(&signature) > EVP_PKEY_size(pkey))
712cca6fc52SDaniel Fojt 		goto err;
713cca6fc52SDaniel Fojt 
714cca6fc52SDaniel Fojt 	if ((mdctx = EVP_MD_CTX_new()) == NULL)
715cca6fc52SDaniel Fojt 		goto err;
716cca6fc52SDaniel Fojt 	if (!EVP_DigestVerifyInit(mdctx, &pctx, sigalg->md(), NULL, pkey))
717cca6fc52SDaniel Fojt 		goto err;
718cca6fc52SDaniel Fojt 	if (sigalg->flags & SIGALG_FLAG_RSA_PSS) {
719cca6fc52SDaniel Fojt 		if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING))
720cca6fc52SDaniel Fojt 			goto err;
721cca6fc52SDaniel Fojt 		if (!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))
722cca6fc52SDaniel Fojt 			goto err;
723cca6fc52SDaniel Fojt 	}
724cca6fc52SDaniel Fojt 	if (!EVP_DigestVerifyUpdate(mdctx, sig_content, sig_content_len)) {
725*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECRYPT_ERROR;
726cca6fc52SDaniel Fojt 		goto err;
727cca6fc52SDaniel Fojt 	}
728cca6fc52SDaniel Fojt 	if (EVP_DigestVerifyFinal(mdctx, CBS_data(&signature),
729cca6fc52SDaniel Fojt 	    CBS_len(&signature)) <= 0) {
730*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECRYPT_ERROR;
731cca6fc52SDaniel Fojt 		goto err;
732cca6fc52SDaniel Fojt 	}
733cca6fc52SDaniel Fojt 
734cca6fc52SDaniel Fojt 	ret = 1;
735cca6fc52SDaniel Fojt 
736cca6fc52SDaniel Fojt  err:
737cca6fc52SDaniel Fojt 	if (!ret && ctx->alert == 0)
738*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECODE_ERROR;
739cca6fc52SDaniel Fojt 	CBB_cleanup(&cbb);
740cca6fc52SDaniel Fojt 	EVP_MD_CTX_free(mdctx);
741cca6fc52SDaniel Fojt 	free(sig_content);
742cca6fc52SDaniel Fojt 
743cca6fc52SDaniel Fojt 	return ret;
744cca6fc52SDaniel Fojt }
745cca6fc52SDaniel Fojt 
746cca6fc52SDaniel Fojt int
747cca6fc52SDaniel Fojt tls13_server_finished_recv(struct tls13_ctx *ctx, CBS *cbs)
748cca6fc52SDaniel Fojt {
749cca6fc52SDaniel Fojt 	struct tls13_secrets *secrets = ctx->hs->secrets;
750cca6fc52SDaniel Fojt 	struct tls13_secret context = { .data = "", .len = 0 };
751cca6fc52SDaniel Fojt 	struct tls13_secret finished_key;
752cca6fc52SDaniel Fojt 	uint8_t transcript_hash[EVP_MAX_MD_SIZE];
753cca6fc52SDaniel Fojt 	size_t transcript_hash_len;
754cca6fc52SDaniel Fojt 	uint8_t *verify_data = NULL;
755cca6fc52SDaniel Fojt 	size_t verify_data_len;
756cca6fc52SDaniel Fojt 	uint8_t key[EVP_MAX_MD_SIZE];
757cca6fc52SDaniel Fojt 	HMAC_CTX *hmac_ctx = NULL;
758cca6fc52SDaniel Fojt 	unsigned int hlen;
759cca6fc52SDaniel Fojt 	int ret = 0;
760cca6fc52SDaniel Fojt 
761cca6fc52SDaniel Fojt 	/*
762cca6fc52SDaniel Fojt 	 * Verify server finished.
763cca6fc52SDaniel Fojt 	 */
764cca6fc52SDaniel Fojt 	finished_key.data = key;
765cca6fc52SDaniel Fojt 	finished_key.len = EVP_MD_size(ctx->hash);
766cca6fc52SDaniel Fojt 
767cca6fc52SDaniel Fojt 	if (!tls13_hkdf_expand_label(&finished_key, ctx->hash,
768cca6fc52SDaniel Fojt 	    &secrets->server_handshake_traffic, "finished",
769cca6fc52SDaniel Fojt 	    &context))
770cca6fc52SDaniel Fojt 		goto err;
771cca6fc52SDaniel Fojt 
772cca6fc52SDaniel Fojt 	if ((hmac_ctx = HMAC_CTX_new()) == NULL)
773cca6fc52SDaniel Fojt 		goto err;
774cca6fc52SDaniel Fojt 	if (!HMAC_Init_ex(hmac_ctx, finished_key.data, finished_key.len,
775cca6fc52SDaniel Fojt 	    ctx->hash, NULL))
776cca6fc52SDaniel Fojt 		goto err;
777cca6fc52SDaniel Fojt 	if (!HMAC_Update(hmac_ctx, ctx->hs->transcript_hash,
778cca6fc52SDaniel Fojt 	    ctx->hs->transcript_hash_len))
779cca6fc52SDaniel Fojt 		goto err;
780cca6fc52SDaniel Fojt 	verify_data_len = HMAC_size(hmac_ctx);
781cca6fc52SDaniel Fojt 	if ((verify_data = calloc(1, verify_data_len)) == NULL)
782cca6fc52SDaniel Fojt 		goto err;
783cca6fc52SDaniel Fojt 	if (!HMAC_Final(hmac_ctx, verify_data, &hlen))
784cca6fc52SDaniel Fojt 		goto err;
785cca6fc52SDaniel Fojt 	if (hlen != verify_data_len)
786cca6fc52SDaniel Fojt 		goto err;
787cca6fc52SDaniel Fojt 
788cca6fc52SDaniel Fojt 	if (!CBS_mem_equal(cbs, verify_data, verify_data_len)) {
789*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_DECRYPT_ERROR;
790cca6fc52SDaniel Fojt 		goto err;
791cca6fc52SDaniel Fojt 	}
792cca6fc52SDaniel Fojt 
793cca6fc52SDaniel Fojt 	if (!CBS_skip(cbs, verify_data_len))
794cca6fc52SDaniel Fojt 		goto err;
795cca6fc52SDaniel Fojt 
796cca6fc52SDaniel Fojt 	/*
797cca6fc52SDaniel Fojt 	 * Derive application traffic keys.
798cca6fc52SDaniel Fojt 	 */
799cca6fc52SDaniel Fojt 	if (!tls1_transcript_hash_value(ctx->ssl, transcript_hash,
800cca6fc52SDaniel Fojt 	    sizeof(transcript_hash), &transcript_hash_len))
801cca6fc52SDaniel Fojt 		goto err;
802cca6fc52SDaniel Fojt 
803cca6fc52SDaniel Fojt 	context.data = transcript_hash;
804cca6fc52SDaniel Fojt 	context.len = transcript_hash_len;
805cca6fc52SDaniel Fojt 
806cca6fc52SDaniel Fojt 	if (!tls13_derive_application_secrets(secrets, &context))
807cca6fc52SDaniel Fojt 		goto err;
808cca6fc52SDaniel Fojt 
809cca6fc52SDaniel Fojt 	/*
810cca6fc52SDaniel Fojt 	 * Any records following the server finished message must be encrypted
811cca6fc52SDaniel Fojt 	 * using the server application traffic keys.
812cca6fc52SDaniel Fojt 	 */
813cca6fc52SDaniel Fojt 	if (!tls13_record_layer_set_read_traffic_key(ctx->rl,
814cca6fc52SDaniel Fojt 	    &secrets->server_application_traffic))
815cca6fc52SDaniel Fojt 		goto err;
816cca6fc52SDaniel Fojt 
817cca6fc52SDaniel Fojt 	tls13_record_layer_allow_ccs(ctx->rl, 0);
818cca6fc52SDaniel Fojt 
819cca6fc52SDaniel Fojt 	ret = 1;
820cca6fc52SDaniel Fojt 
821cca6fc52SDaniel Fojt  err:
822cca6fc52SDaniel Fojt 	HMAC_CTX_free(hmac_ctx);
823cca6fc52SDaniel Fojt 	free(verify_data);
824cca6fc52SDaniel Fojt 
825cca6fc52SDaniel Fojt 	return ret;
826cca6fc52SDaniel Fojt }
827cca6fc52SDaniel Fojt 
828f015dc58SDaniel Fojt static int
829f015dc58SDaniel Fojt tls13_client_check_certificate(struct tls13_ctx *ctx, CERT_PKEY *cpk,
830f015dc58SDaniel Fojt     int *ok, const struct ssl_sigalg **out_sigalg)
831f015dc58SDaniel Fojt {
832f015dc58SDaniel Fojt 	const struct ssl_sigalg *sigalg;
833f015dc58SDaniel Fojt 	SSL *s = ctx->ssl;
834f015dc58SDaniel Fojt 
835f015dc58SDaniel Fojt 	*ok = 0;
836f015dc58SDaniel Fojt 	*out_sigalg = NULL;
837f015dc58SDaniel Fojt 
838f015dc58SDaniel Fojt 	if (cpk->x509 == NULL || cpk->privatekey == NULL)
839f015dc58SDaniel Fojt 		goto done;
840f015dc58SDaniel Fojt 
841f015dc58SDaniel Fojt 	if ((sigalg = ssl_sigalg_select(s, cpk->privatekey)) == NULL)
842f015dc58SDaniel Fojt 		goto done;
843f015dc58SDaniel Fojt 
844f015dc58SDaniel Fojt 	*ok = 1;
845f015dc58SDaniel Fojt 	*out_sigalg = sigalg;
846f015dc58SDaniel Fojt 
847f015dc58SDaniel Fojt  done:
848f015dc58SDaniel Fojt 	return 1;
849f015dc58SDaniel Fojt }
850f015dc58SDaniel Fojt 
851f015dc58SDaniel Fojt static int
852f015dc58SDaniel Fojt tls13_client_select_certificate(struct tls13_ctx *ctx, CERT_PKEY **out_cpk,
853f015dc58SDaniel Fojt     const struct ssl_sigalg **out_sigalg)
854f015dc58SDaniel Fojt {
855f015dc58SDaniel Fojt 	SSL *s = ctx->ssl;
856f015dc58SDaniel Fojt 	const struct ssl_sigalg *sigalg;
857f015dc58SDaniel Fojt 	CERT_PKEY *cpk;
858f015dc58SDaniel Fojt 	int cert_ok;
859f015dc58SDaniel Fojt 
860f015dc58SDaniel Fojt 	*out_cpk = NULL;
861f015dc58SDaniel Fojt 	*out_sigalg = NULL;
862f015dc58SDaniel Fojt 
863*8edacedfSDaniel Fojt 	/*
864*8edacedfSDaniel Fojt 	 * XXX - RFC 8446, 4.4.2.3: the server can communicate preferences
865*8edacedfSDaniel Fojt 	 * with the certificate_authorities (4.2.4) and oid_filters (4.2.5)
866*8edacedfSDaniel Fojt 	 * extensions. We should honor the former and must apply the latter.
867*8edacedfSDaniel Fojt 	 */
868*8edacedfSDaniel Fojt 
869f015dc58SDaniel Fojt 	cpk = &s->cert->pkeys[SSL_PKEY_ECC];
870f015dc58SDaniel Fojt 	if (!tls13_client_check_certificate(ctx, cpk, &cert_ok, &sigalg))
871f015dc58SDaniel Fojt 		return 0;
872f015dc58SDaniel Fojt 	if (cert_ok)
873f015dc58SDaniel Fojt 		goto done;
874f015dc58SDaniel Fojt 
875*8edacedfSDaniel Fojt 	cpk = &s->cert->pkeys[SSL_PKEY_RSA];
876f015dc58SDaniel Fojt 	if (!tls13_client_check_certificate(ctx, cpk, &cert_ok, &sigalg))
877f015dc58SDaniel Fojt 		return 0;
878f015dc58SDaniel Fojt 	if (cert_ok)
879f015dc58SDaniel Fojt 		goto done;
880f015dc58SDaniel Fojt 
881f015dc58SDaniel Fojt 	cpk = NULL;
882f015dc58SDaniel Fojt 	sigalg = NULL;
883f015dc58SDaniel Fojt 
884f015dc58SDaniel Fojt  done:
885f015dc58SDaniel Fojt 	*out_cpk = cpk;
886f015dc58SDaniel Fojt 	*out_sigalg = sigalg;
887f015dc58SDaniel Fojt 
888f015dc58SDaniel Fojt 	return 1;
889f015dc58SDaniel Fojt }
890f015dc58SDaniel Fojt 
891cca6fc52SDaniel Fojt int
892cca6fc52SDaniel Fojt tls13_client_certificate_send(struct tls13_ctx *ctx, CBB *cbb)
893cca6fc52SDaniel Fojt {
894cca6fc52SDaniel Fojt 	SSL *s = ctx->ssl;
895cca6fc52SDaniel Fojt 	CBB cert_request_context, cert_list;
896f015dc58SDaniel Fojt 	const struct ssl_sigalg *sigalg;
897cca6fc52SDaniel Fojt 	STACK_OF(X509) *chain;
898cca6fc52SDaniel Fojt 	CERT_PKEY *cpk;
899cca6fc52SDaniel Fojt 	X509 *cert;
900cca6fc52SDaniel Fojt 	int i, ret = 0;
901cca6fc52SDaniel Fojt 
902f015dc58SDaniel Fojt 	if (!tls13_client_select_certificate(ctx, &cpk, &sigalg))
903f015dc58SDaniel Fojt 		goto err;
904cca6fc52SDaniel Fojt 
905f015dc58SDaniel Fojt 	ctx->hs->cpk = cpk;
906f015dc58SDaniel Fojt 	ctx->hs->sigalg = sigalg;
907cca6fc52SDaniel Fojt 
908cca6fc52SDaniel Fojt 	if (!CBB_add_u8_length_prefixed(cbb, &cert_request_context))
909cca6fc52SDaniel Fojt 		goto err;
910cca6fc52SDaniel Fojt 	if (!CBB_add_u24_length_prefixed(cbb, &cert_list))
911cca6fc52SDaniel Fojt 		goto err;
912cca6fc52SDaniel Fojt 
913f015dc58SDaniel Fojt 	/* No certificate selected. */
914f015dc58SDaniel Fojt 	if (cpk == NULL)
915cca6fc52SDaniel Fojt 		goto done;
916cca6fc52SDaniel Fojt 
917f015dc58SDaniel Fojt 	if ((chain = cpk->chain) == NULL)
918f015dc58SDaniel Fojt 	       chain = s->ctx->extra_certs;
919f015dc58SDaniel Fojt 
920*8edacedfSDaniel Fojt 	if (!tls13_cert_add(ctx, &cert_list, cpk->x509, tlsext_client_build))
921cca6fc52SDaniel Fojt 		goto err;
922cca6fc52SDaniel Fojt 
923cca6fc52SDaniel Fojt 	for (i = 0; i < sk_X509_num(chain); i++) {
924cca6fc52SDaniel Fojt 		cert = sk_X509_value(chain, i);
925*8edacedfSDaniel Fojt 		if (!tls13_cert_add(ctx, &cert_list, cert, tlsext_client_build))
926cca6fc52SDaniel Fojt 			goto err;
927cca6fc52SDaniel Fojt 	}
928cca6fc52SDaniel Fojt 
929cca6fc52SDaniel Fojt 	ctx->handshake_stage.hs_type |= WITH_CCV;
930cca6fc52SDaniel Fojt  done:
931cca6fc52SDaniel Fojt 	if (!CBB_flush(cbb))
932cca6fc52SDaniel Fojt 		goto err;
933cca6fc52SDaniel Fojt 
934cca6fc52SDaniel Fojt 	ret = 1;
935cca6fc52SDaniel Fojt 
936cca6fc52SDaniel Fojt  err:
937cca6fc52SDaniel Fojt 	return ret;
938cca6fc52SDaniel Fojt }
939cca6fc52SDaniel Fojt 
940cca6fc52SDaniel Fojt int
941cca6fc52SDaniel Fojt tls13_client_certificate_verify_send(struct tls13_ctx *ctx, CBB *cbb)
942cca6fc52SDaniel Fojt {
943f015dc58SDaniel Fojt 	const struct ssl_sigalg *sigalg;
944cca6fc52SDaniel Fojt 	uint8_t *sig = NULL, *sig_content = NULL;
945cca6fc52SDaniel Fojt 	size_t sig_len, sig_content_len;
946cca6fc52SDaniel Fojt 	EVP_MD_CTX *mdctx = NULL;
947cca6fc52SDaniel Fojt 	EVP_PKEY_CTX *pctx;
948cca6fc52SDaniel Fojt 	EVP_PKEY *pkey;
949f015dc58SDaniel Fojt 	const CERT_PKEY *cpk;
950cca6fc52SDaniel Fojt 	CBB sig_cbb;
951cca6fc52SDaniel Fojt 	int ret = 0;
952cca6fc52SDaniel Fojt 
953cca6fc52SDaniel Fojt 	memset(&sig_cbb, 0, sizeof(sig_cbb));
954cca6fc52SDaniel Fojt 
955f015dc58SDaniel Fojt 	if ((cpk = ctx->hs->cpk) == NULL)
956cca6fc52SDaniel Fojt 		goto err;
957f015dc58SDaniel Fojt 	if ((sigalg = ctx->hs->sigalg) == NULL)
958f015dc58SDaniel Fojt 		goto err;
959f015dc58SDaniel Fojt 	pkey = cpk->privatekey;
960cca6fc52SDaniel Fojt 
961cca6fc52SDaniel Fojt 	if (!CBB_init(&sig_cbb, 0))
962cca6fc52SDaniel Fojt 		goto err;
963cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&sig_cbb, tls13_cert_verify_pad,
964cca6fc52SDaniel Fojt 	    sizeof(tls13_cert_verify_pad)))
965cca6fc52SDaniel Fojt 		goto err;
966cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&sig_cbb, tls13_cert_client_verify_context,
967cca6fc52SDaniel Fojt 	    strlen(tls13_cert_client_verify_context)))
968cca6fc52SDaniel Fojt 		goto err;
969cca6fc52SDaniel Fojt 	if (!CBB_add_u8(&sig_cbb, 0))
970cca6fc52SDaniel Fojt 		goto err;
971cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&sig_cbb, ctx->hs->transcript_hash,
972cca6fc52SDaniel Fojt 	    ctx->hs->transcript_hash_len))
973cca6fc52SDaniel Fojt 		goto err;
974cca6fc52SDaniel Fojt 	if (!CBB_finish(&sig_cbb, &sig_content, &sig_content_len))
975cca6fc52SDaniel Fojt 		goto err;
976cca6fc52SDaniel Fojt 
977cca6fc52SDaniel Fojt 	if ((mdctx = EVP_MD_CTX_new()) == NULL)
978cca6fc52SDaniel Fojt 		goto err;
979cca6fc52SDaniel Fojt 	if (!EVP_DigestSignInit(mdctx, &pctx, sigalg->md(), NULL, pkey))
980cca6fc52SDaniel Fojt 		goto err;
981cca6fc52SDaniel Fojt 	if (sigalg->flags & SIGALG_FLAG_RSA_PSS) {
982cca6fc52SDaniel Fojt 		if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING))
983cca6fc52SDaniel Fojt 			goto err;
984cca6fc52SDaniel Fojt 		if (!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))
985cca6fc52SDaniel Fojt 			goto err;
986cca6fc52SDaniel Fojt 	}
987cca6fc52SDaniel Fojt 	if (!EVP_DigestSignUpdate(mdctx, sig_content, sig_content_len))
988cca6fc52SDaniel Fojt 		goto err;
989cca6fc52SDaniel Fojt 	if (EVP_DigestSignFinal(mdctx, NULL, &sig_len) <= 0)
990cca6fc52SDaniel Fojt 		goto err;
991cca6fc52SDaniel Fojt 	if ((sig = calloc(1, sig_len)) == NULL)
992cca6fc52SDaniel Fojt 		goto err;
993cca6fc52SDaniel Fojt 	if (EVP_DigestSignFinal(mdctx, sig, &sig_len) <= 0)
994cca6fc52SDaniel Fojt 		goto err;
995cca6fc52SDaniel Fojt 
996cca6fc52SDaniel Fojt 	if (!CBB_add_u16(cbb, sigalg->value))
997cca6fc52SDaniel Fojt 		goto err;
998cca6fc52SDaniel Fojt 	if (!CBB_add_u16_length_prefixed(cbb, &sig_cbb))
999cca6fc52SDaniel Fojt 		goto err;
1000cca6fc52SDaniel Fojt 	if (!CBB_add_bytes(&sig_cbb, sig, sig_len))
1001cca6fc52SDaniel Fojt 		goto err;
1002cca6fc52SDaniel Fojt 
1003cca6fc52SDaniel Fojt 	if (!CBB_flush(cbb))
1004cca6fc52SDaniel Fojt 		goto err;
1005cca6fc52SDaniel Fojt 
1006cca6fc52SDaniel Fojt 	ret = 1;
1007cca6fc52SDaniel Fojt 
1008cca6fc52SDaniel Fojt  err:
1009cca6fc52SDaniel Fojt 	if (!ret && ctx->alert == 0)
1010*8edacedfSDaniel Fojt 		ctx->alert = TLS13_ALERT_INTERNAL_ERROR;
1011cca6fc52SDaniel Fojt 
1012cca6fc52SDaniel Fojt 	CBB_cleanup(&sig_cbb);
1013cca6fc52SDaniel Fojt 	EVP_MD_CTX_free(mdctx);
1014cca6fc52SDaniel Fojt 	free(sig_content);
1015cca6fc52SDaniel Fojt 	free(sig);
1016cca6fc52SDaniel Fojt 
1017cca6fc52SDaniel Fojt 	return ret;
1018cca6fc52SDaniel Fojt }
1019cca6fc52SDaniel Fojt 
1020cca6fc52SDaniel Fojt int
1021cca6fc52SDaniel Fojt tls13_client_end_of_early_data_send(struct tls13_ctx *ctx, CBB *cbb)
1022cca6fc52SDaniel Fojt {
1023cca6fc52SDaniel Fojt 	return 0;
1024cca6fc52SDaniel Fojt }
1025cca6fc52SDaniel Fojt 
1026cca6fc52SDaniel Fojt int
1027cca6fc52SDaniel Fojt tls13_client_finished_send(struct tls13_ctx *ctx, CBB *cbb)
1028cca6fc52SDaniel Fojt {
1029cca6fc52SDaniel Fojt 	struct tls13_secrets *secrets = ctx->hs->secrets;
1030cca6fc52SDaniel Fojt 	struct tls13_secret context = { .data = "", .len = 0 };
1031cca6fc52SDaniel Fojt 	struct tls13_secret finished_key;
1032cca6fc52SDaniel Fojt 	uint8_t transcript_hash[EVP_MAX_MD_SIZE];
1033cca6fc52SDaniel Fojt 	size_t transcript_hash_len;
1034cca6fc52SDaniel Fojt 	uint8_t key[EVP_MAX_MD_SIZE];
1035cca6fc52SDaniel Fojt 	uint8_t *verify_data;
1036cca6fc52SDaniel Fojt 	size_t hmac_len;
1037cca6fc52SDaniel Fojt 	unsigned int hlen;
1038cca6fc52SDaniel Fojt 	HMAC_CTX *hmac_ctx = NULL;
1039cca6fc52SDaniel Fojt 	int ret = 0;
1040cca6fc52SDaniel Fojt 
1041cca6fc52SDaniel Fojt 	finished_key.data = key;
1042cca6fc52SDaniel Fojt 	finished_key.len = EVP_MD_size(ctx->hash);
1043cca6fc52SDaniel Fojt 
1044cca6fc52SDaniel Fojt 	if (!tls13_hkdf_expand_label(&finished_key, ctx->hash,
1045cca6fc52SDaniel Fojt 	    &secrets->client_handshake_traffic, "finished",
1046cca6fc52SDaniel Fojt 	    &context))
1047cca6fc52SDaniel Fojt 		goto err;
1048cca6fc52SDaniel Fojt 
1049cca6fc52SDaniel Fojt 	if (!tls1_transcript_hash_value(ctx->ssl, transcript_hash,
1050cca6fc52SDaniel Fojt 	    sizeof(transcript_hash), &transcript_hash_len))
1051cca6fc52SDaniel Fojt 		goto err;
1052cca6fc52SDaniel Fojt 
1053cca6fc52SDaniel Fojt 	if ((hmac_ctx = HMAC_CTX_new()) == NULL)
1054cca6fc52SDaniel Fojt 		goto err;
1055cca6fc52SDaniel Fojt 	if (!HMAC_Init_ex(hmac_ctx, finished_key.data, finished_key.len,
1056cca6fc52SDaniel Fojt 	    ctx->hash, NULL))
1057cca6fc52SDaniel Fojt 		goto err;
1058cca6fc52SDaniel Fojt 	if (!HMAC_Update(hmac_ctx, transcript_hash, transcript_hash_len))
1059cca6fc52SDaniel Fojt 		goto err;
1060cca6fc52SDaniel Fojt 
1061cca6fc52SDaniel Fojt 	hmac_len = HMAC_size(hmac_ctx);
1062cca6fc52SDaniel Fojt 	if (!CBB_add_space(cbb, &verify_data, hmac_len))
1063cca6fc52SDaniel Fojt 		goto err;
1064cca6fc52SDaniel Fojt 	if (!HMAC_Final(hmac_ctx, verify_data, &hlen))
1065cca6fc52SDaniel Fojt 		goto err;
1066cca6fc52SDaniel Fojt 	if (hlen != hmac_len)
1067cca6fc52SDaniel Fojt 		goto err;
1068cca6fc52SDaniel Fojt 
1069cca6fc52SDaniel Fojt 	ret = 1;
1070cca6fc52SDaniel Fojt 
1071cca6fc52SDaniel Fojt  err:
1072cca6fc52SDaniel Fojt 	HMAC_CTX_free(hmac_ctx);
1073cca6fc52SDaniel Fojt 
1074cca6fc52SDaniel Fojt 	return ret;
1075cca6fc52SDaniel Fojt }
1076cca6fc52SDaniel Fojt 
1077cca6fc52SDaniel Fojt int
1078cca6fc52SDaniel Fojt tls13_client_finished_sent(struct tls13_ctx *ctx)
1079cca6fc52SDaniel Fojt {
1080cca6fc52SDaniel Fojt 	struct tls13_secrets *secrets = ctx->hs->secrets;
1081cca6fc52SDaniel Fojt 
1082cca6fc52SDaniel Fojt 	/*
1083cca6fc52SDaniel Fojt 	 * Any records following the client finished message must be encrypted
1084cca6fc52SDaniel Fojt 	 * using the client application traffic keys.
1085cca6fc52SDaniel Fojt 	 */
1086cca6fc52SDaniel Fojt 	return tls13_record_layer_set_write_traffic_key(ctx->rl,
1087cca6fc52SDaniel Fojt 	    &secrets->client_application_traffic);
1088cca6fc52SDaniel Fojt }
1089