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