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