/src/libressl/ssl/tls13_server.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* $OpenBSD: tls13_server.c,v 1.100 2022/07/24 14:16:29 jsing Exp $ */ |
2 | | /* |
3 | | * Copyright (c) 2019, 2020 Joel Sing <jsing@openbsd.org> |
4 | | * Copyright (c) 2020 Bob Beck <beck@openbsd.org> |
5 | | * |
6 | | * Permission to use, copy, modify, and distribute this software for any |
7 | | * purpose with or without fee is hereby granted, provided that the above |
8 | | * copyright notice and this permission notice appear in all copies. |
9 | | * |
10 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
11 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
12 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR |
13 | | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
14 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
15 | | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
16 | | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
17 | | */ |
18 | | |
19 | | #include <openssl/x509v3.h> |
20 | | |
21 | | #include "ssl_locl.h" |
22 | | #include "ssl_sigalgs.h" |
23 | | #include "ssl_tlsext.h" |
24 | | #include "tls13_handshake.h" |
25 | | #include "tls13_internal.h" |
26 | | |
27 | | int |
28 | | tls13_server_init(struct tls13_ctx *ctx) |
29 | 6.07k | { |
30 | 6.07k | SSL *s = ctx->ssl; |
31 | | |
32 | 6.07k | if (!ssl_supported_tls_version_range(s, &ctx->hs->our_min_tls_version, |
33 | 6.07k | &ctx->hs->our_max_tls_version)) { |
34 | 0 | SSLerror(s, SSL_R_NO_PROTOCOLS_AVAILABLE); |
35 | 0 | return 0; |
36 | 0 | } |
37 | 6.07k | s->version = ctx->hs->our_max_tls_version; |
38 | | |
39 | 6.07k | tls13_record_layer_set_retry_after_phh(ctx->rl, |
40 | 6.07k | (s->internal->mode & SSL_MODE_AUTO_RETRY) != 0); |
41 | | |
42 | 6.07k | if (!ssl_get_new_session(s, 0)) /* XXX */ |
43 | 0 | return 0; |
44 | | |
45 | 6.07k | tls13_record_layer_set_legacy_version(ctx->rl, TLS1_VERSION); |
46 | | |
47 | 6.07k | if (!tls1_transcript_init(s)) |
48 | 0 | return 0; |
49 | | |
50 | 6.07k | arc4random_buf(s->s3->server_random, SSL3_RANDOM_SIZE); |
51 | | |
52 | 6.07k | return 1; |
53 | 6.07k | } |
54 | | |
55 | | int |
56 | | tls13_server_accept(struct tls13_ctx *ctx) |
57 | 6.07k | { |
58 | 6.07k | if (ctx->mode != TLS13_HS_SERVER) |
59 | 0 | return TLS13_IO_FAILURE; |
60 | | |
61 | 6.07k | return tls13_handshake_perform(ctx); |
62 | 6.07k | } |
63 | | |
64 | | static int |
65 | | tls13_client_hello_is_legacy(CBS *cbs) |
66 | 5.70k | { |
67 | 5.70k | CBS extensions_block, extensions, extension_data, versions; |
68 | 5.70k | uint16_t version, max_version = 0; |
69 | 5.70k | uint16_t type; |
70 | | |
71 | 5.70k | CBS_dup(cbs, &extensions_block); |
72 | | |
73 | 5.70k | if (!CBS_get_u16_length_prefixed(&extensions_block, &extensions)) |
74 | 1.02k | return 1; |
75 | | |
76 | 13.9k | while (CBS_len(&extensions) > 0) { |
77 | 9.41k | if (!CBS_get_u16(&extensions, &type)) |
78 | 8 | return 1; |
79 | 9.40k | if (!CBS_get_u16_length_prefixed(&extensions, &extension_data)) |
80 | 64 | return 1; |
81 | | |
82 | 9.33k | if (type != TLSEXT_TYPE_supported_versions) |
83 | 9.00k | continue; |
84 | 337 | if (!CBS_get_u8_length_prefixed(&extension_data, &versions)) |
85 | 4 | return 1; |
86 | 2.32k | while (CBS_len(&versions) > 0) { |
87 | 2.00k | if (!CBS_get_u16(&versions, &version)) |
88 | 13 | return 1; |
89 | 1.98k | if (version >= max_version) |
90 | 803 | max_version = version; |
91 | 1.98k | } |
92 | 320 | if (CBS_len(&extension_data) != 0) |
93 | 6 | return 1; |
94 | 320 | } |
95 | | |
96 | 4.58k | return (max_version < TLS1_3_VERSION); |
97 | 4.68k | } |
98 | | |
99 | | int |
100 | | tls13_client_hello_required_extensions(struct tls13_ctx *ctx) |
101 | 201 | { |
102 | 201 | SSL *s = ctx->ssl; |
103 | | |
104 | | /* |
105 | | * RFC 8446, section 9.2. If the ClientHello has supported_versions |
106 | | * containing TLSv1.3, presence or absence of some extensions requires |
107 | | * presence or absence of others. |
108 | | */ |
109 | | |
110 | | /* |
111 | | * RFC 8446 section 4.2.9 - if we received a pre_shared_key, then we |
112 | | * also need psk_key_exchange_modes. Otherwise, section 9.2 specifies |
113 | | * that we need both signature_algorithms and supported_groups. |
114 | | */ |
115 | 201 | if (tlsext_extension_seen(s, TLSEXT_TYPE_pre_shared_key)) { |
116 | 13 | if (!tlsext_extension_seen(s, |
117 | 13 | TLSEXT_TYPE_psk_key_exchange_modes)) |
118 | 1 | return 0; |
119 | 188 | } else { |
120 | 188 | if (!tlsext_extension_seen(s, TLSEXT_TYPE_signature_algorithms)) |
121 | 7 | return 0; |
122 | 181 | if (!tlsext_extension_seen(s, TLSEXT_TYPE_supported_groups)) |
123 | 1 | return 0; |
124 | 181 | } |
125 | | |
126 | | /* |
127 | | * supported_groups and key_share must either both be present or |
128 | | * both be absent. |
129 | | */ |
130 | 192 | if (tlsext_extension_seen(s, TLSEXT_TYPE_supported_groups) != |
131 | 192 | tlsext_extension_seen(s, TLSEXT_TYPE_key_share)) |
132 | 2 | return 0; |
133 | | |
134 | | /* |
135 | | * XXX - Require server_name from client? If so, we SHOULD enforce |
136 | | * this here - RFC 8446, 9.2. |
137 | | */ |
138 | | |
139 | 190 | return 1; |
140 | 192 | } |
141 | | |
142 | | static const uint8_t tls13_compression_null_only[] = { 0 }; |
143 | | |
144 | | static int |
145 | | tls13_client_hello_process(struct tls13_ctx *ctx, CBS *cbs) |
146 | 5.72k | { |
147 | 5.72k | CBS cipher_suites, client_random, compression_methods, session_id; |
148 | 5.72k | STACK_OF(SSL_CIPHER) *ciphers = NULL; |
149 | 5.72k | const SSL_CIPHER *cipher; |
150 | 5.72k | uint16_t legacy_version; |
151 | 5.72k | int alert_desc; |
152 | 5.72k | SSL *s = ctx->ssl; |
153 | 5.72k | int ret = 0; |
154 | | |
155 | 5.72k | if (!CBS_get_u16(cbs, &legacy_version)) |
156 | 5 | goto err; |
157 | 5.72k | if (!CBS_get_bytes(cbs, &client_random, SSL3_RANDOM_SIZE)) |
158 | 2 | goto err; |
159 | 5.72k | if (!CBS_get_u8_length_prefixed(cbs, &session_id)) |
160 | 8 | goto err; |
161 | 5.71k | if (!CBS_get_u16_length_prefixed(cbs, &cipher_suites)) |
162 | 10 | goto err; |
163 | 5.70k | if (!CBS_get_u8_length_prefixed(cbs, &compression_methods)) |
164 | 1 | goto err; |
165 | | |
166 | 5.70k | if (tls13_client_hello_is_legacy(cbs) || s->version < TLS1_3_VERSION) { |
167 | 5.42k | if (!CBS_skip(cbs, CBS_len(cbs))) |
168 | 0 | goto err; |
169 | 5.42k | return tls13_use_legacy_server(ctx); |
170 | 5.42k | } |
171 | 274 | ctx->hs->negotiated_tls_version = TLS1_3_VERSION; |
172 | 274 | ctx->hs->peer_legacy_version = legacy_version; |
173 | | |
174 | | /* Ensure we send subsequent alerts with the correct record version. */ |
175 | 274 | tls13_record_layer_set_legacy_version(ctx->rl, TLS1_2_VERSION); |
176 | | |
177 | | /* Add decoded values to the current ClientHello hash */ |
178 | 274 | if (!tls13_clienthello_hash_init(ctx)) { |
179 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
180 | 0 | goto err; |
181 | 0 | } |
182 | 274 | if (!tls13_clienthello_hash_update_bytes(ctx, (void *)&legacy_version, |
183 | 274 | sizeof(legacy_version))) { |
184 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
185 | 0 | goto err; |
186 | 0 | } |
187 | 274 | if (!tls13_clienthello_hash_update(ctx, &client_random)) { |
188 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
189 | 0 | goto err; |
190 | 0 | } |
191 | 274 | if (!tls13_clienthello_hash_update(ctx, &session_id)) { |
192 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
193 | 0 | goto err; |
194 | 0 | } |
195 | 274 | if (!tls13_clienthello_hash_update(ctx, &cipher_suites)) { |
196 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
197 | 0 | goto err; |
198 | 0 | } |
199 | 274 | if (!tls13_clienthello_hash_update(ctx, &compression_methods)) { |
200 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
201 | 0 | goto err; |
202 | 0 | } |
203 | | |
204 | 274 | if (!tlsext_server_parse(s, SSL_TLSEXT_MSG_CH, cbs, &alert_desc)) { |
205 | 52 | ctx->alert = alert_desc; |
206 | 52 | goto err; |
207 | 52 | } |
208 | | |
209 | | /* Finalize first ClientHello hash, or validate against it */ |
210 | 222 | if (!ctx->hs->tls13.hrr) { |
211 | 196 | if (!tls13_clienthello_hash_finalize(ctx)) { |
212 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
213 | 0 | goto err; |
214 | 0 | } |
215 | 196 | } else { |
216 | 26 | if (!tls13_clienthello_hash_validate(ctx)) { |
217 | 21 | ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER; |
218 | 21 | goto err; |
219 | 21 | } |
220 | 5 | tls13_clienthello_hash_clear(&ctx->hs->tls13); |
221 | 5 | } |
222 | | |
223 | 201 | if (!tls13_client_hello_required_extensions(ctx)) { |
224 | 11 | ctx->alert = TLS13_ALERT_MISSING_EXTENSION; |
225 | 11 | goto err; |
226 | 11 | } |
227 | | |
228 | | /* |
229 | | * If we got this far we have a supported versions extension that offers |
230 | | * TLS 1.3 or later. This requires the legacy version be set to 0x0303. |
231 | | */ |
232 | 190 | if (legacy_version != TLS1_2_VERSION) { |
233 | 12 | ctx->alert = TLS13_ALERT_PROTOCOL_VERSION; |
234 | 12 | goto err; |
235 | 12 | } |
236 | | |
237 | | /* Store legacy session identifier so we can echo it. */ |
238 | 178 | if (CBS_len(&session_id) > sizeof(ctx->hs->tls13.legacy_session_id)) { |
239 | 1 | ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER; |
240 | 1 | goto err; |
241 | 1 | } |
242 | 177 | if (!CBS_write_bytes(&session_id, ctx->hs->tls13.legacy_session_id, |
243 | 177 | sizeof(ctx->hs->tls13.legacy_session_id), |
244 | 177 | &ctx->hs->tls13.legacy_session_id_len)) { |
245 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
246 | 0 | goto err; |
247 | 0 | } |
248 | | |
249 | | /* Parse cipher suites list and select preferred cipher. */ |
250 | 177 | if ((ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites)) == NULL) { |
251 | 1 | ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER; |
252 | 1 | goto err; |
253 | 1 | } |
254 | 176 | cipher = ssl3_choose_cipher(s, ciphers, SSL_get_ciphers(s)); |
255 | 176 | if (cipher == NULL) { |
256 | 11 | tls13_set_errorx(ctx, TLS13_ERR_NO_SHARED_CIPHER, 0, |
257 | 11 | "no shared cipher found", NULL); |
258 | 11 | ctx->alert = TLS13_ALERT_HANDSHAKE_FAILURE; |
259 | 11 | goto err; |
260 | 11 | } |
261 | 165 | ctx->hs->cipher = cipher; |
262 | | |
263 | 165 | sk_SSL_CIPHER_free(s->session->ciphers); |
264 | 165 | s->session->ciphers = ciphers; |
265 | 165 | ciphers = NULL; |
266 | | |
267 | | /* Ensure only the NULL compression method is advertised. */ |
268 | 165 | if (!CBS_mem_equal(&compression_methods, tls13_compression_null_only, |
269 | 165 | sizeof(tls13_compression_null_only))) { |
270 | 3 | ctx->alert = TLS13_ALERT_ILLEGAL_PARAMETER; |
271 | 3 | goto err; |
272 | 3 | } |
273 | | |
274 | 162 | ret = 1; |
275 | | |
276 | 300 | err: |
277 | 300 | sk_SSL_CIPHER_free(ciphers); |
278 | | |
279 | 300 | return ret; |
280 | 162 | } |
281 | | |
282 | | int |
283 | | tls13_client_hello_recv(struct tls13_ctx *ctx, CBS *cbs) |
284 | 5.67k | { |
285 | 5.67k | SSL *s = ctx->ssl; |
286 | | |
287 | 5.67k | if (!tls13_client_hello_process(ctx, cbs)) |
288 | 110 | goto err; |
289 | | |
290 | | /* See if we switched back to the legacy client method. */ |
291 | 5.56k | if (s->method->version < TLS1_3_VERSION) |
292 | 5.40k | return 1; |
293 | | |
294 | | /* |
295 | | * If a matching key share was provided, we do not need to send a |
296 | | * HelloRetryRequest. |
297 | | */ |
298 | | /* |
299 | | * XXX - ideally NEGOTIATED would only be added after record protection |
300 | | * has been enabled. This would probably mean using either an |
301 | | * INITIAL | WITHOUT_HRR state, or another intermediate state. |
302 | | */ |
303 | 157 | if (ctx->hs->key_share != NULL) |
304 | 85 | ctx->handshake_stage.hs_type |= NEGOTIATED | WITHOUT_HRR; |
305 | | |
306 | | /* XXX - check this is the correct point */ |
307 | 157 | tls13_record_layer_allow_ccs(ctx->rl, 1); |
308 | | |
309 | 157 | return 1; |
310 | | |
311 | 110 | err: |
312 | 110 | return 0; |
313 | 5.56k | } |
314 | | |
315 | | static int |
316 | | tls13_server_hello_build(struct tls13_ctx *ctx, CBB *cbb, int hrr) |
317 | 158 | { |
318 | 158 | uint16_t tlsext_msg_type = SSL_TLSEXT_MSG_SH; |
319 | 158 | const uint8_t *server_random; |
320 | 158 | CBB session_id; |
321 | 158 | SSL *s = ctx->ssl; |
322 | 158 | uint16_t cipher; |
323 | | |
324 | 158 | cipher = SSL_CIPHER_get_value(ctx->hs->cipher); |
325 | 158 | server_random = s->s3->server_random; |
326 | | |
327 | 158 | if (hrr) { |
328 | 70 | server_random = tls13_hello_retry_request_hash; |
329 | 70 | tlsext_msg_type = SSL_TLSEXT_MSG_HRR; |
330 | 70 | } |
331 | | |
332 | 158 | if (!CBB_add_u16(cbb, TLS1_2_VERSION)) |
333 | 0 | goto err; |
334 | 158 | if (!CBB_add_bytes(cbb, server_random, SSL3_RANDOM_SIZE)) |
335 | 0 | goto err; |
336 | 158 | if (!CBB_add_u8_length_prefixed(cbb, &session_id)) |
337 | 0 | goto err; |
338 | 158 | if (!CBB_add_bytes(&session_id, ctx->hs->tls13.legacy_session_id, |
339 | 158 | ctx->hs->tls13.legacy_session_id_len)) |
340 | 0 | goto err; |
341 | 158 | if (!CBB_add_u16(cbb, cipher)) |
342 | 0 | goto err; |
343 | 158 | if (!CBB_add_u8(cbb, 0)) |
344 | 0 | goto err; |
345 | 158 | if (!tlsext_server_build(s, tlsext_msg_type, cbb)) |
346 | 0 | goto err; |
347 | | |
348 | 158 | if (!CBB_flush(cbb)) |
349 | 0 | goto err; |
350 | | |
351 | 158 | return 1; |
352 | 0 | err: |
353 | 0 | return 0; |
354 | 158 | } |
355 | | |
356 | | static int |
357 | | tls13_server_engage_record_protection(struct tls13_ctx *ctx) |
358 | 88 | { |
359 | 88 | struct tls13_secrets *secrets; |
360 | 88 | struct tls13_secret context; |
361 | 88 | unsigned char buf[EVP_MAX_MD_SIZE]; |
362 | 88 | uint8_t *shared_key = NULL; |
363 | 88 | size_t shared_key_len = 0; |
364 | 88 | size_t hash_len; |
365 | 88 | SSL *s = ctx->ssl; |
366 | 88 | int ret = 0; |
367 | | |
368 | 88 | if (!tls_key_share_derive(ctx->hs->key_share, &shared_key, |
369 | 88 | &shared_key_len)) |
370 | 2 | goto err; |
371 | | |
372 | 86 | s->session->cipher = ctx->hs->cipher; |
373 | | |
374 | 86 | if ((ctx->aead = tls13_cipher_aead(ctx->hs->cipher)) == NULL) |
375 | 0 | goto err; |
376 | 86 | if ((ctx->hash = tls13_cipher_hash(ctx->hs->cipher)) == NULL) |
377 | 0 | goto err; |
378 | | |
379 | 86 | if ((secrets = tls13_secrets_create(ctx->hash, 0)) == NULL) |
380 | 0 | goto err; |
381 | 86 | ctx->hs->tls13.secrets = secrets; |
382 | | |
383 | | /* XXX - pass in hash. */ |
384 | 86 | if (!tls1_transcript_hash_init(s)) |
385 | 0 | goto err; |
386 | 86 | tls1_transcript_free(s); |
387 | 86 | if (!tls1_transcript_hash_value(s, buf, sizeof(buf), &hash_len)) |
388 | 0 | goto err; |
389 | 86 | context.data = buf; |
390 | 86 | context.len = hash_len; |
391 | | |
392 | | /* Early secrets. */ |
393 | 86 | if (!tls13_derive_early_secrets(secrets, secrets->zeros.data, |
394 | 86 | secrets->zeros.len, &context)) |
395 | 0 | goto err; |
396 | | |
397 | | /* Handshake secrets. */ |
398 | 86 | if (!tls13_derive_handshake_secrets(ctx->hs->tls13.secrets, shared_key, |
399 | 86 | shared_key_len, &context)) |
400 | 0 | goto err; |
401 | | |
402 | 86 | tls13_record_layer_set_aead(ctx->rl, ctx->aead); |
403 | 86 | tls13_record_layer_set_hash(ctx->rl, ctx->hash); |
404 | | |
405 | 86 | if (!tls13_record_layer_set_read_traffic_key(ctx->rl, |
406 | 86 | &secrets->client_handshake_traffic, ssl_encryption_handshake)) |
407 | 0 | goto err; |
408 | 86 | if (!tls13_record_layer_set_write_traffic_key(ctx->rl, |
409 | 86 | &secrets->server_handshake_traffic, ssl_encryption_handshake)) |
410 | 0 | goto err; |
411 | | |
412 | 86 | ctx->handshake_stage.hs_type |= NEGOTIATED; |
413 | 86 | if (!(SSL_get_verify_mode(s) & SSL_VERIFY_PEER)) |
414 | 86 | ctx->handshake_stage.hs_type |= WITHOUT_CR; |
415 | | |
416 | 86 | ret = 1; |
417 | | |
418 | 88 | err: |
419 | 88 | freezero(shared_key, shared_key_len); |
420 | 88 | return ret; |
421 | 86 | } |
422 | | |
423 | | int |
424 | | tls13_server_hello_retry_request_send(struct tls13_ctx *ctx, CBB *cbb) |
425 | 72 | { |
426 | 72 | int nid; |
427 | | |
428 | 72 | ctx->hs->tls13.hrr = 1; |
429 | | |
430 | 72 | if (!tls13_synthetic_handshake_message(ctx)) |
431 | 0 | return 0; |
432 | | |
433 | 72 | if (ctx->hs->key_share != NULL) |
434 | 0 | return 0; |
435 | 72 | if (!tls1_get_supported_group(ctx->ssl, &nid)) |
436 | 2 | return 0; |
437 | 70 | if (!tls1_ec_nid2group_id(nid, &ctx->hs->tls13.server_group)) |
438 | 0 | return 0; |
439 | | |
440 | 70 | if (!tls13_server_hello_build(ctx, cbb, 1)) |
441 | 0 | return 0; |
442 | | |
443 | 70 | return 1; |
444 | 70 | } |
445 | | |
446 | | int |
447 | | tls13_server_hello_retry_request_sent(struct tls13_ctx *ctx) |
448 | 70 | { |
449 | | /* |
450 | | * If the client has requested middlebox compatibility mode, |
451 | | * we MUST send a dummy CCS following our first handshake message. |
452 | | * See RFC 8446 Appendix D.4. |
453 | | */ |
454 | 70 | if (ctx->hs->tls13.legacy_session_id_len > 0) |
455 | 69 | ctx->send_dummy_ccs_after = 1; |
456 | | |
457 | 70 | return 1; |
458 | 70 | } |
459 | | |
460 | | int |
461 | | tls13_client_hello_retry_recv(struct tls13_ctx *ctx, CBS *cbs) |
462 | 55 | { |
463 | 55 | SSL *s = ctx->ssl; |
464 | | |
465 | 55 | if (!tls13_client_hello_process(ctx, cbs)) |
466 | 28 | return 0; |
467 | | |
468 | | /* XXX - need further checks. */ |
469 | 27 | if (s->method->version < TLS1_3_VERSION) |
470 | 22 | return 0; |
471 | | |
472 | 5 | ctx->hs->tls13.hrr = 0; |
473 | | |
474 | 5 | return 1; |
475 | 27 | } |
476 | | |
477 | | static int |
478 | | tls13_servername_process(struct tls13_ctx *ctx) |
479 | 88 | { |
480 | 88 | uint8_t alert = TLS13_ALERT_INTERNAL_ERROR; |
481 | | |
482 | 88 | if (!tls13_legacy_servername_process(ctx, &alert)) { |
483 | 0 | ctx->alert = alert; |
484 | 0 | return 0; |
485 | 0 | } |
486 | | |
487 | 88 | return 1; |
488 | 88 | } |
489 | | |
490 | | int |
491 | | tls13_server_hello_send(struct tls13_ctx *ctx, CBB *cbb) |
492 | 89 | { |
493 | 89 | if (ctx->hs->key_share == NULL) |
494 | 1 | return 0; |
495 | 88 | if (!tls_key_share_generate(ctx->hs->key_share)) |
496 | 0 | return 0; |
497 | 88 | if (!tls13_servername_process(ctx)) |
498 | 0 | return 0; |
499 | | |
500 | 88 | ctx->hs->tls13.server_group = 0; |
501 | | |
502 | 88 | if (!tls13_server_hello_build(ctx, cbb, 0)) |
503 | 0 | return 0; |
504 | | |
505 | 88 | return 1; |
506 | 88 | } |
507 | | |
508 | | int |
509 | | tls13_server_hello_sent(struct tls13_ctx *ctx) |
510 | 88 | { |
511 | | /* |
512 | | * If the client has requested middlebox compatibility mode, |
513 | | * we MUST send a dummy CCS following our first handshake message. |
514 | | * See RFC 8446 Appendix D.4. |
515 | | */ |
516 | 88 | if ((ctx->handshake_stage.hs_type & WITHOUT_HRR) && |
517 | 88 | ctx->hs->tls13.legacy_session_id_len > 0) |
518 | 54 | ctx->send_dummy_ccs_after = 1; |
519 | | |
520 | 88 | return tls13_server_engage_record_protection(ctx); |
521 | 88 | } |
522 | | |
523 | | int |
524 | | tls13_server_encrypted_extensions_send(struct tls13_ctx *ctx, CBB *cbb) |
525 | 86 | { |
526 | 86 | if (!tlsext_server_build(ctx->ssl, SSL_TLSEXT_MSG_EE, cbb)) |
527 | 0 | goto err; |
528 | | |
529 | 86 | return 1; |
530 | 0 | err: |
531 | 0 | return 0; |
532 | 86 | } |
533 | | |
534 | | int |
535 | | tls13_server_certificate_request_send(struct tls13_ctx *ctx, CBB *cbb) |
536 | 0 | { |
537 | 0 | CBB certificate_request_context; |
538 | |
|
539 | 0 | if (!CBB_add_u8_length_prefixed(cbb, &certificate_request_context)) |
540 | 0 | goto err; |
541 | 0 | if (!tlsext_server_build(ctx->ssl, SSL_TLSEXT_MSG_CR, cbb)) |
542 | 0 | goto err; |
543 | | |
544 | 0 | if (!CBB_flush(cbb)) |
545 | 0 | goto err; |
546 | | |
547 | 0 | return 1; |
548 | 0 | err: |
549 | 0 | return 0; |
550 | 0 | } |
551 | | |
552 | | static int |
553 | | tls13_server_check_certificate(struct tls13_ctx *ctx, SSL_CERT_PKEY *cpk, |
554 | | int *ok, const struct ssl_sigalg **out_sigalg) |
555 | 135 | { |
556 | 135 | const struct ssl_sigalg *sigalg; |
557 | 135 | SSL *s = ctx->ssl; |
558 | | |
559 | 135 | *ok = 0; |
560 | 135 | *out_sigalg = NULL; |
561 | | |
562 | 135 | if (cpk->x509 == NULL || cpk->privatekey == NULL) |
563 | 0 | goto done; |
564 | | |
565 | | /* |
566 | | * The digitalSignature bit MUST be set if the Key Usage extension is |
567 | | * present as per RFC 8446 section 4.4.2.2. |
568 | | */ |
569 | 135 | if (!(X509_get_key_usage(cpk->x509) & X509v3_KU_DIGITAL_SIGNATURE)) |
570 | 0 | goto done; |
571 | | |
572 | 135 | if ((sigalg = ssl_sigalg_select(s, cpk->privatekey)) == NULL) |
573 | 72 | goto done; |
574 | | |
575 | 63 | *ok = 1; |
576 | 63 | *out_sigalg = sigalg; |
577 | | |
578 | 135 | done: |
579 | 135 | return 1; |
580 | 63 | } |
581 | | |
582 | | static int |
583 | | tls13_server_select_certificate(struct tls13_ctx *ctx, SSL_CERT_PKEY **out_cpk, |
584 | | const struct ssl_sigalg **out_sigalg) |
585 | 86 | { |
586 | 86 | SSL *s = ctx->ssl; |
587 | 86 | const struct ssl_sigalg *sigalg; |
588 | 86 | SSL_CERT_PKEY *cpk; |
589 | 86 | int cert_ok; |
590 | | |
591 | 86 | *out_cpk = NULL; |
592 | 86 | *out_sigalg = NULL; |
593 | | |
594 | 86 | cpk = &s->cert->pkeys[SSL_PKEY_ECC]; |
595 | 86 | if (!tls13_server_check_certificate(ctx, cpk, &cert_ok, &sigalg)) |
596 | 0 | return 0; |
597 | 86 | if (cert_ok) |
598 | 37 | goto done; |
599 | | |
600 | 49 | cpk = &s->cert->pkeys[SSL_PKEY_RSA]; |
601 | 49 | if (!tls13_server_check_certificate(ctx, cpk, &cert_ok, &sigalg)) |
602 | 0 | return 0; |
603 | 49 | if (cert_ok) |
604 | 26 | goto done; |
605 | | |
606 | 23 | cpk = NULL; |
607 | 23 | sigalg = NULL; |
608 | | |
609 | 86 | done: |
610 | 86 | *out_cpk = cpk; |
611 | 86 | *out_sigalg = sigalg; |
612 | | |
613 | 86 | return 1; |
614 | 23 | } |
615 | | |
616 | | int |
617 | | tls13_server_certificate_send(struct tls13_ctx *ctx, CBB *cbb) |
618 | 86 | { |
619 | 86 | SSL *s = ctx->ssl; |
620 | 86 | CBB cert_request_context, cert_list; |
621 | 86 | const struct ssl_sigalg *sigalg; |
622 | 86 | X509_STORE_CTX *xsc = NULL; |
623 | 86 | STACK_OF(X509) *chain; |
624 | 86 | SSL_CERT_PKEY *cpk; |
625 | 86 | X509 *cert; |
626 | 86 | int i, ret = 0; |
627 | | |
628 | 86 | if (!tls13_server_select_certificate(ctx, &cpk, &sigalg)) |
629 | 0 | goto err; |
630 | | |
631 | 86 | if (cpk == NULL) { |
632 | | /* A server must always provide a certificate. */ |
633 | 23 | ctx->alert = TLS13_ALERT_HANDSHAKE_FAILURE; |
634 | 23 | tls13_set_errorx(ctx, TLS13_ERR_NO_CERTIFICATE, 0, |
635 | 23 | "no server certificate", NULL); |
636 | 23 | goto err; |
637 | 23 | } |
638 | | |
639 | 63 | ctx->hs->tls13.cpk = cpk; |
640 | 63 | ctx->hs->our_sigalg = sigalg; |
641 | | |
642 | 63 | if ((chain = cpk->chain) == NULL) |
643 | 63 | chain = s->ctx->extra_certs; |
644 | | |
645 | 63 | if (chain == NULL && !(s->internal->mode & SSL_MODE_NO_AUTO_CHAIN)) { |
646 | 63 | if ((xsc = X509_STORE_CTX_new()) == NULL) |
647 | 0 | goto err; |
648 | 63 | if (!X509_STORE_CTX_init(xsc, s->ctx->cert_store, cpk->x509, NULL)) |
649 | 0 | goto err; |
650 | 63 | X509_VERIFY_PARAM_set_flags(X509_STORE_CTX_get0_param(xsc), |
651 | 63 | X509_V_FLAG_LEGACY_VERIFY); |
652 | 63 | X509_verify_cert(xsc); |
653 | 63 | ERR_clear_error(); |
654 | 63 | chain = X509_STORE_CTX_get0_chain(xsc); |
655 | 63 | } |
656 | | |
657 | 63 | if (!CBB_add_u8_length_prefixed(cbb, &cert_request_context)) |
658 | 0 | goto err; |
659 | 63 | if (!CBB_add_u24_length_prefixed(cbb, &cert_list)) |
660 | 0 | goto err; |
661 | | |
662 | 63 | if (!tls13_cert_add(ctx, &cert_list, cpk->x509, tlsext_server_build)) |
663 | 0 | goto err; |
664 | | |
665 | 126 | for (i = 0; i < sk_X509_num(chain); i++) { |
666 | 63 | cert = sk_X509_value(chain, i); |
667 | | |
668 | | /* |
669 | | * In the case of auto chain, the leaf certificate will be at |
670 | | * the top of the chain - skip over it as we've already added |
671 | | * it earlier. |
672 | | */ |
673 | 63 | if (i == 0 && cert == cpk->x509) |
674 | 63 | continue; |
675 | | |
676 | | /* |
677 | | * XXX we don't send extensions with chain certs to avoid sending |
678 | | * a leaf ocsp staple with the chain certs. This needs to get |
679 | | * fixed. |
680 | | */ |
681 | 0 | if (!tls13_cert_add(ctx, &cert_list, cert, NULL)) |
682 | 0 | goto err; |
683 | 0 | } |
684 | | |
685 | 63 | if (!CBB_flush(cbb)) |
686 | 0 | goto err; |
687 | | |
688 | 63 | ret = 1; |
689 | | |
690 | 86 | err: |
691 | 86 | X509_STORE_CTX_free(xsc); |
692 | | |
693 | 86 | return ret; |
694 | 63 | } |
695 | | |
696 | | int |
697 | | tls13_server_certificate_verify_send(struct tls13_ctx *ctx, CBB *cbb) |
698 | 63 | { |
699 | 63 | const struct ssl_sigalg *sigalg; |
700 | 63 | uint8_t *sig = NULL, *sig_content = NULL; |
701 | 63 | size_t sig_len, sig_content_len; |
702 | 63 | EVP_MD_CTX *mdctx = NULL; |
703 | 63 | EVP_PKEY_CTX *pctx; |
704 | 63 | EVP_PKEY *pkey; |
705 | 63 | const SSL_CERT_PKEY *cpk; |
706 | 63 | CBB sig_cbb; |
707 | 63 | int ret = 0; |
708 | | |
709 | 63 | memset(&sig_cbb, 0, sizeof(sig_cbb)); |
710 | | |
711 | 63 | if ((cpk = ctx->hs->tls13.cpk) == NULL) |
712 | 0 | goto err; |
713 | 63 | if ((sigalg = ctx->hs->our_sigalg) == NULL) |
714 | 0 | goto err; |
715 | 63 | pkey = cpk->privatekey; |
716 | | |
717 | 63 | if (!CBB_init(&sig_cbb, 0)) |
718 | 0 | goto err; |
719 | 63 | if (!CBB_add_bytes(&sig_cbb, tls13_cert_verify_pad, |
720 | 63 | sizeof(tls13_cert_verify_pad))) |
721 | 0 | goto err; |
722 | 63 | if (!CBB_add_bytes(&sig_cbb, tls13_cert_server_verify_context, |
723 | 63 | strlen(tls13_cert_server_verify_context))) |
724 | 0 | goto err; |
725 | 63 | if (!CBB_add_u8(&sig_cbb, 0)) |
726 | 0 | goto err; |
727 | 63 | if (!CBB_add_bytes(&sig_cbb, ctx->hs->tls13.transcript_hash, |
728 | 63 | ctx->hs->tls13.transcript_hash_len)) |
729 | 0 | goto err; |
730 | 63 | if (!CBB_finish(&sig_cbb, &sig_content, &sig_content_len)) |
731 | 0 | goto err; |
732 | | |
733 | 63 | if ((mdctx = EVP_MD_CTX_new()) == NULL) |
734 | 0 | goto err; |
735 | 63 | if (!EVP_DigestSignInit(mdctx, &pctx, sigalg->md(), NULL, pkey)) |
736 | 0 | goto err; |
737 | 63 | if (sigalg->flags & SIGALG_FLAG_RSA_PSS) { |
738 | 26 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING)) |
739 | 0 | goto err; |
740 | 26 | if (!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1)) |
741 | 0 | goto err; |
742 | 26 | } |
743 | 63 | if (!EVP_DigestSignUpdate(mdctx, sig_content, sig_content_len)) |
744 | 0 | goto err; |
745 | 63 | if (EVP_DigestSignFinal(mdctx, NULL, &sig_len) <= 0) |
746 | 0 | goto err; |
747 | 63 | if ((sig = calloc(1, sig_len)) == NULL) |
748 | 0 | goto err; |
749 | 63 | if (EVP_DigestSignFinal(mdctx, sig, &sig_len) <= 0) |
750 | 0 | goto err; |
751 | | |
752 | 63 | if (!CBB_add_u16(cbb, sigalg->value)) |
753 | 0 | goto err; |
754 | 63 | if (!CBB_add_u16_length_prefixed(cbb, &sig_cbb)) |
755 | 0 | goto err; |
756 | 63 | if (!CBB_add_bytes(&sig_cbb, sig, sig_len)) |
757 | 0 | goto err; |
758 | | |
759 | 63 | if (!CBB_flush(cbb)) |
760 | 0 | goto err; |
761 | | |
762 | 63 | ret = 1; |
763 | | |
764 | 63 | err: |
765 | 63 | if (!ret && ctx->alert == 0) |
766 | 0 | ctx->alert = TLS13_ALERT_INTERNAL_ERROR; |
767 | | |
768 | 63 | CBB_cleanup(&sig_cbb); |
769 | 63 | EVP_MD_CTX_free(mdctx); |
770 | 63 | free(sig_content); |
771 | 63 | free(sig); |
772 | | |
773 | 63 | return ret; |
774 | 63 | } |
775 | | |
776 | | int |
777 | | tls13_server_finished_send(struct tls13_ctx *ctx, CBB *cbb) |
778 | 63 | { |
779 | 63 | struct tls13_secrets *secrets = ctx->hs->tls13.secrets; |
780 | 63 | struct tls13_secret context = { .data = "", .len = 0 }; |
781 | 63 | struct tls13_secret finished_key = { .data = NULL, .len = 0 } ; |
782 | 63 | uint8_t transcript_hash[EVP_MAX_MD_SIZE]; |
783 | 63 | size_t transcript_hash_len; |
784 | 63 | uint8_t *verify_data; |
785 | 63 | size_t verify_data_len; |
786 | 63 | unsigned int hlen; |
787 | 63 | HMAC_CTX *hmac_ctx = NULL; |
788 | 63 | CBS cbs; |
789 | 63 | int ret = 0; |
790 | | |
791 | 63 | if (!tls13_secret_init(&finished_key, EVP_MD_size(ctx->hash))) |
792 | 0 | goto err; |
793 | | |
794 | 63 | if (!tls13_hkdf_expand_label(&finished_key, ctx->hash, |
795 | 63 | &secrets->server_handshake_traffic, "finished", |
796 | 63 | &context)) |
797 | 0 | goto err; |
798 | | |
799 | 63 | if (!tls1_transcript_hash_value(ctx->ssl, transcript_hash, |
800 | 63 | sizeof(transcript_hash), &transcript_hash_len)) |
801 | 0 | goto err; |
802 | | |
803 | 63 | if ((hmac_ctx = HMAC_CTX_new()) == NULL) |
804 | 0 | goto err; |
805 | 63 | if (!HMAC_Init_ex(hmac_ctx, finished_key.data, finished_key.len, |
806 | 63 | ctx->hash, NULL)) |
807 | 0 | goto err; |
808 | 63 | if (!HMAC_Update(hmac_ctx, transcript_hash, transcript_hash_len)) |
809 | 0 | goto err; |
810 | | |
811 | 63 | verify_data_len = HMAC_size(hmac_ctx); |
812 | 63 | if (!CBB_add_space(cbb, &verify_data, verify_data_len)) |
813 | 0 | goto err; |
814 | 63 | if (!HMAC_Final(hmac_ctx, verify_data, &hlen)) |
815 | 0 | goto err; |
816 | 63 | if (hlen != verify_data_len) |
817 | 0 | goto err; |
818 | | |
819 | 63 | CBS_init(&cbs, verify_data, verify_data_len); |
820 | 63 | if (!CBS_write_bytes(&cbs, ctx->hs->finished, |
821 | 63 | sizeof(ctx->hs->finished), &ctx->hs->finished_len)) |
822 | 0 | goto err; |
823 | | |
824 | 63 | ret = 1; |
825 | | |
826 | 63 | err: |
827 | 63 | tls13_secret_cleanup(&finished_key); |
828 | 63 | HMAC_CTX_free(hmac_ctx); |
829 | | |
830 | 63 | return ret; |
831 | 63 | } |
832 | | |
833 | | int |
834 | | tls13_server_finished_sent(struct tls13_ctx *ctx) |
835 | 63 | { |
836 | 63 | struct tls13_secrets *secrets = ctx->hs->tls13.secrets; |
837 | 63 | struct tls13_secret context = { .data = "", .len = 0 }; |
838 | | |
839 | | /* |
840 | | * Derive application traffic keys. |
841 | | */ |
842 | 63 | context.data = ctx->hs->tls13.transcript_hash; |
843 | 63 | context.len = ctx->hs->tls13.transcript_hash_len; |
844 | | |
845 | 63 | if (!tls13_derive_application_secrets(secrets, &context)) |
846 | 0 | return 0; |
847 | | |
848 | | /* |
849 | | * Any records following the server finished message must be encrypted |
850 | | * using the server application traffic keys. |
851 | | */ |
852 | 63 | return tls13_record_layer_set_write_traffic_key(ctx->rl, |
853 | 63 | &secrets->server_application_traffic, ssl_encryption_application); |
854 | 63 | } |
855 | | |
856 | | int |
857 | | tls13_client_certificate_recv(struct tls13_ctx *ctx, CBS *cbs) |
858 | 0 | { |
859 | 0 | CBS cert_request_context, cert_list, cert_data, cert_exts; |
860 | 0 | struct stack_st_X509 *certs = NULL; |
861 | 0 | SSL *s = ctx->ssl; |
862 | 0 | X509 *cert = NULL; |
863 | 0 | EVP_PKEY *pkey; |
864 | 0 | const uint8_t *p; |
865 | 0 | int cert_type; |
866 | 0 | int ret = 0; |
867 | |
|
868 | 0 | if (!CBS_get_u8_length_prefixed(cbs, &cert_request_context)) |
869 | 0 | goto err; |
870 | 0 | if (CBS_len(&cert_request_context) != 0) |
871 | 0 | goto err; |
872 | 0 | if (!CBS_get_u24_length_prefixed(cbs, &cert_list)) |
873 | 0 | goto err; |
874 | 0 | if (CBS_len(&cert_list) == 0) { |
875 | 0 | if (!(s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) |
876 | 0 | return 1; |
877 | 0 | ctx->alert = TLS13_ALERT_CERTIFICATE_REQUIRED; |
878 | 0 | tls13_set_errorx(ctx, TLS13_ERR_NO_PEER_CERTIFICATE, 0, |
879 | 0 | "peer did not provide a certificate", NULL); |
880 | 0 | goto err; |
881 | 0 | } |
882 | | |
883 | 0 | if ((certs = sk_X509_new_null()) == NULL) |
884 | 0 | goto err; |
885 | 0 | while (CBS_len(&cert_list) > 0) { |
886 | 0 | if (!CBS_get_u24_length_prefixed(&cert_list, &cert_data)) |
887 | 0 | goto err; |
888 | 0 | if (!CBS_get_u16_length_prefixed(&cert_list, &cert_exts)) |
889 | 0 | goto err; |
890 | | |
891 | 0 | p = CBS_data(&cert_data); |
892 | 0 | if ((cert = d2i_X509(NULL, &p, CBS_len(&cert_data))) == NULL) |
893 | 0 | goto err; |
894 | 0 | if (p != CBS_data(&cert_data) + CBS_len(&cert_data)) |
895 | 0 | goto err; |
896 | | |
897 | 0 | if (!sk_X509_push(certs, cert)) |
898 | 0 | goto err; |
899 | | |
900 | 0 | cert = NULL; |
901 | 0 | } |
902 | | |
903 | | /* |
904 | | * At this stage we still have no proof of possession. As such, it would |
905 | | * be preferable to keep the chain and verify once we have successfully |
906 | | * processed the CertificateVerify message. |
907 | | */ |
908 | 0 | if (ssl_verify_cert_chain(s, certs) <= 0) { |
909 | 0 | ctx->alert = ssl_verify_alarm_type(s->verify_result); |
910 | 0 | tls13_set_errorx(ctx, TLS13_ERR_VERIFY_FAILED, 0, |
911 | 0 | "failed to verify peer certificate", NULL); |
912 | 0 | goto err; |
913 | 0 | } |
914 | 0 | ERR_clear_error(); |
915 | | |
916 | | /* |
917 | | * Achtung! Due to API inconsistency, a client includes the peer's leaf |
918 | | * certificate in the stored certificate chain, while a server does not. |
919 | | */ |
920 | 0 | cert = sk_X509_shift(certs); |
921 | |
|
922 | 0 | if ((pkey = X509_get0_pubkey(cert)) == NULL) |
923 | 0 | goto err; |
924 | 0 | if (EVP_PKEY_missing_parameters(pkey)) |
925 | 0 | goto err; |
926 | 0 | if ((cert_type = ssl_cert_type(pkey)) < 0) |
927 | 0 | goto err; |
928 | | |
929 | 0 | X509_up_ref(cert); |
930 | 0 | X509_free(s->session->peer_cert); |
931 | 0 | s->session->peer_cert = cert; |
932 | 0 | s->session->peer_cert_type = cert_type; |
933 | |
|
934 | 0 | s->session->verify_result = s->verify_result; |
935 | |
|
936 | 0 | sk_X509_pop_free(s->session->cert_chain, X509_free); |
937 | 0 | s->session->cert_chain = certs; |
938 | 0 | certs = NULL; |
939 | |
|
940 | 0 | ctx->handshake_stage.hs_type |= WITH_CCV; |
941 | 0 | ret = 1; |
942 | |
|
943 | 0 | err: |
944 | 0 | sk_X509_pop_free(certs, X509_free); |
945 | 0 | X509_free(cert); |
946 | |
|
947 | 0 | return ret; |
948 | 0 | } |
949 | | |
950 | | int |
951 | | tls13_client_certificate_verify_recv(struct tls13_ctx *ctx, CBS *cbs) |
952 | 0 | { |
953 | 0 | const struct ssl_sigalg *sigalg; |
954 | 0 | uint16_t signature_scheme; |
955 | 0 | uint8_t *sig_content = NULL; |
956 | 0 | size_t sig_content_len; |
957 | 0 | EVP_MD_CTX *mdctx = NULL; |
958 | 0 | EVP_PKEY_CTX *pctx; |
959 | 0 | EVP_PKEY *pkey; |
960 | 0 | X509 *cert; |
961 | 0 | CBS signature; |
962 | 0 | CBB cbb; |
963 | 0 | int ret = 0; |
964 | |
|
965 | 0 | memset(&cbb, 0, sizeof(cbb)); |
966 | |
|
967 | 0 | if (!CBS_get_u16(cbs, &signature_scheme)) |
968 | 0 | goto err; |
969 | 0 | if (!CBS_get_u16_length_prefixed(cbs, &signature)) |
970 | 0 | goto err; |
971 | | |
972 | 0 | if (!CBB_init(&cbb, 0)) |
973 | 0 | goto err; |
974 | 0 | if (!CBB_add_bytes(&cbb, tls13_cert_verify_pad, |
975 | 0 | sizeof(tls13_cert_verify_pad))) |
976 | 0 | goto err; |
977 | 0 | if (!CBB_add_bytes(&cbb, tls13_cert_client_verify_context, |
978 | 0 | strlen(tls13_cert_client_verify_context))) |
979 | 0 | goto err; |
980 | 0 | if (!CBB_add_u8(&cbb, 0)) |
981 | 0 | goto err; |
982 | 0 | if (!CBB_add_bytes(&cbb, ctx->hs->tls13.transcript_hash, |
983 | 0 | ctx->hs->tls13.transcript_hash_len)) |
984 | 0 | goto err; |
985 | 0 | if (!CBB_finish(&cbb, &sig_content, &sig_content_len)) |
986 | 0 | goto err; |
987 | | |
988 | 0 | if ((cert = ctx->ssl->session->peer_cert) == NULL) |
989 | 0 | goto err; |
990 | 0 | if ((pkey = X509_get0_pubkey(cert)) == NULL) |
991 | 0 | goto err; |
992 | 0 | if ((sigalg = ssl_sigalg_for_peer(ctx->ssl, pkey, |
993 | 0 | signature_scheme)) == NULL) |
994 | 0 | goto err; |
995 | 0 | ctx->hs->peer_sigalg = sigalg; |
996 | |
|
997 | 0 | if (CBS_len(&signature) > EVP_PKEY_size(pkey)) |
998 | 0 | goto err; |
999 | | |
1000 | 0 | if ((mdctx = EVP_MD_CTX_new()) == NULL) |
1001 | 0 | goto err; |
1002 | 0 | if (!EVP_DigestVerifyInit(mdctx, &pctx, sigalg->md(), NULL, pkey)) |
1003 | 0 | goto err; |
1004 | 0 | if (sigalg->flags & SIGALG_FLAG_RSA_PSS) { |
1005 | 0 | if (!EVP_PKEY_CTX_set_rsa_padding(pctx, RSA_PKCS1_PSS_PADDING)) |
1006 | 0 | goto err; |
1007 | 0 | if (!EVP_PKEY_CTX_set_rsa_pss_saltlen(pctx, -1)) |
1008 | 0 | goto err; |
1009 | 0 | } |
1010 | 0 | if (!EVP_DigestVerifyUpdate(mdctx, sig_content, sig_content_len)) { |
1011 | 0 | ctx->alert = TLS13_ALERT_DECRYPT_ERROR; |
1012 | 0 | goto err; |
1013 | 0 | } |
1014 | 0 | if (EVP_DigestVerifyFinal(mdctx, CBS_data(&signature), |
1015 | 0 | CBS_len(&signature)) <= 0) { |
1016 | 0 | ctx->alert = TLS13_ALERT_DECRYPT_ERROR; |
1017 | 0 | goto err; |
1018 | 0 | } |
1019 | | |
1020 | 0 | ret = 1; |
1021 | |
|
1022 | 0 | err: |
1023 | 0 | if (!ret && ctx->alert == 0) |
1024 | 0 | ctx->alert = TLS13_ALERT_DECODE_ERROR; |
1025 | |
|
1026 | 0 | CBB_cleanup(&cbb); |
1027 | 0 | EVP_MD_CTX_free(mdctx); |
1028 | 0 | free(sig_content); |
1029 | |
|
1030 | 0 | return ret; |
1031 | 0 | } |
1032 | | |
1033 | | int |
1034 | | tls13_client_end_of_early_data_recv(struct tls13_ctx *ctx, CBS *cbs) |
1035 | 0 | { |
1036 | 0 | return 0; |
1037 | 0 | } |
1038 | | |
1039 | | int |
1040 | | tls13_client_finished_recv(struct tls13_ctx *ctx, CBS *cbs) |
1041 | 4 | { |
1042 | 4 | struct tls13_secrets *secrets = ctx->hs->tls13.secrets; |
1043 | 4 | struct tls13_secret context = { .data = "", .len = 0 }; |
1044 | 4 | struct tls13_secret finished_key; |
1045 | 4 | uint8_t *verify_data = NULL; |
1046 | 4 | size_t verify_data_len; |
1047 | 4 | uint8_t key[EVP_MAX_MD_SIZE]; |
1048 | 4 | HMAC_CTX *hmac_ctx = NULL; |
1049 | 4 | unsigned int hlen; |
1050 | 4 | int ret = 0; |
1051 | | |
1052 | | /* |
1053 | | * Verify client finished. |
1054 | | */ |
1055 | 4 | finished_key.data = key; |
1056 | 4 | finished_key.len = EVP_MD_size(ctx->hash); |
1057 | | |
1058 | 4 | if (!tls13_hkdf_expand_label(&finished_key, ctx->hash, |
1059 | 4 | &secrets->client_handshake_traffic, "finished", |
1060 | 4 | &context)) |
1061 | 0 | goto err; |
1062 | | |
1063 | 4 | if ((hmac_ctx = HMAC_CTX_new()) == NULL) |
1064 | 0 | goto err; |
1065 | 4 | if (!HMAC_Init_ex(hmac_ctx, finished_key.data, finished_key.len, |
1066 | 4 | ctx->hash, NULL)) |
1067 | 0 | goto err; |
1068 | 4 | if (!HMAC_Update(hmac_ctx, ctx->hs->tls13.transcript_hash, |
1069 | 4 | ctx->hs->tls13.transcript_hash_len)) |
1070 | 0 | goto err; |
1071 | 4 | verify_data_len = HMAC_size(hmac_ctx); |
1072 | 4 | if ((verify_data = calloc(1, verify_data_len)) == NULL) |
1073 | 0 | goto err; |
1074 | 4 | if (!HMAC_Final(hmac_ctx, verify_data, &hlen)) |
1075 | 0 | goto err; |
1076 | 4 | if (hlen != verify_data_len) |
1077 | 0 | goto err; |
1078 | | |
1079 | 4 | if (!CBS_mem_equal(cbs, verify_data, verify_data_len)) { |
1080 | 4 | ctx->alert = TLS13_ALERT_DECRYPT_ERROR; |
1081 | 4 | goto err; |
1082 | 4 | } |
1083 | | |
1084 | 0 | if (!CBS_write_bytes(cbs, ctx->hs->peer_finished, |
1085 | 0 | sizeof(ctx->hs->peer_finished), |
1086 | 0 | &ctx->hs->peer_finished_len)) |
1087 | 0 | goto err; |
1088 | | |
1089 | 0 | if (!CBS_skip(cbs, verify_data_len)) |
1090 | 0 | goto err; |
1091 | | |
1092 | | /* |
1093 | | * Any records following the client finished message must be encrypted |
1094 | | * using the client application traffic keys. |
1095 | | */ |
1096 | 0 | if (!tls13_record_layer_set_read_traffic_key(ctx->rl, |
1097 | 0 | &secrets->client_application_traffic, ssl_encryption_application)) |
1098 | 0 | goto err; |
1099 | | |
1100 | 0 | tls13_record_layer_allow_ccs(ctx->rl, 0); |
1101 | |
|
1102 | 0 | ret = 1; |
1103 | |
|
1104 | 4 | err: |
1105 | 4 | HMAC_CTX_free(hmac_ctx); |
1106 | 4 | free(verify_data); |
1107 | | |
1108 | 4 | return ret; |
1109 | 0 | } |