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