/proc/self/cwd/external/boringssl/ssl/handshake.cc
Line | Count | Source (jump to first uncovered line) |
1 | | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
2 | | * All rights reserved. |
3 | | * |
4 | | * This package is an SSL implementation written |
5 | | * by Eric Young (eay@cryptsoft.com). |
6 | | * The implementation was written so as to conform with Netscapes SSL. |
7 | | * |
8 | | * This library is free for commercial and non-commercial use as long as |
9 | | * the following conditions are aheared to. The following conditions |
10 | | * apply to all code found in this distribution, be it the RC4, RSA, |
11 | | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
12 | | * included with this distribution is covered by the same copyright terms |
13 | | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
14 | | * |
15 | | * Copyright remains Eric Young's, and as such any Copyright notices in |
16 | | * the code are not to be removed. |
17 | | * If this package is used in a product, Eric Young should be given attribution |
18 | | * as the author of the parts of the library used. |
19 | | * This can be in the form of a textual message at program startup or |
20 | | * in documentation (online or textual) provided with the package. |
21 | | * |
22 | | * Redistribution and use in source and binary forms, with or without |
23 | | * modification, are permitted provided that the following conditions |
24 | | * are met: |
25 | | * 1. Redistributions of source code must retain the copyright |
26 | | * notice, this list of conditions and the following disclaimer. |
27 | | * 2. Redistributions in binary form must reproduce the above copyright |
28 | | * notice, this list of conditions and the following disclaimer in the |
29 | | * documentation and/or other materials provided with the distribution. |
30 | | * 3. All advertising materials mentioning features or use of this software |
31 | | * must display the following acknowledgement: |
32 | | * "This product includes cryptographic software written by |
33 | | * Eric Young (eay@cryptsoft.com)" |
34 | | * The word 'cryptographic' can be left out if the rouines from the library |
35 | | * being used are not cryptographic related :-). |
36 | | * 4. If you include any Windows specific code (or a derivative thereof) from |
37 | | * the apps directory (application code) you must include an acknowledgement: |
38 | | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
39 | | * |
40 | | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
41 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
42 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
43 | | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
44 | | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
45 | | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
46 | | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
47 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
48 | | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
49 | | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
50 | | * SUCH DAMAGE. |
51 | | * |
52 | | * The licence and distribution terms for any publically available version or |
53 | | * derivative of this code cannot be changed. i.e. this code cannot simply be |
54 | | * copied and put under another distribution licence |
55 | | * [including the GNU Public Licence.] |
56 | | */ |
57 | | /* ==================================================================== |
58 | | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
59 | | * |
60 | | * Redistribution and use in source and binary forms, with or without |
61 | | * modification, are permitted provided that the following conditions |
62 | | * are met: |
63 | | * |
64 | | * 1. Redistributions of source code must retain the above copyright |
65 | | * notice, this list of conditions and the following disclaimer. |
66 | | * |
67 | | * 2. Redistributions in binary form must reproduce the above copyright |
68 | | * notice, this list of conditions and the following disclaimer in |
69 | | * the documentation and/or other materials provided with the |
70 | | * distribution. |
71 | | * |
72 | | * 3. All advertising materials mentioning features or use of this |
73 | | * software must display the following acknowledgment: |
74 | | * "This product includes software developed by the OpenSSL Project |
75 | | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
76 | | * |
77 | | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
78 | | * endorse or promote products derived from this software without |
79 | | * prior written permission. For written permission, please contact |
80 | | * openssl-core@openssl.org. |
81 | | * |
82 | | * 5. Products derived from this software may not be called "OpenSSL" |
83 | | * nor may "OpenSSL" appear in their names without prior written |
84 | | * permission of the OpenSSL Project. |
85 | | * |
86 | | * 6. Redistributions of any form whatsoever must retain the following |
87 | | * acknowledgment: |
88 | | * "This product includes software developed by the OpenSSL Project |
89 | | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
90 | | * |
91 | | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
92 | | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
93 | | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
94 | | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
95 | | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
96 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
97 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
98 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
99 | | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
100 | | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
101 | | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
102 | | * OF THE POSSIBILITY OF SUCH DAMAGE. |
103 | | * ==================================================================== |
104 | | * |
105 | | * This product includes cryptographic software written by Eric Young |
106 | | * (eay@cryptsoft.com). This product includes software written by Tim |
107 | | * Hudson (tjh@cryptsoft.com). */ |
108 | | /* ==================================================================== |
109 | | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
110 | | * ECC cipher suite support in OpenSSL originally developed by |
111 | | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ |
112 | | |
113 | | #include <openssl/ssl.h> |
114 | | |
115 | | #include <assert.h> |
116 | | |
117 | | #include <utility> |
118 | | |
119 | | #include <openssl/rand.h> |
120 | | |
121 | | #include "../crypto/internal.h" |
122 | | #include "internal.h" |
123 | | |
124 | | |
125 | | BSSL_NAMESPACE_BEGIN |
126 | | |
127 | | SSL_HANDSHAKE::SSL_HANDSHAKE(SSL *ssl_arg) |
128 | | : ssl(ssl_arg), |
129 | | ech_is_inner(false), |
130 | | ech_authenticated_reject(false), |
131 | | scts_requested(false), |
132 | | handshake_finalized(false), |
133 | | accept_psk_mode(false), |
134 | | cert_request(false), |
135 | | certificate_status_expected(false), |
136 | | ocsp_stapling_requested(false), |
137 | | should_ack_sni(false), |
138 | | in_false_start(false), |
139 | | in_early_data(false), |
140 | | early_data_offered(false), |
141 | | can_early_read(false), |
142 | | can_early_write(false), |
143 | | next_proto_neg_seen(false), |
144 | | ticket_expected(false), |
145 | | extended_master_secret(false), |
146 | | pending_private_key_op(false), |
147 | | handback(false), |
148 | | hints_requested(false), |
149 | | cert_compression_negotiated(false), |
150 | | apply_jdk11_workaround(false), |
151 | | can_release_private_key(false), |
152 | 10 | channel_id_negotiated(false) { |
153 | 10 | assert(ssl); |
154 | | |
155 | | // Draw entropy for all GREASE values at once. This avoids calling |
156 | | // |RAND_bytes| repeatedly and makes the values consistent within a |
157 | | // connection. The latter is so the second ClientHello matches after |
158 | | // HelloRetryRequest and so supported_groups and key_shares are consistent. |
159 | 10 | RAND_bytes(grease_seed, sizeof(grease_seed)); |
160 | 10 | } |
161 | | |
162 | 10 | SSL_HANDSHAKE::~SSL_HANDSHAKE() { |
163 | 10 | ssl->ctx->x509_method->hs_flush_cached_ca_names(this); |
164 | 10 | } |
165 | | |
166 | 0 | void SSL_HANDSHAKE::ResizeSecrets(size_t hash_len) { |
167 | 0 | if (hash_len > SSL_MAX_MD_SIZE) { |
168 | 0 | abort(); |
169 | 0 | } |
170 | 0 | hash_len_ = hash_len; |
171 | 0 | } |
172 | | |
173 | | bool SSL_HANDSHAKE::GetClientHello(SSLMessage *out_msg, |
174 | 0 | SSL_CLIENT_HELLO *out_client_hello) { |
175 | 0 | if (!ech_client_hello_buf.empty()) { |
176 | | // If the backing buffer is non-empty, the ClientHelloInner has been set. |
177 | 0 | out_msg->is_v2_hello = false; |
178 | 0 | out_msg->type = SSL3_MT_CLIENT_HELLO; |
179 | 0 | out_msg->raw = CBS(ech_client_hello_buf); |
180 | 0 | out_msg->body = MakeConstSpan(ech_client_hello_buf).subspan(4); |
181 | 0 | } else if (!ssl->method->get_message(ssl, out_msg)) { |
182 | | // The message has already been read, so this cannot fail. |
183 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
184 | 0 | return false; |
185 | 0 | } |
186 | | |
187 | 0 | if (!ssl_client_hello_init(ssl, out_client_hello, out_msg->body)) { |
188 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
189 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
190 | 0 | return false; |
191 | 0 | } |
192 | 0 | return true; |
193 | 0 | } |
194 | | |
195 | 10 | UniquePtr<SSL_HANDSHAKE> ssl_handshake_new(SSL *ssl) { |
196 | 10 | UniquePtr<SSL_HANDSHAKE> hs = MakeUnique<SSL_HANDSHAKE>(ssl); |
197 | 10 | if (!hs || !hs->transcript.Init()) { |
198 | 0 | return nullptr; |
199 | 0 | } |
200 | 10 | hs->config = ssl->config.get(); |
201 | 10 | if (!hs->config) { |
202 | 0 | assert(hs->config); |
203 | 0 | return nullptr; |
204 | 0 | } |
205 | 10 | return hs; |
206 | 10 | } |
207 | | |
208 | 0 | bool ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type) { |
209 | 0 | if (msg.type != type) { |
210 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
211 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
212 | 0 | ERR_add_error_dataf("got type %d, wanted type %d", msg.type, type); |
213 | 0 | return false; |
214 | 0 | } |
215 | | |
216 | 0 | return true; |
217 | 0 | } |
218 | | |
219 | 0 | bool ssl_add_message_cbb(SSL *ssl, CBB *cbb) { |
220 | 0 | Array<uint8_t> msg; |
221 | 0 | if (!ssl->method->finish_message(ssl, cbb, &msg) || |
222 | 0 | !ssl->method->add_message(ssl, std::move(msg))) { |
223 | 0 | return false; |
224 | 0 | } |
225 | | |
226 | 0 | return true; |
227 | 0 | } |
228 | | |
229 | 2 | size_t ssl_max_handshake_message_len(const SSL *ssl) { |
230 | | // kMaxMessageLen is the default maximum message size for handshakes which do |
231 | | // not accept peer certificate chains. |
232 | 2 | static const size_t kMaxMessageLen = 16384; |
233 | | |
234 | 2 | if (SSL_in_init(ssl)) { |
235 | 2 | SSL_CONFIG *config = ssl->config.get(); // SSL_in_init() implies not NULL. |
236 | 2 | if ((!ssl->server || (config->verify_mode & SSL_VERIFY_PEER)) && |
237 | 2 | kMaxMessageLen < ssl->max_cert_list) { |
238 | 2 | return ssl->max_cert_list; |
239 | 2 | } |
240 | 0 | return kMaxMessageLen; |
241 | 2 | } |
242 | | |
243 | 0 | if (ssl_protocol_version(ssl) < TLS1_3_VERSION) { |
244 | | // In TLS 1.2 and below, the largest acceptable post-handshake message is |
245 | | // a HelloRequest. |
246 | 0 | return 0; |
247 | 0 | } |
248 | | |
249 | 0 | if (ssl->server) { |
250 | | // The largest acceptable post-handshake message for a server is a |
251 | | // KeyUpdate. We will never initiate post-handshake auth. |
252 | 0 | return 1; |
253 | 0 | } |
254 | | |
255 | | // Clients must accept NewSessionTicket, so allow the default size. |
256 | 0 | return kMaxMessageLen; |
257 | 0 | } |
258 | | |
259 | 0 | bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
260 | | // V2ClientHello messages are pre-hashed. |
261 | 0 | if (msg.is_v2_hello) { |
262 | 0 | return true; |
263 | 0 | } |
264 | | |
265 | 0 | return hs->transcript.Update(msg.raw); |
266 | 0 | } |
267 | | |
268 | | bool ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, |
269 | | std::initializer_list<SSLExtension *> extensions, |
270 | 0 | bool ignore_unknown) { |
271 | | // Reset everything. |
272 | 0 | for (SSLExtension *ext : extensions) { |
273 | 0 | ext->present = false; |
274 | 0 | CBS_init(&ext->data, nullptr, 0); |
275 | 0 | if (!ext->allowed) { |
276 | 0 | assert(!ignore_unknown); |
277 | 0 | } |
278 | 0 | } |
279 | | |
280 | 0 | CBS copy = *cbs; |
281 | 0 | while (CBS_len(©) != 0) { |
282 | 0 | uint16_t type; |
283 | 0 | CBS data; |
284 | 0 | if (!CBS_get_u16(©, &type) || |
285 | 0 | !CBS_get_u16_length_prefixed(©, &data)) { |
286 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
287 | 0 | *out_alert = SSL_AD_DECODE_ERROR; |
288 | 0 | return false; |
289 | 0 | } |
290 | | |
291 | 0 | SSLExtension *found = nullptr; |
292 | 0 | for (SSLExtension *ext : extensions) { |
293 | 0 | if (type == ext->type && ext->allowed) { |
294 | 0 | found = ext; |
295 | 0 | break; |
296 | 0 | } |
297 | 0 | } |
298 | |
|
299 | 0 | if (found == nullptr) { |
300 | 0 | if (ignore_unknown) { |
301 | 0 | continue; |
302 | 0 | } |
303 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
304 | 0 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
305 | 0 | return false; |
306 | 0 | } |
307 | | |
308 | | // Duplicate ext_types are forbidden. |
309 | 0 | if (found->present) { |
310 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); |
311 | 0 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
312 | 0 | return false; |
313 | 0 | } |
314 | | |
315 | 0 | found->present = true; |
316 | 0 | found->data = data; |
317 | 0 | } |
318 | | |
319 | 0 | return true; |
320 | 0 | } |
321 | | |
322 | 0 | enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs) { |
323 | 0 | SSL *const ssl = hs->ssl; |
324 | 0 | const SSL_SESSION *prev_session = ssl->s3->established_session.get(); |
325 | 0 | if (prev_session != NULL) { |
326 | | // If renegotiating, the server must not change the server certificate. See |
327 | | // https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation, |
328 | | // so this check is sufficient to ensure the reported peer certificate never |
329 | | // changes on renegotiation. |
330 | 0 | assert(!ssl->server); |
331 | 0 | if (sk_CRYPTO_BUFFER_num(prev_session->certs.get()) != |
332 | 0 | sk_CRYPTO_BUFFER_num(hs->new_session->certs.get())) { |
333 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED); |
334 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
335 | 0 | return ssl_verify_invalid; |
336 | 0 | } |
337 | | |
338 | 0 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()); |
339 | 0 | i++) { |
340 | 0 | const CRYPTO_BUFFER *old_cert = |
341 | 0 | sk_CRYPTO_BUFFER_value(prev_session->certs.get(), i); |
342 | 0 | const CRYPTO_BUFFER *new_cert = |
343 | 0 | sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), i); |
344 | 0 | if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) || |
345 | 0 | OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert), |
346 | 0 | CRYPTO_BUFFER_data(new_cert), |
347 | 0 | CRYPTO_BUFFER_len(old_cert)) != 0) { |
348 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED); |
349 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
350 | 0 | return ssl_verify_invalid; |
351 | 0 | } |
352 | 0 | } |
353 | | |
354 | | // The certificate is identical, so we may skip re-verifying the |
355 | | // certificate. Since we only authenticated the previous one, copy other |
356 | | // authentication from the established session and ignore what was newly |
357 | | // received. |
358 | 0 | hs->new_session->ocsp_response = UpRef(prev_session->ocsp_response); |
359 | 0 | hs->new_session->signed_cert_timestamp_list = |
360 | 0 | UpRef(prev_session->signed_cert_timestamp_list); |
361 | 0 | hs->new_session->verify_result = prev_session->verify_result; |
362 | 0 | return ssl_verify_ok; |
363 | 0 | } |
364 | | |
365 | 0 | uint8_t alert = SSL_AD_CERTIFICATE_UNKNOWN; |
366 | 0 | enum ssl_verify_result_t ret; |
367 | 0 | if (hs->config->custom_verify_callback != nullptr) { |
368 | 0 | ret = hs->config->custom_verify_callback(ssl, &alert); |
369 | 0 | switch (ret) { |
370 | 0 | case ssl_verify_ok: |
371 | 0 | hs->new_session->verify_result = X509_V_OK; |
372 | 0 | break; |
373 | 0 | case ssl_verify_invalid: |
374 | | // If |SSL_VERIFY_NONE|, the error is non-fatal, but we keep the result. |
375 | 0 | if (hs->config->verify_mode == SSL_VERIFY_NONE) { |
376 | 0 | ERR_clear_error(); |
377 | 0 | ret = ssl_verify_ok; |
378 | 0 | } |
379 | 0 | hs->new_session->verify_result = X509_V_ERR_APPLICATION_VERIFICATION; |
380 | 0 | break; |
381 | 0 | case ssl_verify_retry: |
382 | 0 | break; |
383 | 0 | } |
384 | 0 | } else { |
385 | 0 | ret = ssl->ctx->x509_method->session_verify_cert_chain( |
386 | 0 | hs->new_session.get(), hs, &alert) |
387 | 0 | ? ssl_verify_ok |
388 | 0 | : ssl_verify_invalid; |
389 | 0 | } |
390 | | |
391 | 0 | if (ret == ssl_verify_invalid) { |
392 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
393 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
394 | 0 | } |
395 | | |
396 | | // Emulate OpenSSL's client OCSP callback. OpenSSL verifies certificates |
397 | | // before it receives the OCSP, so it needs a second callback for OCSP. |
398 | 0 | if (ret == ssl_verify_ok && !ssl->server && |
399 | 0 | hs->config->ocsp_stapling_enabled && |
400 | 0 | ssl->ctx->legacy_ocsp_callback != nullptr) { |
401 | 0 | int cb_ret = |
402 | 0 | ssl->ctx->legacy_ocsp_callback(ssl, ssl->ctx->legacy_ocsp_callback_arg); |
403 | 0 | if (cb_ret <= 0) { |
404 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR); |
405 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, |
406 | 0 | cb_ret == 0 ? SSL_AD_BAD_CERTIFICATE_STATUS_RESPONSE |
407 | 0 | : SSL_AD_INTERNAL_ERROR); |
408 | 0 | ret = ssl_verify_invalid; |
409 | 0 | } |
410 | 0 | } |
411 | |
|
412 | 0 | return ret; |
413 | 0 | } |
414 | | |
415 | | // Verifies a stored certificate when resuming a session. A few things are |
416 | | // different from verify_peer_cert: |
417 | | // 1. We can't be renegotiating if we're resuming a session. |
418 | | // 2. The session is immutable, so we don't support verify_mode == |
419 | | // SSL_VERIFY_NONE |
420 | | // 3. We don't call the OCSP callback. |
421 | | // 4. We only support custom verify callbacks. |
422 | | enum ssl_verify_result_t ssl_reverify_peer_cert(SSL_HANDSHAKE *hs, |
423 | 0 | bool send_alert) { |
424 | 0 | SSL *const ssl = hs->ssl; |
425 | 0 | assert(ssl->s3->established_session == nullptr); |
426 | 0 | assert(hs->config->verify_mode != SSL_VERIFY_NONE); |
427 | | |
428 | 0 | uint8_t alert = SSL_AD_CERTIFICATE_UNKNOWN; |
429 | 0 | enum ssl_verify_result_t ret = ssl_verify_invalid; |
430 | 0 | if (hs->config->custom_verify_callback != nullptr) { |
431 | 0 | ret = hs->config->custom_verify_callback(ssl, &alert); |
432 | 0 | } |
433 | |
|
434 | 0 | if (ret == ssl_verify_invalid) { |
435 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
436 | 0 | if (send_alert) { |
437 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
438 | 0 | } |
439 | 0 | } |
440 | |
|
441 | 0 | return ret; |
442 | 0 | } |
443 | | |
444 | | static uint16_t grease_index_to_value(const SSL_HANDSHAKE *hs, |
445 | 0 | enum ssl_grease_index_t index) { |
446 | | // This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. |
447 | 0 | uint16_t ret = hs->grease_seed[index]; |
448 | 0 | ret = (ret & 0xf0) | 0x0a; |
449 | 0 | ret |= ret << 8; |
450 | 0 | return ret; |
451 | 0 | } |
452 | | |
453 | | uint16_t ssl_get_grease_value(const SSL_HANDSHAKE *hs, |
454 | 0 | enum ssl_grease_index_t index) { |
455 | 0 | uint16_t ret = grease_index_to_value(hs, index); |
456 | 0 | if (index == ssl_grease_extension2 && |
457 | 0 | ret == grease_index_to_value(hs, ssl_grease_extension1)) { |
458 | | // The two fake extensions must not have the same value. GREASE values are |
459 | | // of the form 0x1a1a, 0x2a2a, 0x3a3a, etc., so XOR to generate a different |
460 | | // one. |
461 | 0 | ret ^= 0x1010; |
462 | 0 | } |
463 | 0 | return ret; |
464 | 0 | } |
465 | | |
466 | 0 | enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs) { |
467 | 0 | SSL *const ssl = hs->ssl; |
468 | 0 | SSLMessage msg; |
469 | 0 | if (!ssl->method->get_message(ssl, &msg)) { |
470 | 0 | return ssl_hs_read_message; |
471 | 0 | } |
472 | | |
473 | 0 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED)) { |
474 | 0 | return ssl_hs_error; |
475 | 0 | } |
476 | | |
477 | | // Snapshot the finished hash before incorporating the new message. |
478 | 0 | uint8_t finished[EVP_MAX_MD_SIZE]; |
479 | 0 | size_t finished_len; |
480 | 0 | if (!hs->transcript.GetFinishedMAC(finished, &finished_len, |
481 | 0 | ssl_handshake_session(hs), !ssl->server) || |
482 | 0 | !ssl_hash_message(hs, msg)) { |
483 | 0 | return ssl_hs_error; |
484 | 0 | } |
485 | | |
486 | 0 | int finished_ok = CBS_mem_equal(&msg.body, finished, finished_len); |
487 | | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
488 | | finished_ok = 1; |
489 | | #endif |
490 | 0 | if (!finished_ok) { |
491 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
492 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
493 | 0 | return ssl_hs_error; |
494 | 0 | } |
495 | | |
496 | | // Copy the Finished so we can use it for renegotiation checks. |
497 | 0 | if (finished_len > sizeof(ssl->s3->previous_client_finished) || |
498 | 0 | finished_len > sizeof(ssl->s3->previous_server_finished)) { |
499 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
500 | 0 | return ssl_hs_error; |
501 | 0 | } |
502 | | |
503 | 0 | if (ssl->server) { |
504 | 0 | OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len); |
505 | 0 | ssl->s3->previous_client_finished_len = finished_len; |
506 | 0 | } else { |
507 | 0 | OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len); |
508 | 0 | ssl->s3->previous_server_finished_len = finished_len; |
509 | 0 | } |
510 | | |
511 | | // The Finished message should be the end of a flight. |
512 | 0 | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
513 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
514 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
515 | 0 | return ssl_hs_error; |
516 | 0 | } |
517 | | |
518 | 0 | ssl->method->next_message(ssl); |
519 | 0 | return ssl_hs_ok; |
520 | 0 | } |
521 | | |
522 | 0 | bool ssl_send_finished(SSL_HANDSHAKE *hs) { |
523 | 0 | SSL *const ssl = hs->ssl; |
524 | 0 | const SSL_SESSION *session = ssl_handshake_session(hs); |
525 | |
|
526 | 0 | uint8_t finished[EVP_MAX_MD_SIZE]; |
527 | 0 | size_t finished_len; |
528 | 0 | if (!hs->transcript.GetFinishedMAC(finished, &finished_len, session, |
529 | 0 | ssl->server)) { |
530 | 0 | return false; |
531 | 0 | } |
532 | | |
533 | | // Log the master secret, if logging is enabled. |
534 | 0 | if (!ssl_log_secret(ssl, "CLIENT_RANDOM", |
535 | 0 | MakeConstSpan(session->secret, session->secret_length))) { |
536 | 0 | return false; |
537 | 0 | } |
538 | | |
539 | | // Copy the Finished so we can use it for renegotiation checks. |
540 | 0 | if (finished_len > sizeof(ssl->s3->previous_client_finished) || |
541 | 0 | finished_len > sizeof(ssl->s3->previous_server_finished)) { |
542 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
543 | 0 | return false; |
544 | 0 | } |
545 | | |
546 | 0 | if (ssl->server) { |
547 | 0 | OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len); |
548 | 0 | ssl->s3->previous_server_finished_len = finished_len; |
549 | 0 | } else { |
550 | 0 | OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len); |
551 | 0 | ssl->s3->previous_client_finished_len = finished_len; |
552 | 0 | } |
553 | |
|
554 | 0 | ScopedCBB cbb; |
555 | 0 | CBB body; |
556 | 0 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_FINISHED) || |
557 | 0 | !CBB_add_bytes(&body, finished, finished_len) || |
558 | 0 | !ssl_add_message_cbb(ssl, cbb.get())) { |
559 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
560 | 0 | return false; |
561 | 0 | } |
562 | | |
563 | 0 | return true; |
564 | 0 | } |
565 | | |
566 | 0 | bool ssl_send_tls12_certificate(SSL_HANDSHAKE *hs) { |
567 | 0 | ScopedCBB cbb; |
568 | 0 | CBB body, certs, cert; |
569 | 0 | if (!hs->ssl->method->init_message(hs->ssl, cbb.get(), &body, |
570 | 0 | SSL3_MT_CERTIFICATE) || |
571 | 0 | !CBB_add_u24_length_prefixed(&body, &certs)) { |
572 | 0 | return false; |
573 | 0 | } |
574 | | |
575 | 0 | if (hs->credential != nullptr) { |
576 | 0 | assert(hs->credential->type == SSLCredentialType::kX509); |
577 | 0 | STACK_OF(CRYPTO_BUFFER) *chain = hs->credential->chain.get(); |
578 | 0 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(chain); i++) { |
579 | 0 | CRYPTO_BUFFER *buffer = sk_CRYPTO_BUFFER_value(chain, i); |
580 | 0 | if (!CBB_add_u24_length_prefixed(&certs, &cert) || |
581 | 0 | !CBB_add_bytes(&cert, CRYPTO_BUFFER_data(buffer), |
582 | 0 | CRYPTO_BUFFER_len(buffer))) { |
583 | 0 | return false; |
584 | 0 | } |
585 | 0 | } |
586 | 0 | } |
587 | | |
588 | 0 | return ssl_add_message_cbb(hs->ssl, cbb.get()); |
589 | 0 | } |
590 | | |
591 | 0 | const SSL_SESSION *ssl_handshake_session(const SSL_HANDSHAKE *hs) { |
592 | 0 | if (hs->new_session) { |
593 | 0 | return hs->new_session.get(); |
594 | 0 | } |
595 | 0 | return hs->ssl->session.get(); |
596 | 0 | } |
597 | | |
598 | 2 | int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return) { |
599 | 2 | SSL *const ssl = hs->ssl; |
600 | 6 | for (;;) { |
601 | | // Resolve the operation the handshake was waiting on. Each condition may |
602 | | // halt the handshake by returning, or continue executing if the handshake |
603 | | // may immediately proceed. Cases which halt the handshake can clear |
604 | | // |hs->wait| to re-enter the state machine on the next iteration, or leave |
605 | | // it set to keep the condition sticky. |
606 | 6 | switch (hs->wait) { |
607 | 0 | case ssl_hs_error: |
608 | 0 | ERR_restore_state(hs->error.get()); |
609 | 0 | return -1; |
610 | | |
611 | 2 | case ssl_hs_flush: { |
612 | 2 | int ret = ssl->method->flush_flight(ssl); |
613 | 2 | if (ret <= 0) { |
614 | 0 | return ret; |
615 | 0 | } |
616 | 2 | break; |
617 | 2 | } |
618 | | |
619 | 2 | case ssl_hs_read_server_hello: |
620 | 2 | case ssl_hs_read_message: |
621 | 2 | case ssl_hs_read_change_cipher_spec: { |
622 | 2 | if (ssl->quic_method) { |
623 | | // QUIC has no ChangeCipherSpec messages. |
624 | 0 | assert(hs->wait != ssl_hs_read_change_cipher_spec); |
625 | | // The caller should call |SSL_provide_quic_data|. Clear |hs->wait| so |
626 | | // the handshake can check if there is sufficient data next iteration. |
627 | 0 | ssl->s3->rwstate = SSL_ERROR_WANT_READ; |
628 | 0 | hs->wait = ssl_hs_ok; |
629 | 0 | return -1; |
630 | 0 | } |
631 | | |
632 | 2 | uint8_t alert = SSL_AD_DECODE_ERROR; |
633 | 2 | size_t consumed = 0; |
634 | 2 | ssl_open_record_t ret; |
635 | 2 | if (hs->wait == ssl_hs_read_change_cipher_spec) { |
636 | 0 | ret = ssl_open_change_cipher_spec(ssl, &consumed, &alert, |
637 | 0 | ssl->s3->read_buffer.span()); |
638 | 2 | } else { |
639 | 2 | ret = ssl_open_handshake(ssl, &consumed, &alert, |
640 | 2 | ssl->s3->read_buffer.span()); |
641 | 2 | } |
642 | 2 | if (ret == ssl_open_record_error && |
643 | 2 | hs->wait == ssl_hs_read_server_hello) { |
644 | 0 | uint32_t err = ERR_peek_error(); |
645 | 0 | if (ERR_GET_LIB(err) == ERR_LIB_SSL && |
646 | 0 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
647 | | // Add a dedicated error code to the queue for a handshake_failure |
648 | | // alert in response to ClientHello. This matches NSS's client |
649 | | // behavior and gives a better error on a (probable) failure to |
650 | | // negotiate initial parameters. Note: this error code comes after |
651 | | // the original one. |
652 | | // |
653 | | // See https://crbug.com/446505. |
654 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
655 | 0 | } |
656 | 0 | } |
657 | 2 | bool retry; |
658 | 2 | int bio_ret = ssl_handle_open_record(ssl, &retry, ret, consumed, alert); |
659 | 2 | if (bio_ret <= 0) { |
660 | 2 | return bio_ret; |
661 | 2 | } |
662 | 0 | if (retry) { |
663 | 0 | continue; |
664 | 0 | } |
665 | 0 | ssl->s3->read_buffer.DiscardConsumed(); |
666 | 0 | break; |
667 | 0 | } |
668 | | |
669 | 0 | case ssl_hs_read_end_of_early_data: { |
670 | 0 | if (ssl->s3->hs->can_early_read) { |
671 | | // While we are processing early data, the handshake returns early. |
672 | 0 | *out_early_return = true; |
673 | 0 | return 1; |
674 | 0 | } |
675 | 0 | hs->wait = ssl_hs_ok; |
676 | 0 | break; |
677 | 0 | } |
678 | | |
679 | 0 | case ssl_hs_certificate_selection_pending: |
680 | 0 | ssl->s3->rwstate = SSL_ERROR_PENDING_CERTIFICATE; |
681 | 0 | hs->wait = ssl_hs_ok; |
682 | 0 | return -1; |
683 | | |
684 | 0 | case ssl_hs_handoff: |
685 | 0 | ssl->s3->rwstate = SSL_ERROR_HANDOFF; |
686 | 0 | hs->wait = ssl_hs_ok; |
687 | 0 | return -1; |
688 | | |
689 | 0 | case ssl_hs_handback: { |
690 | 0 | int ret = ssl->method->flush_flight(ssl); |
691 | 0 | if (ret <= 0) { |
692 | 0 | return ret; |
693 | 0 | } |
694 | 0 | ssl->s3->rwstate = SSL_ERROR_HANDBACK; |
695 | 0 | hs->wait = ssl_hs_handback; |
696 | 0 | return -1; |
697 | 0 | } |
698 | | |
699 | | // The following cases are associated with callback APIs which expect to |
700 | | // be called each time the state machine runs. Thus they set |hs->wait| |
701 | | // to |ssl_hs_ok| so that, next time, we re-enter the state machine and |
702 | | // call the callback again. |
703 | 0 | case ssl_hs_x509_lookup: |
704 | 0 | ssl->s3->rwstate = SSL_ERROR_WANT_X509_LOOKUP; |
705 | 0 | hs->wait = ssl_hs_ok; |
706 | 0 | return -1; |
707 | 0 | case ssl_hs_private_key_operation: |
708 | 0 | ssl->s3->rwstate = SSL_ERROR_WANT_PRIVATE_KEY_OPERATION; |
709 | 0 | hs->wait = ssl_hs_ok; |
710 | 0 | return -1; |
711 | 0 | case ssl_hs_pending_session: |
712 | 0 | ssl->s3->rwstate = SSL_ERROR_PENDING_SESSION; |
713 | 0 | hs->wait = ssl_hs_ok; |
714 | 0 | return -1; |
715 | 0 | case ssl_hs_pending_ticket: |
716 | 0 | ssl->s3->rwstate = SSL_ERROR_PENDING_TICKET; |
717 | 0 | hs->wait = ssl_hs_ok; |
718 | 0 | return -1; |
719 | 0 | case ssl_hs_certificate_verify: |
720 | 0 | ssl->s3->rwstate = SSL_ERROR_WANT_CERTIFICATE_VERIFY; |
721 | 0 | hs->wait = ssl_hs_ok; |
722 | 0 | return -1; |
723 | | |
724 | 0 | case ssl_hs_early_data_rejected: |
725 | 0 | assert(ssl->s3->early_data_reason != ssl_early_data_unknown); |
726 | 0 | assert(!hs->can_early_write); |
727 | 0 | ssl->s3->rwstate = SSL_ERROR_EARLY_DATA_REJECTED; |
728 | 0 | return -1; |
729 | | |
730 | 0 | case ssl_hs_early_return: |
731 | 0 | if (!ssl->server) { |
732 | | // On ECH reject, the handshake should never complete. |
733 | 0 | assert(ssl->s3->ech_status != ssl_ech_rejected); |
734 | 0 | } |
735 | 0 | *out_early_return = true; |
736 | 0 | hs->wait = ssl_hs_ok; |
737 | 0 | return 1; |
738 | | |
739 | 0 | case ssl_hs_hints_ready: |
740 | 0 | ssl->s3->rwstate = SSL_ERROR_HANDSHAKE_HINTS_READY; |
741 | 0 | return -1; |
742 | | |
743 | 2 | case ssl_hs_ok: |
744 | 2 | break; |
745 | 6 | } |
746 | | |
747 | | // Run the state machine again. |
748 | 4 | hs->wait = ssl->do_handshake(hs); |
749 | 4 | if (hs->wait == ssl_hs_error) { |
750 | 0 | hs->error.reset(ERR_save_state()); |
751 | 0 | return -1; |
752 | 0 | } |
753 | 4 | if (hs->wait == ssl_hs_ok) { |
754 | 0 | if (!ssl->server) { |
755 | | // On ECH reject, the handshake should never complete. |
756 | 0 | assert(ssl->s3->ech_status != ssl_ech_rejected); |
757 | 0 | } |
758 | | // The handshake has completed. |
759 | 0 | *out_early_return = false; |
760 | 0 | return 1; |
761 | 0 | } |
762 | | |
763 | | // Otherwise, loop to the beginning and resolve what was blocking the |
764 | | // handshake. |
765 | 4 | } |
766 | 2 | } |
767 | | |
768 | | BSSL_NAMESPACE_END |