1*de0e0e4dSAntonio Huete Jimenez /* $OpenBSD: tls13_client.c,v 1.99 2022/09/11 14:33:07 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 <openssl/ssl3.h>
19cca6fc52SDaniel Fojt
20cca6fc52SDaniel Fojt #include "bytestring.h"
21*de0e0e4dSAntonio Huete Jimenez #include "ssl_locl.h"
22*de0e0e4dSAntonio Huete Jimenez #include "ssl_sigalgs.h"
23cca6fc52SDaniel Fojt #include "ssl_tlsext.h"
24cca6fc52SDaniel Fojt #include "tls13_handshake.h"
25cca6fc52SDaniel Fojt #include "tls13_internal.h"
26cca6fc52SDaniel Fojt
27cca6fc52SDaniel Fojt int
tls13_client_init(struct tls13_ctx * ctx)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
34*de0e0e4dSAntonio Huete Jimenez if (!ssl_supported_tls_version_range(s, &ctx->hs->our_min_tls_version,
35*de0e0e4dSAntonio Huete Jimenez &ctx->hs->our_max_tls_version)) {
36cca6fc52SDaniel Fojt SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE);
37cca6fc52SDaniel Fojt return 0;
38cca6fc52SDaniel Fojt }
39*de0e0e4dSAntonio Huete Jimenez s->version = ctx->hs->our_max_tls_version;
40cca6fc52SDaniel Fojt
418edacedfSDaniel Fojt tls13_record_layer_set_retry_after_phh(ctx->rl,
428edacedfSDaniel Fojt (s->internal->mode & SSL_MODE_AUTO_RETRY) != 0);
438edacedfSDaniel 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;
54*de0e0e4dSAntonio Huete Jimenez if ((ctx->hs->key_share = tls_key_share_new(groups[0])) == NULL)
55cca6fc52SDaniel Fojt return 0;
56*de0e0e4dSAntonio Huete Jimenez if (!tls_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*de0e0e4dSAntonio Huete Jimenez if (ctx->middlebox_compat &&
68*de0e0e4dSAntonio Huete Jimenez ctx->hs->our_max_tls_version >= TLS1_3_VERSION) {
69*de0e0e4dSAntonio Huete Jimenez arc4random_buf(ctx->hs->tls13.legacy_session_id,
70*de0e0e4dSAntonio Huete Jimenez sizeof(ctx->hs->tls13.legacy_session_id));
71*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.legacy_session_id_len =
72*de0e0e4dSAntonio Huete Jimenez sizeof(ctx->hs->tls13.legacy_session_id);
73cca6fc52SDaniel Fojt }
74cca6fc52SDaniel Fojt
75cca6fc52SDaniel Fojt return 1;
76cca6fc52SDaniel Fojt }
77cca6fc52SDaniel Fojt
78cca6fc52SDaniel Fojt int
tls13_client_connect(struct tls13_ctx * ctx)79cca6fc52SDaniel Fojt tls13_client_connect(struct tls13_ctx *ctx)
80cca6fc52SDaniel Fojt {
81cca6fc52SDaniel Fojt if (ctx->mode != TLS13_HS_CLIENT)
82cca6fc52SDaniel Fojt return TLS13_IO_FAILURE;
83cca6fc52SDaniel Fojt
84cca6fc52SDaniel Fojt return tls13_handshake_perform(ctx);
85cca6fc52SDaniel Fojt }
86cca6fc52SDaniel Fojt
87cca6fc52SDaniel Fojt static int
tls13_client_hello_build(struct tls13_ctx * ctx,CBB * cbb)88cca6fc52SDaniel Fojt tls13_client_hello_build(struct tls13_ctx *ctx, CBB *cbb)
89cca6fc52SDaniel Fojt {
90cca6fc52SDaniel Fojt CBB cipher_suites, compression_methods, session_id;
91cca6fc52SDaniel Fojt uint16_t client_version;
92cca6fc52SDaniel Fojt SSL *s = ctx->ssl;
93cca6fc52SDaniel Fojt
94cca6fc52SDaniel Fojt /* Legacy client version is capped at TLS 1.2. */
95*de0e0e4dSAntonio Huete Jimenez if (!ssl_max_legacy_version(s, &client_version))
96*de0e0e4dSAntonio Huete Jimenez goto err;
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;
105*de0e0e4dSAntonio Huete Jimenez if (!CBB_add_bytes(&session_id, ctx->hs->tls13.legacy_session_id,
106*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.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
1218edacedfSDaniel 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
tls13_client_hello_send(struct tls13_ctx * ctx,CBB * cbb)134cca6fc52SDaniel Fojt tls13_client_hello_send(struct tls13_ctx *ctx, CBB *cbb)
135cca6fc52SDaniel Fojt {
136*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->our_min_tls_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
tls13_client_hello_sent(struct tls13_ctx * ctx)149cca6fc52SDaniel Fojt tls13_client_hello_sent(struct tls13_ctx *ctx)
150cca6fc52SDaniel Fojt {
151cca6fc52SDaniel Fojt tls1_transcript_freeze(ctx->ssl);
152cca6fc52SDaniel Fojt
153*de0e0e4dSAntonio Huete Jimenez if (ctx->middlebox_compat) {
154*de0e0e4dSAntonio Huete Jimenez tls13_record_layer_allow_ccs(ctx->rl, 1);
1558edacedfSDaniel Fojt ctx->send_dummy_ccs = 1;
156*de0e0e4dSAntonio Huete Jimenez }
1578edacedfSDaniel Fojt
158cca6fc52SDaniel Fojt return 1;
159cca6fc52SDaniel Fojt }
160cca6fc52SDaniel Fojt
161cca6fc52SDaniel Fojt static int
tls13_server_hello_is_legacy(CBS * cbs)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
tls13_server_hello_is_retry(CBS * cbs)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
tls13_server_hello_process(struct tls13_ctx * ctx,CBS * cbs)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)) {
231*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->our_max_tls_version >= TLS1_3_VERSION) {
232cca6fc52SDaniel Fojt /*
233*de0e0e4dSAntonio Huete Jimenez * 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))) {
2448edacedfSDaniel 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
252*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.use_legacy = 1;
253cca6fc52SDaniel Fojt return 1;
254cca6fc52SDaniel Fojt }
255cca6fc52SDaniel Fojt
256cca6fc52SDaniel Fojt /* From here on in we know we are doing TLSv1.3. */
2578edacedfSDaniel 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;
265*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.hrr = 1;
266cca6fc52SDaniel Fojt }
267cca6fc52SDaniel Fojt
2688edacedfSDaniel 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 /*
274*de0e0e4dSAntonio Huete Jimenez * The supported versions extension indicated 0x0304 or greater.
275*de0e0e4dSAntonio Huete Jimenez * Ensure that it was 0x0304 and that legacy version is set to 0x0303
276*de0e0e4dSAntonio Huete Jimenez * (RFC 8446 section 4.2.1).
277cca6fc52SDaniel Fojt */
278*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->tls13.server_version != TLS1_3_VERSION ||
279*de0e0e4dSAntonio Huete Jimenez legacy_version != TLS1_2_VERSION) {
2808edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_PROTOCOL_VERSION;
281cca6fc52SDaniel Fojt goto err;
282cca6fc52SDaniel Fojt }
283*de0e0e4dSAntonio Huete Jimenez ctx->hs->negotiated_tls_version = ctx->hs->tls13.server_version;
284*de0e0e4dSAntonio Huete Jimenez ctx->hs->peer_legacy_version = legacy_version;
285cca6fc52SDaniel Fojt
286cca6fc52SDaniel Fojt /* The session_id must match. */
287*de0e0e4dSAntonio Huete Jimenez if (!CBS_mem_equal(&session_id, ctx->hs->tls13.legacy_session_id,
288*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.legacy_session_id_len)) {
2898edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
290cca6fc52SDaniel Fojt goto err;
291cca6fc52SDaniel Fojt }
292cca6fc52SDaniel Fojt
293cca6fc52SDaniel Fojt /*
294cca6fc52SDaniel Fojt * Ensure that the cipher suite is one that we offered in the client
295*de0e0e4dSAntonio Huete Jimenez * hello and that it is a TLSv1.3 cipher suite.
296cca6fc52SDaniel Fojt */
297cca6fc52SDaniel Fojt cipher = ssl3_get_cipher_by_value(cipher_suite);
2988edacedfSDaniel Fojt if (cipher == NULL || !ssl_cipher_in_list(SSL_get_ciphers(s), cipher)) {
2998edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
300cca6fc52SDaniel Fojt goto err;
301cca6fc52SDaniel Fojt }
302*de0e0e4dSAntonio Huete Jimenez if (cipher->algorithm_ssl != SSL_TLSV1_3) {
3038edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
304cca6fc52SDaniel Fojt goto err;
305cca6fc52SDaniel Fojt }
306*de0e0e4dSAntonio Huete Jimenez if (!(ctx->handshake_stage.hs_type & WITHOUT_HRR) && !ctx->hs->tls13.hrr) {
307*de0e0e4dSAntonio Huete Jimenez /*
308*de0e0e4dSAntonio Huete Jimenez * A ServerHello following a HelloRetryRequest MUST use the same
309*de0e0e4dSAntonio Huete Jimenez * cipher suite (RFC 8446 section 4.1.4).
310*de0e0e4dSAntonio Huete Jimenez */
311*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->cipher != cipher) {
312*de0e0e4dSAntonio Huete Jimenez ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
313*de0e0e4dSAntonio Huete Jimenez goto err;
314*de0e0e4dSAntonio Huete Jimenez }
315*de0e0e4dSAntonio Huete Jimenez }
316*de0e0e4dSAntonio Huete Jimenez ctx->hs->cipher = cipher;
317cca6fc52SDaniel Fojt
318cca6fc52SDaniel Fojt if (compression_method != 0) {
3198edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
320cca6fc52SDaniel Fojt goto err;
321cca6fc52SDaniel Fojt }
322cca6fc52SDaniel Fojt
323cca6fc52SDaniel Fojt return 1;
324cca6fc52SDaniel Fojt
325cca6fc52SDaniel Fojt err:
326cca6fc52SDaniel Fojt if (ctx->alert == 0)
3278edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECODE_ERROR;
328cca6fc52SDaniel Fojt
329cca6fc52SDaniel Fojt return 0;
330cca6fc52SDaniel Fojt }
331cca6fc52SDaniel Fojt
332cca6fc52SDaniel Fojt static int
tls13_client_engage_record_protection(struct tls13_ctx * ctx)333cca6fc52SDaniel Fojt tls13_client_engage_record_protection(struct tls13_ctx *ctx)
334cca6fc52SDaniel Fojt {
335cca6fc52SDaniel Fojt struct tls13_secrets *secrets;
336cca6fc52SDaniel Fojt struct tls13_secret context;
337cca6fc52SDaniel Fojt unsigned char buf[EVP_MAX_MD_SIZE];
338cca6fc52SDaniel Fojt uint8_t *shared_key = NULL;
339cca6fc52SDaniel Fojt size_t shared_key_len = 0;
340cca6fc52SDaniel Fojt size_t hash_len;
341cca6fc52SDaniel Fojt SSL *s = ctx->ssl;
342cca6fc52SDaniel Fojt int ret = 0;
343cca6fc52SDaniel Fojt
344cca6fc52SDaniel Fojt /* Derive the shared key and engage record protection. */
345cca6fc52SDaniel Fojt
346*de0e0e4dSAntonio Huete Jimenez if (!tls_key_share_derive(ctx->hs->key_share, &shared_key,
347cca6fc52SDaniel Fojt &shared_key_len))
348cca6fc52SDaniel Fojt goto err;
349cca6fc52SDaniel Fojt
350*de0e0e4dSAntonio Huete Jimenez s->session->cipher = ctx->hs->cipher;
351*de0e0e4dSAntonio Huete Jimenez s->session->ssl_version = ctx->hs->tls13.server_version;
352cca6fc52SDaniel Fojt
353*de0e0e4dSAntonio Huete Jimenez if ((ctx->aead = tls13_cipher_aead(ctx->hs->cipher)) == NULL)
354cca6fc52SDaniel Fojt goto err;
355*de0e0e4dSAntonio Huete Jimenez if ((ctx->hash = tls13_cipher_hash(ctx->hs->cipher)) == NULL)
356cca6fc52SDaniel Fojt goto err;
357cca6fc52SDaniel Fojt
358cca6fc52SDaniel Fojt if ((secrets = tls13_secrets_create(ctx->hash, 0)) == NULL)
359cca6fc52SDaniel Fojt goto err;
360*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.secrets = secrets;
361cca6fc52SDaniel Fojt
362cca6fc52SDaniel Fojt /* XXX - pass in hash. */
363cca6fc52SDaniel Fojt if (!tls1_transcript_hash_init(s))
364cca6fc52SDaniel Fojt goto err;
365cca6fc52SDaniel Fojt tls1_transcript_free(s);
366cca6fc52SDaniel Fojt if (!tls1_transcript_hash_value(s, buf, sizeof(buf), &hash_len))
367cca6fc52SDaniel Fojt goto err;
368cca6fc52SDaniel Fojt context.data = buf;
369cca6fc52SDaniel Fojt context.len = hash_len;
370cca6fc52SDaniel Fojt
371cca6fc52SDaniel Fojt /* Early secrets. */
372cca6fc52SDaniel Fojt if (!tls13_derive_early_secrets(secrets, secrets->zeros.data,
373cca6fc52SDaniel Fojt secrets->zeros.len, &context))
374cca6fc52SDaniel Fojt goto err;
375cca6fc52SDaniel Fojt
376cca6fc52SDaniel Fojt /* Handshake secrets. */
377*de0e0e4dSAntonio Huete Jimenez if (!tls13_derive_handshake_secrets(ctx->hs->tls13.secrets, shared_key,
378cca6fc52SDaniel Fojt shared_key_len, &context))
379cca6fc52SDaniel Fojt goto err;
380cca6fc52SDaniel Fojt
381cca6fc52SDaniel Fojt tls13_record_layer_set_aead(ctx->rl, ctx->aead);
382cca6fc52SDaniel Fojt tls13_record_layer_set_hash(ctx->rl, ctx->hash);
383cca6fc52SDaniel Fojt
384cca6fc52SDaniel Fojt if (!tls13_record_layer_set_read_traffic_key(ctx->rl,
385*de0e0e4dSAntonio Huete Jimenez &secrets->server_handshake_traffic, ssl_encryption_handshake))
386cca6fc52SDaniel Fojt goto err;
387cca6fc52SDaniel Fojt if (!tls13_record_layer_set_write_traffic_key(ctx->rl,
388*de0e0e4dSAntonio Huete Jimenez &secrets->client_handshake_traffic, ssl_encryption_handshake))
389cca6fc52SDaniel Fojt goto err;
390cca6fc52SDaniel Fojt
391cca6fc52SDaniel Fojt ret = 1;
392cca6fc52SDaniel Fojt
393cca6fc52SDaniel Fojt err:
394cca6fc52SDaniel Fojt freezero(shared_key, shared_key_len);
395cca6fc52SDaniel Fojt
396cca6fc52SDaniel Fojt return ret;
397cca6fc52SDaniel Fojt }
398cca6fc52SDaniel Fojt
399cca6fc52SDaniel Fojt int
tls13_server_hello_retry_request_recv(struct tls13_ctx * ctx,CBS * cbs)400cca6fc52SDaniel Fojt tls13_server_hello_retry_request_recv(struct tls13_ctx *ctx, CBS *cbs)
401cca6fc52SDaniel Fojt {
402cca6fc52SDaniel Fojt /*
403cca6fc52SDaniel Fojt * The state machine has no way of knowing if we're going to receive a
404cca6fc52SDaniel Fojt * HelloRetryRequest or a ServerHello. As such, we have to handle
405cca6fc52SDaniel Fojt * this case here and hand off to the appropriate function.
406cca6fc52SDaniel Fojt */
407cca6fc52SDaniel Fojt if (!tls13_server_hello_is_retry(cbs)) {
408cca6fc52SDaniel Fojt ctx->handshake_stage.hs_type |= WITHOUT_HRR;
409cca6fc52SDaniel Fojt return tls13_server_hello_recv(ctx, cbs);
410cca6fc52SDaniel Fojt }
411cca6fc52SDaniel Fojt
412cca6fc52SDaniel Fojt if (!tls13_server_hello_process(ctx, cbs))
413cca6fc52SDaniel Fojt return 0;
414cca6fc52SDaniel Fojt
415cca6fc52SDaniel Fojt /*
416*de0e0e4dSAntonio Huete Jimenez * This may have been a TLSv1.2 or earlier ServerHello that just
417*de0e0e4dSAntonio Huete Jimenez * happened to have matching server random...
418cca6fc52SDaniel Fojt */
419*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->tls13.use_legacy)
420cca6fc52SDaniel Fojt return tls13_use_legacy_client(ctx);
421cca6fc52SDaniel Fojt
422*de0e0e4dSAntonio Huete Jimenez if (!ctx->hs->tls13.hrr)
423cca6fc52SDaniel Fojt return 0;
424cca6fc52SDaniel Fojt
425cca6fc52SDaniel Fojt if (!tls13_synthetic_handshake_message(ctx))
426cca6fc52SDaniel Fojt return 0;
427cca6fc52SDaniel Fojt if (!tls13_handshake_msg_record(ctx))
428cca6fc52SDaniel Fojt return 0;
429cca6fc52SDaniel Fojt
430*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.hrr = 0;
431cca6fc52SDaniel Fojt
432cca6fc52SDaniel Fojt return 1;
433cca6fc52SDaniel Fojt }
434cca6fc52SDaniel Fojt
435cca6fc52SDaniel Fojt int
tls13_client_hello_retry_send(struct tls13_ctx * ctx,CBB * cbb)436cca6fc52SDaniel Fojt tls13_client_hello_retry_send(struct tls13_ctx *ctx, CBB *cbb)
437cca6fc52SDaniel Fojt {
438cca6fc52SDaniel Fojt /*
439cca6fc52SDaniel Fojt * Ensure that the server supported group is one that we listed in our
440cca6fc52SDaniel Fojt * supported groups and is not the same as the key share we previously
441cca6fc52SDaniel Fojt * offered.
442cca6fc52SDaniel Fojt */
443*de0e0e4dSAntonio Huete Jimenez if (!tls1_check_group(ctx->ssl, ctx->hs->tls13.server_group))
444cca6fc52SDaniel Fojt return 0; /* XXX alert */
445*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->tls13.server_group == tls_key_share_group(ctx->hs->key_share))
446cca6fc52SDaniel Fojt return 0; /* XXX alert */
447cca6fc52SDaniel Fojt
448cca6fc52SDaniel Fojt /* Switch to new key share. */
449*de0e0e4dSAntonio Huete Jimenez tls_key_share_free(ctx->hs->key_share);
450cca6fc52SDaniel Fojt if ((ctx->hs->key_share =
451*de0e0e4dSAntonio Huete Jimenez tls_key_share_new(ctx->hs->tls13.server_group)) == NULL)
452cca6fc52SDaniel Fojt return 0;
453*de0e0e4dSAntonio Huete Jimenez if (!tls_key_share_generate(ctx->hs->key_share))
454cca6fc52SDaniel Fojt return 0;
455cca6fc52SDaniel Fojt
456cca6fc52SDaniel Fojt if (!tls13_client_hello_build(ctx, cbb))
457cca6fc52SDaniel Fojt return 0;
458cca6fc52SDaniel Fojt
459cca6fc52SDaniel Fojt return 1;
460cca6fc52SDaniel Fojt }
461cca6fc52SDaniel Fojt
462cca6fc52SDaniel Fojt int
tls13_server_hello_recv(struct tls13_ctx * ctx,CBS * cbs)463cca6fc52SDaniel Fojt tls13_server_hello_recv(struct tls13_ctx *ctx, CBS *cbs)
464cca6fc52SDaniel Fojt {
465cca6fc52SDaniel Fojt SSL *s = ctx->ssl;
466cca6fc52SDaniel Fojt
467cca6fc52SDaniel Fojt /*
468cca6fc52SDaniel Fojt * We may have received a legacy (pre-TLSv1.3) ServerHello or a TLSv1.3
469cca6fc52SDaniel Fojt * ServerHello. HelloRetryRequests have already been handled.
470cca6fc52SDaniel Fojt */
471cca6fc52SDaniel Fojt if (!tls13_server_hello_process(ctx, cbs))
472cca6fc52SDaniel Fojt return 0;
473cca6fc52SDaniel Fojt
474cca6fc52SDaniel Fojt if (ctx->handshake_stage.hs_type & WITHOUT_HRR) {
475cca6fc52SDaniel Fojt tls1_transcript_unfreeze(s);
476cca6fc52SDaniel Fojt if (!tls13_handshake_msg_record(ctx))
477cca6fc52SDaniel Fojt return 0;
478cca6fc52SDaniel Fojt }
479cca6fc52SDaniel Fojt
480*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->tls13.use_legacy) {
481cca6fc52SDaniel Fojt if (!(ctx->handshake_stage.hs_type & WITHOUT_HRR))
482cca6fc52SDaniel Fojt return 0;
483cca6fc52SDaniel Fojt return tls13_use_legacy_client(ctx);
484cca6fc52SDaniel Fojt }
485cca6fc52SDaniel Fojt
486*de0e0e4dSAntonio Huete Jimenez if (ctx->hs->tls13.hrr) {
487cca6fc52SDaniel Fojt /* The server has sent two HelloRetryRequests. */
4888edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER;
489cca6fc52SDaniel Fojt return 0;
490cca6fc52SDaniel Fojt }
491cca6fc52SDaniel Fojt
492cca6fc52SDaniel Fojt if (!tls13_client_engage_record_protection(ctx))
493cca6fc52SDaniel Fojt return 0;
494cca6fc52SDaniel Fojt
495cca6fc52SDaniel Fojt ctx->handshake_stage.hs_type |= NEGOTIATED;
496cca6fc52SDaniel Fojt
497cca6fc52SDaniel Fojt return 1;
498cca6fc52SDaniel Fojt }
499cca6fc52SDaniel Fojt
500cca6fc52SDaniel Fojt int
tls13_server_encrypted_extensions_recv(struct tls13_ctx * ctx,CBS * cbs)501cca6fc52SDaniel Fojt tls13_server_encrypted_extensions_recv(struct tls13_ctx *ctx, CBS *cbs)
502cca6fc52SDaniel Fojt {
503cca6fc52SDaniel Fojt int alert_desc;
504cca6fc52SDaniel Fojt
5058edacedfSDaniel Fojt if (!tlsext_client_parse(ctx->ssl, SSL_TLSEXT_MSG_EE, cbs, &alert_desc)) {
506cca6fc52SDaniel Fojt ctx->alert = alert_desc;
507*de0e0e4dSAntonio Huete Jimenez return 0;
508cca6fc52SDaniel Fojt }
509cca6fc52SDaniel Fojt
510cca6fc52SDaniel Fojt return 1;
511cca6fc52SDaniel Fojt }
512cca6fc52SDaniel Fojt
513cca6fc52SDaniel Fojt int
tls13_server_certificate_request_recv(struct tls13_ctx * ctx,CBS * cbs)514cca6fc52SDaniel Fojt tls13_server_certificate_request_recv(struct tls13_ctx *ctx, CBS *cbs)
515cca6fc52SDaniel Fojt {
516cca6fc52SDaniel Fojt CBS cert_request_context;
517cca6fc52SDaniel Fojt int alert_desc;
518cca6fc52SDaniel Fojt
519cca6fc52SDaniel Fojt /*
520cca6fc52SDaniel Fojt * Thanks to poor state design in the RFC, this function can be called
521cca6fc52SDaniel Fojt * when we actually have a certificate message instead of a certificate
522cca6fc52SDaniel Fojt * request... in that case we call the certificate handler after
523cca6fc52SDaniel Fojt * switching state, to avoid advancing state.
524cca6fc52SDaniel Fojt */
525cca6fc52SDaniel Fojt if (tls13_handshake_msg_type(ctx->hs_msg) == TLS13_MT_CERTIFICATE) {
526cca6fc52SDaniel Fojt ctx->handshake_stage.hs_type |= WITHOUT_CR;
527cca6fc52SDaniel Fojt return tls13_server_certificate_recv(ctx, cbs);
528cca6fc52SDaniel Fojt }
529cca6fc52SDaniel Fojt
530cca6fc52SDaniel Fojt if (!CBS_get_u8_length_prefixed(cbs, &cert_request_context))
531cca6fc52SDaniel Fojt goto err;
532cca6fc52SDaniel Fojt if (CBS_len(&cert_request_context) != 0)
533cca6fc52SDaniel Fojt goto err;
534cca6fc52SDaniel Fojt
5358edacedfSDaniel Fojt if (!tlsext_client_parse(ctx->ssl, SSL_TLSEXT_MSG_CR, cbs, &alert_desc)) {
536cca6fc52SDaniel Fojt ctx->alert = alert_desc;
537cca6fc52SDaniel Fojt goto err;
538cca6fc52SDaniel Fojt }
539cca6fc52SDaniel Fojt
540cca6fc52SDaniel Fojt return 1;
541cca6fc52SDaniel Fojt
542cca6fc52SDaniel Fojt err:
543cca6fc52SDaniel Fojt if (ctx->alert == 0)
5448edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECODE_ERROR;
5458edacedfSDaniel Fojt
546cca6fc52SDaniel Fojt return 0;
547cca6fc52SDaniel Fojt }
548cca6fc52SDaniel Fojt
549cca6fc52SDaniel Fojt int
tls13_server_certificate_recv(struct tls13_ctx * ctx,CBS * cbs)550cca6fc52SDaniel Fojt tls13_server_certificate_recv(struct tls13_ctx *ctx, CBS *cbs)
551cca6fc52SDaniel Fojt {
5528edacedfSDaniel Fojt CBS cert_request_context, cert_list, cert_data;
553cca6fc52SDaniel Fojt struct stack_st_X509 *certs = NULL;
554cca6fc52SDaniel Fojt SSL *s = ctx->ssl;
555cca6fc52SDaniel Fojt X509 *cert = NULL;
556cca6fc52SDaniel Fojt const uint8_t *p;
557*de0e0e4dSAntonio Huete Jimenez int alert_desc;
558cca6fc52SDaniel Fojt int ret = 0;
559cca6fc52SDaniel Fojt
560cca6fc52SDaniel Fojt if ((certs = sk_X509_new_null()) == NULL)
561cca6fc52SDaniel Fojt goto err;
562cca6fc52SDaniel Fojt
563cca6fc52SDaniel Fojt if (!CBS_get_u8_length_prefixed(cbs, &cert_request_context))
564cca6fc52SDaniel Fojt goto err;
565cca6fc52SDaniel Fojt if (CBS_len(&cert_request_context) != 0)
566cca6fc52SDaniel Fojt goto err;
567cca6fc52SDaniel Fojt if (!CBS_get_u24_length_prefixed(cbs, &cert_list))
568cca6fc52SDaniel Fojt goto err;
569cca6fc52SDaniel Fojt
570cca6fc52SDaniel Fojt while (CBS_len(&cert_list) > 0) {
571cca6fc52SDaniel Fojt if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data))
572cca6fc52SDaniel Fojt goto err;
5738edacedfSDaniel Fojt
5748edacedfSDaniel Fojt if (!tlsext_client_parse(ctx->ssl, SSL_TLSEXT_MSG_CT,
5758edacedfSDaniel Fojt &cert_list, &alert_desc)) {
5768edacedfSDaniel Fojt ctx->alert = alert_desc;
577cca6fc52SDaniel Fojt goto err;
5788edacedfSDaniel Fojt }
579cca6fc52SDaniel Fojt
580cca6fc52SDaniel Fojt p = CBS_data(&cert_data);
581cca6fc52SDaniel Fojt if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL)
582cca6fc52SDaniel Fojt goto err;
583cca6fc52SDaniel Fojt if (p != CBS_data(&cert_data) + CBS_len(&cert_data))
584cca6fc52SDaniel Fojt goto err;
585cca6fc52SDaniel Fojt
586cca6fc52SDaniel Fojt if (!sk_X509_push(certs, cert))
587cca6fc52SDaniel Fojt goto err;
588cca6fc52SDaniel Fojt
589cca6fc52SDaniel Fojt cert = NULL;
590cca6fc52SDaniel Fojt }
591cca6fc52SDaniel Fojt
592cca6fc52SDaniel Fojt /* A server must always provide a non-empty certificate list. */
593cca6fc52SDaniel Fojt if (sk_X509_num(certs) < 1) {
5948edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECODE_ERROR;
595cca6fc52SDaniel Fojt tls13_set_errorx(ctx, TLS13_ERR_NO_PEER_CERTIFICATE, 0,
596cca6fc52SDaniel Fojt "peer failed to provide a certificate", NULL);
597cca6fc52SDaniel Fojt goto err;
598cca6fc52SDaniel Fojt }
599cca6fc52SDaniel Fojt
600cca6fc52SDaniel Fojt /*
601cca6fc52SDaniel Fojt * At this stage we still have no proof of possession. As such, it would
602cca6fc52SDaniel Fojt * be preferable to keep the chain and verify once we have successfully
603cca6fc52SDaniel Fojt * processed the CertificateVerify message.
604cca6fc52SDaniel Fojt */
605cca6fc52SDaniel Fojt if (ssl_verify_cert_chain(s, certs) <= 0 &&
606cca6fc52SDaniel Fojt s->verify_mode != SSL_VERIFY_NONE) {
607cca6fc52SDaniel Fojt ctx->alert = ssl_verify_alarm_type(s->verify_result);
608cca6fc52SDaniel Fojt tls13_set_errorx(ctx, TLS13_ERR_VERIFY_FAILED, 0,
609cca6fc52SDaniel Fojt "failed to verify peer certificate", NULL);
610cca6fc52SDaniel Fojt goto err;
611cca6fc52SDaniel Fojt }
612*de0e0e4dSAntonio Huete Jimenez s->session->verify_result = s->verify_result;
613cca6fc52SDaniel Fojt ERR_clear_error();
614cca6fc52SDaniel Fojt
615*de0e0e4dSAntonio Huete Jimenez if (!tls_process_peer_certs(s, certs))
616cca6fc52SDaniel Fojt goto err;
617cca6fc52SDaniel Fojt
6188edacedfSDaniel Fojt if (ctx->ocsp_status_recv_cb != NULL &&
6198edacedfSDaniel Fojt !ctx->ocsp_status_recv_cb(ctx))
6208edacedfSDaniel Fojt goto err;
6218edacedfSDaniel Fojt
622cca6fc52SDaniel Fojt ret = 1;
623cca6fc52SDaniel Fojt
624cca6fc52SDaniel Fojt err:
625cca6fc52SDaniel Fojt sk_X509_pop_free(certs, X509_free);
626cca6fc52SDaniel Fojt X509_free(cert);
627cca6fc52SDaniel Fojt
628cca6fc52SDaniel Fojt return ret;
629cca6fc52SDaniel Fojt }
630cca6fc52SDaniel Fojt
631cca6fc52SDaniel Fojt int
tls13_server_certificate_verify_recv(struct tls13_ctx * ctx,CBS * cbs)632cca6fc52SDaniel Fojt tls13_server_certificate_verify_recv(struct tls13_ctx *ctx, CBS *cbs)
633cca6fc52SDaniel Fojt {
634cca6fc52SDaniel Fojt const struct ssl_sigalg *sigalg;
635cca6fc52SDaniel Fojt uint16_t signature_scheme;
636cca6fc52SDaniel Fojt uint8_t *sig_content = NULL;
637cca6fc52SDaniel Fojt size_t sig_content_len;
638cca6fc52SDaniel Fojt EVP_MD_CTX *mdctx = NULL;
639cca6fc52SDaniel Fojt EVP_PKEY_CTX *pctx;
640cca6fc52SDaniel Fojt EVP_PKEY *pkey;
641cca6fc52SDaniel Fojt X509 *cert;
642cca6fc52SDaniel Fojt CBS signature;
643cca6fc52SDaniel Fojt CBB cbb;
644cca6fc52SDaniel Fojt int ret = 0;
645cca6fc52SDaniel Fojt
646cca6fc52SDaniel Fojt memset(&cbb, 0, sizeof(cbb));
647cca6fc52SDaniel Fojt
648cca6fc52SDaniel Fojt if (!CBS_get_u16(cbs, &signature_scheme))
649cca6fc52SDaniel Fojt goto err;
650cca6fc52SDaniel Fojt if (!CBS_get_u16_length_prefixed(cbs, &signature))
651cca6fc52SDaniel Fojt goto err;
652cca6fc52SDaniel Fojt
653cca6fc52SDaniel Fojt if (!CBB_init(&cbb, 0))
654cca6fc52SDaniel Fojt goto err;
655cca6fc52SDaniel Fojt if (!CBB_add_bytes(&cbb, tls13_cert_verify_pad,
656cca6fc52SDaniel Fojt sizeof(tls13_cert_verify_pad)))
657cca6fc52SDaniel Fojt goto err;
658cca6fc52SDaniel Fojt if (!CBB_add_bytes(&cbb, tls13_cert_server_verify_context,
659cca6fc52SDaniel Fojt strlen(tls13_cert_server_verify_context)))
660cca6fc52SDaniel Fojt goto err;
661cca6fc52SDaniel Fojt if (!CBB_add_u8(&cbb, 0))
662cca6fc52SDaniel Fojt goto err;
663*de0e0e4dSAntonio Huete Jimenez if (!CBB_add_bytes(&cbb, ctx->hs->tls13.transcript_hash,
664*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.transcript_hash_len))
665cca6fc52SDaniel Fojt goto err;
666cca6fc52SDaniel Fojt if (!CBB_finish(&cbb, &sig_content, &sig_content_len))
667cca6fc52SDaniel Fojt goto err;
668cca6fc52SDaniel Fojt
669*de0e0e4dSAntonio Huete Jimenez if ((cert = ctx->ssl->session->peer_cert) == NULL)
670cca6fc52SDaniel Fojt goto err;
671cca6fc52SDaniel Fojt if ((pkey = X509_get0_pubkey(cert)) == NULL)
672cca6fc52SDaniel Fojt goto err;
673*de0e0e4dSAntonio Huete Jimenez if ((sigalg = ssl_sigalg_for_peer(ctx->ssl, pkey,
674*de0e0e4dSAntonio Huete Jimenez signature_scheme)) == NULL)
675cca6fc52SDaniel Fojt goto err;
676*de0e0e4dSAntonio Huete Jimenez ctx->hs->peer_sigalg = sigalg;
677cca6fc52SDaniel Fojt
678cca6fc52SDaniel Fojt if (CBS_len(&signature) > EVP_PKEY_size(pkey))
679cca6fc52SDaniel Fojt goto err;
680cca6fc52SDaniel Fojt
681cca6fc52SDaniel Fojt if ((mdctx = EVP_MD_CTX_new()) == NULL)
682cca6fc52SDaniel Fojt goto err;
683cca6fc52SDaniel Fojt if (!EVP_DigestVerifyInit(mdctx, &pctx, sigalg->md(), NULL, pkey))
684cca6fc52SDaniel Fojt goto err;
685cca6fc52SDaniel Fojt if (sigalg->flags & SIGALG_FLAG_RSA_PSS) {
686cca6fc52SDaniel Fojt if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING))
687cca6fc52SDaniel Fojt goto err;
688cca6fc52SDaniel Fojt if (!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))
689cca6fc52SDaniel Fojt goto err;
690cca6fc52SDaniel Fojt }
691cca6fc52SDaniel Fojt if (!EVP_DigestVerifyUpdate(mdctx, sig_content, sig_content_len)) {
6928edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECRYPT_ERROR;
693cca6fc52SDaniel Fojt goto err;
694cca6fc52SDaniel Fojt }
695cca6fc52SDaniel Fojt if (EVP_DigestVerifyFinal(mdctx, CBS_data(&signature),
696cca6fc52SDaniel Fojt CBS_len(&signature)) <= 0) {
6978edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECRYPT_ERROR;
698cca6fc52SDaniel Fojt goto err;
699cca6fc52SDaniel Fojt }
700cca6fc52SDaniel Fojt
701cca6fc52SDaniel Fojt ret = 1;
702cca6fc52SDaniel Fojt
703cca6fc52SDaniel Fojt err:
704cca6fc52SDaniel Fojt if (!ret && ctx->alert == 0)
7058edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECODE_ERROR;
706cca6fc52SDaniel Fojt CBB_cleanup(&cbb);
707cca6fc52SDaniel Fojt EVP_MD_CTX_free(mdctx);
708cca6fc52SDaniel Fojt free(sig_content);
709cca6fc52SDaniel Fojt
710cca6fc52SDaniel Fojt return ret;
711cca6fc52SDaniel Fojt }
712cca6fc52SDaniel Fojt
713cca6fc52SDaniel Fojt int
tls13_server_finished_recv(struct tls13_ctx * ctx,CBS * cbs)714cca6fc52SDaniel Fojt tls13_server_finished_recv(struct tls13_ctx *ctx, CBS *cbs)
715cca6fc52SDaniel Fojt {
716*de0e0e4dSAntonio Huete Jimenez struct tls13_secrets *secrets = ctx->hs->tls13.secrets;
717cca6fc52SDaniel Fojt struct tls13_secret context = { .data = "", .len = 0 };
718cca6fc52SDaniel Fojt struct tls13_secret finished_key;
719cca6fc52SDaniel Fojt uint8_t transcript_hash[EVP_MAX_MD_SIZE];
720cca6fc52SDaniel Fojt size_t transcript_hash_len;
721cca6fc52SDaniel Fojt uint8_t *verify_data = NULL;
722cca6fc52SDaniel Fojt size_t verify_data_len;
723cca6fc52SDaniel Fojt uint8_t key[EVP_MAX_MD_SIZE];
724cca6fc52SDaniel Fojt HMAC_CTX *hmac_ctx = NULL;
725cca6fc52SDaniel Fojt unsigned int hlen;
726cca6fc52SDaniel Fojt int ret = 0;
727cca6fc52SDaniel Fojt
728cca6fc52SDaniel Fojt /*
729cca6fc52SDaniel Fojt * Verify server finished.
730cca6fc52SDaniel Fojt */
731cca6fc52SDaniel Fojt finished_key.data = key;
732cca6fc52SDaniel Fojt finished_key.len = EVP_MD_size(ctx->hash);
733cca6fc52SDaniel Fojt
734cca6fc52SDaniel Fojt if (!tls13_hkdf_expand_label(&finished_key, ctx->hash,
735cca6fc52SDaniel Fojt &secrets->server_handshake_traffic, "finished",
736cca6fc52SDaniel Fojt &context))
737cca6fc52SDaniel Fojt goto err;
738cca6fc52SDaniel Fojt
739cca6fc52SDaniel Fojt if ((hmac_ctx = HMAC_CTX_new()) == NULL)
740cca6fc52SDaniel Fojt goto err;
741cca6fc52SDaniel Fojt if (!HMAC_Init_ex(hmac_ctx, finished_key.data, finished_key.len,
742cca6fc52SDaniel Fojt ctx->hash, NULL))
743cca6fc52SDaniel Fojt goto err;
744*de0e0e4dSAntonio Huete Jimenez if (!HMAC_Update(hmac_ctx, ctx->hs->tls13.transcript_hash,
745*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.transcript_hash_len))
746cca6fc52SDaniel Fojt goto err;
747cca6fc52SDaniel Fojt verify_data_len = HMAC_size(hmac_ctx);
748cca6fc52SDaniel Fojt if ((verify_data = calloc(1, verify_data_len)) == NULL)
749cca6fc52SDaniel Fojt goto err;
750cca6fc52SDaniel Fojt if (!HMAC_Final(hmac_ctx, verify_data, &hlen))
751cca6fc52SDaniel Fojt goto err;
752cca6fc52SDaniel Fojt if (hlen != verify_data_len)
753cca6fc52SDaniel Fojt goto err;
754cca6fc52SDaniel Fojt
755cca6fc52SDaniel Fojt if (!CBS_mem_equal(cbs, verify_data, verify_data_len)) {
7568edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_DECRYPT_ERROR;
757cca6fc52SDaniel Fojt goto err;
758cca6fc52SDaniel Fojt }
759cca6fc52SDaniel Fojt
760*de0e0e4dSAntonio Huete Jimenez if (!CBS_write_bytes(cbs, ctx->hs->peer_finished,
761*de0e0e4dSAntonio Huete Jimenez sizeof(ctx->hs->peer_finished),
762*de0e0e4dSAntonio Huete Jimenez &ctx->hs->peer_finished_len))
763*de0e0e4dSAntonio Huete Jimenez goto err;
764*de0e0e4dSAntonio Huete Jimenez
765cca6fc52SDaniel Fojt if (!CBS_skip(cbs, verify_data_len))
766cca6fc52SDaniel Fojt goto err;
767cca6fc52SDaniel Fojt
768cca6fc52SDaniel Fojt /*
769cca6fc52SDaniel Fojt * Derive application traffic keys.
770cca6fc52SDaniel Fojt */
771cca6fc52SDaniel Fojt if (!tls1_transcript_hash_value(ctx->ssl, transcript_hash,
772cca6fc52SDaniel Fojt sizeof(transcript_hash), &transcript_hash_len))
773cca6fc52SDaniel Fojt goto err;
774cca6fc52SDaniel Fojt
775cca6fc52SDaniel Fojt context.data = transcript_hash;
776cca6fc52SDaniel Fojt context.len = transcript_hash_len;
777cca6fc52SDaniel Fojt
778cca6fc52SDaniel Fojt if (!tls13_derive_application_secrets(secrets, &context))
779cca6fc52SDaniel Fojt goto err;
780cca6fc52SDaniel Fojt
781cca6fc52SDaniel Fojt /*
782cca6fc52SDaniel Fojt * Any records following the server finished message must be encrypted
783cca6fc52SDaniel Fojt * using the server application traffic keys.
784cca6fc52SDaniel Fojt */
785cca6fc52SDaniel Fojt if (!tls13_record_layer_set_read_traffic_key(ctx->rl,
786*de0e0e4dSAntonio Huete Jimenez &secrets->server_application_traffic, ssl_encryption_application))
787cca6fc52SDaniel Fojt goto err;
788cca6fc52SDaniel Fojt
789cca6fc52SDaniel Fojt tls13_record_layer_allow_ccs(ctx->rl, 0);
790cca6fc52SDaniel Fojt
791cca6fc52SDaniel Fojt ret = 1;
792cca6fc52SDaniel Fojt
793cca6fc52SDaniel Fojt err:
794cca6fc52SDaniel Fojt HMAC_CTX_free(hmac_ctx);
795cca6fc52SDaniel Fojt free(verify_data);
796cca6fc52SDaniel Fojt
797cca6fc52SDaniel Fojt return ret;
798cca6fc52SDaniel Fojt }
799cca6fc52SDaniel Fojt
800f015dc58SDaniel Fojt static int
tls13_client_check_certificate(struct tls13_ctx * ctx,SSL_CERT_PKEY * cpk,int * ok,const struct ssl_sigalg ** out_sigalg)801*de0e0e4dSAntonio Huete Jimenez tls13_client_check_certificate(struct tls13_ctx *ctx, SSL_CERT_PKEY *cpk,
802f015dc58SDaniel Fojt int *ok, const struct ssl_sigalg **out_sigalg)
803f015dc58SDaniel Fojt {
804f015dc58SDaniel Fojt const struct ssl_sigalg *sigalg;
805f015dc58SDaniel Fojt SSL *s = ctx->ssl;
806f015dc58SDaniel Fojt
807f015dc58SDaniel Fojt *ok = 0;
808f015dc58SDaniel Fojt *out_sigalg = NULL;
809f015dc58SDaniel Fojt
810f015dc58SDaniel Fojt if (cpk->x509 == NULL || cpk->privatekey == NULL)
811f015dc58SDaniel Fojt goto done;
812f015dc58SDaniel Fojt
813f015dc58SDaniel Fojt if ((sigalg = ssl_sigalg_select(s, cpk->privatekey)) == NULL)
814f015dc58SDaniel Fojt goto done;
815f015dc58SDaniel Fojt
816f015dc58SDaniel Fojt *ok = 1;
817f015dc58SDaniel Fojt *out_sigalg = sigalg;
818f015dc58SDaniel Fojt
819f015dc58SDaniel Fojt done:
820f015dc58SDaniel Fojt return 1;
821f015dc58SDaniel Fojt }
822f015dc58SDaniel Fojt
823f015dc58SDaniel Fojt static int
tls13_client_select_certificate(struct tls13_ctx * ctx,SSL_CERT_PKEY ** out_cpk,const struct ssl_sigalg ** out_sigalg)824*de0e0e4dSAntonio Huete Jimenez tls13_client_select_certificate(struct tls13_ctx *ctx, SSL_CERT_PKEY **out_cpk,
825f015dc58SDaniel Fojt const struct ssl_sigalg **out_sigalg)
826f015dc58SDaniel Fojt {
827f015dc58SDaniel Fojt SSL *s = ctx->ssl;
828f015dc58SDaniel Fojt const struct ssl_sigalg *sigalg;
829*de0e0e4dSAntonio Huete Jimenez SSL_CERT_PKEY *cpk;
830f015dc58SDaniel Fojt int cert_ok;
831f015dc58SDaniel Fojt
832f015dc58SDaniel Fojt *out_cpk = NULL;
833f015dc58SDaniel Fojt *out_sigalg = NULL;
834f015dc58SDaniel Fojt
8358edacedfSDaniel Fojt /*
8368edacedfSDaniel Fojt * XXX - RFC 8446, 4.4.2.3: the server can communicate preferences
8378edacedfSDaniel Fojt * with the certificate_authorities (4.2.4) and oid_filters (4.2.5)
8388edacedfSDaniel Fojt * extensions. We should honor the former and must apply the latter.
8398edacedfSDaniel Fojt */
8408edacedfSDaniel Fojt
841f015dc58SDaniel Fojt cpk = &s->cert->pkeys[SSL_PKEY_ECC];
842f015dc58SDaniel Fojt if (!tls13_client_check_certificate(ctx, cpk, &cert_ok, &sigalg))
843f015dc58SDaniel Fojt return 0;
844f015dc58SDaniel Fojt if (cert_ok)
845f015dc58SDaniel Fojt goto done;
846f015dc58SDaniel Fojt
8478edacedfSDaniel Fojt cpk = &s->cert->pkeys[SSL_PKEY_RSA];
848f015dc58SDaniel Fojt if (!tls13_client_check_certificate(ctx, cpk, &cert_ok, &sigalg))
849f015dc58SDaniel Fojt return 0;
850f015dc58SDaniel Fojt if (cert_ok)
851f015dc58SDaniel Fojt goto done;
852f015dc58SDaniel Fojt
853f015dc58SDaniel Fojt cpk = NULL;
854f015dc58SDaniel Fojt sigalg = NULL;
855f015dc58SDaniel Fojt
856f015dc58SDaniel Fojt done:
857f015dc58SDaniel Fojt *out_cpk = cpk;
858f015dc58SDaniel Fojt *out_sigalg = sigalg;
859f015dc58SDaniel Fojt
860f015dc58SDaniel Fojt return 1;
861f015dc58SDaniel Fojt }
862f015dc58SDaniel Fojt
863cca6fc52SDaniel Fojt int
tls13_client_certificate_send(struct tls13_ctx * ctx,CBB * cbb)864cca6fc52SDaniel Fojt tls13_client_certificate_send(struct tls13_ctx *ctx, CBB *cbb)
865cca6fc52SDaniel Fojt {
866cca6fc52SDaniel Fojt SSL *s = ctx->ssl;
867cca6fc52SDaniel Fojt CBB cert_request_context, cert_list;
868f015dc58SDaniel Fojt const struct ssl_sigalg *sigalg;
869cca6fc52SDaniel Fojt STACK_OF(X509) *chain;
870*de0e0e4dSAntonio Huete Jimenez SSL_CERT_PKEY *cpk;
871cca6fc52SDaniel Fojt X509 *cert;
872cca6fc52SDaniel Fojt int i, ret = 0;
873cca6fc52SDaniel Fojt
874f015dc58SDaniel Fojt if (!tls13_client_select_certificate(ctx, &cpk, &sigalg))
875f015dc58SDaniel Fojt goto err;
876cca6fc52SDaniel Fojt
877*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.cpk = cpk;
878*de0e0e4dSAntonio Huete Jimenez ctx->hs->our_sigalg = sigalg;
879cca6fc52SDaniel Fojt
880cca6fc52SDaniel Fojt if (!CBB_add_u8_length_prefixed(cbb, &cert_request_context))
881cca6fc52SDaniel Fojt goto err;
882cca6fc52SDaniel Fojt if (!CBB_add_u24_length_prefixed(cbb, &cert_list))
883cca6fc52SDaniel Fojt goto err;
884cca6fc52SDaniel Fojt
885f015dc58SDaniel Fojt /* No certificate selected. */
886f015dc58SDaniel Fojt if (cpk == NULL)
887cca6fc52SDaniel Fojt goto done;
888cca6fc52SDaniel Fojt
889f015dc58SDaniel Fojt if ((chain = cpk->chain) == NULL)
890f015dc58SDaniel Fojt chain = s->ctx->extra_certs;
891f015dc58SDaniel Fojt
8928edacedfSDaniel Fojt if (!tls13_cert_add(ctx, &cert_list, cpk->x509, tlsext_client_build))
893cca6fc52SDaniel Fojt goto err;
894cca6fc52SDaniel Fojt
895cca6fc52SDaniel Fojt for (i = 0; i < sk_X509_num(chain); i++) {
896cca6fc52SDaniel Fojt cert = sk_X509_value(chain, i);
8978edacedfSDaniel Fojt if (!tls13_cert_add(ctx, &cert_list, cert, tlsext_client_build))
898cca6fc52SDaniel Fojt goto err;
899cca6fc52SDaniel Fojt }
900cca6fc52SDaniel Fojt
901cca6fc52SDaniel Fojt ctx->handshake_stage.hs_type |= WITH_CCV;
902cca6fc52SDaniel Fojt done:
903cca6fc52SDaniel Fojt if (!CBB_flush(cbb))
904cca6fc52SDaniel Fojt goto err;
905cca6fc52SDaniel Fojt
906cca6fc52SDaniel Fojt ret = 1;
907cca6fc52SDaniel Fojt
908cca6fc52SDaniel Fojt err:
909cca6fc52SDaniel Fojt return ret;
910cca6fc52SDaniel Fojt }
911cca6fc52SDaniel Fojt
912cca6fc52SDaniel Fojt int
tls13_client_certificate_verify_send(struct tls13_ctx * ctx,CBB * cbb)913cca6fc52SDaniel Fojt tls13_client_certificate_verify_send(struct tls13_ctx *ctx, CBB *cbb)
914cca6fc52SDaniel Fojt {
915f015dc58SDaniel Fojt const struct ssl_sigalg *sigalg;
916cca6fc52SDaniel Fojt uint8_t *sig = NULL, *sig_content = NULL;
917cca6fc52SDaniel Fojt size_t sig_len, sig_content_len;
918cca6fc52SDaniel Fojt EVP_MD_CTX *mdctx = NULL;
919cca6fc52SDaniel Fojt EVP_PKEY_CTX *pctx;
920cca6fc52SDaniel Fojt EVP_PKEY *pkey;
921*de0e0e4dSAntonio Huete Jimenez const SSL_CERT_PKEY *cpk;
922cca6fc52SDaniel Fojt CBB sig_cbb;
923cca6fc52SDaniel Fojt int ret = 0;
924cca6fc52SDaniel Fojt
925cca6fc52SDaniel Fojt memset(&sig_cbb, 0, sizeof(sig_cbb));
926cca6fc52SDaniel Fojt
927*de0e0e4dSAntonio Huete Jimenez if ((cpk = ctx->hs->tls13.cpk) == NULL)
928cca6fc52SDaniel Fojt goto err;
929*de0e0e4dSAntonio Huete Jimenez if ((sigalg = ctx->hs->our_sigalg) == NULL)
930f015dc58SDaniel Fojt goto err;
931f015dc58SDaniel Fojt pkey = cpk->privatekey;
932cca6fc52SDaniel Fojt
933cca6fc52SDaniel Fojt if (!CBB_init(&sig_cbb, 0))
934cca6fc52SDaniel Fojt goto err;
935cca6fc52SDaniel Fojt if (!CBB_add_bytes(&sig_cbb, tls13_cert_verify_pad,
936cca6fc52SDaniel Fojt sizeof(tls13_cert_verify_pad)))
937cca6fc52SDaniel Fojt goto err;
938cca6fc52SDaniel Fojt if (!CBB_add_bytes(&sig_cbb, tls13_cert_client_verify_context,
939cca6fc52SDaniel Fojt strlen(tls13_cert_client_verify_context)))
940cca6fc52SDaniel Fojt goto err;
941cca6fc52SDaniel Fojt if (!CBB_add_u8(&sig_cbb, 0))
942cca6fc52SDaniel Fojt goto err;
943*de0e0e4dSAntonio Huete Jimenez if (!CBB_add_bytes(&sig_cbb, ctx->hs->tls13.transcript_hash,
944*de0e0e4dSAntonio Huete Jimenez ctx->hs->tls13.transcript_hash_len))
945cca6fc52SDaniel Fojt goto err;
946cca6fc52SDaniel Fojt if (!CBB_finish(&sig_cbb, &sig_content, &sig_content_len))
947cca6fc52SDaniel Fojt goto err;
948cca6fc52SDaniel Fojt
949cca6fc52SDaniel Fojt if ((mdctx = EVP_MD_CTX_new()) == NULL)
950cca6fc52SDaniel Fojt goto err;
951cca6fc52SDaniel Fojt if (!EVP_DigestSignInit(mdctx, &pctx, sigalg->md(), NULL, pkey))
952cca6fc52SDaniel Fojt goto err;
953cca6fc52SDaniel Fojt if (sigalg->flags & SIGALG_FLAG_RSA_PSS) {
954cca6fc52SDaniel Fojt if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING))
955cca6fc52SDaniel Fojt goto err;
956cca6fc52SDaniel Fojt if (!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1))
957cca6fc52SDaniel Fojt goto err;
958cca6fc52SDaniel Fojt }
959cca6fc52SDaniel Fojt if (!EVP_DigestSignUpdate(mdctx, sig_content, sig_content_len))
960cca6fc52SDaniel Fojt goto err;
961cca6fc52SDaniel Fojt if (EVP_DigestSignFinal(mdctx, NULL, &sig_len) <= 0)
962cca6fc52SDaniel Fojt goto err;
963cca6fc52SDaniel Fojt if ((sig = calloc(1, sig_len)) == NULL)
964cca6fc52SDaniel Fojt goto err;
965cca6fc52SDaniel Fojt if (EVP_DigestSignFinal(mdctx, sig, &sig_len) <= 0)
966cca6fc52SDaniel Fojt goto err;
967cca6fc52SDaniel Fojt
968cca6fc52SDaniel Fojt if (!CBB_add_u16(cbb, sigalg->value))
969cca6fc52SDaniel Fojt goto err;
970cca6fc52SDaniel Fojt if (!CBB_add_u16_length_prefixed(cbb, &sig_cbb))
971cca6fc52SDaniel Fojt goto err;
972cca6fc52SDaniel Fojt if (!CBB_add_bytes(&sig_cbb, sig, sig_len))
973cca6fc52SDaniel Fojt goto err;
974cca6fc52SDaniel Fojt
975cca6fc52SDaniel Fojt if (!CBB_flush(cbb))
976cca6fc52SDaniel Fojt goto err;
977cca6fc52SDaniel Fojt
978cca6fc52SDaniel Fojt ret = 1;
979cca6fc52SDaniel Fojt
980cca6fc52SDaniel Fojt err:
981cca6fc52SDaniel Fojt if (!ret && ctx->alert == 0)
9828edacedfSDaniel Fojt ctx->alert = TLS13_ALERT_INTERNAL_ERROR;
983cca6fc52SDaniel Fojt
984cca6fc52SDaniel Fojt CBB_cleanup(&sig_cbb);
985cca6fc52SDaniel Fojt EVP_MD_CTX_free(mdctx);
986cca6fc52SDaniel Fojt free(sig_content);
987cca6fc52SDaniel Fojt free(sig);
988cca6fc52SDaniel Fojt
989cca6fc52SDaniel Fojt return ret;
990cca6fc52SDaniel Fojt }
991cca6fc52SDaniel Fojt
992cca6fc52SDaniel Fojt int
tls13_client_end_of_early_data_send(struct tls13_ctx * ctx,CBB * cbb)993cca6fc52SDaniel Fojt tls13_client_end_of_early_data_send(struct tls13_ctx *ctx, CBB *cbb)
994cca6fc52SDaniel Fojt {
995cca6fc52SDaniel Fojt return 0;
996cca6fc52SDaniel Fojt }
997cca6fc52SDaniel Fojt
998cca6fc52SDaniel Fojt int
tls13_client_finished_send(struct tls13_ctx * ctx,CBB * cbb)999cca6fc52SDaniel Fojt tls13_client_finished_send(struct tls13_ctx *ctx, CBB *cbb)
1000cca6fc52SDaniel Fojt {
1001*de0e0e4dSAntonio Huete Jimenez struct tls13_secrets *secrets = ctx->hs->tls13.secrets;
1002cca6fc52SDaniel Fojt struct tls13_secret context = { .data = "", .len = 0 };
1003*de0e0e4dSAntonio Huete Jimenez struct tls13_secret finished_key = { .data = NULL, .len = 0 };
1004cca6fc52SDaniel Fojt uint8_t transcript_hash[EVP_MAX_MD_SIZE];
1005cca6fc52SDaniel Fojt size_t transcript_hash_len;
1006cca6fc52SDaniel Fojt uint8_t *verify_data;
1007*de0e0e4dSAntonio Huete Jimenez size_t verify_data_len;
1008cca6fc52SDaniel Fojt unsigned int hlen;
1009cca6fc52SDaniel Fojt HMAC_CTX *hmac_ctx = NULL;
1010*de0e0e4dSAntonio Huete Jimenez CBS cbs;
1011cca6fc52SDaniel Fojt int ret = 0;
1012cca6fc52SDaniel Fojt
1013*de0e0e4dSAntonio Huete Jimenez if (!tls13_secret_init(&finished_key, EVP_MD_size(ctx->hash)))
1014*de0e0e4dSAntonio Huete Jimenez goto err;
1015cca6fc52SDaniel Fojt
1016cca6fc52SDaniel Fojt if (!tls13_hkdf_expand_label(&finished_key, ctx->hash,
1017cca6fc52SDaniel Fojt &secrets->client_handshake_traffic, "finished",
1018cca6fc52SDaniel Fojt &context))
1019cca6fc52SDaniel Fojt goto err;
1020cca6fc52SDaniel Fojt
1021cca6fc52SDaniel Fojt if (!tls1_transcript_hash_value(ctx->ssl, transcript_hash,
1022cca6fc52SDaniel Fojt sizeof(transcript_hash), &transcript_hash_len))
1023cca6fc52SDaniel Fojt goto err;
1024cca6fc52SDaniel Fojt
1025cca6fc52SDaniel Fojt if ((hmac_ctx = HMAC_CTX_new()) == NULL)
1026cca6fc52SDaniel Fojt goto err;
1027cca6fc52SDaniel Fojt if (!HMAC_Init_ex(hmac_ctx, finished_key.data, finished_key.len,
1028cca6fc52SDaniel Fojt ctx->hash, NULL))
1029cca6fc52SDaniel Fojt goto err;
1030cca6fc52SDaniel Fojt if (!HMAC_Update(hmac_ctx, transcript_hash, transcript_hash_len))
1031cca6fc52SDaniel Fojt goto err;
1032cca6fc52SDaniel Fojt
1033*de0e0e4dSAntonio Huete Jimenez verify_data_len = HMAC_size(hmac_ctx);
1034*de0e0e4dSAntonio Huete Jimenez if (!CBB_add_space(cbb, &verify_data, verify_data_len))
1035cca6fc52SDaniel Fojt goto err;
1036cca6fc52SDaniel Fojt if (!HMAC_Final(hmac_ctx, verify_data, &hlen))
1037cca6fc52SDaniel Fojt goto err;
1038*de0e0e4dSAntonio Huete Jimenez if (hlen != verify_data_len)
1039*de0e0e4dSAntonio Huete Jimenez goto err;
1040*de0e0e4dSAntonio Huete Jimenez
1041*de0e0e4dSAntonio Huete Jimenez CBS_init(&cbs, verify_data, verify_data_len);
1042*de0e0e4dSAntonio Huete Jimenez if (!CBS_write_bytes(&cbs, ctx->hs->finished,
1043*de0e0e4dSAntonio Huete Jimenez sizeof(ctx->hs->finished), &ctx->hs->finished_len))
1044cca6fc52SDaniel Fojt goto err;
1045cca6fc52SDaniel Fojt
1046cca6fc52SDaniel Fojt ret = 1;
1047cca6fc52SDaniel Fojt
1048cca6fc52SDaniel Fojt err:
1049*de0e0e4dSAntonio Huete Jimenez tls13_secret_cleanup(&finished_key);
1050cca6fc52SDaniel Fojt HMAC_CTX_free(hmac_ctx);
1051cca6fc52SDaniel Fojt
1052cca6fc52SDaniel Fojt return ret;
1053cca6fc52SDaniel Fojt }
1054cca6fc52SDaniel Fojt
1055cca6fc52SDaniel Fojt int
tls13_client_finished_sent(struct tls13_ctx * ctx)1056cca6fc52SDaniel Fojt tls13_client_finished_sent(struct tls13_ctx *ctx)
1057cca6fc52SDaniel Fojt {
1058*de0e0e4dSAntonio Huete Jimenez struct tls13_secrets *secrets = ctx->hs->tls13.secrets;
1059cca6fc52SDaniel Fojt
1060cca6fc52SDaniel Fojt /*
1061cca6fc52SDaniel Fojt * Any records following the client finished message must be encrypted
1062cca6fc52SDaniel Fojt * using the client application traffic keys.
1063cca6fc52SDaniel Fojt */
1064cca6fc52SDaniel Fojt return tls13_record_layer_set_write_traffic_key(ctx->rl,
1065*de0e0e4dSAntonio Huete Jimenez &secrets->client_application_traffic, ssl_encryption_application);
1066cca6fc52SDaniel Fojt }
1067