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