| 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/buf.h> | 
|---|
| 156 | #include <openssl/bytestring.h> | 
|---|
| 157 | #include <openssl/cipher.h> | 
|---|
| 158 | #include <openssl/ec.h> | 
|---|
| 159 | #include <openssl/ecdsa.h> | 
|---|
| 160 | #include <openssl/err.h> | 
|---|
| 161 | #include <openssl/evp.h> | 
|---|
| 162 | #include <openssl/hmac.h> | 
|---|
| 163 | #include <openssl/md5.h> | 
|---|
| 164 | #include <openssl/mem.h> | 
|---|
| 165 | #include <openssl/nid.h> | 
|---|
| 166 | #include <openssl/rand.h> | 
|---|
| 167 | #include <openssl/x509.h> | 
|---|
| 168 |  | 
|---|
| 169 | #include "internal.h" | 
|---|
| 170 | #include "../crypto/internal.h" | 
|---|
| 171 |  | 
|---|
| 172 |  | 
|---|
| 173 | BSSL_NAMESPACE_BEGIN | 
|---|
| 174 |  | 
|---|
| 175 | bool ssl_client_cipher_list_contains_cipher( | 
|---|
| 176 | const SSL_CLIENT_HELLO *client_hello, uint16_t id) { | 
|---|
| 177 | CBS cipher_suites; | 
|---|
| 178 | CBS_init(&cipher_suites, client_hello->cipher_suites, | 
|---|
| 179 | client_hello->cipher_suites_len); | 
|---|
| 180 |  | 
|---|
| 181 | while (CBS_len(&cipher_suites) > 0) { | 
|---|
| 182 | uint16_t got_id; | 
|---|
| 183 | if (!CBS_get_u16(&cipher_suites, &got_id)) { | 
|---|
| 184 | return false; | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | if (got_id == id) { | 
|---|
| 188 | return true; | 
|---|
| 189 | } | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | return false; | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | static bool negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, | 
|---|
| 196 | const SSL_CLIENT_HELLO *client_hello) { | 
|---|
| 197 | SSL *const ssl = hs->ssl; | 
|---|
| 198 | assert(!ssl->s3->have_version); | 
|---|
| 199 | CBS supported_versions, versions; | 
|---|
| 200 | if (ssl_client_hello_get_extension(client_hello, &supported_versions, | 
|---|
| 201 | TLSEXT_TYPE_supported_versions)) { | 
|---|
| 202 | if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || | 
|---|
| 203 | CBS_len(&supported_versions) != 0 || | 
|---|
| 204 | CBS_len(&versions) == 0) { | 
|---|
| 205 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 206 | *out_alert = SSL_AD_DECODE_ERROR; | 
|---|
| 207 | return false; | 
|---|
| 208 | } | 
|---|
| 209 | } else { | 
|---|
| 210 | // Convert the ClientHello version to an equivalent supported_versions | 
|---|
| 211 | // extension. | 
|---|
| 212 | static const uint8_t kTLSVersions[] = { | 
|---|
| 213 | 0x03, 0x03,  // TLS 1.2 | 
|---|
| 214 | 0x03, 0x02,  // TLS 1.1 | 
|---|
| 215 | 0x03, 0x01,  // TLS 1 | 
|---|
| 216 | }; | 
|---|
| 217 |  | 
|---|
| 218 | static const uint8_t kDTLSVersions[] = { | 
|---|
| 219 | 0xfe, 0xfd,  // DTLS 1.2 | 
|---|
| 220 | 0xfe, 0xff,  // DTLS 1.0 | 
|---|
| 221 | }; | 
|---|
| 222 |  | 
|---|
| 223 | size_t versions_len = 0; | 
|---|
| 224 | if (SSL_is_dtls(ssl)) { | 
|---|
| 225 | if (client_hello->version <= DTLS1_2_VERSION) { | 
|---|
| 226 | versions_len = 4; | 
|---|
| 227 | } else if (client_hello->version <= DTLS1_VERSION) { | 
|---|
| 228 | versions_len = 2; | 
|---|
| 229 | } | 
|---|
| 230 | CBS_init(&versions, kDTLSVersions + sizeof(kDTLSVersions) - versions_len, | 
|---|
| 231 | versions_len); | 
|---|
| 232 | } else { | 
|---|
| 233 | if (client_hello->version >= TLS1_2_VERSION) { | 
|---|
| 234 | versions_len = 6; | 
|---|
| 235 | } else if (client_hello->version >= TLS1_1_VERSION) { | 
|---|
| 236 | versions_len = 4; | 
|---|
| 237 | } else if (client_hello->version >= TLS1_VERSION) { | 
|---|
| 238 | versions_len = 2; | 
|---|
| 239 | } | 
|---|
| 240 | CBS_init(&versions, kTLSVersions + sizeof(kTLSVersions) - versions_len, | 
|---|
| 241 | versions_len); | 
|---|
| 242 | } | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | if (!ssl_negotiate_version(hs, out_alert, &ssl->version, &versions)) { | 
|---|
| 246 | return false; | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | // At this point, the connection's version is known and |ssl->version| is | 
|---|
| 250 | // fixed. Begin enforcing the record-layer version. | 
|---|
| 251 | ssl->s3->have_version = true; | 
|---|
| 252 | ssl->s3->aead_write_ctx->SetVersionIfNullCipher(ssl->version); | 
|---|
| 253 |  | 
|---|
| 254 | // Handle FALLBACK_SCSV. | 
|---|
| 255 | if (ssl_client_cipher_list_contains_cipher(client_hello, | 
|---|
| 256 | SSL3_CK_FALLBACK_SCSV & 0xffff) && | 
|---|
| 257 | ssl_protocol_version(ssl) < hs->max_version) { | 
|---|
| 258 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); | 
|---|
| 259 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; | 
|---|
| 260 | return false; | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | return true; | 
|---|
| 264 | } | 
|---|
| 265 |  | 
|---|
| 266 | static UniquePtr<STACK_OF(SSL_CIPHER)> ssl_parse_client_cipher_list( | 
|---|
| 267 | const SSL_CLIENT_HELLO *client_hello) { | 
|---|
| 268 | CBS cipher_suites; | 
|---|
| 269 | CBS_init(&cipher_suites, client_hello->cipher_suites, | 
|---|
| 270 | client_hello->cipher_suites_len); | 
|---|
| 271 |  | 
|---|
| 272 | UniquePtr<STACK_OF(SSL_CIPHER)> sk(sk_SSL_CIPHER_new_null()); | 
|---|
| 273 | if (!sk) { | 
|---|
| 274 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); | 
|---|
| 275 | return nullptr; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | while (CBS_len(&cipher_suites) > 0) { | 
|---|
| 279 | uint16_t cipher_suite; | 
|---|
| 280 |  | 
|---|
| 281 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { | 
|---|
| 282 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); | 
|---|
| 283 | return nullptr; | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); | 
|---|
| 287 | if (c != NULL && !sk_SSL_CIPHER_push(sk.get(), c)) { | 
|---|
| 288 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); | 
|---|
| 289 | return nullptr; | 
|---|
| 290 | } | 
|---|
| 291 | } | 
|---|
| 292 |  | 
|---|
| 293 | return sk; | 
|---|
| 294 | } | 
|---|
| 295 |  | 
|---|
| 296 | // ssl_get_compatible_server_ciphers determines the key exchange and | 
|---|
| 297 | // authentication cipher suite masks compatible with the server configuration | 
|---|
| 298 | // and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key | 
|---|
| 299 | // exchange mask and |*out_mask_a| to the authentication mask. | 
|---|
| 300 | static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs, | 
|---|
| 301 | uint32_t *out_mask_k, | 
|---|
| 302 | uint32_t *out_mask_a) { | 
|---|
| 303 | uint32_t mask_k = 0; | 
|---|
| 304 | uint32_t mask_a = 0; | 
|---|
| 305 |  | 
|---|
| 306 | if (ssl_has_certificate(hs)) { | 
|---|
| 307 | mask_a |= ssl_cipher_auth_mask_for_key(hs->local_pubkey.get()); | 
|---|
| 308 | if (EVP_PKEY_id(hs->local_pubkey.get()) == EVP_PKEY_RSA) { | 
|---|
| 309 | mask_k |= SSL_kRSA; | 
|---|
| 310 | } | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | // Check for a shared group to consider ECDHE ciphers. | 
|---|
| 314 | uint16_t unused; | 
|---|
| 315 | if (tls1_get_shared_group(hs, &unused)) { | 
|---|
| 316 | mask_k |= SSL_kECDHE; | 
|---|
| 317 | } | 
|---|
| 318 |  | 
|---|
| 319 | // PSK requires a server callback. | 
|---|
| 320 | if (hs->config->psk_server_callback != NULL) { | 
|---|
| 321 | mask_k |= SSL_kPSK; | 
|---|
| 322 | mask_a |= SSL_aPSK; | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | *out_mask_k = mask_k; | 
|---|
| 326 | *out_mask_a = mask_a; | 
|---|
| 327 | } | 
|---|
| 328 |  | 
|---|
| 329 | static const SSL_CIPHER *ssl3_choose_cipher( | 
|---|
| 330 | SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello, | 
|---|
| 331 | const SSLCipherPreferenceList *server_pref) { | 
|---|
| 332 | SSL *const ssl = hs->ssl; | 
|---|
| 333 | const STACK_OF(SSL_CIPHER) *prio, *allow; | 
|---|
| 334 | // in_group_flags will either be NULL, or will point to an array of bytes | 
|---|
| 335 | // which indicate equal-preference groups in the |prio| stack. See the | 
|---|
| 336 | // comment about |in_group_flags| in the |SSLCipherPreferenceList| | 
|---|
| 337 | // struct. | 
|---|
| 338 | const bool *in_group_flags; | 
|---|
| 339 | // group_min contains the minimal index so far found in a group, or -1 if no | 
|---|
| 340 | // such value exists yet. | 
|---|
| 341 | int group_min = -1; | 
|---|
| 342 |  | 
|---|
| 343 | UniquePtr<STACK_OF(SSL_CIPHER)> client_pref = | 
|---|
| 344 | ssl_parse_client_cipher_list(client_hello); | 
|---|
| 345 | if (!client_pref) { | 
|---|
| 346 | return nullptr; | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { | 
|---|
| 350 | prio = server_pref->ciphers.get(); | 
|---|
| 351 | in_group_flags = server_pref->in_group_flags; | 
|---|
| 352 | allow = client_pref.get(); | 
|---|
| 353 | } else { | 
|---|
| 354 | prio = client_pref.get(); | 
|---|
| 355 | in_group_flags = NULL; | 
|---|
| 356 | allow = server_pref->ciphers.get(); | 
|---|
| 357 | } | 
|---|
| 358 |  | 
|---|
| 359 | uint32_t mask_k, mask_a; | 
|---|
| 360 | ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a); | 
|---|
| 361 |  | 
|---|
| 362 | for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) { | 
|---|
| 363 | const SSL_CIPHER *c = sk_SSL_CIPHER_value(prio, i); | 
|---|
| 364 |  | 
|---|
| 365 | size_t cipher_index; | 
|---|
| 366 | if (// Check if the cipher is supported for the current version. | 
|---|
| 367 | SSL_CIPHER_get_min_version(c) <= ssl_protocol_version(ssl) && | 
|---|
| 368 | ssl_protocol_version(ssl) <= SSL_CIPHER_get_max_version(c) && | 
|---|
| 369 | // Check the cipher is supported for the server configuration. | 
|---|
| 370 | (c->algorithm_mkey & mask_k) && | 
|---|
| 371 | (c->algorithm_auth & mask_a) && | 
|---|
| 372 | // Check the cipher is in the |allow| list. | 
|---|
| 373 | sk_SSL_CIPHER_find(allow, &cipher_index, c)) { | 
|---|
| 374 | if (in_group_flags != NULL && in_group_flags[i]) { | 
|---|
| 375 | // This element of |prio| is in a group. Update the minimum index found | 
|---|
| 376 | // so far and continue looking. | 
|---|
| 377 | if (group_min == -1 || (size_t)group_min > cipher_index) { | 
|---|
| 378 | group_min = cipher_index; | 
|---|
| 379 | } | 
|---|
| 380 | } else { | 
|---|
| 381 | if (group_min != -1 && (size_t)group_min < cipher_index) { | 
|---|
| 382 | cipher_index = group_min; | 
|---|
| 383 | } | 
|---|
| 384 | return sk_SSL_CIPHER_value(allow, cipher_index); | 
|---|
| 385 | } | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | if (in_group_flags != NULL && !in_group_flags[i] && group_min != -1) { | 
|---|
| 389 | // We are about to leave a group, but we found a match in it, so that's | 
|---|
| 390 | // our answer. | 
|---|
| 391 | return sk_SSL_CIPHER_value(allow, group_min); | 
|---|
| 392 | } | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | return nullptr; | 
|---|
| 396 | } | 
|---|
| 397 |  | 
|---|
| 398 | static enum ssl_hs_wait_t do_start_accept(SSL_HANDSHAKE *hs) { | 
|---|
| 399 | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_START, 1); | 
|---|
| 400 | hs->state = state12_read_client_hello; | 
|---|
| 401 | return ssl_hs_ok; | 
|---|
| 402 | } | 
|---|
| 403 |  | 
|---|
| 404 | // is_probably_jdk11_with_tls13 returns whether |client_hello| was probably sent | 
|---|
| 405 | // from a JDK 11 client with both TLS 1.3 and a prior version enabled. | 
|---|
| 406 | static bool is_probably_jdk11_with_tls13(const SSL_CLIENT_HELLO *client_hello) { | 
|---|
| 407 | // JDK 11 ClientHellos contain a number of unusual properties which should | 
|---|
| 408 | // limit false positives. | 
|---|
| 409 |  | 
|---|
| 410 | // JDK 11 does not support ChaCha20-Poly1305. This is unusual: many modern | 
|---|
| 411 | // clients implement ChaCha20-Poly1305. | 
|---|
| 412 | if (ssl_client_cipher_list_contains_cipher( | 
|---|
| 413 | client_hello, TLS1_CK_CHACHA20_POLY1305_SHA256 & 0xffff)) { | 
|---|
| 414 | return false; | 
|---|
| 415 | } | 
|---|
| 416 |  | 
|---|
| 417 | // JDK 11 always sends extensions in a particular order. | 
|---|
| 418 | constexpr uint16_t kMaxFragmentLength = 0x0001; | 
|---|
| 419 | constexpr uint16_t kStatusRequestV2 = 0x0011; | 
|---|
| 420 | static CONSTEXPR_ARRAY struct { | 
|---|
| 421 | uint16_t id; | 
|---|
| 422 | bool required; | 
|---|
| 423 | } kJavaExtensions[] = { | 
|---|
| 424 | {TLSEXT_TYPE_server_name, false}, | 
|---|
| 425 | {kMaxFragmentLength, false}, | 
|---|
| 426 | {TLSEXT_TYPE_status_request, false}, | 
|---|
| 427 | {TLSEXT_TYPE_supported_groups, true}, | 
|---|
| 428 | {TLSEXT_TYPE_ec_point_formats, false}, | 
|---|
| 429 | {TLSEXT_TYPE_signature_algorithms, true}, | 
|---|
| 430 | // Java always sends signature_algorithms_cert. | 
|---|
| 431 | {TLSEXT_TYPE_signature_algorithms_cert, true}, | 
|---|
| 432 | {TLSEXT_TYPE_application_layer_protocol_negotiation, false}, | 
|---|
| 433 | {kStatusRequestV2, false}, | 
|---|
| 434 | {TLSEXT_TYPE_extended_master_secret, false}, | 
|---|
| 435 | {TLSEXT_TYPE_supported_versions, true}, | 
|---|
| 436 | {TLSEXT_TYPE_cookie, false}, | 
|---|
| 437 | {TLSEXT_TYPE_psk_key_exchange_modes, true}, | 
|---|
| 438 | {TLSEXT_TYPE_key_share, true}, | 
|---|
| 439 | {TLSEXT_TYPE_renegotiate, false}, | 
|---|
| 440 | {TLSEXT_TYPE_pre_shared_key, false}, | 
|---|
| 441 | }; | 
|---|
| 442 | Span<const uint8_t> sigalgs, sigalgs_cert; | 
|---|
| 443 | bool has_status_request = false, has_status_request_v2 = false; | 
|---|
| 444 | CBS extensions, supported_groups; | 
|---|
| 445 | CBS_init(&extensions, client_hello->extensions, client_hello->extensions_len); | 
|---|
| 446 | for (const auto &java_extension : kJavaExtensions) { | 
|---|
| 447 | CBS copy = extensions; | 
|---|
| 448 | uint16_t id; | 
|---|
| 449 | if (CBS_get_u16(©, &id) && id == java_extension.id) { | 
|---|
| 450 | // The next extension is the one we expected. | 
|---|
| 451 | extensions = copy; | 
|---|
| 452 | CBS body; | 
|---|
| 453 | if (!CBS_get_u16_length_prefixed(&extensions, &body)) { | 
|---|
| 454 | return false; | 
|---|
| 455 | } | 
|---|
| 456 | switch (id) { | 
|---|
| 457 | case TLSEXT_TYPE_status_request: | 
|---|
| 458 | has_status_request = true; | 
|---|
| 459 | break; | 
|---|
| 460 | case kStatusRequestV2: | 
|---|
| 461 | has_status_request_v2 = true; | 
|---|
| 462 | break; | 
|---|
| 463 | case TLSEXT_TYPE_signature_algorithms: | 
|---|
| 464 | sigalgs = body; | 
|---|
| 465 | break; | 
|---|
| 466 | case TLSEXT_TYPE_signature_algorithms_cert: | 
|---|
| 467 | sigalgs_cert = body; | 
|---|
| 468 | break; | 
|---|
| 469 | case TLSEXT_TYPE_supported_groups: | 
|---|
| 470 | supported_groups = body; | 
|---|
| 471 | break; | 
|---|
| 472 | } | 
|---|
| 473 | } else if (java_extension.required) { | 
|---|
| 474 | return false; | 
|---|
| 475 | } | 
|---|
| 476 | } | 
|---|
| 477 | if (CBS_len(&extensions) != 0) { | 
|---|
| 478 | return false; | 
|---|
| 479 | } | 
|---|
| 480 |  | 
|---|
| 481 | // JDK 11 never advertises X25519. It is not offered by default, and | 
|---|
| 482 | // -Djdk.tls.namedGroups=x25519 does not work. This is unusual: many modern | 
|---|
| 483 | // clients implement X25519. | 
|---|
| 484 | while (CBS_len(&supported_groups) > 0) { | 
|---|
| 485 | uint16_t group; | 
|---|
| 486 | if (!CBS_get_u16(&supported_groups, &group) || | 
|---|
| 487 | group == SSL_CURVE_X25519) { | 
|---|
| 488 | return false; | 
|---|
| 489 | } | 
|---|
| 490 | } | 
|---|
| 491 |  | 
|---|
| 492 | if (// JDK 11 always sends the same contents in signature_algorithms and | 
|---|
| 493 | // signature_algorithms_cert. This is unusual: signature_algorithms_cert, | 
|---|
| 494 | // if omitted, is treated as if it were signature_algorithms. | 
|---|
| 495 | sigalgs != sigalgs_cert || | 
|---|
| 496 | // When TLS 1.2 or below is enabled, JDK 11 sends status_request_v2 iff it | 
|---|
| 497 | // sends status_request. This is unusual: status_request_v2 is not widely | 
|---|
| 498 | // implemented. | 
|---|
| 499 | has_status_request != has_status_request_v2) { | 
|---|
| 500 | return false; | 
|---|
| 501 | } | 
|---|
| 502 |  | 
|---|
| 503 | return true; | 
|---|
| 504 | } | 
|---|
| 505 |  | 
|---|
| 506 | static enum ssl_hs_wait_t do_read_client_hello(SSL_HANDSHAKE *hs) { | 
|---|
| 507 | SSL *const ssl = hs->ssl; | 
|---|
| 508 |  | 
|---|
| 509 | SSLMessage msg; | 
|---|
| 510 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 511 | return ssl_hs_read_message; | 
|---|
| 512 | } | 
|---|
| 513 |  | 
|---|
| 514 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_HELLO)) { | 
|---|
| 515 | return ssl_hs_error; | 
|---|
| 516 | } | 
|---|
| 517 |  | 
|---|
| 518 | SSL_CLIENT_HELLO client_hello; | 
|---|
| 519 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { | 
|---|
| 520 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 521 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 522 | return ssl_hs_error; | 
|---|
| 523 | } | 
|---|
| 524 |  | 
|---|
| 525 | if (hs->config->handoff) { | 
|---|
| 526 | return ssl_hs_handoff; | 
|---|
| 527 | } | 
|---|
| 528 |  | 
|---|
| 529 | // Run the early callback. | 
|---|
| 530 | if (ssl->ctx->select_certificate_cb != NULL) { | 
|---|
| 531 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { | 
|---|
| 532 | case ssl_select_cert_retry: | 
|---|
| 533 | return ssl_hs_certificate_selection_pending; | 
|---|
| 534 |  | 
|---|
| 535 | case ssl_select_cert_error: | 
|---|
| 536 | // Connection rejected. | 
|---|
| 537 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); | 
|---|
| 538 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 539 | return ssl_hs_error; | 
|---|
| 540 |  | 
|---|
| 541 | default: | 
|---|
| 542 | /* fallthrough */; | 
|---|
| 543 | } | 
|---|
| 544 | } | 
|---|
| 545 |  | 
|---|
| 546 | // Freeze the version range after the early callback. | 
|---|
| 547 | if (!ssl_get_version_range(hs, &hs->min_version, &hs->max_version)) { | 
|---|
| 548 | return ssl_hs_error; | 
|---|
| 549 | } | 
|---|
| 550 |  | 
|---|
| 551 | if (hs->config->jdk11_workaround && | 
|---|
| 552 | is_probably_jdk11_with_tls13(&client_hello)) { | 
|---|
| 553 | hs->apply_jdk11_workaround = true; | 
|---|
| 554 | } | 
|---|
| 555 |  | 
|---|
| 556 | uint8_t alert = SSL_AD_DECODE_ERROR; | 
|---|
| 557 | if (!negotiate_version(hs, &alert, &client_hello)) { | 
|---|
| 558 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); | 
|---|
| 559 | return ssl_hs_error; | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | hs->client_version = client_hello.version; | 
|---|
| 563 | if (client_hello.random_len != SSL3_RANDOM_SIZE) { | 
|---|
| 564 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 565 | return ssl_hs_error; | 
|---|
| 566 | } | 
|---|
| 567 | OPENSSL_memcpy(ssl->s3->client_random, client_hello.random, | 
|---|
| 568 | client_hello.random_len); | 
|---|
| 569 |  | 
|---|
| 570 | // Only null compression is supported. TLS 1.3 further requires the peer | 
|---|
| 571 | // advertise no other compression. | 
|---|
| 572 | if (OPENSSL_memchr(client_hello.compression_methods, 0, | 
|---|
| 573 | client_hello.compression_methods_len) == NULL || | 
|---|
| 574 | (ssl_protocol_version(ssl) >= TLS1_3_VERSION && | 
|---|
| 575 | client_hello.compression_methods_len != 1)) { | 
|---|
| 576 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST); | 
|---|
| 577 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); | 
|---|
| 578 | return ssl_hs_error; | 
|---|
| 579 | } | 
|---|
| 580 |  | 
|---|
| 581 | // TLS extensions. | 
|---|
| 582 | if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) { | 
|---|
| 583 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); | 
|---|
| 584 | return ssl_hs_error; | 
|---|
| 585 | } | 
|---|
| 586 |  | 
|---|
| 587 | hs->state = state12_select_certificate; | 
|---|
| 588 | return ssl_hs_ok; | 
|---|
| 589 | } | 
|---|
| 590 |  | 
|---|
| 591 | static enum ssl_hs_wait_t do_select_certificate(SSL_HANDSHAKE *hs) { | 
|---|
| 592 | SSL *const ssl = hs->ssl; | 
|---|
| 593 |  | 
|---|
| 594 | SSLMessage msg; | 
|---|
| 595 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 596 | return ssl_hs_read_message; | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | // Call |cert_cb| to update server certificates if required. | 
|---|
| 600 | if (hs->config->cert->cert_cb != NULL) { | 
|---|
| 601 | int rv = hs->config->cert->cert_cb(ssl, hs->config->cert->cert_cb_arg); | 
|---|
| 602 | if (rv == 0) { | 
|---|
| 603 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); | 
|---|
| 604 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 605 | return ssl_hs_error; | 
|---|
| 606 | } | 
|---|
| 607 | if (rv < 0) { | 
|---|
| 608 | return ssl_hs_x509_lookup; | 
|---|
| 609 | } | 
|---|
| 610 | } | 
|---|
| 611 |  | 
|---|
| 612 | if (!ssl_on_certificate_selected(hs)) { | 
|---|
| 613 | return ssl_hs_error; | 
|---|
| 614 | } | 
|---|
| 615 |  | 
|---|
| 616 | if (hs->ocsp_stapling_requested && | 
|---|
| 617 | ssl->ctx->legacy_ocsp_callback != nullptr) { | 
|---|
| 618 | switch (ssl->ctx->legacy_ocsp_callback( | 
|---|
| 619 | ssl, ssl->ctx->legacy_ocsp_callback_arg)) { | 
|---|
| 620 | case SSL_TLSEXT_ERR_OK: | 
|---|
| 621 | break; | 
|---|
| 622 | case SSL_TLSEXT_ERR_NOACK: | 
|---|
| 623 | hs->ocsp_stapling_requested = false; | 
|---|
| 624 | break; | 
|---|
| 625 | default: | 
|---|
| 626 | OPENSSL_PUT_ERROR(SSL, SSL_R_OCSP_CB_ERROR); | 
|---|
| 627 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 628 | return ssl_hs_error; | 
|---|
| 629 | } | 
|---|
| 630 | } | 
|---|
| 631 |  | 
|---|
| 632 | if (ssl_protocol_version(ssl) >= TLS1_3_VERSION) { | 
|---|
| 633 | // Jump to the TLS 1.3 state machine. | 
|---|
| 634 | hs->state = state12_tls13; | 
|---|
| 635 | return ssl_hs_ok; | 
|---|
| 636 | } | 
|---|
| 637 |  | 
|---|
| 638 | ssl->s3->early_data_reason = ssl_early_data_protocol_version; | 
|---|
| 639 |  | 
|---|
| 640 | SSL_CLIENT_HELLO client_hello; | 
|---|
| 641 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { | 
|---|
| 642 | return ssl_hs_error; | 
|---|
| 643 | } | 
|---|
| 644 |  | 
|---|
| 645 | // Negotiate the cipher suite. This must be done after |cert_cb| so the | 
|---|
| 646 | // certificate is finalized. | 
|---|
| 647 | SSLCipherPreferenceList *prefs = hs->config->cipher_list | 
|---|
| 648 | ? hs->config->cipher_list.get() | 
|---|
| 649 | : ssl->ctx->cipher_list.get(); | 
|---|
| 650 | hs->new_cipher = ssl3_choose_cipher(hs, &client_hello, prefs); | 
|---|
| 651 | if (hs->new_cipher == NULL) { | 
|---|
| 652 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); | 
|---|
| 653 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 654 | return ssl_hs_error; | 
|---|
| 655 | } | 
|---|
| 656 |  | 
|---|
| 657 | hs->state = state12_select_parameters; | 
|---|
| 658 | return ssl_hs_ok; | 
|---|
| 659 | } | 
|---|
| 660 |  | 
|---|
| 661 | static enum ssl_hs_wait_t do_tls13(SSL_HANDSHAKE *hs) { | 
|---|
| 662 | enum ssl_hs_wait_t wait = tls13_server_handshake(hs); | 
|---|
| 663 | if (wait == ssl_hs_ok) { | 
|---|
| 664 | hs->state = state12_finish_server_handshake; | 
|---|
| 665 | return ssl_hs_ok; | 
|---|
| 666 | } | 
|---|
| 667 |  | 
|---|
| 668 | return wait; | 
|---|
| 669 | } | 
|---|
| 670 |  | 
|---|
| 671 | static enum ssl_hs_wait_t do_select_parameters(SSL_HANDSHAKE *hs) { | 
|---|
| 672 | SSL *const ssl = hs->ssl; | 
|---|
| 673 |  | 
|---|
| 674 | SSLMessage msg; | 
|---|
| 675 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 676 | return ssl_hs_read_message; | 
|---|
| 677 | } | 
|---|
| 678 |  | 
|---|
| 679 | SSL_CLIENT_HELLO client_hello; | 
|---|
| 680 | if (!ssl_client_hello_init(ssl, &client_hello, msg)) { | 
|---|
| 681 | return ssl_hs_error; | 
|---|
| 682 | } | 
|---|
| 683 |  | 
|---|
| 684 | // Determine whether we are doing session resumption. | 
|---|
| 685 | UniquePtr<SSL_SESSION> session; | 
|---|
| 686 | bool tickets_supported = false, renew_ticket = false; | 
|---|
| 687 | enum ssl_hs_wait_t wait = ssl_get_prev_session( | 
|---|
| 688 | hs, &session, &tickets_supported, &renew_ticket, &client_hello); | 
|---|
| 689 | if (wait != ssl_hs_ok) { | 
|---|
| 690 | return wait; | 
|---|
| 691 | } | 
|---|
| 692 |  | 
|---|
| 693 | if (session) { | 
|---|
| 694 | if (session->extended_master_secret && !hs->extended_master_secret) { | 
|---|
| 695 | // A ClientHello without EMS that attempts to resume a session with EMS | 
|---|
| 696 | // is fatal to the connection. | 
|---|
| 697 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); | 
|---|
| 698 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 699 | return ssl_hs_error; | 
|---|
| 700 | } | 
|---|
| 701 |  | 
|---|
| 702 | if (!ssl_session_is_resumable(hs, session.get()) || | 
|---|
| 703 | // If the client offers the EMS extension, but the previous session | 
|---|
| 704 | // didn't use it, then negotiate a new session. | 
|---|
| 705 | hs->extended_master_secret != session->extended_master_secret) { | 
|---|
| 706 | session.reset(); | 
|---|
| 707 | } | 
|---|
| 708 | } | 
|---|
| 709 |  | 
|---|
| 710 | if (session) { | 
|---|
| 711 | // Use the old session. | 
|---|
| 712 | hs->ticket_expected = renew_ticket; | 
|---|
| 713 | ssl->session = std::move(session); | 
|---|
| 714 | ssl->s3->session_reused = true; | 
|---|
| 715 | } else { | 
|---|
| 716 | hs->ticket_expected = tickets_supported; | 
|---|
| 717 | ssl_set_session(ssl, NULL); | 
|---|
| 718 | if (!ssl_get_new_session(hs, 1 /* server */)) { | 
|---|
| 719 | return ssl_hs_error; | 
|---|
| 720 | } | 
|---|
| 721 |  | 
|---|
| 722 | // Clear the session ID if we want the session to be single-use. | 
|---|
| 723 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { | 
|---|
| 724 | hs->new_session->session_id_length = 0; | 
|---|
| 725 | } | 
|---|
| 726 | } | 
|---|
| 727 |  | 
|---|
| 728 | if (ssl->ctx->dos_protection_cb != NULL && | 
|---|
| 729 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { | 
|---|
| 730 | // Connection rejected for DOS reasons. | 
|---|
| 731 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); | 
|---|
| 732 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 733 | return ssl_hs_error; | 
|---|
| 734 | } | 
|---|
| 735 |  | 
|---|
| 736 | if (ssl->session == NULL) { | 
|---|
| 737 | hs->new_session->cipher = hs->new_cipher; | 
|---|
| 738 |  | 
|---|
| 739 | // Determine whether to request a client certificate. | 
|---|
| 740 | hs->cert_request = !!(hs->config->verify_mode & SSL_VERIFY_PEER); | 
|---|
| 741 | // Only request a certificate if Channel ID isn't negotiated. | 
|---|
| 742 | if ((hs->config->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && | 
|---|
| 743 | ssl->s3->channel_id_valid) { | 
|---|
| 744 | hs->cert_request = false; | 
|---|
| 745 | } | 
|---|
| 746 | // CertificateRequest may only be sent in certificate-based ciphers. | 
|---|
| 747 | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { | 
|---|
| 748 | hs->cert_request = false; | 
|---|
| 749 | } | 
|---|
| 750 |  | 
|---|
| 751 | if (!hs->cert_request) { | 
|---|
| 752 | // OpenSSL returns X509_V_OK when no certificates are requested. This is | 
|---|
| 753 | // classed by them as a bug, but it's assumed by at least NGINX. | 
|---|
| 754 | hs->new_session->verify_result = X509_V_OK; | 
|---|
| 755 | } | 
|---|
| 756 | } | 
|---|
| 757 |  | 
|---|
| 758 | // HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was | 
|---|
| 759 | // deferred. Complete it now. | 
|---|
| 760 | uint8_t alert = SSL_AD_DECODE_ERROR; | 
|---|
| 761 | if (!ssl_negotiate_alpn(hs, &alert, &client_hello)) { | 
|---|
| 762 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); | 
|---|
| 763 | return ssl_hs_error; | 
|---|
| 764 | } | 
|---|
| 765 |  | 
|---|
| 766 | // Now that all parameters are known, initialize the handshake hash and hash | 
|---|
| 767 | // the ClientHello. | 
|---|
| 768 | if (!hs->transcript.InitHash(ssl_protocol_version(ssl), hs->new_cipher) || | 
|---|
| 769 | !ssl_hash_message(hs, msg)) { | 
|---|
| 770 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 771 | return ssl_hs_error; | 
|---|
| 772 | } | 
|---|
| 773 |  | 
|---|
| 774 | // Handback includes the whole handshake transcript, so we cannot free the | 
|---|
| 775 | // transcript buffer in the handback case. | 
|---|
| 776 | if (!hs->cert_request && !hs->handback) { | 
|---|
| 777 | hs->transcript.FreeBuffer(); | 
|---|
| 778 | } | 
|---|
| 779 |  | 
|---|
| 780 | ssl->method->next_message(ssl); | 
|---|
| 781 |  | 
|---|
| 782 | hs->state = state12_send_server_hello; | 
|---|
| 783 | return ssl_hs_ok; | 
|---|
| 784 | } | 
|---|
| 785 |  | 
|---|
| 786 | static void copy_suffix(Span<uint8_t> out, Span<const uint8_t> in) { | 
|---|
| 787 | out = out.subspan(out.size() - in.size()); | 
|---|
| 788 | assert(out.size() == in.size()); | 
|---|
| 789 | OPENSSL_memcpy(out.data(), in.data(), in.size()); | 
|---|
| 790 | } | 
|---|
| 791 |  | 
|---|
| 792 | static enum ssl_hs_wait_t do_send_server_hello(SSL_HANDSHAKE *hs) { | 
|---|
| 793 | SSL *const ssl = hs->ssl; | 
|---|
| 794 |  | 
|---|
| 795 | // We only accept ChannelIDs on connections with ECDHE in order to avoid a | 
|---|
| 796 | // known attack while we fix ChannelID itself. | 
|---|
| 797 | if (ssl->s3->channel_id_valid && | 
|---|
| 798 | (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { | 
|---|
| 799 | ssl->s3->channel_id_valid = false; | 
|---|
| 800 | } | 
|---|
| 801 |  | 
|---|
| 802 | // If this is a resumption and the original handshake didn't support | 
|---|
| 803 | // ChannelID then we didn't record the original handshake hashes in the | 
|---|
| 804 | // session and so cannot resume with ChannelIDs. | 
|---|
| 805 | if (ssl->session != NULL && | 
|---|
| 806 | ssl->session->original_handshake_hash_len == 0) { | 
|---|
| 807 | ssl->s3->channel_id_valid = false; | 
|---|
| 808 | } | 
|---|
| 809 |  | 
|---|
| 810 | struct OPENSSL_timeval now; | 
|---|
| 811 | ssl_get_current_time(ssl, &now); | 
|---|
| 812 | ssl->s3->server_random[0] = now.tv_sec >> 24; | 
|---|
| 813 | ssl->s3->server_random[1] = now.tv_sec >> 16; | 
|---|
| 814 | ssl->s3->server_random[2] = now.tv_sec >> 8; | 
|---|
| 815 | ssl->s3->server_random[3] = now.tv_sec; | 
|---|
| 816 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { | 
|---|
| 817 | return ssl_hs_error; | 
|---|
| 818 | } | 
|---|
| 819 |  | 
|---|
| 820 | // Implement the TLS 1.3 anti-downgrade feature. | 
|---|
| 821 | if (ssl_supports_version(hs, TLS1_3_VERSION)) { | 
|---|
| 822 | if (ssl_protocol_version(ssl) == TLS1_2_VERSION) { | 
|---|
| 823 | if (hs->apply_jdk11_workaround) { | 
|---|
| 824 | // JDK 11 implements the TLS 1.3 downgrade signal, so we cannot send it | 
|---|
| 825 | // here. However, the signal is only effective if all TLS 1.2 | 
|---|
| 826 | // ServerHellos produced by the server are marked. Thus we send a | 
|---|
| 827 | // different non-standard signal for the time being, until JDK 11.0.2 is | 
|---|
| 828 | // released and clients have updated. | 
|---|
| 829 | copy_suffix(ssl->s3->server_random, kJDK11DowngradeRandom); | 
|---|
| 830 | } else { | 
|---|
| 831 | copy_suffix(ssl->s3->server_random, kTLS13DowngradeRandom); | 
|---|
| 832 | } | 
|---|
| 833 | } else { | 
|---|
| 834 | copy_suffix(ssl->s3->server_random, kTLS12DowngradeRandom); | 
|---|
| 835 | } | 
|---|
| 836 | } | 
|---|
| 837 |  | 
|---|
| 838 | const SSL_SESSION *session = hs->new_session.get(); | 
|---|
| 839 | if (ssl->session != nullptr) { | 
|---|
| 840 | session = ssl->session.get(); | 
|---|
| 841 | } | 
|---|
| 842 |  | 
|---|
| 843 | ScopedCBB cbb; | 
|---|
| 844 | CBB body, session_id; | 
|---|
| 845 | if (!ssl->method->init_message(ssl, cbb.get(), &body, SSL3_MT_SERVER_HELLO) || | 
|---|
| 846 | !CBB_add_u16(&body, ssl->version) || | 
|---|
| 847 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || | 
|---|
| 848 | !CBB_add_u8_length_prefixed(&body, &session_id) || | 
|---|
| 849 | !CBB_add_bytes(&session_id, session->session_id, | 
|---|
| 850 | session->session_id_length) || | 
|---|
| 851 | !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) || | 
|---|
| 852 | !CBB_add_u8(&body, 0 /* no compression */) || | 
|---|
| 853 | !ssl_add_serverhello_tlsext(hs, &body) || | 
|---|
| 854 | !ssl_add_message_cbb(ssl, cbb.get())) { | 
|---|
| 855 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 856 | return ssl_hs_error; | 
|---|
| 857 | } | 
|---|
| 858 |  | 
|---|
| 859 | if (ssl->session != NULL) { | 
|---|
| 860 | hs->state = state12_send_server_finished; | 
|---|
| 861 | } else { | 
|---|
| 862 | hs->state = state12_send_server_certificate; | 
|---|
| 863 | } | 
|---|
| 864 | return ssl_hs_ok; | 
|---|
| 865 | } | 
|---|
| 866 |  | 
|---|
| 867 | static enum ssl_hs_wait_t do_send_server_certificate(SSL_HANDSHAKE *hs) { | 
|---|
| 868 | SSL *const ssl = hs->ssl; | 
|---|
| 869 | ScopedCBB cbb; | 
|---|
| 870 |  | 
|---|
| 871 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { | 
|---|
| 872 | if (!ssl_has_certificate(hs)) { | 
|---|
| 873 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); | 
|---|
| 874 | return ssl_hs_error; | 
|---|
| 875 | } | 
|---|
| 876 |  | 
|---|
| 877 | if (!ssl_output_cert_chain(hs)) { | 
|---|
| 878 | return ssl_hs_error; | 
|---|
| 879 | } | 
|---|
| 880 |  | 
|---|
| 881 | if (hs->certificate_status_expected) { | 
|---|
| 882 | CBB body, ocsp_response; | 
|---|
| 883 | if (!ssl->method->init_message(ssl, cbb.get(), &body, | 
|---|
| 884 | SSL3_MT_CERTIFICATE_STATUS) || | 
|---|
| 885 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || | 
|---|
| 886 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || | 
|---|
| 887 | !CBB_add_bytes( | 
|---|
| 888 | &ocsp_response, | 
|---|
| 889 | CRYPTO_BUFFER_data(hs->config->cert->ocsp_response.get()), | 
|---|
| 890 | CRYPTO_BUFFER_len(hs->config->cert->ocsp_response.get())) || | 
|---|
| 891 | !ssl_add_message_cbb(ssl, cbb.get())) { | 
|---|
| 892 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 893 | return ssl_hs_error; | 
|---|
| 894 | } | 
|---|
| 895 | } | 
|---|
| 896 | } | 
|---|
| 897 |  | 
|---|
| 898 | // Assemble ServerKeyExchange parameters if needed. | 
|---|
| 899 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; | 
|---|
| 900 | uint32_t alg_a = hs->new_cipher->algorithm_auth; | 
|---|
| 901 | if (ssl_cipher_requires_server_key_exchange(hs->new_cipher) || | 
|---|
| 902 | ((alg_a & SSL_aPSK) && hs->config->psk_identity_hint)) { | 
|---|
| 903 | // Pre-allocate enough room to comfortably fit an ECDHE public key. Prepend | 
|---|
| 904 | // the client and server randoms for the signing transcript. | 
|---|
| 905 | CBB child; | 
|---|
| 906 | if (!CBB_init(cbb.get(), SSL3_RANDOM_SIZE * 2 + 128) || | 
|---|
| 907 | !CBB_add_bytes(cbb.get(), ssl->s3->client_random, SSL3_RANDOM_SIZE) || | 
|---|
| 908 | !CBB_add_bytes(cbb.get(), ssl->s3->server_random, SSL3_RANDOM_SIZE)) { | 
|---|
| 909 | return ssl_hs_error; | 
|---|
| 910 | } | 
|---|
| 911 |  | 
|---|
| 912 | // PSK ciphers begin with an identity hint. | 
|---|
| 913 | if (alg_a & SSL_aPSK) { | 
|---|
| 914 | size_t len = hs->config->psk_identity_hint == nullptr | 
|---|
| 915 | ? 0 | 
|---|
| 916 | : strlen(hs->config->psk_identity_hint.get()); | 
|---|
| 917 | if (!CBB_add_u16_length_prefixed(cbb.get(), &child) || | 
|---|
| 918 | !CBB_add_bytes(&child, | 
|---|
| 919 | (const uint8_t *)hs->config->psk_identity_hint.get(), | 
|---|
| 920 | len)) { | 
|---|
| 921 | return ssl_hs_error; | 
|---|
| 922 | } | 
|---|
| 923 | } | 
|---|
| 924 |  | 
|---|
| 925 | if (alg_k & SSL_kECDHE) { | 
|---|
| 926 | // Determine the group to use. | 
|---|
| 927 | uint16_t group_id; | 
|---|
| 928 | if (!tls1_get_shared_group(hs, &group_id)) { | 
|---|
| 929 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 930 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 931 | return ssl_hs_error; | 
|---|
| 932 | } | 
|---|
| 933 | hs->new_session->group_id = group_id; | 
|---|
| 934 |  | 
|---|
| 935 | // Set up ECDH, generate a key, and emit the public half. | 
|---|
| 936 | hs->key_shares[0] = SSLKeyShare::Create(group_id); | 
|---|
| 937 | if (!hs->key_shares[0] || | 
|---|
| 938 | !CBB_add_u8(cbb.get(), NAMED_CURVE_TYPE) || | 
|---|
| 939 | !CBB_add_u16(cbb.get(), group_id) || | 
|---|
| 940 | !CBB_add_u8_length_prefixed(cbb.get(), &child) || | 
|---|
| 941 | !hs->key_shares[0]->Offer(&child)) { | 
|---|
| 942 | return ssl_hs_error; | 
|---|
| 943 | } | 
|---|
| 944 | } else { | 
|---|
| 945 | assert(alg_k & SSL_kPSK); | 
|---|
| 946 | } | 
|---|
| 947 |  | 
|---|
| 948 | if (!CBBFinishArray(cbb.get(), &hs->server_params)) { | 
|---|
| 949 | return ssl_hs_error; | 
|---|
| 950 | } | 
|---|
| 951 | } | 
|---|
| 952 |  | 
|---|
| 953 | hs->state = state12_send_server_key_exchange; | 
|---|
| 954 | return ssl_hs_ok; | 
|---|
| 955 | } | 
|---|
| 956 |  | 
|---|
| 957 | static enum ssl_hs_wait_t do_send_server_key_exchange(SSL_HANDSHAKE *hs) { | 
|---|
| 958 | SSL *const ssl = hs->ssl; | 
|---|
| 959 |  | 
|---|
| 960 | if (hs->server_params.size() == 0) { | 
|---|
| 961 | hs->state = state12_send_server_hello_done; | 
|---|
| 962 | return ssl_hs_ok; | 
|---|
| 963 | } | 
|---|
| 964 |  | 
|---|
| 965 | ScopedCBB cbb; | 
|---|
| 966 | CBB body, child; | 
|---|
| 967 | if (!ssl->method->init_message(ssl, cbb.get(), &body, | 
|---|
| 968 | SSL3_MT_SERVER_KEY_EXCHANGE) || | 
|---|
| 969 | // |hs->server_params| contains a prefix for signing. | 
|---|
| 970 | hs->server_params.size() < 2 * SSL3_RANDOM_SIZE || | 
|---|
| 971 | !CBB_add_bytes(&body, hs->server_params.data() + 2 * SSL3_RANDOM_SIZE, | 
|---|
| 972 | hs->server_params.size() - 2 * SSL3_RANDOM_SIZE)) { | 
|---|
| 973 | return ssl_hs_error; | 
|---|
| 974 | } | 
|---|
| 975 |  | 
|---|
| 976 | // Add a signature. | 
|---|
| 977 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { | 
|---|
| 978 | if (!ssl_has_private_key(hs)) { | 
|---|
| 979 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 980 | return ssl_hs_error; | 
|---|
| 981 | } | 
|---|
| 982 |  | 
|---|
| 983 | // Determine the signature algorithm. | 
|---|
| 984 | uint16_t signature_algorithm; | 
|---|
| 985 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { | 
|---|
| 986 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 987 | return ssl_hs_error; | 
|---|
| 988 | } | 
|---|
| 989 | if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { | 
|---|
| 990 | if (!CBB_add_u16(&body, signature_algorithm)) { | 
|---|
| 991 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 992 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 993 | return ssl_hs_error; | 
|---|
| 994 | } | 
|---|
| 995 | } | 
|---|
| 996 |  | 
|---|
| 997 | // Add space for the signature. | 
|---|
| 998 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey.get()); | 
|---|
| 999 | uint8_t *ptr; | 
|---|
| 1000 | if (!CBB_add_u16_length_prefixed(&body, &child) || | 
|---|
| 1001 | !CBB_reserve(&child, &ptr, max_sig_len)) { | 
|---|
| 1002 | return ssl_hs_error; | 
|---|
| 1003 | } | 
|---|
| 1004 |  | 
|---|
| 1005 | size_t sig_len; | 
|---|
| 1006 | switch (ssl_private_key_sign(hs, ptr, &sig_len, max_sig_len, | 
|---|
| 1007 | signature_algorithm, hs->server_params)) { | 
|---|
| 1008 | case ssl_private_key_success: | 
|---|
| 1009 | if (!CBB_did_write(&child, sig_len)) { | 
|---|
| 1010 | return ssl_hs_error; | 
|---|
| 1011 | } | 
|---|
| 1012 | break; | 
|---|
| 1013 | case ssl_private_key_failure: | 
|---|
| 1014 | return ssl_hs_error; | 
|---|
| 1015 | case ssl_private_key_retry: | 
|---|
| 1016 | return ssl_hs_private_key_operation; | 
|---|
| 1017 | } | 
|---|
| 1018 | } | 
|---|
| 1019 |  | 
|---|
| 1020 | if (!ssl_add_message_cbb(ssl, cbb.get())) { | 
|---|
| 1021 | return ssl_hs_error; | 
|---|
| 1022 | } | 
|---|
| 1023 |  | 
|---|
| 1024 | hs->server_params.Reset(); | 
|---|
| 1025 |  | 
|---|
| 1026 | hs->state = state12_send_server_hello_done; | 
|---|
| 1027 | return ssl_hs_ok; | 
|---|
| 1028 | } | 
|---|
| 1029 |  | 
|---|
| 1030 | static enum ssl_hs_wait_t do_send_server_hello_done(SSL_HANDSHAKE *hs) { | 
|---|
| 1031 | SSL *const ssl = hs->ssl; | 
|---|
| 1032 |  | 
|---|
| 1033 | ScopedCBB cbb; | 
|---|
| 1034 | CBB body; | 
|---|
| 1035 |  | 
|---|
| 1036 | if (hs->cert_request) { | 
|---|
| 1037 | CBB cert_types, sigalgs_cbb; | 
|---|
| 1038 | if (!ssl->method->init_message(ssl, cbb.get(), &body, | 
|---|
| 1039 | SSL3_MT_CERTIFICATE_REQUEST) || | 
|---|
| 1040 | !CBB_add_u8_length_prefixed(&body, &cert_types) || | 
|---|
| 1041 | !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) || | 
|---|
| 1042 | !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN) || | 
|---|
| 1043 | // TLS 1.2 has no way to specify different signature algorithms for | 
|---|
| 1044 | // certificates and the online signature, so emit the more restrictive | 
|---|
| 1045 | // certificate list. | 
|---|
| 1046 | (ssl_protocol_version(ssl) >= TLS1_2_VERSION && | 
|---|
| 1047 | (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) || | 
|---|
| 1048 | !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb, true /* certs */))) || | 
|---|
| 1049 | !ssl_add_client_CA_list(hs, &body) || | 
|---|
| 1050 | !ssl_add_message_cbb(ssl, cbb.get())) { | 
|---|
| 1051 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 1052 | return ssl_hs_error; | 
|---|
| 1053 | } | 
|---|
| 1054 | } | 
|---|
| 1055 |  | 
|---|
| 1056 | if (!ssl->method->init_message(ssl, cbb.get(), &body, | 
|---|
| 1057 | SSL3_MT_SERVER_HELLO_DONE) || | 
|---|
| 1058 | !ssl_add_message_cbb(ssl, cbb.get())) { | 
|---|
| 1059 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 1060 | return ssl_hs_error; | 
|---|
| 1061 | } | 
|---|
| 1062 |  | 
|---|
| 1063 | hs->state = state12_read_client_certificate; | 
|---|
| 1064 | return ssl_hs_flush; | 
|---|
| 1065 | } | 
|---|
| 1066 |  | 
|---|
| 1067 | static enum ssl_hs_wait_t do_read_client_certificate(SSL_HANDSHAKE *hs) { | 
|---|
| 1068 | SSL *const ssl = hs->ssl; | 
|---|
| 1069 |  | 
|---|
| 1070 | if (hs->handback && hs->new_cipher->algorithm_mkey == SSL_kECDHE) { | 
|---|
| 1071 | return ssl_hs_handback; | 
|---|
| 1072 | } | 
|---|
| 1073 | if (!hs->cert_request) { | 
|---|
| 1074 | hs->state = state12_verify_client_certificate; | 
|---|
| 1075 | return ssl_hs_ok; | 
|---|
| 1076 | } | 
|---|
| 1077 |  | 
|---|
| 1078 | SSLMessage msg; | 
|---|
| 1079 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 1080 | return ssl_hs_read_message; | 
|---|
| 1081 | } | 
|---|
| 1082 |  | 
|---|
| 1083 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE)) { | 
|---|
| 1084 | return ssl_hs_error; | 
|---|
| 1085 | } | 
|---|
| 1086 |  | 
|---|
| 1087 | if (!ssl_hash_message(hs, msg)) { | 
|---|
| 1088 | return ssl_hs_error; | 
|---|
| 1089 | } | 
|---|
| 1090 |  | 
|---|
| 1091 | CBS certificate_msg = msg.body; | 
|---|
| 1092 | uint8_t alert = SSL_AD_DECODE_ERROR; | 
|---|
| 1093 | if (!ssl_parse_cert_chain(&alert, &hs->new_session->certs, &hs->peer_pubkey, | 
|---|
| 1094 | hs->config->retain_only_sha256_of_client_certs | 
|---|
| 1095 | ? hs->new_session->peer_sha256 | 
|---|
| 1096 | : nullptr, | 
|---|
| 1097 | &certificate_msg, ssl->ctx->pool)) { | 
|---|
| 1098 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); | 
|---|
| 1099 | return ssl_hs_error; | 
|---|
| 1100 | } | 
|---|
| 1101 |  | 
|---|
| 1102 | if (CBS_len(&certificate_msg) != 0 || | 
|---|
| 1103 | !ssl->ctx->x509_method->session_cache_objects(hs->new_session.get())) { | 
|---|
| 1104 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1105 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1106 | return ssl_hs_error; | 
|---|
| 1107 | } | 
|---|
| 1108 |  | 
|---|
| 1109 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) == 0) { | 
|---|
| 1110 | // No client certificate so the handshake buffer may be discarded. | 
|---|
| 1111 | hs->transcript.FreeBuffer(); | 
|---|
| 1112 |  | 
|---|
| 1113 | if (hs->config->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { | 
|---|
| 1114 | // Fail for TLS only if we required a certificate | 
|---|
| 1115 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); | 
|---|
| 1116 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 1117 | return ssl_hs_error; | 
|---|
| 1118 | } | 
|---|
| 1119 |  | 
|---|
| 1120 | // OpenSSL returns X509_V_OK when no certificates are received. This is | 
|---|
| 1121 | // classed by them as a bug, but it's assumed by at least NGINX. | 
|---|
| 1122 | hs->new_session->verify_result = X509_V_OK; | 
|---|
| 1123 | } else if (hs->config->retain_only_sha256_of_client_certs) { | 
|---|
| 1124 | // The hash will have been filled in. | 
|---|
| 1125 | hs->new_session->peer_sha256_valid = 1; | 
|---|
| 1126 | } | 
|---|
| 1127 |  | 
|---|
| 1128 | ssl->method->next_message(ssl); | 
|---|
| 1129 | hs->state = state12_verify_client_certificate; | 
|---|
| 1130 | return ssl_hs_ok; | 
|---|
| 1131 | } | 
|---|
| 1132 |  | 
|---|
| 1133 | static enum ssl_hs_wait_t do_verify_client_certificate(SSL_HANDSHAKE *hs) { | 
|---|
| 1134 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs.get()) > 0) { | 
|---|
| 1135 | switch (ssl_verify_peer_cert(hs)) { | 
|---|
| 1136 | case ssl_verify_ok: | 
|---|
| 1137 | break; | 
|---|
| 1138 | case ssl_verify_invalid: | 
|---|
| 1139 | return ssl_hs_error; | 
|---|
| 1140 | case ssl_verify_retry: | 
|---|
| 1141 | return ssl_hs_certificate_verify; | 
|---|
| 1142 | } | 
|---|
| 1143 | } | 
|---|
| 1144 |  | 
|---|
| 1145 | hs->state = state12_read_client_key_exchange; | 
|---|
| 1146 | return ssl_hs_ok; | 
|---|
| 1147 | } | 
|---|
| 1148 |  | 
|---|
| 1149 | static enum ssl_hs_wait_t do_read_client_key_exchange(SSL_HANDSHAKE *hs) { | 
|---|
| 1150 | SSL *const ssl = hs->ssl; | 
|---|
| 1151 | SSLMessage msg; | 
|---|
| 1152 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 1153 | return ssl_hs_read_message; | 
|---|
| 1154 | } | 
|---|
| 1155 |  | 
|---|
| 1156 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CLIENT_KEY_EXCHANGE)) { | 
|---|
| 1157 | return ssl_hs_error; | 
|---|
| 1158 | } | 
|---|
| 1159 |  | 
|---|
| 1160 | CBS client_key_exchange = msg.body; | 
|---|
| 1161 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; | 
|---|
| 1162 | uint32_t alg_a = hs->new_cipher->algorithm_auth; | 
|---|
| 1163 |  | 
|---|
| 1164 | // If using a PSK key exchange, parse the PSK identity. | 
|---|
| 1165 | if (alg_a & SSL_aPSK) { | 
|---|
| 1166 | CBS psk_identity; | 
|---|
| 1167 |  | 
|---|
| 1168 | // If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, | 
|---|
| 1169 | // then this is the only field in the message. | 
|---|
| 1170 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || | 
|---|
| 1171 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { | 
|---|
| 1172 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1173 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1174 | return ssl_hs_error; | 
|---|
| 1175 | } | 
|---|
| 1176 |  | 
|---|
| 1177 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || | 
|---|
| 1178 | CBS_contains_zero_byte(&psk_identity)) { | 
|---|
| 1179 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); | 
|---|
| 1180 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); | 
|---|
| 1181 | return ssl_hs_error; | 
|---|
| 1182 | } | 
|---|
| 1183 | char *raw = nullptr; | 
|---|
| 1184 | if (!CBS_strdup(&psk_identity, &raw)) { | 
|---|
| 1185 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); | 
|---|
| 1186 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 1187 | return ssl_hs_error; | 
|---|
| 1188 | } | 
|---|
| 1189 | hs->new_session->psk_identity.reset(raw); | 
|---|
| 1190 | } | 
|---|
| 1191 |  | 
|---|
| 1192 | // Depending on the key exchange method, compute |premaster_secret|. | 
|---|
| 1193 | Array<uint8_t> premaster_secret; | 
|---|
| 1194 | if (alg_k & SSL_kRSA) { | 
|---|
| 1195 | CBS encrypted_premaster_secret; | 
|---|
| 1196 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, | 
|---|
| 1197 | &encrypted_premaster_secret) || | 
|---|
| 1198 | CBS_len(&client_key_exchange) != 0) { | 
|---|
| 1199 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1200 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1201 | return ssl_hs_error; | 
|---|
| 1202 | } | 
|---|
| 1203 |  | 
|---|
| 1204 | // Allocate a buffer large enough for an RSA decryption. | 
|---|
| 1205 | Array<uint8_t> decrypt_buf; | 
|---|
| 1206 | if (!decrypt_buf.Init(EVP_PKEY_size(hs->local_pubkey.get()))) { | 
|---|
| 1207 | return ssl_hs_error; | 
|---|
| 1208 | } | 
|---|
| 1209 |  | 
|---|
| 1210 | // Decrypt with no padding. PKCS#1 padding will be removed as part of the | 
|---|
| 1211 | // timing-sensitive code below. | 
|---|
| 1212 | size_t decrypt_len; | 
|---|
| 1213 | switch (ssl_private_key_decrypt(hs, decrypt_buf.data(), &decrypt_len, | 
|---|
| 1214 | decrypt_buf.size(), | 
|---|
| 1215 | encrypted_premaster_secret)) { | 
|---|
| 1216 | case ssl_private_key_success: | 
|---|
| 1217 | break; | 
|---|
| 1218 | case ssl_private_key_failure: | 
|---|
| 1219 | return ssl_hs_error; | 
|---|
| 1220 | case ssl_private_key_retry: | 
|---|
| 1221 | return ssl_hs_private_key_operation; | 
|---|
| 1222 | } | 
|---|
| 1223 |  | 
|---|
| 1224 | if (decrypt_len != decrypt_buf.size()) { | 
|---|
| 1225 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); | 
|---|
| 1226 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); | 
|---|
| 1227 | return ssl_hs_error; | 
|---|
| 1228 | } | 
|---|
| 1229 |  | 
|---|
| 1230 | CONSTTIME_SECRET(decrypt_buf.data(), decrypt_len); | 
|---|
| 1231 |  | 
|---|
| 1232 | // Prepare a random premaster, to be used on invalid padding. See RFC 5246, | 
|---|
| 1233 | // section 7.4.7.1. | 
|---|
| 1234 | if (!premaster_secret.Init(SSL_MAX_MASTER_KEY_LENGTH) || | 
|---|
| 1235 | !RAND_bytes(premaster_secret.data(), premaster_secret.size())) { | 
|---|
| 1236 | return ssl_hs_error; | 
|---|
| 1237 | } | 
|---|
| 1238 |  | 
|---|
| 1239 | // The smallest padded premaster is 11 bytes of overhead. Small keys are | 
|---|
| 1240 | // publicly invalid. | 
|---|
| 1241 | if (decrypt_len < 11 + premaster_secret.size()) { | 
|---|
| 1242 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); | 
|---|
| 1243 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); | 
|---|
| 1244 | return ssl_hs_error; | 
|---|
| 1245 | } | 
|---|
| 1246 |  | 
|---|
| 1247 | // Check the padding. See RFC 3447, section 7.2.2. | 
|---|
| 1248 | size_t padding_len = decrypt_len - premaster_secret.size(); | 
|---|
| 1249 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & | 
|---|
| 1250 | constant_time_eq_int_8(decrypt_buf[1], 2); | 
|---|
| 1251 | for (size_t i = 2; i < padding_len - 1; i++) { | 
|---|
| 1252 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); | 
|---|
| 1253 | } | 
|---|
| 1254 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); | 
|---|
| 1255 |  | 
|---|
| 1256 | // The premaster secret must begin with |client_version|. This too must be | 
|---|
| 1257 | // checked in constant time (http://eprint.iacr.org/2003/052/). | 
|---|
| 1258 | good &= constant_time_eq_8(decrypt_buf[padding_len], | 
|---|
| 1259 | (unsigned)(hs->client_version >> 8)); | 
|---|
| 1260 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], | 
|---|
| 1261 | (unsigned)(hs->client_version & 0xff)); | 
|---|
| 1262 |  | 
|---|
| 1263 | // Select, in constant time, either the decrypted premaster or the random | 
|---|
| 1264 | // premaster based on |good|. | 
|---|
| 1265 | for (size_t i = 0; i < premaster_secret.size(); i++) { | 
|---|
| 1266 | premaster_secret[i] = constant_time_select_8( | 
|---|
| 1267 | good, decrypt_buf[padding_len + i], premaster_secret[i]); | 
|---|
| 1268 | } | 
|---|
| 1269 | } else if (alg_k & SSL_kECDHE) { | 
|---|
| 1270 | // Parse the ClientKeyExchange. | 
|---|
| 1271 | CBS peer_key; | 
|---|
| 1272 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) || | 
|---|
| 1273 | CBS_len(&client_key_exchange) != 0) { | 
|---|
| 1274 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1275 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1276 | return ssl_hs_error; | 
|---|
| 1277 | } | 
|---|
| 1278 |  | 
|---|
| 1279 | // Compute the premaster. | 
|---|
| 1280 | uint8_t alert = SSL_AD_DECODE_ERROR; | 
|---|
| 1281 | if (!hs->key_shares[0]->Finish(&premaster_secret, &alert, peer_key)) { | 
|---|
| 1282 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); | 
|---|
| 1283 | return ssl_hs_error; | 
|---|
| 1284 | } | 
|---|
| 1285 |  | 
|---|
| 1286 | // The key exchange state may now be discarded. | 
|---|
| 1287 | hs->key_shares[0].reset(); | 
|---|
| 1288 | hs->key_shares[1].reset(); | 
|---|
| 1289 | } else if (!(alg_k & SSL_kPSK)) { | 
|---|
| 1290 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 1291 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); | 
|---|
| 1292 | return ssl_hs_error; | 
|---|
| 1293 | } | 
|---|
| 1294 |  | 
|---|
| 1295 | // For a PSK cipher suite, the actual pre-master secret is combined with the | 
|---|
| 1296 | // pre-shared key. | 
|---|
| 1297 | if (alg_a & SSL_aPSK) { | 
|---|
| 1298 | if (hs->config->psk_server_callback == NULL) { | 
|---|
| 1299 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 1300 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 1301 | return ssl_hs_error; | 
|---|
| 1302 | } | 
|---|
| 1303 |  | 
|---|
| 1304 | // Look up the key for the identity. | 
|---|
| 1305 | uint8_t psk[PSK_MAX_PSK_LEN]; | 
|---|
| 1306 | unsigned psk_len = hs->config->psk_server_callback( | 
|---|
| 1307 | ssl, hs->new_session->psk_identity.get(), psk, sizeof(psk)); | 
|---|
| 1308 | if (psk_len > PSK_MAX_PSK_LEN) { | 
|---|
| 1309 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); | 
|---|
| 1310 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); | 
|---|
| 1311 | return ssl_hs_error; | 
|---|
| 1312 | } else if (psk_len == 0) { | 
|---|
| 1313 | // PSK related to the given identity not found. | 
|---|
| 1314 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); | 
|---|
| 1315 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNKNOWN_PSK_IDENTITY); | 
|---|
| 1316 | return ssl_hs_error; | 
|---|
| 1317 | } | 
|---|
| 1318 |  | 
|---|
| 1319 | if (alg_k & SSL_kPSK) { | 
|---|
| 1320 | // In plain PSK, other_secret is a block of 0s with the same length as the | 
|---|
| 1321 | // pre-shared key. | 
|---|
| 1322 | if (!premaster_secret.Init(psk_len)) { | 
|---|
| 1323 | return ssl_hs_error; | 
|---|
| 1324 | } | 
|---|
| 1325 | OPENSSL_memset(premaster_secret.data(), 0, premaster_secret.size()); | 
|---|
| 1326 | } | 
|---|
| 1327 |  | 
|---|
| 1328 | ScopedCBB new_premaster; | 
|---|
| 1329 | CBB child; | 
|---|
| 1330 | if (!CBB_init(new_premaster.get(), | 
|---|
| 1331 | 2 + psk_len + 2 + premaster_secret.size()) || | 
|---|
| 1332 | !CBB_add_u16_length_prefixed(new_premaster.get(), &child) || | 
|---|
| 1333 | !CBB_add_bytes(&child, premaster_secret.data(), | 
|---|
| 1334 | premaster_secret.size()) || | 
|---|
| 1335 | !CBB_add_u16_length_prefixed(new_premaster.get(), &child) || | 
|---|
| 1336 | !CBB_add_bytes(&child, psk, psk_len) || | 
|---|
| 1337 | !CBBFinishArray(new_premaster.get(), &premaster_secret)) { | 
|---|
| 1338 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); | 
|---|
| 1339 | return ssl_hs_error; | 
|---|
| 1340 | } | 
|---|
| 1341 | } | 
|---|
| 1342 |  | 
|---|
| 1343 | if (!ssl_hash_message(hs, msg)) { | 
|---|
| 1344 | return ssl_hs_error; | 
|---|
| 1345 | } | 
|---|
| 1346 |  | 
|---|
| 1347 | // Compute the master secret. | 
|---|
| 1348 | hs->new_session->master_key_length = tls1_generate_master_secret( | 
|---|
| 1349 | hs, hs->new_session->master_key, premaster_secret); | 
|---|
| 1350 | if (hs->new_session->master_key_length == 0) { | 
|---|
| 1351 | return ssl_hs_error; | 
|---|
| 1352 | } | 
|---|
| 1353 | hs->new_session->extended_master_secret = hs->extended_master_secret; | 
|---|
| 1354 | CONSTTIME_DECLASSIFY(hs->new_session->master_key, | 
|---|
| 1355 | hs->new_session->master_key_length); | 
|---|
| 1356 |  | 
|---|
| 1357 | ssl->method->next_message(ssl); | 
|---|
| 1358 | hs->state = state12_read_client_certificate_verify; | 
|---|
| 1359 | return ssl_hs_ok; | 
|---|
| 1360 | } | 
|---|
| 1361 |  | 
|---|
| 1362 | static enum ssl_hs_wait_t do_read_client_certificate_verify(SSL_HANDSHAKE *hs) { | 
|---|
| 1363 | SSL *const ssl = hs->ssl; | 
|---|
| 1364 |  | 
|---|
| 1365 | // Only RSA and ECDSA client certificates are supported, so a | 
|---|
| 1366 | // CertificateVerify is required if and only if there's a client certificate. | 
|---|
| 1367 | if (!hs->peer_pubkey) { | 
|---|
| 1368 | hs->transcript.FreeBuffer(); | 
|---|
| 1369 | hs->state = state12_read_change_cipher_spec; | 
|---|
| 1370 | return ssl_hs_ok; | 
|---|
| 1371 | } | 
|---|
| 1372 |  | 
|---|
| 1373 | SSLMessage msg; | 
|---|
| 1374 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 1375 | return ssl_hs_read_message; | 
|---|
| 1376 | } | 
|---|
| 1377 |  | 
|---|
| 1378 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CERTIFICATE_VERIFY)) { | 
|---|
| 1379 | return ssl_hs_error; | 
|---|
| 1380 | } | 
|---|
| 1381 |  | 
|---|
| 1382 | CBS certificate_verify = msg.body, signature; | 
|---|
| 1383 |  | 
|---|
| 1384 | // Determine the signature algorithm. | 
|---|
| 1385 | uint16_t signature_algorithm = 0; | 
|---|
| 1386 | if (ssl_protocol_version(ssl) >= TLS1_2_VERSION) { | 
|---|
| 1387 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { | 
|---|
| 1388 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1389 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1390 | return ssl_hs_error; | 
|---|
| 1391 | } | 
|---|
| 1392 | uint8_t alert = SSL_AD_DECODE_ERROR; | 
|---|
| 1393 | if (!tls12_check_peer_sigalg(ssl, &alert, signature_algorithm)) { | 
|---|
| 1394 | ssl_send_alert(ssl, SSL3_AL_FATAL, alert); | 
|---|
| 1395 | return ssl_hs_error; | 
|---|
| 1396 | } | 
|---|
| 1397 | hs->new_session->peer_signature_algorithm = signature_algorithm; | 
|---|
| 1398 | } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, | 
|---|
| 1399 | hs->peer_pubkey.get())) { | 
|---|
| 1400 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); | 
|---|
| 1401 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNSUPPORTED_CERTIFICATE); | 
|---|
| 1402 | return ssl_hs_error; | 
|---|
| 1403 | } | 
|---|
| 1404 |  | 
|---|
| 1405 | // Parse and verify the signature. | 
|---|
| 1406 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || | 
|---|
| 1407 | CBS_len(&certificate_verify) != 0) { | 
|---|
| 1408 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1409 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1410 | return ssl_hs_error; | 
|---|
| 1411 | } | 
|---|
| 1412 |  | 
|---|
| 1413 | if (!ssl_public_key_verify(ssl, signature, signature_algorithm, | 
|---|
| 1414 | hs->peer_pubkey.get(), hs->transcript.buffer())) { | 
|---|
| 1415 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); | 
|---|
| 1416 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); | 
|---|
| 1417 | return ssl_hs_error; | 
|---|
| 1418 | } | 
|---|
| 1419 |  | 
|---|
| 1420 | // The handshake buffer is no longer necessary, and we may hash the current | 
|---|
| 1421 | // message. | 
|---|
| 1422 | hs->transcript.FreeBuffer(); | 
|---|
| 1423 | if (!ssl_hash_message(hs, msg)) { | 
|---|
| 1424 | return ssl_hs_error; | 
|---|
| 1425 | } | 
|---|
| 1426 |  | 
|---|
| 1427 | ssl->method->next_message(ssl); | 
|---|
| 1428 | hs->state = state12_read_change_cipher_spec; | 
|---|
| 1429 | return ssl_hs_ok; | 
|---|
| 1430 | } | 
|---|
| 1431 |  | 
|---|
| 1432 | static enum ssl_hs_wait_t do_read_change_cipher_spec(SSL_HANDSHAKE *hs) { | 
|---|
| 1433 | if (hs->handback && hs->ssl->session != NULL) { | 
|---|
| 1434 | return ssl_hs_handback; | 
|---|
| 1435 | } | 
|---|
| 1436 | hs->state = state12_process_change_cipher_spec; | 
|---|
| 1437 | return ssl_hs_read_change_cipher_spec; | 
|---|
| 1438 | } | 
|---|
| 1439 |  | 
|---|
| 1440 | static enum ssl_hs_wait_t do_process_change_cipher_spec(SSL_HANDSHAKE *hs) { | 
|---|
| 1441 | if (!tls1_change_cipher_state(hs, evp_aead_open)) { | 
|---|
| 1442 | return ssl_hs_error; | 
|---|
| 1443 | } | 
|---|
| 1444 |  | 
|---|
| 1445 | hs->state = state12_read_next_proto; | 
|---|
| 1446 | return ssl_hs_ok; | 
|---|
| 1447 | } | 
|---|
| 1448 |  | 
|---|
| 1449 | static enum ssl_hs_wait_t do_read_next_proto(SSL_HANDSHAKE *hs) { | 
|---|
| 1450 | SSL *const ssl = hs->ssl; | 
|---|
| 1451 |  | 
|---|
| 1452 | if (!hs->next_proto_neg_seen) { | 
|---|
| 1453 | hs->state = state12_read_channel_id; | 
|---|
| 1454 | return ssl_hs_ok; | 
|---|
| 1455 | } | 
|---|
| 1456 |  | 
|---|
| 1457 | SSLMessage msg; | 
|---|
| 1458 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 1459 | return ssl_hs_read_message; | 
|---|
| 1460 | } | 
|---|
| 1461 |  | 
|---|
| 1462 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_NEXT_PROTO) || | 
|---|
| 1463 | !ssl_hash_message(hs, msg)) { | 
|---|
| 1464 | return ssl_hs_error; | 
|---|
| 1465 | } | 
|---|
| 1466 |  | 
|---|
| 1467 | CBS next_protocol = msg.body, selected_protocol, padding; | 
|---|
| 1468 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || | 
|---|
| 1469 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || | 
|---|
| 1470 | CBS_len(&next_protocol) != 0) { | 
|---|
| 1471 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); | 
|---|
| 1472 | ssl_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); | 
|---|
| 1473 | return ssl_hs_error; | 
|---|
| 1474 | } | 
|---|
| 1475 |  | 
|---|
| 1476 | if (!ssl->s3->next_proto_negotiated.CopyFrom(selected_protocol)) { | 
|---|
| 1477 | return ssl_hs_error; | 
|---|
| 1478 | } | 
|---|
| 1479 |  | 
|---|
| 1480 | ssl->method->next_message(ssl); | 
|---|
| 1481 | hs->state = state12_read_channel_id; | 
|---|
| 1482 | return ssl_hs_ok; | 
|---|
| 1483 | } | 
|---|
| 1484 |  | 
|---|
| 1485 | static enum ssl_hs_wait_t do_read_channel_id(SSL_HANDSHAKE *hs) { | 
|---|
| 1486 | SSL *const ssl = hs->ssl; | 
|---|
| 1487 |  | 
|---|
| 1488 | if (!ssl->s3->channel_id_valid) { | 
|---|
| 1489 | hs->state = state12_read_client_finished; | 
|---|
| 1490 | return ssl_hs_ok; | 
|---|
| 1491 | } | 
|---|
| 1492 |  | 
|---|
| 1493 | SSLMessage msg; | 
|---|
| 1494 | if (!ssl->method->get_message(ssl, &msg)) { | 
|---|
| 1495 | return ssl_hs_read_message; | 
|---|
| 1496 | } | 
|---|
| 1497 |  | 
|---|
| 1498 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_CHANNEL_ID) || | 
|---|
| 1499 | !tls1_verify_channel_id(hs, msg) || | 
|---|
| 1500 | !ssl_hash_message(hs, msg)) { | 
|---|
| 1501 | return ssl_hs_error; | 
|---|
| 1502 | } | 
|---|
| 1503 |  | 
|---|
| 1504 | ssl->method->next_message(ssl); | 
|---|
| 1505 | hs->state = state12_read_client_finished; | 
|---|
| 1506 | return ssl_hs_ok; | 
|---|
| 1507 | } | 
|---|
| 1508 |  | 
|---|
| 1509 | static enum ssl_hs_wait_t do_read_client_finished(SSL_HANDSHAKE *hs) { | 
|---|
| 1510 | SSL *const ssl = hs->ssl; | 
|---|
| 1511 | enum ssl_hs_wait_t wait = ssl_get_finished(hs); | 
|---|
| 1512 | if (wait != ssl_hs_ok) { | 
|---|
| 1513 | return wait; | 
|---|
| 1514 | } | 
|---|
| 1515 |  | 
|---|
| 1516 | if (ssl->session != NULL) { | 
|---|
| 1517 | hs->state = state12_finish_server_handshake; | 
|---|
| 1518 | } else { | 
|---|
| 1519 | hs->state = state12_send_server_finished; | 
|---|
| 1520 | } | 
|---|
| 1521 |  | 
|---|
| 1522 | // If this is a full handshake with ChannelID then record the handshake | 
|---|
| 1523 | // hashes in |hs->new_session| in case we need them to verify a | 
|---|
| 1524 | // ChannelID signature on a resumption of this session in the future. | 
|---|
| 1525 | if (ssl->session == NULL && ssl->s3->channel_id_valid && | 
|---|
| 1526 | !tls1_record_handshake_hashes_for_channel_id(hs)) { | 
|---|
| 1527 | return ssl_hs_error; | 
|---|
| 1528 | } | 
|---|
| 1529 |  | 
|---|
| 1530 | return ssl_hs_ok; | 
|---|
| 1531 | } | 
|---|
| 1532 |  | 
|---|
| 1533 | static enum ssl_hs_wait_t do_send_server_finished(SSL_HANDSHAKE *hs) { | 
|---|
| 1534 | SSL *const ssl = hs->ssl; | 
|---|
| 1535 |  | 
|---|
| 1536 | if (hs->ticket_expected) { | 
|---|
| 1537 | const SSL_SESSION *session; | 
|---|
| 1538 | UniquePtr<SSL_SESSION> session_copy; | 
|---|
| 1539 | if (ssl->session == NULL) { | 
|---|
| 1540 | // Fix the timeout to measure from the ticket issuance time. | 
|---|
| 1541 | ssl_session_rebase_time(ssl, hs->new_session.get()); | 
|---|
| 1542 | session = hs->new_session.get(); | 
|---|
| 1543 | } else { | 
|---|
| 1544 | // We are renewing an existing session. Duplicate the session to adjust | 
|---|
| 1545 | // the timeout. | 
|---|
| 1546 | session_copy = | 
|---|
| 1547 | SSL_SESSION_dup(ssl->session.get(), SSL_SESSION_INCLUDE_NONAUTH); | 
|---|
| 1548 | if (!session_copy) { | 
|---|
| 1549 | return ssl_hs_error; | 
|---|
| 1550 | } | 
|---|
| 1551 |  | 
|---|
| 1552 | ssl_session_rebase_time(ssl, session_copy.get()); | 
|---|
| 1553 | session = session_copy.get(); | 
|---|
| 1554 | } | 
|---|
| 1555 |  | 
|---|
| 1556 | ScopedCBB cbb; | 
|---|
| 1557 | CBB body, ticket; | 
|---|
| 1558 | if (!ssl->method->init_message(ssl, cbb.get(), &body, | 
|---|
| 1559 | SSL3_MT_NEW_SESSION_TICKET) || | 
|---|
| 1560 | !CBB_add_u32(&body, session->timeout) || | 
|---|
| 1561 | !CBB_add_u16_length_prefixed(&body, &ticket) || | 
|---|
| 1562 | !ssl_encrypt_ticket(hs, &ticket, session) || | 
|---|
| 1563 | !ssl_add_message_cbb(ssl, cbb.get())) { | 
|---|
| 1564 | return ssl_hs_error; | 
|---|
| 1565 | } | 
|---|
| 1566 | } | 
|---|
| 1567 |  | 
|---|
| 1568 | if (!ssl->method->add_change_cipher_spec(ssl) || | 
|---|
| 1569 | !tls1_change_cipher_state(hs, evp_aead_seal) || | 
|---|
| 1570 | !ssl_send_finished(hs)) { | 
|---|
| 1571 | return ssl_hs_error; | 
|---|
| 1572 | } | 
|---|
| 1573 |  | 
|---|
| 1574 | if (ssl->session != NULL) { | 
|---|
| 1575 | hs->state = state12_read_change_cipher_spec; | 
|---|
| 1576 | } else { | 
|---|
| 1577 | hs->state = state12_finish_server_handshake; | 
|---|
| 1578 | } | 
|---|
| 1579 | return ssl_hs_flush; | 
|---|
| 1580 | } | 
|---|
| 1581 |  | 
|---|
| 1582 | static enum ssl_hs_wait_t do_finish_server_handshake(SSL_HANDSHAKE *hs) { | 
|---|
| 1583 | SSL *const ssl = hs->ssl; | 
|---|
| 1584 |  | 
|---|
| 1585 | if (hs->handback) { | 
|---|
| 1586 | return ssl_hs_handback; | 
|---|
| 1587 | } | 
|---|
| 1588 |  | 
|---|
| 1589 | ssl->method->on_handshake_complete(ssl); | 
|---|
| 1590 |  | 
|---|
| 1591 | // If we aren't retaining peer certificates then we can discard it now. | 
|---|
| 1592 | if (hs->new_session != NULL && | 
|---|
| 1593 | hs->config->retain_only_sha256_of_client_certs) { | 
|---|
| 1594 | hs->new_session->certs.reset(); | 
|---|
| 1595 | ssl->ctx->x509_method->session_clear(hs->new_session.get()); | 
|---|
| 1596 | } | 
|---|
| 1597 |  | 
|---|
| 1598 | if (ssl->session != NULL) { | 
|---|
| 1599 | ssl->s3->established_session = UpRef(ssl->session); | 
|---|
| 1600 | } else { | 
|---|
| 1601 | ssl->s3->established_session = std::move(hs->new_session); | 
|---|
| 1602 | ssl->s3->established_session->not_resumable = false; | 
|---|
| 1603 | } | 
|---|
| 1604 |  | 
|---|
| 1605 | hs->handshake_finalized = true; | 
|---|
| 1606 | ssl->s3->initial_handshake_complete = true; | 
|---|
| 1607 | ssl_update_cache(hs, SSL_SESS_CACHE_SERVER); | 
|---|
| 1608 |  | 
|---|
| 1609 | hs->state = state12_done; | 
|---|
| 1610 | return ssl_hs_ok; | 
|---|
| 1611 | } | 
|---|
| 1612 |  | 
|---|
| 1613 | enum ssl_hs_wait_t ssl_server_handshake(SSL_HANDSHAKE *hs) { | 
|---|
| 1614 | while (hs->state != state12_done) { | 
|---|
| 1615 | enum ssl_hs_wait_t ret = ssl_hs_error; | 
|---|
| 1616 | enum tls12_server_hs_state_t state = | 
|---|
| 1617 | static_cast<enum tls12_server_hs_state_t>(hs->state); | 
|---|
| 1618 | switch (state) { | 
|---|
| 1619 | case state12_start_accept: | 
|---|
| 1620 | ret = do_start_accept(hs); | 
|---|
| 1621 | break; | 
|---|
| 1622 | case state12_read_client_hello: | 
|---|
| 1623 | ret = do_read_client_hello(hs); | 
|---|
| 1624 | break; | 
|---|
| 1625 | case state12_select_certificate: | 
|---|
| 1626 | ret = do_select_certificate(hs); | 
|---|
| 1627 | break; | 
|---|
| 1628 | case state12_tls13: | 
|---|
| 1629 | ret = do_tls13(hs); | 
|---|
| 1630 | break; | 
|---|
| 1631 | case state12_select_parameters: | 
|---|
| 1632 | ret = do_select_parameters(hs); | 
|---|
| 1633 | break; | 
|---|
| 1634 | case state12_send_server_hello: | 
|---|
| 1635 | ret = do_send_server_hello(hs); | 
|---|
| 1636 | break; | 
|---|
| 1637 | case state12_send_server_certificate: | 
|---|
| 1638 | ret = do_send_server_certificate(hs); | 
|---|
| 1639 | break; | 
|---|
| 1640 | case state12_send_server_key_exchange: | 
|---|
| 1641 | ret = do_send_server_key_exchange(hs); | 
|---|
| 1642 | break; | 
|---|
| 1643 | case state12_send_server_hello_done: | 
|---|
| 1644 | ret = do_send_server_hello_done(hs); | 
|---|
| 1645 | break; | 
|---|
| 1646 | case state12_read_client_certificate: | 
|---|
| 1647 | ret = do_read_client_certificate(hs); | 
|---|
| 1648 | break; | 
|---|
| 1649 | case state12_verify_client_certificate: | 
|---|
| 1650 | ret = do_verify_client_certificate(hs); | 
|---|
| 1651 | break; | 
|---|
| 1652 | case state12_read_client_key_exchange: | 
|---|
| 1653 | ret = do_read_client_key_exchange(hs); | 
|---|
| 1654 | break; | 
|---|
| 1655 | case state12_read_client_certificate_verify: | 
|---|
| 1656 | ret = do_read_client_certificate_verify(hs); | 
|---|
| 1657 | break; | 
|---|
| 1658 | case state12_read_change_cipher_spec: | 
|---|
| 1659 | ret = do_read_change_cipher_spec(hs); | 
|---|
| 1660 | break; | 
|---|
| 1661 | case state12_process_change_cipher_spec: | 
|---|
| 1662 | ret = do_process_change_cipher_spec(hs); | 
|---|
| 1663 | break; | 
|---|
| 1664 | case state12_read_next_proto: | 
|---|
| 1665 | ret = do_read_next_proto(hs); | 
|---|
| 1666 | break; | 
|---|
| 1667 | case state12_read_channel_id: | 
|---|
| 1668 | ret = do_read_channel_id(hs); | 
|---|
| 1669 | break; | 
|---|
| 1670 | case state12_read_client_finished: | 
|---|
| 1671 | ret = do_read_client_finished(hs); | 
|---|
| 1672 | break; | 
|---|
| 1673 | case state12_send_server_finished: | 
|---|
| 1674 | ret = do_send_server_finished(hs); | 
|---|
| 1675 | break; | 
|---|
| 1676 | case state12_finish_server_handshake: | 
|---|
| 1677 | ret = do_finish_server_handshake(hs); | 
|---|
| 1678 | break; | 
|---|
| 1679 | case state12_done: | 
|---|
| 1680 | ret = ssl_hs_ok; | 
|---|
| 1681 | break; | 
|---|
| 1682 | } | 
|---|
| 1683 |  | 
|---|
| 1684 | if (hs->state != state) { | 
|---|
| 1685 | ssl_do_info_callback(hs->ssl, SSL_CB_ACCEPT_LOOP, 1); | 
|---|
| 1686 | } | 
|---|
| 1687 |  | 
|---|
| 1688 | if (ret != ssl_hs_ok) { | 
|---|
| 1689 | return ret; | 
|---|
| 1690 | } | 
|---|
| 1691 | } | 
|---|
| 1692 |  | 
|---|
| 1693 | ssl_do_info_callback(hs->ssl, SSL_CB_HANDSHAKE_DONE, 1); | 
|---|
| 1694 | return ssl_hs_ok; | 
|---|
| 1695 | } | 
|---|
| 1696 |  | 
|---|
| 1697 | const char *ssl_server_handshake_state(SSL_HANDSHAKE *hs) { | 
|---|
| 1698 | enum tls12_server_hs_state_t state = | 
|---|
| 1699 | static_cast<enum tls12_server_hs_state_t>(hs->state); | 
|---|
| 1700 | switch (state) { | 
|---|
| 1701 | case state12_start_accept: | 
|---|
| 1702 | return "TLS server start_accept"; | 
|---|
| 1703 | case state12_read_client_hello: | 
|---|
| 1704 | return "TLS server read_client_hello"; | 
|---|
| 1705 | case state12_select_certificate: | 
|---|
| 1706 | return "TLS server select_certificate"; | 
|---|
| 1707 | case state12_tls13: | 
|---|
| 1708 | return tls13_server_handshake_state(hs); | 
|---|
| 1709 | case state12_select_parameters: | 
|---|
| 1710 | return "TLS server select_parameters"; | 
|---|
| 1711 | case state12_send_server_hello: | 
|---|
| 1712 | return "TLS server send_server_hello"; | 
|---|
| 1713 | case state12_send_server_certificate: | 
|---|
| 1714 | return "TLS server send_server_certificate"; | 
|---|
| 1715 | case state12_send_server_key_exchange: | 
|---|
| 1716 | return "TLS server send_server_key_exchange"; | 
|---|
| 1717 | case state12_send_server_hello_done: | 
|---|
| 1718 | return "TLS server send_server_hello_done"; | 
|---|
| 1719 | case state12_read_client_certificate: | 
|---|
| 1720 | return "TLS server read_client_certificate"; | 
|---|
| 1721 | case state12_verify_client_certificate: | 
|---|
| 1722 | return "TLS server verify_client_certificate"; | 
|---|
| 1723 | case state12_read_client_key_exchange: | 
|---|
| 1724 | return "TLS server read_client_key_exchange"; | 
|---|
| 1725 | case state12_read_client_certificate_verify: | 
|---|
| 1726 | return "TLS server read_client_certificate_verify"; | 
|---|
| 1727 | case state12_read_change_cipher_spec: | 
|---|
| 1728 | return "TLS server read_change_cipher_spec"; | 
|---|
| 1729 | case state12_process_change_cipher_spec: | 
|---|
| 1730 | return "TLS server process_change_cipher_spec"; | 
|---|
| 1731 | case state12_read_next_proto: | 
|---|
| 1732 | return "TLS server read_next_proto"; | 
|---|
| 1733 | case state12_read_channel_id: | 
|---|
| 1734 | return "TLS server read_channel_id"; | 
|---|
| 1735 | case state12_read_client_finished: | 
|---|
| 1736 | return "TLS server read_client_finished"; | 
|---|
| 1737 | case state12_send_server_finished: | 
|---|
| 1738 | return "TLS server send_server_finished"; | 
|---|
| 1739 | case state12_finish_server_handshake: | 
|---|
| 1740 | return "TLS server finish_server_handshake"; | 
|---|
| 1741 | case state12_done: | 
|---|
| 1742 | return "TLS server done"; | 
|---|
| 1743 | } | 
|---|
| 1744 |  | 
|---|
| 1745 | return "TLS server unknown"; | 
|---|
| 1746 | } | 
|---|
| 1747 |  | 
|---|
| 1748 | BSSL_NAMESPACE_END | 
|---|
| 1749 |  | 
|---|