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