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