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