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