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; |
| 906 | uint8_t al = SSL_AD_INTERNAL_ERROR; |
| 907 | int ret = -1; |
| 908 | SSL_SESSION *session = NULL; |
| 909 | |
| 910 | SSL_CLIENT_HELLO client_hello; |
| 911 | if (!ssl_client_hello_init(ssl, &client_hello, ssl->init_msg, |
| 912 | ssl->init_num)) { |
| 913 | return -1; |
| 914 | } |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 915 | |
| 916 | /* Determine whether we are doing session resumption. */ |
| 917 | int tickets_supported = 0, renew_ticket = 0; |
| 918 | switch (ssl_get_prev_session(ssl, &session, &tickets_supported, &renew_ticket, |
| 919 | &client_hello)) { |
| 920 | case ssl_session_success: |
| 921 | break; |
| 922 | case ssl_session_error: |
| 923 | goto err; |
| 924 | case ssl_session_retry: |
| 925 | ssl->rwstate = SSL_PENDING_SESSION; |
| 926 | goto err; |
Adam Langley | 4c341d0 | 2017-03-08 19:33:21 -0800 | [diff] [blame] | 927 | case ssl_session_ticket_retry: |
| 928 | ssl->rwstate = SSL_PENDING_TICKET; |
| 929 | goto err; |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | if (session != NULL) { |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 933 | if (session->extended_master_secret && !hs->extended_master_secret) { |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 934 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 935 | * is fatal to the connection. */ |
| 936 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 937 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
| 938 | goto f_err; |
| 939 | } |
| 940 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 941 | if (!ssl_session_is_resumable(hs, session) || |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 942 | /* If the client offers the EMS extension, but the previous session |
| 943 | * didn't use it, then negotiate a new session. */ |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 944 | hs->extended_master_secret != session->extended_master_secret) { |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 945 | SSL_SESSION_free(session); |
| 946 | session = NULL; |
| 947 | } |
| 948 | } |
| 949 | |
| 950 | if (session != NULL) { |
| 951 | /* Use the old session. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 952 | hs->ticket_expected = renew_ticket; |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 953 | ssl->session = session; |
| 954 | session = NULL; |
| 955 | ssl->s3->session_reused = 1; |
| 956 | } else { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 957 | hs->ticket_expected = tickets_supported; |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 958 | ssl_set_session(ssl, NULL); |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 959 | if (!ssl_get_new_session(hs, 1 /* server */)) { |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 960 | goto err; |
| 961 | } |
| 962 | |
| 963 | /* Clear the session ID if we want the session to be single-use. */ |
| 964 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 965 | hs->new_session->session_id_length = 0; |
David Benjamin | 34202b9 | 2016-11-16 19:07:53 +0900 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
| 969 | if (ssl->ctx->dos_protection_cb != NULL && |
| 970 | ssl->ctx->dos_protection_cb(&client_hello) == 0) { |
| 971 | /* Connection rejected for DOS reasons. */ |
| 972 | al = SSL_AD_INTERNAL_ERROR; |
| 973 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
| 974 | goto f_err; |
| 975 | } |
| 976 | |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 977 | if (ssl->session == NULL) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 978 | hs->new_session->cipher = hs->new_cipher; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 979 | |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 980 | /* On new sessions, stash the SNI value in the session. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 981 | if (hs->hostname != NULL) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 982 | OPENSSL_free(hs->new_session->tlsext_hostname); |
| 983 | hs->new_session->tlsext_hostname = BUF_strdup(hs->hostname); |
| 984 | if (hs->new_session->tlsext_hostname == NULL) { |
David Benjamin | 4eb95cc | 2016-11-16 17:08:23 +0900 | [diff] [blame] | 985 | al = SSL_AD_INTERNAL_ERROR; |
| 986 | goto f_err; |
| 987 | } |
| 988 | } |
| 989 | |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 990 | /* Determine whether to request a client certificate. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 991 | hs->cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER); |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 992 | /* Only request a certificate if Channel ID isn't negotiated. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 993 | if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 994 | ssl->s3->tlsext_channel_id_valid) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 995 | hs->cert_request = 0; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 996 | } |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 997 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 998 | if (!ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 999 | hs->cert_request = 0; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 1000 | } |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1001 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1002 | if (!hs->cert_request) { |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1003 | /* OpenSSL returns X509_V_OK when no certificates are requested. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 1004 | * 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] | 1005 | hs->new_session->verify_result = X509_V_OK; |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1006 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1007 | } |
| 1008 | |
David Benjamin | f01f42a | 2016-11-16 19:05:33 +0900 | [diff] [blame] | 1009 | /* HTTP/2 negotiation depends on the cipher suite, so ALPN negotiation was |
| 1010 | * deferred. Complete it now. */ |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1011 | if (!ssl_negotiate_alpn(hs, &al, &client_hello)) { |
David Benjamin | 9ef31f0 | 2016-10-31 18:01:13 -0400 | [diff] [blame] | 1012 | goto f_err; |
| 1013 | } |
| 1014 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1015 | /* Now that all parameters are known, initialize the handshake hash and hash |
| 1016 | * the ClientHello. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1017 | if (!SSL_TRANSCRIPT_init_hash(&hs->transcript, ssl3_protocol_version(ssl), |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1018 | hs->new_cipher->algorithm_prf) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1019 | !ssl_hash_current_message(hs)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1020 | goto f_err; |
| 1021 | } |
| 1022 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1023 | /* Release the handshake buffer if client authentication isn't required. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1024 | if (!hs->cert_request) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1025 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1026 | } |
| 1027 | |
David Benjamin | 0bd71eb | 2015-12-01 14:14:40 -0500 | [diff] [blame] | 1028 | ret = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1029 | |
| 1030 | if (0) { |
| 1031 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1032 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1035 | err: |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 1036 | SSL_SESSION_free(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1037 | return ret; |
| 1038 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1039 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1040 | static int ssl3_send_server_hello(SSL_HANDSHAKE *hs) { |
| 1041 | SSL *const ssl = hs->ssl; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1042 | |
| 1043 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 1044 | * known attack while we fix ChannelID itself. */ |
| 1045 | if (ssl->s3->tlsext_channel_id_valid && |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1046 | (hs->new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1047 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1048 | } |
| 1049 | |
| 1050 | /* If this is a resumption and the original handshake didn't support |
| 1051 | * ChannelID then we didn't record the original handshake hashes in the |
| 1052 | * session and so cannot resume with ChannelIDs. */ |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1053 | if (ssl->session != NULL && |
| 1054 | ssl->session->original_handshake_hash_len == 0) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1055 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1056 | } |
| 1057 | |
David Benjamin | ad8f5e1 | 2017-02-20 17:00:20 -0500 | [diff] [blame] | 1058 | struct OPENSSL_timeval now; |
David Benjamin | 721e8b7 | 2016-08-03 13:13:17 -0400 | [diff] [blame] | 1059 | ssl_get_current_time(ssl, &now); |
| 1060 | ssl->s3->server_random[0] = now.tv_sec >> 24; |
| 1061 | ssl->s3->server_random[1] = now.tv_sec >> 16; |
| 1062 | ssl->s3->server_random[2] = now.tv_sec >> 8; |
| 1063 | ssl->s3->server_random[3] = now.tv_sec; |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 1064 | if (!RAND_bytes(ssl->s3->server_random + 4, SSL3_RANDOM_SIZE - 4)) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1065 | return -1; |
| 1066 | } |
| 1067 | |
David Benjamin | 5510863 | 2016-08-11 22:01:18 -0400 | [diff] [blame] | 1068 | /* TODO(davidben): Implement the TLS 1.1 and 1.2 downgrade sentinels once TLS |
| 1069 | * 1.3 is finalized and we are not implementing a draft version. */ |
David Benjamin | 1f61f0d | 2016-07-10 12:20:35 -0400 | [diff] [blame] | 1070 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1071 | const SSL_SESSION *session = hs->new_session; |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1072 | if (ssl->session != NULL) { |
| 1073 | session = ssl->session; |
| 1074 | } |
| 1075 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1076 | CBB cbb, body, session_id; |
| 1077 | if (!ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_SERVER_HELLO) || |
| 1078 | !CBB_add_u16(&body, ssl->version) || |
| 1079 | !CBB_add_bytes(&body, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1080 | !CBB_add_u8_length_prefixed(&body, &session_id) || |
Steven Valdez | 87eab49 | 2016-06-27 16:34:59 -0400 | [diff] [blame] | 1081 | !CBB_add_bytes(&session_id, session->session_id, |
| 1082 | session->session_id_length) || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1083 | !CBB_add_u16(&body, ssl_cipher_get_value(hs->new_cipher)) || |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1084 | !CBB_add_u8(&body, 0 /* no compression */) || |
David Benjamin | 8c880a2 | 2016-12-03 02:20:34 -0500 | [diff] [blame] | 1085 | !ssl_add_serverhello_tlsext(hs, &body) || |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1086 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1087 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1088 | CBB_cleanup(&cbb); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1092 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1093 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1094 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1095 | static int ssl3_send_server_certificate(SSL_HANDSHAKE *hs) { |
| 1096 | SSL *const ssl = hs->ssl; |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1097 | if (!ssl_has_certificate(ssl)) { |
| 1098 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATE_SET); |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1099 | return -1; |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1100 | } |
| 1101 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1102 | if (!ssl3_output_cert_chain(ssl)) { |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1103 | return -1; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1104 | } |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1105 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1106 | } |
| 1107 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1108 | static int ssl3_send_certificate_status(SSL_HANDSHAKE *hs) { |
| 1109 | SSL *const ssl = hs->ssl; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1110 | CBB cbb, body, ocsp_response; |
| 1111 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1112 | SSL3_MT_CERTIFICATE_STATUS) || |
| 1113 | !CBB_add_u8(&body, TLSEXT_STATUSTYPE_ocsp) || |
| 1114 | !CBB_add_u24_length_prefixed(&body, &ocsp_response) || |
David Benjamin | 83a3212 | 2017-02-14 18:34:54 -0500 | [diff] [blame] | 1115 | !CBB_add_bytes(&ocsp_response, |
| 1116 | CRYPTO_BUFFER_data(ssl->cert->ocsp_response), |
| 1117 | CRYPTO_BUFFER_len(ssl->cert->ocsp_response)) || |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1118 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1119 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1120 | CBB_cleanup(&cbb); |
| 1121 | return -1; |
| 1122 | } |
| 1123 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1124 | return 1; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1127 | static int ssl3_send_server_key_exchange(SSL_HANDSHAKE *hs) { |
| 1128 | SSL *const ssl = hs->ssl; |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1129 | CBB cbb, child; |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1130 | CBB_zero(&cbb); |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1131 | |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1132 | /* Put together the parameters. */ |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1133 | if (hs->state == SSL3_ST_SW_KEY_EXCH_A) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1134 | uint32_t alg_k = hs->new_cipher->algorithm_mkey; |
| 1135 | uint32_t alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1136 | |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1137 | /* Pre-allocate enough room to comfortably fit an ECDHE public key. */ |
| 1138 | if (!CBB_init(&cbb, 128)) { |
| 1139 | goto err; |
| 1140 | } |
| 1141 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1142 | /* PSK ciphers begin with an identity hint. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1143 | if (alg_a & SSL_aPSK) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1144 | size_t len = |
| 1145 | (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint); |
| 1146 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1147 | !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint, |
| 1148 | len)) { |
| 1149 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1150 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1151 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1152 | |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1153 | if (alg_k & SSL_kECDHE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1154 | /* Determine the group to use. */ |
| 1155 | uint16_t group_id; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1156 | if (!tls1_get_shared_group(hs, &group_id)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1157 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1158 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1159 | goto err; |
| 1160 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1161 | hs->new_session->group_id = group_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1162 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1163 | /* Set up ECDH, generate a key, and emit the public half. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1164 | if (!SSL_ECDH_CTX_init(&hs->ecdh_ctx, group_id) || |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1165 | !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1166 | !CBB_add_u16(&cbb, group_id) || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1167 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1168 | !SSL_ECDH_CTX_offer(&hs->ecdh_ctx, &child)) { |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1169 | goto err; |
| 1170 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1171 | } else { |
| 1172 | assert(alg_k & SSL_kPSK); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1173 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1174 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1175 | if (!CBB_finish(&cbb, &hs->server_params, &hs->server_params_len)) { |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1176 | goto err; |
| 1177 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /* Assemble the message. */ |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1181 | CBB body; |
| 1182 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1183 | SSL3_MT_SERVER_KEY_EXCHANGE) || |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1184 | !CBB_add_bytes(&body, hs->server_params, hs->server_params_len)) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1185 | goto err; |
| 1186 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1187 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1188 | /* Add a signature. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1189 | if (ssl_cipher_uses_certificate_auth(hs->new_cipher)) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1190 | if (!ssl_has_private_key(ssl)) { |
| 1191 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1192 | goto err; |
| 1193 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1194 | |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1195 | /* Determine the signature algorithm. */ |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1196 | uint16_t signature_algorithm; |
David Benjamin | f3c8f8d | 2016-11-17 17:20:47 +0900 | [diff] [blame] | 1197 | if (!tls1_choose_signature_algorithm(hs, &signature_algorithm)) { |
David Benjamin | ea9a0d5 | 2016-07-08 15:52:59 -0700 | [diff] [blame] | 1198 | goto err; |
| 1199 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1200 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1201 | if (!CBB_add_u16(&body, signature_algorithm)) { |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1202 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1203 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1204 | goto err; |
| 1205 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* Add space for the signature. */ |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 1209 | const size_t max_sig_len = EVP_PKEY_size(hs->local_pubkey); |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1210 | uint8_t *ptr; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1211 | if (!CBB_add_u16_length_prefixed(&body, &child) || |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1212 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1213 | goto err; |
| 1214 | } |
| 1215 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1216 | size_t sig_len; |
| 1217 | enum ssl_private_key_result_t sign_result; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1218 | if (hs->state == SSL3_ST_SW_KEY_EXCH_A) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1219 | CBB transcript; |
| 1220 | uint8_t *transcript_data; |
| 1221 | size_t transcript_len; |
| 1222 | if (!CBB_init(&transcript, |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1223 | 2 * SSL3_RANDOM_SIZE + hs->server_params_len) || |
| 1224 | !CBB_add_bytes(&transcript, ssl->s3->client_random, |
| 1225 | SSL3_RANDOM_SIZE) || |
| 1226 | !CBB_add_bytes(&transcript, ssl->s3->server_random, |
| 1227 | SSL3_RANDOM_SIZE) || |
| 1228 | !CBB_add_bytes(&transcript, hs->server_params, |
| 1229 | hs->server_params_len) || |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1230 | !CBB_finish(&transcript, &transcript_data, &transcript_len)) { |
| 1231 | CBB_cleanup(&transcript); |
| 1232 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1233 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1234 | goto err; |
| 1235 | } |
| 1236 | |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1237 | 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] | 1238 | signature_algorithm, transcript_data, |
| 1239 | transcript_len); |
| 1240 | OPENSSL_free(transcript_data); |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1241 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1242 | assert(hs->state == SSL3_ST_SW_KEY_EXCH_B); |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 1243 | 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] | 1244 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1245 | |
| 1246 | switch (sign_result) { |
| 1247 | case ssl_private_key_success: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1248 | if (!CBB_did_write(&child, sig_len)) { |
| 1249 | goto err; |
| 1250 | } |
| 1251 | break; |
| 1252 | case ssl_private_key_failure: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1253 | goto err; |
| 1254 | case ssl_private_key_retry: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1255 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1256 | hs->state = SSL3_ST_SW_KEY_EXCH_B; |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1257 | goto err; |
| 1258 | } |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1259 | } |
| 1260 | |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1261 | if (!ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1262 | goto err; |
| 1263 | } |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1264 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1265 | OPENSSL_free(hs->server_params); |
| 1266 | hs->server_params = NULL; |
| 1267 | hs->server_params_len = 0; |
David Benjamin | c42acee | 2016-06-17 17:47:58 -0400 | [diff] [blame] | 1268 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1269 | return 1; |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1270 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1271 | err: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1272 | CBB_cleanup(&cbb); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1273 | return -1; |
| 1274 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1275 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1276 | static int ssl3_send_certificate_request(SSL_HANDSHAKE *hs) { |
| 1277 | SSL *const ssl = hs->ssl; |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1278 | CBB cbb, body, cert_types, sigalgs_cbb; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1279 | if (!ssl->method->init_message(ssl, &cbb, &body, |
| 1280 | SSL3_MT_CERTIFICATE_REQUEST) || |
| 1281 | !CBB_add_u8_length_prefixed(&body, &cert_types) || |
David Benjamin | cc17c24 | 2017-03-30 22:53:03 -0500 | [diff] [blame] | 1282 | !CBB_add_u8(&cert_types, SSL3_CT_RSA_SIGN) || |
| 1283 | (ssl->version >= TLS1_VERSION && |
| 1284 | !CBB_add_u8(&cert_types, TLS_CT_ECDSA_SIGN))) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1285 | goto err; |
| 1286 | } |
| 1287 | |
| 1288 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | 6952211 | 2017-03-28 15:38:29 -0500 | [diff] [blame] | 1289 | if (!CBB_add_u16_length_prefixed(&body, &sigalgs_cbb) || |
| 1290 | !tls12_add_verify_sigalgs(ssl, &sigalgs_cbb)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1291 | goto err; |
| 1292 | } |
| 1293 | } |
| 1294 | |
David Benjamin | 32a66d5 | 2016-07-13 22:03:11 -0400 | [diff] [blame] | 1295 | if (!ssl_add_client_CA_list(ssl, &body) || |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1296 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1297 | goto err; |
| 1298 | } |
| 1299 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1300 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1301 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1302 | err: |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1303 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1304 | CBB_cleanup(&cbb); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1305 | return -1; |
| 1306 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1307 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1308 | static int ssl3_send_server_hello_done(SSL_HANDSHAKE *hs) { |
| 1309 | SSL *const ssl = hs->ssl; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1310 | CBB cbb, body; |
| 1311 | 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] | 1312 | !ssl_add_message_cbb(ssl, &cbb)) { |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1313 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1314 | CBB_cleanup(&cbb); |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1318 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1319 | } |
| 1320 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1321 | static int ssl3_get_client_certificate(SSL_HANDSHAKE *hs) { |
| 1322 | SSL *const ssl = hs->ssl; |
| 1323 | assert(hs->cert_request); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1324 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1325 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1326 | if (msg_ret <= 0) { |
| 1327 | return msg_ret; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 1331 | if (ssl->version == SSL3_VERSION && |
| 1332 | ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1333 | /* In SSL 3.0, the Certificate message is omitted to signal no |
| 1334 | * certificate. */ |
David Benjamin | da2630c | 2016-08-01 19:57:43 -0400 | [diff] [blame] | 1335 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1336 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1337 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 1338 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1339 | } |
| 1340 | |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1341 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 1342 | * 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] | 1343 | hs->new_session->verify_result = X509_V_OK; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1344 | ssl->s3->tmp.reuse_message = 1; |
| 1345 | return 1; |
| 1346 | } |
| 1347 | |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1348 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1349 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_UNEXPECTED_MESSAGE); |
| 1350 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1351 | } |
| 1352 | |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1353 | if (!ssl_hash_current_message(hs)) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1354 | return -1; |
| 1355 | } |
| 1356 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1357 | CBS certificate_msg; |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1358 | CBS_init(&certificate_msg, ssl->init_msg, ssl->init_num); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1359 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1360 | sk_CRYPTO_BUFFER_pop_free(hs->new_session->certs, CRYPTO_BUFFER_free); |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1361 | EVP_PKEY_free(hs->peer_pubkey); |
| 1362 | hs->peer_pubkey = NULL; |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1363 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1364 | hs->new_session->certs = ssl_parse_cert_chain( |
| 1365 | &alert, &hs->peer_pubkey, |
| 1366 | ssl->retain_only_sha256_of_client_certs ? hs->new_session->peer_sha256 |
| 1367 | : NULL, |
| 1368 | &certificate_msg, ssl->ctx->pool); |
| 1369 | if (hs->new_session->certs == NULL) { |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1370 | ssl3_send_alert(ssl, SSL3_AL_FATAL, alert); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1371 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1372 | } |
| 1373 | |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1374 | if (CBS_len(&certificate_msg) != 0 || |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1375 | !ssl->ctx->x509_method->session_cache_objects(hs->new_session)) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1376 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1377 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1378 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1379 | } |
| 1380 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1381 | if (sk_CRYPTO_BUFFER_num(hs->new_session->certs) == 0) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1382 | /* No client certificate so the handshake buffer may be discarded. */ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1383 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1384 | |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 1385 | /* In SSL 3.0, sending no certificate is signaled by omitting the |
| 1386 | * Certificate message. */ |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1387 | if (ssl->version == SSL3_VERSION) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1388 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1389 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1390 | return -1; |
David Benjamin | e455e51 | 2016-08-01 20:11:13 -0400 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | if (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT) { |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1394 | /* Fail for TLS only if we required a certificate */ |
| 1395 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1396 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1397 | return -1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1398 | } |
Adam Langley | 3764683 | 2016-08-01 16:16:46 -0700 | [diff] [blame] | 1399 | |
| 1400 | /* OpenSSL returns X509_V_OK when no certificates are received. This is |
David Benjamin | dd634eb | 2016-08-18 16:40:28 -0400 | [diff] [blame] | 1401 | * 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] | 1402 | hs->new_session->verify_result = X509_V_OK; |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1403 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1404 | } |
| 1405 | |
Adam Langley | 68e7124 | 2016-12-12 11:06:16 -0800 | [diff] [blame] | 1406 | /* The hash will have been filled in. */ |
| 1407 | if (ssl->retain_only_sha256_of_client_certs) { |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1408 | hs->new_session->peer_sha256_valid = 1; |
Adam Langley | 364f7a6 | 2016-12-12 10:51:00 -0800 | [diff] [blame] | 1409 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1410 | |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 1411 | 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] | 1412 | return -1; |
| 1413 | } |
Adam Langley | 2a3b343 | 2017-02-10 13:56:48 -0800 | [diff] [blame] | 1414 | |
David Benjamin | 5c900c8 | 2016-07-13 23:03:26 -0400 | [diff] [blame] | 1415 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1416 | } |
| 1417 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1418 | static int ssl3_get_client_key_exchange(SSL_HANDSHAKE *hs) { |
| 1419 | SSL *const ssl = hs->ssl; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1420 | int al; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1421 | CBS client_key_exchange; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 1422 | uint32_t alg_k; |
| 1423 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1424 | uint8_t *premaster_secret = NULL; |
| 1425 | size_t premaster_secret_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1426 | uint8_t *decrypt_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1427 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1428 | unsigned psk_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1429 | uint8_t psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1430 | |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1431 | if (hs->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1432 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1433 | if (ret <= 0) { |
| 1434 | return ret; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1435 | } |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1436 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1437 | if (!ssl_check_message_type(ssl, SSL3_MT_CLIENT_KEY_EXCHANGE) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1438 | !ssl_hash_current_message(hs)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1439 | return -1; |
| 1440 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1441 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1442 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1443 | CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num); |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1444 | alg_k = hs->new_cipher->algorithm_mkey; |
| 1445 | alg_a = hs->new_cipher->algorithm_auth; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1446 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1447 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1448 | if (alg_a & SSL_aPSK) { |
| 1449 | CBS psk_identity; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1450 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1451 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1452 | * then this is the only field in the message. */ |
| 1453 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1454 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1455 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1456 | al = SSL_AD_DECODE_ERROR; |
| 1457 | goto f_err; |
| 1458 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1459 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1460 | if (ssl->psk_server_callback == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1461 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1462 | al = SSL_AD_INTERNAL_ERROR; |
| 1463 | goto f_err; |
| 1464 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1465 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1466 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1467 | CBS_contains_zero_byte(&psk_identity)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1468 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1469 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1470 | goto f_err; |
| 1471 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1472 | |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1473 | if (!CBS_strdup(&psk_identity, &hs->new_session->psk_identity)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1474 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1475 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1476 | goto f_err; |
| 1477 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1478 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1479 | /* Look up the key for the identity. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1480 | psk_len = ssl->psk_server_callback(ssl, hs->new_session->psk_identity, psk, |
| 1481 | sizeof(psk)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1482 | if (psk_len > PSK_MAX_PSK_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1483 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1484 | al = SSL_AD_INTERNAL_ERROR; |
| 1485 | goto f_err; |
| 1486 | } else if (psk_len == 0) { |
| 1487 | /* PSK related to the given identity not found */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1488 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1489 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1490 | goto f_err; |
| 1491 | } |
| 1492 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1493 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1494 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1495 | * |premaster_secret_len|. */ |
| 1496 | if (alg_k & SSL_kRSA) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1497 | /* Allocate a buffer large enough for an RSA decryption. */ |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 1498 | const size_t rsa_size = EVP_PKEY_size(hs->local_pubkey); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1499 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1500 | if (decrypt_buf == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1501 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1502 | goto err; |
| 1503 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1504 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1505 | enum ssl_private_key_result_t decrypt_result; |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1506 | size_t decrypt_len; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1507 | if (hs->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1508 | if (!ssl_has_private_key(ssl) || |
David Benjamin | a232a71 | 2017-03-30 15:51:53 -0500 | [diff] [blame] | 1509 | EVP_PKEY_id(hs->local_pubkey) != EVP_PKEY_RSA) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1510 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1511 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1512 | goto f_err; |
| 1513 | } |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1514 | CBS encrypted_premaster_secret; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1515 | if (ssl->version > SSL3_VERSION) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1516 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1517 | &encrypted_premaster_secret) || |
| 1518 | CBS_len(&client_key_exchange) != 0) { |
David Benjamin | ef5e515 | 2015-11-18 20:35:31 -0500 | [diff] [blame] | 1519 | al = SSL_AD_DECODE_ERROR; |
| 1520 | OPENSSL_PUT_ERROR(SSL, |
| 1521 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1522 | goto f_err; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1523 | } |
| 1524 | } else { |
| 1525 | encrypted_premaster_secret = client_key_exchange; |
| 1526 | } |
| 1527 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1528 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1529 | * timing-sensitive code below. */ |
| 1530 | decrypt_result = ssl_private_key_decrypt( |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1531 | ssl, decrypt_buf, &decrypt_len, rsa_size, |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1532 | CBS_data(&encrypted_premaster_secret), |
| 1533 | CBS_len(&encrypted_premaster_secret)); |
| 1534 | } else { |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1535 | assert(hs->state == SSL3_ST_SR_KEY_EXCH_B); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1536 | /* Complete async decrypt. */ |
David Benjamin | d3440b4 | 2016-07-14 14:52:41 -0400 | [diff] [blame] | 1537 | decrypt_result = |
| 1538 | ssl_private_key_complete(ssl, decrypt_buf, &decrypt_len, rsa_size); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1539 | } |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1540 | |
| 1541 | switch (decrypt_result) { |
| 1542 | case ssl_private_key_success: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1543 | break; |
| 1544 | case ssl_private_key_failure: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1545 | goto err; |
| 1546 | case ssl_private_key_retry: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1547 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | cb0c29f | 2016-12-12 17:00:50 -0500 | [diff] [blame] | 1548 | hs->state = SSL3_ST_SR_KEY_EXCH_B; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1549 | goto err; |
| 1550 | } |
| 1551 | |
Daniel Bathgate | 4365c3f | 2016-04-14 17:18:02 -0400 | [diff] [blame] | 1552 | if (decrypt_len != rsa_size) { |
| 1553 | al = SSL_AD_DECRYPT_ERROR; |
| 1554 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1555 | goto f_err; |
| 1556 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1557 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1558 | /* Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1559 | * section 7.4.7.1. */ |
| 1560 | premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1561 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1562 | if (premaster_secret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1563 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1564 | goto err; |
| 1565 | } |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1566 | if (!RAND_bytes(premaster_secret, premaster_secret_len)) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1567 | goto err; |
| 1568 | } |
| 1569 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1570 | /* The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1571 | * publicly invalid. */ |
| 1572 | if (decrypt_len < 11 + premaster_secret_len) { |
| 1573 | al = SSL_AD_DECRYPT_ERROR; |
| 1574 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1575 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1576 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1577 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1578 | /* Check the padding. See RFC 3447, section 7.2.2. */ |
| 1579 | size_t padding_len = decrypt_len - premaster_secret_len; |
| 1580 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1581 | constant_time_eq_int_8(decrypt_buf[1], 2); |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1582 | for (size_t i = 2; i < padding_len - 1; i++) { |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1583 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1584 | } |
| 1585 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1586 | |
| 1587 | /* The premaster secret must begin with |client_version|. This too must be |
| 1588 | * checked in constant time (http://eprint.iacr.org/2003/052/). */ |
| 1589 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 1590 | (unsigned)(hs->client_version >> 8)); |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1591 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
David Benjamin | f04c2e9 | 2016-12-06 13:35:25 -0500 | [diff] [blame] | 1592 | (unsigned)(hs->client_version & 0xff)); |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1593 | |
| 1594 | /* Select, in constant time, either the decrypted premaster or the random |
| 1595 | * premaster based on |good|. */ |
David Benjamin | 5409123 | 2016-09-05 12:47:25 -0400 | [diff] [blame] | 1596 | for (size_t i = 0; i < premaster_secret_len; i++) { |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1597 | premaster_secret[i] = constant_time_select_8( |
| 1598 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1599 | } |
| 1600 | |
| 1601 | OPENSSL_free(decrypt_buf); |
| 1602 | decrypt_buf = NULL; |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1603 | } else if (alg_k & SSL_kECDHE) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1604 | /* Parse the ClientKeyExchange. */ |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1605 | CBS peer_key; |
Matthew Braithwaite | 7e06de5 | 2017-04-10 15:52:14 -0700 | [diff] [blame] | 1606 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &peer_key) || |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1607 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1608 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1609 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1610 | goto f_err; |
| 1611 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1612 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1613 | /* Compute the premaster. */ |
Adam Langley | c68e5b9 | 2017-02-08 13:33:15 -0800 | [diff] [blame] | 1614 | uint8_t alert = SSL_AD_DECODE_ERROR; |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1615 | if (!SSL_ECDH_CTX_finish(&hs->ecdh_ctx, &premaster_secret, |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1616 | &premaster_secret_len, &alert, CBS_data(&peer_key), |
| 1617 | CBS_len(&peer_key))) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1618 | al = alert; |
| 1619 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1620 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1621 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1622 | /* The key exchange state may now be discarded. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1623 | SSL_ECDH_CTX_cleanup(&hs->ecdh_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1624 | } else if (alg_k & SSL_kPSK) { |
| 1625 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1626 | * pre-shared key. */ |
| 1627 | premaster_secret_len = psk_len; |
| 1628 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1629 | if (premaster_secret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1630 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1631 | goto err; |
| 1632 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 1633 | OPENSSL_memset(premaster_secret, 0, premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1634 | } else { |
| 1635 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1636 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1637 | goto f_err; |
| 1638 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1639 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1640 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1641 | * pre-shared key. */ |
| 1642 | if (alg_a & SSL_aPSK) { |
| 1643 | CBB new_premaster, child; |
| 1644 | uint8_t *new_data; |
| 1645 | size_t new_len; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1646 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 1647 | CBB_zero(&new_premaster); |
| 1648 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1649 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1650 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1651 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1652 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1653 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1654 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1655 | CBB_cleanup(&new_premaster); |
| 1656 | goto err; |
| 1657 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1658 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1659 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1660 | OPENSSL_free(premaster_secret); |
| 1661 | premaster_secret = new_data; |
| 1662 | premaster_secret_len = new_len; |
| 1663 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1664 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1665 | /* Compute the master secret */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1666 | hs->new_session->master_key_length = tls1_generate_master_secret( |
| 1667 | hs, hs->new_session->master_key, premaster_secret, premaster_secret_len); |
| 1668 | if (hs->new_session->master_key_length == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1669 | goto err; |
| 1670 | } |
David Benjamin | fc02b59 | 2017-02-17 16:26:01 -0500 | [diff] [blame] | 1671 | hs->new_session->extended_master_secret = hs->extended_master_secret; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1672 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1673 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1674 | OPENSSL_free(premaster_secret); |
| 1675 | return 1; |
| 1676 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1677 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1678 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1679 | err: |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1680 | if (premaster_secret != NULL) { |
| 1681 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1682 | OPENSSL_free(premaster_secret); |
| 1683 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1684 | OPENSSL_free(decrypt_buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1685 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1686 | return -1; |
| 1687 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1688 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1689 | static int ssl3_get_cert_verify(SSL_HANDSHAKE *hs) { |
| 1690 | SSL *const ssl = hs->ssl; |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1691 | int al; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1692 | CBS certificate_verify, signature; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 1693 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1694 | /* Only RSA and ECDSA client certificates are supported, so a |
| 1695 | * CertificateVerify is required if and only if there's a client certificate. |
| 1696 | * */ |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1697 | if (hs->peer_pubkey == NULL) { |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1698 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1699 | return 1; |
| 1700 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1701 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1702 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1703 | if (msg_ret <= 0) { |
| 1704 | return msg_ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1705 | } |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 1706 | |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1707 | if (!ssl_check_message_type(ssl, SSL3_MT_CERTIFICATE_VERIFY)) { |
| 1708 | return -1; |
| 1709 | } |
| 1710 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1711 | CBS_init(&certificate_verify, ssl->init_msg, ssl->init_num); |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 1712 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1713 | /* Determine the digest type if needbe. */ |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1714 | uint16_t signature_algorithm = 0; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1715 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Steven Valdez | 0256385 | 2016-06-23 13:33:05 -0400 | [diff] [blame] | 1716 | if (!CBS_get_u16(&certificate_verify, &signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1717 | al = SSL_AD_DECODE_ERROR; |
| 1718 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1719 | goto f_err; |
| 1720 | } |
David Benjamin | 887c300 | 2016-07-08 16:15:32 -0700 | [diff] [blame] | 1721 | if (!tls12_check_peer_sigalg(ssl, &al, signature_algorithm)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1722 | goto f_err; |
| 1723 | } |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1724 | hs->new_session->peer_signature_algorithm = signature_algorithm; |
David Benjamin | a365138 | 2017-04-20 17:49:36 -0400 | [diff] [blame] | 1725 | } else if (!tls1_get_legacy_signature_algorithm(&signature_algorithm, |
| 1726 | hs->peer_pubkey)) { |
David Benjamin | 49ec9bb | 2016-07-14 00:11:26 -0400 | [diff] [blame] | 1727 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 1728 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 1729 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1730 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1731 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1732 | /* Parse and verify the signature. */ |
| 1733 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1734 | CBS_len(&certificate_verify) != 0) { |
| 1735 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1736 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1737 | goto f_err; |
| 1738 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1739 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1740 | int sig_ok; |
| 1741 | /* The SSL3 construction for CertificateVerify does not decompose into a |
| 1742 | * single final digest and signature, and must be special-cased. */ |
| 1743 | if (ssl3_protocol_version(ssl) == SSL3_VERSION) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1744 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1745 | size_t digest_len; |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1746 | if (!SSL_TRANSCRIPT_ssl3_cert_verify_hash(&hs->transcript, digest, |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1747 | &digest_len, hs->new_session, |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1748 | signature_algorithm)) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1749 | goto err; |
| 1750 | } |
| 1751 | |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1752 | EVP_PKEY_CTX *pctx = EVP_PKEY_CTX_new(hs->peer_pubkey, NULL); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1753 | sig_ok = pctx != NULL && |
| 1754 | EVP_PKEY_verify_init(pctx) && |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1755 | EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 1756 | digest, digest_len); |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1757 | EVP_PKEY_CTX_free(pctx); |
| 1758 | } else { |
| 1759 | sig_ok = ssl_public_key_verify( |
| 1760 | ssl, CBS_data(&signature), CBS_len(&signature), signature_algorithm, |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1761 | hs->peer_pubkey, (const uint8_t *)hs->transcript.buffer->data, |
| 1762 | hs->transcript.buffer->length); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1763 | } |
Steven Valdez | f0451ca | 2016-06-29 13:16:27 -0400 | [diff] [blame] | 1764 | |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1765 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1766 | sig_ok = 1; |
| 1767 | ERR_clear_error(); |
| 1768 | #endif |
| 1769 | if (!sig_ok) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1770 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1771 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1772 | goto f_err; |
| 1773 | } |
| 1774 | |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1775 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 1776 | * message.*/ |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1777 | SSL_TRANSCRIPT_free_buffer(&hs->transcript); |
| 1778 | if (!ssl_hash_current_message(hs)) { |
Steven Valdez | 2b8415e | 2016-06-30 13:27:23 -0400 | [diff] [blame] | 1779 | goto err; |
| 1780 | } |
| 1781 | |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1782 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1783 | |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1784 | f_err: |
| 1785 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1786 | err: |
Adam Langley | d515722 | 2016-12-12 11:37:43 -0800 | [diff] [blame] | 1787 | return 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1788 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1789 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1790 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 1791 | * sets the next_proto member in s if found */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1792 | static int ssl3_get_next_proto(SSL_HANDSHAKE *hs) { |
| 1793 | SSL *const ssl = hs->ssl; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1794 | int ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1795 | if (ret <= 0) { |
| 1796 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1797 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1798 | |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1799 | if (!ssl_check_message_type(ssl, SSL3_MT_NEXT_PROTO) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1800 | !ssl_hash_current_message(hs)) { |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1801 | return -1; |
| 1802 | } |
| 1803 | |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1804 | CBS next_protocol, selected_protocol, padding; |
| 1805 | CBS_init(&next_protocol, ssl->init_msg, ssl->init_num); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1806 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 1807 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
David Benjamin | 639846e | 2016-09-09 11:41:18 -0400 | [diff] [blame] | 1808 | CBS_len(&next_protocol) != 0) { |
| 1809 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1810 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_DECODE_ERROR); |
| 1811 | return 0; |
| 1812 | } |
| 1813 | |
| 1814 | if (!CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated, |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 1815 | &ssl->s3->next_proto_negotiated_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1816 | return 0; |
| 1817 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1818 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1819 | return 1; |
| 1820 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1821 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1822 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1823 | static int ssl3_get_channel_id(SSL_HANDSHAKE *hs) { |
| 1824 | SSL *const ssl = hs->ssl; |
David Benjamin | f71036e | 2017-01-21 14:49:39 -0500 | [diff] [blame] | 1825 | int msg_ret = ssl->method->ssl_get_message(ssl); |
David Benjamin | 09eb655 | 2016-07-08 14:32:11 -0700 | [diff] [blame] | 1826 | if (msg_ret <= 0) { |
| 1827 | return msg_ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1828 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1829 | |
David Benjamin | 276b7e8 | 2017-01-21 14:13:39 -0500 | [diff] [blame] | 1830 | if (!ssl_check_message_type(ssl, SSL3_MT_CHANNEL_ID) || |
Steven Valdez | 908ac19 | 2017-01-12 13:17:07 -0500 | [diff] [blame] | 1831 | !tls1_verify_channel_id(hs) || |
| 1832 | !ssl_hash_current_message(hs)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1833 | return -1; |
| 1834 | } |
Nick Harper | 60a85cb | 2016-09-23 16:25:11 -0700 | [diff] [blame] | 1835 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1836 | } |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1837 | |
David Benjamin | c3c8882 | 2016-11-14 10:32:04 +0900 | [diff] [blame] | 1838 | static int ssl3_send_new_session_ticket(SSL_HANDSHAKE *hs) { |
| 1839 | SSL *const ssl = hs->ssl; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1840 | const SSL_SESSION *session; |
| 1841 | SSL_SESSION *session_copy = NULL; |
| 1842 | if (ssl->session == NULL) { |
| 1843 | /* Fix the timeout to measure from the ticket issuance time. */ |
David Benjamin | 45738dd | 2017-02-09 20:01:26 -0500 | [diff] [blame] | 1844 | ssl_session_rebase_time(ssl, hs->new_session); |
| 1845 | session = hs->new_session; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1846 | } else { |
| 1847 | /* We are renewing an existing session. Duplicate the session to adjust the |
| 1848 | * timeout. */ |
| 1849 | session_copy = SSL_SESSION_dup(ssl->session, SSL_SESSION_INCLUDE_NONAUTH); |
| 1850 | if (session_copy == NULL) { |
| 1851 | return -1; |
| 1852 | } |
| 1853 | |
David Benjamin | 17b3083 | 2017-01-28 14:00:32 -0500 | [diff] [blame] | 1854 | ssl_session_rebase_time(ssl, session_copy); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1855 | session = session_copy; |
| 1856 | } |
| 1857 | |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1858 | CBB cbb, body, ticket; |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1859 | int ok = |
| 1860 | ssl->method->init_message(ssl, &cbb, &body, SSL3_MT_NEW_SESSION_TICKET) && |
| 1861 | CBB_add_u32(&body, session->timeout) && |
| 1862 | CBB_add_u16_length_prefixed(&body, &ticket) && |
| 1863 | ssl_encrypt_ticket(ssl, &ticket, session) && |
David Benjamin | daf207a | 2017-01-03 18:37:41 -0500 | [diff] [blame] | 1864 | ssl_add_message_cbb(ssl, &cbb); |
David Benjamin | 123db57 | 2016-11-03 16:59:25 -0400 | [diff] [blame] | 1865 | |
| 1866 | SSL_SESSION_free(session_copy); |
| 1867 | CBB_cleanup(&cbb); |
| 1868 | |
| 1869 | if (!ok) { |
David Benjamin | e75cc27 | 2016-11-03 18:06:27 -0400 | [diff] [blame] | 1870 | return -1; |
David Benjamin | 7583643 | 2016-06-17 18:48:29 -0400 | [diff] [blame] | 1871 | } |
| 1872 | |
David Benjamin | 16315f7 | 2017-01-12 20:02:05 -0500 | [diff] [blame] | 1873 | return 1; |
David Benjamin | 9f1dc82 | 2016-06-07 17:03:46 -0400 | [diff] [blame] | 1874 | } |