/src/boringssl/ssl/handshake_server.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-2007 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 | | */ |
110 | | /* ==================================================================== |
111 | | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
112 | | * |
113 | | * Portions of the attached software ("Contribution") are developed by |
114 | | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
115 | | * |
116 | | * The Contribution is licensed pursuant to the OpenSSL open source |
117 | | * license provided above. |
118 | | * |
119 | | * ECC cipher suite support in OpenSSL originally written by |
120 | | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
121 | | * |
122 | | */ |
123 | | /* ==================================================================== |
124 | | * Copyright 2005 Nokia. All rights reserved. |
125 | | * |
126 | | * The portions of the attached software ("Contribution") is developed by |
127 | | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
128 | | * license. |
129 | | * |
130 | | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
131 | | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
132 | | * support (see RFC 4279) to OpenSSL. |
133 | | * |
134 | | * No patent licenses or other rights except those expressly stated in |
135 | | * the OpenSSL open source license shall be deemed granted or received |
136 | | * expressly, by implication, estoppel, or otherwise. |
137 | | * |
138 | | * No assurances are provided by Nokia that the Contribution does not |
139 | | * infringe the patent or other intellectual property rights of any third |
140 | | * party or that the license provides you with all the necessary rights |
141 | | * to make use of the Contribution. |
142 | | * |
143 | | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
144 | | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
145 | | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
146 | | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
147 | | * OTHERWISE. */ |
148 | | |
149 | | #include <openssl/ssl.h> |
150 | | |
151 | | #include <assert.h> |
152 | | #include <string.h> |
153 | | |
154 | | #include <openssl/bn.h> |
155 | | #include <openssl/bytestring.h> |
156 | | #include <openssl/cipher.h> |
157 | | #include <openssl/curve25519.h> |
158 | | #include <openssl/digest.h> |
159 | | #include <openssl/ec.h> |
160 | | #include <openssl/ecdsa.h> |
161 | | #include <openssl/err.h> |
162 | | #include <openssl/evp.h> |
163 | | #include <openssl/hmac.h> |
164 | | #include <openssl/md5.h> |
165 | | #include <openssl/mem.h> |
166 | | #include <openssl/nid.h> |
167 | | #include <openssl/rand.h> |
168 | | #include <openssl/x509.h> |
169 | | |
170 | | #include "internal.h" |
171 | | #include "../crypto/internal.h" |
172 | | |
173 | | |
174 | | BSSL_NAMESPACE_BEGIN |
175 | | |
176 | | bool ssl_client_cipher_list_contains_cipher( |
177 | 10.6k | const SSL_CLIENT_HELLO *client_hello, uint16_t id) { |
178 | 10.6k | CBS cipher_suites; |
179 | 10.6k | CBS_init(&cipher_suites, client_hello->cipher_suites, |
180 | 10.6k | client_hello->cipher_suites_len); |
181 | | |
182 | 118k | while (CBS_len(&cipher_suites) > 0) { |
183 | 108k | uint16_t got_id; |
184 | 108k | if (!CBS_get_u16(&cipher_suites, &got_id)) { |
185 | 0 | return false; |
186 | 0 | } |
187 | | |
188 | 108k | if (got_id == id) { |
189 | 466 | return true; |
190 | 466 | } |
191 | 108k | } |
192 | | |
193 | 10.2k | return false; |
194 | 10.6k | } |
195 | | |
196 | | static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
197 | 6.35k | const SSL_CLIENT_HELLO *client_hello) { |
198 | 6.35k | SSL *const ssl = hs->ssl; |
199 | 6.35k | assert(!ssl->s3->have_version); |
200 | 0 | CBS supported_versions, versions; |
201 | 6.35k | if (ssl_client_hello_get_extension(client_hello, &supported_versions, |
202 | 6.35k | TLSEXT_TYPE_supported_versions)) { |
203 | 899 | if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || |
204 | 899 | CBS_len(&supported_versions) != 0 || |
205 | 899 | CBS_len(&versions) == 0) { |
206 | 11 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
207 | 11 | *out_alert = SSL_AD_DECODE_ERROR; |
208 | 11 | return false; |
209 | 11 | } |
210 | 5.45k | } else { |
211 | | // Convert the ClientHello version to an equivalent supported_versions |
212 | | // extension. |
213 | 5.45k | static const uint8_t kTLSVersions[] = { |
214 | 5.45k | 0x03, 0x03, // TLS 1.2 |
215 | 5.45k | 0x03, 0x02, // TLS 1.1 |
216 | 5.45k | 0x03, 0x01, // TLS 1 |
217 | 5.45k | }; |
218 | | |
219 | 5.45k | static const uint8_t kDTLSVersions[] = { |
220 | 5.45k | 0xfe, 0xfd, // DTLS 1.2 |
221 | 5.45k | 0xfe, 0xff, // DTLS 1.0 |
222 | 5.45k | }; |
223 | | |
224 | 5.45k | size_t versions_len = 0; |
225 | 5.45k | if (SSL_is_dtls(ssl)) { |
226 | 2.02k | if (client_hello->version <= DTLS1_2_VERSION) { |
227 | 1.18k | versions_len = 4; |
228 | 1.18k | } else if (client_hello->version <= DTLS1_VERSION) { |
229 | 832 | versions_len = 2; |
230 | 832 | } |
231 | 2.02k | CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len, |
232 | 2.02k | versions_len); |
233 | 3.42k | } else { |
234 | 3.42k | if (client_hello->version >= TLS1_2_VERSION) { |
235 | 2.18k | versions_len = 6; |
236 | 2.18k | } else if (client_hello->version >= TLS1_1_VERSION) { |
237 | 782 | versions_len = 4; |
238 | 782 | } else if (client_hello->version >= TLS1_VERSION) { |
239 | 448 | versions_len = 2; |
240 | 448 | } |
241 | 3.42k | CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len, |
242 | 3.42k | versions_len); |
243 | 3.42k | } |
244 | 5.45k | } |
245 | | |
246 | 6.34k | if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) { |
247 | 38 | return false; |
248 | 38 | } |
249 | | |
250 | | // At this point, the connection's version is known and |ssl->version| is |
251 | | // fixed. Begin enforcing the record-layer version. |
252 | 6.30k | ssl->s3->have_version = true; |
253 | 6.30k | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); |
254 | | |
255 | | // Handle FALLBACK_SCSV. |
256 | 6.30k | if (ssl_client_cipher_list_contains_cipher(client_hello, |
257 | 6.30k | SSL3_CK_FALLBACK_SCSV & 0xffff) && |
258 | 6.30k | ssl_protocol_version(ssl) < hs->max_version) { |
259 | 6 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
260 | 6 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
261 | 6 | return false; |
262 | 6 | } |
263 | | |
264 | 6.29k | return true; |
265 | 6.30k | } |
266 | | |
267 | | static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list( |
268 | 5.25k | const SSL_CLIENT_HELLO *client_hello) { |
269 | 5.25k | CBS cipher_suites; |
270 | 5.25k | CBS_init(&cipher_suites, client_hello->cipher_suites, |
271 | 5.25k | client_hello->cipher_suites_len); |
272 | | |
273 | 5.25k | UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null()); |
274 | 5.25k | if (!sk) { |
275 | 0 | return nullptr; |
276 | 0 | } |
277 | | |
278 | 59.5k | while (CBS_len(&cipher_suites) > 0) { |
279 | 54.3k | uint16_t cipher_suite; |
280 | | |
281 | 54.3k | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
282 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
283 | 0 | return nullptr; |
284 | 0 | } |
285 | | |
286 | 54.3k | const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); |
287 | 54.3k | if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) { |
288 | 0 | return nullptr; |
289 | 0 | } |
290 | 54.3k | } |
291 | | |
292 | 5.25k | return sk; |
293 | 5.25k | } |
294 | | |
295 | | // ssl_get_compatible_server_ciphers determines the key exchange and |
296 | | // authentication cipher suite masks compatible with the server configuration |
297 | | // and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key |
298 | | // exchange mask and |*out_mask_a| to the authentication mask. |
299 | | static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs, |
300 | | uint32_t *out_mask_k, |
301 | 5.25k | uint32_t *out_mask_a) { |
302 | 5.25k | uint32_t mask_k = 0; |
303 | 5.25k | uint32_t mask_a = 0; |
304 | | |
305 | 5.25k | if (ssl_has_certificate(hs)) { |
306 | 5.25k | mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get()); |
307 | 5.25k | if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) { |
308 | 5.25k | mask_k |= SSL_kRSA; |
309 | 5.25k | } |
310 | 5.25k | } |
311 | | |
312 | | // Check for a shared group to consider ECDHE ciphers. |
313 | 5.25k | uint16_t unused; |
314 | 5.25k | if (tls1_get_shared_group(hs, &unused)) { |
315 | 4.50k | mask_k |= SSL_kECDHE; |
316 | 4.50k | } |
317 | | |
318 | | // PSK requires a server callback. |
319 | 5.25k | if (hs->config->psk_server_callback != NULL) { |
320 | 0 | mask_k |= SSL_kPSK; |
321 | 0 | mask_a |= SSL_aPSK; |
322 | 0 | } |
323 | | |
324 | 5.25k | *out_mask_k = mask_k; |
325 | 5.25k | *out_mask_a = mask_a; |
326 | 5.25k | } |
327 | | |
328 | | static const SSL_CIPHER *choose_cipher( |
329 | | SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello, |
330 | 5.25k | const SSLCipherPreferenceList *server_pref) { |
331 | 5.25k | SSL *const ssl = hs->ssl; |
332 | 5.25k | const STACK_OF(SSL_CIPHER) *prio, *allow; |
333 | | // in_group_flags will either be NULL, or will point to an array of bytes |
334 | | // which indicate equal-preference groups in the |prio| stack. See the |
335 | | // comment about |in_group_flags| in the |SSLCipherPreferenceList| |
336 | | // struct. |
337 | 5.25k | const bool *in_group_flags; |
338 | | // group_min contains the minimal index so far found in a group, or -1 if no |
339 | | // such value exists yet. |
340 | 5.25k | int group_min = -1; |
341 | | |
342 | 5.25k | UniquePtr<STACK_OF(SSL_CIPHER)> client_pref = |
343 | 5.25k | ssl_parse_client_cipher_list(client_hello); |
344 | 5.25k | if (!client_pref) { |
345 | 0 | return nullptr; |
346 | 0 | } |
347 | | |
348 | 5.25k | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
349 | 0 | prio = server_pref->ciphers.get(); |
350 | 0 | in_group_flags = server_pref->in_group_flags; |
351 | 0 | allow = client_pref.get(); |
352 | 5.25k | } else { |
353 | 5.25k | prio = client_pref.get(); |
354 | 5.25k | in_group_flags = NULL; |
355 | 5.25k | allow = server_pref->ciphers.get(); |
356 | 5.25k | } |
357 | | |
358 | 5.25k | uint32_t mask_k, mask_a; |
359 | 5.25k | ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a); |
360 | | |
361 | 11.6k | for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) { |
362 | 11.4k | const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i); |
363 | | |
364 | 11.4k | size_t cipher_index; |
365 | 11.4k | if (// Check if the cipher is supported for the current version. |
366 | 11.4k | SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) && |
367 | 11.4k | ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) && |
368 | | // Check the cipher is supported for the server configuration. |
369 | 11.4k | (c->algorithm_mkey & mask_k) && |
370 | 11.4k | (c->algorithm_auth & mask_a) && |
371 | | // Check the cipher is in the |allow| list. |
372 | 11.4k | sk_SSL_CIPHER_find(allow, &cipher_index, c)) { |
373 | 5.00k | if (in_group_flags != NULL && in_group_flags[i]) { |
374 | | // This element of |prio| is in a group. Update the minimum index found |
375 | | // so far and continue looking. |
376 | 0 | if (group_min == -1 || (size_t)group_min > cipher_index) { |
377 | 0 | group_min = cipher_index; |
378 | 0 | } |
379 | 5.00k | } else { |
380 | 5.00k | if (group_min != -1 && (size_t)group_min < cipher_index) { |
381 | 0 | cipher_index = group_min; |
382 | 0 | } |
383 | 5.00k | return sk_SSL_CIPHER_value(allow, cipher_index); |
384 | 5.00k | } |
385 | 5.00k | } |
386 | | |
387 | 6.43k | if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) { |
388 | | // We are about to leave a group, but we found a match in it, so that's |
389 | | // our answer. |
390 | 0 | return sk_SSL_CIPHER_value(allow, group_min); |
391 | 0 | } |
392 | 6.43k | } |
393 | | |
394 | 253 | return nullptr; |
395 | 5.25k | } |
396 | | |
397 | 8.27k | static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) { |
398 | 8.27k | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1); |
399 | 8.27k | hs->state = state12_read_client_hello; |
400 | 8.27k | return ssl_hs_ok; |
401 | 8.27k | } |
402 | | |
403 | | // is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent |
404 | | // from a JDK 11 client with both TLS 1.3 and a prior version enabled. |
405 | 0 | static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) { |
406 | | // JDK 11 ClientHellos contain a number of unusual properties which should |
407 | | // limit false positives. |
408 | | |
409 | | // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern |
410 | | // clients implement ChaCha20-Poly1305. |
411 | 0 | if (ssl_client_cipher_list_contains_cipher( |
412 | 0 | client_hello, TLS1_3_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { |
413 | 0 | return false; |
414 | 0 | } |
415 | | |
416 | | // JDK 11 always sends extensions in a particular order. |
417 | 0 | constexpr uint16_t kMaxFragmentLength = 0x0001; |
418 | 0 | constexpr uint16_t kStatusRequestV2 = 0x0011; |
419 | 0 | static constexpr struct { |
420 | 0 | uint16_t id; |
421 | 0 | bool required; |
422 | 0 | } kJavaExtensions[] = { |
423 | 0 | {TLSEXT_TYPE_server_name, false}, |
424 | 0 | {kMaxFragmentLength, false}, |
425 | 0 | {TLSEXT_TYPE_status_request, false}, |
426 | 0 | {TLSEXT_TYPE_supported_groups, true}, |
427 | 0 | {TLSEXT_TYPE_ec_point_formats, false}, |
428 | 0 | {TLSEXT_TYPE_signature_algorithms, true}, |
429 | | // Java always sends signature_algorithms_cert. |
430 | 0 | {TLSEXT_TYPE_signature_algorithms_cert, true}, |
431 | 0 | {TLSEXT_TYPE_application_layer_protocol_negotiation, false}, |
432 | 0 | {kStatusRequestV2, false}, |
433 | 0 | {TLSEXT_TYPE_extended_master_secret, false}, |
434 | 0 | {TLSEXT_TYPE_supported_versions, true}, |
435 | 0 | {TLSEXT_TYPE_cookie, false}, |
436 | 0 | {TLSEXT_TYPE_psk_key_exchange_modes, true}, |
437 | 0 | {TLSEXT_TYPE_key_share, true}, |
438 | 0 | {TLSEXT_TYPE_renegotiate, false}, |
439 | 0 | {TLSEXT_TYPE_pre_shared_key, false}, |
440 | 0 | }; |
441 | 0 | Span<const uint8_t> sigalgs, sigalgs_cert; |
442 | 0 | bool has_status_request = false, has_status_request_v2 = false; |
443 | 0 | CBS extensions, supported_groups; |
444 | 0 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); |
445 | 0 | for (const auto &java_extension : kJavaExtensions) { |
446 | 0 | CBS copy = extensions; |
447 | 0 | uint16_t id; |
448 | 0 | if (CBS_get_u16(©, &id) && id == java_extension.id) { |
449 | | // The next extension is the one we expected. |
450 | 0 | extensions = copy; |
451 | 0 | CBS body; |
452 | 0 | if (!CBS_get_u16_length_prefixed(&extensions, &body)) { |
453 | 0 | return false; |
454 | 0 | } |
455 | 0 | switch (id) { |
456 | 0 | case TLSEXT_TYPE_status_request: |
457 | 0 | has_status_request = true; |
458 | 0 | break; |
459 | 0 | case kStatusRequestV2: |
460 | 0 | has_status_request_v2 = true; |
461 | 0 | break; |
462 | 0 | case TLSEXT_TYPE_signature_algorithms: |
463 | 0 | sigalgs = body; |
464 | 0 | break; |
465 | 0 | case TLSEXT_TYPE_signature_algorithms_cert: |
466 | 0 | sigalgs_cert = body; |
467 | 0 | break; |
468 | 0 | case TLSEXT_TYPE_supported_groups: |
469 | 0 | supported_groups = body; |
470 | 0 | break; |
471 | 0 | } |
472 | 0 | } else if (java_extension.required) { |
473 | 0 | return false; |
474 | 0 | } |
475 | 0 | } |
476 | 0 | if (CBS_len(&extensions) != 0) { |
477 | 0 | return false; |
478 | 0 | } |
479 | | |
480 | | // JDK 11 never advertises X25519. It is not offered by default, and |
481 | | // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern |
482 | | // clients implement X25519. |
483 | 0 | while (CBS_len(&supported_groups) > 0) { |
484 | 0 | uint16_t group; |
485 | 0 | if (!CBS_get_u16(&supported_groups, &group) || |
486 | 0 | group == SSL_GROUP_X25519) { |
487 | 0 | return false; |
488 | 0 | } |
489 | 0 | } |
490 | | |
491 | 0 | if (// JDK 11 always sends the same contents in signature_algorithms and |
492 | | // signature_algorithms_cert. This is unusual: signature_algorithms_cert, |
493 | | // if omitted, is treated as if it were signature_algorithms. |
494 | 0 | sigalgs != sigalgs_cert || |
495 | | // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it |
496 | | // sends status_request. This is unusual: status_request_v2 is not widely |
497 | | // implemented. |
498 | 0 | has_status_request != has_status_request_v2) { |
499 | 0 | return false; |
500 | 0 | } |
501 | | |
502 | 0 | return true; |
503 | 0 | } |
504 | | |
505 | | static bool decrypt_ech(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
506 | 6.54k | const SSL_CLIENT_HELLO *client_hello) { |
507 | 6.54k | SSL *const ssl = hs->ssl; |
508 | 6.54k | CBS body; |
509 | 6.54k | if (!ssl_client_hello_get_extension(client_hello, &body, |
510 | 6.54k | TLSEXT_TYPE_encrypted_client_hello)) { |
511 | 6.15k | return true; |
512 | 6.15k | } |
513 | 385 | uint8_t type; |
514 | 385 | if (!CBS_get_u8(&body, &type)) { |
515 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
516 | 3 | *out_alert = SSL_AD_DECODE_ERROR; |
517 | 3 | return false; |
518 | 3 | } |
519 | 382 | if (type != ECH_CLIENT_OUTER) { |
520 | 68 | return true; |
521 | 68 | } |
522 | | // This is a ClientHelloOuter ECH extension. Attempt to decrypt it. |
523 | 314 | uint8_t config_id; |
524 | 314 | uint16_t kdf_id, aead_id; |
525 | 314 | CBS enc, payload; |
526 | 314 | if (!CBS_get_u16(&body, &kdf_id) || // |
527 | 314 | !CBS_get_u16(&body, &aead_id) || // |
528 | 314 | !CBS_get_u8(&body, &config_id) || |
529 | 314 | !CBS_get_u16_length_prefixed(&body, &enc) || |
530 | 314 | !CBS_get_u16_length_prefixed(&body, &payload) || // |
531 | 314 | CBS_len(&body) != 0) { |
532 | 16 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
533 | 16 | *out_alert = SSL_AD_DECODE_ERROR; |
534 | 16 | return false; |
535 | 16 | } |
536 | | |
537 | 298 | { |
538 | 298 | MutexReadLock lock(&ssl->ctx->lock); |
539 | 298 | hs->ech_keys = UpRef(ssl->ctx->ech_keys); |
540 | 298 | } |
541 | | |
542 | 298 | if (!hs->ech_keys) { |
543 | 0 | ssl->s3->ech_status = ssl_ech_rejected; |
544 | 0 | return true; |
545 | 0 | } |
546 | | |
547 | 298 | for (const auto &config : hs->ech_keys->configs) { |
548 | 298 | hs->ech_hpke_ctx.Reset(); |
549 | 298 | if (config_id != config->ech_config().config_id || |
550 | 298 | !config->SetupContext(hs->ech_hpke_ctx.get(), kdf_id, aead_id, enc)) { |
551 | | // Ignore the error and try another ECHConfig. |
552 | 76 | ERR_clear_error(); |
553 | 76 | continue; |
554 | 76 | } |
555 | 222 | bool is_decrypt_error; |
556 | 222 | if (!ssl_client_hello_decrypt(hs, out_alert, &is_decrypt_error, |
557 | 222 | &hs->ech_client_hello_buf, client_hello, |
558 | 222 | payload)) { |
559 | 145 | if (is_decrypt_error) { |
560 | | // Ignore the error and try another ECHConfig. |
561 | 13 | ERR_clear_error(); |
562 | | // The |out_alert| calling convention currently relies on a default of |
563 | | // |SSL_AD_DECODE_ERROR|. https://crbug.com/boringssl/373 tracks |
564 | | // switching to sum types, which avoids this. |
565 | 13 | *out_alert = SSL_AD_DECODE_ERROR; |
566 | 13 | continue; |
567 | 13 | } |
568 | 132 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
569 | 132 | return false; |
570 | 145 | } |
571 | 77 | hs->ech_config_id = config_id; |
572 | 77 | ssl->s3->ech_status = ssl_ech_accepted; |
573 | 77 | return true; |
574 | 222 | } |
575 | | |
576 | | // If we did not accept ECH, proceed with the ClientHelloOuter. Note this |
577 | | // could be key mismatch or ECH GREASE, so we must complete the handshake |
578 | | // as usual, except EncryptedExtensions will contain retry configs. |
579 | 89 | ssl->s3->ech_status = ssl_ech_rejected; |
580 | 89 | return true; |
581 | 298 | } |
582 | | |
583 | | static bool extract_sni(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
584 | 6.39k | const SSL_CLIENT_HELLO *client_hello) { |
585 | 6.39k | SSL *const ssl = hs->ssl; |
586 | 6.39k | CBS sni; |
587 | 6.39k | if (!ssl_client_hello_get_extension(client_hello, &sni, |
588 | 6.39k | TLSEXT_TYPE_server_name)) { |
589 | | // No SNI extension to parse. |
590 | 4.12k | return true; |
591 | 4.12k | } |
592 | | |
593 | 2.27k | CBS server_name_list, host_name; |
594 | 2.27k | uint8_t name_type; |
595 | 2.27k | if (!CBS_get_u16_length_prefixed(&sni, &server_name_list) || |
596 | 2.27k | !CBS_get_u8(&server_name_list, &name_type) || |
597 | | // Although the server_name extension was intended to be extensible to |
598 | | // new name types and multiple names, OpenSSL 1.0.x had a bug which meant |
599 | | // different name types will cause an error. Further, RFC 4366 originally |
600 | | // defined syntax inextensibly. RFC 6066 corrected this mistake, but |
601 | | // adding new name types is no longer feasible. |
602 | | // |
603 | | // Act as if the extensibility does not exist to simplify parsing. |
604 | 2.27k | !CBS_get_u16_length_prefixed(&server_name_list, &host_name) || |
605 | 2.27k | CBS_len(&server_name_list) != 0 || |
606 | 2.27k | CBS_len(&sni) != 0) { |
607 | 30 | *out_alert = SSL_AD_DECODE_ERROR; |
608 | 30 | return false; |
609 | 30 | } |
610 | | |
611 | 2.24k | if (name_type != TLSEXT_NAMETYPE_host_name || |
612 | 2.24k | CBS_len(&host_name) == 0 || |
613 | 2.24k | CBS_len(&host_name) > TLSEXT_MAXLEN_host_name || |
614 | 2.24k | CBS_contains_zero_byte(&host_name)) { |
615 | 11 | *out_alert = SSL_AD_UNRECOGNIZED_NAME; |
616 | 11 | return false; |
617 | 11 | } |
618 | | |
619 | | // Copy the hostname as a string. |
620 | 2.23k | char *raw = nullptr; |
621 | 2.23k | if (!CBS_strdup(&host_name, &raw)) { |
622 | 0 | *out_alert = SSL_AD_INTERNAL_ERROR; |
623 | 0 | return false; |
624 | 0 | } |
625 | 2.23k | ssl->s3->hostname.reset(raw); |
626 | | |
627 | 2.23k | hs->should_ack_sni = true; |
628 | 2.23k | return true; |
629 | 2.23k | } |
630 | | |
631 | 25.4k | static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) { |
632 | 25.4k | SSL *const ssl = hs->ssl; |
633 | | |
634 | 25.4k | SSLMessage msg; |
635 | 25.4k | if (!ssl->method->get_message(ssl, &msg)) { |
636 | 18.7k | return ssl_hs_read_message; |
637 | 18.7k | } |
638 | | |
639 | 6.73k | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { |
640 | 27 | return ssl_hs_error; |
641 | 27 | } |
642 | | |
643 | 6.70k | SSL_CLIENT_HELLO client_hello; |
644 | 6.70k | if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) { |
645 | 151 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
646 | 151 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
647 | 151 | return ssl_hs_error; |
648 | 151 | } |
649 | | |
650 | | // ClientHello should be the end of the flight. We check this early to cover |
651 | | // all protocol versions. |
652 | 6.55k | if (ssl->method->has_unprocessed_handshake_data(ssl)) { |
653 | 10 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
654 | 10 | OPENSSL_PUT_ERROR(SSL, SSL_R_EXCESS_HANDSHAKE_DATA); |
655 | 10 | return ssl_hs_error; |
656 | 10 | } |
657 | | |
658 | 6.54k | if (hs->config->handoff) { |
659 | 4 | return ssl_hs_handoff; |
660 | 4 | } |
661 | | |
662 | 6.54k | uint8_t alert = SSL_AD_DECODE_ERROR; |
663 | 6.54k | if (!decrypt_ech(hs, &alert, &client_hello)) { |
664 | 151 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
665 | 151 | return ssl_hs_error; |
666 | 151 | } |
667 | | |
668 | | // ECH may have changed which ClientHello we process. Update |msg| and |
669 | | // |client_hello| in case. |
670 | 6.39k | if (!hs->GetClientHello(&msg, &client_hello)) { |
671 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
672 | 0 | return ssl_hs_error; |
673 | 0 | } |
674 | | |
675 | 6.39k | if (!extract_sni(hs, &alert, &client_hello)) { |
676 | 41 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
677 | 41 | return ssl_hs_error; |
678 | 41 | } |
679 | | |
680 | 6.35k | hs->state = state12_read_client_hello_after_ech; |
681 | 6.35k | return ssl_hs_ok; |
682 | 6.39k | } |
683 | | |
684 | 6.35k | static enum ssl_hs_wait_t do_read_client_hello_after_ech(SSL_HANDSHAKE *hs) { |
685 | 6.35k | SSL *const ssl = hs->ssl; |
686 | | |
687 | 6.35k | SSLMessage msg_unused; |
688 | 6.35k | SSL_CLIENT_HELLO client_hello; |
689 | 6.35k | if (!hs->GetClientHello(&msg_unused, &client_hello)) { |
690 | 0 | return ssl_hs_error; |
691 | 0 | } |
692 | | |
693 | | // Run the early callback. |
694 | 6.35k | if (ssl->ctx->select_certificate_cb != NULL) { |
695 | 0 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
696 | 0 | case ssl_select_cert_retry: |
697 | 0 | return ssl_hs_certificate_selection_pending; |
698 | | |
699 | 0 | case ssl_select_cert_error: |
700 | | // Connection rejected. |
701 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
702 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
703 | 0 | return ssl_hs_error; |
704 | | |
705 | 0 | default: |
706 | 0 | /* fallthrough */; |
707 | 0 | } |
708 | 0 | } |
709 | | |
710 | | // Freeze the version range after the early callback. |
711 | 6.35k | if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) { |
712 | 0 | return ssl_hs_error; |
713 | 0 | } |
714 | | |
715 | 6.35k | if (hs->config->jdk11_workaround && |
716 | 6.35k | is_probably_jdk11_with_tls13(&client_hello)) { |
717 | 0 | hs->apply_jdk11_workaround = true; |
718 | 0 | } |
719 | | |
720 | 6.35k | uint8_t alert = SSL_AD_DECODE_ERROR; |
721 | 6.35k | if (!negotiate_version(hs, &alert, &client_hello)) { |
722 | 55 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
723 | 55 | return ssl_hs_error; |
724 | 55 | } |
725 | | |
726 | 6.29k | hs->client_version = client_hello.version; |
727 | 6.29k | if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
728 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
729 | 0 | return ssl_hs_error; |
730 | 0 | } |
731 | 6.29k | OPENSSL_memcpy(ssl->s3->client_random, client_hello.random, |
732 | 6.29k | client_hello.random_len); |
733 | | |
734 | | // Only null compression is supported. TLS 1.3 further requires the peer |
735 | | // advertise no other compression. |
736 | 6.29k | if (OPENSSL_memchr(client_hello.compression_methods, 0, |
737 | 6.29k | client_hello.compression_methods_len) == NULL || |
738 | 6.29k | (ssl_protocol_version(ssl) >= TLS1_3_VERSION && |
739 | 6.27k | client_hello.compression_methods_len != 1)) { |
740 | 27 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST); |
741 | 27 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
742 | 27 | return ssl_hs_error; |
743 | 27 | } |
744 | | |
745 | | // TLS extensions. |
746 | 6.26k | if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) { |
747 | 178 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
748 | 178 | return ssl_hs_error; |
749 | 178 | } |
750 | | |
751 | 6.09k | hs->state = state12_select_certificate; |
752 | 6.09k | return ssl_hs_ok; |
753 | 6.26k | } |
754 | | |
755 | 6.09k | static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) { |
756 | 6.09k | SSL *const ssl = hs->ssl; |
757 | | |
758 | | // Call |cert_cb| to update server certificates if required. |
759 | 6.09k | if (hs->config->cert->cert_cb != NULL) { |
760 | 0 | int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); |
761 | 0 | if (rv == 0) { |
762 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
763 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
764 | 0 | return ssl_hs_error; |
765 | 0 | } |
766 | 0 | if (rv < 0) { |
767 | 0 | return ssl_hs_x509_lookup; |
768 | 0 | } |
769 | 0 | } |
770 | | |
771 | 6.09k | if (!ssl_on_certificate_selected(hs)) { |
772 | 0 | return ssl_hs_error; |
773 | 0 | } |
774 | | |
775 | 6.09k | if (hs->ocsp_stapling_requested && |
776 | 6.09k | ssl->ctx->legacy_ocsp_callback != nullptr) { |
777 | 0 | switch (ssl->ctx->legacy_ocsp_callback( |
778 | 0 | ssl, ssl->ctx->legacy_ocsp_callback_arg)) { |
779 | 0 | case SSL_TLSEXT_ERR_OK: |
780 | 0 | break; |
781 | 0 | case SSL_TLSEXT_ERR_NOACK: |
782 | 0 | hs->ocsp_stapling_requested = false; |
783 | 0 | break; |
784 | 0 | default: |
785 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR); |
786 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
787 | 0 | return ssl_hs_error; |
788 | 0 | } |
789 | 0 | } |
790 | | |
791 | 6.09k | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { |
792 | | // Jump to the TLS 1.3 state machine. |
793 | 837 | hs->state = state12_tls13; |
794 | 837 | return ssl_hs_ok; |
795 | 837 | } |
796 | | |
797 | | // It should not be possible to negotiate TLS 1.2 with ECH. The |
798 | | // ClientHelloInner decoding function rejects ClientHellos which offer TLS 1.2 |
799 | | // or below. |
800 | 5.25k | assert(ssl->s3->ech_status != ssl_ech_accepted); |
801 | | |
802 | 0 | ssl->s3->early_data_reason = ssl_early_data_protocol_version; |
803 | | |
804 | 5.25k | SSLMessage msg_unused; |
805 | 5.25k | SSL_CLIENT_HELLO client_hello; |
806 | 5.25k | if (!hs->GetClientHello(&msg_unused, &client_hello)) { |
807 | 0 | return ssl_hs_error; |
808 | 0 | } |
809 | | |
810 | | // Negotiate the cipher suite. This must be done after |cert_cb| so the |
811 | | // certificate is finalized. |
812 | 5.25k | SSLCipherPreferenceList *prefs = hs->config->cipher_list |
813 | 5.25k | ? hs->config->cipher_list.get() |
814 | 5.25k | : ssl->ctx->cipher_list.get(); |
815 | 5.25k | hs->new_cipher = choose_cipher(hs, &client_hello, prefs); |
816 | 5.25k | if (hs->new_cipher == NULL) { |
817 | 253 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
818 | 253 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
819 | 253 | return ssl_hs_error; |
820 | 253 | } |
821 | | |
822 | 5.00k | hs->state = state12_select_parameters; |
823 | 5.00k | return ssl_hs_ok; |
824 | 5.25k | } |
825 | | |
826 | 4.40k | static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) { |
827 | 4.40k | enum ssl_hs_wait_t wait = tls13_server_handshake(hs); |
828 | 4.40k | if (wait == ssl_hs_ok) { |
829 | 204 | hs->state = state12_finish_server_handshake; |
830 | 204 | return ssl_hs_ok; |
831 | 204 | } |
832 | | |
833 | 4.19k | return wait; |
834 | 4.40k | } |
835 | | |
836 | 5.00k | static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { |
837 | 5.00k | SSL *const ssl = hs->ssl; |
838 | | |
839 | 5.00k | SSLMessage msg; |
840 | 5.00k | if (!ssl->method->get_message(ssl, &msg)) { |
841 | 0 | return ssl_hs_read_message; |
842 | 0 | } |
843 | | |
844 | 5.00k | SSL_CLIENT_HELLO client_hello; |
845 | 5.00k | if (!ssl_client_hello_init(ssl, &client_hello, msg.body)) { |
846 | 0 | return ssl_hs_error; |
847 | 0 | } |
848 | | |
849 | 5.00k | hs->session_id_len = client_hello.session_id_len; |
850 | | // This is checked in |ssl_client_hello_init|. |
851 | 5.00k | assert(hs->session_id_len <= sizeof(hs->session_id)); |
852 | 0 | OPENSSL_memcpy(hs->session_id, client_hello.session_id, hs->session_id_len); |
853 | | |
854 | | // Determine whether we are doing session resumption. |
855 | 5.00k | UniquePtr<SSL_SESSION> session; |
856 | 5.00k | bool tickets_supported = false, renew_ticket = false; |
857 | 5.00k | enum ssl_hs_wait_t wait = ssl_get_prev_session( |
858 | 5.00k | hs, &session, &tickets_supported, &renew_ticket, &client_hello); |
859 | 5.00k | if (wait != ssl_hs_ok) { |
860 | 0 | return wait; |
861 | 0 | } |
862 | | |
863 | 5.00k | if (session) { |
864 | 94 | if (session->extended_master_secret && !hs->extended_master_secret) { |
865 | | // A ClientHello without EMS that attempts to resume a session with EMS |
866 | | // is fatal to the connection. |
867 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
868 | 3 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
869 | 3 | return ssl_hs_error; |
870 | 3 | } |
871 | | |
872 | 91 | if (!ssl_session_is_resumable(hs, session.get()) || |
873 | | // If the client offers the EMS extension, but the previous session |
874 | | // didn't use it, then negotiate a new session. |
875 | 91 | hs->extended_master_secret != session->extended_master_secret) { |
876 | 38 | session.reset(); |
877 | 38 | } |
878 | 91 | } |
879 | | |
880 | 4.99k | if (session) { |
881 | | // Use the old session. |
882 | 53 | hs->ticket_expected = renew_ticket; |
883 | 53 | ssl->session = std::move(session); |
884 | 53 | ssl->s3->session_reused = true; |
885 | 53 | hs->can_release_private_key = true; |
886 | 4.94k | } else { |
887 | 4.94k | hs->ticket_expected = tickets_supported; |
888 | 4.94k | ssl_set_session(ssl, nullptr); |
889 | 4.94k | if (!ssl_get_new_session(hs)) { |
890 | 0 | return ssl_hs_error; |
891 | 0 | } |
892 | | |
893 | | // Assign a session ID if not using session tickets. |
894 | 4.94k | if (!hs->ticket_expected && |
895 | 4.94k | (ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
896 | 3.88k | hs->new_session->session_id_length = SSL3_SSL_SESSION_ID_LENGTH; |
897 | 3.88k | RAND_bytes(hs->new_session->session_id, |
898 | 3.88k | hs->new_session->session_id_length); |
899 | 3.88k | } |
900 | 4.94k | } |
901 | | |
902 | 4.99k | if (ssl->ctx->dos_protection_cb != NULL && |
903 | 4.99k | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
904 | | // Connection rejected for DOS reasons. |
905 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
906 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
907 | 0 | return ssl_hs_error; |
908 | 0 | } |
909 | | |
910 | 4.99k | if (ssl->session == NULL) { |
911 | 4.94k | hs->new_session->cipher = hs->new_cipher; |
912 | | |
913 | | // Determine whether to request a client certificate. |
914 | 4.94k | hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); |
915 | | // Only request a certificate if Channel ID isn't negotiated. |
916 | 4.94k | if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
917 | 4.94k | hs->channel_id_negotiated) { |
918 | 0 | hs->cert_request = false; |
919 | 0 | } |
920 | | // CertificateRequest may only be sent in certificate-based ciphers. |
921 | 4.94k | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
922 | 0 | hs->cert_request = false; |
923 | 0 | } |
924 | | |
925 | 4.94k | if (!hs->cert_request) { |
926 | | // OpenSSL returns X509_V_OK when no certificates are requested. This is |
927 | | // classed by them as a bug, but it's assumed by at least NGINX. |
928 | 2.25k | hs->new_session->verify_result = X509_V_OK; |
929 | 2.25k | } |
930 | 4.94k | } |
931 | | |
932 | | // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
933 | | // deferred. Complete it now. |
934 | 4.99k | uint8_t alert = SSL_AD_DECODE_ERROR; |
935 | 4.99k | if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { |
936 | 36 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
937 | 36 | return ssl_hs_error; |
938 | 36 | } |
939 | | |
940 | | // Now that all parameters are known, initialize the handshake hash and hash |
941 | | // the ClientHello. |
942 | 4.96k | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || |
943 | 4.96k | !ssl_hash_message(hs, msg)) { |
944 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
945 | 0 | return ssl_hs_error; |
946 | 0 | } |
947 | | |
948 | | // Handback includes the whole handshake transcript, so we cannot free the |
949 | | // transcript buffer in the handback case. |
950 | 4.96k | if (!hs->cert_request && !hs->handback) { |
951 | 2.27k | hs->transcript.FreeBuffer(); |
952 | 2.27k | } |
953 | | |
954 | 4.96k | ssl->method->next_message(ssl); |
955 | | |
956 | 4.96k | hs->state = state12_send_server_hello; |
957 | 4.96k | return ssl_hs_ok; |
958 | 4.96k | } |
959 | | |
960 | 3.11k | static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) { |
961 | 3.11k | out = out.last(in.size()); |
962 | 3.11k | OPENSSL_memcpy(out.data(), in.data(), in.size()); |
963 | 3.11k | } |
964 | | |
965 | 4.96k | static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { |
966 | 4.96k | SSL *const ssl = hs->ssl; |
967 | | |
968 | | // We only accept ChannelIDs on connections with ECDHE in order to avoid a |
969 | | // known attack while we fix ChannelID itself. |
970 | 4.96k | if (hs->channel_id_negotiated && |
971 | 4.96k | (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
972 | 1 | hs->channel_id_negotiated = false; |
973 | 1 | } |
974 | | |
975 | | // If this is a resumption and the original handshake didn't support |
976 | | // ChannelID then we didn't record the original handshake hashes in the |
977 | | // session and so cannot resume with ChannelIDs. |
978 | 4.96k | if (ssl->session != NULL && |
979 | 4.96k | ssl->session->original_handshake_hash_len == 0) { |
980 | 51 | hs->channel_id_negotiated = false; |
981 | 51 | } |
982 | | |
983 | 4.96k | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
984 | 4.96k | if (hints && !hs->hints_requested && |
985 | 4.96k | hints->server_random_tls12.size() == SSL3_RANDOM_SIZE) { |
986 | 55 | OPENSSL_memcpy(ssl->s3->server_random, hints->server_random_tls12.data(), |
987 | 55 | SSL3_RANDOM_SIZE); |
988 | 4.90k | } else { |
989 | 4.90k | struct OPENSSL_timeval now; |
990 | 4.90k | ssl_get_current_time(ssl, &now); |
991 | 4.90k | CRYPTO_store_u32_be(ssl->s3->server_random, |
992 | 4.90k | static_cast<uint32_t>(now.tv_sec)); |
993 | 4.90k | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
994 | 0 | return ssl_hs_error; |
995 | 0 | } |
996 | 4.90k | if (hints && hs->hints_requested && |
997 | 4.90k | !hints->server_random_tls12.CopyFrom(ssl->s3->server_random)) { |
998 | 0 | return ssl_hs_error; |
999 | 0 | } |
1000 | 4.90k | } |
1001 | | |
1002 | | // Implement the TLS 1.3 anti-downgrade feature. |
1003 | 4.96k | if (ssl_supports_version(hs, TLS1_3_VERSION)) { |
1004 | 3.11k | if (ssl_protocol_version(ssl) == TLS1_2_VERSION) { |
1005 | 1.94k | if (hs->apply_jdk11_workaround) { |
1006 | | // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it |
1007 | | // here. However, the signal is only effective if all TLS 1.2 |
1008 | | // ServerHellos produced by the server are marked. Thus we send a |
1009 | | // different non-standard signal for the time being, until JDK 11.0.2 is |
1010 | | // released and clients have updated. |
1011 | 0 | copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom); |
1012 | 1.94k | } else { |
1013 | 1.94k | copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom); |
1014 | 1.94k | } |
1015 | 1.94k | } else { |
1016 | 1.17k | copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom); |
1017 | 1.17k | } |
1018 | 3.11k | } |
1019 | | |
1020 | 4.96k | Span<const uint8_t> session_id; |
1021 | 4.96k | if (ssl->session != nullptr) { |
1022 | | // Echo the session ID from the ClientHello to indicate resumption. |
1023 | 53 | session_id = MakeConstSpan(hs->session_id, hs->session_id_len); |
1024 | 4.90k | } else { |
1025 | 4.90k | session_id = MakeConstSpan(hs->new_session->session_id, |
1026 | 4.90k | hs->new_session->session_id_length); |
1027 | 4.90k | } |
1028 | | |
1029 | 4.96k | ScopedCBB cbb; |
1030 | 4.96k | CBB body, session_id_bytes; |
1031 | 4.96k | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || |
1032 | 4.96k | !CBB_add_u16(&body, ssl->version) || |
1033 | 4.96k | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
1034 | 4.96k | !CBB_add_u8_length_prefixed(&body, &session_id_bytes) || |
1035 | 4.96k | !CBB_add_bytes(&session_id_bytes, session_id.data(), session_id.size()) || |
1036 | 4.96k | !CBB_add_u16(&body, SSL_CIPHER_get_protocol_id(hs->new_cipher)) || |
1037 | 4.96k | !CBB_add_u8(&body, 0 /* no compression */) || |
1038 | 4.96k | !ssl_add_serverhello_tlsext(hs, &body) || |
1039 | 4.96k | !ssl_add_message_cbb(ssl, cbb.get())) { |
1040 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1041 | 0 | return ssl_hs_error; |
1042 | 0 | } |
1043 | | |
1044 | 4.96k | if (ssl->session != nullptr) { |
1045 | | // No additional hints to generate in resumption. |
1046 | 53 | if (hs->hints_requested) { |
1047 | 0 | return ssl_hs_hints_ready; |
1048 | 0 | } |
1049 | 53 | hs->state = state12_send_server_finished; |
1050 | 4.90k | } else { |
1051 | 4.90k | hs->state = state12_send_server_certificate; |
1052 | 4.90k | } |
1053 | 4.96k | return ssl_hs_ok; |
1054 | 4.96k | } |
1055 | | |
1056 | 4.90k | static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) { |
1057 | 4.90k | SSL *const ssl = hs->ssl; |
1058 | 4.90k | ScopedCBB cbb; |
1059 | | |
1060 | 4.90k | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
1061 | 4.90k | if (!ssl_has_certificate(hs)) { |
1062 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
1063 | 0 | return ssl_hs_error; |
1064 | 0 | } |
1065 | | |
1066 | 4.90k | if (!ssl_output_cert_chain(hs)) { |
1067 | 0 | return ssl_hs_error; |
1068 | 0 | } |
1069 | | |
1070 | 4.90k | if (hs->certificate_status_expected) { |
1071 | 865 | CBB body, ocsp_response; |
1072 | 865 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
1073 | 865 | SSL3_MT_CERTIFICATE_STATUS) || |
1074 | 865 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
1075 | 865 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
1076 | 865 | !CBB_add_bytes( |
1077 | 865 | &ocsp_response, |
1078 | 865 | CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()), |
1079 | 865 | CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) || |
1080 | 865 | !ssl_add_message_cbb(ssl, cbb.get())) { |
1081 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1082 | 0 | return ssl_hs_error; |
1083 | 0 | } |
1084 | 865 | } |
1085 | 4.90k | } |
1086 | | |
1087 | | // Assemble ServerKeyExchange parameters if needed. |
1088 | 4.90k | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
1089 | 4.90k | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
1090 | 4.90k | if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) || |
1091 | 4.90k | ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) { |
1092 | | // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend |
1093 | | // the client and server randoms for the signing transcript. |
1094 | 4.31k | CBB child; |
1095 | 4.31k | if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) || |
1096 | 4.31k | !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || |
1097 | 4.31k | !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) { |
1098 | 0 | return ssl_hs_error; |
1099 | 0 | } |
1100 | | |
1101 | | // PSK ciphers begin with an identity hint. |
1102 | 4.31k | if (alg_a & SSL_aPSK) { |
1103 | 0 | size_t len = hs->config->psk_identity_hint == nullptr |
1104 | 0 | ? 0 |
1105 | 0 | : strlen(hs->config->psk_identity_hint.get()); |
1106 | 0 | if (!CBB_add_u16_length_prefixed(cbb.get(), &child) || |
1107 | 0 | !CBB_add_bytes(&child, |
1108 | 0 | (const uint8_t *)hs->config->psk_identity_hint.get(), |
1109 | 0 | len)) { |
1110 | 0 | return ssl_hs_error; |
1111 | 0 | } |
1112 | 0 | } |
1113 | | |
1114 | 4.31k | if (alg_k & SSL_kECDHE) { |
1115 | | // Determine the group to use. |
1116 | 4.31k | uint16_t group_id; |
1117 | 4.31k | if (!tls1_get_shared_group(hs, &group_id)) { |
1118 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1119 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
1120 | 0 | return ssl_hs_error; |
1121 | 0 | } |
1122 | 4.31k | hs->new_session->group_id = group_id; |
1123 | | |
1124 | 4.31k | hs->key_shares[0] = SSLKeyShare::Create(group_id); |
1125 | 4.31k | if (!hs->key_shares[0] || |
1126 | 4.31k | !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) || |
1127 | 4.31k | !CBB_add_u16(cbb.get(), group_id) || |
1128 | 4.31k | !CBB_add_u8_length_prefixed(cbb.get(), &child)) { |
1129 | 0 | return ssl_hs_error; |
1130 | 0 | } |
1131 | | |
1132 | 4.31k | SSL_HANDSHAKE_HINTS *const hints = hs->hints.get(); |
1133 | 4.31k | bool hint_ok = false; |
1134 | 4.31k | if (hints && !hs->hints_requested && |
1135 | 4.31k | hints->ecdhe_group_id == group_id && |
1136 | 4.31k | !hints->ecdhe_public_key.empty() && |
1137 | 4.31k | !hints->ecdhe_private_key.empty()) { |
1138 | 338 | CBS cbs = MakeConstSpan(hints->ecdhe_private_key); |
1139 | 338 | hint_ok = hs->key_shares[0]->DeserializePrivateKey(&cbs); |
1140 | 338 | } |
1141 | 4.31k | if (hint_ok) { |
1142 | | // Reuse the ECDH key from handshake hints. |
1143 | 336 | if (!CBB_add_bytes(&child, hints->ecdhe_public_key.data(), |
1144 | 336 | hints->ecdhe_public_key.size())) { |
1145 | 0 | return ssl_hs_error; |
1146 | 0 | } |
1147 | 3.98k | } else { |
1148 | | // Generate a key, and emit the public half. |
1149 | 3.98k | if (!hs->key_shares[0]->Generate(&child)) { |
1150 | 0 | return ssl_hs_error; |
1151 | 0 | } |
1152 | | // If generating hints, save the ECDHE key. |
1153 | 3.98k | if (hints && hs->hints_requested) { |
1154 | 0 | bssl::ScopedCBB private_key_cbb; |
1155 | 0 | if (!hints->ecdhe_public_key.CopyFrom( |
1156 | 0 | MakeConstSpan(CBB_data(&child), CBB_len(&child))) || |
1157 | 0 | !CBB_init(private_key_cbb.get(), 32) || |
1158 | 0 | !hs->key_shares[0]->SerializePrivateKey(private_key_cbb.get()) || |
1159 | 0 | !CBBFinishArray(private_key_cbb.get(), |
1160 | 0 | &hints->ecdhe_private_key)) { |
1161 | 0 | return ssl_hs_error; |
1162 | 0 | } |
1163 | 0 | hints->ecdhe_group_id = group_id; |
1164 | 0 | } |
1165 | 3.98k | } |
1166 | 4.31k | } else { |
1167 | 0 | assert(alg_k & SSL_kPSK); |
1168 | 0 | } |
1169 | | |
1170 | 4.31k | if (!CBBFinishArray(cbb.get(), &hs->server_params)) { |
1171 | 0 | return ssl_hs_error; |
1172 | 0 | } |
1173 | 4.31k | } |
1174 | | |
1175 | 4.90k | hs->state = state12_send_server_key_exchange; |
1176 | 4.90k | return ssl_hs_ok; |
1177 | 4.90k | } |
1178 | | |
1179 | 4.90k | static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) { |
1180 | 4.90k | SSL *const ssl = hs->ssl; |
1181 | | |
1182 | 4.90k | if (hs->server_params.size() == 0) { |
1183 | 590 | hs->state = state12_send_server_hello_done; |
1184 | 590 | return ssl_hs_ok; |
1185 | 590 | } |
1186 | | |
1187 | 4.31k | ScopedCBB cbb; |
1188 | 4.31k | CBB body, child; |
1189 | 4.31k | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
1190 | 4.31k | SSL3_MT_SERVER_KEY_EXCHANGE) || |
1191 | | // |hs->server_params| contains a prefix for signing. |
1192 | 4.31k | hs->server_params.size() < 2 * SSL3_RANDOM_SIZE || |
1193 | 4.31k | !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE, |
1194 | 4.31k | hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) { |
1195 | 0 | return ssl_hs_error; |
1196 | 0 | } |
1197 | | |
1198 | | // Add a signature. |
1199 | 4.31k | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
1200 | 4.31k | if (!ssl_has_private_key(hs)) { |
1201 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
1202 | 0 | return ssl_hs_error; |
1203 | 0 | } |
1204 | | |
1205 | | // Determine the signature algorithm. |
1206 | 4.31k | uint16_t signature_algorithm; |
1207 | 4.31k | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
1208 | 36 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
1209 | 36 | return ssl_hs_error; |
1210 | 36 | } |
1211 | 4.28k | if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { |
1212 | 2.47k | if (!CBB_add_u16(&body, signature_algorithm)) { |
1213 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1214 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
1215 | 0 | return ssl_hs_error; |
1216 | 0 | } |
1217 | 2.47k | } |
1218 | | |
1219 | | // Add space for the signature. |
1220 | 4.28k | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); |
1221 | 4.28k | uint8_t *ptr; |
1222 | 4.28k | if (!CBB_add_u16_length_prefixed(&body, &child) || |
1223 | 4.28k | !CBB_reserve(&child, &ptr, max_sig_len)) { |
1224 | 0 | return ssl_hs_error; |
1225 | 0 | } |
1226 | | |
1227 | 4.28k | size_t sig_len; |
1228 | 4.28k | switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len, |
1229 | 4.28k | signature_algorithm, hs->server_params)) { |
1230 | 4.28k | case ssl_private_key_success: |
1231 | 4.28k | if (!CBB_did_write(&child, sig_len)) { |
1232 | 0 | return ssl_hs_error; |
1233 | 0 | } |
1234 | 4.28k | break; |
1235 | 4.28k | case ssl_private_key_failure: |
1236 | 0 | return ssl_hs_error; |
1237 | 0 | case ssl_private_key_retry: |
1238 | 0 | return ssl_hs_private_key_operation; |
1239 | 4.28k | } |
1240 | 4.28k | } |
1241 | | |
1242 | 4.28k | hs->can_release_private_key = true; |
1243 | 4.28k | if (!ssl_add_message_cbb(ssl, cbb.get())) { |
1244 | 0 | return ssl_hs_error; |
1245 | 0 | } |
1246 | | |
1247 | 4.28k | hs->server_params.Reset(); |
1248 | | |
1249 | 4.28k | hs->state = state12_send_server_hello_done; |
1250 | 4.28k | return ssl_hs_ok; |
1251 | 4.28k | } |
1252 | | |
1253 | 4.87k | static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) { |
1254 | 4.87k | SSL *const ssl = hs->ssl; |
1255 | 4.87k | if (hs->hints_requested) { |
1256 | 0 | return ssl_hs_hints_ready; |
1257 | 0 | } |
1258 | | |
1259 | 4.87k | ScopedCBB cbb; |
1260 | 4.87k | CBB body; |
1261 | | |
1262 | 4.87k | if (hs->cert_request) { |
1263 | 2.68k | CBB cert_types, sigalgs_cbb; |
1264 | 2.68k | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
1265 | 2.68k | SSL3_MT_CERTIFICATE_REQUEST) || |
1266 | 2.68k | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
1267 | 2.68k | !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) || |
1268 | 2.68k | !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) || |
1269 | 2.68k | (ssl_protocol_version(ssl) >= TLS1_2_VERSION && |
1270 | 2.68k | (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) || |
1271 | 1.46k | !tls12_add_verify_sigalgs(hs, &sigalgs_cbb))) || |
1272 | 2.68k | !ssl_add_client_CA_list(hs, &body) || |
1273 | 2.68k | !ssl_add_message_cbb(ssl, cbb.get())) { |
1274 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1275 | 0 | return ssl_hs_error; |
1276 | 0 | } |
1277 | 2.68k | } |
1278 | | |
1279 | 4.87k | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
1280 | 4.87k | SSL3_MT_SERVER_HELLO_DONE) || |
1281 | 4.87k | !ssl_add_message_cbb(ssl, cbb.get())) { |
1282 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1283 | 0 | return ssl_hs_error; |
1284 | 0 | } |
1285 | | |
1286 | 4.87k | hs->state = state12_read_client_certificate; |
1287 | 4.87k | return ssl_hs_flush; |
1288 | 4.87k | } |
1289 | | |
1290 | 9.69k | static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { |
1291 | 9.69k | SSL *const ssl = hs->ssl; |
1292 | | |
1293 | 9.69k | if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) { |
1294 | 0 | return ssl_hs_handback; |
1295 | 0 | } |
1296 | 9.69k | if (!hs->cert_request) { |
1297 | 2.19k | hs->state = state12_verify_client_certificate; |
1298 | 2.19k | return ssl_hs_ok; |
1299 | 2.19k | } |
1300 | | |
1301 | 7.50k | SSLMessage msg; |
1302 | 7.50k | if (!ssl->method->get_message(ssl, &msg)) { |
1303 | 5.01k | return ssl_hs_read_message; |
1304 | 5.01k | } |
1305 | | |
1306 | 2.49k | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { |
1307 | 3 | return ssl_hs_error; |
1308 | 3 | } |
1309 | | |
1310 | 2.49k | if (!ssl_hash_message(hs, msg)) { |
1311 | 0 | return ssl_hs_error; |
1312 | 0 | } |
1313 | | |
1314 | 2.49k | CBS certificate_msg = msg.body; |
1315 | 2.49k | uint8_t alert = SSL_AD_DECODE_ERROR; |
1316 | 2.49k | if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey, |
1317 | 2.49k | hs->config->retain_only_sha256_of_client_certs |
1318 | 2.49k | ? hs->new_session->peer_sha256 |
1319 | 2.49k | : nullptr, |
1320 | 2.49k | &certificate_msg, ssl->ctx->pool)) { |
1321 | 493 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
1322 | 493 | return ssl_hs_error; |
1323 | 493 | } |
1324 | | |
1325 | 1.99k | if (CBS_len(&certificate_msg) != 0 || |
1326 | 1.99k | !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { |
1327 | 440 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1328 | 440 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1329 | 440 | return ssl_hs_error; |
1330 | 440 | } |
1331 | | |
1332 | 1.55k | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { |
1333 | | // No client certificate so the handshake buffer may be discarded. |
1334 | 12 | hs->transcript.FreeBuffer(); |
1335 | | |
1336 | 12 | if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
1337 | | // Fail for TLS only if we required a certificate |
1338 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
1339 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
1340 | 0 | return ssl_hs_error; |
1341 | 0 | } |
1342 | | |
1343 | | // OpenSSL returns X509_V_OK when no certificates are received. This is |
1344 | | // classed by them as a bug, but it's assumed by at least NGINX. |
1345 | 12 | hs->new_session->verify_result = X509_V_OK; |
1346 | 1.54k | } else if (hs->config->retain_only_sha256_of_client_certs) { |
1347 | | // The hash will have been filled in. |
1348 | 0 | hs->new_session->peer_sha256_valid = true; |
1349 | 0 | } |
1350 | | |
1351 | 1.55k | ssl->method->next_message(ssl); |
1352 | 1.55k | hs->state = state12_verify_client_certificate; |
1353 | 1.55k | return ssl_hs_ok; |
1354 | 1.55k | } |
1355 | | |
1356 | 3.74k | static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) { |
1357 | 3.74k | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) { |
1358 | 1.54k | switch (ssl_verify_peer_cert(hs)) { |
1359 | 1.54k | case ssl_verify_ok: |
1360 | 1.54k | break; |
1361 | 0 | case ssl_verify_invalid: |
1362 | 0 | return ssl_hs_error; |
1363 | 0 | case ssl_verify_retry: |
1364 | 0 | return ssl_hs_certificate_verify; |
1365 | 1.54k | } |
1366 | 1.54k | } |
1367 | | |
1368 | 3.74k | hs->state = state12_read_client_key_exchange; |
1369 | 3.74k | return ssl_hs_ok; |
1370 | 3.74k | } |
1371 | | |
1372 | 7.47k | static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) { |
1373 | 7.47k | SSL *const ssl = hs->ssl; |
1374 | 7.47k | SSLMessage msg; |
1375 | 7.47k | if (!ssl->method->get_message(ssl, &msg)) { |
1376 | 4.65k | return ssl_hs_read_message; |
1377 | 4.65k | } |
1378 | | |
1379 | 2.81k | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) { |
1380 | 4 | return ssl_hs_error; |
1381 | 4 | } |
1382 | | |
1383 | 2.81k | CBS client_key_exchange = msg.body; |
1384 | 2.81k | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
1385 | 2.81k | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
1386 | | |
1387 | | // If using a PSK key exchange, parse the PSK identity. |
1388 | 2.81k | if (alg_a & SSL_aPSK) { |
1389 | 0 | CBS psk_identity; |
1390 | | |
1391 | | // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
1392 | | // then this is the only field in the message. |
1393 | 0 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
1394 | 0 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
1395 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1396 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1397 | 0 | return ssl_hs_error; |
1398 | 0 | } |
1399 | | |
1400 | 0 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
1401 | 0 | CBS_contains_zero_byte(&psk_identity)) { |
1402 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
1403 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
1404 | 0 | return ssl_hs_error; |
1405 | 0 | } |
1406 | 0 | char *raw = nullptr; |
1407 | 0 | if (!CBS_strdup(&psk_identity, &raw)) { |
1408 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
1409 | 0 | return ssl_hs_error; |
1410 | 0 | } |
1411 | 0 | hs->new_session->psk_identity.reset(raw); |
1412 | 0 | } |
1413 | | |
1414 | | // Depending on the key exchange method, compute |premaster_secret|. |
1415 | 2.81k | Array<uint8_t> premaster_secret; |
1416 | 2.81k | if (alg_k & SSL_kRSA) { |
1417 | 39 | CBS encrypted_premaster_secret; |
1418 | 39 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
1419 | 39 | &encrypted_premaster_secret) || |
1420 | 39 | CBS_len(&client_key_exchange) != 0) { |
1421 | 23 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1422 | 23 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1423 | 23 | return ssl_hs_error; |
1424 | 23 | } |
1425 | | |
1426 | | // Allocate a buffer large enough for an RSA decryption. |
1427 | 16 | Array<uint8_t> decrypt_buf; |
1428 | 16 | if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) { |
1429 | 0 | return ssl_hs_error; |
1430 | 0 | } |
1431 | | |
1432 | | // Decrypt with no padding. PKCS#1 padding will be removed as part of the |
1433 | | // timing-sensitive code below. |
1434 | 16 | size_t decrypt_len; |
1435 | 16 | switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len, |
1436 | 16 | decrypt_buf.size(), |
1437 | 16 | encrypted_premaster_secret)) { |
1438 | 11 | case ssl_private_key_success: |
1439 | 11 | break; |
1440 | 5 | case ssl_private_key_failure: |
1441 | 5 | return ssl_hs_error; |
1442 | 0 | case ssl_private_key_retry: |
1443 | 0 | return ssl_hs_private_key_operation; |
1444 | 16 | } |
1445 | | |
1446 | 11 | if (decrypt_len != decrypt_buf.size()) { |
1447 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
1448 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
1449 | 0 | return ssl_hs_error; |
1450 | 0 | } |
1451 | | |
1452 | 11 | CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len); |
1453 | | |
1454 | | // Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
1455 | | // section 7.4.7.1. |
1456 | 11 | if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) || |
1457 | 11 | !RAND_bytes(premaster_secret.data(), premaster_secret.size())) { |
1458 | 0 | return ssl_hs_error; |
1459 | 0 | } |
1460 | | |
1461 | | // The smallest padded premaster is 11 bytes of overhead. Small keys are |
1462 | | // publicly invalid. |
1463 | 11 | if (decrypt_len < 11 + premaster_secret.size()) { |
1464 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
1465 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
1466 | 0 | return ssl_hs_error; |
1467 | 0 | } |
1468 | | |
1469 | | // Check the padding. See RFC 3447, section 7.2.2. |
1470 | 11 | size_t padding_len = decrypt_len - premaster_secret.size(); |
1471 | 11 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
1472 | 11 | constant_time_eq_int_8(decrypt_buf[1], 2); |
1473 | 2.26k | for (size_t i = 2; i < padding_len - 1; i++) { |
1474 | 2.25k | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
1475 | 2.25k | } |
1476 | 11 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
1477 | | |
1478 | | // The premaster secret must begin with |client_version|. This too must be |
1479 | | // checked in constant time (http://eprint.iacr.org/2003/052/). |
1480 | 11 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
1481 | 11 | (unsigned)(hs->client_version >> 8)); |
1482 | 11 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
1483 | 11 | (unsigned)(hs->client_version & 0xff)); |
1484 | | |
1485 | | // Select, in constant time, either the decrypted premaster or the random |
1486 | | // premaster based on |good|. |
1487 | 539 | for (size_t i = 0; i < premaster_secret.size(); i++) { |
1488 | 528 | premaster_secret[i] = constant_time_select_8( |
1489 | 528 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
1490 | 528 | } |
1491 | 2.77k | } else if (alg_k & SSL_kECDHE) { |
1492 | | // Parse the ClientKeyExchange. |
1493 | 2.77k | CBS ciphertext; |
1494 | 2.77k | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ciphertext) || |
1495 | 2.77k | CBS_len(&client_key_exchange) != 0) { |
1496 | 17 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1497 | 17 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1498 | 17 | return ssl_hs_error; |
1499 | 17 | } |
1500 | | |
1501 | | // Decapsulate the premaster secret. |
1502 | 2.75k | uint8_t alert = SSL_AD_DECODE_ERROR; |
1503 | 2.75k | if (!hs->key_shares[0]->Decap(&premaster_secret, &alert, ciphertext)) { |
1504 | 46 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
1505 | 46 | return ssl_hs_error; |
1506 | 46 | } |
1507 | | |
1508 | | // The key exchange state may now be discarded. |
1509 | 2.70k | hs->key_shares[0].reset(); |
1510 | 2.70k | hs->key_shares[1].reset(); |
1511 | 2.70k | } else if (!(alg_k & SSL_kPSK)) { |
1512 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1513 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
1514 | 0 | return ssl_hs_error; |
1515 | 0 | } |
1516 | | |
1517 | | // For a PSK cipher suite, the actual pre-master secret is combined with the |
1518 | | // pre-shared key. |
1519 | 2.72k | if (alg_a & SSL_aPSK) { |
1520 | 0 | if (hs->config->psk_server_callback == NULL) { |
1521 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1522 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
1523 | 0 | return ssl_hs_error; |
1524 | 0 | } |
1525 | | |
1526 | | // Look up the key for the identity. |
1527 | 0 | uint8_t psk[PSK_MAX_PSK_LEN]; |
1528 | 0 | unsigned psk_len = hs->config->psk_server_callback( |
1529 | 0 | ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk)); |
1530 | 0 | if (psk_len > PSK_MAX_PSK_LEN) { |
1531 | 0 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
1532 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
1533 | 0 | return ssl_hs_error; |
1534 | 0 | } else if (psk_len == 0) { |
1535 | | // PSK related to the given identity not found. |
1536 | 0 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
1537 | 0 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY); |
1538 | 0 | return ssl_hs_error; |
1539 | 0 | } |
1540 | | |
1541 | 0 | if (alg_k & SSL_kPSK) { |
1542 | | // In plain PSK, other_secret is a block of 0s with the same length as the |
1543 | | // pre-shared key. |
1544 | 0 | if (!premaster_secret.Init(psk_len)) { |
1545 | 0 | return ssl_hs_error; |
1546 | 0 | } |
1547 | 0 | OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size()); |
1548 | 0 | } |
1549 | | |
1550 | 0 | ScopedCBB new_premaster; |
1551 | 0 | CBB child; |
1552 | 0 | if (!CBB_init(new_premaster.get(), |
1553 | 0 | 2 + psk_len + 2 + premaster_secret.size()) || |
1554 | 0 | !CBB_add_u16_length_prefixed(new_premaster.get(), &child) || |
1555 | 0 | !CBB_add_bytes(&child, premaster_secret.data(), |
1556 | 0 | premaster_secret.size()) || |
1557 | 0 | !CBB_add_u16_length_prefixed(new_premaster.get(), &child) || |
1558 | 0 | !CBB_add_bytes(&child, psk, psk_len) || |
1559 | 0 | !CBBFinishArray(new_premaster.get(), &premaster_secret)) { |
1560 | 0 | return ssl_hs_error; |
1561 | 0 | } |
1562 | 0 | } |
1563 | | |
1564 | 2.72k | if (!ssl_hash_message(hs, msg)) { |
1565 | 0 | return ssl_hs_error; |
1566 | 0 | } |
1567 | | |
1568 | | // Compute the master secret. |
1569 | 2.72k | hs->new_session->secret_length = tls1_generate_master_secret( |
1570 | 2.72k | hs, hs->new_session->secret, premaster_secret); |
1571 | 2.72k | if (hs->new_session->secret_length == 0) { |
1572 | 0 | return ssl_hs_error; |
1573 | 0 | } |
1574 | 2.72k | hs->new_session->extended_master_secret = hs->extended_master_secret; |
1575 | 2.72k | CONSTTIME_DECLASSIFY(hs->new_session->secret, hs->new_session->secret_length); |
1576 | 2.72k | hs->can_release_private_key = true; |
1577 | | |
1578 | 2.72k | ssl->method->next_message(ssl); |
1579 | 2.72k | hs->state = state12_read_client_certificate_verify; |
1580 | 2.72k | return ssl_hs_ok; |
1581 | 2.72k | } |
1582 | | |
1583 | 4.79k | static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) { |
1584 | 4.79k | SSL *const ssl = hs->ssl; |
1585 | | |
1586 | | // Only RSA and ECDSA client certificates are supported, so a |
1587 | | // CertificateVerify is required if and only if there's a client certificate. |
1588 | 4.79k | if (!hs->peer_pubkey) { |
1589 | 1.22k | hs->transcript.FreeBuffer(); |
1590 | 1.22k | hs->state = state12_read_change_cipher_spec; |
1591 | 1.22k | return ssl_hs_ok; |
1592 | 1.22k | } |
1593 | | |
1594 | 3.56k | SSLMessage msg; |
1595 | 3.56k | if (!ssl->method->get_message(ssl, &msg)) { |
1596 | 2.09k | return ssl_hs_read_message; |
1597 | 2.09k | } |
1598 | | |
1599 | 1.47k | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) { |
1600 | 2 | return ssl_hs_error; |
1601 | 2 | } |
1602 | | |
1603 | | // The peer certificate must be valid for signing. |
1604 | 1.47k | const CRYPTO_BUFFER *leaf = |
1605 | 1.47k | sk_CRYPTO_BUFFER_value(hs->new_session->certs.get(), 0); |
1606 | 1.47k | CBS leaf_cbs; |
1607 | 1.47k | CRYPTO_BUFFER_init_CBS(leaf, &leaf_cbs); |
1608 | 1.47k | if (!ssl_cert_check_key_usage(&leaf_cbs, key_usage_digital_signature)) { |
1609 | 21 | return ssl_hs_error; |
1610 | 21 | } |
1611 | | |
1612 | 1.45k | CBS certificate_verify = msg.body, signature; |
1613 | | |
1614 | | // Determine the signature algorithm. |
1615 | 1.45k | uint16_t signature_algorithm = 0; |
1616 | 1.45k | if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { |
1617 | 695 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
1618 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1619 | 3 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1620 | 3 | return ssl_hs_error; |
1621 | 3 | } |
1622 | 692 | uint8_t alert = SSL_AD_DECODE_ERROR; |
1623 | 692 | if (!tls12_check_peer_sigalg(hs, &alert, signature_algorithm)) { |
1624 | 42 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); |
1625 | 42 | return ssl_hs_error; |
1626 | 42 | } |
1627 | 650 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
1628 | 756 | } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, |
1629 | 756 | hs->peer_pubkey.get())) { |
1630 | 1 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
1631 | 1 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE); |
1632 | 1 | return ssl_hs_error; |
1633 | 1 | } |
1634 | | |
1635 | | // Parse and verify the signature. |
1636 | 1.40k | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
1637 | 1.40k | CBS_len(&certificate_verify) != 0) { |
1638 | 8 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1639 | 8 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1640 | 8 | return ssl_hs_error; |
1641 | 8 | } |
1642 | | |
1643 | 1.39k | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, |
1644 | 1.39k | hs->peer_pubkey.get(), hs->transcript.buffer())) { |
1645 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
1646 | 3 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
1647 | 3 | return ssl_hs_error; |
1648 | 3 | } |
1649 | | |
1650 | | // The handshake buffer is no longer necessary, and we may hash the current |
1651 | | // message. |
1652 | 1.39k | hs->transcript.FreeBuffer(); |
1653 | 1.39k | if (!ssl_hash_message(hs, msg)) { |
1654 | 0 | return ssl_hs_error; |
1655 | 0 | } |
1656 | | |
1657 | 1.39k | ssl->method->next_message(ssl); |
1658 | 1.39k | hs->state = state12_read_change_cipher_spec; |
1659 | 1.39k | return ssl_hs_ok; |
1660 | 1.39k | } |
1661 | | |
1662 | 2.67k | static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) { |
1663 | 2.67k | if (hs->handback && hs->ssl->session != NULL) { |
1664 | 0 | return ssl_hs_handback; |
1665 | 0 | } |
1666 | 2.67k | hs->state = state12_process_change_cipher_spec; |
1667 | 2.67k | return ssl_hs_read_change_cipher_spec; |
1668 | 2.67k | } |
1669 | | |
1670 | 772 | static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) { |
1671 | 772 | if (!tls1_change_cipher_state(hs, evp_aead_open)) { |
1672 | 3 | return ssl_hs_error; |
1673 | 3 | } |
1674 | | |
1675 | 769 | hs->state = state12_read_next_proto; |
1676 | 769 | return ssl_hs_ok; |
1677 | 772 | } |
1678 | | |
1679 | 961 | static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) { |
1680 | 961 | SSL *const ssl = hs->ssl; |
1681 | | |
1682 | 961 | if (!hs->next_proto_neg_seen) { |
1683 | 754 | hs->state = state12_read_channel_id; |
1684 | 754 | return ssl_hs_ok; |
1685 | 754 | } |
1686 | | |
1687 | 207 | SSLMessage msg; |
1688 | 207 | if (!ssl->method->get_message(ssl, &msg)) { |
1689 | 201 | return ssl_hs_read_message; |
1690 | 201 | } |
1691 | | |
1692 | 6 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) || |
1693 | 6 | !ssl_hash_message(hs, msg)) { |
1694 | 1 | return ssl_hs_error; |
1695 | 1 | } |
1696 | | |
1697 | 5 | CBS next_protocol = msg.body, selected_protocol, padding; |
1698 | 5 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
1699 | 5 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
1700 | 5 | CBS_len(&next_protocol) != 0) { |
1701 | 3 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
1702 | 3 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
1703 | 3 | return ssl_hs_error; |
1704 | 3 | } |
1705 | | |
1706 | 2 | if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) { |
1707 | 0 | return ssl_hs_error; |
1708 | 0 | } |
1709 | | |
1710 | 2 | ssl->method->next_message(ssl); |
1711 | 2 | hs->state = state12_read_channel_id; |
1712 | 2 | return ssl_hs_ok; |
1713 | 2 | } |
1714 | | |
1715 | 1.13k | static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) { |
1716 | 1.13k | SSL *const ssl = hs->ssl; |
1717 | | |
1718 | 1.13k | if (!hs->channel_id_negotiated) { |
1719 | 547 | hs->state = state12_read_client_finished; |
1720 | 547 | return ssl_hs_ok; |
1721 | 547 | } |
1722 | | |
1723 | 588 | SSLMessage msg; |
1724 | 588 | if (!ssl->method->get_message(ssl, &msg)) { |
1725 | 396 | return ssl_hs_read_message; |
1726 | 396 | } |
1727 | | |
1728 | 192 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || |
1729 | 192 | !tls1_verify_channel_id(hs, msg) || |
1730 | 192 | !ssl_hash_message(hs, msg)) { |
1731 | 28 | return ssl_hs_error; |
1732 | 28 | } |
1733 | | |
1734 | 164 | ssl->method->next_message(ssl); |
1735 | 164 | hs->state = state12_read_client_finished; |
1736 | 164 | return ssl_hs_ok; |
1737 | 192 | } |
1738 | | |
1739 | 1.43k | static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { |
1740 | 1.43k | SSL *const ssl = hs->ssl; |
1741 | 1.43k | enum ssl_hs_wait_t wait = ssl_get_finished(hs); |
1742 | 1.43k | if (wait != ssl_hs_ok) { |
1743 | 1.04k | return wait; |
1744 | 1.04k | } |
1745 | | |
1746 | 396 | if (ssl->session != NULL) { |
1747 | 4 | hs->state = state12_finish_server_handshake; |
1748 | 392 | } else { |
1749 | 392 | hs->state = state12_send_server_finished; |
1750 | 392 | } |
1751 | | |
1752 | | // If this is a full handshake with ChannelID then record the handshake |
1753 | | // hashes in |hs->new_session| in case we need them to verify a |
1754 | | // ChannelID signature on a resumption of this session in the future. |
1755 | 396 | if (ssl->session == NULL && ssl->s3->channel_id_valid && |
1756 | 396 | !tls1_record_handshake_hashes_for_channel_id(hs)) { |
1757 | 0 | return ssl_hs_error; |
1758 | 0 | } |
1759 | | |
1760 | 396 | return ssl_hs_ok; |
1761 | 396 | } |
1762 | | |
1763 | 445 | static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { |
1764 | 445 | SSL *const ssl = hs->ssl; |
1765 | | |
1766 | 445 | if (hs->ticket_expected) { |
1767 | 155 | const SSL_SESSION *session; |
1768 | 155 | UniquePtr<SSL_SESSION> session_copy; |
1769 | 155 | if (ssl->session == NULL) { |
1770 | | // Fix the timeout to measure from the ticket issuance time. |
1771 | 155 | ssl_session_rebase_time(ssl, hs->new_session.get()); |
1772 | 155 | session = hs->new_session.get(); |
1773 | 155 | } else { |
1774 | | // We are renewing an existing session. Duplicate the session to adjust |
1775 | | // the timeout. |
1776 | 0 | session_copy = |
1777 | 0 | SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH); |
1778 | 0 | if (!session_copy) { |
1779 | 0 | return ssl_hs_error; |
1780 | 0 | } |
1781 | | |
1782 | 0 | ssl_session_rebase_time(ssl, session_copy.get()); |
1783 | 0 | session = session_copy.get(); |
1784 | 0 | } |
1785 | | |
1786 | 155 | ScopedCBB cbb; |
1787 | 155 | CBB body, ticket; |
1788 | 155 | if (!ssl->method->init_message(ssl, cbb.get(), &body, |
1789 | 155 | SSL3_MT_NEW_SESSION_TICKET) || |
1790 | 155 | !CBB_add_u32(&body, session->timeout) || |
1791 | 155 | !CBB_add_u16_length_prefixed(&body, &ticket) || |
1792 | 155 | !ssl_encrypt_ticket(hs, &ticket, session) || |
1793 | 155 | !ssl_add_message_cbb(ssl, cbb.get())) { |
1794 | 0 | return ssl_hs_error; |
1795 | 0 | } |
1796 | 155 | } |
1797 | | |
1798 | 445 | if (!ssl->method->add_change_cipher_spec(ssl) || |
1799 | 445 | !tls1_change_cipher_state(hs, evp_aead_seal) || |
1800 | 445 | !ssl_send_finished(hs)) { |
1801 | 0 | return ssl_hs_error; |
1802 | 0 | } |
1803 | | |
1804 | 445 | if (ssl->session != NULL) { |
1805 | 53 | hs->state = state12_read_change_cipher_spec; |
1806 | 392 | } else { |
1807 | 392 | hs->state = state12_finish_server_handshake; |
1808 | 392 | } |
1809 | 445 | return ssl_hs_flush; |
1810 | 445 | } |
1811 | | |
1812 | 600 | static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) { |
1813 | 600 | SSL *const ssl = hs->ssl; |
1814 | | |
1815 | 600 | if (hs->handback) { |
1816 | 0 | return ssl_hs_handback; |
1817 | 0 | } |
1818 | | |
1819 | 600 | ssl->method->on_handshake_complete(ssl); |
1820 | | |
1821 | | // If we aren't retaining peer certificates then we can discard it now. |
1822 | 600 | if (hs->new_session != NULL && |
1823 | 600 | hs->config->retain_only_sha256_of_client_certs) { |
1824 | 0 | hs->new_session->certs.reset(); |
1825 | 0 | ssl->ctx->x509_method->session_clear(hs->new_session.get()); |
1826 | 0 | } |
1827 | | |
1828 | 600 | bool has_new_session = hs->new_session != nullptr; |
1829 | 600 | if (has_new_session) { |
1830 | 596 | assert(ssl->session == nullptr); |
1831 | 0 | ssl->s3->established_session = std::move(hs->new_session); |
1832 | 596 | ssl->s3->established_session->not_resumable = false; |
1833 | 596 | } else { |
1834 | 4 | assert(ssl->session != nullptr); |
1835 | 0 | ssl->s3->established_session = UpRef(ssl->session); |
1836 | 4 | } |
1837 | | |
1838 | 0 | hs->handshake_finalized = true; |
1839 | 600 | ssl->s3->initial_handshake_complete = true; |
1840 | 600 | if (has_new_session) { |
1841 | 596 | ssl_update_cache(ssl); |
1842 | 596 | } |
1843 | | |
1844 | 600 | hs->state = state12_done; |
1845 | 600 | return ssl_hs_ok; |
1846 | 600 | } |
1847 | | |
1848 | 47.0k | enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) { |
1849 | 109k | while (hs->state != state12_done) { |
1850 | 108k | enum ssl_hs_wait_t ret = ssl_hs_error; |
1851 | 108k | enum tls12_server_hs_state_t state = |
1852 | 108k | static_cast<enum tls12_server_hs_state_t>(hs->state); |
1853 | 108k | switch (state) { |
1854 | 8.27k | case state12_start_accept: |
1855 | 8.27k | ret = do_start_accept(hs); |
1856 | 8.27k | break; |
1857 | 25.4k | case state12_read_client_hello: |
1858 | 25.4k | ret = do_read_client_hello(hs); |
1859 | 25.4k | break; |
1860 | 6.35k | case state12_read_client_hello_after_ech: |
1861 | 6.35k | ret = do_read_client_hello_after_ech(hs); |
1862 | 6.35k | break; |
1863 | 6.09k | case state12_select_certificate: |
1864 | 6.09k | ret = do_select_certificate(hs); |
1865 | 6.09k | break; |
1866 | 4.40k | case state12_tls13: |
1867 | 4.40k | ret = do_tls13(hs); |
1868 | 4.40k | break; |
1869 | 5.00k | case state12_select_parameters: |
1870 | 5.00k | ret = do_select_parameters(hs); |
1871 | 5.00k | break; |
1872 | 4.96k | case state12_send_server_hello: |
1873 | 4.96k | ret = do_send_server_hello(hs); |
1874 | 4.96k | break; |
1875 | 4.90k | case state12_send_server_certificate: |
1876 | 4.90k | ret = do_send_server_certificate(hs); |
1877 | 4.90k | break; |
1878 | 4.90k | case state12_send_server_key_exchange: |
1879 | 4.90k | ret = do_send_server_key_exchange(hs); |
1880 | 4.90k | break; |
1881 | 4.87k | case state12_send_server_hello_done: |
1882 | 4.87k | ret = do_send_server_hello_done(hs); |
1883 | 4.87k | break; |
1884 | 9.69k | case state12_read_client_certificate: |
1885 | 9.69k | ret = do_read_client_certificate(hs); |
1886 | 9.69k | break; |
1887 | 3.74k | case state12_verify_client_certificate: |
1888 | 3.74k | ret = do_verify_client_certificate(hs); |
1889 | 3.74k | break; |
1890 | 7.47k | case state12_read_client_key_exchange: |
1891 | 7.47k | ret = do_read_client_key_exchange(hs); |
1892 | 7.47k | break; |
1893 | 4.79k | case state12_read_client_certificate_verify: |
1894 | 4.79k | ret = do_read_client_certificate_verify(hs); |
1895 | 4.79k | break; |
1896 | 2.67k | case state12_read_change_cipher_spec: |
1897 | 2.67k | ret = do_read_change_cipher_spec(hs); |
1898 | 2.67k | break; |
1899 | 772 | case state12_process_change_cipher_spec: |
1900 | 772 | ret = do_process_change_cipher_spec(hs); |
1901 | 772 | break; |
1902 | 961 | case state12_read_next_proto: |
1903 | 961 | ret = do_read_next_proto(hs); |
1904 | 961 | break; |
1905 | 1.13k | case state12_read_channel_id: |
1906 | 1.13k | ret = do_read_channel_id(hs); |
1907 | 1.13k | break; |
1908 | 1.43k | case state12_read_client_finished: |
1909 | 1.43k | ret = do_read_client_finished(hs); |
1910 | 1.43k | break; |
1911 | 445 | case state12_send_server_finished: |
1912 | 445 | ret = do_send_server_finished(hs); |
1913 | 445 | break; |
1914 | 600 | case state12_finish_server_handshake: |
1915 | 600 | ret = do_finish_server_handshake(hs); |
1916 | 600 | break; |
1917 | 0 | case state12_done: |
1918 | 0 | ret = ssl_hs_ok; |
1919 | 0 | break; |
1920 | 108k | } |
1921 | | |
1922 | 108k | if (hs->state != state) { |
1923 | 70.5k | ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); |
1924 | 70.5k | } |
1925 | | |
1926 | 108k | if (ret != ssl_hs_ok) { |
1927 | 46.4k | return ret; |
1928 | 46.4k | } |
1929 | 108k | } |
1930 | | |
1931 | 600 | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1); |
1932 | 600 | return ssl_hs_ok; |
1933 | 47.0k | } |
1934 | | |
1935 | 0 | const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) { |
1936 | 0 | enum tls12_server_hs_state_t state = |
1937 | 0 | static_cast<enum tls12_server_hs_state_t>(hs->state); |
1938 | 0 | switch (state) { |
1939 | 0 | case state12_start_accept: |
1940 | 0 | return "TLS server start_accept"; |
1941 | 0 | case state12_read_client_hello: |
1942 | 0 | return "TLS server read_client_hello"; |
1943 | 0 | case state12_read_client_hello_after_ech: |
1944 | 0 | return "TLS server read_client_hello_after_ech"; |
1945 | 0 | case state12_select_certificate: |
1946 | 0 | return "TLS server select_certificate"; |
1947 | 0 | case state12_tls13: |
1948 | 0 | return tls13_server_handshake_state(hs); |
1949 | 0 | case state12_select_parameters: |
1950 | 0 | return "TLS server select_parameters"; |
1951 | 0 | case state12_send_server_hello: |
1952 | 0 | return "TLS server send_server_hello"; |
1953 | 0 | case state12_send_server_certificate: |
1954 | 0 | return "TLS server send_server_certificate"; |
1955 | 0 | case state12_send_server_key_exchange: |
1956 | 0 | return "TLS server send_server_key_exchange"; |
1957 | 0 | case state12_send_server_hello_done: |
1958 | 0 | return "TLS server send_server_hello_done"; |
1959 | 0 | case state12_read_client_certificate: |
1960 | 0 | return "TLS server read_client_certificate"; |
1961 | 0 | case state12_verify_client_certificate: |
1962 | 0 | return "TLS server verify_client_certificate"; |
1963 | 0 | case state12_read_client_key_exchange: |
1964 | 0 | return "TLS server read_client_key_exchange"; |
1965 | 0 | case state12_read_client_certificate_verify: |
1966 | 0 | return "TLS server read_client_certificate_verify"; |
1967 | 0 | case state12_read_change_cipher_spec: |
1968 | 0 | return "TLS server read_change_cipher_spec"; |
1969 | 0 | case state12_process_change_cipher_spec: |
1970 | 0 | return "TLS server process_change_cipher_spec"; |
1971 | 0 | case state12_read_next_proto: |
1972 | 0 | return "TLS server read_next_proto"; |
1973 | 0 | case state12_read_channel_id: |
1974 | 0 | return "TLS server read_channel_id"; |
1975 | 0 | case state12_read_client_finished: |
1976 | 0 | return "TLS server read_client_finished"; |
1977 | 0 | case state12_send_server_finished: |
1978 | 0 | return "TLS server send_server_finished"; |
1979 | 0 | case state12_finish_server_handshake: |
1980 | 0 | return "TLS server finish_server_handshake"; |
1981 | 0 | case state12_done: |
1982 | 0 | return "TLS server done"; |
1983 | 0 | } |
1984 | | |
1985 | 0 | return "TLS server unknown"; |
1986 | 0 | } |
1987 | | |
1988 | | BSSL_NAMESPACE_END |