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 | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 149 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 150 | #include <stdio.h> |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 151 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | |
| 153 | #include <openssl/bn.h> |
| 154 | #include <openssl/buf.h> |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 155 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 156 | #include <openssl/cipher.h> |
| 157 | #include <openssl/dh.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 158 | #include <openssl/ec.h> |
| 159 | #include <openssl/ecdsa.h> |
David Benjamin | f0ae170 | 2015-04-07 23:05:04 -0400 | [diff] [blame] | 160 | #include <openssl/err.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 161 | #include <openssl/evp.h> |
| 162 | #include <openssl/hmac.h> |
| 163 | #include <openssl/md5.h> |
| 164 | #include <openssl/mem.h> |
| 165 | #include <openssl/obj.h> |
| 166 | #include <openssl/rand.h> |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | #include <openssl/sha.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 168 | #include <openssl/x509.h> |
| 169 | |
David Benjamin | 2ee94aa | 2015-04-07 22:38:30 -0400 | [diff] [blame] | 170 | #include "internal.h" |
Adam Langley | af6e45b | 2014-11-03 19:34:49 -0800 | [diff] [blame] | 171 | #include "../crypto/internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 172 | #include "../crypto/dh/internal.h" |
| 173 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 174 | |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 175 | /* INITIAL_SNIFF_BUFFER_SIZE is the number of bytes read in the initial sniff |
| 176 | * buffer. */ |
| 177 | #define INITIAL_SNIFF_BUFFER_SIZE 8 |
| 178 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 179 | int ssl3_accept(SSL *s) { |
| 180 | BUF_MEM *buf = NULL; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 181 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 182 | void (*cb)(const SSL *ssl, int type, int val) = NULL; |
| 183 | int ret = -1; |
| 184 | int new_state, state, skip = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 185 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 186 | assert(s->handshake_func == ssl3_accept); |
| 187 | assert(s->server); |
| 188 | assert(!SSL_IS_DTLS(s)); |
David Benjamin | beb4702 | 2014-11-30 02:58:52 -0500 | [diff] [blame] | 189 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 190 | ERR_clear_error(); |
| 191 | ERR_clear_system_error(); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 193 | if (s->info_callback != NULL) { |
| 194 | cb = s->info_callback; |
| 195 | } else if (s->ctx->info_callback != NULL) { |
| 196 | cb = s->ctx->info_callback; |
| 197 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 198 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 199 | s->in_handshake++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 201 | if (s->cert == NULL) { |
| 202 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_NO_CERTIFICATE_SET); |
| 203 | return -1; |
| 204 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 205 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 206 | for (;;) { |
| 207 | state = s->state; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 209 | switch (s->state) { |
| 210 | case SSL_ST_RENEGOTIATE: |
| 211 | /* This state is the renegotiate entry point. It sends a HelloRequest |
| 212 | * and nothing else. */ |
| 213 | s->renegotiate = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 214 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 215 | if (cb != NULL) { |
| 216 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 217 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 218 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 219 | if (s->init_buf == NULL) { |
| 220 | buf = BUF_MEM_new(); |
| 221 | if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 222 | ret = -1; |
| 223 | goto end; |
| 224 | } |
| 225 | s->init_buf = buf; |
| 226 | buf = NULL; |
| 227 | } |
| 228 | s->init_num = 0; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 229 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 230 | if (!s->s3->send_connection_binding && |
| 231 | !(s->options & SSL_OP_ALLOW_UNSAFE_LEGACY_RENEGOTIATION)) { |
| 232 | /* Server attempting to renegotiate with client that doesn't support |
| 233 | * secure renegotiation. */ |
| 234 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, |
| 235 | SSL_R_UNSAFE_LEGACY_RENEGOTIATION_DISABLED); |
| 236 | ssl3_send_alert(s, SSL3_AL_FATAL, SSL_AD_HANDSHAKE_FAILURE); |
| 237 | ret = -1; |
| 238 | goto end; |
| 239 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 240 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 241 | s->state = SSL3_ST_SW_HELLO_REQ_A; |
| 242 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 243 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 244 | case SSL3_ST_SW_HELLO_REQ_A: |
| 245 | case SSL3_ST_SW_HELLO_REQ_B: |
| 246 | s->shutdown = 0; |
| 247 | ret = ssl3_send_hello_request(s); |
| 248 | if (ret <= 0) { |
| 249 | goto end; |
| 250 | } |
| 251 | s->s3->tmp.next_state = SSL3_ST_SW_HELLO_REQ_C; |
| 252 | s->state = SSL3_ST_SW_FLUSH; |
| 253 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 255 | if (!ssl3_init_finished_mac(s)) { |
| 256 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 257 | ret = -1; |
| 258 | goto end; |
| 259 | } |
| 260 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 261 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 262 | case SSL3_ST_SW_HELLO_REQ_C: |
| 263 | s->state = SSL_ST_OK; |
| 264 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 266 | case SSL_ST_ACCEPT: |
| 267 | case SSL_ST_BEFORE | SSL_ST_ACCEPT: |
| 268 | /* This state is the entry point for the handshake itself (initial and |
| 269 | * renegotiation). */ |
| 270 | if (cb != NULL) { |
| 271 | cb(s, SSL_CB_HANDSHAKE_START, 1); |
| 272 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 273 | |
David Benjamin | b16346b | 2015-04-08 19:16:58 -0400 | [diff] [blame] | 274 | if ((s->version >> 8) != 3) { |
| 275 | /* TODO(davidben): Some consumers clear |s->version| to break the |
| 276 | * handshake in a callback. Remove this when they're using proper |
| 277 | * APIs. */ |
| 278 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 279 | ret = -1; |
| 280 | goto end; |
| 281 | } |
| 282 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 283 | if (s->init_buf == NULL) { |
| 284 | buf = BUF_MEM_new(); |
| 285 | if (!buf || !BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) { |
| 286 | ret = -1; |
| 287 | goto end; |
| 288 | } |
| 289 | s->init_buf = buf; |
| 290 | buf = NULL; |
| 291 | } |
| 292 | s->init_num = 0; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 293 | |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 294 | /* Enable a write buffer. This groups handshake messages within a flight |
| 295 | * into a single write. */ |
| 296 | if (!ssl_init_wbio_buffer(s, 1)) { |
| 297 | ret = -1; |
| 298 | goto end; |
| 299 | } |
| 300 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 301 | if (!ssl3_init_finished_mac(s)) { |
| 302 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, ERR_R_INTERNAL_ERROR); |
| 303 | ret = -1; |
| 304 | goto end; |
| 305 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 306 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 307 | if (!s->s3->have_version) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 308 | s->state = SSL3_ST_SR_INITIAL_BYTES; |
| 309 | } else { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 310 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 311 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 312 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 313 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 314 | case SSL3_ST_SR_INITIAL_BYTES: |
| 315 | ret = ssl3_get_initial_bytes(s); |
| 316 | if (ret <= 0) { |
| 317 | goto end; |
| 318 | } |
| 319 | /* ssl3_get_initial_bytes sets s->state to one of |
| 320 | * SSL3_ST_SR_V2_CLIENT_HELLO or SSL3_ST_SR_CLNT_HELLO_A on success. */ |
| 321 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 322 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 323 | case SSL3_ST_SR_V2_CLIENT_HELLO: |
| 324 | ret = ssl3_get_v2_client_hello(s); |
| 325 | if (ret <= 0) { |
| 326 | goto end; |
| 327 | } |
| 328 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 329 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 330 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 331 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 332 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 333 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 334 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 335 | s->shutdown = 0; |
| 336 | ret = ssl3_get_client_hello(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 337 | if (ret <= 0) { |
| 338 | goto end; |
| 339 | } |
| 340 | s->renegotiate = 2; |
| 341 | s->state = SSL3_ST_SW_SRVR_HELLO_A; |
| 342 | s->init_num = 0; |
| 343 | break; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 344 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 345 | case SSL3_ST_SW_SRVR_HELLO_A: |
| 346 | case SSL3_ST_SW_SRVR_HELLO_B: |
| 347 | ret = ssl3_send_server_hello(s); |
| 348 | if (ret <= 0) { |
| 349 | goto end; |
| 350 | } |
| 351 | if (s->hit) { |
| 352 | if (s->tlsext_ticket_expected) { |
| 353 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 354 | } else { |
| 355 | s->state = SSL3_ST_SW_CHANGE_A; |
| 356 | } |
| 357 | } else { |
| 358 | s->state = SSL3_ST_SW_CERT_A; |
| 359 | } |
| 360 | s->init_num = 0; |
| 361 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 363 | case SSL3_ST_SW_CERT_A: |
| 364 | case SSL3_ST_SW_CERT_B: |
| 365 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 366 | ret = ssl3_send_server_certificate(s); |
| 367 | if (ret <= 0) { |
| 368 | goto end; |
| 369 | } |
| 370 | if (s->s3->tmp.certificate_status_expected) { |
| 371 | s->state = SSL3_ST_SW_CERT_STATUS_A; |
| 372 | } else { |
| 373 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 374 | } |
| 375 | } else { |
| 376 | skip = 1; |
| 377 | s->state = SSL3_ST_SW_KEY_EXCH_A; |
| 378 | } |
| 379 | s->init_num = 0; |
| 380 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 381 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 382 | case SSL3_ST_SW_KEY_EXCH_A: |
| 383 | case SSL3_ST_SW_KEY_EXCH_B: |
| 384 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 385 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 386 | /* Send a ServerKeyExchange message if: |
| 387 | * - The key exchange is ephemeral or anonymous |
| 388 | * Diffie-Hellman. |
| 389 | * - There is a PSK identity hint. |
| 390 | * |
| 391 | * TODO(davidben): This logic is currently duplicated in d1_srvr.c. Fix |
| 392 | * this. In the meantime, keep them in sync. */ |
| 393 | if (ssl_cipher_requires_server_key_exchange(s->s3->tmp.new_cipher) || |
| 394 | ((alg_a & SSL_aPSK) && s->psk_identity_hint)) { |
| 395 | ret = ssl3_send_server_key_exchange(s); |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 396 | if (ret <= 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 397 | goto end; |
David Benjamin | 6eb000d | 2015-02-11 01:17:41 -0500 | [diff] [blame] | 398 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 399 | } else { |
| 400 | skip = 1; |
| 401 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 402 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 403 | s->state = SSL3_ST_SW_CERT_REQ_A; |
| 404 | s->init_num = 0; |
| 405 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 406 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 407 | case SSL3_ST_SW_CERT_REQ_A: |
| 408 | case SSL3_ST_SW_CERT_REQ_B: |
| 409 | if (/* don't request cert unless asked for it: */ |
| 410 | !(s->verify_mode & SSL_VERIFY_PEER) || |
| 411 | /* Don't request a certificate if an obc was presented */ |
| 412 | ((s->verify_mode & SSL_VERIFY_PEER_IF_NO_OBC) && |
| 413 | s->s3->tlsext_channel_id_valid) || |
| 414 | /* if SSL_VERIFY_CLIENT_ONCE is set, |
| 415 | * don't request cert during re-negotiation: */ |
| 416 | ((s->session->peer != NULL) && |
| 417 | (s->verify_mode & SSL_VERIFY_CLIENT_ONCE)) || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 418 | /* With normal PSK Certificates and |
| 419 | * Certificate Requests are omitted */ |
| 420 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kPSK)) { |
| 421 | /* no cert request */ |
| 422 | skip = 1; |
| 423 | s->s3->tmp.cert_request = 0; |
| 424 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
| 425 | if (s->s3->handshake_buffer && |
| 426 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 427 | return -1; |
| 428 | } |
| 429 | } else { |
| 430 | s->s3->tmp.cert_request = 1; |
| 431 | ret = ssl3_send_certificate_request(s); |
| 432 | if (ret <= 0) { |
| 433 | goto end; |
| 434 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 435 | s->state = SSL3_ST_SW_SRVR_DONE_A; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 436 | s->init_num = 0; |
| 437 | } |
| 438 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 439 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 440 | case SSL3_ST_SW_SRVR_DONE_A: |
| 441 | case SSL3_ST_SW_SRVR_DONE_B: |
| 442 | ret = ssl3_send_server_done(s); |
| 443 | if (ret <= 0) { |
| 444 | goto end; |
| 445 | } |
| 446 | s->s3->tmp.next_state = SSL3_ST_SR_CERT_A; |
| 447 | s->state = SSL3_ST_SW_FLUSH; |
| 448 | s->init_num = 0; |
| 449 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 450 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 451 | case SSL3_ST_SW_FLUSH: |
| 452 | /* This code originally checked to see if any data was pending using |
| 453 | * BIO_CTRL_INFO and then flushed. This caused problems as documented |
| 454 | * in PR#1939. The proposed fix doesn't completely resolve this issue |
| 455 | * as buggy implementations of BIO_CTRL_PENDING still exist. So instead |
| 456 | * we just flush unconditionally. */ |
| 457 | s->rwstate = SSL_WRITING; |
| 458 | if (BIO_flush(s->wbio) <= 0) { |
| 459 | ret = -1; |
| 460 | goto end; |
| 461 | } |
| 462 | s->rwstate = SSL_NOTHING; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 463 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 464 | s->state = s->s3->tmp.next_state; |
| 465 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 466 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 467 | case SSL3_ST_SR_CERT_A: |
| 468 | case SSL3_ST_SR_CERT_B: |
| 469 | if (s->s3->tmp.cert_request) { |
| 470 | ret = ssl3_get_client_certificate(s); |
| 471 | if (ret <= 0) { |
| 472 | goto end; |
| 473 | } |
| 474 | } |
| 475 | s->init_num = 0; |
| 476 | s->state = SSL3_ST_SR_KEY_EXCH_A; |
| 477 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 478 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 479 | case SSL3_ST_SR_KEY_EXCH_A: |
| 480 | case SSL3_ST_SR_KEY_EXCH_B: |
| 481 | ret = ssl3_get_client_key_exchange(s); |
| 482 | if (ret <= 0) { |
| 483 | goto end; |
| 484 | } |
| 485 | s->state = SSL3_ST_SR_CERT_VRFY_A; |
| 486 | s->init_num = 0; |
| 487 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 488 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 489 | case SSL3_ST_SR_CERT_VRFY_A: |
| 490 | case SSL3_ST_SR_CERT_VRFY_B: |
| 491 | ret = ssl3_get_cert_verify(s); |
| 492 | if (ret <= 0) { |
| 493 | goto end; |
| 494 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 495 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 496 | s->state = SSL3_ST_SR_CHANGE; |
| 497 | s->init_num = 0; |
| 498 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 499 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 500 | case SSL3_ST_SR_CHANGE: { |
| 501 | char next_proto_neg = 0; |
| 502 | char channel_id = 0; |
| 503 | next_proto_neg = s->s3->next_proto_neg_seen; |
| 504 | channel_id = s->s3->tlsext_channel_id_valid; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 505 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 506 | /* At this point, the next message must be entirely behind a |
| 507 | * ChangeCipherSpec. */ |
| 508 | if (!ssl3_expect_change_cipher_spec(s)) { |
| 509 | ret = -1; |
| 510 | goto end; |
| 511 | } |
| 512 | if (next_proto_neg) { |
| 513 | s->state = SSL3_ST_SR_NEXT_PROTO_A; |
| 514 | } else if (channel_id) { |
| 515 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
| 516 | } else { |
| 517 | s->state = SSL3_ST_SR_FINISHED_A; |
| 518 | } |
| 519 | break; |
| 520 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 521 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 522 | case SSL3_ST_SR_NEXT_PROTO_A: |
| 523 | case SSL3_ST_SR_NEXT_PROTO_B: |
| 524 | ret = ssl3_get_next_proto(s); |
| 525 | if (ret <= 0) { |
| 526 | goto end; |
| 527 | } |
| 528 | s->init_num = 0; |
| 529 | if (s->s3->tlsext_channel_id_valid) { |
| 530 | s->state = SSL3_ST_SR_CHANNEL_ID_A; |
| 531 | } else { |
| 532 | s->state = SSL3_ST_SR_FINISHED_A; |
| 533 | } |
| 534 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 535 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 536 | case SSL3_ST_SR_CHANNEL_ID_A: |
| 537 | case SSL3_ST_SR_CHANNEL_ID_B: |
| 538 | ret = ssl3_get_channel_id(s); |
| 539 | if (ret <= 0) { |
| 540 | goto end; |
| 541 | } |
| 542 | s->init_num = 0; |
| 543 | s->state = SSL3_ST_SR_FINISHED_A; |
| 544 | break; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 545 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 546 | case SSL3_ST_SR_FINISHED_A: |
| 547 | case SSL3_ST_SR_FINISHED_B: |
| 548 | ret = |
| 549 | ssl3_get_finished(s, SSL3_ST_SR_FINISHED_A, SSL3_ST_SR_FINISHED_B); |
| 550 | if (ret <= 0) { |
| 551 | goto end; |
| 552 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 553 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 554 | if (s->hit) { |
| 555 | s->state = SSL_ST_OK; |
| 556 | } else if (s->tlsext_ticket_expected) { |
| 557 | s->state = SSL3_ST_SW_SESSION_TICKET_A; |
| 558 | } else { |
| 559 | s->state = SSL3_ST_SW_CHANGE_A; |
| 560 | } |
| 561 | /* If this is a full handshake with ChannelID then record the hashshake |
| 562 | * hashes in |s->session| in case we need them to verify a ChannelID |
| 563 | * signature on a resumption of this session in the future. */ |
| 564 | if (!s->hit && s->s3->tlsext_channel_id_new) { |
| 565 | ret = tls1_record_handshake_hashes_for_channel_id(s); |
| 566 | if (ret <= 0) { |
| 567 | goto end; |
| 568 | } |
| 569 | } |
| 570 | s->init_num = 0; |
| 571 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 572 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 573 | case SSL3_ST_SW_SESSION_TICKET_A: |
| 574 | case SSL3_ST_SW_SESSION_TICKET_B: |
| 575 | ret = ssl3_send_new_session_ticket(s); |
| 576 | if (ret <= 0) { |
| 577 | goto end; |
| 578 | } |
| 579 | s->state = SSL3_ST_SW_CHANGE_A; |
| 580 | s->init_num = 0; |
| 581 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 582 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 583 | case SSL3_ST_SW_CHANGE_A: |
| 584 | case SSL3_ST_SW_CHANGE_B: |
| 585 | s->session->cipher = s->s3->tmp.new_cipher; |
| 586 | if (!s->enc_method->setup_key_block(s)) { |
| 587 | ret = -1; |
| 588 | goto end; |
| 589 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 590 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 591 | ret = ssl3_send_change_cipher_spec(s, SSL3_ST_SW_CHANGE_A, |
| 592 | SSL3_ST_SW_CHANGE_B); |
| 593 | if (ret <= 0) { |
| 594 | goto end; |
| 595 | } |
| 596 | s->state = SSL3_ST_SW_FINISHED_A; |
| 597 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 598 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 599 | if (!s->enc_method->change_cipher_state( |
| 600 | s, SSL3_CHANGE_CIPHER_SERVER_WRITE)) { |
| 601 | ret = -1; |
| 602 | goto end; |
| 603 | } |
| 604 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 605 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 606 | case SSL3_ST_SW_FINISHED_A: |
| 607 | case SSL3_ST_SW_FINISHED_B: |
| 608 | ret = |
| 609 | ssl3_send_finished(s, SSL3_ST_SW_FINISHED_A, SSL3_ST_SW_FINISHED_B, |
| 610 | s->enc_method->server_finished_label, |
| 611 | s->enc_method->server_finished_label_len); |
| 612 | if (ret <= 0) { |
| 613 | goto end; |
| 614 | } |
| 615 | s->state = SSL3_ST_SW_FLUSH; |
| 616 | if (s->hit) { |
| 617 | s->s3->tmp.next_state = SSL3_ST_SR_CHANGE; |
| 618 | } else { |
| 619 | s->s3->tmp.next_state = SSL_ST_OK; |
| 620 | } |
| 621 | s->init_num = 0; |
| 622 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 623 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 624 | case SSL_ST_OK: |
| 625 | /* clean a few things up */ |
| 626 | ssl3_cleanup_key_block(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 627 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 628 | BUF_MEM_free(s->init_buf); |
| 629 | s->init_buf = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 630 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 631 | /* remove buffering on output */ |
| 632 | ssl_free_wbio_buffer(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 633 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 634 | s->init_num = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 635 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 636 | /* If we aren't retaining peer certificates then we can discard it |
| 637 | * now. */ |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 638 | if (s->ctx->retain_only_sha256_of_client_certs) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 639 | X509_free(s->session->peer); |
| 640 | s->session->peer = NULL; |
| 641 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 642 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 643 | if (s->renegotiate == 2) { |
| 644 | /* skipped if we just sent a HelloRequest */ |
| 645 | s->renegotiate = 0; |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 646 | s->s3->initial_handshake_complete = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 647 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 648 | ssl_update_cache(s, SSL_SESS_CACHE_SERVER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 649 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 650 | if (cb != NULL) { |
| 651 | cb(s, SSL_CB_HANDSHAKE_DONE, 1); |
| 652 | } |
| 653 | } |
Adam Langley | 7587253 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 654 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 655 | ret = 1; |
| 656 | goto end; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 657 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 658 | default: |
| 659 | OPENSSL_PUT_ERROR(SSL, ssl3_accept, SSL_R_UNKNOWN_STATE); |
| 660 | ret = -1; |
| 661 | goto end; |
| 662 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 663 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 664 | if (!s->s3->tmp.reuse_message && !skip && cb != NULL && s->state != state) { |
| 665 | new_state = s->state; |
| 666 | s->state = state; |
| 667 | cb(s, SSL_CB_ACCEPT_LOOP, 1); |
| 668 | s->state = new_state; |
| 669 | } |
| 670 | skip = 0; |
| 671 | } |
| 672 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 673 | end: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 674 | s->in_handshake--; |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 675 | BUF_MEM_free(buf); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 676 | if (cb != NULL) { |
| 677 | cb(s, SSL_CB_ACCEPT_EXIT, ret); |
| 678 | } |
| 679 | return ret; |
| 680 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 681 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 682 | static int ssl3_read_sniff_buffer(SSL *s, size_t n) { |
| 683 | if (s->s3->sniff_buffer == NULL) { |
| 684 | s->s3->sniff_buffer = BUF_MEM_new(); |
| 685 | } |
| 686 | if (s->s3->sniff_buffer == NULL || !BUF_MEM_grow(s->s3->sniff_buffer, n)) { |
| 687 | return -1; |
| 688 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 689 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 690 | while (s->s3->sniff_buffer_len < n) { |
| 691 | int ret; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 692 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 693 | s->rwstate = SSL_READING; |
| 694 | ret = BIO_read(s->rbio, s->s3->sniff_buffer->data + s->s3->sniff_buffer_len, |
| 695 | n - s->s3->sniff_buffer_len); |
| 696 | if (ret <= 0) { |
| 697 | return ret; |
| 698 | } |
| 699 | s->rwstate = SSL_NOTHING; |
| 700 | s->s3->sniff_buffer_len += ret; |
| 701 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 702 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 703 | return 1; |
| 704 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 705 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 706 | int ssl3_get_initial_bytes(SSL *s) { |
| 707 | int ret; |
| 708 | const uint8_t *p; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 709 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 710 | /* Read the first 8 bytes. To recognize a ClientHello or V2ClientHello only |
| 711 | * needs the first 6 bytes, but 8 is needed to recognize CONNECT below. */ |
| 712 | ret = ssl3_read_sniff_buffer(s, INITIAL_SNIFF_BUFFER_SIZE); |
| 713 | if (ret <= 0) { |
| 714 | return ret; |
| 715 | } |
| 716 | assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE); |
| 717 | p = (const uint8_t *)s->s3->sniff_buffer->data; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 718 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 719 | /* Some dedicated error codes for protocol mixups should the application wish |
| 720 | * to interpret them differently. (These do not overlap with ClientHello or |
| 721 | * V2ClientHello.) */ |
| 722 | if (strncmp("GET ", (const char *)p, 4) == 0 || |
| 723 | strncmp("POST ", (const char *)p, 5) == 0 || |
| 724 | strncmp("HEAD ", (const char *)p, 5) == 0 || |
| 725 | strncmp("PUT ", (const char *)p, 4) == 0) { |
| 726 | OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTP_REQUEST); |
| 727 | return -1; |
| 728 | } |
| 729 | if (strncmp("CONNECT ", (const char *)p, 8) == 0) { |
| 730 | OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_HTTPS_PROXY_REQUEST); |
| 731 | return -1; |
| 732 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 733 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 734 | /* Determine if this is a ClientHello or V2ClientHello. */ |
| 735 | if ((p[0] & 0x80) && p[2] == SSL2_MT_CLIENT_HELLO && |
| 736 | p[3] >= SSL3_VERSION_MAJOR) { |
| 737 | /* This is a V2ClientHello. */ |
| 738 | s->state = SSL3_ST_SR_V2_CLIENT_HELLO; |
| 739 | return 1; |
| 740 | } |
| 741 | if (p[0] == SSL3_RT_HANDSHAKE && p[1] >= SSL3_VERSION_MAJOR && |
| 742 | p[5] == SSL3_MT_CLIENT_HELLO) { |
| 743 | /* This is a ClientHello. Initialize the record layer with the already |
| 744 | * consumed data and continue the handshake. */ |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 745 | if (!ssl3_setup_read_buffer(s)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 746 | return -1; |
| 747 | } |
| 748 | assert(s->rstate == SSL_ST_READ_HEADER); |
David Benjamin | 6a08da2 | 2015-05-08 22:58:12 -0400 | [diff] [blame] | 749 | /* There cannot have already been data in the record layer. */ |
| 750 | assert(s->s3->rbuf.left == 0); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 751 | memcpy(s->s3->rbuf.buf, p, s->s3->sniff_buffer_len); |
| 752 | s->s3->rbuf.offset = 0; |
| 753 | s->s3->rbuf.left = s->s3->sniff_buffer_len; |
| 754 | s->packet_length = 0; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 755 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 756 | BUF_MEM_free(s->s3->sniff_buffer); |
| 757 | s->s3->sniff_buffer = NULL; |
| 758 | s->s3->sniff_buffer_len = 0; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 759 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 760 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 761 | return 1; |
| 762 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 763 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 764 | OPENSSL_PUT_ERROR(SSL, ssl3_get_initial_bytes, SSL_R_UNKNOWN_PROTOCOL); |
| 765 | return -1; |
| 766 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 767 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 768 | int ssl3_get_v2_client_hello(SSL *s) { |
| 769 | const uint8_t *p; |
| 770 | int ret; |
| 771 | CBS v2_client_hello, cipher_specs, session_id, challenge; |
| 772 | size_t msg_length, rand_len, len; |
| 773 | uint8_t msg_type; |
| 774 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 775 | CBB client_hello, hello_body, cipher_suites; |
| 776 | uint8_t random[SSL3_RANDOM_SIZE]; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 777 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 778 | /* Read the remainder of the V2ClientHello. We have previously read 8 bytes |
| 779 | * in ssl3_get_initial_bytes. */ |
| 780 | assert(s->s3->sniff_buffer_len >= INITIAL_SNIFF_BUFFER_SIZE); |
| 781 | p = (const uint8_t *)s->s3->sniff_buffer->data; |
| 782 | msg_length = ((p[0] & 0x7f) << 8) | p[1]; |
| 783 | if (msg_length > (1024 * 4)) { |
| 784 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE); |
| 785 | return -1; |
| 786 | } |
| 787 | if (msg_length < INITIAL_SNIFF_BUFFER_SIZE - 2) { |
| 788 | /* Reject lengths that are too short early. We have already read 8 bytes, |
| 789 | * so we should not attempt to process an (invalid) V2ClientHello which |
| 790 | * would be shorter than that. */ |
| 791 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, |
| 792 | SSL_R_RECORD_LENGTH_MISMATCH); |
| 793 | return -1; |
| 794 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 795 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 796 | ret = ssl3_read_sniff_buffer(s, msg_length + 2); |
| 797 | if (ret <= 0) { |
| 798 | return ret; |
| 799 | } |
| 800 | assert(s->s3->sniff_buffer_len == msg_length + 2); |
| 801 | CBS_init(&v2_client_hello, (const uint8_t *)s->s3->sniff_buffer->data + 2, |
| 802 | msg_length); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 803 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 804 | /* The V2ClientHello without the length is incorporated into the Finished |
| 805 | * hash. */ |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 806 | if (!ssl3_finish_mac(s, CBS_data(&v2_client_hello), |
| 807 | CBS_len(&v2_client_hello))) { |
| 808 | return -1; |
| 809 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 810 | if (s->msg_callback) { |
| 811 | s->msg_callback(0, SSL2_VERSION, 0, CBS_data(&v2_client_hello), |
| 812 | CBS_len(&v2_client_hello), s, s->msg_callback_arg); |
| 813 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 814 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 815 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 816 | !CBS_get_u16(&v2_client_hello, &version) || |
| 817 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 818 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 819 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 820 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 821 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 822 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 823 | CBS_len(&v2_client_hello) != 0) { |
| 824 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR); |
| 825 | return -1; |
| 826 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 827 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 828 | /* msg_type has already been checked. */ |
| 829 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 831 | /* The client_random is the V2ClientHello challenge. Truncate or |
| 832 | * left-pad with zeros as needed. */ |
| 833 | memset(random, 0, SSL3_RANDOM_SIZE); |
| 834 | rand_len = CBS_len(&challenge); |
| 835 | if (rand_len > SSL3_RANDOM_SIZE) { |
| 836 | rand_len = SSL3_RANDOM_SIZE; |
| 837 | } |
| 838 | memcpy(random + (SSL3_RANDOM_SIZE - rand_len), CBS_data(&challenge), |
| 839 | rand_len); |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 840 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 841 | /* Write out an equivalent SSLv3 ClientHello. */ |
| 842 | if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, |
| 843 | s->init_buf->max)) { |
| 844 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_MALLOC_FAILURE); |
| 845 | return -1; |
| 846 | } |
| 847 | if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || |
| 848 | !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || |
| 849 | !CBB_add_u16(&hello_body, version) || |
| 850 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 851 | /* No session id. */ |
| 852 | !CBB_add_u8(&hello_body, 0) || |
| 853 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) { |
| 854 | CBB_cleanup(&client_hello); |
| 855 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 856 | return -1; |
| 857 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 858 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 859 | /* Copy the cipher suites. */ |
| 860 | while (CBS_len(&cipher_specs) > 0) { |
| 861 | uint32_t cipher_spec; |
| 862 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) { |
| 863 | CBB_cleanup(&client_hello); |
| 864 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, SSL_R_DECODE_ERROR); |
| 865 | return -1; |
| 866 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 867 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 868 | /* Skip SSLv2 ciphers. */ |
| 869 | if ((cipher_spec & 0xff0000) != 0) { |
| 870 | continue; |
| 871 | } |
| 872 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) { |
| 873 | CBB_cleanup(&client_hello); |
| 874 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 875 | return -1; |
| 876 | } |
| 877 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 878 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 879 | /* Add the null compression scheme and finish. */ |
| 880 | if (!CBB_add_u8(&hello_body, 1) || !CBB_add_u8(&hello_body, 0) || |
| 881 | !CBB_finish(&client_hello, NULL, &len)) { |
| 882 | CBB_cleanup(&client_hello); |
| 883 | OPENSSL_PUT_ERROR(SSL, ssl3_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 884 | return -1; |
| 885 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 886 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 887 | /* Mark the message for "re"-use by the version-specific method. */ |
| 888 | s->s3->tmp.reuse_message = 1; |
| 889 | s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO; |
| 890 | /* The handshake message header is 4 bytes. */ |
| 891 | s->s3->tmp.message_size = len - 4; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 892 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 893 | /* Drop the sniff buffer. */ |
| 894 | BUF_MEM_free(s->s3->sniff_buffer); |
| 895 | s->s3->sniff_buffer = NULL; |
| 896 | s->s3->sniff_buffer_len = 0; |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 897 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 898 | return 1; |
| 899 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 900 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 901 | int ssl3_send_hello_request(SSL *s) { |
| 902 | if (s->state == SSL3_ST_SW_HELLO_REQ_A) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 903 | if (!ssl_set_handshake_header(s, SSL3_MT_HELLO_REQUEST, 0)) { |
| 904 | return -1; |
| 905 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 906 | s->state = SSL3_ST_SW_HELLO_REQ_B; |
| 907 | } |
David Benjamin | 4b755cb | 2014-12-12 03:58:07 -0500 | [diff] [blame] | 908 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 909 | /* SSL3_ST_SW_HELLO_REQ_B */ |
| 910 | return ssl_do_write(s); |
| 911 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 912 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 913 | int ssl3_get_client_hello(SSL *s) { |
| 914 | int i, ok, al = SSL_AD_INTERNAL_ERROR, ret = -1; |
| 915 | long n; |
| 916 | const SSL_CIPHER *c; |
| 917 | STACK_OF(SSL_CIPHER) *ciphers = NULL; |
| 918 | struct ssl_early_callback_ctx early_ctx; |
| 919 | CBS client_hello; |
| 920 | uint16_t client_version; |
| 921 | CBS client_random, session_id, cipher_suites, compression_methods; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 922 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 923 | /* We do this so that we will respond with our native type. If we are TLSv1 |
| 924 | * and we get SSLv3, we will respond with TLSv1, This down switching should |
| 925 | * be handled by a different method. If we are SSLv3, we will respond with |
| 926 | * SSLv3, even if prompted with TLSv1. */ |
| 927 | switch (s->state) { |
| 928 | case SSL3_ST_SR_CLNT_HELLO_A: |
| 929 | case SSL3_ST_SR_CLNT_HELLO_B: |
| 930 | n = s->method->ssl_get_message( |
| 931 | s, SSL3_ST_SR_CLNT_HELLO_A, SSL3_ST_SR_CLNT_HELLO_B, |
| 932 | SSL3_MT_CLIENT_HELLO, SSL3_RT_MAX_PLAIN_LENGTH, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 933 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 934 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 935 | if (!ok) { |
| 936 | return n; |
| 937 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 938 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 939 | s->state = SSL3_ST_SR_CLNT_HELLO_C; |
| 940 | /* fallthrough */ |
| 941 | case SSL3_ST_SR_CLNT_HELLO_C: |
| 942 | case SSL3_ST_SR_CLNT_HELLO_D: |
| 943 | /* We have previously parsed the ClientHello message, and can't call |
| 944 | * ssl_get_message again without hashing the message into the Finished |
| 945 | * digest again. */ |
| 946 | n = s->init_num; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 947 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 948 | memset(&early_ctx, 0, sizeof(early_ctx)); |
| 949 | early_ctx.ssl = s; |
| 950 | early_ctx.client_hello = s->init_msg; |
| 951 | early_ctx.client_hello_len = n; |
| 952 | if (!ssl_early_callback_init(&early_ctx)) { |
| 953 | al = SSL_AD_DECODE_ERROR; |
| 954 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 955 | SSL_R_CLIENTHELLO_PARSE_FAILED); |
| 956 | goto f_err; |
| 957 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 958 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 959 | if (s->state == SSL3_ST_SR_CLNT_HELLO_C && |
| 960 | s->ctx->select_certificate_cb != NULL) { |
| 961 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 962 | switch (s->ctx->select_certificate_cb(&early_ctx)) { |
| 963 | case 0: |
David Benjamin | 2fff5bf | 2015-02-09 04:32:28 -0500 | [diff] [blame] | 964 | s->rwstate = SSL_CERTIFICATE_SELECTION_PENDING; |
| 965 | goto err; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 966 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 967 | case -1: |
| 968 | /* Connection rejected. */ |
| 969 | al = SSL_AD_ACCESS_DENIED; |
| 970 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 971 | SSL_R_CONNECTION_REJECTED); |
| 972 | goto f_err; |
Adam Langley | dc9b141 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 973 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 974 | default: |
| 975 | /* fallthrough */; |
| 976 | } |
| 977 | } |
| 978 | s->state = SSL3_ST_SR_CLNT_HELLO_D; |
| 979 | break; |
Adam Langley | b2ce058 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 980 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 981 | default: |
| 982 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNKNOWN_STATE); |
| 983 | return -1; |
| 984 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 985 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 986 | CBS_init(&client_hello, s->init_msg, n); |
| 987 | if (!CBS_get_u16(&client_hello, &client_version) || |
| 988 | !CBS_get_bytes(&client_hello, &client_random, SSL3_RANDOM_SIZE) || |
| 989 | !CBS_get_u8_length_prefixed(&client_hello, &session_id) || |
| 990 | CBS_len(&session_id) > SSL_MAX_SSL_SESSION_ID_LENGTH) { |
| 991 | al = SSL_AD_DECODE_ERROR; |
| 992 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 993 | goto f_err; |
| 994 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 995 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 996 | /* use version from inside client hello, not from record header (may differ: |
| 997 | * see RFC 2246, Appendix E, second paragraph) */ |
| 998 | s->client_version = client_version; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 999 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1000 | /* Load the client random. */ |
| 1001 | memcpy(s->s3->client_random, CBS_data(&client_random), SSL3_RANDOM_SIZE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1002 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1003 | if (SSL_IS_DTLS(s)) { |
| 1004 | CBS cookie; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1005 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1006 | if (!CBS_get_u8_length_prefixed(&client_hello, &cookie) || |
| 1007 | CBS_len(&cookie) > DTLS1_COOKIE_LENGTH) { |
| 1008 | al = SSL_AD_DECODE_ERROR; |
| 1009 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 1010 | goto f_err; |
| 1011 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1012 | } |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 1013 | |
David Benjamin | e6df054 | 2015-05-12 22:02:08 -0400 | [diff] [blame] | 1014 | /* Note: This codepath may run twice if |ssl_get_prev_session| completes |
| 1015 | * asynchronously. |
| 1016 | * |
| 1017 | * TODO(davidben): Clean up the order of events around ClientHello |
| 1018 | * processing. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1019 | if (!s->s3->have_version) { |
| 1020 | /* Select version to use */ |
| 1021 | uint16_t version = ssl3_get_mutual_version(s, client_version); |
| 1022 | if (version == 0) { |
| 1023 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL); |
| 1024 | s->version = s->client_version; |
| 1025 | al = SSL_AD_PROTOCOL_VERSION; |
| 1026 | goto f_err; |
| 1027 | } |
| 1028 | s->version = version; |
| 1029 | s->enc_method = ssl3_get_enc_method(version); |
| 1030 | assert(s->enc_method != NULL); |
| 1031 | /* At this point, the connection's version is known and |s->version| is |
| 1032 | * fixed. Begin enforcing the record-layer version. */ |
| 1033 | s->s3->have_version = 1; |
| 1034 | } else if (SSL_IS_DTLS(s) ? (s->client_version > s->version) |
| 1035 | : (s->client_version < s->version)) { |
| 1036 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_WRONG_VERSION_NUMBER); |
| 1037 | al = SSL_AD_PROTOCOL_VERSION; |
| 1038 | goto f_err; |
| 1039 | } |
David Benjamin | 8b8c006 | 2014-11-23 02:47:52 -0500 | [diff] [blame] | 1040 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1041 | s->hit = 0; |
David Benjamin | 4b27d9f | 2015-05-12 22:42:52 -0400 | [diff] [blame^] | 1042 | if (s->s3->initial_handshake_complete) { |
| 1043 | /* Renegotiations do not participate in session resumption. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1044 | if (!ssl_get_new_session(s, 1)) { |
| 1045 | goto err; |
| 1046 | } |
| 1047 | } else { |
| 1048 | i = ssl_get_prev_session(s, &early_ctx); |
| 1049 | if (i == PENDING_SESSION) { |
David Benjamin | 2fff5bf | 2015-02-09 04:32:28 -0500 | [diff] [blame] | 1050 | s->rwstate = SSL_PENDING_SESSION; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1051 | goto err; |
| 1052 | } else if (i == -1) { |
| 1053 | goto err; |
| 1054 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1055 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1056 | /* Only resume if the session's version matches the negotiated version: |
| 1057 | * most clients do not accept a mismatch. */ |
| 1058 | if (i == 1 && s->version == s->session->ssl_version) { |
| 1059 | s->hit = 1; |
| 1060 | } else { |
| 1061 | /* No session was found or it was unacceptable. */ |
| 1062 | if (!ssl_get_new_session(s, 1)) { |
| 1063 | goto err; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1067 | |
Adam Langley | 524e717 | 2015-02-20 16:04:00 -0800 | [diff] [blame] | 1068 | if (s->ctx->dos_protection_cb != NULL && s->ctx->dos_protection_cb(&early_ctx) == 0) { |
| 1069 | /* Connection rejected for DOS reasons. */ |
| 1070 | al = SSL_AD_ACCESS_DENIED; |
| 1071 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CONNECTION_REJECTED); |
| 1072 | goto f_err; |
| 1073 | } |
| 1074 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1075 | if (!CBS_get_u16_length_prefixed(&client_hello, &cipher_suites) || |
David Benjamin | 93de5e5 | 2015-04-17 22:33:25 -0400 | [diff] [blame] | 1076 | CBS_len(&cipher_suites) == 0 || |
| 1077 | CBS_len(&cipher_suites) % 2 != 0 || |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1078 | !CBS_get_u8_length_prefixed(&client_hello, &compression_methods) || |
| 1079 | CBS_len(&compression_methods) == 0) { |
| 1080 | al = SSL_AD_DECODE_ERROR; |
| 1081 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_DECODE_ERROR); |
| 1082 | goto f_err; |
| 1083 | } |
David Benjamin | 22f9bcc | 2014-07-13 12:29:21 -0400 | [diff] [blame] | 1084 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1085 | ciphers = ssl_bytes_to_cipher_list(s, &cipher_suites); |
| 1086 | if (ciphers == NULL) { |
| 1087 | goto err; |
| 1088 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1089 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1090 | /* If it is a hit, check that the cipher is in the list. */ |
David Benjamin | 93de5e5 | 2015-04-17 22:33:25 -0400 | [diff] [blame] | 1091 | if (s->hit) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1092 | size_t j; |
| 1093 | int found_cipher = 0; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 1094 | uint32_t id = s->session->cipher->id; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1095 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1096 | for (j = 0; j < sk_SSL_CIPHER_num(ciphers); j++) { |
| 1097 | c = sk_SSL_CIPHER_value(ciphers, j); |
| 1098 | if (c->id == id) { |
| 1099 | found_cipher = 1; |
| 1100 | break; |
| 1101 | } |
| 1102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1103 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1104 | if (!found_cipher) { |
| 1105 | /* we need to have the cipher in the cipher list if we are asked to reuse |
| 1106 | * it */ |
| 1107 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1108 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 1109 | SSL_R_REQUIRED_CIPHER_MISSING); |
| 1110 | goto f_err; |
| 1111 | } |
| 1112 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1113 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1114 | /* Only null compression is supported. */ |
| 1115 | if (memchr(CBS_data(&compression_methods), 0, |
| 1116 | CBS_len(&compression_methods)) == NULL) { |
| 1117 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1118 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, |
| 1119 | SSL_R_NO_COMPRESSION_SPECIFIED); |
| 1120 | goto f_err; |
| 1121 | } |
David Benjamin | dc72ff7 | 2014-06-25 12:36:10 -0400 | [diff] [blame] | 1122 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1123 | /* TLS extensions. */ |
| 1124 | if (s->version >= SSL3_VERSION && |
| 1125 | !ssl_parse_clienthello_tlsext(s, &client_hello)) { |
| 1126 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_PARSE_TLSEXT); |
| 1127 | goto err; |
| 1128 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1129 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1130 | /* There should be nothing left over in the record. */ |
| 1131 | if (CBS_len(&client_hello) != 0) { |
| 1132 | /* wrong packet length */ |
| 1133 | al = SSL_AD_DECODE_ERROR; |
| 1134 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_BAD_PACKET_LENGTH); |
| 1135 | goto f_err; |
| 1136 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1137 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1138 | /* Given ciphers and SSL_get_ciphers, we must pick a cipher */ |
| 1139 | if (!s->hit) { |
| 1140 | if (ciphers == NULL) { |
| 1141 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1142 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_CIPHERS_PASSED); |
| 1143 | goto f_err; |
| 1144 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1145 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1146 | /* Let cert callback update server certificates if required */ |
| 1147 | if (s->cert->cert_cb) { |
| 1148 | int rv = s->cert->cert_cb(s, s->cert->cert_cb_arg); |
| 1149 | if (rv == 0) { |
| 1150 | al = SSL_AD_INTERNAL_ERROR; |
| 1151 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_CERT_CB_ERROR); |
| 1152 | goto f_err; |
| 1153 | } |
| 1154 | if (rv < 0) { |
| 1155 | s->rwstate = SSL_X509_LOOKUP; |
| 1156 | goto err; |
| 1157 | } |
| 1158 | s->rwstate = SSL_NOTHING; |
| 1159 | } |
| 1160 | c = ssl3_choose_cipher(s, ciphers, ssl_get_cipher_preferences(s)); |
| 1161 | |
| 1162 | if (c == NULL) { |
| 1163 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1164 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_hello, SSL_R_NO_SHARED_CIPHER); |
| 1165 | goto f_err; |
| 1166 | } |
| 1167 | s->s3->tmp.new_cipher = c; |
| 1168 | } else { |
| 1169 | /* Session-id reuse */ |
| 1170 | s->s3->tmp.new_cipher = s->session->cipher; |
| 1171 | } |
| 1172 | |
| 1173 | if ((!SSL_USE_SIGALGS(s) || !(s->verify_mode & SSL_VERIFY_PEER)) && |
| 1174 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 1175 | goto f_err; |
| 1176 | } |
| 1177 | |
| 1178 | /* we now have the following setup; |
| 1179 | * client_random |
| 1180 | * cipher_list - our prefered list of ciphers |
| 1181 | * ciphers - the clients prefered list of ciphers |
| 1182 | * compression - basically ignored right now |
| 1183 | * ssl version is set - sslv3 |
| 1184 | * s->session - The ssl session has been setup. |
| 1185 | * s->hit - session reuse flag |
| 1186 | * s->tmp.new_cipher - the new cipher to use. */ |
| 1187 | |
| 1188 | if (ret < 0) { |
| 1189 | ret = -ret; |
| 1190 | } |
| 1191 | |
| 1192 | if (0) { |
| 1193 | f_err: |
| 1194 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 1195 | } |
| 1196 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1197 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1198 | sk_SSL_CIPHER_free(ciphers); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1199 | return ret; |
| 1200 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1201 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1202 | int ssl3_send_server_hello(SSL *s) { |
| 1203 | uint8_t *buf; |
| 1204 | uint8_t *p, *d; |
| 1205 | int sl; |
| 1206 | unsigned long l; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1207 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1208 | if (s->state == SSL3_ST_SW_SRVR_HELLO_A) { |
| 1209 | /* We only accept ChannelIDs on connections with ECDHE in order to avoid a |
| 1210 | * known attack while we fix ChannelID itself. */ |
| 1211 | if (s->s3->tlsext_channel_id_valid && |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1212 | (s->s3->tmp.new_cipher->algorithm_mkey & SSL_kECDHE) == 0) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1213 | s->s3->tlsext_channel_id_valid = 0; |
| 1214 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1215 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1216 | /* If this is a resumption and the original handshake didn't support |
| 1217 | * ChannelID then we didn't record the original handshake hashes in the |
| 1218 | * session and so cannot resume with ChannelIDs. */ |
| 1219 | if (s->hit && s->s3->tlsext_channel_id_new && |
| 1220 | s->session->original_handshake_hash_len == 0) { |
| 1221 | s->s3->tlsext_channel_id_valid = 0; |
| 1222 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1223 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1224 | buf = (uint8_t *)s->init_buf->data; |
| 1225 | /* Do the message type and length last */ |
| 1226 | d = p = ssl_handshake_start(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1227 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1228 | *(p++) = s->version >> 8; |
| 1229 | *(p++) = s->version & 0xff; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1230 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1231 | /* Random stuff */ |
| 1232 | if (!ssl_fill_hello_random(s, 1, s->s3->server_random, SSL3_RANDOM_SIZE)) { |
| 1233 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1234 | return -1; |
| 1235 | } |
| 1236 | memcpy(p, s->s3->server_random, SSL3_RANDOM_SIZE); |
| 1237 | p += SSL3_RANDOM_SIZE; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1238 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1239 | /* There are several cases for the session ID to send |
| 1240 | * back in the server hello: |
| 1241 | * - For session reuse from the session cache, we send back the old session |
| 1242 | * ID. |
| 1243 | * - If stateless session reuse (using a session ticket) is successful, we |
| 1244 | * send back the client's "session ID" (which doesn't actually identify |
| 1245 | * the session). |
| 1246 | * - If it is a new session, we send back the new session ID. |
| 1247 | * - However, if we want the new session to be single-use, we send back a |
| 1248 | * 0-length session ID. |
| 1249 | * s->hit is non-zero in either case of session reuse, so the following |
| 1250 | * won't overwrite an ID that we're supposed to send back. */ |
| 1251 | if (!(s->ctx->session_cache_mode & SSL_SESS_CACHE_SERVER) && !s->hit) { |
| 1252 | s->session->session_id_length = 0; |
| 1253 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1254 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1255 | sl = s->session->session_id_length; |
| 1256 | if (sl > (int)sizeof(s->session->session_id)) { |
| 1257 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1258 | return -1; |
| 1259 | } |
| 1260 | *(p++) = sl; |
| 1261 | memcpy(p, s->session->session_id, sl); |
| 1262 | p += sl; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1263 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1264 | /* put the cipher */ |
| 1265 | s2n(ssl3_get_cipher_value(s->s3->tmp.new_cipher), p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1266 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1267 | /* put the compression method */ |
| 1268 | *(p++) = 0; |
| 1269 | if (ssl_prepare_serverhello_tlsext(s) <= 0) { |
| 1270 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, SSL_R_SERVERHELLO_TLSEXT); |
| 1271 | return -1; |
| 1272 | } |
| 1273 | p = ssl_add_serverhello_tlsext(s, p, buf + SSL3_RT_MAX_PLAIN_LENGTH); |
| 1274 | if (p == NULL) { |
| 1275 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_hello, ERR_R_INTERNAL_ERROR); |
| 1276 | return -1; |
| 1277 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1278 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1279 | /* do the header */ |
| 1280 | l = (p - d); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1281 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_HELLO, l)) { |
| 1282 | return -1; |
| 1283 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1284 | s->state = SSL3_ST_SW_SRVR_HELLO_B; |
| 1285 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1286 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1287 | /* SSL3_ST_SW_SRVR_HELLO_B */ |
| 1288 | return ssl_do_write(s); |
| 1289 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1290 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1291 | int ssl3_send_server_done(SSL *s) { |
| 1292 | if (s->state == SSL3_ST_SW_SRVR_DONE_A) { |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1293 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_DONE, 0)) { |
| 1294 | return -1; |
| 1295 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1296 | s->state = SSL3_ST_SW_SRVR_DONE_B; |
| 1297 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1298 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1299 | /* SSL3_ST_SW_SRVR_DONE_B */ |
| 1300 | return ssl_do_write(s); |
| 1301 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1302 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1303 | int ssl3_send_server_key_exchange(SSL *s) { |
| 1304 | DH *dh = NULL, *dhp; |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1305 | EC_KEY *ecdh = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1306 | uint8_t *encodedPoint = NULL; |
| 1307 | int encodedlen = 0; |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 1308 | uint16_t curve_id = 0; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1309 | BN_CTX *bn_ctx = NULL; |
| 1310 | const char *psk_identity_hint = NULL; |
| 1311 | size_t psk_identity_hint_len = 0; |
| 1312 | EVP_PKEY *pkey; |
| 1313 | uint8_t *p, *d; |
| 1314 | int al, i; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 1315 | uint32_t alg_k; |
| 1316 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1317 | int n; |
| 1318 | CERT *cert; |
| 1319 | BIGNUM *r[4]; |
| 1320 | int nr[4], kn; |
| 1321 | BUF_MEM *buf; |
| 1322 | EVP_MD_CTX md_ctx; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1323 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1324 | EVP_MD_CTX_init(&md_ctx); |
| 1325 | if (s->state == SSL3_ST_SW_KEY_EXCH_A) { |
| 1326 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1327 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
| 1328 | cert = s->cert; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1329 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1330 | buf = s->init_buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1331 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1332 | r[0] = r[1] = r[2] = r[3] = NULL; |
| 1333 | n = 0; |
| 1334 | if (alg_a & SSL_aPSK) { |
| 1335 | /* size for PSK identity hint */ |
| 1336 | psk_identity_hint = s->psk_identity_hint; |
| 1337 | if (psk_identity_hint) { |
| 1338 | psk_identity_hint_len = strlen(psk_identity_hint); |
| 1339 | } else { |
| 1340 | psk_identity_hint_len = 0; |
| 1341 | } |
| 1342 | n += 2 + psk_identity_hint_len; |
| 1343 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1344 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1345 | if (alg_k & SSL_kDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1346 | dhp = cert->dh_tmp; |
| 1347 | if (dhp == NULL && s->cert->dh_tmp_cb != NULL) { |
| 1348 | dhp = s->cert->dh_tmp_cb(s, 0, 1024); |
| 1349 | } |
| 1350 | if (dhp == NULL) { |
| 1351 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1352 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1353 | SSL_R_MISSING_TMP_DH_KEY); |
| 1354 | goto f_err; |
| 1355 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1356 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1357 | if (s->s3->tmp.dh != NULL) { |
| 1358 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1359 | ERR_R_INTERNAL_ERROR); |
| 1360 | goto err; |
| 1361 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1362 | dh = DHparams_dup(dhp); |
| 1363 | if (dh == NULL) { |
| 1364 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1365 | goto err; |
| 1366 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1367 | s->s3->tmp.dh = dh; |
David Benjamin | 9f226a5 | 2015-04-26 22:12:32 -0400 | [diff] [blame] | 1368 | |
| 1369 | if (!DH_generate_key(dh)) { |
| 1370 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_DH_LIB); |
| 1371 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1372 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1373 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1374 | r[0] = dh->p; |
| 1375 | r[1] = dh->g; |
| 1376 | r[2] = dh->pub_key; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1377 | } else if (alg_k & SSL_kECDHE) { |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1378 | /* Determine the curve to use. */ |
| 1379 | int nid = NID_undef; |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 1380 | if (cert->ecdh_nid != NID_undef) { |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1381 | nid = cert->ecdh_nid; |
| 1382 | } else if (cert->ecdh_tmp_cb != NULL) { |
| 1383 | /* Note: |ecdh_tmp_cb| does NOT pass ownership of the result |
| 1384 | * to the caller. */ |
| 1385 | EC_KEY *template = s->cert->ecdh_tmp_cb(s, 0, 1024); |
| 1386 | if (template != NULL && EC_KEY_get0_group(template) != NULL) { |
| 1387 | nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(template)); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1388 | } |
David Benjamin | dd97878 | 2015-04-24 15:20:13 -0400 | [diff] [blame] | 1389 | } else { |
| 1390 | nid = tls1_get_shared_curve(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1391 | } |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1392 | if (nid == NID_undef) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1393 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1394 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1395 | SSL_R_MISSING_TMP_ECDH_KEY); |
| 1396 | goto f_err; |
| 1397 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1398 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1399 | if (s->s3->tmp.ecdh != NULL) { |
| 1400 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1401 | ERR_R_INTERNAL_ERROR); |
| 1402 | goto err; |
| 1403 | } |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1404 | ecdh = EC_KEY_new_by_curve_name(nid); |
| 1405 | if (ecdh == NULL) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1406 | goto err; |
| 1407 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1408 | s->s3->tmp.ecdh = ecdh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1409 | |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1410 | if (!EC_KEY_generate_key(ecdh)) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1411 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1412 | goto err; |
| 1413 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1414 | |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 1415 | /* We only support ephemeral ECDH keys over named (not generic) curves. */ |
David Benjamin | c0f763b | 2015-03-27 02:05:39 -0400 | [diff] [blame] | 1416 | const EC_GROUP *group = EC_KEY_get0_group(ecdh); |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 1417 | if (!tls1_ec_nid2curve_id(&curve_id, EC_GROUP_get_curve_name(group))) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1418 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1419 | SSL_R_UNSUPPORTED_ELLIPTIC_CURVE); |
| 1420 | goto err; |
| 1421 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1422 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1423 | /* Encode the public key. First check the size of encoding and allocate |
| 1424 | * memory accordingly. */ |
| 1425 | encodedlen = |
| 1426 | EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), |
| 1427 | POINT_CONVERSION_UNCOMPRESSED, NULL, 0, NULL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1428 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1429 | encodedPoint = (uint8_t *)OPENSSL_malloc(encodedlen * sizeof(uint8_t)); |
| 1430 | bn_ctx = BN_CTX_new(); |
| 1431 | if (encodedPoint == NULL || bn_ctx == NULL) { |
| 1432 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1433 | ERR_R_MALLOC_FAILURE); |
| 1434 | goto err; |
| 1435 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1436 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1437 | encodedlen = EC_POINT_point2oct(group, EC_KEY_get0_public_key(ecdh), |
| 1438 | POINT_CONVERSION_UNCOMPRESSED, |
| 1439 | encodedPoint, encodedlen, bn_ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1440 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1441 | if (encodedlen == 0) { |
| 1442 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_R_ECDH_LIB); |
| 1443 | goto err; |
| 1444 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1445 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1446 | BN_CTX_free(bn_ctx); |
| 1447 | bn_ctx = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1448 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1449 | /* We only support named (not generic) curves in ECDH ephemeral key |
| 1450 | * exchanges. In this situation, we need four additional bytes to encode |
| 1451 | * the entire ServerECDHParams structure. */ |
| 1452 | n += 4 + encodedlen; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1453 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1454 | /* We'll generate the serverKeyExchange message explicitly so we can set |
| 1455 | * these to NULLs */ |
| 1456 | r[0] = NULL; |
| 1457 | r[1] = NULL; |
| 1458 | r[2] = NULL; |
| 1459 | r[3] = NULL; |
| 1460 | } else if (!(alg_k & SSL_kPSK)) { |
| 1461 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1462 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1463 | SSL_R_UNKNOWN_KEY_EXCHANGE_TYPE); |
| 1464 | goto f_err; |
| 1465 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1466 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1467 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
| 1468 | nr[i] = BN_num_bytes(r[i]); |
| 1469 | n += 2 + nr[i]; |
| 1470 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1471 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1472 | if (ssl_cipher_has_server_public_key(s->s3->tmp.new_cipher)) { |
| 1473 | pkey = ssl_get_sign_pkey(s, s->s3->tmp.new_cipher); |
| 1474 | if (pkey == NULL) { |
| 1475 | al = SSL_AD_DECODE_ERROR; |
| 1476 | goto f_err; |
| 1477 | } |
| 1478 | kn = EVP_PKEY_size(pkey); |
| 1479 | } else { |
| 1480 | pkey = NULL; |
| 1481 | kn = 0; |
| 1482 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1483 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1484 | if (!BUF_MEM_grow_clean(buf, n + SSL_HM_HEADER_LENGTH(s) + kn)) { |
| 1485 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_BUF); |
| 1486 | goto err; |
| 1487 | } |
| 1488 | d = p = ssl_handshake_start(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1489 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1490 | for (i = 0; i < 4 && r[i] != NULL; i++) { |
| 1491 | s2n(nr[i], p); |
| 1492 | BN_bn2bin(r[i], p); |
| 1493 | p += nr[i]; |
| 1494 | } |
David Benjamin | 00505ec | 2014-11-26 16:38:00 -0500 | [diff] [blame] | 1495 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1496 | /* Note: ECDHE PSK ciphersuites use SSL_kECDHE and SSL_aPSK. When one of |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1497 | * them is used, the server key exchange record needs to have both the |
| 1498 | * psk_identity_hint and the ServerECDHParams. */ |
| 1499 | if (alg_a & SSL_aPSK) { |
| 1500 | /* copy PSK identity hint (if provided) */ |
| 1501 | s2n(psk_identity_hint_len, p); |
| 1502 | if (psk_identity_hint_len > 0) { |
| 1503 | memcpy(p, psk_identity_hint, psk_identity_hint_len); |
| 1504 | p += psk_identity_hint_len; |
| 1505 | } |
| 1506 | } |
David Benjamin | 00505ec | 2014-11-26 16:38:00 -0500 | [diff] [blame] | 1507 | |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1508 | if (alg_k & SSL_kECDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1509 | /* We only support named (not generic) curves. In this situation, the |
| 1510 | * serverKeyExchange message has: |
| 1511 | * [1 byte CurveType], [2 byte CurveName] |
| 1512 | * [1 byte length of encoded point], followed by |
| 1513 | * the actual encoded point itself. */ |
David Benjamin | 70bd80a | 2014-12-27 03:06:46 -0500 | [diff] [blame] | 1514 | *(p++) = NAMED_CURVE_TYPE; |
| 1515 | *(p++) = (uint8_t)(curve_id >> 8); |
| 1516 | *(p++) = (uint8_t)(curve_id & 0xff); |
| 1517 | *(p++) = encodedlen; |
| 1518 | memcpy(p, encodedPoint, encodedlen); |
| 1519 | p += encodedlen; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1520 | OPENSSL_free(encodedPoint); |
| 1521 | encodedPoint = NULL; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1523 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1524 | /* not anonymous */ |
| 1525 | if (pkey != NULL) { |
| 1526 | /* n is the length of the params, they start at &(d[4]) and p points to |
| 1527 | * the space at the end. */ |
| 1528 | const EVP_MD *md; |
| 1529 | size_t sig_len = EVP_PKEY_size(pkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1530 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1531 | /* Determine signature algorithm. */ |
| 1532 | if (SSL_USE_SIGALGS(s)) { |
| 1533 | md = tls1_choose_signing_digest(s, pkey); |
| 1534 | if (!tls12_get_sigandhash(p, pkey, md)) { |
| 1535 | /* Should never happen */ |
| 1536 | al = SSL_AD_INTERNAL_ERROR; |
| 1537 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, |
| 1538 | ERR_R_INTERNAL_ERROR); |
| 1539 | goto f_err; |
| 1540 | } |
| 1541 | p += 2; |
| 1542 | } else if (pkey->type == EVP_PKEY_RSA) { |
| 1543 | md = EVP_md5_sha1(); |
| 1544 | } else { |
| 1545 | md = EVP_sha1(); |
| 1546 | } |
| 1547 | |
| 1548 | if (!EVP_DigestSignInit(&md_ctx, NULL, md, NULL, pkey) || |
| 1549 | !EVP_DigestSignUpdate(&md_ctx, s->s3->client_random, |
| 1550 | SSL3_RANDOM_SIZE) || |
| 1551 | !EVP_DigestSignUpdate(&md_ctx, s->s3->server_random, |
| 1552 | SSL3_RANDOM_SIZE) || |
| 1553 | !EVP_DigestSignUpdate(&md_ctx, d, n) || |
| 1554 | !EVP_DigestSignFinal(&md_ctx, &p[2], &sig_len)) { |
| 1555 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_key_exchange, ERR_LIB_EVP); |
| 1556 | goto err; |
| 1557 | } |
| 1558 | |
| 1559 | s2n(sig_len, p); |
| 1560 | n += sig_len + 2; |
| 1561 | if (SSL_USE_SIGALGS(s)) { |
| 1562 | n += 2; |
| 1563 | } |
| 1564 | } |
| 1565 | |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1566 | if (!ssl_set_handshake_header(s, SSL3_MT_SERVER_KEY_EXCHANGE, n)) { |
| 1567 | goto err; |
| 1568 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | s->state = SSL3_ST_SW_KEY_EXCH_B; |
| 1572 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1573 | return ssl_do_write(s); |
| 1574 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1575 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1576 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1577 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 1578 | OPENSSL_free(encodedPoint); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1579 | BN_CTX_free(bn_ctx); |
| 1580 | EVP_MD_CTX_cleanup(&md_ctx); |
| 1581 | return -1; |
| 1582 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1583 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1584 | int ssl3_send_certificate_request(SSL *s) { |
| 1585 | uint8_t *p, *d; |
| 1586 | size_t i; |
| 1587 | int j, nl, off, n; |
| 1588 | STACK_OF(X509_NAME) *sk = NULL; |
| 1589 | X509_NAME *name; |
| 1590 | BUF_MEM *buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1591 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1592 | if (s->state == SSL3_ST_SW_CERT_REQ_A) { |
| 1593 | buf = s->init_buf; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1594 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1595 | d = p = ssl_handshake_start(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1596 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1597 | /* get the list of acceptable cert types */ |
| 1598 | p++; |
| 1599 | n = ssl3_get_req_cert_type(s, p); |
| 1600 | d[0] = n; |
| 1601 | p += n; |
| 1602 | n++; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1603 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1604 | if (SSL_USE_SIGALGS(s)) { |
| 1605 | const uint8_t *psigs; |
| 1606 | nl = tls12_get_psigalgs(s, &psigs); |
| 1607 | s2n(nl, p); |
| 1608 | memcpy(p, psigs, nl); |
| 1609 | p += nl; |
| 1610 | n += nl + 2; |
| 1611 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1612 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1613 | off = n; |
| 1614 | p += 2; |
| 1615 | n += 2; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1616 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1617 | sk = SSL_get_client_CA_list(s); |
| 1618 | nl = 0; |
| 1619 | if (sk != NULL) { |
| 1620 | for (i = 0; i < sk_X509_NAME_num(sk); i++) { |
| 1621 | name = sk_X509_NAME_value(sk, i); |
| 1622 | j = i2d_X509_NAME(name, NULL); |
| 1623 | if (!BUF_MEM_grow_clean(buf, SSL_HM_HEADER_LENGTH(s) + n + j + 2)) { |
| 1624 | OPENSSL_PUT_ERROR(SSL, ssl3_send_certificate_request, ERR_R_BUF_LIB); |
| 1625 | goto err; |
| 1626 | } |
| 1627 | p = ssl_handshake_start(s) + n; |
| 1628 | s2n(j, p); |
| 1629 | i2d_X509_NAME(name, &p); |
| 1630 | n += 2 + j; |
| 1631 | nl += 2 + j; |
| 1632 | } |
| 1633 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1634 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1635 | /* else no CA names */ |
| 1636 | p = ssl_handshake_start(s) + off; |
| 1637 | s2n(nl, p); |
| 1638 | |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 1639 | if (!ssl_set_handshake_header(s, SSL3_MT_CERTIFICATE_REQUEST, n)) { |
| 1640 | goto err; |
| 1641 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1642 | s->state = SSL3_ST_SW_CERT_REQ_B; |
| 1643 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1644 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1645 | /* SSL3_ST_SW_CERT_REQ_B */ |
| 1646 | return ssl_do_write(s); |
| 1647 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1648 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1649 | return -1; |
| 1650 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1651 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1652 | int ssl3_get_client_key_exchange(SSL *s) { |
| 1653 | int al, ok; |
| 1654 | long n; |
| 1655 | CBS client_key_exchange; |
David Benjamin | 3fa2777 | 2015-04-17 22:32:19 -0400 | [diff] [blame] | 1656 | uint32_t alg_k; |
| 1657 | uint32_t alg_a; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1658 | uint8_t *premaster_secret = NULL; |
| 1659 | size_t premaster_secret_len = 0; |
| 1660 | RSA *rsa = NULL; |
| 1661 | uint8_t *decrypt_buf = NULL; |
| 1662 | EVP_PKEY *pkey = NULL; |
| 1663 | BIGNUM *pub = NULL; |
| 1664 | DH *dh_srvr; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1665 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1666 | EC_KEY *srvr_ecdh = NULL; |
| 1667 | EVP_PKEY *clnt_pub_pkey = NULL; |
| 1668 | EC_POINT *clnt_ecpoint = NULL; |
| 1669 | BN_CTX *bn_ctx = NULL; |
| 1670 | unsigned int psk_len = 0; |
| 1671 | uint8_t psk[PSK_MAX_PSK_LEN]; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1672 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1673 | n = s->method->ssl_get_message(s, SSL3_ST_SR_KEY_EXCH_A, |
| 1674 | SSL3_ST_SR_KEY_EXCH_B, |
| 1675 | SSL3_MT_CLIENT_KEY_EXCHANGE, 2048, /* ??? */ |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 1676 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1677 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1678 | if (!ok) { |
| 1679 | return n; |
| 1680 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1681 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1682 | CBS_init(&client_key_exchange, s->init_msg, n); |
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 | alg_k = s->s3->tmp.new_cipher->algorithm_mkey; |
| 1685 | alg_a = s->s3->tmp.new_cipher->algorithm_auth; |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1686 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1687 | /* If using a PSK key exchange, prepare the pre-shared key. */ |
| 1688 | if (alg_a & SSL_aPSK) { |
| 1689 | CBS psk_identity; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1690 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1691 | /* If using PSK, the ClientKeyExchange contains a psk_identity. If PSK, |
| 1692 | * then this is the only field in the message. */ |
| 1693 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &psk_identity) || |
| 1694 | ((alg_k & SSL_kPSK) && CBS_len(&client_key_exchange) != 0)) { |
| 1695 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 1696 | al = SSL_AD_DECODE_ERROR; |
| 1697 | goto f_err; |
| 1698 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1699 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1700 | if (s->psk_server_callback == NULL) { |
| 1701 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1702 | SSL_R_PSK_NO_SERVER_CB); |
| 1703 | al = SSL_AD_INTERNAL_ERROR; |
| 1704 | goto f_err; |
| 1705 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1706 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1707 | if (CBS_len(&psk_identity) > PSK_MAX_IDENTITY_LEN || |
| 1708 | CBS_contains_zero_byte(&psk_identity)) { |
| 1709 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1710 | SSL_R_DATA_LENGTH_TOO_LONG); |
| 1711 | al = SSL_AD_ILLEGAL_PARAMETER; |
| 1712 | goto f_err; |
| 1713 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1714 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1715 | if (!CBS_strdup(&psk_identity, &s->session->psk_identity)) { |
| 1716 | al = SSL_AD_INTERNAL_ERROR; |
| 1717 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1718 | ERR_R_MALLOC_FAILURE); |
| 1719 | goto f_err; |
| 1720 | } |
Adam Langley | c26c802 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1721 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1722 | /* Look up the key for the identity. */ |
| 1723 | psk_len = |
| 1724 | s->psk_server_callback(s, s->session->psk_identity, psk, sizeof(psk)); |
| 1725 | if (psk_len > PSK_MAX_PSK_LEN) { |
| 1726 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1727 | ERR_R_INTERNAL_ERROR); |
| 1728 | al = SSL_AD_INTERNAL_ERROR; |
| 1729 | goto f_err; |
| 1730 | } else if (psk_len == 0) { |
| 1731 | /* PSK related to the given identity not found */ |
| 1732 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1733 | SSL_R_PSK_IDENTITY_NOT_FOUND); |
| 1734 | al = SSL_AD_UNKNOWN_PSK_IDENTITY; |
| 1735 | goto f_err; |
| 1736 | } |
| 1737 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1738 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1739 | /* Depending on the key exchange method, compute |premaster_secret| and |
| 1740 | * |premaster_secret_len|. */ |
| 1741 | if (alg_k & SSL_kRSA) { |
| 1742 | CBS encrypted_premaster_secret; |
| 1743 | uint8_t rand_premaster_secret[SSL_MAX_MASTER_KEY_LENGTH]; |
| 1744 | uint8_t good; |
| 1745 | size_t rsa_size, decrypt_len, premaster_index, j; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1746 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1747 | pkey = s->cert->pkeys[SSL_PKEY_RSA_ENC].privatekey; |
| 1748 | if (pkey == NULL || pkey->type != EVP_PKEY_RSA || pkey->pkey.rsa == NULL) { |
| 1749 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1750 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1751 | SSL_R_MISSING_RSA_CERTIFICATE); |
| 1752 | goto f_err; |
| 1753 | } |
| 1754 | rsa = pkey->pkey.rsa; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1755 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1756 | /* TLS and [incidentally] DTLS{0xFEFF} */ |
| 1757 | if (s->version > SSL3_VERSION) { |
| 1758 | CBS copy = client_key_exchange; |
| 1759 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, |
| 1760 | &encrypted_premaster_secret) || |
| 1761 | CBS_len(&client_key_exchange) != 0) { |
| 1762 | if (!(s->options & SSL_OP_TLS_D5_BUG)) { |
| 1763 | al = SSL_AD_DECODE_ERROR; |
| 1764 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1765 | SSL_R_TLS_RSA_ENCRYPTED_VALUE_LENGTH_IS_WRONG); |
| 1766 | goto f_err; |
| 1767 | } else { |
| 1768 | encrypted_premaster_secret = copy; |
| 1769 | } |
| 1770 | } |
| 1771 | } else { |
| 1772 | encrypted_premaster_secret = client_key_exchange; |
| 1773 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1774 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1775 | /* Reject overly short RSA keys because we want to be sure that the buffer |
| 1776 | * size makes it safe to iterate over the entire size of a premaster secret |
| 1777 | * (SSL_MAX_MASTER_KEY_LENGTH). The actual expected size is larger due to |
| 1778 | * RSA padding, but the bound is sufficient to be safe. */ |
| 1779 | rsa_size = RSA_size(rsa); |
| 1780 | if (rsa_size < SSL_MAX_MASTER_KEY_LENGTH) { |
| 1781 | al = SSL_AD_DECRYPT_ERROR; |
| 1782 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1783 | SSL_R_DECRYPTION_FAILED); |
| 1784 | goto f_err; |
| 1785 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1786 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1787 | /* We must not leak whether a decryption failure occurs because of |
| 1788 | * Bleichenbacher's attack on PKCS #1 v1.5 RSA padding (see RFC 2246, |
| 1789 | * section 7.4.7.1). The code follows that advice of the TLS RFC and |
| 1790 | * generates a random premaster secret for the case that the decrypt fails. |
| 1791 | * See https://tools.ietf.org/html/rfc5246#section-7.4.7.1 */ |
| 1792 | if (!RAND_bytes(rand_premaster_secret, sizeof(rand_premaster_secret))) { |
| 1793 | goto err; |
| 1794 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1795 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1796 | /* Allocate a buffer large enough for an RSA decryption. */ |
| 1797 | decrypt_buf = OPENSSL_malloc(rsa_size); |
| 1798 | if (decrypt_buf == NULL) { |
| 1799 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1800 | ERR_R_MALLOC_FAILURE); |
| 1801 | goto err; |
| 1802 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1803 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1804 | /* Decrypt with no padding. PKCS#1 padding will be removed as part of the |
| 1805 | * timing-sensitive code below. */ |
| 1806 | if (!RSA_decrypt(rsa, &decrypt_len, decrypt_buf, rsa_size, |
| 1807 | CBS_data(&encrypted_premaster_secret), |
| 1808 | CBS_len(&encrypted_premaster_secret), RSA_NO_PADDING)) { |
| 1809 | goto err; |
| 1810 | } |
| 1811 | if (decrypt_len != rsa_size) { |
| 1812 | /* This should never happen, but do a check so we do not read |
| 1813 | * uninitialized memory. */ |
| 1814 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1815 | ERR_R_INTERNAL_ERROR); |
| 1816 | goto err; |
| 1817 | } |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1818 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1819 | /* Remove the PKCS#1 padding and adjust |decrypt_len| as appropriate. |
| 1820 | * |good| will be 0xff if the premaster is acceptable and zero otherwise. |
| 1821 | * */ |
| 1822 | good = |
| 1823 | constant_time_eq_int_8(RSA_message_index_PKCS1_type_2( |
| 1824 | decrypt_buf, decrypt_len, &premaster_index), |
| 1825 | 1); |
| 1826 | decrypt_len = decrypt_len - premaster_index; |
David Benjamin | 0aa0767 | 2014-07-24 15:34:14 -0400 | [diff] [blame] | 1827 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1828 | /* decrypt_len should be SSL_MAX_MASTER_KEY_LENGTH. */ |
| 1829 | good &= constant_time_eq_8(decrypt_len, SSL_MAX_MASTER_KEY_LENGTH); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1830 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1831 | /* Copy over the unpadded premaster. Whatever the value of |
| 1832 | * |decrypt_good_mask|, copy as if the premaster were the right length. It |
| 1833 | * is important the memory access pattern be constant. */ |
| 1834 | premaster_secret = |
| 1835 | BUF_memdup(decrypt_buf + (rsa_size - SSL_MAX_MASTER_KEY_LENGTH), |
| 1836 | SSL_MAX_MASTER_KEY_LENGTH); |
| 1837 | if (premaster_secret == NULL) { |
| 1838 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1839 | ERR_R_MALLOC_FAILURE); |
| 1840 | goto err; |
| 1841 | } |
| 1842 | OPENSSL_free(decrypt_buf); |
| 1843 | decrypt_buf = NULL; |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1844 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1845 | /* If the version in the decrypted pre-master secret is correct then |
| 1846 | * version_good will be 0xff, otherwise it'll be zero. The |
| 1847 | * Klima-Pokorny-Rosa extension of Bleichenbacher's attack |
| 1848 | * (http://eprint.iacr.org/2003/052/) exploits the version number check as |
| 1849 | * a "bad version oracle". Thus version checks are done in constant time |
| 1850 | * and are treated like any other decryption error. */ |
| 1851 | good &= constant_time_eq_8(premaster_secret[0], |
| 1852 | (unsigned)(s->client_version >> 8)); |
| 1853 | good &= constant_time_eq_8(premaster_secret[1], |
| 1854 | (unsigned)(s->client_version & 0xff)); |
Adam Langley | acff73f | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1855 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1856 | /* Now copy rand_premaster_secret over premaster_secret using |
| 1857 | * decrypt_good_mask. */ |
| 1858 | for (j = 0; j < sizeof(rand_premaster_secret); j++) { |
| 1859 | premaster_secret[j] = constant_time_select_8(good, premaster_secret[j], |
| 1860 | rand_premaster_secret[j]); |
| 1861 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1862 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1863 | premaster_secret_len = sizeof(rand_premaster_secret); |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1864 | } else if (alg_k & SSL_kDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1865 | CBS dh_Yc; |
| 1866 | int dh_len; |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1867 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1868 | if (!CBS_get_u16_length_prefixed(&client_key_exchange, &dh_Yc) || |
| 1869 | CBS_len(&dh_Yc) == 0 || CBS_len(&client_key_exchange) != 0) { |
| 1870 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1871 | SSL_R_DH_PUBLIC_VALUE_LENGTH_IS_WRONG); |
| 1872 | al = SSL_R_DECODE_ERROR; |
| 1873 | goto f_err; |
| 1874 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1875 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1876 | if (s->s3->tmp.dh == NULL) { |
| 1877 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 1878 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1879 | SSL_R_MISSING_TMP_DH_KEY); |
| 1880 | goto f_err; |
| 1881 | } |
| 1882 | dh_srvr = s->s3->tmp.dh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1883 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1884 | pub = BN_bin2bn(CBS_data(&dh_Yc), CBS_len(&dh_Yc), NULL); |
| 1885 | if (pub == NULL) { |
| 1886 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_BN_LIB); |
| 1887 | goto err; |
| 1888 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1889 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1890 | /* Allocate a buffer for the premaster secret. */ |
| 1891 | premaster_secret = OPENSSL_malloc(DH_size(dh_srvr)); |
| 1892 | if (premaster_secret == NULL) { |
| 1893 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1894 | ERR_R_MALLOC_FAILURE); |
David Benjamin | c35fb01 | 2015-02-12 00:48:45 -0500 | [diff] [blame] | 1895 | BN_clear_free(pub); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1896 | goto err; |
| 1897 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1898 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1899 | dh_len = DH_compute_key(premaster_secret, pub, dh_srvr); |
| 1900 | if (dh_len <= 0) { |
| 1901 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_DH_LIB); |
| 1902 | BN_clear_free(pub); |
| 1903 | goto err; |
| 1904 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 1905 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1906 | DH_free(s->s3->tmp.dh); |
| 1907 | s->s3->tmp.dh = NULL; |
| 1908 | BN_clear_free(pub); |
| 1909 | pub = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1910 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1911 | premaster_secret_len = dh_len; |
David Benjamin | 7061e28 | 2015-03-19 11:10:48 -0400 | [diff] [blame] | 1912 | } else if (alg_k & SSL_kECDHE) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1913 | int field_size = 0, ecdh_len; |
| 1914 | const EC_KEY *tkey; |
| 1915 | const EC_GROUP *group; |
| 1916 | const BIGNUM *priv_key; |
| 1917 | CBS ecdh_Yc; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1918 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1919 | /* initialize structures for server's ECDH key pair */ |
| 1920 | srvr_ecdh = EC_KEY_new(); |
| 1921 | if (srvr_ecdh == NULL) { |
| 1922 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1923 | ERR_R_MALLOC_FAILURE); |
| 1924 | goto err; |
| 1925 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1926 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1927 | /* Use the ephermeral values we saved when generating the ServerKeyExchange |
| 1928 | * msg. */ |
| 1929 | tkey = s->s3->tmp.ecdh; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1930 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1931 | group = EC_KEY_get0_group(tkey); |
| 1932 | priv_key = EC_KEY_get0_private_key(tkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1933 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1934 | if (!EC_KEY_set_group(srvr_ecdh, group) || |
| 1935 | !EC_KEY_set_private_key(srvr_ecdh, priv_key)) { |
| 1936 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 1937 | goto err; |
| 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 | /* Let's get client's public key */ |
| 1941 | clnt_ecpoint = EC_POINT_new(group); |
| 1942 | if (clnt_ecpoint == NULL) { |
| 1943 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1944 | ERR_R_MALLOC_FAILURE); |
| 1945 | goto err; |
| 1946 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1947 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1948 | /* Get client's public key from encoded point in the ClientKeyExchange |
| 1949 | * message. */ |
| 1950 | if (!CBS_get_u8_length_prefixed(&client_key_exchange, &ecdh_Yc) || |
| 1951 | CBS_len(&client_key_exchange) != 0) { |
| 1952 | al = SSL_AD_DECODE_ERROR; |
| 1953 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, SSL_R_DECODE_ERROR); |
| 1954 | goto f_err; |
| 1955 | } |
David Benjamin | bd30f8e | 2014-08-19 16:02:38 -0400 | [diff] [blame] | 1956 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1957 | bn_ctx = BN_CTX_new(); |
| 1958 | if (bn_ctx == NULL) { |
| 1959 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1960 | ERR_R_MALLOC_FAILURE); |
| 1961 | goto err; |
| 1962 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1963 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1964 | if (!EC_POINT_oct2point(group, clnt_ecpoint, CBS_data(&ecdh_Yc), |
| 1965 | CBS_len(&ecdh_Yc), bn_ctx)) { |
| 1966 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_EC_LIB); |
| 1967 | goto err; |
| 1968 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1969 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1970 | /* Allocate a buffer for both the secret and the PSK. */ |
| 1971 | field_size = EC_GROUP_get_degree(group); |
| 1972 | if (field_size <= 0) { |
| 1973 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 1974 | goto err; |
| 1975 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1976 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1977 | ecdh_len = (field_size + 7) / 8; |
| 1978 | premaster_secret = OPENSSL_malloc(ecdh_len); |
| 1979 | if (premaster_secret == NULL) { |
| 1980 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 1981 | ERR_R_MALLOC_FAILURE); |
| 1982 | goto err; |
| 1983 | } |
David Benjamin | 35c0230 | 2014-07-13 04:14:59 -0400 | [diff] [blame] | 1984 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1985 | /* Compute the shared pre-master secret */ |
| 1986 | ecdh_len = ECDH_compute_key(premaster_secret, ecdh_len, clnt_ecpoint, |
| 1987 | srvr_ecdh, NULL); |
| 1988 | if (ecdh_len <= 0) { |
| 1989 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, ERR_R_ECDH_LIB); |
| 1990 | goto err; |
| 1991 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1992 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 1993 | EVP_PKEY_free(clnt_pub_pkey); |
| 1994 | clnt_pub_pkey = NULL; |
| 1995 | EC_POINT_free(clnt_ecpoint); |
| 1996 | clnt_ecpoint = NULL; |
| 1997 | EC_KEY_free(srvr_ecdh); |
| 1998 | srvr_ecdh = NULL; |
| 1999 | BN_CTX_free(bn_ctx); |
| 2000 | bn_ctx = NULL; |
| 2001 | EC_KEY_free(s->s3->tmp.ecdh); |
| 2002 | s->s3->tmp.ecdh = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2003 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2004 | premaster_secret_len = ecdh_len; |
| 2005 | } else if (alg_k & SSL_kPSK) { |
| 2006 | /* For plain PSK, other_secret is a block of 0s with the same length as the |
| 2007 | * pre-shared key. */ |
| 2008 | premaster_secret_len = psk_len; |
| 2009 | premaster_secret = OPENSSL_malloc(premaster_secret_len); |
| 2010 | if (premaster_secret == NULL) { |
| 2011 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2012 | ERR_R_MALLOC_FAILURE); |
| 2013 | goto err; |
| 2014 | } |
| 2015 | memset(premaster_secret, 0, premaster_secret_len); |
| 2016 | } else { |
| 2017 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2018 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2019 | SSL_R_UNKNOWN_CIPHER_TYPE); |
| 2020 | goto f_err; |
| 2021 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2022 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2023 | /* For a PSK cipher suite, the actual pre-master secret is combined with the |
| 2024 | * pre-shared key. */ |
| 2025 | if (alg_a & SSL_aPSK) { |
| 2026 | CBB new_premaster, child; |
| 2027 | uint8_t *new_data; |
| 2028 | size_t new_len; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2029 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2030 | if (!CBB_init(&new_premaster, 2 + psk_len + 2 + premaster_secret_len)) { |
| 2031 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2032 | ERR_R_MALLOC_FAILURE); |
| 2033 | goto err; |
| 2034 | } |
| 2035 | if (!CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2036 | !CBB_add_bytes(&child, premaster_secret, premaster_secret_len) || |
| 2037 | !CBB_add_u16_length_prefixed(&new_premaster, &child) || |
| 2038 | !CBB_add_bytes(&child, psk, psk_len) || |
| 2039 | !CBB_finish(&new_premaster, &new_data, &new_len)) { |
| 2040 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_key_exchange, |
| 2041 | ERR_R_INTERNAL_ERROR); |
| 2042 | CBB_cleanup(&new_premaster); |
| 2043 | goto err; |
| 2044 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2045 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2046 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2047 | OPENSSL_free(premaster_secret); |
| 2048 | premaster_secret = new_data; |
| 2049 | premaster_secret_len = new_len; |
| 2050 | } |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2051 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2052 | /* Compute the master secret */ |
| 2053 | s->session->master_key_length = s->enc_method->generate_master_secret( |
| 2054 | s, s->session->master_key, premaster_secret, premaster_secret_len); |
| 2055 | if (s->session->master_key_length == 0) { |
| 2056 | goto err; |
| 2057 | } |
| 2058 | s->session->extended_master_secret = s->s3->tmp.extended_master_secret; |
David Benjamin | 14c83e7 | 2014-07-13 04:54:57 -0400 | [diff] [blame] | 2059 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2060 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2061 | OPENSSL_free(premaster_secret); |
| 2062 | return 1; |
| 2063 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2064 | f_err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2065 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2066 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2067 | if (premaster_secret) { |
| 2068 | if (premaster_secret_len) { |
| 2069 | OPENSSL_cleanse(premaster_secret, premaster_secret_len); |
| 2070 | } |
| 2071 | OPENSSL_free(premaster_secret); |
| 2072 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2073 | OPENSSL_free(decrypt_buf); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2074 | EVP_PKEY_free(clnt_pub_pkey); |
| 2075 | EC_POINT_free(clnt_ecpoint); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2076 | EC_KEY_free(srvr_ecdh); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2077 | BN_CTX_free(bn_ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2078 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2079 | return -1; |
| 2080 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2081 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2082 | int ssl3_get_cert_verify(SSL *s) { |
| 2083 | int al, ok, ret = 0; |
| 2084 | long n; |
| 2085 | CBS certificate_verify, signature; |
| 2086 | X509 *peer = s->session->peer; |
| 2087 | EVP_PKEY *pkey = NULL; |
| 2088 | const EVP_MD *md = NULL; |
| 2089 | uint8_t digest[EVP_MAX_MD_SIZE]; |
| 2090 | size_t digest_length; |
| 2091 | EVP_PKEY_CTX *pctx = NULL; |
David Benjamin | 6553b37 | 2014-07-22 14:11:30 -0400 | [diff] [blame] | 2092 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2093 | /* Only RSA and ECDSA client certificates are supported, so a |
| 2094 | * CertificateVerify is required if and only if there's a client certificate. |
| 2095 | * */ |
| 2096 | if (peer == NULL) { |
| 2097 | if (s->s3->handshake_buffer && |
| 2098 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2099 | return -1; |
| 2100 | } |
| 2101 | return 1; |
| 2102 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2103 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2104 | n = s->method->ssl_get_message( |
| 2105 | s, SSL3_ST_SR_CERT_VRFY_A, SSL3_ST_SR_CERT_VRFY_B, |
| 2106 | SSL3_MT_CERTIFICATE_VERIFY, SSL3_RT_MAX_PLAIN_LENGTH, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 2107 | ssl_dont_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2108 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2109 | if (!ok) { |
| 2110 | return n; |
| 2111 | } |
David Benjamin | ef86550 | 2014-08-24 02:48:34 -0400 | [diff] [blame] | 2112 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2113 | /* Filter out unsupported certificate types. */ |
| 2114 | pkey = X509_get_pubkey(peer); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 2115 | if (pkey == NULL) { |
| 2116 | goto err; |
| 2117 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2118 | if (!(X509_certificate_type(peer, pkey) & EVP_PKT_SIGN) || |
| 2119 | (pkey->type != EVP_PKEY_RSA && pkey->type != EVP_PKEY_EC)) { |
| 2120 | al = SSL_AD_UNSUPPORTED_CERTIFICATE; |
| 2121 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, |
| 2122 | SSL_R_PEER_ERROR_UNSUPPORTED_CERTIFICATE_TYPE); |
| 2123 | goto f_err; |
| 2124 | } |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2125 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2126 | CBS_init(&certificate_verify, s->init_msg, n); |
David Benjamin | 6897dbe | 2014-07-12 20:18:28 -0400 | [diff] [blame] | 2127 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2128 | /* Determine the digest type if needbe. */ |
| 2129 | if (SSL_USE_SIGALGS(s) && |
| 2130 | !tls12_check_peer_sigalg(&md, &al, s, &certificate_verify, pkey)) { |
| 2131 | goto f_err; |
| 2132 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2133 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2134 | /* Compute the digest. */ |
| 2135 | if (!ssl3_cert_verify_hash(s, digest, &digest_length, &md, pkey)) { |
| 2136 | goto err; |
| 2137 | } |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2138 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2139 | /* The handshake buffer is no longer necessary, and we may hash the current |
| 2140 | * message.*/ |
| 2141 | if (s->s3->handshake_buffer && |
| 2142 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2143 | goto err; |
| 2144 | } |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2145 | if (!ssl3_hash_current_message(s)) { |
| 2146 | goto err; |
| 2147 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2148 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2149 | /* Parse and verify the signature. */ |
| 2150 | if (!CBS_get_u16_length_prefixed(&certificate_verify, &signature) || |
| 2151 | CBS_len(&certificate_verify) != 0) { |
| 2152 | al = SSL_AD_DECODE_ERROR; |
| 2153 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_DECODE_ERROR); |
| 2154 | goto f_err; |
| 2155 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2156 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2157 | pctx = EVP_PKEY_CTX_new(pkey, NULL); |
| 2158 | if (pctx == NULL) { |
| 2159 | goto err; |
| 2160 | } |
| 2161 | if (!EVP_PKEY_verify_init(pctx) || |
| 2162 | !EVP_PKEY_CTX_set_signature_md(pctx, md) || |
| 2163 | !EVP_PKEY_verify(pctx, CBS_data(&signature), CBS_len(&signature), digest, |
| 2164 | digest_length)) { |
| 2165 | al = SSL_AD_DECRYPT_ERROR; |
| 2166 | OPENSSL_PUT_ERROR(SSL, ssl3_get_cert_verify, SSL_R_BAD_SIGNATURE); |
| 2167 | goto f_err; |
| 2168 | } |
| 2169 | |
| 2170 | ret = 1; |
| 2171 | |
| 2172 | if (0) { |
| 2173 | f_err: |
| 2174 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2175 | } |
| 2176 | |
David Benjamin | 854dd65 | 2014-08-26 00:32:30 -0400 | [diff] [blame] | 2177 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2178 | EVP_PKEY_CTX_free(pctx); |
| 2179 | EVP_PKEY_free(pkey); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2180 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2181 | return ret; |
| 2182 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2183 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2184 | int ssl3_get_client_certificate(SSL *s) { |
| 2185 | int i, ok, al, ret = -1; |
| 2186 | X509 *x = NULL; |
| 2187 | unsigned long n; |
| 2188 | STACK_OF(X509) *sk = NULL; |
| 2189 | SHA256_CTX sha256; |
| 2190 | CBS certificate_msg, certificate_list; |
| 2191 | int is_first_certificate = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2192 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2193 | n = s->method->ssl_get_message(s, SSL3_ST_SR_CERT_A, SSL3_ST_SR_CERT_B, -1, |
David Benjamin | 1d0a194 | 2015-04-26 15:35:35 -0400 | [diff] [blame] | 2194 | (long)s->max_cert_list, ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2195 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2196 | if (!ok) { |
| 2197 | return n; |
| 2198 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2199 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2200 | if (s->s3->tmp.message_type == SSL3_MT_CLIENT_KEY_EXCHANGE) { |
| 2201 | if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2202 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
| 2203 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2204 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2205 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2206 | goto f_err; |
| 2207 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2208 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2209 | /* If tls asked for a client cert, the client must return a 0 list */ |
| 2210 | if (s->version > SSL3_VERSION && s->s3->tmp.cert_request) { |
| 2211 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2212 | SSL_R_TLS_PEER_DID_NOT_RESPOND_WITH_CERTIFICATE_LIST); |
| 2213 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 2214 | goto f_err; |
| 2215 | } |
| 2216 | s->s3->tmp.reuse_message = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2217 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2218 | return 1; |
| 2219 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2220 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2221 | if (s->s3->tmp.message_type != SSL3_MT_CERTIFICATE) { |
| 2222 | al = SSL_AD_UNEXPECTED_MESSAGE; |
| 2223 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2224 | SSL_R_WRONG_MESSAGE_TYPE); |
| 2225 | goto f_err; |
| 2226 | } |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2227 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2228 | CBS_init(&certificate_msg, s->init_msg, n); |
David Benjamin | d5b1f84 | 2014-07-11 23:43:48 -0400 | [diff] [blame] | 2229 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2230 | sk = sk_X509_new_null(); |
| 2231 | if (sk == NULL) { |
| 2232 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2233 | goto err; |
| 2234 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2235 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2236 | if (!CBS_get_u24_length_prefixed(&certificate_msg, &certificate_list) || |
| 2237 | CBS_len(&certificate_msg) != 0) { |
| 2238 | al = SSL_AD_DECODE_ERROR; |
| 2239 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
| 2240 | goto f_err; |
| 2241 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2242 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2243 | while (CBS_len(&certificate_list) > 0) { |
| 2244 | CBS certificate; |
| 2245 | const uint8_t *data; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2246 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2247 | if (!CBS_get_u24_length_prefixed(&certificate_list, &certificate)) { |
| 2248 | al = SSL_AD_DECODE_ERROR; |
| 2249 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, SSL_R_DECODE_ERROR); |
| 2250 | goto f_err; |
| 2251 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2252 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2253 | if (is_first_certificate && s->ctx->retain_only_sha256_of_client_certs) { |
| 2254 | /* If this is the first certificate, and we don't want to keep peer |
| 2255 | * certificates in memory, then we hash it right away. */ |
| 2256 | SHA256_Init(&sha256); |
| 2257 | SHA256_Update(&sha256, CBS_data(&certificate), CBS_len(&certificate)); |
| 2258 | SHA256_Final(s->session->peer_sha256, &sha256); |
| 2259 | s->session->peer_sha256_valid = 1; |
| 2260 | } |
| 2261 | is_first_certificate = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2262 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2263 | data = CBS_data(&certificate); |
| 2264 | x = d2i_X509(NULL, &data, CBS_len(&certificate)); |
| 2265 | if (x == NULL) { |
| 2266 | al = SSL_AD_BAD_CERTIFICATE; |
| 2267 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_ASN1_LIB); |
| 2268 | goto f_err; |
| 2269 | } |
| 2270 | if (data != CBS_data(&certificate) + CBS_len(&certificate)) { |
| 2271 | al = SSL_AD_DECODE_ERROR; |
| 2272 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2273 | SSL_R_CERT_LENGTH_MISMATCH); |
| 2274 | goto f_err; |
| 2275 | } |
| 2276 | if (!sk_X509_push(sk, x)) { |
| 2277 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2278 | goto err; |
| 2279 | } |
| 2280 | x = NULL; |
| 2281 | } |
| 2282 | |
| 2283 | if (sk_X509_num(sk) <= 0) { |
| 2284 | /* TLS does not mind 0 certs returned */ |
| 2285 | if (s->version == SSL3_VERSION) { |
| 2286 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2287 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2288 | SSL_R_NO_CERTIFICATES_RETURNED); |
| 2289 | goto f_err; |
| 2290 | } |
| 2291 | /* Fail for TLS only if we required a certificate */ |
| 2292 | else if ((s->verify_mode & SSL_VERIFY_PEER) && |
| 2293 | (s->verify_mode & SSL_VERIFY_FAIL_IF_NO_PEER_CERT)) { |
| 2294 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2295 | SSL_R_PEER_DID_NOT_RETURN_A_CERTIFICATE); |
| 2296 | al = SSL_AD_HANDSHAKE_FAILURE; |
| 2297 | goto f_err; |
| 2298 | } |
| 2299 | /* No client certificate so digest cached records */ |
| 2300 | if (s->s3->handshake_buffer && |
| 2301 | !ssl3_digest_cached_records(s, free_handshake_buffer)) { |
| 2302 | al = SSL_AD_INTERNAL_ERROR; |
| 2303 | goto f_err; |
| 2304 | } |
| 2305 | } else { |
| 2306 | i = ssl_verify_cert_chain(s, sk); |
| 2307 | if (i <= 0) { |
| 2308 | al = ssl_verify_alarm_type(s->verify_result); |
| 2309 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, |
| 2310 | SSL_R_CERTIFICATE_VERIFY_FAILED); |
| 2311 | goto f_err; |
| 2312 | } |
| 2313 | } |
| 2314 | |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2315 | X509_free(s->session->peer); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2316 | s->session->peer = sk_X509_shift(sk); |
| 2317 | s->session->verify_result = s->verify_result; |
| 2318 | |
| 2319 | /* With the current implementation, sess_cert will always be NULL when we |
| 2320 | * arrive here. */ |
| 2321 | if (s->session->sess_cert == NULL) { |
| 2322 | s->session->sess_cert = ssl_sess_cert_new(); |
| 2323 | if (s->session->sess_cert == NULL) { |
| 2324 | OPENSSL_PUT_ERROR(SSL, ssl3_get_client_certificate, ERR_R_MALLOC_FAILURE); |
| 2325 | goto err; |
| 2326 | } |
| 2327 | } |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2328 | sk_X509_pop_free(s->session->sess_cert->cert_chain, X509_free); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2329 | s->session->sess_cert->cert_chain = sk; |
| 2330 | /* Inconsistency alert: cert_chain does *not* include the peer's own |
| 2331 | * certificate, while we do include it in s3_clnt.c */ |
| 2332 | |
| 2333 | sk = NULL; |
| 2334 | |
| 2335 | ret = 1; |
| 2336 | |
| 2337 | if (0) { |
| 2338 | f_err: |
| 2339 | ssl3_send_alert(s, SSL3_AL_FATAL, al); |
| 2340 | } |
| 2341 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2342 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2343 | X509_free(x); |
| 2344 | sk_X509_pop_free(sk, X509_free); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2345 | return ret; |
| 2346 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2347 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2348 | int ssl3_send_server_certificate(SSL *s) { |
| 2349 | CERT_PKEY *cpk; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2350 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2351 | if (s->state == SSL3_ST_SW_CERT_A) { |
| 2352 | cpk = ssl_get_server_send_pkey(s); |
| 2353 | if (cpk == NULL) { |
| 2354 | OPENSSL_PUT_ERROR(SSL, ssl3_send_server_certificate, |
| 2355 | ERR_R_INTERNAL_ERROR); |
| 2356 | return 0; |
| 2357 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2358 | |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 2359 | if (!ssl3_output_cert_chain(s, cpk)) { |
| 2360 | return 0; |
| 2361 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2362 | s->state = SSL3_ST_SW_CERT_B; |
| 2363 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2364 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2365 | /* SSL3_ST_SW_CERT_B */ |
| 2366 | return ssl_do_write(s); |
| 2367 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2368 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2369 | /* send a new session ticket (not necessarily for a new session) */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2370 | int ssl3_send_new_session_ticket(SSL *s) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2371 | int ret = -1; |
| 2372 | uint8_t *session = NULL; |
| 2373 | size_t session_len; |
| 2374 | EVP_CIPHER_CTX ctx; |
| 2375 | HMAC_CTX hctx; |
| 2376 | |
| 2377 | EVP_CIPHER_CTX_init(&ctx); |
| 2378 | HMAC_CTX_init(&hctx); |
| 2379 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2380 | if (s->state == SSL3_ST_SW_SESSION_TICKET_A) { |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2381 | uint8_t *p, *macstart; |
| 2382 | int len; |
| 2383 | unsigned int hlen; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2384 | SSL_CTX *tctx = s->initial_ctx; |
| 2385 | uint8_t iv[EVP_MAX_IV_LENGTH]; |
| 2386 | uint8_t key_name[16]; |
| 2387 | /* The maximum overhead of encrypting the session is 16 (key name) + IV + |
| 2388 | * one block of encryption overhead + HMAC. */ |
| 2389 | const size_t max_ticket_overhead = |
| 2390 | 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] | 2391 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2392 | /* Serialize the SSL_SESSION to be encoded into the ticket. */ |
| 2393 | if (!SSL_SESSION_to_bytes_for_ticket(s->session, &session, &session_len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2394 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2395 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2396 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2397 | /* If the session is too long, emit a dummy value rather than abort the |
| 2398 | * connection. */ |
| 2399 | if (session_len > 0xFFFF - max_ticket_overhead) { |
| 2400 | static const char kTicketPlaceholder[] = "TICKET TOO LARGE"; |
| 2401 | const size_t placeholder_len = strlen(kTicketPlaceholder); |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 2402 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2403 | OPENSSL_free(session); |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2404 | session = NULL; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 2405 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2406 | p = ssl_handshake_start(s); |
| 2407 | /* Emit ticket_lifetime_hint. */ |
| 2408 | l2n(0, p); |
| 2409 | /* Emit ticket. */ |
| 2410 | s2n(placeholder_len, p); |
| 2411 | memcpy(p, kTicketPlaceholder, placeholder_len); |
| 2412 | p += placeholder_len; |
David Benjamin | 3cac450 | 2014-10-21 01:46:30 -0400 | [diff] [blame] | 2413 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2414 | len = p - ssl_handshake_start(s); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2415 | if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2416 | goto err; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2417 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2418 | s->state = SSL3_ST_SW_SESSION_TICKET_B; |
| 2419 | return ssl_do_write(s); |
| 2420 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2421 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2422 | /* Grow buffer if need be: the length calculation is as follows: |
| 2423 | * handshake_header_length + 4 (ticket lifetime hint) + 2 (ticket length) + |
| 2424 | * max_ticket_overhead + * session_length */ |
| 2425 | if (!BUF_MEM_grow(s->init_buf, SSL_HM_HEADER_LENGTH(s) + 6 + |
| 2426 | max_ticket_overhead + session_len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2427 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2428 | } |
| 2429 | p = ssl_handshake_start(s); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2430 | /* Initialize HMAC and cipher contexts. If callback present it does all the |
| 2431 | * work otherwise use generated values from parent ctx. */ |
| 2432 | if (tctx->tlsext_ticket_key_cb) { |
David Benjamin | adcc395 | 2015-04-26 13:07:57 -0400 | [diff] [blame] | 2433 | if (tctx->tlsext_ticket_key_cb(s, key_name, iv, &ctx, &hctx, |
| 2434 | 1 /* encrypt */) < 0) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2435 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2436 | } |
| 2437 | } else { |
| 2438 | if (!RAND_bytes(iv, 16) || |
| 2439 | !EVP_EncryptInit_ex(&ctx, EVP_aes_128_cbc(), NULL, |
| 2440 | tctx->tlsext_tick_aes_key, iv) || |
| 2441 | !HMAC_Init_ex(&hctx, tctx->tlsext_tick_hmac_key, 16, tlsext_tick_md(), |
| 2442 | NULL)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2443 | goto err; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2444 | } |
| 2445 | memcpy(key_name, tctx->tlsext_tick_key_name, 16); |
| 2446 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2447 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2448 | /* Ticket lifetime hint (advisory only): We leave this unspecified for |
| 2449 | * resumed session (for simplicity), and guess that tickets for new |
| 2450 | * sessions will live as long as their sessions. */ |
| 2451 | l2n(s->hit ? 0 : s->session->timeout, p); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2452 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2453 | /* Skip ticket length for now */ |
| 2454 | p += 2; |
| 2455 | /* Output key name */ |
| 2456 | macstart = p; |
| 2457 | memcpy(p, key_name, 16); |
| 2458 | p += 16; |
| 2459 | /* output IV */ |
| 2460 | memcpy(p, iv, EVP_CIPHER_CTX_iv_length(&ctx)); |
| 2461 | p += EVP_CIPHER_CTX_iv_length(&ctx); |
| 2462 | /* Encrypt session data */ |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2463 | if (!EVP_EncryptUpdate(&ctx, p, &len, session, session_len)) { |
| 2464 | goto err; |
| 2465 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2466 | p += len; |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2467 | if (!EVP_EncryptFinal_ex(&ctx, p, &len)) { |
| 2468 | goto err; |
| 2469 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2470 | p += len; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2471 | |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2472 | if (!HMAC_Update(&hctx, macstart, p - macstart) || |
| 2473 | !HMAC_Final(&hctx, p, &hlen)) { |
| 2474 | goto err; |
| 2475 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2476 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2477 | p += hlen; |
| 2478 | /* Now write out lengths: p points to end of data written */ |
| 2479 | /* Total length */ |
| 2480 | len = p - ssl_handshake_start(s); |
| 2481 | /* Skip ticket lifetime hint */ |
| 2482 | p = ssl_handshake_start(s) + 4; |
| 2483 | s2n(len - 6, p); |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2484 | if (!ssl_set_handshake_header(s, SSL3_MT_NEWSESSION_TICKET, len)) { |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2485 | goto err; |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2486 | } |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2487 | s->state = SSL3_ST_SW_SESSION_TICKET_B; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2488 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2489 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2490 | /* SSL3_ST_SW_SESSION_TICKET_B */ |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2491 | ret = ssl_do_write(s); |
| 2492 | |
| 2493 | err: |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2494 | OPENSSL_free(session); |
David Benjamin | bcd3745 | 2015-03-09 14:48:28 -0400 | [diff] [blame] | 2495 | EVP_CIPHER_CTX_cleanup(&ctx); |
| 2496 | HMAC_CTX_cleanup(&hctx); |
| 2497 | return ret; |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2498 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2499 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2500 | /* ssl3_get_next_proto reads a Next Protocol Negotiation handshake message. It |
| 2501 | * sets the next_proto member in s if found */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2502 | int ssl3_get_next_proto(SSL *s) { |
| 2503 | int ok; |
| 2504 | long n; |
| 2505 | CBS next_protocol, selected_protocol, padding; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2506 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2507 | /* Clients cannot send a NextProtocol message if we didn't see the extension |
| 2508 | * in their ClientHello */ |
| 2509 | if (!s->s3->next_proto_neg_seen) { |
| 2510 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, |
| 2511 | SSL_R_GOT_NEXT_PROTO_WITHOUT_EXTENSION); |
| 2512 | return -1; |
| 2513 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2514 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2515 | n = s->method->ssl_get_message(s, SSL3_ST_SR_NEXT_PROTO_A, |
| 2516 | SSL3_ST_SR_NEXT_PROTO_B, SSL3_MT_NEXT_PROTO, |
| 2517 | 514, /* See the payload format below */ |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 2518 | ssl_hash_message, &ok); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2519 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2520 | if (!ok) { |
| 2521 | return n; |
| 2522 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2523 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2524 | /* s->state doesn't reflect whether ChangeCipherSpec has been received in |
| 2525 | * this handshake, but s->s3->change_cipher_spec does (will be reset by |
| 2526 | * ssl3_get_finished). |
| 2527 | * |
| 2528 | * TODO(davidben): Is this check now redundant with |
| 2529 | * SSL3_FLAGS_EXPECT_CCS? */ |
| 2530 | if (!s->s3->change_cipher_spec) { |
| 2531 | OPENSSL_PUT_ERROR(SSL, ssl3_get_next_proto, |
| 2532 | SSL_R_GOT_NEXT_PROTO_BEFORE_A_CCS); |
| 2533 | return -1; |
| 2534 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2535 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2536 | CBS_init(&next_protocol, s->init_msg, n); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2537 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2538 | /* The payload looks like: |
| 2539 | * uint8 proto_len; |
| 2540 | * uint8 proto[proto_len]; |
| 2541 | * uint8 padding_len; |
| 2542 | * uint8 padding[padding_len]; */ |
| 2543 | if (!CBS_get_u8_length_prefixed(&next_protocol, &selected_protocol) || |
| 2544 | !CBS_get_u8_length_prefixed(&next_protocol, &padding) || |
| 2545 | CBS_len(&next_protocol) != 0 || |
| 2546 | !CBS_stow(&selected_protocol, &s->next_proto_negotiated, |
| 2547 | &s->next_proto_negotiated_len)) { |
| 2548 | return 0; |
| 2549 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2550 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2551 | return 1; |
| 2552 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2553 | |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2554 | /* ssl3_get_channel_id reads and verifies a ClientID handshake message. */ |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2555 | int ssl3_get_channel_id(SSL *s) { |
| 2556 | int ret = -1, ok; |
| 2557 | long n; |
| 2558 | EVP_MD_CTX md_ctx; |
| 2559 | uint8_t channel_id_hash[SHA256_DIGEST_LENGTH]; |
| 2560 | unsigned int channel_id_hash_len; |
| 2561 | const uint8_t *p; |
| 2562 | uint16_t extension_type, expected_extension_type; |
| 2563 | EC_GROUP *p256 = NULL; |
| 2564 | EC_KEY *key = NULL; |
| 2565 | EC_POINT *point = NULL; |
| 2566 | ECDSA_SIG sig; |
| 2567 | BIGNUM x, y; |
| 2568 | CBS encrypted_extensions, extension; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2569 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2570 | n = s->method->ssl_get_message( |
| 2571 | s, SSL3_ST_SR_CHANNEL_ID_A, SSL3_ST_SR_CHANNEL_ID_B, |
| 2572 | SSL3_MT_ENCRYPTED_EXTENSIONS, 2 + 2 + TLSEXT_CHANNEL_ID_SIZE, |
David Benjamin | 5ca39fb | 2015-03-01 23:57:54 -0500 | [diff] [blame] | 2573 | ssl_dont_hash_message, &ok); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2574 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2575 | if (!ok) { |
| 2576 | return n; |
| 2577 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2578 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2579 | /* Before incorporating the EncryptedExtensions message to the handshake |
| 2580 | * hash, compute the hash that should have been signed. */ |
| 2581 | channel_id_hash_len = sizeof(channel_id_hash); |
| 2582 | EVP_MD_CTX_init(&md_ctx); |
| 2583 | if (!EVP_DigestInit_ex(&md_ctx, EVP_sha256(), NULL) || |
| 2584 | !tls1_channel_id_hash(&md_ctx, s) || |
| 2585 | !EVP_DigestFinal(&md_ctx, channel_id_hash, &channel_id_hash_len)) { |
| 2586 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2587 | return -1; |
| 2588 | } |
| 2589 | EVP_MD_CTX_cleanup(&md_ctx); |
| 2590 | assert(channel_id_hash_len == SHA256_DIGEST_LENGTH); |
David Benjamin | 880b14e | 2014-08-25 22:35:07 -0400 | [diff] [blame] | 2591 | |
David Benjamin | fbdfefb | 2015-02-16 19:33:53 -0500 | [diff] [blame] | 2592 | if (!ssl3_hash_current_message(s)) { |
| 2593 | return -1; |
| 2594 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2595 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2596 | /* s->state doesn't reflect whether ChangeCipherSpec has been received in |
| 2597 | * this handshake, but s->s3->change_cipher_spec does (will be reset by |
| 2598 | * ssl3_get_finished). |
| 2599 | * |
| 2600 | * TODO(davidben): Is this check now redundant with SSL3_FLAGS_EXPECT_CCS? */ |
| 2601 | if (!s->s3->change_cipher_spec) { |
| 2602 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, |
| 2603 | SSL_R_GOT_CHANNEL_ID_BEFORE_A_CCS); |
| 2604 | return -1; |
| 2605 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2606 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2607 | CBS_init(&encrypted_extensions, s->init_msg, n); |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2608 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2609 | /* EncryptedExtensions could include multiple extensions, but the only |
| 2610 | * extension that could be negotiated is ChannelID, so there can only be one |
| 2611 | * entry. |
| 2612 | * |
| 2613 | * The payload looks like: |
| 2614 | * uint16 extension_type |
| 2615 | * uint16 extension_len; |
| 2616 | * uint8 x[32]; |
| 2617 | * uint8 y[32]; |
| 2618 | * uint8 r[32]; |
| 2619 | * uint8 s[32]; */ |
| 2620 | expected_extension_type = TLSEXT_TYPE_channel_id; |
| 2621 | if (s->s3->tlsext_channel_id_new) { |
| 2622 | expected_extension_type = TLSEXT_TYPE_channel_id_new; |
| 2623 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2624 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2625 | if (!CBS_get_u16(&encrypted_extensions, &extension_type) || |
| 2626 | !CBS_get_u16_length_prefixed(&encrypted_extensions, &extension) || |
| 2627 | CBS_len(&encrypted_extensions) != 0 || |
| 2628 | extension_type != expected_extension_type || |
| 2629 | CBS_len(&extension) != TLSEXT_CHANNEL_ID_SIZE) { |
| 2630 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_INVALID_MESSAGE); |
| 2631 | return -1; |
| 2632 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2633 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2634 | p256 = EC_GROUP_new_by_curve_name(NID_X9_62_prime256v1); |
| 2635 | if (!p256) { |
| 2636 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, SSL_R_NO_P256_SUPPORT); |
| 2637 | return -1; |
| 2638 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2639 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2640 | BN_init(&x); |
| 2641 | BN_init(&y); |
| 2642 | sig.r = BN_new(); |
| 2643 | sig.s = BN_new(); |
David Benjamin | 9d0847a | 2015-02-16 03:57:55 -0500 | [diff] [blame] | 2644 | if (sig.r == NULL || sig.s == NULL) { |
| 2645 | goto err; |
| 2646 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2647 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2648 | p = CBS_data(&extension); |
| 2649 | if (BN_bin2bn(p + 0, 32, &x) == NULL || |
| 2650 | BN_bin2bn(p + 32, 32, &y) == NULL || |
| 2651 | BN_bin2bn(p + 64, 32, sig.r) == NULL || |
| 2652 | BN_bin2bn(p + 96, 32, sig.s) == NULL) { |
| 2653 | goto err; |
| 2654 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2655 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2656 | point = EC_POINT_new(p256); |
| 2657 | if (!point || !EC_POINT_set_affine_coordinates_GFp(p256, point, &x, &y, NULL)) { |
| 2658 | goto err; |
| 2659 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2660 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2661 | key = EC_KEY_new(); |
| 2662 | if (!key || !EC_KEY_set_group(key, p256) || |
| 2663 | !EC_KEY_set_public_key(key, point)) { |
| 2664 | goto err; |
| 2665 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2666 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2667 | /* We stored the handshake hash in |tlsext_channel_id| the first time that we |
| 2668 | * were called. */ |
| 2669 | if (!ECDSA_do_verify(channel_id_hash, channel_id_hash_len, &sig, key)) { |
| 2670 | OPENSSL_PUT_ERROR(SSL, ssl3_get_channel_id, |
| 2671 | SSL_R_CHANNEL_ID_SIGNATURE_INVALID); |
| 2672 | s->s3->tlsext_channel_id_valid = 0; |
| 2673 | goto err; |
| 2674 | } |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2675 | |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2676 | memcpy(s->s3->tlsext_channel_id, p, 64); |
| 2677 | ret = 1; |
Adam Langley | 1258b6a | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 2678 | |
| 2679 | err: |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2680 | BN_free(&x); |
| 2681 | BN_free(&y); |
| 2682 | BN_free(sig.r); |
| 2683 | BN_free(sig.s); |
David Benjamin | 2755a3e | 2015-04-22 16:17:58 -0400 | [diff] [blame] | 2684 | EC_KEY_free(key); |
| 2685 | EC_POINT_free(point); |
| 2686 | EC_GROUP_free(p256); |
Adam Langley | fcf2583 | 2014-12-18 17:42:32 -0800 | [diff] [blame] | 2687 | return ret; |
| 2688 | } |