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