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> |
| 158 | #include <openssl/dh.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 159 | #include <openssl/ec.h> |
| 160 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 161 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 162 | #include <openssl/evp.h> |
| 163 | #include <openssl/hmac.h> |
| 164 | #include <openssl/md5.h> |
| 165 | #include <openssl/mem.h> |
David Benjamin | 9819367 | 2016-03-25 18:07:11 -0400 | [diff] [blame] | 166 | #include <openssl/nid.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | #include <openssl/rand.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 169 | #include <openssl/x509.h> |
| 170 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 171 | #include "internal.h" |
Adam Langley | af6e45b | 2014-11-03 19:34:49 -0800 | [diff] [blame] | 172 | #include "../crypto/internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 173 | #include "../crypto/dh/internal.h" |
| 174 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 175 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 176 | static int ssl3_get_initial_bytes(SSL *ssl); |
| 177 | static int ssl3_get_v2_client_hello(SSL *ssl); |
| 178 | static int ssl3_get_client_hello(SSL *ssl); |
| 179 | static int ssl3_send_server_hello(SSL *ssl); |
| 180 | static int ssl3_send_certificate_status(SSL *ssl); |
| 181 | static int ssl3_send_server_done(SSL *ssl); |
| 182 | static int ssl3_send_server_key_exchange(SSL *ssl); |
| 183 | static int ssl3_send_certificate_request(SSL *ssl); |
| 184 | static int ssl3_get_client_key_exchange(SSL *ssl); |
| 185 | static int ssl3_get_cert_verify(SSL *ssl); |
| 186 | static int ssl3_get_client_certificate(SSL *ssl); |
| 187 | static int ssl3_send_server_certificate(SSL *ssl); |
| 188 | static int ssl3_send_new_session_ticket(SSL *ssl); |
| 189 | static int ssl3_get_next_proto(SSL *ssl); |
| 190 | static int ssl3_get_channel_id(SSL *ssl); |
| 191 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 192 | int ssl3_accept(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | BUF_MEM *buf = NULL; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 194 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 195 | int ret = -1; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 196 | int state, skip = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 198 | assert(ssl->handshake_func == ssl3_accept); |
| 199 | assert(ssl->server); |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 200 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 201 | for (;;) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 202 | state = ssl->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 204 | switch (ssl->state) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 205 | case SSL_ST_ACCEPT: |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 206 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_START, 1); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 207 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 208 | if (ssl->init_buf == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | buf = BUF_MEM_new(); |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 210 | if (!buf || !BUF_MEM_reserve(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 211 | ret = -1; |
| 212 | goto end; |
| 213 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 214 | ssl->init_buf = buf; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 215 | buf = NULL; |
| 216 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 217 | ssl->init_num = 0; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 218 | |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 219 | /* Enable a write buffer. This groups handshake messages within a flight |
| 220 | * into a single write. */ |
David Benjamin | b095f0f | 2016-05-05 21:50:24 -0400 | [diff] [blame] | 221 | if (!ssl_init_wbio_buffer(ssl)) { |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 222 | ret = -1; |
| 223 | goto end; |
| 224 | } |
| 225 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 226 | if (!ssl3_init_handshake_buffer(ssl)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 227 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 228 | ret = -1; |
| 229 | goto end; |
| 230 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 231 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 232 | if (!ssl->s3->have_version && !SSL_IS_DTLS(ssl)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 233 | ssl->state = SSL3_ST_SR_INITIAL_BYTES; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 234 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 235 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 236 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 237 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 239 | case SSL3_ST_SR_INITIAL_BYTES: |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 240 | assert(!SSL_IS_DTLS(ssl)); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 241 | ret = ssl3_get_initial_bytes(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 242 | if (ret <= 0) { |
| 243 | goto end; |
| 244 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 245 | /* ssl3_get_initial_bytes sets ssl->state to one of |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 246 | * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */ |
| 247 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 248 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 249 | case SSL3_ST_SR_V2_CLIENT_HELLO: |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 250 | assert(!SSL_IS_DTLS(ssl)); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 251 | ret = ssl3_get_v2_client_hello(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 252 | if (ret <= 0) { |
| 253 | goto end; |
| 254 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 255 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 256 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 257 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 258 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 259 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 260 | case SSL3_ST_SR_CLNT_HELLO_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 261 | ret = ssl3_get_client_hello(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 262 | if (ret <= 0) { |
| 263 | goto end; |
| 264 | } |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 265 | ssl->method->received_flight(ssl); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 266 | ssl->state = SSL3_ST_SW_SRVR_HELLO_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 267 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 268 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 269 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 270 | case SSL3_ST_SW_SRVR_HELLO_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 271 | ret = ssl3_send_server_hello(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 272 | if (ret <= 0) { |
| 273 | goto end; |
| 274 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 275 | if (ssl->hit) { |
| 276 | if (ssl->tlsext_ticket_expected) { |
| 277 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 278 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 279 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 280 | } |
| 281 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 282 | ssl->state = SSL3_ST_SW_CERT_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 283 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 284 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 286 | case SSL3_ST_SW_CERT_A: |
| 287 | case SSL3_ST_SW_CERT_B: |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 288 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 289 | ret = ssl3_send_server_certificate(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 290 | if (ret <= 0) { |
| 291 | goto end; |
| 292 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 293 | if (ssl->s3->tmp.certificate_status_expected) { |
| 294 | ssl->state = SSL3_ST_SW_CERT_STATUS_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 295 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 296 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 297 | } |
| 298 | } else { |
| 299 | skip = 1; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 300 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 301 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 302 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 304 | case SSL3_ST_SW_CERT_STATUS_A: |
| 305 | case SSL3_ST_SW_CERT_STATUS_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 306 | ret = ssl3_send_certificate_status(ssl); |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 307 | if (ret <= 0) { |
| 308 | goto end; |
| 309 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 310 | ssl->state = SSL3_ST_SW_KEY_EXCH_A; |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 311 | break; |
| 312 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 313 | case SSL3_ST_SW_KEY_EXCH_A: |
| 314 | case SSL3_ST_SW_KEY_EXCH_B: |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 315 | case SSL3_ST_SW_KEY_EXCH_C: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 316 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 317 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 318 | /* PSK ciphers send ServerKeyExchange if there is an identity hint. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 319 | if (ssl_cipher_requires_server_key_exchange(ssl->s3->tmp.new_cipher) || |
| 320 | ((alg_a & SSL_aPSK) && ssl->psk_identity_hint)) { |
| 321 | ret = ssl3_send_server_key_exchange(ssl); |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 322 | if (ret <= 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 323 | goto end; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 324 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 325 | } else { |
| 326 | skip = 1; |
| 327 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 328 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 329 | ssl->state = SSL3_ST_SW_CERT_REQ_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 330 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 331 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 332 | case SSL3_ST_SW_CERT_REQ_A: |
| 333 | case SSL3_ST_SW_CERT_REQ_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 334 | if (ssl->s3->tmp.cert_request) { |
| 335 | ret = ssl3_send_certificate_request(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 336 | if (ret <= 0) { |
| 337 | goto end; |
| 338 | } |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 339 | } else { |
| 340 | skip = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 341 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 342 | ssl->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 343 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 344 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 345 | case SSL3_ST_SW_SRVR_DONE_A: |
| 346 | case SSL3_ST_SW_SRVR_DONE_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 347 | ret = ssl3_send_server_done(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 348 | if (ret <= 0) { |
| 349 | goto end; |
| 350 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 351 | ssl->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 352 | ssl->state = SSL3_ST_SW_FLUSH; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 353 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 354 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 355 | case SSL3_ST_SW_FLUSH: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 356 | if (BIO_flush(ssl->wbio) <= 0) { |
David Benjamin | 4c5ddb8 | 2016-03-11 22:56:19 -0500 | [diff] [blame] | 357 | ssl->rwstate = SSL_WRITING; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 358 | ret = -1; |
| 359 | goto end; |
| 360 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 361 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 362 | ssl->state = ssl->s3->tmp.next_state; |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 363 | if (ssl->state != SSL_ST_OK) { |
| 364 | ssl->method->expect_flight(ssl); |
| 365 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 366 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 368 | case SSL3_ST_SR_CERT_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 369 | if (ssl->s3->tmp.cert_request) { |
| 370 | ret = ssl3_get_client_certificate(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 371 | if (ret <= 0) { |
| 372 | goto end; |
| 373 | } |
| 374 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 375 | ssl->state = SSL3_ST_SR_KEY_EXCH_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 376 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 377 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 378 | case SSL3_ST_SR_KEY_EXCH_A: |
| 379 | case SSL3_ST_SR_KEY_EXCH_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 380 | ret = ssl3_get_client_key_exchange(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 381 | if (ret <= 0) { |
| 382 | goto end; |
| 383 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 384 | ssl->state = SSL3_ST_SR_CERT_VRFY_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 385 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 387 | case SSL3_ST_SR_CERT_VRFY_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 388 | ret = ssl3_get_cert_verify(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 389 | if (ret <= 0) { |
| 390 | goto end; |
| 391 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 393 | ssl->state = SSL3_ST_SR_CHANGE; |
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 | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 396 | case SSL3_ST_SR_CHANGE: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 397 | ret = ssl->method->ssl_read_change_cipher_spec(ssl); |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 398 | if (ret <= 0) { |
| 399 | goto end; |
| 400 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 401 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 402 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_READ)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 403 | ret = -1; |
| 404 | goto end; |
| 405 | } |
David Benjamin | a41280d | 2015-11-26 02:16:49 -0500 | [diff] [blame] | 406 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 407 | if (ssl->s3->next_proto_neg_seen) { |
| 408 | ssl->state = SSL3_ST_SR_NEXT_PROTO_A; |
| 409 | } else if (ssl->s3->tlsext_channel_id_valid) { |
| 410 | ssl->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 411 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 412 | ssl->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 413 | } |
| 414 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 415 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 416 | case SSL3_ST_SR_NEXT_PROTO_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 417 | ret = ssl3_get_next_proto(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 418 | if (ret <= 0) { |
| 419 | goto end; |
| 420 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 421 | if (ssl->s3->tlsext_channel_id_valid) { |
| 422 | ssl->state = SSL3_ST_SR_CHANNEL_ID_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 423 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 424 | ssl->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 425 | } |
| 426 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 427 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 428 | case SSL3_ST_SR_CHANNEL_ID_A: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 429 | ret = ssl3_get_channel_id(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 430 | if (ret <= 0) { |
| 431 | goto end; |
| 432 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 433 | ssl->state = SSL3_ST_SR_FINISHED_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 434 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 435 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 436 | case SSL3_ST_SR_FINISHED_A: |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 437 | ret = ssl3_get_finished(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 438 | if (ret <= 0) { |
| 439 | goto end; |
| 440 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 441 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 442 | ssl->method->received_flight(ssl); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 443 | if (ssl->hit) { |
| 444 | ssl->state = SSL_ST_OK; |
| 445 | } else if (ssl->tlsext_ticket_expected) { |
| 446 | ssl->state = SSL3_ST_SW_SESSION_TICKET_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 447 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 448 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 449 | } |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 450 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 451 | /* If this is a full handshake with ChannelID then record the hashshake |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 452 | * hashes in |ssl->session| in case we need them to verify a ChannelID |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 453 | * signature on a resumption of this session in the future. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 454 | if (!ssl->hit && ssl->s3->tlsext_channel_id_valid) { |
| 455 | ret = tls1_record_handshake_hashes_for_channel_id(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 456 | if (ret <= 0) { |
| 457 | goto end; |
| 458 | } |
| 459 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 460 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 461 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 462 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 463 | case SSL3_ST_SW_SESSION_TICKET_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 464 | ret = ssl3_send_new_session_ticket(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 465 | if (ret <= 0) { |
| 466 | goto end; |
| 467 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 468 | ssl->state = SSL3_ST_SW_CHANGE_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 469 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 470 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 471 | case SSL3_ST_SW_CHANGE_A: |
| 472 | case SSL3_ST_SW_CHANGE_B: |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 473 | ret = ssl->method->send_change_cipher_spec(ssl, SSL3_ST_SW_CHANGE_A, |
| 474 | SSL3_ST_SW_CHANGE_B); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 475 | if (ret <= 0) { |
| 476 | goto end; |
| 477 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 478 | ssl->state = SSL3_ST_SW_FINISHED_A; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 479 | |
David Benjamin | 57997da | 2015-12-25 15:48:39 -0500 | [diff] [blame] | 480 | if (!tls1_change_cipher_state(ssl, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 481 | ret = -1; |
| 482 | goto end; |
| 483 | } |
| 484 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 485 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 486 | case SSL3_ST_SW_FINISHED_A: |
| 487 | case SSL3_ST_SW_FINISHED_B: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 488 | ret = ssl3_send_finished(ssl, SSL3_ST_SW_FINISHED_A, |
David Benjamin | baa1216 | 2015-12-29 19:13:58 -0500 | [diff] [blame] | 489 | SSL3_ST_SW_FINISHED_B); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 490 | if (ret <= 0) { |
| 491 | goto end; |
| 492 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 493 | ssl->state = SSL3_ST_SW_FLUSH; |
| 494 | if (ssl->hit) { |
| 495 | ssl->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 497 | ssl->s3->tmp.next_state = SSL_ST_OK; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 498 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 499 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 501 | case SSL_ST_OK: |
| 502 | /* clean a few things up */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 503 | ssl3_cleanup_key_block(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 504 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 505 | /* In DTLS, |init_buf| cannot be released because post-handshake |
| 506 | * retransmit relies on that buffer being available as scratch space. |
| 507 | * |
| 508 | * TODO(davidben): Fix this. */ |
| 509 | if (!SSL_IS_DTLS(ssl)) { |
| 510 | BUF_MEM_free(ssl->init_buf); |
| 511 | ssl->init_buf = NULL; |
| 512 | ssl->init_num = 0; |
| 513 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 514 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 515 | /* remove buffering on output */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 516 | ssl_free_wbio_buffer(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 518 | /* If we aren't retaining peer certificates then we can discard it |
| 519 | * now. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 520 | if (ssl->ctx->retain_only_sha256_of_client_certs) { |
| 521 | X509_free(ssl->session->peer); |
| 522 | ssl->session->peer = NULL; |
| 523 | sk_X509_pop_free(ssl->session->cert_chain, X509_free); |
| 524 | ssl->session->cert_chain = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 525 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 526 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 527 | if (SSL_IS_DTLS(ssl)) { |
| 528 | ssl->d1->handshake_read_seq = 0; |
| 529 | ssl->d1->handshake_write_seq = 0; |
| 530 | ssl->d1->next_handshake_write_seq = 0; |
| 531 | } |
| 532 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 533 | ssl->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 534 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 535 | ssl_update_cache(ssl, SSL_SESS_CACHE_SERVER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 537 | ssl_do_info_callback(ssl, SSL_CB_HANDSHAKE_DONE, 1); |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 538 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 539 | ret = 1; |
| 540 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 541 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 542 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 543 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 544 | ret = -1; |
| 545 | goto end; |
| 546 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 547 | |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 548 | if (!ssl->s3->tmp.reuse_message && !skip && ssl->state != state) { |
| 549 | int new_state = ssl->state; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 550 | ssl->state = state; |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 551 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_LOOP, 1); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 552 | ssl->state = new_state; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 553 | } |
| 554 | skip = 0; |
| 555 | } |
| 556 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 557 | end: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 558 | BUF_MEM_free(buf); |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 559 | ssl_do_info_callback(ssl, SSL_CB_ACCEPT_EXIT, ret); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 560 | return ret; |
| 561 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 562 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 563 | static int ssl3_get_initial_bytes(SSL *ssl) { |
David Benjamin | 894f48c | 2015-07-24 23:48:52 -0400 | [diff] [blame] | 564 | /* Read the first 5 bytes, the size of the TLS record header. This is |
| 565 | * sufficient to detect a V2ClientHello and ensures that we never read beyond |
| 566 | * the first record. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 567 | int ret = ssl_read_buffer_extend_to(ssl, SSL3_RT_HEADER_LENGTH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 568 | if (ret <= 0) { |
| 569 | return ret; |
| 570 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 571 | assert(ssl_read_buffer_len(ssl) == SSL3_RT_HEADER_LENGTH); |
| 572 | const uint8_t *p = ssl_read_buffer(ssl); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 573 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 574 | /* Some dedicated error codes for protocol mixups should the application wish |
| 575 | * to interpret them differently. (These do not overlap with ClientHello or |
| 576 | * V2ClientHello.) */ |
| 577 | if (strncmp("GET ", (const char *)p, 4) == 0 || |
| 578 | strncmp("POST ", (const char *)p, 5) == 0 || |
| 579 | strncmp("HEAD ", (const char *)p, 5) == 0 || |
| 580 | strncmp("PUT ", (const char *)p, 4) == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 581 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTP_REQUEST); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 582 | return -1; |
| 583 | } |
David Benjamin | 894f48c | 2015-07-24 23:48:52 -0400 | [diff] [blame] | 584 | if (strncmp("CONNE", (const char *)p, 5) == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 585 | OPENSSL_PUT_ERROR(SSL, SSL_R_HTTPS_PROXY_REQUEST); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 586 | return -1; |
| 587 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 588 | |
David Benjamin | 97760d5 | 2015-07-24 23:02:49 -0400 | [diff] [blame] | 589 | /* Determine if this is a V2ClientHello. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 590 | if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO && |
| 591 | p[3] >= SSL3_VERSION_MAJOR) { |
| 592 | /* This is a V2ClientHello. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 593 | ssl->state = SSL3_ST_SR_V2_CLIENT_HELLO; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 594 | return 1; |
| 595 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 596 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 597 | /* Fall through to the standard logic. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 598 | ssl->state = SSL3_ST_SR_CLNT_HELLO_A; |
David Benjamin | 97760d5 | 2015-07-24 23:02:49 -0400 | [diff] [blame] | 599 | return 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 600 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 601 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 602 | static int ssl3_get_v2_client_hello(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 603 | const uint8_t *p; |
| 604 | int ret; |
| 605 | CBS v2_client_hello, cipher_specs, session_id, challenge; |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 606 | size_t msg_length, rand_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 607 | uint8_t msg_type; |
| 608 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 609 | CBB client_hello, hello_body, cipher_suites; |
| 610 | uint8_t random[SSL3_RANDOM_SIZE]; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 611 | |
David Benjamin | 45c6c3e | 2015-07-24 23:24:12 -0400 | [diff] [blame] | 612 | /* Determine the length of the V2ClientHello. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 613 | assert(ssl_read_buffer_len(ssl) >= SSL3_RT_HEADER_LENGTH); |
| 614 | p = ssl_read_buffer(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 615 | msg_length = ((p[0] & 0x7f) << 8) | p[1]; |
| 616 | if (msg_length > (1024 * 4)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 617 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_TOO_LARGE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 618 | return -1; |
| 619 | } |
David Benjamin | 894f48c | 2015-07-24 23:48:52 -0400 | [diff] [blame] | 620 | if (msg_length < SSL3_RT_HEADER_LENGTH - 2) { |
David Benjamin | 45c6c3e | 2015-07-24 23:24:12 -0400 | [diff] [blame] | 621 | /* Reject lengths that are too short early. We have already read |
David Benjamin | 894f48c | 2015-07-24 23:48:52 -0400 | [diff] [blame] | 622 | * |SSL3_RT_HEADER_LENGTH| bytes, so we should not attempt to process an |
David Benjamin | 45c6c3e | 2015-07-24 23:24:12 -0400 | [diff] [blame] | 623 | * (invalid) V2ClientHello which would be shorter than that. */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 624 | OPENSSL_PUT_ERROR(SSL, SSL_R_RECORD_LENGTH_MISMATCH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 625 | return -1; |
| 626 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 627 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 628 | /* Read the remainder of the V2ClientHello. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 629 | ret = ssl_read_buffer_extend_to(ssl, 2 + msg_length); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 630 | if (ret <= 0) { |
| 631 | return ret; |
| 632 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 633 | assert(ssl_read_buffer_len(ssl) == msg_length + 2); |
| 634 | CBS_init(&v2_client_hello, ssl_read_buffer(ssl) + 2, msg_length); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 635 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 636 | /* The V2ClientHello without the length is incorporated into the handshake |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 637 | * hash. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 638 | if (!ssl3_update_handshake_hash(ssl, CBS_data(&v2_client_hello), |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 639 | CBS_len(&v2_client_hello))) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 640 | return -1; |
| 641 | } |
David Benjamin | 4e9cc71 | 2016-06-01 20:16:03 -0400 | [diff] [blame] | 642 | |
| 643 | ssl_do_msg_callback(ssl, 0 /* read */, SSL2_VERSION, 0, |
| 644 | CBS_data(&v2_client_hello), CBS_len(&v2_client_hello)); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 645 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 646 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 647 | !CBS_get_u16(&v2_client_hello, &version) || |
| 648 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 649 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 650 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 651 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 652 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 653 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 654 | CBS_len(&v2_client_hello) != 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 655 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 656 | return -1; |
| 657 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 658 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 659 | /* msg_type has already been checked. */ |
| 660 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 661 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 662 | /* The client_random is the V2ClientHello challenge. Truncate or |
| 663 | * left-pad with zeros as needed. */ |
| 664 | memset(random, 0, SSL3_RANDOM_SIZE); |
| 665 | rand_len = CBS_len(&challenge); |
| 666 | if (rand_len > SSL3_RANDOM_SIZE) { |
| 667 | rand_len = SSL3_RANDOM_SIZE; |
| 668 | } |
| 669 | memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 670 | rand_len); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 671 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 672 | /* Write out an equivalent SSLv3 ClientHello. */ |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 673 | CBB_zero(&client_hello); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 674 | if (!CBB_init_fixed(&client_hello, (uint8_t *)ssl->init_buf->data, |
| 675 | ssl->init_buf->max) || |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 676 | !CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 677 | !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || |
| 678 | !CBB_add_u16(&hello_body, version) || |
| 679 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 680 | /* No session id. */ |
| 681 | !CBB_add_u8(&hello_body, 0) || |
| 682 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
| 683 | CBB_cleanup(&client_hello); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 684 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 685 | return -1; |
| 686 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 687 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 688 | /* Copy the cipher suites. */ |
| 689 | while (CBS_len(&cipher_specs) > 0) { |
| 690 | uint32_t cipher_spec; |
| 691 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
| 692 | CBB_cleanup(&client_hello); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 693 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 694 | return -1; |
| 695 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 696 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 697 | /* Skip SSLv2 ciphers. */ |
| 698 | if ((cipher_spec & 0xff0000) != 0) { |
| 699 | continue; |
| 700 | } |
| 701 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
| 702 | CBB_cleanup(&client_hello); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 703 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 704 | return -1; |
| 705 | } |
| 706 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 707 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 708 | /* Add the null compression scheme and finish. */ |
| 709 | if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) || |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 710 | !CBB_finish(&client_hello, NULL, &ssl->init_buf->length)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 711 | CBB_cleanup(&client_hello); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 712 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 713 | return -1; |
| 714 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 715 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 716 | /* Mark the message for "re"-use by the version-specific method. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 717 | ssl->s3->tmp.reuse_message = 1; |
| 718 | ssl->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO; |
David Benjamin | a6338be | 2016-05-13 18:12:19 -0400 | [diff] [blame] | 719 | ssl->s3->tmp.message_complete = 1; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 720 | |
David Benjamin | b8d28cf | 2015-07-28 21:34:45 -0400 | [diff] [blame] | 721 | /* Consume and discard the V2ClientHello. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 722 | ssl_read_buffer_consume(ssl, 2 + msg_length); |
| 723 | ssl_read_buffer_discard(ssl); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 724 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 725 | return 1; |
| 726 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 727 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 728 | static int ssl3_get_client_hello(SSL *ssl) { |
David Benjamin | 4b30b28 | 2015-05-26 16:37:58 -0400 | [diff] [blame] | 729 | int ok, al = SSL_AD_INTERNAL_ERROR, ret = -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 730 | long n; |
| 731 | const SSL_CIPHER *c; |
| 732 | STACK_OF(SSL_CIPHER) *ciphers = NULL; |
| 733 | struct ssl_early_callback_ctx early_ctx; |
| 734 | CBS client_hello; |
| 735 | uint16_t client_version; |
| 736 | CBS client_random, session_id, cipher_suites, compression_methods; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 737 | SSL_SESSION *session = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 738 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 739 | /* We do this so that we will respond with our native type. If we are TLSv1 |
| 740 | * and we get SSLv3, we will respond with TLSv1, This down switching should |
| 741 | * be handled by a different method. If we are SSLv3, we will respond with |
| 742 | * SSLv3, even if prompted with TLSv1. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 743 | switch (ssl->state) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 744 | case SSL3_ST_SR_CLNT_HELLO_A: |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 745 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_CLIENT_HELLO, |
| 746 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 747 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 748 | if (!ok) { |
| 749 | return n; |
| 750 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 751 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 752 | ssl->state = SSL3_ST_SR_CLNT_HELLO_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 753 | /* fallthrough */ |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 754 | case SSL3_ST_SR_CLNT_HELLO_B: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 755 | case SSL3_ST_SR_CLNT_HELLO_C: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 756 | /* We have previously parsed the ClientHello message, and can't call |
| 757 | * ssl_get_message again without hashing the message into the Finished |
| 758 | * digest again. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 759 | n = ssl->init_num; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 760 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 761 | memset(&early_ctx, 0, sizeof(early_ctx)); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 762 | early_ctx.ssl = ssl; |
| 763 | early_ctx.client_hello = ssl->init_msg; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 764 | early_ctx.client_hello_len = n; |
| 765 | if (!ssl_early_callback_init(&early_ctx)) { |
| 766 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 767 | OPENSSL_PUT_ERROR(SSL, SSL_R_CLIENTHELLO_PARSE_FAILED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 768 | goto f_err; |
| 769 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 770 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 771 | if (ssl->state == SSL3_ST_SR_CLNT_HELLO_B && |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 772 | ssl->ctx->select_certificate_cb != NULL) { |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 773 | ssl->state = SSL3_ST_SR_CLNT_HELLO_C; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 774 | switch (ssl->ctx->select_certificate_cb(&early_ctx)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 775 | case 0: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 776 | ssl->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
David Benjamin | 2fff5bf | 2015-02-09 04:32:28 -0500 | [diff] [blame] | 777 | goto err; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 778 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 779 | case -1: |
| 780 | /* Connection rejected. */ |
| 781 | al = SSL_AD_ACCESS_DENIED; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 782 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 783 | goto f_err; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 784 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 785 | default: |
| 786 | /* fallthrough */; |
| 787 | } |
| 788 | } |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 789 | ssl->state = SSL3_ST_SR_CLNT_HELLO_C; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 790 | break; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 791 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 792 | default: |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 793 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_STATE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 794 | return -1; |
| 795 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 796 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 797 | CBS_init(&client_hello, ssl->init_msg, n); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 798 | if (!CBS_get_u16(&client_hello, &client_version) || |
| 799 | !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) || |
| 800 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 801 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 802 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 803 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 804 | goto f_err; |
| 805 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 806 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 807 | /* use version from inside client hello, not from record header (may differ: |
| 808 | * see RFC 2246, Appendix E, second paragraph) */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 809 | ssl->client_version = client_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 810 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 811 | /* Load the client random. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 812 | memcpy(ssl->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 813 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 814 | if (SSL_IS_DTLS(ssl)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 815 | CBS cookie; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 816 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 817 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 818 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
| 819 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 820 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 821 | goto f_err; |
| 822 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 823 | } |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 824 | |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 825 | /* Note: This codepath may run twice if |ssl_get_prev_session| completes |
| 826 | * asynchronously. |
| 827 | * |
| 828 | * TODO(davidben): Clean up the order of events around ClientHello |
| 829 | * processing. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 830 | if (!ssl->s3->have_version) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 831 | /* Select version to use */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 832 | uint16_t version = ssl3_get_mutual_version(ssl, client_version); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 833 | if (version == 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 834 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNSUPPORTED_PROTOCOL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 835 | ssl->version = ssl->client_version; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 836 | al = SSL_AD_PROTOCOL_VERSION; |
| 837 | goto f_err; |
| 838 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 839 | ssl->version = version; |
David Benjamin | b83003e | 2015-12-30 17:39:26 -0500 | [diff] [blame] | 840 | ssl->s3->enc_method = ssl3_get_enc_method(version); |
| 841 | assert(ssl->s3->enc_method != NULL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 842 | /* At this point, the connection's version is known and |ssl->version| is |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 843 | * fixed. Begin enforcing the record-layer version. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 844 | ssl->s3->have_version = 1; |
| 845 | } else if (SSL_IS_DTLS(ssl) ? (ssl->client_version > ssl->version) |
| 846 | : (ssl->client_version < ssl->version)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 847 | OPENSSL_PUT_ERROR(SSL, SSL_R_WRONG_VERSION_NUMBER); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 848 | al = SSL_AD_PROTOCOL_VERSION; |
| 849 | goto f_err; |
| 850 | } |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 851 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 852 | ssl->hit = 0; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 853 | int send_new_ticket = 0; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 854 | switch (ssl_get_prev_session(ssl, &session, &send_new_ticket, &early_ctx)) { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 855 | case ssl_session_success: |
| 856 | break; |
| 857 | case ssl_session_error: |
| 858 | goto err; |
| 859 | case ssl_session_retry: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 860 | ssl->rwstate = SSL_PENDING_SESSION; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 861 | goto err; |
David Benjamin | 4b30b28 | 2015-05-26 16:37:58 -0400 | [diff] [blame] | 862 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 863 | ssl->tlsext_ticket_expected = send_new_ticket; |
David Benjamin | 4b30b28 | 2015-05-26 16:37:58 -0400 | [diff] [blame] | 864 | |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 865 | /* The EMS state is needed when making the resumption decision, but |
| 866 | * extensions are not normally parsed until later. This detects the EMS |
| 867 | * extension for the resumption decision and it's checked against the result |
| 868 | * of the normal parse later in this function. */ |
| 869 | const uint8_t *ems_data; |
| 870 | size_t ems_len; |
| 871 | int have_extended_master_secret = |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 872 | ssl->version != SSL3_VERSION && |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 873 | SSL_early_callback_ctx_extension_get(&early_ctx, |
| 874 | TLSEXT_TYPE_extended_master_secret, |
| 875 | &ems_data, &ems_len) && |
| 876 | ems_len == 0; |
| 877 | |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 878 | if (session != NULL) { |
| 879 | if (session->extended_master_secret && |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 880 | !have_extended_master_secret) { |
| 881 | /* A ClientHello without EMS that attempts to resume a session with EMS |
| 882 | * is fatal to the connection. */ |
| 883 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 884 | OPENSSL_PUT_ERROR(SSL, SSL_R_RESUMED_EMS_SESSION_WITHOUT_EMS_EXTENSION); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 885 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 886 | } |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 887 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 888 | ssl->hit = |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 889 | /* Only resume if the session's version matches the negotiated version: |
| 890 | * most clients do not accept a mismatch. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 891 | ssl->version == session->ssl_version && |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 892 | /* If the client offers the EMS extension, but the previous session |
| 893 | * didn't use it, then negotiate a new session. */ |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 894 | have_extended_master_secret == session->extended_master_secret; |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 895 | } |
| 896 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 897 | if (ssl->hit) { |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 898 | /* Use the new session. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 899 | SSL_SESSION_free(ssl->session); |
| 900 | ssl->session = session; |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 901 | session = NULL; |
| 902 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 903 | ssl->verify_result = ssl->session->verify_result; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 904 | } else { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 905 | if (!ssl_get_new_session(ssl, 1 /* server */)) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 906 | goto err; |
| 907 | } |
| 908 | |
| 909 | /* Clear the session ID if we want the session to be single-use. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 910 | if (!(ssl->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER)) { |
| 911 | ssl->session->session_id_length = 0; |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 912 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 913 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 914 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 915 | if (ssl->ctx->dos_protection_cb != NULL && |
| 916 | ssl->ctx->dos_protection_cb(&early_ctx) == 0) { |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 917 | /* Connection rejected for DOS reasons. */ |
| 918 | al = SSL_AD_ACCESS_DENIED; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 919 | OPENSSL_PUT_ERROR(SSL, SSL_R_CONNECTION_REJECTED); |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 920 | goto f_err; |
| 921 | } |
| 922 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 923 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
David Benjamin | 93de5e5 | 2015-04-17 22:33:25 -0400 | [diff] [blame] | 924 | CBS_len(&cipher_suites) == 0 || |
| 925 | CBS_len(&cipher_suites) % 2 != 0 || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 926 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 927 | CBS_len(&compression_methods) == 0) { |
| 928 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 929 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 930 | goto f_err; |
| 931 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 932 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 933 | ciphers = ssl_bytes_to_cipher_list(ssl, &cipher_suites); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 934 | if (ciphers == NULL) { |
| 935 | goto err; |
| 936 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 937 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 938 | /* If it is a hit, check that the cipher is in the list. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 939 | if (ssl->hit) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 940 | size_t j; |
| 941 | int found_cipher = 0; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 942 | uint32_t id = ssl->session->cipher->id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 943 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 944 | for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) { |
| 945 | c = sk_SSL_CIPHER_value(ciphers, j); |
| 946 | if (c->id == id) { |
| 947 | found_cipher = 1; |
| 948 | break; |
| 949 | } |
| 950 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 951 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 952 | if (!found_cipher) { |
| 953 | /* we need to have the cipher in the cipher list if we are asked to reuse |
| 954 | * it */ |
| 955 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 956 | OPENSSL_PUT_ERROR(SSL, SSL_R_REQUIRED_CIPHER_MISSING); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 957 | goto f_err; |
| 958 | } |
| 959 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 960 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 961 | /* Only null compression is supported. */ |
| 962 | if (memchr(CBS_data(&compression_methods), 0, |
| 963 | CBS_len(&compression_methods)) == NULL) { |
| 964 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 965 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_COMPRESSION_SPECIFIED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 966 | goto f_err; |
| 967 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 968 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 969 | /* TLS extensions. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 970 | if (ssl->version >= SSL3_VERSION && |
| 971 | !ssl_parse_clienthello_tlsext(ssl, &client_hello)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 972 | OPENSSL_PUT_ERROR(SSL, SSL_R_PARSE_TLSEXT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 973 | goto err; |
| 974 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 975 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 976 | /* There should be nothing left over in the record. */ |
| 977 | if (CBS_len(&client_hello) != 0) { |
| 978 | /* wrong packet length */ |
| 979 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 980 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_PACKET_LENGTH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 981 | goto f_err; |
| 982 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 983 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 984 | if (have_extended_master_secret != ssl->s3->tmp.extended_master_secret) { |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 985 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 986 | OPENSSL_PUT_ERROR(SSL, SSL_R_EMS_STATE_INCONSISTENT); |
Adam Langley | ba5934b | 2015-06-02 10:50:35 -0700 | [diff] [blame] | 987 | goto f_err; |
| 988 | } |
| 989 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 990 | /* Given ciphers and SSL_get_ciphers, we must pick a cipher */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 991 | if (!ssl->hit) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 992 | if (ciphers == NULL) { |
| 993 | al = SSL_AD_ILLEGAL_PARAMETER; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 994 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CIPHERS_PASSED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 995 | goto f_err; |
| 996 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 997 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 998 | /* Let cert callback update server certificates if required */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 999 | if (ssl->cert->cert_cb) { |
| 1000 | int rv = ssl->cert->cert_cb(ssl, ssl->cert->cert_cb_arg); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1001 | if (rv == 0) { |
| 1002 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1003 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_CB_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1004 | goto f_err; |
| 1005 | } |
| 1006 | if (rv < 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1007 | ssl->rwstate = SSL_X509_LOOKUP; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1008 | goto err; |
| 1009 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1010 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1011 | c = ssl3_choose_cipher(ssl, ciphers, ssl_get_cipher_preferences(ssl)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1012 | |
| 1013 | if (c == NULL) { |
| 1014 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1015 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_SHARED_CIPHER); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1016 | goto f_err; |
| 1017 | } |
David Benjamin | 0623bce | 2015-12-25 15:40:14 -0500 | [diff] [blame] | 1018 | ssl->session->cipher = c; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1019 | ssl->s3->tmp.new_cipher = c; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 1020 | |
| 1021 | /* Determine whether to request a client certificate. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1022 | ssl->s3->tmp.cert_request = !!(ssl->verify_mode & SSL_VERIFY_PEER); |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 1023 | /* Only request a certificate if Channel ID isn't negotiated. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1024 | if ((ssl->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 1025 | ssl->s3->tlsext_channel_id_valid) { |
| 1026 | ssl->s3->tmp.cert_request = 0; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 1027 | } |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 1028 | /* CertificateRequest may only be sent in certificate-based ciphers. */ |
| 1029 | if (!ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1030 | ssl->s3->tmp.cert_request = 0; |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 1031 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1032 | } else { |
| 1033 | /* Session-id reuse */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1034 | ssl->s3->tmp.new_cipher = ssl->session->cipher; |
| 1035 | ssl->s3->tmp.cert_request = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1036 | } |
| 1037 | |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1038 | /* Now that the cipher is known, initialize the handshake hash. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1039 | if (!ssl3_init_handshake_hash(ssl)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1040 | goto f_err; |
| 1041 | } |
| 1042 | |
David Benjamin | 5c1ce29 | 2015-05-26 16:59:43 -0400 | [diff] [blame] | 1043 | /* In TLS 1.2, client authentication requires hashing the handshake transcript |
| 1044 | * under a different hash. Otherwise, release the handshake buffer. */ |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1045 | if (!ssl->s3->tmp.cert_request || |
| 1046 | ssl3_protocol_version(ssl) < TLS1_2_VERSION) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1047 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /* we now have the following setup; |
| 1051 | * client_random |
| 1052 | * cipher_list - our prefered list of ciphers |
| 1053 | * ciphers - the clients prefered list of ciphers |
| 1054 | * compression - basically ignored right now |
| 1055 | * ssl version is set - sslv3 |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1056 | * ssl->session - The ssl session has been setup. |
| 1057 | * ssl->hit - session reuse flag |
| 1058 | * ssl->tmp.new_cipher - the new cipher to use. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1059 | |
David Benjamin | 0bd71eb | 2015-12-01 14:14:40 -0500 | [diff] [blame] | 1060 | ret = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1061 | |
| 1062 | if (0) { |
| 1063 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1064 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1065 | } |
| 1066 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1067 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1068 | sk_SSL_CIPHER_free(ciphers); |
David Benjamin | e3aa1d9 | 2015-06-16 15:34:50 -0400 | [diff] [blame] | 1069 | SSL_SESSION_free(session); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1070 | return ret; |
| 1071 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1072 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1073 | static int ssl3_send_server_hello(SSL *ssl) { |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1074 | if (ssl->state == SSL3_ST_SW_SRVR_HELLO_B) { |
| 1075 | return ssl_do_write(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1076 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1077 | |
David Benjamin | 5638046 | 2015-10-10 14:59:09 -0400 | [diff] [blame] | 1078 | assert(ssl->state == SSL3_ST_SW_SRVR_HELLO_A); |
| 1079 | |
| 1080 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 1081 | * known attack while we fix ChannelID itself. */ |
| 1082 | if (ssl->s3->tlsext_channel_id_valid && |
| 1083 | (ssl->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
| 1084 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1085 | } |
| 1086 | |
| 1087 | /* If this is a resumption and the original handshake didn't support |
| 1088 | * ChannelID then we didn't record the original handshake hashes in the |
| 1089 | * session and so cannot resume with ChannelIDs. */ |
| 1090 | if (ssl->hit && ssl->session->original_handshake_hash_len == 0) { |
| 1091 | ssl->s3->tlsext_channel_id_valid = 0; |
| 1092 | } |
| 1093 | |
| 1094 | if (!ssl_fill_hello_random(ssl->s3->server_random, SSL3_RANDOM_SIZE, |
| 1095 | 1 /* server */)) { |
| 1096 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1097 | return -1; |
| 1098 | } |
| 1099 | |
| 1100 | CBB cbb, session_id; |
| 1101 | size_t length; |
| 1102 | CBB_zero(&cbb); |
| 1103 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1104 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 1105 | !CBB_add_u16(&cbb, ssl->version) || |
| 1106 | !CBB_add_bytes(&cbb, ssl->s3->server_random, SSL3_RANDOM_SIZE) || |
| 1107 | !CBB_add_u8_length_prefixed(&cbb, &session_id) || |
| 1108 | !CBB_add_bytes(&session_id, ssl->session->session_id, |
| 1109 | ssl->session->session_id_length) || |
| 1110 | !CBB_add_u16(&cbb, ssl_cipher_get_value(ssl->s3->tmp.new_cipher)) || |
| 1111 | !CBB_add_u8(&cbb, 0 /* no compression */) || |
| 1112 | !ssl_add_serverhello_tlsext(ssl, &cbb) || |
| 1113 | !CBB_finish(&cbb, NULL, &length) || |
| 1114 | !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_HELLO, length)) { |
| 1115 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1116 | CBB_cleanup(&cbb); |
| 1117 | return -1; |
| 1118 | } |
| 1119 | |
| 1120 | ssl->state = SSL3_ST_SW_SRVR_HELLO_B; |
| 1121 | return ssl_do_write(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1122 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1123 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1124 | static int ssl3_send_certificate_status(SSL *ssl) { |
Paul Lietar | aeeff2c | 2015-08-12 11:47:11 +0100 | [diff] [blame] | 1125 | if (ssl->state == SSL3_ST_SW_CERT_STATUS_A) { |
| 1126 | CBB out, ocsp_response; |
| 1127 | size_t length; |
| 1128 | |
| 1129 | CBB_zero(&out); |
| 1130 | if (!CBB_init_fixed(&out, ssl_handshake_start(ssl), |
| 1131 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl)) || |
| 1132 | !CBB_add_u8(&out, TLSEXT_STATUSTYPE_ocsp) || |
| 1133 | !CBB_add_u24_length_prefixed(&out, &ocsp_response) || |
| 1134 | !CBB_add_bytes(&ocsp_response, ssl->ctx->ocsp_response, |
| 1135 | ssl->ctx->ocsp_response_length) || |
| 1136 | !CBB_finish(&out, NULL, &length) || |
| 1137 | !ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_STATUS, length)) { |
| 1138 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
| 1139 | CBB_cleanup(&out); |
| 1140 | return -1; |
| 1141 | } |
| 1142 | |
| 1143 | ssl->state = SSL3_ST_SW_CERT_STATUS_B; |
| 1144 | } |
| 1145 | |
| 1146 | /* SSL3_ST_SW_CERT_STATUS_B */ |
| 1147 | return ssl_do_write(ssl); |
| 1148 | } |
| 1149 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1150 | static int ssl3_send_server_done(SSL *ssl) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1151 | if (ssl->state == SSL3_ST_SW_SRVR_DONE_A) { |
| 1152 | if (!ssl_set_handshake_header(ssl, SSL3_MT_SERVER_DONE, 0)) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1153 | return -1; |
| 1154 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1155 | ssl->state = SSL3_ST_SW_SRVR_DONE_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1156 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1157 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1158 | /* SSL3_ST_SW_SRVR_DONE_B */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1159 | return ssl_do_write(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1160 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1161 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1162 | static int ssl3_send_server_key_exchange(SSL *ssl) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1163 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_C) { |
| 1164 | return ssl_do_write(ssl); |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1165 | } |
| 1166 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1167 | CBB cbb, child; |
| 1168 | if (!CBB_init_fixed(&cbb, ssl_handshake_start(ssl), |
| 1169 | ssl->init_buf->max - SSL_HM_HEADER_LENGTH(ssl))) { |
| 1170 | goto err; |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1171 | } |
| 1172 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1173 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
| 1174 | /* This is the first iteration, so write parameters. */ |
| 1175 | uint32_t alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1176 | uint32_t alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1177 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1178 | /* PSK ciphers begin with an identity hint. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1179 | if (alg_a & SSL_aPSK) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1180 | size_t len = |
| 1181 | (ssl->psk_identity_hint == NULL) ? 0 : strlen(ssl->psk_identity_hint); |
| 1182 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1183 | !CBB_add_bytes(&child, (const uint8_t *)ssl->psk_identity_hint, |
| 1184 | len)) { |
| 1185 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1186 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1187 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1188 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1189 | if (alg_k & SSL_kDHE) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1190 | /* Determine the group to use. */ |
| 1191 | DH *params = ssl->cert->dh_tmp; |
| 1192 | if (params == NULL && ssl->cert->dh_tmp_cb != NULL) { |
| 1193 | params = ssl->cert->dh_tmp_cb(ssl, 0, 1024); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1194 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1195 | if (params == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1196 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_DH_KEY); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1197 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1198 | goto err; |
| 1199 | } |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1200 | ssl->session->key_exchange_info = DH_num_bits(params); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1201 | |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1202 | /* Set up DH, generate a key, and emit the public half. */ |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1203 | DH *dh = DHparams_dup(params); |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1204 | if (dh == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1205 | goto err; |
| 1206 | } |
David Benjamin | 9f226a5 | 2015-04-26 22:12:32 -0400 | [diff] [blame] | 1207 | |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1208 | SSL_ECDH_CTX_init_for_dhe(&ssl->s3->tmp.ecdh_ctx, dh); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1209 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1210 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->p), params->p) || |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1211 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1212 | !BN_bn2cbb_padded(&child, BN_num_bytes(params->g), params->g) || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1213 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1214 | !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) { |
David Benjamin | 9f226a5 | 2015-04-26 22:12:32 -0400 | [diff] [blame] | 1215 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1216 | } |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1217 | } else if (alg_k & SSL_kECDHE) { |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1218 | /* Determine the group to use. */ |
| 1219 | uint16_t group_id; |
| 1220 | if (!tls1_get_shared_group(ssl, &group_id)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1221 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_TMP_ECDH_KEY); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1222 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1223 | goto err; |
| 1224 | } |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1225 | ssl->session->key_exchange_info = group_id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1226 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1227 | /* Set up ECDH, generate a key, and emit the public half. */ |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1228 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, group_id) || |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1229 | !CBB_add_u8(&cbb, NAMED_CURVE_TYPE) || |
Steven Valdez | ce902a9 | 2016-05-17 11:47:53 -0400 | [diff] [blame] | 1230 | !CBB_add_u16(&cbb, group_id) || |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1231 | !CBB_add_u8_length_prefixed(&cbb, &child) || |
| 1232 | !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) { |
| 1233 | goto err; |
| 1234 | } |
| 1235 | } else if (alg_k & SSL_kCECPQ1) { |
| 1236 | if (!SSL_ECDH_CTX_init(&ssl->s3->tmp.ecdh_ctx, SSL_GROUP_CECPQ1) || |
| 1237 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1238 | !SSL_ECDH_CTX_offer(&ssl->s3->tmp.ecdh_ctx, &child)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1239 | goto err; |
| 1240 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1241 | } else { |
| 1242 | assert(alg_k & SSL_kPSK); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1243 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1244 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1245 | /* Otherwise, restore |cbb| from the previous iteration. |
| 1246 | * TODO(davidben): When |ssl->init_buf| is gone, come up with a simpler |
| 1247 | * pattern. Probably keep the |CBB| around in the handshake state. */ |
| 1248 | } else if (!CBB_did_write(&cbb, ssl->init_num - SSL_HM_HEADER_LENGTH(ssl))) { |
| 1249 | goto err; |
| 1250 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1251 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1252 | /* Add a signature. */ |
David Benjamin | c032dfa | 2016-05-12 14:54:57 -0400 | [diff] [blame] | 1253 | if (ssl_cipher_uses_certificate_auth(ssl->s3->tmp.new_cipher)) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1254 | if (!ssl_has_private_key(ssl)) { |
| 1255 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1256 | goto err; |
| 1257 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1258 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1259 | const size_t max_sig_len = ssl_private_key_max_signature_len(ssl); |
| 1260 | size_t sig_len; |
| 1261 | enum ssl_private_key_result_t sign_result; |
| 1262 | if (ssl->state == SSL3_ST_SW_KEY_EXCH_A) { |
| 1263 | /* This is the first iteration, so set up the signature. Sample the |
| 1264 | * parameter length before adding a signature algorithm. */ |
| 1265 | if (!CBB_flush(&cbb)) { |
Adam Langley | c4f25ce | 2015-11-26 16:39:08 -0800 | [diff] [blame] | 1266 | goto err; |
| 1267 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1268 | size_t params_len = CBB_len(&cbb); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1269 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1270 | /* Determine signature algorithm. */ |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1271 | const EVP_MD *md; |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1272 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1273 | md = tls1_choose_signing_digest(ssl); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1274 | if (!tls12_add_sigandhash(ssl, &cbb, md)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1275 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1276 | ssl3_send_alert(ssl, SSL3_AL_FATAL, SSL_AD_INTERNAL_ERROR); |
| 1277 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1278 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1279 | } else if (ssl_private_key_type(ssl) == EVP_PKEY_RSA) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1280 | md = EVP_md5_sha1(); |
| 1281 | } else { |
| 1282 | md = EVP_sha1(); |
| 1283 | } |
| 1284 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1285 | /* Compute the digest and sign it. */ |
| 1286 | uint8_t digest[EVP_MAX_MD_SIZE]; |
Adam Langley | 5fb18c6 | 2015-12-22 09:37:19 -0800 | [diff] [blame] | 1287 | unsigned digest_len = 0; |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1288 | EVP_MD_CTX md_ctx; |
| 1289 | EVP_MD_CTX_init(&md_ctx); |
| 1290 | int digest_ret = |
| 1291 | EVP_DigestInit_ex(&md_ctx, md, NULL) && |
| 1292 | EVP_DigestUpdate(&md_ctx, ssl->s3->client_random, SSL3_RANDOM_SIZE) && |
| 1293 | EVP_DigestUpdate(&md_ctx, ssl->s3->server_random, SSL3_RANDOM_SIZE) && |
| 1294 | EVP_DigestUpdate(&md_ctx, CBB_data(&cbb), params_len) && |
| 1295 | EVP_DigestFinal_ex(&md_ctx, digest, &digest_len); |
| 1296 | EVP_MD_CTX_cleanup(&md_ctx); |
David Benjamin | fc82512 | 2015-12-18 01:57:43 -0500 | [diff] [blame] | 1297 | uint8_t *ptr; |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1298 | if (!digest_ret || |
| 1299 | !CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1300 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1301 | goto err; |
| 1302 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1303 | sign_result = ssl_private_key_sign(ssl, ptr, &sig_len, max_sig_len, md, |
| 1304 | digest, digest_len); |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1305 | } else { |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1306 | assert(ssl->state == SSL3_ST_SW_KEY_EXCH_B); |
| 1307 | |
| 1308 | /* Retry the signature. */ |
| 1309 | uint8_t *ptr; |
| 1310 | if (!CBB_add_u16_length_prefixed(&cbb, &child) || |
| 1311 | !CBB_reserve(&child, &ptr, max_sig_len)) { |
| 1312 | goto err; |
| 1313 | } |
| 1314 | sign_result = |
| 1315 | ssl_private_key_sign_complete(ssl, ptr, &sig_len, max_sig_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1316 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1317 | |
| 1318 | switch (sign_result) { |
| 1319 | case ssl_private_key_success: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1320 | if (!CBB_did_write(&child, sig_len)) { |
| 1321 | goto err; |
| 1322 | } |
| 1323 | break; |
| 1324 | case ssl_private_key_failure: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1325 | goto err; |
| 1326 | case ssl_private_key_retry: |
| 1327 | /* Discard the unfinished signature and save the state of |cbb| for the |
| 1328 | * next iteration. */ |
| 1329 | CBB_discard_child(&cbb); |
| 1330 | ssl->init_num = SSL_HM_HEADER_LENGTH(ssl) + CBB_len(&cbb); |
| 1331 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
| 1332 | ssl->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1333 | goto err; |
| 1334 | } |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1335 | } |
| 1336 | |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1337 | size_t length; |
| 1338 | if (!CBB_finish(&cbb, NULL, &length) || |
| 1339 | !ssl_set_handshake_header(ssl, SSL3_MT_SERVER_KEY_EXCHANGE, length)) { |
David Benjamin | 1f9f9c4 | 2015-08-28 16:17:59 -0400 | [diff] [blame] | 1340 | goto err; |
| 1341 | } |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1342 | ssl->state = SSL3_ST_SW_KEY_EXCH_C; |
| 1343 | return ssl_do_write(ssl); |
nagendra modadugu | 601448a | 2015-07-24 09:31:31 -0700 | [diff] [blame] | 1344 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1345 | err: |
David Benjamin | 2a0b391 | 2015-12-18 01:01:21 -0500 | [diff] [blame] | 1346 | CBB_cleanup(&cbb); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1347 | return -1; |
| 1348 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1349 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1350 | static int ssl3_send_certificate_request(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1351 | uint8_t *p, *d; |
| 1352 | size_t i; |
| 1353 | int j, nl, off, n; |
| 1354 | STACK_OF(X509_NAME) *sk = NULL; |
| 1355 | X509_NAME *name; |
| 1356 | BUF_MEM *buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1357 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1358 | if (ssl->state == SSL3_ST_SW_CERT_REQ_A) { |
| 1359 | buf = ssl->init_buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1360 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1361 | d = p = ssl_handshake_start(ssl); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1362 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1363 | /* get the list of acceptable cert types */ |
| 1364 | p++; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1365 | n = ssl3_get_req_cert_type(ssl, p); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1366 | d[0] = n; |
| 1367 | p += n; |
| 1368 | n++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1369 | |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1370 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1371 | const uint8_t *psigs; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1372 | nl = tls12_get_psigalgs(ssl, &psigs); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1373 | s2n(nl, p); |
| 1374 | memcpy(p, psigs, nl); |
| 1375 | p += nl; |
| 1376 | n += nl + 2; |
| 1377 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1378 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1379 | off = n; |
| 1380 | p += 2; |
| 1381 | n += 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1382 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1383 | sk = SSL_get_client_CA_list(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1384 | nl = 0; |
| 1385 | if (sk != NULL) { |
| 1386 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 1387 | name = sk_X509_NAME_value(sk, i); |
| 1388 | j = i2d_X509_NAME(name, NULL); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1389 | if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(ssl) + n + j + 2)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1390 | OPENSSL_PUT_ERROR(SSL, ERR_R_BUF_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1391 | goto err; |
| 1392 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1393 | p = ssl_handshake_start(ssl) + n; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1394 | s2n(j, p); |
| 1395 | i2d_X509_NAME(name, &p); |
| 1396 | n += 2 + j; |
| 1397 | nl += 2 + j; |
| 1398 | } |
| 1399 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1400 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1401 | /* else no CA names */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1402 | p = ssl_handshake_start(ssl) + off; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1403 | s2n(nl, p); |
| 1404 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1405 | if (!ssl_set_handshake_header(ssl, SSL3_MT_CERTIFICATE_REQUEST, n)) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1406 | goto err; |
| 1407 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1408 | ssl->state = SSL3_ST_SW_CERT_REQ_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1409 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1410 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1411 | /* SSL3_ST_SW_CERT_REQ_B */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1412 | return ssl_do_write(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1413 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1414 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1415 | return -1; |
| 1416 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1417 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1418 | static int ssl3_get_client_key_exchange(SSL *ssl) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1419 | int al; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1420 | CBS client_key_exchange; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 1421 | uint32_t alg_k; |
| 1422 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1423 | uint8_t *premaster_secret = NULL; |
| 1424 | size_t premaster_secret_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1425 | uint8_t *decrypt_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1426 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1427 | unsigned psk_len = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1428 | uint8_t psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1429 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1430 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1431 | int ok; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1432 | const long n = ssl->method->ssl_get_message( |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1433 | ssl, SSL3_MT_CLIENT_KEY_EXCHANGE, ssl_hash_message, &ok); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1434 | if (!ok) { |
| 1435 | return n; |
| 1436 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1437 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1438 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1439 | CBS_init(&client_key_exchange, ssl->init_msg, ssl->init_num); |
| 1440 | alg_k = ssl->s3->tmp.new_cipher->algorithm_mkey; |
| 1441 | alg_a = ssl->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1442 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1443 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1444 | if (alg_a & SSL_aPSK) { |
| 1445 | CBS psk_identity; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1446 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1447 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1448 | * then this is the only field in the message. */ |
| 1449 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1450 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1451 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1452 | al = SSL_AD_DECODE_ERROR; |
| 1453 | goto f_err; |
| 1454 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1455 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1456 | if (ssl->psk_server_callback == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1457 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_NO_SERVER_CB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1458 | al = SSL_AD_INTERNAL_ERROR; |
| 1459 | goto f_err; |
| 1460 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1461 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1462 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1463 | CBS_contains_zero_byte(&psk_identity)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1464 | OPENSSL_PUT_ERROR(SSL, SSL_R_DATA_LENGTH_TOO_LONG); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1465 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1466 | goto f_err; |
| 1467 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1468 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1469 | if (!CBS_strdup(&psk_identity, &ssl->session->psk_identity)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1470 | al = SSL_AD_INTERNAL_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1471 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1472 | goto f_err; |
| 1473 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1474 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1475 | /* Look up the key for the identity. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1476 | psk_len = ssl->psk_server_callback(ssl, ssl->session->psk_identity, psk, |
| 1477 | sizeof(psk)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1478 | if (psk_len > PSK_MAX_PSK_LEN) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1479 | OPENSSL_PUT_ERROR(SSL, ERR_R_INTERNAL_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1480 | al = SSL_AD_INTERNAL_ERROR; |
| 1481 | goto f_err; |
| 1482 | } else if (psk_len == 0) { |
| 1483 | /* PSK related to the given identity not found */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1484 | OPENSSL_PUT_ERROR(SSL, SSL_R_PSK_IDENTITY_NOT_FOUND); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1485 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1486 | goto f_err; |
| 1487 | } |
| 1488 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1489 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1490 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1491 | * |premaster_secret_len|. */ |
| 1492 | if (alg_k & SSL_kRSA) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1493 | /* Allocate a buffer large enough for an RSA decryption. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1494 | const size_t rsa_size = ssl_private_key_max_signature_len(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1495 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1496 | if (decrypt_buf == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1497 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1498 | goto err; |
| 1499 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1500 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1501 | enum ssl_private_key_result_t decrypt_result; |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1502 | size_t decrypt_len; |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1503 | if (ssl->state == SSL3_ST_SR_KEY_EXCH_A) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1504 | if (!ssl_has_private_key(ssl) || |
| 1505 | ssl_private_key_type(ssl) != EVP_PKEY_RSA) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1506 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1507 | OPENSSL_PUT_ERROR(SSL, SSL_R_MISSING_RSA_CERTIFICATE); |
| 1508 | goto f_err; |
| 1509 | } |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1510 | CBS encrypted_premaster_secret; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1511 | if (ssl->version > SSL3_VERSION) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1512 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1513 | &encrypted_premaster_secret) || |
| 1514 | CBS_len(&client_key_exchange) != 0) { |
David Benjamin | ef5e515 | 2015-11-18 20:35:31 -0500 | [diff] [blame] | 1515 | al = SSL_AD_DECODE_ERROR; |
| 1516 | OPENSSL_PUT_ERROR(SSL, |
| 1517 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1518 | goto f_err; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1519 | } |
| 1520 | } else { |
| 1521 | encrypted_premaster_secret = client_key_exchange; |
| 1522 | } |
| 1523 | |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1524 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1525 | * timing-sensitive code below. */ |
| 1526 | decrypt_result = ssl_private_key_decrypt( |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1527 | ssl, decrypt_buf, &decrypt_len, rsa_size, |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1528 | CBS_data(&encrypted_premaster_secret), |
| 1529 | CBS_len(&encrypted_premaster_secret)); |
| 1530 | } else { |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1531 | assert(ssl->state == SSL3_ST_SR_KEY_EXCH_B); |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1532 | /* Complete async decrypt. */ |
| 1533 | decrypt_result = ssl_private_key_decrypt_complete( |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1534 | ssl, decrypt_buf, &decrypt_len, rsa_size); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1535 | } |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1536 | |
| 1537 | switch (decrypt_result) { |
| 1538 | case ssl_private_key_success: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1539 | break; |
| 1540 | case ssl_private_key_failure: |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1541 | goto err; |
| 1542 | case ssl_private_key_retry: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1543 | ssl->rwstate = SSL_PRIVATE_KEY_OPERATION; |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1544 | ssl->state = SSL3_ST_SR_KEY_EXCH_B; |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1545 | goto err; |
| 1546 | } |
| 1547 | |
Daniel Bathgate | 4365c3f | 2016-04-14 17:18:02 -0400 | [diff] [blame] | 1548 | if (decrypt_len != rsa_size) { |
| 1549 | al = SSL_AD_DECRYPT_ERROR; |
| 1550 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1551 | goto f_err; |
| 1552 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1553 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1554 | /* Prepare a random premaster, to be used on invalid padding. See RFC 5246, |
| 1555 | * section 7.4.7.1. */ |
| 1556 | premaster_secret_len = SSL_MAX_MASTER_KEY_LENGTH; |
| 1557 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1558 | if (premaster_secret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1559 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1560 | goto err; |
| 1561 | } |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1562 | if (!RAND_bytes(premaster_secret, premaster_secret_len)) { |
nagendra modadugu | 3398dbf | 2015-08-07 14:07:52 -0700 | [diff] [blame] | 1563 | goto err; |
| 1564 | } |
| 1565 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1566 | /* The smallest padded premaster is 11 bytes of overhead. Small keys are |
| 1567 | * publicly invalid. */ |
| 1568 | if (decrypt_len < 11 + premaster_secret_len) { |
| 1569 | al = SSL_AD_DECRYPT_ERROR; |
| 1570 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECRYPTION_FAILED); |
| 1571 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1572 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1573 | |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1574 | /* Check the padding. See RFC 3447, section 7.2.2. */ |
| 1575 | size_t padding_len = decrypt_len - premaster_secret_len; |
| 1576 | uint8_t good = constant_time_eq_int_8(decrypt_buf[0], 0) & |
| 1577 | constant_time_eq_int_8(decrypt_buf[1], 2); |
| 1578 | size_t i; |
| 1579 | for (i = 2; i < padding_len - 1; i++) { |
| 1580 | good &= ~constant_time_is_zero_8(decrypt_buf[i]); |
| 1581 | } |
| 1582 | good &= constant_time_is_zero_8(decrypt_buf[padding_len - 1]); |
| 1583 | |
| 1584 | /* The premaster secret must begin with |client_version|. This too must be |
| 1585 | * checked in constant time (http://eprint.iacr.org/2003/052/). */ |
| 1586 | good &= constant_time_eq_8(decrypt_buf[padding_len], |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1587 | (unsigned)(ssl->client_version >> 8)); |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1588 | good &= constant_time_eq_8(decrypt_buf[padding_len + 1], |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1589 | (unsigned)(ssl->client_version & 0xff)); |
David Benjamin | 3f5b43d | 2015-12-01 19:31:24 -0500 | [diff] [blame] | 1590 | |
| 1591 | /* Select, in constant time, either the decrypted premaster or the random |
| 1592 | * premaster based on |good|. */ |
| 1593 | for (i = 0; i < premaster_secret_len; i++) { |
| 1594 | premaster_secret[i] = constant_time_select_8( |
| 1595 | good, decrypt_buf[padding_len + i], premaster_secret[i]); |
| 1596 | } |
| 1597 | |
| 1598 | OPENSSL_free(decrypt_buf); |
| 1599 | decrypt_buf = NULL; |
Matt Braithwaite | 053931e | 2016-05-25 12:06:05 -0700 | [diff] [blame] | 1600 | } else if (alg_k & (SSL_kECDHE|SSL_kDHE|SSL_kCECPQ1)) { |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1601 | /* Parse the ClientKeyExchange. */ |
David Benjamin | 974c7ba | 2015-12-19 16:58:04 -0500 | [diff] [blame] | 1602 | CBS peer_key; |
Matt Braithwaite | e25775b | 2016-05-16 16:31:05 -0700 | [diff] [blame] | 1603 | if (!SSL_ECDH_CTX_get_key(&ssl->s3->tmp.ecdh_ctx, &client_key_exchange, |
| 1604 | &peer_key) || |
| 1605 | CBS_len(&client_key_exchange) != 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1606 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1607 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1608 | goto f_err; |
| 1609 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1610 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1611 | /* Compute the premaster. */ |
| 1612 | uint8_t alert; |
Matt Braithwaite | f4ce8e5 | 2016-05-16 14:27:14 -0700 | [diff] [blame] | 1613 | if (!SSL_ECDH_CTX_finish(&ssl->s3->tmp.ecdh_ctx, &premaster_secret, |
| 1614 | &premaster_secret_len, &alert, CBS_data(&peer_key), |
| 1615 | CBS_len(&peer_key))) { |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1616 | al = alert; |
| 1617 | goto f_err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1618 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1619 | |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1620 | /* The key exchange state may now be discarded. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1621 | SSL_ECDH_CTX_cleanup(&ssl->s3->tmp.ecdh_ctx); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1622 | } else if (alg_k & SSL_kPSK) { |
| 1623 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 1624 | * pre-shared key. */ |
| 1625 | premaster_secret_len = psk_len; |
| 1626 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 1627 | if (premaster_secret == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1628 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1629 | goto err; |
| 1630 | } |
| 1631 | memset(premaster_secret, 0, premaster_secret_len); |
| 1632 | } else { |
| 1633 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1634 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNKNOWN_CIPHER_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1635 | goto f_err; |
| 1636 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1637 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1638 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 1639 | * pre-shared key. */ |
| 1640 | if (alg_a & SSL_aPSK) { |
| 1641 | CBB new_premaster, child; |
| 1642 | uint8_t *new_data; |
| 1643 | size_t new_len; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1644 | |
David Benjamin | a865320 | 2015-06-28 01:26:10 -0400 | [diff] [blame] | 1645 | CBB_zero(&new_premaster); |
| 1646 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len) || |
| 1647 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1648 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 1649 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 1650 | !CBB_add_bytes(&child, psk, psk_len) || |
| 1651 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1652 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1653 | CBB_cleanup(&new_premaster); |
| 1654 | goto err; |
| 1655 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1656 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1657 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1658 | OPENSSL_free(premaster_secret); |
| 1659 | premaster_secret = new_data; |
| 1660 | premaster_secret_len = new_len; |
| 1661 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1662 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1663 | /* Compute the master secret */ |
David Benjamin | f8d8071 | 2015-12-29 18:56:28 -0500 | [diff] [blame] | 1664 | ssl->session->master_key_length = tls1_generate_master_secret( |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1665 | ssl, ssl->session->master_key, premaster_secret, premaster_secret_len); |
| 1666 | if (ssl->session->master_key_length == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1667 | goto err; |
| 1668 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1669 | ssl->session->extended_master_secret = ssl->s3->tmp.extended_master_secret; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1670 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1671 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 1672 | OPENSSL_free(premaster_secret); |
| 1673 | return 1; |
| 1674 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1675 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1676 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1677 | err: |
David Benjamin | 4298d77 | 2015-12-19 00:18:25 -0500 | [diff] [blame] | 1678 | if (premaster_secret != NULL) { |
| 1679 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1680 | OPENSSL_free(premaster_secret); |
| 1681 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1682 | OPENSSL_free(decrypt_buf); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1683 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1684 | return -1; |
| 1685 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1686 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1687 | static int ssl3_get_cert_verify(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1688 | int al, ok, ret = 0; |
| 1689 | long n; |
| 1690 | CBS certificate_verify, signature; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1691 | X509 *peer = ssl->session->peer; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1692 | EVP_PKEY *pkey = NULL; |
| 1693 | const EVP_MD *md = NULL; |
| 1694 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 1695 | size_t digest_length; |
| 1696 | EVP_PKEY_CTX *pctx = NULL; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 1697 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1698 | /* Only RSA and ECDSA client certificates are supported, so a |
| 1699 | * CertificateVerify is required if and only if there's a client certificate. |
| 1700 | * */ |
| 1701 | if (peer == NULL) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1702 | ssl3_free_handshake_buffer(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1703 | return 1; |
| 1704 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1705 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1706 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_CERTIFICATE_VERIFY, |
| 1707 | ssl_dont_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1708 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1709 | if (!ok) { |
| 1710 | return n; |
| 1711 | } |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 1712 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1713 | /* Filter out unsupported certificate types. */ |
| 1714 | pkey = X509_get_pubkey(peer); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1715 | if (pkey == NULL) { |
| 1716 | goto err; |
| 1717 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1718 | if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) || |
| 1719 | (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) { |
| 1720 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1721 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1722 | goto f_err; |
| 1723 | } |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 1724 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1725 | CBS_init(&certificate_verify, ssl->init_msg, n); |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 1726 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1727 | /* Determine the digest type if needbe. */ |
David Benjamin | a1e9cab | 2015-12-30 00:08:49 -0500 | [diff] [blame] | 1728 | if (ssl3_protocol_version(ssl) >= TLS1_2_VERSION) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1729 | uint8_t hash, signature_type; |
| 1730 | if (!CBS_get_u8(&certificate_verify, &hash) || |
| 1731 | !CBS_get_u8(&certificate_verify, &signature_type)) { |
| 1732 | al = SSL_AD_DECODE_ERROR; |
| 1733 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
| 1734 | goto f_err; |
| 1735 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1736 | if (!tls12_check_peer_sigalg(ssl, &md, &al, hash, signature_type, pkey)) { |
David Benjamin | 6e80765 | 2015-11-02 12:02:20 -0500 | [diff] [blame] | 1737 | goto f_err; |
| 1738 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1739 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1740 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1741 | /* Compute the digest. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1742 | if (!ssl3_cert_verify_hash(ssl, digest, &digest_length, &md, pkey->type)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1743 | goto err; |
| 1744 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1745 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1746 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 1747 | * message.*/ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1748 | ssl3_free_handshake_buffer(ssl); |
| 1749 | if (!ssl3_hash_current_message(ssl)) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1750 | goto err; |
| 1751 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1752 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1753 | /* Parse and verify the signature. */ |
| 1754 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 1755 | CBS_len(&certificate_verify) != 0) { |
| 1756 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1757 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1758 | goto f_err; |
| 1759 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1760 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1761 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 1762 | if (pctx == NULL) { |
| 1763 | goto err; |
| 1764 | } |
David Benjamin | bf82aed | 2016-03-01 22:57:40 -0500 | [diff] [blame] | 1765 | int sig_ok = EVP_PKEY_verify_init(pctx) && |
| 1766 | EVP_PKEY_CTX_set_signature_md(pctx, md) && |
| 1767 | EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), |
| 1768 | digest, digest_length); |
| 1769 | #if defined(BORINGSSL_UNSAFE_FUZZER_MODE) |
| 1770 | sig_ok = 1; |
| 1771 | ERR_clear_error(); |
| 1772 | #endif |
| 1773 | if (!sig_ok) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1774 | al = SSL_AD_DECRYPT_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1775 | OPENSSL_PUT_ERROR(SSL, SSL_R_BAD_SIGNATURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1776 | goto f_err; |
| 1777 | } |
| 1778 | |
| 1779 | ret = 1; |
| 1780 | |
| 1781 | if (0) { |
| 1782 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1783 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1784 | } |
| 1785 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 1786 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1787 | EVP_PKEY_CTX_free(pctx); |
| 1788 | EVP_PKEY_free(pkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1789 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1790 | return ret; |
| 1791 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1792 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1793 | static int ssl3_get_client_certificate(SSL *ssl) { |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 1794 | int ok, al, ret = -1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1795 | X509 *x = NULL; |
| 1796 | unsigned long n; |
| 1797 | STACK_OF(X509) *sk = NULL; |
| 1798 | SHA256_CTX sha256; |
| 1799 | CBS certificate_msg, certificate_list; |
| 1800 | int is_first_certificate = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1801 | |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1802 | assert(ssl->s3->tmp.cert_request); |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 1803 | n = ssl->method->ssl_get_message(ssl, -1, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1804 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1805 | if (!ok) { |
| 1806 | return n; |
| 1807 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1808 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1809 | if (ssl->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1810 | if (ssl->version == SSL3_VERSION && |
| 1811 | ssl->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
| 1812 | /* In SSL 3.0, the Certificate message is omitted to signal no certificate. */ |
| 1813 | if ((ssl->verify_mode & SSL_VERIFY_PEER) && |
| 1814 | (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
| 1815 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 1816 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1817 | goto f_err; |
| 1818 | } |
| 1819 | |
| 1820 | ssl->s3->tmp.reuse_message = 1; |
| 1821 | return 1; |
| 1822 | } |
| 1823 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1824 | al = SSL_AD_UNEXPECTED_MESSAGE; |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1825 | OPENSSL_PUT_ERROR(SSL, SSL_R_UNEXPECTED_MESSAGE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1826 | goto f_err; |
| 1827 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 1828 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1829 | CBS_init(&certificate_msg, ssl->init_msg, n); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 1830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1831 | sk = sk_X509_new_null(); |
| 1832 | if (sk == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1833 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1834 | goto err; |
| 1835 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1836 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1837 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 1838 | CBS_len(&certificate_msg) != 0) { |
| 1839 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1840 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1841 | goto f_err; |
| 1842 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1843 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1844 | while (CBS_len(&certificate_list) > 0) { |
| 1845 | CBS certificate; |
| 1846 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1847 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1848 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 1849 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1850 | OPENSSL_PUT_ERROR(SSL, SSL_R_DECODE_ERROR); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1851 | goto f_err; |
| 1852 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1853 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1854 | if (is_first_certificate && ssl->ctx->retain_only_sha256_of_client_certs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1855 | /* If this is the first certificate, and we don't want to keep peer |
| 1856 | * certificates in memory, then we hash it right away. */ |
| 1857 | SHA256_Init(&sha256); |
| 1858 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1859 | SHA256_Final(ssl->session->peer_sha256, &sha256); |
| 1860 | ssl->session->peer_sha256_valid = 1; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1861 | } |
| 1862 | is_first_certificate = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1863 | |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1864 | /* A u24 length cannot overflow a long. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1865 | data = CBS_data(&certificate); |
David Benjamin | af07365 | 2015-11-12 23:09:30 -0800 | [diff] [blame] | 1866 | x = d2i_X509(NULL, &data, (long)CBS_len(&certificate)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1867 | if (x == NULL) { |
| 1868 | al = SSL_AD_BAD_CERTIFICATE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1869 | OPENSSL_PUT_ERROR(SSL, ERR_R_ASN1_LIB); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1870 | goto f_err; |
| 1871 | } |
| 1872 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 1873 | al = SSL_AD_DECODE_ERROR; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1874 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERT_LENGTH_MISMATCH); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1875 | goto f_err; |
| 1876 | } |
| 1877 | if (!sk_X509_push(sk, x)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1878 | OPENSSL_PUT_ERROR(SSL, ERR_R_MALLOC_FAILURE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1879 | goto err; |
| 1880 | } |
| 1881 | x = NULL; |
| 1882 | } |
| 1883 | |
| 1884 | if (sk_X509_num(sk) <= 0) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1885 | /* No client certificate so the handshake buffer may be discarded. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1886 | ssl3_free_handshake_buffer(ssl); |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1887 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1888 | /* TLS does not mind 0 certs returned */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1889 | if (ssl->version == SSL3_VERSION) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1890 | al = SSL_AD_HANDSHAKE_FAILURE; |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1891 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_CERTIFICATES_RETURNED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1892 | goto f_err; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1893 | } else if ((ssl->verify_mode & SSL_VERIFY_PEER) && |
David Benjamin | df28c3a | 2016-03-10 16:11:51 -0500 | [diff] [blame] | 1894 | (ssl->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
David Benjamin | 9550c3a | 2015-08-05 08:50:34 -0400 | [diff] [blame] | 1895 | /* Fail for TLS only if we required a certificate */ |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1896 | OPENSSL_PUT_ERROR(SSL, SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1897 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1898 | goto f_err; |
| 1899 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1900 | } else { |
David Benjamin | a857159 | 2016-03-10 01:41:55 -0500 | [diff] [blame] | 1901 | if (ssl_verify_cert_chain(ssl, sk) <= 0) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1902 | al = ssl_verify_alarm_type(ssl->verify_result); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 1903 | OPENSSL_PUT_ERROR(SSL, SSL_R_CERTIFICATE_VERIFY_FAILED); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1904 | goto f_err; |
| 1905 | } |
| 1906 | } |
| 1907 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1908 | X509_free(ssl->session->peer); |
| 1909 | ssl->session->peer = sk_X509_shift(sk); |
| 1910 | ssl->session->verify_result = ssl->verify_result; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1911 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1912 | sk_X509_pop_free(ssl->session->cert_chain, X509_free); |
| 1913 | ssl->session->cert_chain = sk; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1914 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 1915 | * certificate, while we do include it in s3_clnt.c */ |
| 1916 | |
| 1917 | sk = NULL; |
| 1918 | |
| 1919 | ret = 1; |
| 1920 | |
| 1921 | if (0) { |
| 1922 | f_err: |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1923 | ssl3_send_alert(ssl, SSL3_AL_FATAL, al); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1924 | } |
| 1925 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1926 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1927 | X509_free(x); |
| 1928 | sk_X509_pop_free(sk, X509_free); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1929 | return ret; |
| 1930 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1931 | |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1932 | static int ssl3_send_server_certificate(SSL *ssl) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1933 | if (ssl->state == SSL3_ST_SW_CERT_A) { |
| 1934 | if (!ssl3_output_cert_chain(ssl)) { |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 1935 | return 0; |
| 1936 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1937 | ssl->state = SSL3_ST_SW_CERT_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1938 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1939 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1940 | /* SSL3_ST_SW_CERT_B */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1941 | return ssl_do_write(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1942 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1943 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1944 | /* send a new session ticket (not necessarily for a new session) */ |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 1945 | static int ssl3_send_new_session_ticket(SSL *ssl) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 1946 | int ret = -1; |
| 1947 | uint8_t *session = NULL; |
| 1948 | size_t session_len; |
| 1949 | EVP_CIPHER_CTX ctx; |
| 1950 | HMAC_CTX hctx; |
| 1951 | |
| 1952 | EVP_CIPHER_CTX_init(&ctx); |
| 1953 | HMAC_CTX_init(&hctx); |
| 1954 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1955 | if (ssl->state == SSL3_ST_SW_SESSION_TICKET_A) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1956 | uint8_t *p, *macstart; |
| 1957 | int len; |
| 1958 | unsigned int hlen; |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1959 | SSL_CTX *tctx = ssl->initial_ctx; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1960 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 1961 | uint8_t key_name[16]; |
| 1962 | /* The maximum overhead of encrypting the session is 16 (key name) + IV + |
| 1963 | * one block of encryption overhead + HMAC. */ |
| 1964 | const size_t max_ticket_overhead = |
| 1965 | 16 + EVP_MAX_IV_LENGTH + EVP_MAX_BLOCK_LENGTH + EVP_MAX_MD_SIZE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1966 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1967 | /* Serialize the SSL_SESSION to be encoded into the ticket. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1968 | if (!SSL_SESSION_to_bytes_for_ticket(ssl->session, &session, |
| 1969 | &session_len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 1970 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1971 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1972 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1973 | /* If the session is too long, emit a dummy value rather than abort the |
| 1974 | * connection. */ |
| 1975 | if (session_len > 0xFFFF - max_ticket_overhead) { |
| 1976 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
| 1977 | const size_t placeholder_len = strlen(kTicketPlaceholder); |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 1978 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1979 | OPENSSL_free(session); |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 1980 | session = NULL; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 1981 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1982 | p = ssl_handshake_start(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1983 | /* Emit ticket_lifetime_hint. */ |
| 1984 | l2n(0, p); |
| 1985 | /* Emit ticket. */ |
| 1986 | s2n(placeholder_len, p); |
| 1987 | memcpy(p, kTicketPlaceholder, placeholder_len); |
| 1988 | p += placeholder_len; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 1989 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1990 | len = p - ssl_handshake_start(ssl); |
Steven Valdez | bbd43b5 | 2016-06-01 11:46:58 -0400 | [diff] [blame] | 1991 | if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 1992 | goto err; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1993 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 1994 | ssl->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 1995 | return ssl_do_write(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1996 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1997 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1998 | /* Grow buffer if need be: the length calculation is as follows: |
| 1999 | * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) + |
| 2000 | * max_ticket_overhead + * session_length */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2001 | if (!BUF_MEM_grow(ssl->init_buf, SSL_HM_HEADER_LENGTH(ssl) + 6 + |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2002 | max_ticket_overhead + session_len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2003 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2004 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2005 | p = ssl_handshake_start(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2006 | /* Initialize HMAC and cipher contexts. If callback present it does all the |
| 2007 | * work otherwise use generated values from parent ctx. */ |
| 2008 | if (tctx->tlsext_ticket_key_cb) { |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2009 | if (tctx->tlsext_ticket_key_cb(ssl, key_name, iv, &ctx, &hctx, |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 2010 | 1 /* encrypt */) < 0) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2011 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2012 | } |
| 2013 | } else { |
| 2014 | if (!RAND_bytes(iv, 16) || |
| 2015 | !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2016 | tctx->tlsext_tick_aes_key, iv) || |
| 2017 | !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), |
| 2018 | NULL)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2019 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2020 | } |
| 2021 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 2022 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2023 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2024 | /* Ticket lifetime hint (advisory only): We leave this unspecified for |
| 2025 | * resumed session (for simplicity), and guess that tickets for new |
| 2026 | * sessions will live as long as their sessions. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2027 | l2n(ssl->hit ? 0 : ssl->session->timeout, p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2028 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2029 | /* Skip ticket length for now */ |
| 2030 | p += 2; |
| 2031 | /* Output key name */ |
| 2032 | macstart = p; |
| 2033 | memcpy(p, key_name, 16); |
| 2034 | p += 16; |
| 2035 | /* output IV */ |
| 2036 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 2037 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 2038 | /* Encrypt session data */ |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2039 | if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) { |
| 2040 | goto err; |
| 2041 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2042 | p += len; |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2043 | if (!EVP_EncryptFinal_ex(&ctx, p, &len)) { |
| 2044 | goto err; |
| 2045 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2046 | p += len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2047 | |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2048 | if (!HMAC_Update(&hctx, macstart, p - macstart) || |
| 2049 | !HMAC_Final(&hctx, p, &hlen)) { |
| 2050 | goto err; |
| 2051 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2052 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2053 | p += hlen; |
| 2054 | /* Now write out lengths: p points to end of data written */ |
| 2055 | /* Total length */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2056 | len = p - ssl_handshake_start(ssl); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2057 | /* Skip ticket lifetime hint */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2058 | p = ssl_handshake_start(ssl) + 4; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2059 | s2n(len - 6, p); |
Steven Valdez | bbd43b5 | 2016-06-01 11:46:58 -0400 | [diff] [blame] | 2060 | if (!ssl_set_handshake_header(ssl, SSL3_MT_NEW_SESSION_TICKET, len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2061 | goto err; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2062 | } |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2063 | ssl->state = SSL3_ST_SW_SESSION_TICKET_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2064 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2065 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2066 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2067 | ret = ssl_do_write(ssl); |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2068 | |
| 2069 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2070 | OPENSSL_free(session); |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2071 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2072 | HMAC_CTX_cleanup(&hctx); |
| 2073 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2074 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2075 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2076 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 2077 | * sets the next_proto member in s if found */ |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 2078 | static int ssl3_get_next_proto(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2079 | int ok; |
| 2080 | long n; |
| 2081 | CBS next_protocol, selected_protocol, padding; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2082 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2083 | /* Clients cannot send a NextProtocol message if we didn't see the extension |
| 2084 | * in their ClientHello */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2085 | if (!ssl->s3->next_proto_neg_seen) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2086 | OPENSSL_PUT_ERROR(SSL, SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2087 | return -1; |
| 2088 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2089 | |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 2090 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_NEXT_PROTO, ssl_hash_message, |
| 2091 | &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2092 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2093 | if (!ok) { |
| 2094 | return n; |
| 2095 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2096 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2097 | CBS_init(&next_protocol, ssl->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2098 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2099 | /* The payload looks like: |
| 2100 | * uint8 proto_len; |
| 2101 | * uint8 proto[proto_len]; |
| 2102 | * uint8 padding_len; |
| 2103 | * uint8 padding[padding_len]; */ |
| 2104 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 2105 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 2106 | CBS_len(&next_protocol) != 0 || |
David Benjamin | 79978df | 2015-12-25 15:56:49 -0500 | [diff] [blame] | 2107 | !CBS_stow(&selected_protocol, &ssl->s3->next_proto_negotiated, |
| 2108 | &ssl->s3->next_proto_negotiated_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2109 | return 0; |
| 2110 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2111 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2112 | return 1; |
| 2113 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2114 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2115 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
David Benjamin | df50eec | 2016-06-07 16:49:42 -0400 | [diff] [blame^] | 2116 | static int ssl3_get_channel_id(SSL *ssl) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2117 | int ret = -1, ok; |
| 2118 | long n; |
David Benjamin | d6a4ae9 | 2015-08-06 11:10:51 -0400 | [diff] [blame] | 2119 | uint8_t channel_id_hash[EVP_MAX_MD_SIZE]; |
| 2120 | size_t channel_id_hash_len; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2121 | const uint8_t *p; |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2122 | uint16_t extension_type; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2123 | EC_GROUP *p256 = NULL; |
| 2124 | EC_KEY *key = NULL; |
| 2125 | EC_POINT *point = NULL; |
| 2126 | ECDSA_SIG sig; |
| 2127 | BIGNUM x, y; |
| 2128 | CBS encrypted_extensions, extension; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2129 | |
Steven Valdez | 1eca1d3 | 2016-05-23 16:29:25 -0400 | [diff] [blame] | 2130 | n = ssl->method->ssl_get_message(ssl, SSL3_MT_CHANNEL_ID_ENCRYPTED_EXTENSIONS, |
David Benjamin | 1e6d6df | 2016-05-13 18:28:17 -0400 | [diff] [blame] | 2131 | ssl_dont_hash_message, &ok); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2132 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2133 | if (!ok) { |
| 2134 | return n; |
| 2135 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2136 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2137 | /* Before incorporating the EncryptedExtensions message to the handshake |
| 2138 | * hash, compute the hash that should have been signed. */ |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2139 | if (!tls1_channel_id_hash(ssl, channel_id_hash, &channel_id_hash_len)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2140 | return -1; |
| 2141 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2142 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 2143 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2144 | if (!ssl3_hash_current_message(ssl)) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2145 | return -1; |
| 2146 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2147 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2148 | CBS_init(&encrypted_extensions, ssl->init_msg, n); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2149 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2150 | /* EncryptedExtensions could include multiple extensions, but the only |
| 2151 | * extension that could be negotiated is ChannelID, so there can only be one |
| 2152 | * entry. |
| 2153 | * |
| 2154 | * The payload looks like: |
| 2155 | * uint16 extension_type |
| 2156 | * uint16 extension_len; |
| 2157 | * uint8 x[32]; |
| 2158 | * uint8 y[32]; |
| 2159 | * uint8 r[32]; |
| 2160 | * uint8 s[32]; */ |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2161 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2162 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 2163 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 2164 | CBS_len(&encrypted_extensions) != 0 || |
Adam Langley | 49c7af1 | 2015-07-10 14:33:46 -0700 | [diff] [blame] | 2165 | extension_type != TLSEXT_TYPE_channel_id || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2166 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2167 | OPENSSL_PUT_ERROR(SSL, SSL_R_INVALID_MESSAGE); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2168 | return -1; |
| 2169 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2170 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2171 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2172 | if (!p256) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2173 | OPENSSL_PUT_ERROR(SSL, SSL_R_NO_P256_SUPPORT); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2174 | return -1; |
| 2175 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2176 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2177 | BN_init(&x); |
| 2178 | BN_init(&y); |
| 2179 | sig.r = BN_new(); |
| 2180 | sig.s = BN_new(); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 2181 | if (sig.r == NULL || sig.s == NULL) { |
| 2182 | goto err; |
| 2183 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2184 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2185 | p = CBS_data(&extension); |
| 2186 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 2187 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 2188 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 2189 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 2190 | goto err; |
| 2191 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2193 | point = EC_POINT_new(p256); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2194 | if (!point || |
| 2195 | !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2196 | goto err; |
| 2197 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2199 | key = EC_KEY_new(); |
| 2200 | if (!key || !EC_KEY_set_group(key, p256) || |
| 2201 | !EC_KEY_set_public_key(key, point)) { |
| 2202 | goto err; |
| 2203 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2204 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2205 | /* We stored the handshake hash in |tlsext_channel_id| the first time that we |
| 2206 | * were called. */ |
| 2207 | if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 2208 | OPENSSL_PUT_ERROR(SSL, SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2209 | ssl->s3->tlsext_channel_id_valid = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2210 | goto err; |
| 2211 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2212 | |
David Benjamin | 0d56f88 | 2015-12-19 17:05:56 -0500 | [diff] [blame] | 2213 | memcpy(ssl->s3->tlsext_channel_id, p, 64); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2214 | ret = 1; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2215 | |
| 2216 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2217 | BN_free(&x); |
| 2218 | BN_free(&y); |
| 2219 | BN_free(sig.r); |
| 2220 | BN_free(sig.s); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2221 | EC_KEY_free(key); |
| 2222 | EC_POINT_free(point); |
| 2223 | EC_GROUP_free(p256); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2224 | return ret; |
| 2225 | } |