Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 1 | /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) |
| 2 | * All rights reserved. |
| 3 | * |
| 4 | * This package is an SSL implementation written |
| 5 | * by Eric Young (eay@cryptsoft.com). |
| 6 | * The implementation was written so as to conform with Netscapes SSL. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). */ |
| 108 | /* ==================================================================== |
| 109 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 110 | * ECC cipher suite support in OpenSSL originally developed by |
| 111 | * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project. */ |
| 112 | |
| 113 | #include <openssl/ssl.h> |
| 114 | |
| 115 | #include <assert.h> |
| 116 | |
| 117 | #include "../crypto/internal.h" |
| 118 | #include "internal.h" |
| 119 | |
| 120 | |
| 121 | namespace bssl { |
| 122 | |
| 123 | SSL_HANDSHAKE::SSL_HANDSHAKE(SSL *ssl_arg) |
| 124 | : ssl(ssl_arg), |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 125 | scts_requested(false), |
| 126 | needs_psk_binder(false), |
| 127 | received_hello_retry_request(false), |
| 128 | received_custom_extension(false), |
| 129 | handshake_finalized(false), |
| 130 | accept_psk_mode(false), |
| 131 | cert_request(false), |
| 132 | certificate_status_expected(false), |
| 133 | ocsp_stapling_requested(false), |
| 134 | should_ack_sni(false), |
| 135 | in_false_start(false), |
| 136 | in_early_data(false), |
| 137 | early_data_offered(false), |
| 138 | can_early_read(false), |
| 139 | can_early_write(false), |
| 140 | next_proto_neg_seen(false), |
| 141 | ticket_expected(false), |
| 142 | extended_master_secret(false), |
| 143 | pending_private_key_op(false) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | SSL_HANDSHAKE::~SSL_HANDSHAKE() { |
| 147 | OPENSSL_cleanse(secret, sizeof(secret)); |
| 148 | OPENSSL_cleanse(early_traffic_secret, sizeof(early_traffic_secret)); |
| 149 | OPENSSL_cleanse(client_handshake_secret, sizeof(client_handshake_secret)); |
| 150 | OPENSSL_cleanse(server_handshake_secret, sizeof(server_handshake_secret)); |
| 151 | OPENSSL_cleanse(client_traffic_secret_0, sizeof(client_traffic_secret_0)); |
| 152 | OPENSSL_cleanse(server_traffic_secret_0, sizeof(server_traffic_secret_0)); |
| 153 | OPENSSL_free(cookie); |
| 154 | OPENSSL_free(key_share_bytes); |
| 155 | OPENSSL_free(ecdh_public_key); |
| 156 | OPENSSL_free(peer_sigalgs); |
| 157 | OPENSSL_free(peer_supported_group_list); |
| 158 | OPENSSL_free(peer_key); |
| 159 | OPENSSL_free(server_params); |
| 160 | ssl->ctx->x509_method->hs_flush_cached_ca_names(this); |
| 161 | OPENSSL_free(certificate_types); |
Martin Kreichgauer | 6dc892f | 2017-08-30 10:49:05 -0700 | [diff] [blame^] | 162 | OPENSSL_free(key_block); |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | SSL_HANDSHAKE *ssl_handshake_new(SSL *ssl) { |
| 166 | UniquePtr<SSL_HANDSHAKE> hs = MakeUnique<SSL_HANDSHAKE>(ssl); |
| 167 | if (!hs || |
| 168 | !hs->transcript.Init()) { |
| 169 | return nullptr; |
| 170 | } |
| 171 | return hs.release(); |
| 172 | } |
| 173 | |
| 174 | void ssl_handshake_free(SSL_HANDSHAKE *hs) { Delete(hs); } |
| 175 | |
| 176 | int ssl_check_message_type(SSL *ssl, const SSLMessage &msg, int type) { |
| 177 | if (msg.type != type) { |
| 178 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 179 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
| 180 | ERR_add_error_dataf("got type %d, wanted type %d", msg.type, type); |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | return 1; |
| 185 | } |
| 186 | |
| 187 | int ssl_add_message_cbb(SSL *ssl, CBB *cbb) { |
| 188 | uint8_t *msg; |
| 189 | size_t len; |
| 190 | if (!ssl->method->finish_message(ssl, cbb, &msg, &len) || |
| 191 | !ssl->method->add_message(ssl, msg, len)) { |
| 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | size_t ssl_max_handshake_message_len(const SSL *ssl) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 199 | // kMaxMessageLen is the default maximum message size for handshakes which do |
| 200 | // not accept peer certificate chains. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 201 | static const size_t kMaxMessageLen = 16384; |
| 202 | |
| 203 | if (SSL_in_init(ssl)) { |
| 204 | if ((!ssl->server || (ssl->verify_mode & SSL_VERIFY_PEER)) && |
| 205 | kMaxMessageLen < ssl->max_cert_list) { |
| 206 | return ssl->max_cert_list; |
| 207 | } |
| 208 | return kMaxMessageLen; |
| 209 | } |
| 210 | |
| 211 | if (ssl3_protocol_version(ssl) < TLS1_3_VERSION) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 212 | // In TLS 1.2 and below, the largest acceptable post-handshake message is |
| 213 | // a HelloRequest. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | if (ssl->server) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 218 | // The largest acceptable post-handshake message for a server is a |
| 219 | // KeyUpdate. We will never initiate post-handshake auth. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 220 | return 1; |
| 221 | } |
| 222 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 223 | // Clients must accept NewSessionTicket and CertificateRequest, so allow the |
| 224 | // default size. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 225 | return kMaxMessageLen; |
| 226 | } |
| 227 | |
| 228 | bool ssl_hash_message(SSL_HANDSHAKE *hs, const SSLMessage &msg) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 229 | // V2ClientHello messages are pre-hashed. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 230 | if (msg.is_v2_hello) { |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | return hs->transcript.Update(CBS_data(&msg.raw), CBS_len(&msg.raw)); |
| 235 | } |
| 236 | |
| 237 | int ssl_parse_extensions(const CBS *cbs, uint8_t *out_alert, |
| 238 | const SSL_EXTENSION_TYPE *ext_types, |
| 239 | size_t num_ext_types, int ignore_unknown) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 240 | // Reset everything. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 241 | for (size_t i = 0; i < num_ext_types; i++) { |
| 242 | *ext_types[i].out_present = 0; |
| 243 | CBS_init(ext_types[i].out_data, NULL, 0); |
| 244 | } |
| 245 | |
| 246 | CBS copy = *cbs; |
| 247 | while (CBS_len(©) != 0) { |
| 248 | uint16_t type; |
| 249 | CBS data; |
| 250 | if (!CBS_get_u16(©, &type) || |
| 251 | !CBS_get_u16_length_prefixed(©, &data)) { |
| 252 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 253 | *out_alert = SSL_AD_DECODE_ERROR; |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | const SSL_EXTENSION_TYPE *ext_type = NULL; |
| 258 | for (size_t i = 0; i < num_ext_types; i++) { |
| 259 | if (type == ext_types[i].type) { |
| 260 | ext_type = &ext_types[i]; |
| 261 | break; |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | if (ext_type == NULL) { |
| 266 | if (ignore_unknown) { |
| 267 | continue; |
| 268 | } |
| 269 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_EXTENSION); |
| 270 | *out_alert = SSL_AD_UNSUPPORTED_EXTENSION; |
| 271 | return 0; |
| 272 | } |
| 273 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 274 | // Duplicate ext_types are forbidden. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 275 | if (*ext_type->out_present) { |
| 276 | OPENSSL_PUT_ERROR(SSL, SSL_R_DUPLICATE_EXTENSION); |
| 277 | *out_alert = SSL_AD_ILLEGAL_PARAMETER; |
| 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | *ext_type->out_present = 1; |
| 282 | *ext_type->out_data = data; |
| 283 | } |
| 284 | |
| 285 | return 1; |
| 286 | } |
| 287 | |
| 288 | static void set_crypto_buffer(CRYPTO_BUFFER **dest, CRYPTO_BUFFER *src) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 289 | // TODO(davidben): Remove this helper once |SSL_SESSION| can use |UniquePtr| |
| 290 | // and |UniquePtr| has up_ref helpers. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 291 | CRYPTO_BUFFER_free(*dest); |
| 292 | *dest = src; |
| 293 | if (src != nullptr) { |
| 294 | CRYPTO_BUFFER_up_ref(src); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | enum ssl_verify_result_t ssl_verify_peer_cert(SSL_HANDSHAKE *hs) { |
| 299 | SSL *const ssl = hs->ssl; |
| 300 | const SSL_SESSION *prev_session = ssl->s3->established_session; |
| 301 | if (prev_session != NULL) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 302 | // If renegotiating, the server must not change the server certificate. See |
| 303 | // https://mitls.org/pages/attacks/3SHAKE. We never resume on renegotiation, |
| 304 | // so this check is sufficient to ensure the reported peer certificate never |
| 305 | // changes on renegotiation. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 306 | assert(!ssl->server); |
| 307 | if (sk_CRYPTO_BUFFER_num(prev_session->certs) != |
| 308 | sk_CRYPTO_BUFFER_num(hs->new_session->certs)) { |
| 309 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED); |
| 310 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 311 | return ssl_verify_invalid; |
| 312 | } |
| 313 | |
| 314 | for (size_t i = 0; i < sk_CRYPTO_BUFFER_num(hs->new_session->certs); i++) { |
| 315 | const CRYPTO_BUFFER *old_cert = |
| 316 | sk_CRYPTO_BUFFER_value(prev_session->certs, i); |
| 317 | const CRYPTO_BUFFER *new_cert = |
| 318 | sk_CRYPTO_BUFFER_value(hs->new_session->certs, i); |
| 319 | if (CRYPTO_BUFFER_len(old_cert) != CRYPTO_BUFFER_len(new_cert) || |
| 320 | OPENSSL_memcmp(CRYPTO_BUFFER_data(old_cert), |
| 321 | CRYPTO_BUFFER_data(new_cert), |
| 322 | CRYPTO_BUFFER_len(old_cert)) != 0) { |
| 323 | OPENSSL_PUT_ERROR(SSL, SSL_R_SERVER_CERT_CHANGED); |
| 324 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 325 | return ssl_verify_invalid; |
| 326 | } |
| 327 | } |
| 328 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 329 | // The certificate is identical, so we may skip re-verifying the |
| 330 | // certificate. Since we only authenticated the previous one, copy other |
| 331 | // authentication from the established session and ignore what was newly |
| 332 | // received. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 333 | set_crypto_buffer(&hs->new_session->ocsp_response, |
| 334 | prev_session->ocsp_response); |
| 335 | set_crypto_buffer(&hs->new_session->signed_cert_timestamp_list, |
| 336 | prev_session->signed_cert_timestamp_list); |
| 337 | hs->new_session->verify_result = prev_session->verify_result; |
| 338 | return ssl_verify_ok; |
| 339 | } |
| 340 | |
| 341 | uint8_t alert = SSL_AD_CERTIFICATE_UNKNOWN; |
| 342 | enum ssl_verify_result_t ret; |
| 343 | if (ssl->custom_verify_callback != nullptr) { |
| 344 | ret = ssl->custom_verify_callback(ssl, &alert); |
| 345 | switch (ret) { |
| 346 | case ssl_verify_ok: |
| 347 | hs->new_session->verify_result = X509_V_OK; |
| 348 | break; |
| 349 | case ssl_verify_invalid: |
| 350 | hs->new_session->verify_result = X509_V_ERR_APPLICATION_VERIFICATION; |
| 351 | break; |
| 352 | case ssl_verify_retry: |
| 353 | break; |
| 354 | } |
| 355 | } else { |
| 356 | ret = ssl->ctx->x509_method->session_verify_cert_chain( |
| 357 | hs->new_session.get(), ssl, &alert) |
| 358 | ? ssl_verify_ok |
| 359 | : ssl_verify_invalid; |
| 360 | } |
| 361 | |
| 362 | if (ret == ssl_verify_invalid) { |
| 363 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 364 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 365 | } |
| 366 | |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | uint16_t ssl_get_grease_value(const SSL *ssl, enum ssl_grease_index_t index) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 371 | // Use the client_random or server_random for entropy. This both avoids |
| 372 | // calling |RAND_bytes| on a single byte repeatedly and ensures the values are |
| 373 | // deterministic. This allows the same ClientHello be sent twice for a |
| 374 | // HelloRetryRequest or the same group be advertised in both supported_groups |
| 375 | // and key_shares. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 376 | uint16_t ret = ssl->server ? ssl->s3->server_random[index] |
| 377 | : ssl->s3->client_random[index]; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 378 | // The first four bytes of server_random are a timestamp prior to TLS 1.3, but |
| 379 | // servers have no fields to GREASE until TLS 1.3. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 380 | assert(!ssl->server || ssl3_protocol_version(ssl) >= TLS1_3_VERSION); |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 381 | // This generates a random value of the form 0xωaωa, for all 0 ≤ ω < 16. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 382 | ret = (ret & 0xf0) | 0x0a; |
| 383 | ret |= ret << 8; |
| 384 | return ret; |
| 385 | } |
| 386 | |
| 387 | enum ssl_hs_wait_t ssl_get_finished(SSL_HANDSHAKE *hs) { |
| 388 | SSL *const ssl = hs->ssl; |
| 389 | SSLMessage msg; |
| 390 | if (!ssl->method->get_message(ssl, &msg)) { |
| 391 | return ssl_hs_read_message; |
| 392 | } |
| 393 | |
| 394 | if (!ssl_check_message_type(ssl, msg, SSL3_MT_FINISHED)) { |
| 395 | return ssl_hs_error; |
| 396 | } |
| 397 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 398 | // Snapshot the finished hash before incorporating the new message. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 399 | uint8_t finished[EVP_MAX_MD_SIZE]; |
| 400 | size_t finished_len; |
| 401 | if (!hs->transcript.GetFinishedMAC(finished, &finished_len, |
David Benjamin | 6abaa31 | 2017-08-31 01:12:28 -0400 | [diff] [blame] | 402 | SSL_get_session(ssl), !ssl->server) || |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 403 | !ssl_hash_message(hs, msg)) { |
| 404 | return ssl_hs_error; |
| 405 | } |
| 406 | |
| 407 | int finished_ok = CBS_mem_equal(&msg.body, finished, finished_len); |
| 408 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 409 | finished_ok = 1; |
| 410 | #endif |
| 411 | if (!finished_ok) { |
| 412 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECRYPT_ERROR); |
| 413 | OPENSSL_PUT_ERROR(SSL, SSL_R_DIGEST_CHECK_FAILED); |
| 414 | return ssl_hs_error; |
| 415 | } |
| 416 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 417 | // Copy the Finished so we can use it for renegotiation checks. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 418 | if (ssl->version != SSL3_VERSION) { |
| 419 | if (finished_len > sizeof(ssl->s3->previous_client_finished) || |
| 420 | finished_len > sizeof(ssl->s3->previous_server_finished)) { |
| 421 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 422 | return ssl_hs_error; |
| 423 | } |
| 424 | |
| 425 | if (ssl->server) { |
| 426 | OPENSSL_memcpy(ssl->s3->previous_client_finished, finished, finished_len); |
| 427 | ssl->s3->previous_client_finished_len = finished_len; |
| 428 | } else { |
| 429 | OPENSSL_memcpy(ssl->s3->previous_server_finished, finished, finished_len); |
| 430 | ssl->s3->previous_server_finished_len = finished_len; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | ssl->method->next_message(ssl); |
| 435 | return ssl_hs_ok; |
| 436 | } |
| 437 | |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 438 | int ssl_run_handshake(SSL_HANDSHAKE *hs, bool *out_early_return) { |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 439 | SSL *const ssl = hs->ssl; |
| 440 | for (;;) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 441 | // Resolve the operation the handshake was waiting on. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 442 | switch (hs->wait) { |
| 443 | case ssl_hs_error: |
| 444 | OPENSSL_PUT_ERROR(SSL, SSL_R_SSL_HANDSHAKE_FAILURE); |
| 445 | return -1; |
| 446 | |
| 447 | case ssl_hs_flush: { |
| 448 | int ret = ssl->method->flush_flight(ssl); |
| 449 | if (ret <= 0) { |
| 450 | return ret; |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | case ssl_hs_read_server_hello: |
| 456 | case ssl_hs_read_message: { |
| 457 | int ret = ssl->method->read_message(ssl); |
| 458 | if (ret <= 0) { |
| 459 | uint32_t err = ERR_peek_error(); |
| 460 | if (hs->wait == ssl_hs_read_server_hello && |
| 461 | ERR_GET_LIB(err) == ERR_LIB_SSL && |
| 462 | ERR_GET_REASON(err) == SSL_R_SSLV3_ALERT_HANDSHAKE_FAILURE) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 463 | // Add a dedicated error code to the queue for a handshake_failure |
| 464 | // alert in response to ClientHello. This matches NSS's client |
| 465 | // behavior and gives a better error on a (probable) failure to |
| 466 | // negotiate initial parameters. Note: this error code comes after |
| 467 | // the original one. |
| 468 | // |
| 469 | // See https://crbug.com/446505. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 470 | OPENSSL_PUT_ERROR(SSL, SSL_R_HANDSHAKE_FAILURE_ON_CLIENT_HELLO); |
| 471 | } |
| 472 | return ret; |
| 473 | } |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | case ssl_hs_read_change_cipher_spec: { |
| 478 | int ret = ssl->method->read_change_cipher_spec(ssl); |
| 479 | if (ret <= 0) { |
| 480 | return ret; |
| 481 | } |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | case ssl_hs_read_end_of_early_data: { |
| 486 | if (ssl->s3->hs->can_early_read) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 487 | // While we are processing early data, the handshake returns early. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 488 | *out_early_return = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 489 | return 1; |
| 490 | } |
| 491 | hs->wait = ssl_hs_ok; |
| 492 | break; |
| 493 | } |
| 494 | |
| 495 | case ssl_hs_certificate_selection_pending: |
| 496 | ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 497 | hs->wait = ssl_hs_ok; |
| 498 | return -1; |
| 499 | |
| 500 | case ssl_hs_x509_lookup: |
| 501 | ssl->rwstate = SSL_X509_LOOKUP; |
| 502 | hs->wait = ssl_hs_ok; |
| 503 | return -1; |
| 504 | |
| 505 | case ssl_hs_channel_id_lookup: |
| 506 | ssl->rwstate = SSL_CHANNEL_ID_LOOKUP; |
| 507 | hs->wait = ssl_hs_ok; |
| 508 | return -1; |
| 509 | |
| 510 | case ssl_hs_private_key_operation: |
| 511 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 512 | hs->wait = ssl_hs_ok; |
| 513 | return -1; |
| 514 | |
| 515 | case ssl_hs_pending_session: |
| 516 | ssl->rwstate = SSL_PENDING_SESSION; |
| 517 | hs->wait = ssl_hs_ok; |
| 518 | return -1; |
| 519 | |
| 520 | case ssl_hs_pending_ticket: |
| 521 | ssl->rwstate = SSL_PENDING_TICKET; |
| 522 | hs->wait = ssl_hs_ok; |
| 523 | return -1; |
| 524 | |
| 525 | case ssl_hs_certificate_verify: |
| 526 | ssl->rwstate = SSL_CERTIFICATE_VERIFY; |
| 527 | hs->wait = ssl_hs_ok; |
| 528 | return -1; |
| 529 | |
| 530 | case ssl_hs_early_data_rejected: |
| 531 | ssl->rwstate = SSL_EARLY_DATA_REJECTED; |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 532 | // Cause |SSL_write| to start failing immediately. |
David Benjamin | fd45ee7 | 2017-08-31 14:49:09 -0400 | [diff] [blame] | 533 | hs->can_early_write = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 534 | return -1; |
| 535 | |
| 536 | case ssl_hs_early_return: |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 537 | *out_early_return = true; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 538 | hs->wait = ssl_hs_ok; |
| 539 | return 1; |
| 540 | |
| 541 | case ssl_hs_ok: |
| 542 | break; |
| 543 | } |
| 544 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 545 | // Run the state machine again. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 546 | hs->wait = ssl->do_handshake(hs); |
| 547 | if (hs->wait == ssl_hs_error) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 548 | // Don't loop around to avoid a stray |SSL_R_SSL_HANDSHAKE_FAILURE| the |
| 549 | // first time around. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 550 | return -1; |
| 551 | } |
| 552 | if (hs->wait == ssl_hs_ok) { |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 553 | // The handshake has completed. |
David Benjamin | 74795b3 | 2017-08-31 15:13:12 -0400 | [diff] [blame] | 554 | *out_early_return = false; |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 555 | return 1; |
| 556 | } |
| 557 | |
David Benjamin | c11ea942 | 2017-08-29 16:33:21 -0400 | [diff] [blame] | 558 | // Otherwise, loop to the beginning and resolve what was blocking the |
| 559 | // handshake. |
Steven Valdez | 4d71a9a | 2017-08-14 15:08:34 -0400 | [diff] [blame] | 560 | } |
| 561 | } |
| 562 | |
| 563 | } // namespace bssl |