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. |
| 7 | * |
| 8 | * This library is free for commercial and non-commercial use as long as |
| 9 | * the following conditions are aheared to. The following conditions |
| 10 | * apply to all code found in this distribution, be it the RC4, RSA, |
| 11 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation |
| 12 | * included with this distribution is covered by the same copyright terms |
| 13 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). |
| 14 | * |
| 15 | * Copyright remains Eric Young's, and as such any Copyright notices in |
| 16 | * the code are not to be removed. |
| 17 | * If this package is used in a product, Eric Young should be given attribution |
| 18 | * as the author of the parts of the library used. |
| 19 | * This can be in the form of a textual message at program startup or |
| 20 | * in documentation (online or textual) provided with the package. |
| 21 | * |
| 22 | * Redistribution and use in source and binary forms, with or without |
| 23 | * modification, are permitted provided that the following conditions |
| 24 | * are met: |
| 25 | * 1. Redistributions of source code must retain the copyright |
| 26 | * notice, this list of conditions and the following disclaimer. |
| 27 | * 2. Redistributions in binary form must reproduce the above copyright |
| 28 | * notice, this list of conditions and the following disclaimer in the |
| 29 | * documentation and/or other materials provided with the distribution. |
| 30 | * 3. All advertising materials mentioning features or use of this software |
| 31 | * must display the following acknowledgement: |
| 32 | * "This product includes cryptographic software written by |
| 33 | * Eric Young (eay@cryptsoft.com)" |
| 34 | * The word 'cryptographic' can be left out if the rouines from the library |
| 35 | * being used are not cryptographic related :-). |
| 36 | * 4. If you include any Windows specific code (or a derivative thereof) from |
| 37 | * the apps directory (application code) you must include an acknowledgement: |
| 38 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" |
| 39 | * |
| 40 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND |
| 41 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 42 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 43 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 44 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 45 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 46 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 48 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 49 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 50 | * SUCH DAMAGE. |
| 51 | * |
| 52 | * The licence and distribution terms for any publically available version or |
| 53 | * derivative of this code cannot be changed. i.e. this code cannot simply be |
| 54 | * copied and put under another distribution licence |
| 55 | * [including the GNU Public Licence.] |
| 56 | */ |
| 57 | /* ==================================================================== |
| 58 | * Copyright (c) 1998-2007 The OpenSSL Project. All rights reserved. |
| 59 | * |
| 60 | * Redistribution and use in source and binary forms, with or without |
| 61 | * modification, are permitted provided that the following conditions |
| 62 | * are met: |
| 63 | * |
| 64 | * 1. Redistributions of source code must retain the above copyright |
| 65 | * notice, this list of conditions and the following disclaimer. |
| 66 | * |
| 67 | * 2. Redistributions in binary form must reproduce the above copyright |
| 68 | * notice, this list of conditions and the following disclaimer in |
| 69 | * the documentation and/or other materials provided with the |
| 70 | * distribution. |
| 71 | * |
| 72 | * 3. All advertising materials mentioning features or use of this |
| 73 | * software must display the following acknowledgment: |
| 74 | * "This product includes software developed by the OpenSSL Project |
| 75 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" |
| 76 | * |
| 77 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 78 | * endorse or promote products derived from this software without |
| 79 | * prior written permission. For written permission, please contact |
| 80 | * openssl-core@openssl.org. |
| 81 | * |
| 82 | * 5. Products derived from this software may not be called "OpenSSL" |
| 83 | * nor may "OpenSSL" appear in their names without prior written |
| 84 | * permission of the OpenSSL Project. |
| 85 | * |
| 86 | * 6. Redistributions of any form whatsoever must retain the following |
| 87 | * acknowledgment: |
| 88 | * "This product includes software developed by the OpenSSL Project |
| 89 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" |
| 90 | * |
| 91 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 92 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 93 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 94 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 95 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 96 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 97 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 98 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 99 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 100 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 101 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 102 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 103 | * ==================================================================== |
| 104 | * |
| 105 | * This product includes cryptographic software written by Eric Young |
| 106 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 107 | * Hudson (tjh@cryptsoft.com). |
| 108 | * |
| 109 | */ |
| 110 | /* ==================================================================== |
| 111 | * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. |
| 112 | * |
| 113 | * Portions of the attached software ("Contribution") are developed by |
| 114 | * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project. |
| 115 | * |
| 116 | * The Contribution is licensed pursuant to the OpenSSL open source |
| 117 | * license provided above. |
| 118 | * |
| 119 | * ECC cipher suite support in OpenSSL originally written by |
| 120 | * Vipul Gupta and Sumit Gupta of Sun Microsystems Laboratories. |
| 121 | * |
| 122 | */ |
| 123 | /* ==================================================================== |
| 124 | * Copyright 2005 Nokia. All rights reserved. |
| 125 | * |
| 126 | * The portions of the attached software ("Contribution") is developed by |
| 127 | * Nokia Corporation and is licensed pursuant to the OpenSSL open source |
| 128 | * license. |
| 129 | * |
| 130 | * The Contribution, originally written by Mika Kousa and Pasi Eronen of |
| 131 | * Nokia Corporation, consists of the "PSK" (Pre-Shared Key) ciphersuites |
| 132 | * support (see RFC 4279) to OpenSSL. |
| 133 | * |
| 134 | * No patent licenses or other rights except those expressly stated in |
| 135 | * the OpenSSL open source license shall be deemed granted or received |
| 136 | * expressly, by implication, estoppel, or otherwise. |
| 137 | * |
| 138 | * No assurances are provided by Nokia that the Contribution does not |
| 139 | * infringe the patent or other intellectual property rights of any third |
| 140 | * party or that the license provides you with all the necessary rights |
| 141 | * to make use of the Contribution. |
| 142 | * |
| 143 | * THE SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. IN |
| 144 | * ADDITION TO THE DISCLAIMERS INCLUDED IN THE LICENSE, NOKIA |
| 145 | * SPECIFICALLY DISCLAIMS ANY LIABILITY FOR CLAIMS BROUGHT BY YOU OR ANY |
| 146 | * OTHER ENTITY BASED ON INFRINGEMENT OF INTELLECTUAL PROPERTY RIGHTS OR |
| 147 | * OTHERWISE. */ |
| 148 | |
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> |
| 158 | #include <openssl/dh.h> |
| 159 | #include <openssl/ec.h> |
| 160 | #include <openssl/ecdsa.h> |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 161 | #include <openssl/err.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 162 | #include <openssl/evp.h> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 166 | #include <openssl/nid.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | #include <openssl/rand.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 168 | #include <openssl/x509.h> |
| 169 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 170 | #include "internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 171 | #include "../crypto/internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 172 | |
| 173 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 174 | static int ssl3_process_client_hello(SSL_HANDSHAKE *hs); |
| 175 | static int ssl3_select_certificate(SSL_HANDSHAKE *hs); |
| 176 | static int ssl3_select_parameters(SSL_HANDSHAKE *hs); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 177 | static int ssl3_send_server_hello(SSL_HANDSHAKE *hs); |
| 178 | static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs); |
| 179 | static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs); |
| 180 | static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs); |
| 181 | static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs); |
| 182 | static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs); |
| 183 | static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs); |
| 184 | static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs); |
| 185 | static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs); |
| 186 | static int ssl3_get_next_proto(SSL_HANDSHAKE *hs); |
| 187 | static int ssl3_get_channel_id(SSL_HANDSHAKE *hs); |
| 188 | static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 189 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 190 | static struct CRYPTO_STATIC_MUTEX g_v2clienthello_lock = |
| 191 | CRYPTO_STATIC_MUTEX_INIT; |
| 192 | static uint64_t g_v2clienthello_count = 0; |
| 193 | |
| 194 | uint64_t SSL_get_v2clienthello_count(void) { |
| 195 | CRYPTO_STATIC_MUTEX_lock_read(&g_v2clienthello_lock); |
| 196 | uint64_t ret = g_v2clienthello_count; |
| 197 | CRYPTO_STATIC_MUTEX_unlock_read(&g_v2clienthello_lock); |
| 198 | return ret; |
| 199 | } |
| 200 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 201 | int ssl3_accept(SSL_HANDSHAKE *hs) { |
| 202 | SSL *const ssl = hs->ssl; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 203 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 204 | int ret = -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 205 | int state, skip = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 206 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 207 | assert(ssl->handshake_func == ssl3_accept); |
| 208 | assert(ssl->server); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 209 | |
| 210 | for (;;) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 211 | state = hs->state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 212 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 213 | switch (hs->state) { |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 214 | case SSL_ST_INIT: |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 215 | hs->state = SSL_ST_ACCEPT; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 216 | skip = 1; |
| 217 | break; |
| 218 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 219 | case SSL_ST_ACCEPT: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 220 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 221 | hs->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 222 | break; |
| 223 | |
| 224 | case SSL3_ST_SR_CLNT_HELLO_A: |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 225 | ret = ssl->method->ssl_get_message(ssl); |
| 226 | if (ret <= 0) { |
| 227 | goto end; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 228 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 229 | hs->state = SSL3_ST_SR_CLNT_HELLO_B; |
| 230 | break; |
| 231 | |
| 232 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 233 | ret = ssl3_process_client_hello(hs); |
| 234 | if (ret <= 0) { |
| 235 | goto end; |
| 236 | } |
| 237 | hs->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 238 | break; |
| 239 | |
| 240 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 241 | ret = ssl3_select_certificate(hs); |
| 242 | if (ret <= 0) { |
| 243 | goto end; |
| 244 | } |
| 245 | if (hs->state != SSL_ST_TLS13) { |
| 246 | hs->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 247 | } |
| 248 | break; |
| 249 | |
| 250 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 251 | ret = ssl3_select_parameters(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 252 | if (ret <= 0) { |
| 253 | goto end; |
| 254 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 255 | ssl->method->received_flight(ssl); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 256 | hs->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 257 | break; |
| 258 | |
| 259 | case SSL3_ST_SW_SRVR_HELLO_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 260 | ret = ssl3_send_server_hello(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 261 | if (ret <= 0) { |
| 262 | goto end; |
| 263 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 264 | if (ssl->session != NULL) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 265 | hs->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 266 | } else { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 267 | hs->state = SSL3_ST_SW_CERT_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 268 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 269 | break; |
| 270 | |
| 271 | case SSL3_ST_SW_CERT_A: |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 272 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 273 | ret = ssl3_send_server_certificate(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 274 | if (ret <= 0) { |
| 275 | goto end; |
| 276 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 277 | } else { |
| 278 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 279 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 280 | hs->state = SSL3_ST_SW_CERT_STATUS_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 281 | break; |
| 282 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 283 | case SSL3_ST_SW_CERT_STATUS_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 284 | if (hs->certificate_status_expected) { |
| 285 | ret = ssl3_send_certificate_status(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 286 | if (ret <= 0) { |
| 287 | goto end; |
| 288 | } |
| 289 | } else { |
| 290 | skip = 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 291 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 292 | hs->state = SSL3_ST_SW_KEY_EXCH_A; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 293 | break; |
| 294 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 295 | case SSL3_ST_SW_KEY_EXCH_A: |
| 296 | case SSL3_ST_SW_KEY_EXCH_B: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 297 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 298 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 299 | /* PSK ciphers send ServerKeyExchange if there is an identity hint. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 300 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) || |
| 301 | ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 302 | ret = ssl3_send_server_key_exchange(hs); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 303 | if (ret <= 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 304 | goto end; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 305 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 306 | } else { |
| 307 | skip = 1; |
| 308 | } |
| 309 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 310 | hs->state = SSL3_ST_SW_CERT_REQ_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 311 | break; |
| 312 | |
| 313 | case SSL3_ST_SW_CERT_REQ_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 314 | if (hs->cert_request) { |
| 315 | ret = ssl3_send_certificate_request(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 316 | if (ret <= 0) { |
| 317 | goto end; |
| 318 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 319 | } else { |
| 320 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 321 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 322 | hs->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 323 | break; |
| 324 | |
| 325 | case SSL3_ST_SW_SRVR_DONE_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 326 | ret = ssl3_send_server_hello_done(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 327 | if (ret <= 0) { |
| 328 | goto end; |
| 329 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 330 | hs->next_state = SSL3_ST_SR_CERT_A; |
| 331 | hs->state = SSL3_ST_SW_FLUSH; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 332 | break; |
| 333 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 334 | case SSL3_ST_SR_CERT_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 335 | if (hs->cert_request) { |
| 336 | ret = ssl3_get_client_certificate(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 337 | if (ret <= 0) { |
| 338 | goto end; |
| 339 | } |
| 340 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 341 | hs->state = SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 342 | break; |
| 343 | |
| 344 | case SSL3_ST_SR_KEY_EXCH_A: |
| 345 | case SSL3_ST_SR_KEY_EXCH_B: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 346 | ret = ssl3_get_client_key_exchange(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 347 | if (ret <= 0) { |
| 348 | goto end; |
| 349 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 350 | hs->state = SSL3_ST_SR_CERT_VRFY_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 351 | break; |
| 352 | |
| 353 | case SSL3_ST_SR_CERT_VRFY_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 354 | ret = ssl3_get_cert_verify(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 355 | if (ret <= 0) { |
| 356 | goto end; |
| 357 | } |
| 358 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 359 | hs->state = SSL3_ST_SR_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 360 | break; |
| 361 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 362 | case SSL3_ST_SR_CHANGE: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 363 | ret = ssl->method->read_change_cipher_spec(ssl); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 364 | if (ret <= 0) { |
| 365 | goto end; |
| 366 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 367 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 368 | if (!tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_READ)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 369 | ret = -1; |
| 370 | goto end; |
| 371 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 372 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 373 | hs->state = SSL3_ST_SR_NEXT_PROTO_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 374 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 375 | |
| 376 | case SSL3_ST_SR_NEXT_PROTO_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 377 | if (hs->next_proto_neg_seen) { |
| 378 | ret = ssl3_get_next_proto(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 379 | if (ret <= 0) { |
| 380 | goto end; |
| 381 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 382 | } else { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 383 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 384 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 385 | hs->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case SSL3_ST_SR_CHANNEL_ID_A: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 389 | if (ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 390 | ret = ssl3_get_channel_id(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 391 | if (ret <= 0) { |
| 392 | goto end; |
| 393 | } |
| 394 | } else { |
| 395 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 396 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 397 | hs->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 398 | break; |
| 399 | |
| 400 | case SSL3_ST_SR_FINISHED_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 401 | ret = ssl3_get_finished(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 402 | if (ret <= 0) { |
| 403 | goto end; |
| 404 | } |
| 405 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 406 | ssl->method->received_flight(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 407 | if (ssl->session != NULL) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 408 | hs->state = SSL_ST_OK; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 409 | } else { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 410 | hs->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 411 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 412 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 413 | /* If this is a full handshake with ChannelID then record the handshake |
| 414 | * hashes in |ssl->s3->new_session| in case we need them to verify a |
| 415 | * ChannelID signature on a resumption of this session in the future. */ |
| 416 | if (ssl->session == NULL && ssl->s3->tlsext_channel_id_valid) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 417 | ret = tls1_record_handshake_hashes_for_channel_id(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 418 | if (ret <= 0) { |
| 419 | goto end; |
| 420 | } |
| 421 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 422 | break; |
| 423 | |
| 424 | case SSL3_ST_SW_SESSION_TICKET_A: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 425 | if (hs->ticket_expected) { |
| 426 | ret = ssl3_send_new_session_ticket(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 427 | if (ret <= 0) { |
| 428 | goto end; |
| 429 | } |
| 430 | } else { |
| 431 | skip = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 432 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 433 | hs->state = SSL3_ST_SW_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 434 | break; |
| 435 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 436 | case SSL3_ST_SW_CHANGE: |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 437 | if (!ssl->method->add_change_cipher_spec(ssl) || |
| 438 | !tls1_change_cipher_state(hs, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 439 | ret = -1; |
| 440 | goto end; |
| 441 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 442 | |
| 443 | hs->state = SSL3_ST_SW_FINISHED_A; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 444 | break; |
| 445 | |
| 446 | case SSL3_ST_SW_FINISHED_A: |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 447 | ret = ssl3_send_finished(hs); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 448 | if (ret <= 0) { |
| 449 | goto end; |
| 450 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 451 | hs->state = SSL3_ST_SW_FLUSH; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 452 | if (ssl->session != NULL) { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 453 | hs->next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 454 | } else { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 455 | hs->next_state = SSL_ST_OK; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 456 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 457 | break; |
| 458 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 459 | case SSL3_ST_SW_FLUSH: |
David Benjamin | f31229b | 2017-01-25 14:08:15 -0500 | [diff] [blame] | 460 | ret = ssl->method->flush_flight(ssl); |
| 461 | if (ret <= 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 462 | goto end; |
| 463 | } |
| 464 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 465 | hs->state = hs->next_state; |
| 466 | if (hs->state != SSL_ST_OK) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 467 | ssl->method->expect_flight(ssl); |
| 468 | } |
| 469 | break; |
| 470 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 471 | case SSL_ST_TLS13: |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 472 | ret = tls13_handshake(hs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 473 | if (ret <= 0) { |
| 474 | goto end; |
| 475 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 476 | hs->state = SSL_ST_OK; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 477 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 478 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 479 | case SSL_ST_OK: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 480 | ssl->method->release_current_message(ssl, 1 /* free_buffer */); |
| 481 | |
| 482 | /* If we aren't retaining peer certificates then we can discard it |
| 483 | * now. */ |
| 484 | if (ssl->s3->new_session != NULL && |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 485 | ssl->retain_only_sha256_of_client_certs) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 486 | sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, |
| 487 | CRYPTO_BUFFER_free); |
| 488 | ssl->s3->new_session->certs = NULL; |
| 489 | ssl->ctx->x509_method->session_clear(ssl->s3->new_session); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | SSL_SESSION_free(ssl->s3->established_session); |
| 493 | if (ssl->session != NULL) { |
| 494 | SSL_SESSION_up_ref(ssl->session); |
| 495 | ssl->s3->established_session = ssl->session; |
| 496 | } else { |
| 497 | ssl->s3->established_session = ssl->s3->new_session; |
| 498 | ssl->s3->established_session->not_resumable = 0; |
| 499 | ssl->s3->new_session = NULL; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 500 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 501 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 502 | if (hs->v2_clienthello) { |
| 503 | CRYPTO_STATIC_MUTEX_lock_write(&g_v2clienthello_lock); |
| 504 | g_v2clienthello_count++; |
| 505 | CRYPTO_STATIC_MUTEX_unlock_write(&g_v2clienthello_lock); |
| 506 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 507 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 508 | ssl->s3->initial_handshake_complete = 1; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 509 | ssl_update_cache(hs, SSL_SESS_CACHE_SERVER); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 510 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 511 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 512 | ret = 1; |
| 513 | goto end; |
| 514 | |
| 515 | default: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 516 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 517 | ret = -1; |
| 518 | goto end; |
| 519 | } |
| 520 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 521 | if (!ssl->s3->tmp.reuse_message && !skip && hs->state != state) { |
| 522 | int new_state = hs->state; |
| 523 | hs->state = state; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 524 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 525 | hs->state = new_state; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 526 | } |
| 527 | skip = 0; |
| 528 | } |
| 529 | |
| 530 | end: |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 531 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 532 | return ret; |
| 533 | } |
| 534 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 535 | int ssl_client_cipher_list_contains_cipher(const SSL_CLIENT_HELLO *client_hello, |
| 536 | uint16_t id) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 537 | CBS cipher_suites; |
| 538 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 539 | client_hello->cipher_suites_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 540 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 541 | while (CBS_len(&cipher_suites) > 0) { |
| 542 | uint16_t got_id; |
| 543 | if (!CBS_get_u16(&cipher_suites, &got_id)) { |
| 544 | return 0; |
| 545 | } |
| 546 | |
| 547 | if (got_id == id) { |
| 548 | return 1; |
| 549 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 550 | } |
| 551 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 552 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 553 | } |
| 554 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 555 | static int negotiate_version(SSL_HANDSHAKE *hs, uint8_t *out_alert, |
| 556 | const SSL_CLIENT_HELLO *client_hello) { |
| 557 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 558 | assert(!ssl->s3->have_version); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 559 | uint16_t min_version, max_version; |
| 560 | if (!ssl_get_version_range(ssl, &min_version, &max_version)) { |
| 561 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 562 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 563 | } |
| 564 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 565 | uint16_t version = 0; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 566 | /* Check supported_versions extension if it is present. */ |
| 567 | CBS supported_versions; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 568 | if (ssl_client_hello_get_extension(client_hello, &supported_versions, |
| 569 | TLSEXT_TYPE_supported_versions)) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 570 | CBS versions; |
| 571 | if (!CBS_get_u8_length_prefixed(&supported_versions, &versions) || |
| 572 | CBS_len(&supported_versions) != 0 || |
| 573 | CBS_len(&versions) == 0) { |
| 574 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 575 | *out_alert = SSL_AD_DECODE_ERROR; |
| 576 | return 0; |
| 577 | } |
| 578 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 579 | /* Choose the newest commonly-supported version advertised by the client. |
| 580 | * The client orders the versions according to its preferences, but we're |
| 581 | * not required to honor the client's preferences. */ |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 582 | int found_version = 0; |
| 583 | while (CBS_len(&versions) != 0) { |
| 584 | uint16_t ext_version; |
| 585 | if (!CBS_get_u16(&versions, &ext_version)) { |
| 586 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 587 | *out_alert = SSL_AD_DECODE_ERROR; |
| 588 | return 0; |
| 589 | } |
| 590 | if (!ssl->method->version_from_wire(&ext_version, ext_version)) { |
| 591 | continue; |
| 592 | } |
| 593 | if (min_version <= ext_version && |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 594 | ext_version <= max_version && |
| 595 | (!found_version || version < ext_version)) { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 596 | version = ext_version; |
| 597 | found_version = 1; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 598 | } |
| 599 | } |
| 600 | |
| 601 | if (!found_version) { |
| 602 | goto unsupported_protocol; |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 603 | } |
| 604 | } else { |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 605 | /* Process ClientHello.version instead. Note that versions beyond (D)TLS 1.2 |
| 606 | * do not use this mechanism. */ |
| 607 | if (SSL_is_dtls(ssl)) { |
| 608 | if (client_hello->version <= DTLS1_2_VERSION) { |
| 609 | version = TLS1_2_VERSION; |
| 610 | } else if (client_hello->version <= DTLS1_VERSION) { |
| 611 | version = TLS1_1_VERSION; |
| 612 | } else { |
| 613 | goto unsupported_protocol; |
| 614 | } |
| 615 | } else { |
| 616 | if (client_hello->version >= TLS1_2_VERSION) { |
| 617 | version = TLS1_2_VERSION; |
| 618 | } else if (client_hello->version >= TLS1_1_VERSION) { |
| 619 | version = TLS1_1_VERSION; |
| 620 | } else if (client_hello->version >= TLS1_VERSION) { |
| 621 | version = TLS1_VERSION; |
| 622 | } else if (client_hello->version >= SSL3_VERSION) { |
| 623 | version = SSL3_VERSION; |
| 624 | } else { |
| 625 | goto unsupported_protocol; |
| 626 | } |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 627 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 628 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 629 | /* Apply our minimum and maximum version. */ |
| 630 | if (version > max_version) { |
| 631 | version = max_version; |
| 632 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 633 | |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 634 | if (version < min_version) { |
| 635 | goto unsupported_protocol; |
| 636 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 637 | } |
| 638 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 639 | /* Handle FALLBACK_SCSV. */ |
| 640 | if (ssl_client_cipher_list_contains_cipher(client_hello, |
| 641 | SSL3_CK_FALLBACK_SCSV & 0xffff) && |
| 642 | version < max_version) { |
| 643 | OPENSSL_PUT_ERROR(SSL, SSL_R_INAPPROPRIATE_FALLBACK); |
| 644 | *out_alert = SSL3_AD_INAPPROPRIATE_FALLBACK; |
| 645 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 646 | } |
| 647 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 648 | hs->client_version = client_hello->version; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 649 | ssl->version = ssl->method->version_to_wire(version); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 650 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 651 | /* At this point, the connection's version is known and |ssl->version| is |
| 652 | * fixed. Begin enforcing the record-layer version. */ |
| 653 | ssl->s3->have_version = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 654 | |
| 655 | return 1; |
Steven Valdez | bb1ceac | 2016-10-07 10:34:51 -0400 | [diff] [blame] | 656 | |
| 657 | unsupported_protocol: |
| 658 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
| 659 | *out_alert = SSL_AD_PROTOCOL_VERSION; |
| 660 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 661 | } |
| 662 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 663 | static STACK_OF(SSL_CIPHER) * |
| 664 | ssl_parse_client_cipher_list(const SSL_CLIENT_HELLO *client_hello) { |
| 665 | CBS cipher_suites; |
| 666 | CBS_init(&cipher_suites, client_hello->cipher_suites, |
| 667 | client_hello->cipher_suites_len); |
| 668 | |
| 669 | STACK_OF(SSL_CIPHER) *sk = sk_SSL_CIPHER_new_null(); |
| 670 | if (sk == NULL) { |
| 671 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 672 | goto err; |
| 673 | } |
| 674 | |
| 675 | while (CBS_len(&cipher_suites) > 0) { |
| 676 | uint16_t cipher_suite; |
| 677 | |
| 678 | if (!CBS_get_u16(&cipher_suites, &cipher_suite)) { |
| 679 | OPENSSL_PUT_ERROR(SSL, SSL_R_ERROR_IN_RECEIVED_CIPHER_LIST); |
| 680 | goto err; |
| 681 | } |
| 682 | |
| 683 | const SSL_CIPHER *c = SSL_get_cipher_by_value(cipher_suite); |
| 684 | if (c != NULL && !sk_SSL_CIPHER_push(sk, c)) { |
| 685 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
| 686 | goto err; |
| 687 | } |
| 688 | } |
| 689 | |
| 690 | return sk; |
| 691 | |
| 692 | err: |
| 693 | sk_SSL_CIPHER_free(sk); |
| 694 | return NULL; |
| 695 | } |
| 696 | |
| 697 | /* ssl_get_compatible_server_ciphers determines the key exchange and |
| 698 | * authentication cipher suite masks compatible with the server configuration |
| 699 | * and current ClientHello parameters of |hs|. It sets |*out_mask_k| to the key |
| 700 | * exchange mask and |*out_mask_a| to the authentication mask. */ |
| 701 | static void ssl_get_compatible_server_ciphers(SSL_HANDSHAKE *hs, |
| 702 | uint32_t *out_mask_k, |
| 703 | uint32_t *out_mask_a) { |
| 704 | SSL *const ssl = hs->ssl; |
| 705 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 706 | *out_mask_k = SSL_kGENERIC; |
| 707 | *out_mask_a = SSL_aGENERIC; |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | uint32_t mask_k = 0; |
| 712 | uint32_t mask_a = 0; |
| 713 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 714 | if (ssl_has_certificate(ssl)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 715 | int type = ssl_private_key_type(ssl); |
| 716 | if (type == NID_rsaEncryption) { |
| 717 | mask_k |= SSL_kRSA; |
| 718 | mask_a |= SSL_aRSA; |
| 719 | } else if (ssl_is_ecdsa_key_type(type)) { |
| 720 | mask_a |= SSL_aECDSA; |
| 721 | } |
| 722 | } |
| 723 | |
| 724 | if (ssl->cert->dh_tmp != NULL || ssl->cert->dh_tmp_cb != NULL) { |
| 725 | mask_k |= SSL_kDHE; |
| 726 | } |
| 727 | |
| 728 | /* Check for a shared group to consider ECDHE ciphers. */ |
| 729 | uint16_t unused; |
| 730 | if (tls1_get_shared_group(hs, &unused)) { |
| 731 | mask_k |= SSL_kECDHE; |
| 732 | } |
| 733 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 734 | /* PSK requires a server callback. */ |
| 735 | if (ssl->psk_server_callback != NULL) { |
| 736 | mask_k |= SSL_kPSK; |
| 737 | mask_a |= SSL_aPSK; |
| 738 | } |
| 739 | |
| 740 | *out_mask_k = mask_k; |
| 741 | *out_mask_a = mask_a; |
| 742 | } |
| 743 | |
| 744 | static const SSL_CIPHER *ssl3_choose_cipher( |
| 745 | SSL_HANDSHAKE *hs, const SSL_CLIENT_HELLO *client_hello, |
| 746 | const struct ssl_cipher_preference_list_st *server_pref) { |
| 747 | SSL *const ssl = hs->ssl; |
| 748 | const SSL_CIPHER *c, *ret = NULL; |
| 749 | STACK_OF(SSL_CIPHER) *srvr = server_pref->ciphers, *prio, *allow; |
| 750 | int ok; |
| 751 | size_t cipher_index; |
| 752 | uint32_t alg_k, alg_a, mask_k, mask_a; |
| 753 | /* in_group_flags will either be NULL, or will point to an array of bytes |
| 754 | * which indicate equal-preference groups in the |prio| stack. See the |
| 755 | * comment about |in_group_flags| in the |ssl_cipher_preference_list_st| |
| 756 | * struct. */ |
| 757 | const uint8_t *in_group_flags; |
| 758 | /* group_min contains the minimal index so far found in a group, or -1 if no |
| 759 | * such value exists yet. */ |
| 760 | int group_min = -1; |
| 761 | |
| 762 | STACK_OF(SSL_CIPHER) *clnt = ssl_parse_client_cipher_list(client_hello); |
| 763 | if (clnt == NULL) { |
| 764 | return NULL; |
| 765 | } |
| 766 | |
| 767 | if (ssl->options & SSL_OP_CIPHER_SERVER_PREFERENCE) { |
| 768 | prio = srvr; |
| 769 | in_group_flags = server_pref->in_group_flags; |
| 770 | allow = clnt; |
| 771 | } else { |
| 772 | prio = clnt; |
| 773 | in_group_flags = NULL; |
| 774 | allow = srvr; |
| 775 | } |
| 776 | |
| 777 | ssl_get_compatible_server_ciphers(hs, &mask_k, &mask_a); |
| 778 | |
| 779 | for (size_t i = 0; i < sk_SSL_CIPHER_num(prio); i++) { |
| 780 | c = sk_SSL_CIPHER_value(prio, i); |
| 781 | |
| 782 | ok = 1; |
| 783 | |
| 784 | /* Check the TLS version. */ |
| 785 | if (SSL_CIPHER_get_min_version(c) > ssl3_protocol_version(ssl) || |
| 786 | SSL_CIPHER_get_max_version(c) < ssl3_protocol_version(ssl)) { |
| 787 | ok = 0; |
| 788 | } |
| 789 | |
| 790 | alg_k = c->algorithm_mkey; |
| 791 | alg_a = c->algorithm_auth; |
| 792 | |
| 793 | ok = ok && (alg_k & mask_k) && (alg_a & mask_a); |
| 794 | |
| 795 | if (ok && sk_SSL_CIPHER_find(allow, &cipher_index, c)) { |
| 796 | if (in_group_flags != NULL && in_group_flags[i] == 1) { |
| 797 | /* This element of |prio| is in a group. Update the minimum index found |
| 798 | * so far and continue looking. */ |
| 799 | if (group_min == -1 || (size_t)group_min > cipher_index) { |
| 800 | group_min = cipher_index; |
| 801 | } |
| 802 | } else { |
| 803 | if (group_min != -1 && (size_t)group_min < cipher_index) { |
| 804 | cipher_index = group_min; |
| 805 | } |
| 806 | ret = sk_SSL_CIPHER_value(allow, cipher_index); |
| 807 | break; |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | if (in_group_flags != NULL && in_group_flags[i] == 0 && group_min != -1) { |
| 812 | /* We are about to leave a group, but we found a match in it, so that's |
| 813 | * our answer. */ |
| 814 | ret = sk_SSL_CIPHER_value(allow, group_min); |
| 815 | break; |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | sk_SSL_CIPHER_free(clnt); |
| 820 | return ret; |
| 821 | } |
| 822 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 823 | static int ssl3_process_client_hello(SSL_HANDSHAKE *hs) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 824 | SSL *const ssl = hs->ssl; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 825 | if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_HELLO)) { |
| 826 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 827 | } |
| 828 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 829 | SSL_CLIENT_HELLO client_hello; |
| 830 | if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg, |
| 831 | ssl->init_num)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 832 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 833 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 834 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 835 | } |
| 836 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 837 | /* Run the early callback. */ |
| 838 | if (ssl->ctx->select_certificate_cb != NULL) { |
| 839 | switch (ssl->ctx->select_certificate_cb(&client_hello)) { |
| 840 | case 0: |
| 841 | ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 842 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 843 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 844 | case -1: |
| 845 | /* Connection rejected. */ |
| 846 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 847 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 848 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 849 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 850 | default: |
| 851 | /* fallthrough */; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 852 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 853 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 854 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 855 | uint8_t alert = SSL_AD_DECODE_ERROR; |
| 856 | if (!negotiate_version(hs, &alert, &client_hello)) { |
| 857 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
| 858 | return -1; |
| 859 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 860 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 861 | /* Load the client random. */ |
| 862 | if (client_hello.random_len != SSL3_RANDOM_SIZE) { |
| 863 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 864 | return -1; |
| 865 | } |
| 866 | OPENSSL_memcpy(ssl->s3->client_random, client_hello.random, |
| 867 | client_hello.random_len); |
| 868 | |
| 869 | /* Only null compression is supported. TLS 1.3 further requires the peer |
| 870 | * advertise no other compression. */ |
| 871 | if (OPENSSL_memchr(client_hello.compression_methods, 0, |
| 872 | client_hello.compression_methods_len) == NULL || |
| 873 | (ssl3_protocol_version(ssl) >= TLS1_3_VERSION && |
| 874 | client_hello.compression_methods_len != 1)) { |
| 875 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_COMPRESSION_LIST); |
| 876 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_ILLEGAL_PARAMETER); |
| 877 | return -1; |
| 878 | } |
| 879 | |
| 880 | /* TLS extensions. */ |
| 881 | if (!ssl_parse_clienthello_tlsext(hs, &client_hello)) { |
| 882 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
| 883 | return -1; |
| 884 | } |
| 885 | |
| 886 | return 1; |
| 887 | } |
| 888 | |
| 889 | static int ssl3_select_certificate(SSL_HANDSHAKE *hs) { |
| 890 | SSL *const ssl = hs->ssl; |
| 891 | /* Call |cert_cb| to update server certificates if required. */ |
| 892 | if (ssl->cert->cert_cb != NULL) { |
| 893 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
| 894 | if (rv == 0) { |
| 895 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
| 896 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 897 | return -1; |
| 898 | } |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 899 | if (rv < 0) { |
| 900 | ssl->rwstate = SSL_X509_LOOKUP; |
| 901 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 902 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 903 | } |
| 904 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 905 | if (!ssl_auto_chain_if_needed(ssl)) { |
| 906 | return -1; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 907 | } |
| 908 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 909 | if (ssl3_protocol_version(ssl) >= TLS1_3_VERSION) { |
| 910 | /* Jump to the TLS 1.3 state machine. */ |
| 911 | hs->state = SSL_ST_TLS13; |
| 912 | hs->do_tls13_handshake = tls13_server_handshake; |
| 913 | return 1; |
| 914 | } |
| 915 | |
| 916 | SSL_CLIENT_HELLO client_hello; |
| 917 | if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg, |
| 918 | ssl->init_num)) { |
| 919 | return -1; |
| 920 | } |
| 921 | |
| 922 | /* Negotiate the cipher suite. This must be done after |cert_cb| so the |
| 923 | * certificate is finalized. */ |
| 924 | ssl->s3->tmp.new_cipher = |
| 925 | ssl3_choose_cipher(hs, &client_hello, ssl_get_cipher_preferences(ssl)); |
| 926 | if (ssl->s3->tmp.new_cipher == NULL) { |
| 927 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
| 928 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 929 | return -1; |
| 930 | } |
| 931 | |
| 932 | return 1; |
| 933 | } |
| 934 | |
| 935 | static int ssl3_select_parameters(SSL_HANDSHAKE *hs) { |
| 936 | SSL *const ssl = hs->ssl; |
| 937 | uint8_t al = SSL_AD_INTERNAL_ERROR; |
| 938 | int ret = -1; |
| 939 | SSL_SESSION *session = NULL; |
| 940 | |
| 941 | SSL_CLIENT_HELLO client_hello; |
| 942 | if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg, |
| 943 | ssl->init_num)) { |
| 944 | return -1; |
| 945 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 946 | |
| 947 | /* Determine whether we are doing session resumption. */ |
| 948 | int tickets_supported = 0, renew_ticket = 0; |
| 949 | switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket, |
| 950 | &client_hello)) { |
| 951 | case ssl_session_success: |
| 952 | break; |
| 953 | case ssl_session_error: |
| 954 | goto err; |
| 955 | case ssl_session_retry: |
| 956 | ssl->rwstate = SSL_PENDING_SESSION; |
| 957 | goto err; |
| 958 | } |
| 959 | |
| 960 | if (session != NULL) { |
| 961 | if (session->extended_master_secret && |
| 962 | !ssl->s3->tmp.extended_master_secret) { |
| 963 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 964 | * is fatal to the connection. */ |
| 965 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 966 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
| 967 | goto f_err; |
| 968 | } |
| 969 | |
| 970 | if (!ssl_session_is_resumable(ssl, session) || |
| 971 | /* If the client offers the EMS extension, but the previous session |
| 972 | * didn't use it, then negotiate a new session. */ |
| 973 | ssl->s3->tmp.extended_master_secret != |
| 974 | session->extended_master_secret) { |
| 975 | SSL_SESSION_free(session); |
| 976 | session = NULL; |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | if (session != NULL) { |
| 981 | /* Use the old session. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 982 | hs->ticket_expected = renew_ticket; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 983 | ssl->session = session; |
| 984 | session = NULL; |
| 985 | ssl->s3->session_reused = 1; |
| 986 | } else { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 987 | hs->ticket_expected = tickets_supported; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 988 | ssl_set_session(ssl, NULL); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 989 | if (!ssl_get_new_session(hs, 1 /* server */)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 990 | goto err; |
| 991 | } |
| 992 | |
| 993 | /* Clear the session ID if we want the session to be single-use. */ |
| 994 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
| 995 | ssl->s3->new_session->session_id_length = 0; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | if (ssl->ctx->dos_protection_cb != NULL && |
| 1000 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 1001 | /* Connection rejected for DOS reasons. */ |
| 1002 | al = SSL_AD_INTERNAL_ERROR; |
| 1003 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 1004 | goto f_err; |
| 1005 | } |
| 1006 | |
| 1007 | if (ssl->session == NULL) { |
| 1008 | ssl->s3->new_session->cipher = ssl->s3->tmp.new_cipher; |
| 1009 | |
| 1010 | /* On new sessions, stash the SNI value in the session. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1011 | if (hs->hostname != NULL) { |
Robert Sloan | 47f43ed | 2017-02-06 14:55:15 -0800 | [diff] [blame] | 1012 | OPENSSL_free(ssl->s3->new_session->tlsext_hostname); |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1013 | ssl->s3->new_session->tlsext_hostname = BUF_strdup(hs->hostname); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1014 | if (ssl->s3->new_session->tlsext_hostname == NULL) { |
| 1015 | al = SSL_AD_INTERNAL_ERROR; |
| 1016 | goto f_err; |
| 1017 | } |
| 1018 | } |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1019 | |
| 1020 | /* Determine whether to request a client certificate. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1021 | hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER); |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1022 | /* Only request a certificate if Channel ID isn't negotiated. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1023 | if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 1024 | ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1025 | hs->cert_request = 0; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1026 | } |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1027 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
| 1028 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1029 | hs->cert_request = 0; |
Adam Langley | f4e4272 | 2015-06-04 17:45:09 -0700 | [diff] [blame] | 1030 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1031 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1032 | if (!hs->cert_request) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1033 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
| 1034 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1035 | ssl->s3->new_session->verify_result = X509_V_OK; |
| 1036 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1037 | } |
| 1038 | |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1039 | /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 1040 | * deferred. Complete it now. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1041 | if (!ssl_negotiate_alpn(hs, &al, &client_hello)) { |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1042 | goto f_err; |
| 1043 | } |
| 1044 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1045 | /* Now that all parameters are known, initialize the handshake hash and hash |
| 1046 | * the ClientHello. */ |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1047 | if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl), |
| 1048 | ssl->s3->tmp.new_cipher->algorithm_prf) || |
| 1049 | !ssl_hash_current_message(hs)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1050 | goto f_err; |
| 1051 | } |
| 1052 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1053 | /* Release the handshake buffer if client authentication isn't required. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1054 | if (!hs->cert_request) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1055 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1056 | } |
| 1057 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1058 | ret = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1059 | |
| 1060 | if (0) { |
| 1061 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1062 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1063 | } |
| 1064 | |
| 1065 | err: |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1066 | SSL_SESSION_free(session); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1067 | return ret; |
| 1068 | } |
| 1069 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1070 | static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) { |
| 1071 | SSL *const ssl = hs->ssl; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1072 | |
| 1073 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 1074 | * known attack while we fix ChannelID itself. */ |
| 1075 | if (ssl->s3->tlsext_channel_id_valid && |
| 1076 | (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 1077 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1078 | } |
| 1079 | |
| 1080 | /* If this is a resumption and the original handshake didn't support |
| 1081 | * ChannelID then we didn't record the original handshake hashes in the |
| 1082 | * session and so cannot resume with ChannelIDs. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1083 | if (ssl->session != NULL && |
| 1084 | ssl->session->original_handshake_hash_len == 0) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1085 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1086 | } |
| 1087 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1088 | struct timeval now; |
| 1089 | ssl_get_current_time(ssl, &now); |
| 1090 | ssl->s3->server_random[0] = now.tv_sec >> 24; |
| 1091 | ssl->s3->server_random[1] = now.tv_sec >> 16; |
| 1092 | ssl->s3->server_random[2] = now.tv_sec >> 8; |
| 1093 | ssl->s3->server_random[3] = now.tv_sec; |
| 1094 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1095 | return -1; |
| 1096 | } |
| 1097 | |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1098 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 1099 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1100 | |
| 1101 | const SSL_SESSION *session = ssl->s3->new_session; |
| 1102 | if (ssl->session != NULL) { |
| 1103 | session = ssl->session; |
| 1104 | } |
| 1105 | |
| 1106 | CBB cbb, body, session_id; |
| 1107 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) || |
| 1108 | !CBB_add_u16(&body, ssl->version) || |
| 1109 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1110 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
| 1111 | !CBB_add_bytes(&session_id, session->session_id, |
| 1112 | session->session_id_length) || |
| 1113 | !CBB_add_u16(&body, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) || |
| 1114 | !CBB_add_u8(&body, 0 /* no compression */) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1115 | !ssl_add_serverhello_tlsext(hs, &body) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1116 | !ssl_add_message_cbb(ssl, &cbb)) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1117 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1118 | CBB_cleanup(&cbb); |
| 1119 | return -1; |
| 1120 | } |
| 1121 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1122 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1123 | } |
| 1124 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1125 | static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) { |
| 1126 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1127 | if (!ssl_has_certificate(ssl)) { |
| 1128 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1129 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1130 | } |
| 1131 | |
| 1132 | if (!ssl3_output_cert_chain(ssl)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1133 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1134 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1135 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1136 | } |
| 1137 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1138 | static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) { |
| 1139 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1140 | CBB cbb, body, ocsp_response; |
| 1141 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1142 | SSL3_MT_CERTIFICATE_STATUS) || |
| 1143 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 1144 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1145 | !CBB_add_bytes(&ocsp_response, CRYPTO_BUFFER_data(ssl->ocsp_response), |
| 1146 | CRYPTO_BUFFER_len(ssl->ocsp_response)) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1147 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1148 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1149 | CBB_cleanup(&cbb); |
| 1150 | return -1; |
| 1151 | } |
| 1152 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1153 | return 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1156 | static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) { |
| 1157 | SSL *const ssl = hs->ssl; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1158 | CBB cbb, child; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1159 | CBB_zero(&cbb); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1160 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1161 | /* Put together the parameters. */ |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1162 | if (hs->state == SSL3_ST_SW_KEY_EXCH_A) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1163 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1164 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1165 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1166 | /* Pre-allocate enough room to comfortably fit an ECDHE public key. */ |
| 1167 | if (!CBB_init(&cbb, 128)) { |
| 1168 | goto err; |
| 1169 | } |
| 1170 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1171 | /* PSK ciphers begin with an identity hint. */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1172 | if (alg_a & SSL_aPSK) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1173 | size_t len = |
| 1174 | (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint); |
| 1175 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1176 | !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint, |
| 1177 | len)) { |
| 1178 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1179 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1180 | } |
| 1181 | |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1182 | if (alg_k & SSL_kDHE) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1183 | /* Determine the group to use. */ |
| 1184 | DH *params = ssl->cert->dh_tmp; |
| 1185 | if (params == NULL && ssl->cert->dh_tmp_cb != NULL) { |
| 1186 | params = ssl->cert->dh_tmp_cb(ssl, 0, 1024); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1187 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1188 | if (params == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1189 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1190 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1191 | goto err; |
| 1192 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1193 | |
| 1194 | /* Set up DH, generate a key, and emit the public half. */ |
| 1195 | DH *dh = DHparams_dup(params); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1196 | if (dh == NULL) { |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1197 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1198 | } |
| 1199 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1200 | SSL_ECDH_CTX_init_for_dhe(&hs->ecdh_ctx, dh); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1201 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1202 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) || |
| 1203 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1204 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) || |
| 1205 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1206 | !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1207 | goto err; |
| 1208 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1209 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1210 | /* Determine the group to use. */ |
| 1211 | uint16_t group_id; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1212 | if (!tls1_get_shared_group(hs, &group_id)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1213 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1214 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1215 | goto err; |
| 1216 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1217 | ssl->s3->new_session->group_id = group_id; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1218 | |
| 1219 | /* Set up ECDH, generate a key, and emit the public half. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1220 | if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1221 | !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1222 | !CBB_add_u16(&cbb, group_id) || |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1223 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1224 | !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1225 | goto err; |
| 1226 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1227 | } else { |
| 1228 | assert(alg_k & SSL_kPSK); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1229 | } |
| 1230 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1231 | if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1232 | goto err; |
| 1233 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1234 | } |
| 1235 | |
| 1236 | /* Assemble the message. */ |
| 1237 | CBB body; |
| 1238 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1239 | SSL3_MT_SERVER_KEY_EXCHANGE) || |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1240 | !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1241 | goto err; |
| 1242 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1243 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1244 | /* Add a signature. */ |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1245 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1246 | if (!ssl_has_private_key(ssl)) { |
| 1247 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1248 | goto err; |
| 1249 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1250 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1251 | /* Determine the signature algorithm. */ |
| 1252 | uint16_t signature_algorithm; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1253 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1254 | goto err; |
| 1255 | } |
| 1256 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1257 | if (!CBB_add_u16(&body, signature_algorithm)) { |
| 1258 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1259 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1260 | goto err; |
| 1261 | } |
| 1262 | } |
| 1263 | |
| 1264 | /* Add space for the signature. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1265 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1266 | uint8_t *ptr; |
| 1267 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
| 1268 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1269 | goto err; |
| 1270 | } |
| 1271 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1272 | size_t sig_len; |
| 1273 | enum ssl_private_key_result_t sign_result; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1274 | if (hs->state == SSL3_ST_SW_KEY_EXCH_A) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1275 | CBB transcript; |
| 1276 | uint8_t *transcript_data; |
| 1277 | size_t transcript_len; |
| 1278 | if (!CBB_init(&transcript, |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1279 | 2 * SSL3_RANDOM_SIZE + hs->server_params_len) || |
| 1280 | !CBB_add_bytes(&transcript, ssl->s3->client_random, |
| 1281 | SSL3_RANDOM_SIZE) || |
| 1282 | !CBB_add_bytes(&transcript, ssl->s3->server_random, |
| 1283 | SSL3_RANDOM_SIZE) || |
| 1284 | !CBB_add_bytes(&transcript, hs->server_params, |
| 1285 | hs->server_params_len) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1286 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1287 | CBB_cleanup(&transcript); |
| 1288 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1289 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1290 | goto err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1291 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1292 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1293 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, |
| 1294 | signature_algorithm, transcript_data, |
| 1295 | transcript_len); |
| 1296 | OPENSSL_free(transcript_data); |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1297 | } else { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1298 | assert(hs->state == SSL3_ST_SW_KEY_EXCH_B); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1299 | sign_result = ssl_private_key_complete(ssl, ptr, &sig_len, max_sig_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1300 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1301 | |
| 1302 | switch (sign_result) { |
| 1303 | case ssl_private_key_success: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1304 | if (!CBB_did_write(&child, sig_len)) { |
| 1305 | goto err; |
| 1306 | } |
| 1307 | break; |
| 1308 | case ssl_private_key_failure: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1309 | goto err; |
| 1310 | case ssl_private_key_retry: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1311 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1312 | hs->state = SSL3_ST_SW_KEY_EXCH_B; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1313 | goto err; |
| 1314 | } |
Adam Langley | 1e4884f | 2015-09-24 10:57:52 -0700 | [diff] [blame] | 1315 | } |
| 1316 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1317 | if (!ssl_add_message_cbb(ssl, &cbb)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1318 | goto err; |
| 1319 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1320 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1321 | OPENSSL_free(hs->server_params); |
| 1322 | hs->server_params = NULL; |
| 1323 | hs->server_params_len = 0; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1324 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1325 | return 1; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1326 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1327 | err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1328 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1329 | return -1; |
| 1330 | } |
| 1331 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1332 | static int add_cert_types(SSL *ssl, CBB *cbb) { |
| 1333 | /* Get configured signature algorithms. */ |
| 1334 | int have_rsa_sign = 0; |
| 1335 | int have_ecdsa_sign = 0; |
| 1336 | const uint16_t *sig_algs; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1337 | size_t num_sig_algs = tls12_get_verify_sigalgs(ssl, &sig_algs); |
| 1338 | for (size_t i = 0; i < num_sig_algs; i++) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1339 | switch (sig_algs[i]) { |
| 1340 | case SSL_SIGN_RSA_PKCS1_SHA512: |
| 1341 | case SSL_SIGN_RSA_PKCS1_SHA384: |
| 1342 | case SSL_SIGN_RSA_PKCS1_SHA256: |
| 1343 | case SSL_SIGN_RSA_PKCS1_SHA1: |
| 1344 | have_rsa_sign = 1; |
| 1345 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1346 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1347 | case SSL_SIGN_ECDSA_SECP521R1_SHA512: |
| 1348 | case SSL_SIGN_ECDSA_SECP384R1_SHA384: |
| 1349 | case SSL_SIGN_ECDSA_SECP256R1_SHA256: |
| 1350 | case SSL_SIGN_ECDSA_SHA1: |
| 1351 | have_ecdsa_sign = 1; |
| 1352 | break; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1353 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1354 | } |
| 1355 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1356 | if (have_rsa_sign && !CBB_add_u8(cbb, SSL3_CT_RSA_SIGN)) { |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | /* ECDSA certs can be used with RSA cipher suites as well so we don't need to |
| 1361 | * check for SSL_kECDH or SSL_kECDHE. */ |
| 1362 | if (ssl->version >= TLS1_VERSION && have_ecdsa_sign && |
| 1363 | !CBB_add_u8(cbb, TLS_CT_ECDSA_SIGN)) { |
| 1364 | return 0; |
| 1365 | } |
| 1366 | |
| 1367 | return 1; |
| 1368 | } |
| 1369 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1370 | static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) { |
| 1371 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1372 | CBB cbb, body, cert_types, sigalgs_cbb; |
| 1373 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1374 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 1375 | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
| 1376 | !add_cert_types(ssl, &cert_types)) { |
| 1377 | goto err; |
| 1378 | } |
| 1379 | |
| 1380 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
| 1381 | const uint16_t *sigalgs; |
David Benjamin | 95add82 | 2016-10-19 01:09:12 -0400 | [diff] [blame] | 1382 | size_t num_sigalgs = tls12_get_verify_sigalgs(ssl, &sigalgs); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1383 | if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb)) { |
| 1384 | goto err; |
| 1385 | } |
| 1386 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1387 | for (size_t i = 0; i < num_sigalgs; i++) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1388 | if (!CBB_add_u16(&sigalgs_cbb, sigalgs[i])) { |
| 1389 | goto err; |
| 1390 | } |
| 1391 | } |
| 1392 | } |
| 1393 | |
| 1394 | if (!ssl_add_client_CA_list(ssl, &body) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1395 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1396 | goto err; |
| 1397 | } |
| 1398 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1399 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1400 | |
| 1401 | err: |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1402 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1403 | CBB_cleanup(&cbb); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1404 | return -1; |
| 1405 | } |
| 1406 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1407 | static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) { |
| 1408 | SSL *const ssl = hs->ssl; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1409 | CBB cbb, body; |
| 1410 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO_DONE) || |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1411 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1412 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1413 | CBB_cleanup(&cbb); |
| 1414 | return -1; |
| 1415 | } |
| 1416 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1417 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1418 | } |
| 1419 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1420 | static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) { |
| 1421 | SSL *const ssl = hs->ssl; |
| 1422 | assert(hs->cert_request); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1423 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1424 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1425 | if (msg_ret <= 0) { |
| 1426 | return msg_ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1427 | } |
| 1428 | |
| 1429 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 1430 | if (ssl->version == SSL3_VERSION && |
| 1431 | ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1432 | /* In SSL 3.0, the Certificate message is omitted to signal no |
| 1433 | * certificate. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1434 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1435 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1436 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1437 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1438 | } |
| 1439 | |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1440 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1441 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1442 | ssl->s3->new_session->verify_result = X509_V_OK; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1443 | ssl->s3->tmp.reuse_message = 1; |
| 1444 | return 1; |
| 1445 | } |
| 1446 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1447 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1448 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1449 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1450 | } |
| 1451 | |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1452 | if (!ssl_hash_current_message(hs)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1453 | return -1; |
| 1454 | } |
| 1455 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1456 | CBS certificate_msg; |
| 1457 | CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1458 | |
| 1459 | sk_CRYPTO_BUFFER_pop_free(ssl->s3->new_session->certs, CRYPTO_BUFFER_free); |
| 1460 | EVP_PKEY_free(hs->peer_pubkey); |
| 1461 | hs->peer_pubkey = NULL; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1462 | uint8_t alert = SSL_AD_DECODE_ERROR; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1463 | ssl->s3->new_session->certs = |
| 1464 | ssl_parse_cert_chain(&alert, &hs->peer_pubkey, |
| 1465 | ssl->retain_only_sha256_of_client_certs |
| 1466 | ? ssl->s3->new_session->peer_sha256 |
| 1467 | : NULL, |
| 1468 | &certificate_msg, ssl->ctx->pool); |
| 1469 | if (ssl->s3->new_session->certs == NULL) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1470 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1471 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1472 | } |
| 1473 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1474 | if (CBS_len(&certificate_msg) != 0 || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1475 | !ssl->ctx->x509_method->session_cache_objects(ssl->s3->new_session)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1476 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1477 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1478 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1479 | } |
| 1480 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1481 | if (sk_CRYPTO_BUFFER_num(ssl->s3->new_session->certs) == 0) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1482 | /* No client certificate so the handshake buffer may be discarded. */ |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1483 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1484 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1485 | /* In SSL 3.0, sending no certificate is signaled by omitting the |
| 1486 | * Certificate message. */ |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1487 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1488 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1489 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1490 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1494 | /* Fail for TLS only if we required a certificate */ |
| 1495 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1496 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1497 | return -1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1498 | } |
David Benjamin | f0c4a6c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1499 | |
| 1500 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
| 1501 | * classed by them as a bug, but it's assumed by at least NGINX. */ |
| 1502 | ssl->s3->new_session->verify_result = X509_V_OK; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1503 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1504 | } |
| 1505 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1506 | /* The hash will have been filled in. */ |
| 1507 | if (ssl->retain_only_sha256_of_client_certs) { |
| 1508 | ssl->s3->new_session->peer_sha256_valid = 1; |
| 1509 | } |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1510 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1511 | if (!ssl_verify_cert_chain(ssl, &ssl->s3->new_session->verify_result, |
| 1512 | ssl->s3->new_session->x509_chain)) { |
| 1513 | return -1; |
| 1514 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1515 | return 1; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1518 | static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) { |
| 1519 | SSL *const ssl = hs->ssl; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1520 | int al; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1521 | CBS client_key_exchange; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1522 | uint32_t alg_k; |
| 1523 | uint32_t alg_a; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1524 | uint8_t *premaster_secret = NULL; |
| 1525 | size_t premaster_secret_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1526 | uint8_t *decrypt_buf = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1527 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1528 | unsigned psk_len = 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1529 | uint8_t psk[PSK_MAX_PSK_LEN]; |
| 1530 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1531 | if (hs->state == SSL3_ST_SR_KEY_EXCH_A) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1532 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1533 | if (ret <= 0) { |
| 1534 | return ret; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1535 | } |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1536 | |
| 1537 | if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1538 | !ssl_hash_current_message(hs)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1539 | return -1; |
| 1540 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1541 | } |
| 1542 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1543 | CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num); |
| 1544 | alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1545 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1546 | |
| 1547 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1548 | if (alg_a & SSL_aPSK) { |
| 1549 | CBS psk_identity; |
| 1550 | |
| 1551 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1552 | * then this is the only field in the message. */ |
| 1553 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1554 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1555 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1556 | al = SSL_AD_DECODE_ERROR; |
| 1557 | goto f_err; |
| 1558 | } |
| 1559 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1560 | if (ssl->psk_server_callback == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1561 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1562 | al = SSL_AD_INTERNAL_ERROR; |
| 1563 | goto f_err; |
| 1564 | } |
| 1565 | |
| 1566 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1567 | CBS_contains_zero_byte(&psk_identity)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1568 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1569 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1570 | goto f_err; |
| 1571 | } |
| 1572 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1573 | if (!CBS_strdup(&psk_identity, &ssl->s3->new_session->psk_identity)) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1574 | al = SSL_AD_INTERNAL_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1575 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1576 | goto f_err; |
| 1577 | } |
| 1578 | |
| 1579 | /* Look up the key for the identity. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1580 | psk_len = ssl->psk_server_callback(ssl, ssl->s3->new_session->psk_identity, |
| 1581 | psk, sizeof(psk)); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1582 | if (psk_len > PSK_MAX_PSK_LEN) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1583 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1584 | al = SSL_AD_INTERNAL_ERROR; |
| 1585 | goto f_err; |
| 1586 | } else if (psk_len == 0) { |
| 1587 | /* PSK related to the given identity not found */ |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1588 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1589 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1590 | goto f_err; |
| 1591 | } |
| 1592 | } |
| 1593 | |
| 1594 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1595 | * |premaster_secret_len|. */ |
| 1596 | if (alg_k & SSL_kRSA) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1597 | /* Allocate a buffer large enough for an RSA decryption. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1598 | const size_t rsa_size = ssl_private_key_max_signature_len(ssl); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1599 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1600 | if (decrypt_buf == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1601 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1602 | goto err; |
| 1603 | } |
| 1604 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1605 | enum ssl_private_key_result_t decrypt_result; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1606 | size_t decrypt_len; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1607 | if (hs->state == SSL3_ST_SR_KEY_EXCH_A) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1608 | if (!ssl_has_private_key(ssl) || |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1609 | ssl_private_key_type(ssl) != NID_rsaEncryption) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1610 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1611 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1612 | goto f_err; |
| 1613 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1614 | CBS encrypted_premaster_secret; |
| 1615 | if (ssl->version > SSL3_VERSION) { |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1616 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1617 | &encrypted_premaster_secret) || |
| 1618 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1619 | al = SSL_AD_DECODE_ERROR; |
| 1620 | OPENSSL_PUT_ERROR(SSL, |
| 1621 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1622 | goto f_err; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1623 | } |
| 1624 | } else { |
| 1625 | encrypted_premaster_secret = client_key_exchange; |
| 1626 | } |
| 1627 | |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1628 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1629 | * timing-sensitive code below. */ |
| 1630 | decrypt_result = ssl_private_key_decrypt( |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1631 | ssl, decrypt_buf, &decrypt_len, rsa_size, |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1632 | CBS_data(&encrypted_premaster_secret), |
| 1633 | CBS_len(&encrypted_premaster_secret)); |
| 1634 | } else { |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1635 | assert(hs->state == SSL3_ST_SR_KEY_EXCH_B); |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1636 | /* Complete async decrypt. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1637 | decrypt_result = |
| 1638 | ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1639 | } |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1640 | |
| 1641 | switch (decrypt_result) { |
| 1642 | case ssl_private_key_success: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1643 | break; |
| 1644 | case ssl_private_key_failure: |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1645 | goto err; |
| 1646 | case ssl_private_key_retry: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1647 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1648 | hs->state = SSL3_ST_SR_KEY_EXCH_B; |
Kenny Root | e99801b | 2015-11-06 15:31:15 -0800 | [diff] [blame] | 1649 | goto err; |
| 1650 | } |
| 1651 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1652 | if (decrypt_len != rsa_size) { |
| 1653 | al = SSL_AD_DECRYPT_ERROR; |
| 1654 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1655 | goto f_err; |
| 1656 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1657 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1658 | /* Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1659 | * section 7.4.7.1. */ |
| 1660 | premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1661 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1662 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1663 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1664 | goto err; |
| 1665 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1666 | if (!RAND_bytes(premaster_secret, premaster_secret_len)) { |
| 1667 | goto err; |
| 1668 | } |
| 1669 | |
| 1670 | /* The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1671 | * publicly invalid. */ |
| 1672 | if (decrypt_len < 11 + premaster_secret_len) { |
| 1673 | al = SSL_AD_DECRYPT_ERROR; |
| 1674 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1675 | goto f_err; |
| 1676 | } |
| 1677 | |
| 1678 | /* Check the padding. See RFC 3447, section 7.2.2. */ |
| 1679 | size_t padding_len = decrypt_len - premaster_secret_len; |
| 1680 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1681 | constant_time_eq_int_8(decrypt_buf[1], 2); |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1682 | for (size_t i = 2; i < padding_len - 1; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1683 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1684 | } |
| 1685 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1686 | |
| 1687 | /* The premaster secret must begin with |client_version|. This too must be |
| 1688 | * checked in constant time (http://eprint.iacr.org/2003/052/). */ |
| 1689 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1690 | (unsigned)(hs->client_version >> 8)); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1691 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1692 | (unsigned)(hs->client_version & 0xff)); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1693 | |
| 1694 | /* Select, in constant time, either the decrypted premaster or the random |
| 1695 | * premaster based on |good|. */ |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1696 | for (size_t i = 0; i < premaster_secret_len; i++) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1697 | premaster_secret[i] = constant_time_select_8( |
| 1698 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1699 | } |
| 1700 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1701 | OPENSSL_free(decrypt_buf); |
| 1702 | decrypt_buf = NULL; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1703 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE)) { |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1704 | /* Parse the ClientKeyExchange. */ |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1705 | CBS peer_key; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1706 | if (!SSL_ECDH_CTX_get_key(&hs->ecdh_ctx, &client_key_exchange, &peer_key) || |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1707 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1708 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1709 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1710 | goto f_err; |
| 1711 | } |
| 1712 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1713 | /* Compute the premaster. */ |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1714 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1715 | if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret, |
David Benjamin | d316cba | 2016-06-02 16:17:39 -0400 | [diff] [blame] | 1716 | &premaster_secret_len, &alert, CBS_data(&peer_key), |
| 1717 | CBS_len(&peer_key))) { |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1718 | al = alert; |
| 1719 | goto f_err; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1720 | } |
| 1721 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1722 | /* The key exchange state may now be discarded. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1723 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1724 | } else if (alg_k & SSL_kPSK) { |
| 1725 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1726 | * pre-shared key. */ |
| 1727 | premaster_secret_len = psk_len; |
| 1728 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1729 | if (premaster_secret == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1730 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1731 | goto err; |
| 1732 | } |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 1733 | OPENSSL_memset(premaster_secret, 0, premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1734 | } else { |
| 1735 | al = SSL_AD_HANDSHAKE_FAILURE; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1736 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1737 | goto f_err; |
| 1738 | } |
| 1739 | |
| 1740 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1741 | * pre-shared key. */ |
| 1742 | if (alg_a & SSL_aPSK) { |
| 1743 | CBB new_premaster, child; |
| 1744 | uint8_t *new_data; |
| 1745 | size_t new_len; |
| 1746 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1747 | CBB_zero(&new_premaster); |
| 1748 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1749 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1750 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1751 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1752 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1753 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1754 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1755 | CBB_cleanup(&new_premaster); |
| 1756 | goto err; |
| 1757 | } |
| 1758 | |
| 1759 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1760 | OPENSSL_free(premaster_secret); |
| 1761 | premaster_secret = new_data; |
| 1762 | premaster_secret_len = new_len; |
| 1763 | } |
| 1764 | |
| 1765 | /* Compute the master secret */ |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1766 | ssl->s3->new_session->master_key_length = |
| 1767 | tls1_generate_master_secret(hs, ssl->s3->new_session->master_key, |
| 1768 | premaster_secret, premaster_secret_len); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1769 | if (ssl->s3->new_session->master_key_length == 0) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1770 | goto err; |
| 1771 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1772 | ssl->s3->new_session->extended_master_secret = |
| 1773 | ssl->s3->tmp.extended_master_secret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1774 | |
| 1775 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1776 | OPENSSL_free(premaster_secret); |
| 1777 | return 1; |
| 1778 | |
| 1779 | f_err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1780 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1781 | err: |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 1782 | if (premaster_secret != NULL) { |
| 1783 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1784 | OPENSSL_free(premaster_secret); |
| 1785 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1786 | OPENSSL_free(decrypt_buf); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1787 | |
| 1788 | return -1; |
| 1789 | } |
| 1790 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1791 | static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) { |
| 1792 | SSL *const ssl = hs->ssl; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1793 | int al; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1794 | CBS certificate_verify, signature; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1795 | |
| 1796 | /* Only RSA and ECDSA client certificates are supported, so a |
| 1797 | * CertificateVerify is required if and only if there's a client certificate. |
| 1798 | * */ |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1799 | if (hs->peer_pubkey == NULL) { |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1800 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1801 | return 1; |
| 1802 | } |
| 1803 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1804 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1805 | if (msg_ret <= 0) { |
| 1806 | return msg_ret; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1807 | } |
| 1808 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1809 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) { |
| 1810 | return -1; |
| 1811 | } |
| 1812 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1813 | CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1814 | |
| 1815 | /* Determine the digest type if needbe. */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1816 | uint16_t signature_algorithm = 0; |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1817 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1818 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1819 | al = SSL_AD_DECODE_ERROR; |
| 1820 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1821 | goto f_err; |
| 1822 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1823 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
Adam Langley | fad6327 | 2015-11-12 12:15:39 -0800 | [diff] [blame] | 1824 | goto f_err; |
| 1825 | } |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1826 | ssl->s3->new_session->peer_signature_algorithm = signature_algorithm; |
| 1827 | } else if (hs->peer_pubkey->type == EVP_PKEY_RSA) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1828 | signature_algorithm = SSL_SIGN_RSA_PKCS1_MD5_SHA1; |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1829 | } else if (hs->peer_pubkey->type == EVP_PKEY_EC) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1830 | signature_algorithm = SSL_SIGN_ECDSA_SHA1; |
| 1831 | } else { |
| 1832 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1833 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1834 | goto f_err; |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1835 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1836 | |
| 1837 | /* Parse and verify the signature. */ |
| 1838 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1839 | CBS_len(&certificate_verify) != 0) { |
| 1840 | al = SSL_AD_DECODE_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1841 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1842 | goto f_err; |
| 1843 | } |
| 1844 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1845 | int sig_ok; |
| 1846 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1847 | * single final digest and signature, and must be special-cased. */ |
| 1848 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1849 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1850 | size_t digest_len; |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1851 | if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest, |
| 1852 | &digest_len, ssl->s3->new_session, |
| 1853 | signature_algorithm)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1854 | goto err; |
| 1855 | } |
| 1856 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1857 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1858 | sig_ok = pctx != NULL && |
| 1859 | EVP_PKEY_verify_init(pctx) && |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1860 | EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 1861 | digest, digest_len); |
| 1862 | EVP_PKEY_CTX_free(pctx); |
| 1863 | } else { |
| 1864 | sig_ok = ssl_public_key_verify( |
| 1865 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1866 | hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data, |
| 1867 | hs->transcript.buffer->length); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1868 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1869 | |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 1870 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1871 | sig_ok = 1; |
| 1872 | ERR_clear_error(); |
| 1873 | #endif |
| 1874 | if (!sig_ok) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1875 | al = SSL_AD_DECRYPT_ERROR; |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 1876 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1877 | goto f_err; |
| 1878 | } |
| 1879 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1880 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 1881 | * message.*/ |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1882 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
| 1883 | if (!ssl_hash_current_message(hs)) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1884 | goto err; |
| 1885 | } |
| 1886 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1887 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1888 | |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1889 | f_err: |
| 1890 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1891 | err: |
Steven Valdez | e7531f0 | 2016-12-14 13:29:57 -0500 | [diff] [blame] | 1892 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1893 | } |
| 1894 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1895 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 1896 | * sets the next_proto member in s if found */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1897 | static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) { |
| 1898 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1899 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1900 | if (ret <= 0) { |
| 1901 | return ret; |
| 1902 | } |
| 1903 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1904 | if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1905 | !ssl_hash_current_message(hs)) { |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1906 | return -1; |
| 1907 | } |
| 1908 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1909 | CBS next_protocol, selected_protocol, padding; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1910 | CBS_init(&next_protocol, ssl->init_msg, ssl->init_num); |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1911 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1912 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
David Benjamin | 7c0d06c | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1913 | CBS_len(&next_protocol) != 0) { |
| 1914 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1915 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated, |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1920 | &ssl->s3->next_proto_negotiated_len)) { |
| 1921 | return 0; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1924 | return 1; |
| 1925 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1926 | |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1927 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1928 | static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) { |
| 1929 | SSL *const ssl = hs->ssl; |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1930 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1931 | if (msg_ret <= 0) { |
| 1932 | return msg_ret; |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1933 | } |
| 1934 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1935 | if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) || |
Robert Sloan | 5d62578 | 2017-02-13 09:55:39 -0800 | [diff] [blame^] | 1936 | !tls1_verify_channel_id(hs) || |
| 1937 | !ssl_hash_current_message(hs)) { |
David Benjamin | 6e899c7 | 2016-06-09 18:02:18 -0400 | [diff] [blame] | 1938 | return -1; |
| 1939 | } |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1940 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1941 | } |
| 1942 | |
David Benjamin | 1b24967 | 2016-12-06 18:25:50 -0500 | [diff] [blame] | 1943 | static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) { |
| 1944 | SSL *const ssl = hs->ssl; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1945 | const SSL_SESSION *session; |
| 1946 | SSL_SESSION *session_copy = NULL; |
| 1947 | if (ssl->session == NULL) { |
| 1948 | /* Fix the timeout to measure from the ticket issuance time. */ |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1949 | ssl_session_rebase_time(ssl, ssl->s3->new_session); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1950 | session = ssl->s3->new_session; |
| 1951 | } else { |
| 1952 | /* We are renewing an existing session. Duplicate the session to adjust the |
| 1953 | * timeout. */ |
| 1954 | session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
| 1955 | if (session_copy == NULL) { |
| 1956 | return -1; |
| 1957 | } |
| 1958 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1959 | ssl_session_rebase_time(ssl, session_copy); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1960 | session = session_copy; |
| 1961 | } |
| 1962 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1963 | CBB cbb, body, ticket; |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1964 | int ok = |
| 1965 | ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) && |
| 1966 | CBB_add_u32(&body, session->timeout) && |
| 1967 | CBB_add_u16_length_prefixed(&body, &ticket) && |
| 1968 | ssl_encrypt_ticket(ssl, &ticket, session) && |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1969 | ssl_add_message_cbb(ssl, &cbb); |
Steven Valdez | 909b19f | 2016-11-21 15:35:44 -0500 | [diff] [blame] | 1970 | |
| 1971 | SSL_SESSION_free(session_copy); |
| 1972 | CBB_cleanup(&cbb); |
| 1973 | |
| 1974 | if (!ok) { |
| 1975 | return -1; |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 1976 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 1977 | |
Robert Sloan | 4d1ac50 | 2017-02-06 08:36:14 -0800 | [diff] [blame] | 1978 | return 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 1979 | } |