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