/src/boringssl/ssl/tls13_client.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (c) 2016, Google Inc. |
2 | | * |
3 | | * Permission to use, copy, modify, and/or distribute this software for any |
4 | | * purpose with or without fee is hereby granted, provided that the above |
5 | | * copyright notice and this permission notice appear in all copies. |
6 | | * |
7 | | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES |
8 | | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF |
9 | | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
10 | | * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
11 | | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION |
12 | | * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN |
13 | | * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ |
14 | | |
15 | | #include <openssl/ssl.h> |
16 | | |
17 | | #include <assert.h> |
18 | | #include <limits.h> |
19 | | #include <string.h> |
20 | | |
21 | | #include <utility> |
22 | | |
23 | | #include <openssl/bytestring.h> |
24 | | #include <openssl/digest.h> |
25 | | #include <openssl/err.h> |
26 | | #include <openssl/mem.h> |
27 | | #include <openssl/sha.h> |
28 | | #include <openssl/stack.h> |
29 | | |
30 | | #include "../crypto/internal.h" |
31 | | #include "internal.h" |
32 | | |
33 | | |
34 | | BSSL_NAMESPACE_BEGIN |
35 | | |
36 | | enum client_hs_state_t { |
37 | | state_read_hello_retry_request = 0, |
38 | | state_send_second_client_hello, |
39 | | state_read_server_hello, |
40 | | state_read_encrypted_extensions, |
41 | | state_read_certificate_request, |
42 | | state_read_server_certificate, |
43 | | state_read_server_certificate_verify, |
44 | | state_server_certificate_reverify, |
45 | | state_read_server_finished, |
46 | | state_send_end_of_early_data, |
47 | | state_send_client_encrypted_extensions, |
48 | | state_send_client_certificate, |
49 | | state_send_client_certificate_verify, |
50 | | state_complete_second_flight, |
51 | | state_done, |
52 | | }; |
53 | | |
54 | | static const uint8_t kZeroes[EVP_MAX_MD_SIZE] = {0}; |
55 | | |
56 | | // end_of_early_data closes the early data stream for |hs| and switches the |
57 | | // encryption level to |level|. It returns true on success and false on error. |
58 | 0 | static bool close_early_data(SSL_HANDSHAKE *hs, ssl_encryption_level_t level) { |
59 | 0 | SSL *const ssl = hs->ssl; |
60 | 0 | assert(hs->in_early_data); |
61 | | |
62 | | // Note |can_early_write| may already be false if |SSL_write| exceeded the |
63 | | // early data write limit. |
64 | 0 | hs->can_early_write = false; |
65 | | |
66 | | // 0-RTT write states on the client differ between TLS 1.3, DTLS 1.3, and |
67 | | // QUIC. TLS 1.3 has one write encryption level at a time. 0-RTT write keys |
68 | | // overwrite the null cipher and defer handshake write keys. While a |
69 | | // HelloRetryRequest can cause us to rewind back to the null cipher, sequence |
70 | | // numbers have no effect, so we can install a "new" null cipher. |
71 | | // |
72 | | // In QUIC and DTLS 1.3, 0-RTT write state cannot override or defer the normal |
73 | | // write state. The two ClientHello sequence numbers must align, and handshake |
74 | | // write keys must be installed early to ACK the EncryptedExtensions. |
75 | | // |
76 | | // We do not currently implement DTLS 1.3 and, in QUIC, the caller handles |
77 | | // 0-RTT data, so we can skip installing 0-RTT keys and act as if there is one |
78 | | // write level. If we implement DTLS 1.3, we'll need to model this better. |
79 | 0 | if (ssl->quic_method == nullptr) { |
80 | 0 | if (level == ssl_encryption_initial) { |
81 | 0 | bssl::UniquePtr<SSLAEADContext> null_ctx = |
82 | 0 | SSLAEADContext::CreateNullCipher(SSL_is_dtls(ssl)); |
83 | 0 | if (!null_ctx || |
84 | 0 | !ssl->method->set_write_state(ssl, ssl_encryption_initial, |
85 | 0 | std::move(null_ctx), |
86 | 0 | /*secret_for_quic=*/{})) { |
87 | 0 | return false; |
88 | 0 | } |
89 | 0 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
90 | 0 | } else { |
91 | 0 | assert(level == ssl_encryption_handshake); |
92 | 0 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
93 | 0 | hs->new_session.get(), |
94 | 0 | hs->client_handshake_secret())) { |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | } |
98 | 0 | } |
99 | | |
100 | 0 | assert(ssl->s3->write_level == level); |
101 | 0 | return true; |
102 | 0 | } |
103 | | |
104 | | static bool parse_server_hello_tls13(const SSL_HANDSHAKE *hs, |
105 | | ParsedServerHello *out, uint8_t *out_alert, |
106 | 0 | const SSLMessage &msg) { |
107 | 0 | if (!ssl_parse_server_hello(out, out_alert, msg)) { |
108 | 0 | return false; |
109 | 0 | } |
110 | | // The RFC8446 version of the structure fixes some legacy values. |
111 | | // Additionally, the session ID must echo the original one. |
112 | 0 | if (out->legacy_version != TLS1_2_VERSION || |
113 | 0 | out->compression_method != 0 || |
114 | 0 | !CBS_mem_equal(&out->session_id, hs->session_id, hs->session_id_len) || |
115 | 0 | CBS_len(&out->extensions) == 0) { |
116 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
117 | 0 | *out_alert = SSL_AD_DECODE_ERROR; |
118 | 0 | return false; |
119 | 0 | } |
120 | 0 | return true; |
121 | 0 | } |
122 | | |
123 | 0 | static bool is_hello_retry_request(const ParsedServerHello &server_hello) { |
124 | 0 | return Span<const uint8_t>(server_hello.random) == kHelloRetryRequest; |
125 | 0 | } |
126 | | |
127 | | static bool check_ech_confirmation(const SSL_HANDSHAKE *hs, bool *out_accepted, |
128 | | uint8_t *out_alert, |
129 | 0 | const ParsedServerHello &server_hello) { |
130 | 0 | const bool is_hrr = is_hello_retry_request(server_hello); |
131 | 0 | size_t offset; |
132 | 0 | if (is_hrr) { |
133 | | // We check for an unsolicited extension when parsing all of them. |
134 | 0 | SSLExtension ech(TLSEXT_TYPE_encrypted_client_hello); |
135 | 0 | if (!ssl_parse_extensions(&server_hello.extensions, out_alert, {&ech}, |
136 | 0 | /*ignore_unknown=*/true)) { |
137 | 0 | return false; |
138 | 0 | } |
139 | 0 | if (!ech.present) { |
140 | 0 | *out_accepted = false; |
141 | 0 | return true; |
142 | 0 | } |
143 | 0 | if (CBS_len(&ech.data) != ECH_CONFIRMATION_SIGNAL_LEN) { |
144 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
145 | 0 | *out_alert = SSL_AD_DECODE_ERROR; |
146 | 0 | return false; |
147 | 0 | } |
148 | 0 | offset = CBS_data(&ech.data) - CBS_data(&server_hello.raw); |
149 | 0 | } else { |
150 | 0 | offset = ssl_ech_confirmation_signal_hello_offset(hs->ssl); |
151 | 0 | } |
152 | | |
153 | 0 | if (!hs->selected_ech_config) { |
154 | 0 | *out_accepted = false; |
155 | 0 | return true; |
156 | 0 | } |
157 | | |
158 | 0 | uint8_t expected[ECH_CONFIRMATION_SIGNAL_LEN]; |
159 | 0 | if (!ssl_ech_accept_confirmation(hs, expected, hs->inner_client_random, |
160 | 0 | hs->inner_transcript, is_hrr, |
161 | 0 | server_hello.raw, offset)) { |
162 | 0 | *out_alert = SSL_AD_INTERNAL_ERROR; |
163 | 0 | return false; |
164 | 0 | } |
165 | | |
166 | 0 | *out_accepted = CRYPTO_memcmp(CBS_data(&server_hello.raw) + offset, expected, |
167 | 0 | sizeof(expected)) == 0; |
168 | 0 | return true; |
169 | 0 | } |
170 | | |
171 | 0 | static enum ssl_hs_wait_t do_read_hello_retry_request(SSL_HANDSHAKE *hs) { |
172 | 0 | SSL *const ssl = hs->ssl; |
173 | 0 | assert(ssl->s3->have_version); |
174 | 0 | SSLMessage msg; |
175 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
176 | 0 | return ssl_hs_read_message; |
177 | 0 | } |
178 | | |
179 | | // Queue up a ChangeCipherSpec for whenever we next send something. This |
180 | | // will be before the second ClientHello. If we offered early data, this was |
181 | | // already done. |
182 | 0 | if (!hs->early_data_offered && |
183 | 0 | !ssl->method->add_change_cipher_spec(ssl)) { |
184 | 0 | return ssl_hs_error; |
185 | 0 | } |
186 | | |
187 | 0 | ParsedServerHello server_hello; |
188 | 0 | uint8_t alert = SSL_AD_DECODE_ERROR; |
189 | 0 | if (!parse_server_hello_tls13(hs, &server_hello, &alert, msg)) { |
190 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
191 | 0 | return ssl_hs_error; |
192 | 0 | } |
193 | | |
194 | | // The cipher suite must be one we offered. We currently offer all supported |
195 | | // TLS 1.3 ciphers unless policy controls limited it. So we check the version |
196 | | // and that it's ok per policy. |
197 | 0 | const SSL_CIPHER *cipher = SSL_get_cipher_by_value(server_hello.cipher_suite); |
198 | 0 | if (cipher == nullptr || |
199 | 0 | SSL_CIPHER_get_min_version(cipher) > ssl_protocol_version(ssl) || |
200 | 0 | SSL_CIPHER_get_max_version(cipher) < ssl_protocol_version(ssl) || |
201 | 0 | !ssl_tls13_cipher_meets_policy(SSL_CIPHER_get_protocol_id(cipher), |
202 | 0 | ssl->config->tls13_cipher_policy)) { |
203 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
204 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
205 | 0 | return ssl_hs_error; |
206 | 0 | } |
207 | | |
208 | 0 | hs->new_cipher = cipher; |
209 | |
|
210 | 0 | const bool is_hrr = is_hello_retry_request(server_hello); |
211 | 0 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
212 | 0 | (is_hrr && !hs->transcript.UpdateForHelloRetryRequest())) { |
213 | 0 | return ssl_hs_error; |
214 | 0 | } |
215 | 0 | if (hs->selected_ech_config) { |
216 | 0 | if (!hs->inner_transcript.InitHash(ssl_protocol_version(ssl), |
217 | 0 | hs->new_cipher) || |
218 | 0 | (is_hrr && !hs->inner_transcript.UpdateForHelloRetryRequest())) { |
219 | 0 | return ssl_hs_error; |
220 | 0 | } |
221 | 0 | } |
222 | | |
223 | | // Determine which ClientHello the server is responding to. Run |
224 | | // |check_ech_confirmation| unconditionally, so we validate the extension |
225 | | // contents. |
226 | 0 | bool ech_accepted; |
227 | 0 | if (!check_ech_confirmation(hs, &ech_accepted, &alert, server_hello)) { |
228 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
229 | 0 | return ssl_hs_error; |
230 | 0 | } |
231 | 0 | if (hs->selected_ech_config) { |
232 | 0 | ssl->s3->ech_status = ech_accepted ? ssl_ech_accepted : ssl_ech_rejected; |
233 | 0 | } |
234 | |
|
235 | 0 | if (!is_hrr) { |
236 | 0 | hs->tls13_state = state_read_server_hello; |
237 | 0 | return ssl_hs_ok; |
238 | 0 | } |
239 | | |
240 | | // The ECH extension, if present, was already parsed by |
241 | | // |check_ech_confirmation|. |
242 | 0 | SSLExtension cookie(TLSEXT_TYPE_cookie), key_share(TLSEXT_TYPE_key_share), |
243 | 0 | supported_versions(TLSEXT_TYPE_supported_versions), |
244 | 0 | ech_unused(TLSEXT_TYPE_encrypted_client_hello, |
245 | 0 | hs->selected_ech_config || hs->config->ech_grease_enabled); |
246 | 0 | if (!ssl_parse_extensions( |
247 | 0 | &server_hello.extensions, &alert, |
248 | 0 | {&cookie, &key_share, &supported_versions, &ech_unused}, |
249 | 0 | /*ignore_unknown=*/false)) { |
250 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
251 | 0 | return ssl_hs_error; |
252 | 0 | } |
253 | | |
254 | 0 | if (!cookie.present && !key_share.present) { |
255 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMPTY_HELLO_RETRY_REQUEST); |
256 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
257 | 0 | return ssl_hs_error; |
258 | 0 | } |
259 | 0 | if (cookie.present) { |
260 | 0 | CBS cookie_value; |
261 | 0 | if (!CBS_get_u16_length_prefixed(&cookie.data, &cookie_value) || |
262 | 0 | CBS_len(&cookie_value) == 0 || |
263 | 0 | CBS_len(&cookie.data) != 0) { |
264 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
265 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
266 | 0 | return ssl_hs_error; |
267 | 0 | } |
268 | | |
269 | 0 | if (!hs->cookie.CopyFrom(cookie_value)) { |
270 | 0 | return ssl_hs_error; |
271 | 0 | } |
272 | 0 | } |
273 | | |
274 | 0 | if (key_share.present) { |
275 | 0 | uint16_t group_id; |
276 | 0 | if (!CBS_get_u16(&key_share.data, &group_id) || |
277 | 0 | CBS_len(&key_share.data) != 0) { |
278 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
279 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
280 | 0 | return ssl_hs_error; |
281 | 0 | } |
282 | | |
283 | | // The group must be supported. |
284 | 0 | if (!tls1_check_group_id(hs, group_id)) { |
285 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
286 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
287 | 0 | return ssl_hs_error; |
288 | 0 | } |
289 | | |
290 | | // Check that the HelloRetryRequest does not request a key share that was |
291 | | // provided in the initial ClientHello. |
292 | 0 | if (hs->key_shares[0]->GroupID() == group_id || |
293 | 0 | (hs->key_shares[1] && hs->key_shares[1]->GroupID() == group_id)) { |
294 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
295 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CURVE); |
296 | 0 | return ssl_hs_error; |
297 | 0 | } |
298 | | |
299 | 0 | if (!ssl_setup_key_shares(hs, group_id)) { |
300 | 0 | return ssl_hs_error; |
301 | 0 | } |
302 | 0 | } |
303 | | |
304 | | // Although we now know whether ClientHelloInner was used, we currently |
305 | | // maintain both transcripts up to ServerHello. We could swap transcripts |
306 | | // early, but then ClientHello construction and |check_ech_confirmation| |
307 | | // become more complex. |
308 | 0 | if (!ssl_hash_message(hs, msg)) { |
309 | 0 | return ssl_hs_error; |
310 | 0 | } |
311 | 0 | if (ssl->s3->ech_status == ssl_ech_accepted && |
312 | 0 | !hs->inner_transcript.Update(msg.raw)) { |
313 | 0 | return ssl_hs_error; |
314 | 0 | } |
315 | | |
316 | | // HelloRetryRequest should be the end of the flight. |
317 | 0 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
318 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
319 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
320 | 0 | return ssl_hs_error; |
321 | 0 | } |
322 | | |
323 | 0 | ssl->method->next_message(ssl); |
324 | 0 | ssl->s3->used_hello_retry_request = true; |
325 | 0 | hs->tls13_state = state_send_second_client_hello; |
326 | | // 0-RTT is rejected if we receive a HelloRetryRequest. |
327 | 0 | if (hs->in_early_data) { |
328 | 0 | ssl->s3->early_data_reason = ssl_early_data_hello_retry_request; |
329 | 0 | if (!close_early_data(hs, ssl_encryption_initial)) { |
330 | 0 | return ssl_hs_error; |
331 | 0 | } |
332 | 0 | return ssl_hs_early_data_rejected; |
333 | 0 | } |
334 | 0 | return ssl_hs_ok; |
335 | 0 | } |
336 | | |
337 | 0 | static enum ssl_hs_wait_t do_send_second_client_hello(SSL_HANDSHAKE *hs) { |
338 | | // Any 0-RTT keys must have been discarded. |
339 | 0 | assert(hs->ssl->s3->write_level == ssl_encryption_initial); |
340 | | |
341 | | // Build the second ClientHelloInner, if applicable. The second ClientHello |
342 | | // uses an empty string for |enc|. |
343 | 0 | if (hs->ssl->s3->ech_status == ssl_ech_accepted && |
344 | 0 | !ssl_encrypt_client_hello(hs, {})) { |
345 | 0 | return ssl_hs_error; |
346 | 0 | } |
347 | | |
348 | 0 | if (!ssl_add_client_hello(hs)) { |
349 | 0 | return ssl_hs_error; |
350 | 0 | } |
351 | | |
352 | 0 | ssl_done_writing_client_hello(hs); |
353 | 0 | hs->tls13_state = state_read_server_hello; |
354 | 0 | return ssl_hs_flush; |
355 | 0 | } |
356 | | |
357 | 0 | static enum ssl_hs_wait_t do_read_server_hello(SSL_HANDSHAKE *hs) { |
358 | 0 | SSL *const ssl = hs->ssl; |
359 | 0 | SSLMessage msg; |
360 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
361 | 0 | return ssl_hs_read_message; |
362 | 0 | } |
363 | 0 | ParsedServerHello server_hello; |
364 | 0 | uint8_t alert = SSL_AD_DECODE_ERROR; |
365 | 0 | if (!parse_server_hello_tls13(hs, &server_hello, &alert, msg)) { |
366 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
367 | 0 | return ssl_hs_error; |
368 | 0 | } |
369 | | |
370 | | // Forbid a second HelloRetryRequest. |
371 | 0 | if (is_hello_retry_request(server_hello)) { |
372 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
373 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
374 | 0 | return ssl_hs_error; |
375 | 0 | } |
376 | | |
377 | | // Check the cipher suite, in case this is after HelloRetryRequest. |
378 | 0 | if (SSL_CIPHER_get_protocol_id(hs->new_cipher) != server_hello.cipher_suite) { |
379 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_CIPHER_RETURNED); |
380 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
381 | 0 | return ssl_hs_error; |
382 | 0 | } |
383 | | |
384 | 0 | if (ssl->s3->ech_status == ssl_ech_accepted) { |
385 | 0 | if (ssl->s3->used_hello_retry_request) { |
386 | | // HelloRetryRequest and ServerHello must accept ECH consistently. |
387 | 0 | bool ech_accepted; |
388 | 0 | if (!check_ech_confirmation(hs, &ech_accepted, &alert, server_hello)) { |
389 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
390 | 0 | return ssl_hs_error; |
391 | 0 | } |
392 | 0 | if (!ech_accepted) { |
393 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_INCONSISTENT_ECH_NEGOTIATION); |
394 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
395 | 0 | return ssl_hs_error; |
396 | 0 | } |
397 | 0 | } |
398 | | |
399 | 0 | hs->transcript = std::move(hs->inner_transcript); |
400 | 0 | hs->extensions.sent = hs->inner_extensions_sent; |
401 | | // Report the inner random value through |SSL_get_client_random|. |
402 | 0 | OPENSSL_memcpy(ssl->s3->client_random, hs->inner_client_random, |
403 | 0 | SSL3_RANDOM_SIZE); |
404 | 0 | } |
405 | | |
406 | 0 | OPENSSL_memcpy(ssl->s3->server_random, CBS_data(&server_hello.random), |
407 | 0 | SSL3_RANDOM_SIZE); |
408 | | |
409 | | // When offering ECH, |ssl->session| is only offered in ClientHelloInner. |
410 | 0 | const bool pre_shared_key_allowed = |
411 | 0 | ssl->session != nullptr && ssl->s3->ech_status != ssl_ech_rejected; |
412 | 0 | SSLExtension key_share(TLSEXT_TYPE_key_share), |
413 | 0 | pre_shared_key(TLSEXT_TYPE_pre_shared_key, pre_shared_key_allowed), |
414 | 0 | supported_versions(TLSEXT_TYPE_supported_versions); |
415 | 0 | if (!ssl_parse_extensions(&server_hello.extensions, &alert, |
416 | 0 | {&key_share, &pre_shared_key, &supported_versions}, |
417 | 0 | /*ignore_unknown=*/false)) { |
418 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
419 | 0 | return ssl_hs_error; |
420 | 0 | } |
421 | | |
422 | | // Recheck supported_versions, in case this is after HelloRetryRequest. |
423 | 0 | uint16_t version; |
424 | 0 | if (!supported_versions.present || |
425 | 0 | !CBS_get_u16(&supported_versions.data, &version) || |
426 | 0 | CBS_len(&supported_versions.data) != 0 || |
427 | 0 | version != ssl->version) { |
428 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_SECOND_SERVERHELLO_VERSION_MISMATCH); |
429 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
430 | 0 | return ssl_hs_error; |
431 | 0 | } |
432 | | |
433 | 0 | alert = SSL_AD_DECODE_ERROR; |
434 | 0 | if (pre_shared_key.present) { |
435 | 0 | if (!ssl_ext_pre_shared_key_parse_serverhello(hs, &alert, |
436 | 0 | &pre_shared_key.data)) { |
437 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
438 | 0 | return ssl_hs_error; |
439 | 0 | } |
440 | | |
441 | 0 | if (ssl->session->ssl_version != ssl->version) { |
442 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_VERSION_NOT_RETURNED); |
443 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
444 | 0 | return ssl_hs_error; |
445 | 0 | } |
446 | | |
447 | 0 | if (ssl->session->cipher->algorithm_prf != hs->new_cipher->algorithm_prf) { |
448 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_OLD_SESSION_PRF_HASH_MISMATCH); |
449 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
450 | 0 | return ssl_hs_error; |
451 | 0 | } |
452 | | |
453 | 0 | if (!ssl_session_is_context_valid(hs, ssl->session.get())) { |
454 | | // This is actually a client application bug. |
455 | 0 | OPENSSL_PUT_ERROR(SSL, |
456 | 0 | SSL_R_ATTEMPT_TO_REUSE_SESSION_IN_DIFFERENT_CONTEXT); |
457 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
458 | 0 | return ssl_hs_error; |
459 | 0 | } |
460 | | |
461 | 0 | ssl->s3->session_reused = true; |
462 | 0 | hs->can_release_private_key = true; |
463 | | // Only authentication information carries over in TLS 1.3. |
464 | 0 | hs->new_session = |
465 | 0 | SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_DUP_AUTH_ONLY); |
466 | 0 | if (!hs->new_session) { |
467 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
468 | 0 | return ssl_hs_error; |
469 | 0 | } |
470 | 0 | ssl_set_session(ssl, NULL); |
471 | | |
472 | | // Resumption incorporates fresh key material, so refresh the timeout. |
473 | 0 | ssl_session_renew_timeout(ssl, hs->new_session.get(), |
474 | 0 | ssl->session_ctx->session_psk_dhe_timeout); |
475 | 0 | } else if (!ssl_get_new_session(hs)) { |
476 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
477 | 0 | return ssl_hs_error; |
478 | 0 | } |
479 | | |
480 | 0 | hs->new_session->cipher = hs->new_cipher; |
481 | | |
482 | | // Set up the key schedule and incorporate the PSK into the running secret. |
483 | 0 | size_t hash_len = EVP_MD_size( |
484 | 0 | ssl_get_handshake_digest(ssl_protocol_version(ssl), hs->new_cipher)); |
485 | 0 | if (!tls13_init_key_schedule( |
486 | 0 | hs, ssl->s3->session_reused |
487 | 0 | ? MakeConstSpan(hs->new_session->secret, |
488 | 0 | hs->new_session->secret_length) |
489 | 0 | : MakeConstSpan(kZeroes, hash_len))) { |
490 | 0 | return ssl_hs_error; |
491 | 0 | } |
492 | | |
493 | 0 | if (!key_share.present) { |
494 | | // We do not support psk_ke and thus always require a key share. |
495 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_KEY_SHARE); |
496 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_MISSING_EXTENSION); |
497 | 0 | return ssl_hs_error; |
498 | 0 | } |
499 | | |
500 | | // Resolve ECDHE and incorporate it into the secret. |
501 | 0 | Array<uint8_t> dhe_secret; |
502 | 0 | alert = SSL_AD_DECODE_ERROR; |
503 | 0 | if (!ssl_ext_key_share_parse_serverhello(hs, &dhe_secret, &alert, |
504 | 0 | &key_share.data)) { |
505 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
506 | 0 | return ssl_hs_error; |
507 | 0 | } |
508 | | |
509 | 0 | if (!tls13_advance_key_schedule(hs, dhe_secret) || |
510 | 0 | !ssl_hash_message(hs, msg) || |
511 | 0 | !tls13_derive_handshake_secrets(hs)) { |
512 | 0 | return ssl_hs_error; |
513 | 0 | } |
514 | | |
515 | | // If currently sending early data over TCP, we defer installing client |
516 | | // traffic keys to when the early data stream is closed. See |
517 | | // |close_early_data|. Note if the server has already rejected 0-RTT via |
518 | | // HelloRetryRequest, |in_early_data| is already false. |
519 | 0 | if (!hs->in_early_data || ssl->quic_method != nullptr) { |
520 | 0 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_seal, |
521 | 0 | hs->new_session.get(), |
522 | 0 | hs->client_handshake_secret())) { |
523 | 0 | return ssl_hs_error; |
524 | 0 | } |
525 | 0 | } |
526 | | |
527 | 0 | if (!tls13_set_traffic_key(ssl, ssl_encryption_handshake, evp_aead_open, |
528 | 0 | hs->new_session.get(), |
529 | 0 | hs->server_handshake_secret())) { |
530 | 0 | return ssl_hs_error; |
531 | 0 | } |
532 | | |
533 | 0 | ssl->method->next_message(ssl); |
534 | 0 | hs->tls13_state = state_read_encrypted_extensions; |
535 | 0 | return ssl_hs_ok; |
536 | 0 | } |
537 | | |
538 | 0 | static enum ssl_hs_wait_t do_read_encrypted_extensions(SSL_HANDSHAKE *hs) { |
539 | 0 | SSL *const ssl = hs->ssl; |
540 | 0 | SSLMessage msg; |
541 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
542 | 0 | return ssl_hs_read_message; |
543 | 0 | } |
544 | 0 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_ENCRYPTED_EXTENSIONS)) { |
545 | 0 | return ssl_hs_error; |
546 | 0 | } |
547 | | |
548 | 0 | CBS body = msg.body, extensions; |
549 | 0 | if (!CBS_get_u16_length_prefixed(&body, &extensions) || |
550 | 0 | CBS_len(&body) != 0) { |
551 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
552 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
553 | 0 | return ssl_hs_error; |
554 | 0 | } |
555 | | |
556 | 0 | if (!ssl_parse_serverhello_tlsext(hs, &extensions)) { |
557 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
558 | 0 | return ssl_hs_error; |
559 | 0 | } |
560 | | |
561 | 0 | if (ssl->s3->early_data_accepted) { |
562 | | // The extension parser checks the server resumed the session. |
563 | 0 | assert(ssl->s3->session_reused); |
564 | | // If offering ECH, the server may not accept early data with |
565 | | // ClientHelloOuter. We do not offer sessions with ClientHelloOuter, so this |
566 | | // this should be implied by checking |session_reused|. |
567 | 0 | assert(ssl->s3->ech_status != ssl_ech_rejected); |
568 | | |
569 | 0 | if (hs->early_session->cipher != hs->new_session->cipher) { |
570 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CIPHER_MISMATCH_ON_EARLY_DATA); |
571 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
572 | 0 | return ssl_hs_error; |
573 | 0 | } |
574 | 0 | if (MakeConstSpan(hs->early_session->early_alpn) != |
575 | 0 | ssl->s3->alpn_selected) { |
576 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_ALPN_MISMATCH_ON_EARLY_DATA); |
577 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
578 | 0 | return ssl_hs_error; |
579 | 0 | } |
580 | | // Channel ID is incompatible with 0-RTT. The ALPS extension should be |
581 | | // negotiated implicitly. |
582 | 0 | if (hs->channel_id_negotiated || |
583 | 0 | hs->new_session->has_application_settings) { |
584 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION_ON_EARLY_DATA); |
585 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
586 | 0 | return ssl_hs_error; |
587 | 0 | } |
588 | 0 | hs->new_session->has_application_settings = |
589 | 0 | hs->early_session->has_application_settings; |
590 | 0 | if (!hs->new_session->local_application_settings.CopyFrom( |
591 | 0 | hs->early_session->local_application_settings) || |
592 | 0 | !hs->new_session->peer_application_settings.CopyFrom( |
593 | 0 | hs->early_session->peer_application_settings)) { |
594 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
595 | 0 | return ssl_hs_error; |
596 | 0 | } |
597 | 0 | } |
598 | | |
599 | | // Store the negotiated ALPN in the session. |
600 | 0 | if (!hs->new_session->early_alpn.CopyFrom(ssl->s3->alpn_selected)) { |
601 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
602 | 0 | return ssl_hs_error; |
603 | 0 | } |
604 | | |
605 | 0 | if (!ssl_hash_message(hs, msg)) { |
606 | 0 | return ssl_hs_error; |
607 | 0 | } |
608 | | |
609 | 0 | ssl->method->next_message(ssl); |
610 | 0 | hs->tls13_state = state_read_certificate_request; |
611 | 0 | if (hs->in_early_data && !ssl->s3->early_data_accepted) { |
612 | 0 | if (!close_early_data(hs, ssl_encryption_handshake)) { |
613 | 0 | return ssl_hs_error; |
614 | 0 | } |
615 | 0 | return ssl_hs_early_data_rejected; |
616 | 0 | } |
617 | 0 | return ssl_hs_ok; |
618 | 0 | } |
619 | | |
620 | 0 | static enum ssl_hs_wait_t do_read_certificate_request(SSL_HANDSHAKE *hs) { |
621 | 0 | SSL *const ssl = hs->ssl; |
622 | | // CertificateRequest may only be sent in non-resumption handshakes. |
623 | 0 | if (ssl->s3->session_reused) { |
624 | 0 | if (ssl->ctx->reverify_on_resume && !ssl->s3->early_data_accepted) { |
625 | 0 | hs->tls13_state = state_server_certificate_reverify; |
626 | 0 | return ssl_hs_ok; |
627 | 0 | } |
628 | 0 | hs->tls13_state = state_read_server_finished; |
629 | 0 | return ssl_hs_ok; |
630 | 0 | } |
631 | | |
632 | 0 | SSLMessage msg; |
633 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
634 | 0 | return ssl_hs_read_message; |
635 | 0 | } |
636 | | |
637 | | // CertificateRequest is optional. |
638 | 0 | if (msg.type != SSL3_MT_CERTIFICATE_REQUEST) { |
639 | 0 | hs->tls13_state = state_read_server_certificate; |
640 | 0 | return ssl_hs_ok; |
641 | 0 | } |
642 | | |
643 | | |
644 | 0 | SSLExtension sigalgs(TLSEXT_TYPE_signature_algorithms), |
645 | 0 | ca(TLSEXT_TYPE_certificate_authorities); |
646 | 0 | CBS body = msg.body, context, extensions, supported_signature_algorithms; |
647 | 0 | uint8_t alert = SSL_AD_DECODE_ERROR; |
648 | 0 | if (!CBS_get_u8_length_prefixed(&body, &context) || |
649 | | // The request context is always empty during the handshake. |
650 | 0 | CBS_len(&context) != 0 || |
651 | 0 | !CBS_get_u16_length_prefixed(&body, &extensions) || // |
652 | 0 | CBS_len(&body) != 0 || |
653 | 0 | !ssl_parse_extensions(&extensions, &alert, {&sigalgs, &ca}, |
654 | 0 | /*ignore_unknown=*/true) || |
655 | 0 | !sigalgs.present || |
656 | 0 | !CBS_get_u16_length_prefixed(&sigalgs.data, |
657 | 0 | &supported_signature_algorithms) || |
658 | 0 | !tls1_parse_peer_sigalgs(hs, &supported_signature_algorithms)) { |
659 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
660 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
661 | 0 | return ssl_hs_error; |
662 | 0 | } |
663 | | |
664 | 0 | if (ca.present) { |
665 | 0 | hs->ca_names = ssl_parse_client_CA_list(ssl, &alert, &ca.data); |
666 | 0 | if (!hs->ca_names) { |
667 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
668 | 0 | return ssl_hs_error; |
669 | 0 | } |
670 | 0 | } else { |
671 | 0 | hs->ca_names.reset(sk_CRYPTO_BUFFER_new_null()); |
672 | 0 | if (!hs->ca_names) { |
673 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
674 | 0 | return ssl_hs_error; |
675 | 0 | } |
676 | 0 | } |
677 | | |
678 | 0 | hs->cert_request = true; |
679 | 0 | ssl->ctx->x509_method->hs_flush_cached_ca_names(hs); |
680 | |
|
681 | 0 | if (!ssl_hash_message(hs, msg)) { |
682 | 0 | return ssl_hs_error; |
683 | 0 | } |
684 | | |
685 | 0 | ssl->method->next_message(ssl); |
686 | 0 | hs->tls13_state = state_read_server_certificate; |
687 | 0 | return ssl_hs_ok; |
688 | 0 | } |
689 | | |
690 | 0 | static enum ssl_hs_wait_t do_read_server_certificate(SSL_HANDSHAKE *hs) { |
691 | 0 | SSL *const ssl = hs->ssl; |
692 | 0 | SSLMessage msg; |
693 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
694 | 0 | return ssl_hs_read_message; |
695 | 0 | } |
696 | | |
697 | 0 | if (msg.type != SSL3_MT_COMPRESSED_CERTIFICATE && |
698 | 0 | !ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { |
699 | 0 | return ssl_hs_error; |
700 | 0 | } |
701 | | |
702 | 0 | if (!tls13_process_certificate(hs, msg, false /* certificate required */) || |
703 | 0 | !ssl_hash_message(hs, msg)) { |
704 | 0 | return ssl_hs_error; |
705 | 0 | } |
706 | | |
707 | 0 | ssl->method->next_message(ssl); |
708 | 0 | hs->tls13_state = state_read_server_certificate_verify; |
709 | 0 | return ssl_hs_ok; |
710 | 0 | } |
711 | | |
712 | 0 | static enum ssl_hs_wait_t do_read_server_certificate_verify(SSL_HANDSHAKE *hs) { |
713 | 0 | SSL *const ssl = hs->ssl; |
714 | 0 | SSLMessage msg; |
715 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
716 | 0 | return ssl_hs_read_message; |
717 | 0 | } |
718 | 0 | switch (ssl_verify_peer_cert(hs)) { |
719 | 0 | case ssl_verify_ok: |
720 | 0 | break; |
721 | 0 | case ssl_verify_invalid: |
722 | 0 | return ssl_hs_error; |
723 | 0 | case ssl_verify_retry: |
724 | 0 | hs->tls13_state = state_read_server_certificate_verify; |
725 | 0 | return ssl_hs_certificate_verify; |
726 | 0 | } |
727 | | |
728 | 0 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY) || |
729 | 0 | !tls13_process_certificate_verify(hs, msg) || |
730 | 0 | !ssl_hash_message(hs, msg)) { |
731 | 0 | return ssl_hs_error; |
732 | 0 | } |
733 | | |
734 | 0 | ssl->method->next_message(ssl); |
735 | 0 | hs->tls13_state = state_read_server_finished; |
736 | 0 | return ssl_hs_ok; |
737 | 0 | } |
738 | | |
739 | 0 | static enum ssl_hs_wait_t do_server_certificate_reverify(SSL_HANDSHAKE *hs) { |
740 | 0 | switch (ssl_reverify_peer_cert(hs, /*send_alert=*/true)) { |
741 | 0 | case ssl_verify_ok: |
742 | 0 | break; |
743 | 0 | case ssl_verify_invalid: |
744 | 0 | return ssl_hs_error; |
745 | 0 | case ssl_verify_retry: |
746 | 0 | hs->tls13_state = state_server_certificate_reverify; |
747 | 0 | return ssl_hs_certificate_verify; |
748 | 0 | } |
749 | 0 | hs->tls13_state = state_read_server_finished; |
750 | 0 | return ssl_hs_ok; |
751 | 0 | } |
752 | | |
753 | 0 | static enum ssl_hs_wait_t do_read_server_finished(SSL_HANDSHAKE *hs) { |
754 | 0 | SSL *const ssl = hs->ssl; |
755 | 0 | SSLMessage msg; |
756 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
757 | 0 | return ssl_hs_read_message; |
758 | 0 | } |
759 | 0 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED) || |
760 | 0 | !tls13_process_finished(hs, msg, false /* don't use saved value */) || |
761 | 0 | !ssl_hash_message(hs, msg) || |
762 | | // Update the secret to the master secret and derive traffic keys. |
763 | 0 | !tls13_advance_key_schedule( |
764 | 0 | hs, MakeConstSpan(kZeroes, hs->transcript.DigestLen())) || |
765 | 0 | !tls13_derive_application_secrets(hs)) { |
766 | 0 | return ssl_hs_error; |
767 | 0 | } |
768 | | |
769 | | // Finished should be the end of the flight. |
770 | 0 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
771 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
772 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
773 | 0 | return ssl_hs_error; |
774 | 0 | } |
775 | | |
776 | 0 | ssl->method->next_message(ssl); |
777 | 0 | hs->tls13_state = state_send_end_of_early_data; |
778 | 0 | return ssl_hs_ok; |
779 | 0 | } |
780 | | |
781 | 0 | static enum ssl_hs_wait_t do_send_end_of_early_data(SSL_HANDSHAKE *hs) { |
782 | 0 | SSL *const ssl = hs->ssl; |
783 | |
|
784 | 0 | if (ssl->s3->early_data_accepted) { |
785 | | // QUIC omits the EndOfEarlyData message. See RFC 9001, section 8.3. |
786 | 0 | if (ssl->quic_method == nullptr) { |
787 | 0 | ScopedCBB cbb; |
788 | 0 | CBB body; |
789 | 0 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
790 | 0 | SSL3_MT_END_OF_EARLY_DATA) || |
791 | 0 | !ssl_add_message_cbb(ssl, cbb.get())) { |
792 | 0 | return ssl_hs_error; |
793 | 0 | } |
794 | 0 | } |
795 | | |
796 | 0 | if (!close_early_data(hs, ssl_encryption_handshake)) { |
797 | 0 | return ssl_hs_error; |
798 | 0 | } |
799 | 0 | } |
800 | | |
801 | 0 | hs->tls13_state = state_send_client_encrypted_extensions; |
802 | 0 | return ssl_hs_ok; |
803 | 0 | } |
804 | | |
805 | | static enum ssl_hs_wait_t do_send_client_encrypted_extensions( |
806 | 0 | SSL_HANDSHAKE *hs) { |
807 | 0 | SSL *const ssl = hs->ssl; |
808 | | // For now, only one extension uses client EncryptedExtensions. This function |
809 | | // may be generalized if others use it in the future. |
810 | 0 | if (hs->new_session->has_application_settings && |
811 | 0 | !ssl->s3->early_data_accepted) { |
812 | 0 | ScopedCBB cbb; |
813 | 0 | CBB body, extensions, extension; |
814 | 0 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
815 | 0 | SSL3_MT_ENCRYPTED_EXTENSIONS) || |
816 | 0 | !CBB_add_u16_length_prefixed(&body, &extensions) || |
817 | 0 | !CBB_add_u16(&extensions, TLSEXT_TYPE_application_settings) || |
818 | 0 | !CBB_add_u16_length_prefixed(&extensions, &extension) || |
819 | 0 | !CBB_add_bytes(&extension, |
820 | 0 | hs->new_session->local_application_settings.data(), |
821 | 0 | hs->new_session->local_application_settings.size()) || |
822 | 0 | !ssl_add_message_cbb(ssl, cbb.get())) { |
823 | 0 | return ssl_hs_error; |
824 | 0 | } |
825 | 0 | } |
826 | | |
827 | 0 | hs->tls13_state = state_send_client_certificate; |
828 | 0 | return ssl_hs_ok; |
829 | 0 | } |
830 | | |
831 | 0 | static enum ssl_hs_wait_t do_send_client_certificate(SSL_HANDSHAKE *hs) { |
832 | 0 | SSL *const ssl = hs->ssl; |
833 | | |
834 | | // The peer didn't request a certificate. |
835 | 0 | if (!hs->cert_request) { |
836 | 0 | hs->tls13_state = state_complete_second_flight; |
837 | 0 | return ssl_hs_ok; |
838 | 0 | } |
839 | | |
840 | 0 | if (ssl->s3->ech_status == ssl_ech_rejected) { |
841 | | // Do not send client certificates on ECH reject. We have not authenticated |
842 | | // the server for the name that can learn the certificate. |
843 | 0 | SSL_certs_clear(ssl); |
844 | 0 | } else if (hs->config->cert->cert_cb != nullptr) { |
845 | | // Call cert_cb to update the certificate. |
846 | 0 | int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); |
847 | 0 | if (rv == 0) { |
848 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
849 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
850 | 0 | return ssl_hs_error; |
851 | 0 | } |
852 | 0 | if (rv < 0) { |
853 | 0 | hs->tls13_state = state_send_client_certificate; |
854 | 0 | return ssl_hs_x509_lookup; |
855 | 0 | } |
856 | 0 | } |
857 | | |
858 | 0 | if (!ssl_on_certificate_selected(hs) || |
859 | 0 | !tls13_add_certificate(hs)) { |
860 | 0 | return ssl_hs_error; |
861 | 0 | } |
862 | | |
863 | 0 | hs->tls13_state = state_send_client_certificate_verify; |
864 | 0 | return ssl_hs_ok; |
865 | 0 | } |
866 | | |
867 | 0 | static enum ssl_hs_wait_t do_send_client_certificate_verify(SSL_HANDSHAKE *hs) { |
868 | | // Don't send CertificateVerify if there is no certificate. |
869 | 0 | if (!ssl_has_certificate(hs)) { |
870 | 0 | hs->tls13_state = state_complete_second_flight; |
871 | 0 | return ssl_hs_ok; |
872 | 0 | } |
873 | | |
874 | 0 | switch (tls13_add_certificate_verify(hs)) { |
875 | 0 | case ssl_private_key_success: |
876 | 0 | hs->tls13_state = state_complete_second_flight; |
877 | 0 | return ssl_hs_ok; |
878 | | |
879 | 0 | case ssl_private_key_retry: |
880 | 0 | hs->tls13_state = state_send_client_certificate_verify; |
881 | 0 | return ssl_hs_private_key_operation; |
882 | | |
883 | 0 | case ssl_private_key_failure: |
884 | 0 | return ssl_hs_error; |
885 | 0 | } |
886 | | |
887 | 0 | assert(0); |
888 | 0 | return ssl_hs_error; |
889 | 0 | } |
890 | | |
891 | 0 | static enum ssl_hs_wait_t do_complete_second_flight(SSL_HANDSHAKE *hs) { |
892 | 0 | SSL *const ssl = hs->ssl; |
893 | 0 | hs->can_release_private_key = true; |
894 | | |
895 | | // Send a Channel ID assertion if necessary. |
896 | 0 | if (hs->channel_id_negotiated) { |
897 | 0 | ScopedCBB cbb; |
898 | 0 | CBB body; |
899 | 0 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_CHANNEL_ID) || |
900 | 0 | !tls1_write_channel_id(hs, &body) || |
901 | 0 | !ssl_add_message_cbb(ssl, cbb.get())) { |
902 | 0 | return ssl_hs_error; |
903 | 0 | } |
904 | 0 | } |
905 | | |
906 | | // Send a Finished message. |
907 | 0 | if (!tls13_add_finished(hs)) { |
908 | 0 | return ssl_hs_error; |
909 | 0 | } |
910 | | |
911 | | // Derive the final keys and enable them. |
912 | 0 | if (!tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_seal, |
913 | 0 | hs->new_session.get(), |
914 | 0 | hs->client_traffic_secret_0()) || |
915 | 0 | !tls13_set_traffic_key(ssl, ssl_encryption_application, evp_aead_open, |
916 | 0 | hs->new_session.get(), |
917 | 0 | hs->server_traffic_secret_0()) || |
918 | 0 | !tls13_derive_resumption_secret(hs)) { |
919 | 0 | return ssl_hs_error; |
920 | 0 | } |
921 | | |
922 | 0 | hs->tls13_state = state_done; |
923 | 0 | return ssl_hs_flush; |
924 | 0 | } |
925 | | |
926 | 0 | enum ssl_hs_wait_t tls13_client_handshake(SSL_HANDSHAKE *hs) { |
927 | 0 | while (hs->tls13_state != state_done) { |
928 | 0 | enum ssl_hs_wait_t ret = ssl_hs_error; |
929 | 0 | enum client_hs_state_t state = |
930 | 0 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
931 | 0 | switch (state) { |
932 | 0 | case state_read_hello_retry_request: |
933 | 0 | ret = do_read_hello_retry_request(hs); |
934 | 0 | break; |
935 | 0 | case state_send_second_client_hello: |
936 | 0 | ret = do_send_second_client_hello(hs); |
937 | 0 | break; |
938 | 0 | case state_read_server_hello: |
939 | 0 | ret = do_read_server_hello(hs); |
940 | 0 | break; |
941 | 0 | case state_read_encrypted_extensions: |
942 | 0 | ret = do_read_encrypted_extensions(hs); |
943 | 0 | break; |
944 | 0 | case state_read_certificate_request: |
945 | 0 | ret = do_read_certificate_request(hs); |
946 | 0 | break; |
947 | 0 | case state_read_server_certificate: |
948 | 0 | ret = do_read_server_certificate(hs); |
949 | 0 | break; |
950 | 0 | case state_read_server_certificate_verify: |
951 | 0 | ret = do_read_server_certificate_verify(hs); |
952 | 0 | break; |
953 | 0 | case state_server_certificate_reverify: |
954 | 0 | ret = do_server_certificate_reverify(hs); |
955 | 0 | break; |
956 | 0 | case state_read_server_finished: |
957 | 0 | ret = do_read_server_finished(hs); |
958 | 0 | break; |
959 | 0 | case state_send_end_of_early_data: |
960 | 0 | ret = do_send_end_of_early_data(hs); |
961 | 0 | break; |
962 | 0 | case state_send_client_certificate: |
963 | 0 | ret = do_send_client_certificate(hs); |
964 | 0 | break; |
965 | 0 | case state_send_client_encrypted_extensions: |
966 | 0 | ret = do_send_client_encrypted_extensions(hs); |
967 | 0 | break; |
968 | 0 | case state_send_client_certificate_verify: |
969 | 0 | ret = do_send_client_certificate_verify(hs); |
970 | 0 | break; |
971 | 0 | case state_complete_second_flight: |
972 | 0 | ret = do_complete_second_flight(hs); |
973 | 0 | break; |
974 | 0 | case state_done: |
975 | 0 | ret = ssl_hs_ok; |
976 | 0 | break; |
977 | 0 | } |
978 | | |
979 | 0 | if (hs->tls13_state != state) { |
980 | 0 | ssl_do_info_callback(hs->ssl, SSL_CB_CONNECT_LOOP, 1); |
981 | 0 | } |
982 | |
|
983 | 0 | if (ret != ssl_hs_ok) { |
984 | 0 | return ret; |
985 | 0 | } |
986 | 0 | } |
987 | | |
988 | 0 | return ssl_hs_ok; |
989 | 0 | } |
990 | | |
991 | 0 | const char *tls13_client_handshake_state(SSL_HANDSHAKE *hs) { |
992 | 0 | enum client_hs_state_t state = |
993 | 0 | static_cast<enum client_hs_state_t>(hs->tls13_state); |
994 | 0 | switch (state) { |
995 | 0 | case state_read_hello_retry_request: |
996 | 0 | return "TLS 1.3 client read_hello_retry_request"; |
997 | 0 | case state_send_second_client_hello: |
998 | 0 | return "TLS 1.3 client send_second_client_hello"; |
999 | 0 | case state_read_server_hello: |
1000 | 0 | return "TLS 1.3 client read_server_hello"; |
1001 | 0 | case state_read_encrypted_extensions: |
1002 | 0 | return "TLS 1.3 client read_encrypted_extensions"; |
1003 | 0 | case state_read_certificate_request: |
1004 | 0 | return "TLS 1.3 client read_certificate_request"; |
1005 | 0 | case state_read_server_certificate: |
1006 | 0 | return "TLS 1.3 client read_server_certificate"; |
1007 | 0 | case state_read_server_certificate_verify: |
1008 | 0 | return "TLS 1.3 client read_server_certificate_verify"; |
1009 | 0 | case state_server_certificate_reverify: |
1010 | 0 | return "TLS 1.3 client server_certificate_reverify"; |
1011 | 0 | case state_read_server_finished: |
1012 | 0 | return "TLS 1.3 client read_server_finished"; |
1013 | 0 | case state_send_end_of_early_data: |
1014 | 0 | return "TLS 1.3 client send_end_of_early_data"; |
1015 | 0 | case state_send_client_encrypted_extensions: |
1016 | 0 | return "TLS 1.3 client send_client_encrypted_extensions"; |
1017 | 0 | case state_send_client_certificate: |
1018 | 0 | return "TLS 1.3 client send_client_certificate"; |
1019 | 0 | case state_send_client_certificate_verify: |
1020 | 0 | return "TLS 1.3 client send_client_certificate_verify"; |
1021 | 0 | case state_complete_second_flight: |
1022 | 0 | return "TLS 1.3 client complete_second_flight"; |
1023 | 0 | case state_done: |
1024 | 0 | return "TLS 1.3 client done"; |
1025 | 0 | } |
1026 | | |
1027 | 0 | return "TLS 1.3 client unknown"; |
1028 | 0 | } |
1029 | | |
1030 | 0 | bool tls13_process_new_session_ticket(SSL *ssl, const SSLMessage &msg) { |
1031 | 0 | if (ssl->s3->write_shutdown != ssl_shutdown_none) { |
1032 | | // Ignore tickets on shutdown. Callers tend to indiscriminately call |
1033 | | // |SSL_shutdown| before destroying an |SSL|, at which point calling the new |
1034 | | // session callback may be confusing. |
1035 | 0 | return true; |
1036 | 0 | } |
1037 | | |
1038 | 0 | CBS body = msg.body; |
1039 | 0 | UniquePtr<SSL_SESSION> session = tls13_create_session_with_ticket(ssl, &body); |
1040 | 0 | if (!session) { |
1041 | 0 | return false; |
1042 | 0 | } |
1043 | | |
1044 | 0 | if ((ssl->session_ctx->session_cache_mode & SSL_SESS_CACHE_CLIENT) && |
1045 | 0 | ssl->session_ctx->new_session_cb != NULL && |
1046 | 0 | ssl->session_ctx->new_session_cb(ssl, session.get())) { |
1047 | | // |new_session_cb|'s return value signals that it took ownership. |
1048 | 0 | session.release(); |
1049 | 0 | } |
1050 | |
|
1051 | 0 | return true; |
1052 | 0 | } |
1053 | | |
1054 | 0 | UniquePtr<SSL_SESSION> tls13_create_session_with_ticket(SSL *ssl, CBS *body) { |
1055 | 0 | UniquePtr<SSL_SESSION> session = SSL_SESSION_dup( |
1056 | 0 | ssl->s3->established_session.get(), SSL_SESSION_INCLUDE_NONAUTH); |
1057 | 0 | if (!session) { |
1058 | 0 | return nullptr; |
1059 | 0 | } |
1060 | | |
1061 | 0 | ssl_session_rebase_time(ssl, session.get()); |
1062 | |
|
1063 | 0 | uint32_t server_timeout; |
1064 | 0 | CBS ticket_nonce, ticket, extensions; |
1065 | 0 | if (!CBS_get_u32(body, &server_timeout) || |
1066 | 0 | !CBS_get_u32(body, &session->ticket_age_add) || |
1067 | 0 | !CBS_get_u8_length_prefixed(body, &ticket_nonce) || |
1068 | 0 | !CBS_get_u16_length_prefixed(body, &ticket) || |
1069 | 0 | !session->ticket.CopyFrom(ticket) || |
1070 | 0 | !CBS_get_u16_length_prefixed(body, &extensions) || |
1071 | 0 | CBS_len(body) != 0) { |
1072 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1073 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1074 | 0 | return nullptr; |
1075 | 0 | } |
1076 | | |
1077 | | // Cap the renewable lifetime by the server advertised value. This avoids |
1078 | | // wasting bandwidth on 0-RTT when we know the server will reject it. |
1079 | 0 | if (session->timeout > server_timeout) { |
1080 | 0 | session->timeout = server_timeout; |
1081 | 0 | } |
1082 | |
|
1083 | 0 | if (!tls13_derive_session_psk(session.get(), ticket_nonce)) { |
1084 | 0 | return nullptr; |
1085 | 0 | } |
1086 | | |
1087 | 0 | SSLExtension early_data(TLSEXT_TYPE_early_data); |
1088 | 0 | uint8_t alert = SSL_AD_DECODE_ERROR; |
1089 | 0 | if (!ssl_parse_extensions(&extensions, &alert, {&early_data}, |
1090 | 0 | /*ignore_unknown=*/true)) { |
1091 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
1092 | 0 | return nullptr; |
1093 | 0 | } |
1094 | | |
1095 | 0 | if (early_data.present) { |
1096 | 0 | if (!CBS_get_u32(&early_data.data, &session->ticket_max_early_data) || |
1097 | 0 | CBS_len(&early_data.data) != 0) { |
1098 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1099 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1100 | 0 | return nullptr; |
1101 | 0 | } |
1102 | | |
1103 | | // QUIC does not use the max_early_data_size parameter and always sets it to |
1104 | | // a fixed value. See RFC 9001, section 4.6.1. |
1105 | 0 | if (ssl->quic_method != nullptr && |
1106 | 0 | session->ticket_max_early_data != 0xffffffff) { |
1107 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
1108 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1109 | 0 | return nullptr; |
1110 | 0 | } |
1111 | 0 | } |
1112 | | |
1113 | | // Historically, OpenSSL filled in fake session IDs for ticket-based sessions. |
1114 | | // Envoy's tests depend on this, although perhaps they shouldn't. |
1115 | 0 | SHA256(CBS_data(&ticket), CBS_len(&ticket), session->session_id); |
1116 | 0 | session->session_id_length = SHA256_DIGEST_LENGTH; |
1117 | |
|
1118 | 0 | session->ticket_age_add_valid = true; |
1119 | 0 | session->not_resumable = false; |
1120 | |
|
1121 | 0 | return session; |
1122 | 0 | } |
1123 | | |
1124 | | BSSL_NAMESPACE_END |