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