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-2006 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 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 109 | #include <assert.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | #include <stdio.h> |
| 111 | |
| 112 | #include <openssl/buf.h> |
| 113 | #include <openssl/err.h> |
| 114 | #include <openssl/evp.h> |
| 115 | #include <openssl/mem.h> |
| 116 | #include <openssl/obj.h> |
| 117 | #include <openssl/rand.h> |
| 118 | |
| 119 | #include "ssl_locl.h" |
| 120 | |
| 121 | static const SSL_METHOD *ssl23_get_server_method(int ver); |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 122 | static int ssl23_get_client_hello(SSL *s); |
| 123 | static int ssl23_get_v2_client_hello(SSL *s); |
| 124 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 125 | static const SSL_METHOD *ssl23_get_server_method(int ver) |
| 126 | { |
| 127 | if (ver == SSL3_VERSION) |
| 128 | return(SSLv3_server_method()); |
| 129 | else if (ver == TLS1_VERSION) |
| 130 | return(TLSv1_server_method()); |
| 131 | else if (ver == TLS1_1_VERSION) |
| 132 | return(TLSv1_1_server_method()); |
| 133 | else if (ver == TLS1_2_VERSION) |
| 134 | return(TLSv1_2_server_method()); |
| 135 | else |
| 136 | return(NULL); |
| 137 | } |
| 138 | |
| 139 | IMPLEMENT_ssl23_meth_func(SSLv23_server_method, |
| 140 | ssl23_accept, |
| 141 | ssl_undefined_function, |
| 142 | ssl23_get_server_method) |
| 143 | |
| 144 | int ssl23_accept(SSL *s) |
| 145 | { |
| 146 | BUF_MEM *buf; |
| 147 | void (*cb)(const SSL *ssl,int type,int val)=NULL; |
| 148 | int ret= -1; |
| 149 | int new_state,state; |
| 150 | |
| 151 | ERR_clear_error(); |
| 152 | ERR_clear_system_error(); |
| 153 | |
| 154 | if (s->info_callback != NULL) |
| 155 | cb=s->info_callback; |
| 156 | else if (s->ctx->info_callback != NULL) |
| 157 | cb=s->ctx->info_callback; |
| 158 | |
| 159 | s->in_handshake++; |
| 160 | if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); |
| 161 | |
| 162 | for (;;) |
| 163 | { |
| 164 | state=s->state; |
| 165 | |
| 166 | switch(s->state) |
| 167 | { |
| 168 | case SSL_ST_BEFORE: |
| 169 | case SSL_ST_ACCEPT: |
| 170 | case SSL_ST_BEFORE|SSL_ST_ACCEPT: |
| 171 | case SSL_ST_OK|SSL_ST_ACCEPT: |
| 172 | |
| 173 | s->server=1; |
| 174 | if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1); |
| 175 | |
| 176 | /* s->version=SSL3_VERSION; */ |
| 177 | s->type=SSL_ST_ACCEPT; |
| 178 | |
| 179 | if (s->init_buf == NULL) |
| 180 | { |
| 181 | if ((buf=BUF_MEM_new()) == NULL) |
| 182 | { |
| 183 | ret= -1; |
| 184 | goto end; |
| 185 | } |
| 186 | if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH)) |
| 187 | { |
| 188 | ret= -1; |
| 189 | goto end; |
| 190 | } |
| 191 | s->init_buf=buf; |
| 192 | } |
| 193 | |
Adam Langley | 69a0160 | 2014-11-17 17:26:55 -0800 | [diff] [blame^] | 194 | if (!ssl3_init_finished_mac(s)) |
| 195 | { |
| 196 | OPENSSL_PUT_ERROR(SSL, ssl23_accept, ERR_R_INTERNAL_ERROR); |
| 197 | ret = -1; |
| 198 | goto end; |
| 199 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 200 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 201 | s->state=SSL23_ST_SR_CLNT_HELLO; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 202 | s->ctx->stats.sess_accept++; |
| 203 | s->init_num=0; |
| 204 | break; |
| 205 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 206 | case SSL23_ST_SR_CLNT_HELLO: |
| 207 | s->shutdown = 0; |
| 208 | ret = ssl23_get_client_hello(s); |
| 209 | if (ret <= 0) goto end; |
| 210 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 211 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 212 | case SSL23_ST_SR_V2_CLNT_HELLO: |
| 213 | ret = ssl23_get_v2_client_hello(s); |
| 214 | if (ret <= 0) goto end; |
| 215 | break; |
| 216 | |
| 217 | case SSL23_ST_SR_SWITCH_VERSION: |
| 218 | if (!ssl_init_wbio_buffer(s, 1)) |
| 219 | { |
| 220 | ret = -1; |
| 221 | goto end; |
| 222 | } |
| 223 | |
| 224 | s->state = SSL3_ST_SR_CLNT_HELLO_A; |
| 225 | s->method = ssl23_get_server_method(s->version); |
| 226 | assert(s->method != NULL); |
| 227 | s->handshake_func = s->method->ssl_accept; |
| 228 | s->init_num = 0; |
| 229 | |
| 230 | /* NULL the callback; SSL_accept will call it instead. */ |
| 231 | cb = NULL; |
| 232 | ret = SSL_accept(s); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | goto end; |
| 234 | /* break; */ |
| 235 | |
| 236 | default: |
| 237 | OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE); |
| 238 | ret= -1; |
| 239 | goto end; |
| 240 | /* break; */ |
| 241 | } |
| 242 | |
| 243 | if ((cb != NULL) && (s->state != state)) |
| 244 | { |
| 245 | new_state=s->state; |
| 246 | s->state=state; |
| 247 | cb(s,SSL_CB_ACCEPT_LOOP,1); |
| 248 | s->state=new_state; |
| 249 | } |
| 250 | } |
| 251 | end: |
| 252 | s->in_handshake--; |
| 253 | if (cb != NULL) |
| 254 | cb(s,SSL_CB_ACCEPT_EXIT,ret); |
| 255 | return(ret); |
| 256 | } |
| 257 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 258 | /* ssl23_get_mutual_version determines the highest supported version for a |
| 259 | * client which reports a highest version of |client_version|. On success, it |
| 260 | * returns 1 and sets |*out_version| to the negotiated version. Otherwise, it |
| 261 | * returns 0. */ |
| 262 | static int ssl23_get_mutual_version(SSL *s, int *out_version, uint16_t client_version) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 263 | { |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 264 | if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 265 | { |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 266 | *out_version = TLS1_2_VERSION; |
| 267 | return 1; |
| 268 | } |
| 269 | if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1)) |
| 270 | { |
| 271 | *out_version = TLS1_1_VERSION; |
| 272 | return 1; |
| 273 | } |
| 274 | if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1)) |
| 275 | { |
| 276 | *out_version = TLS1_VERSION; |
| 277 | return 1; |
| 278 | } |
| 279 | if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3)) |
| 280 | { |
| 281 | *out_version = SSL3_VERSION; |
| 282 | return 1; |
| 283 | } |
| 284 | return 0; |
| 285 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 286 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 287 | static int ssl23_get_client_hello(SSL *s) |
| 288 | { |
| 289 | uint8_t *p; |
| 290 | int n = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 291 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 292 | /* Sniff enough of the input to determine ClientHello type and the |
| 293 | * client version. */ |
| 294 | if (!ssl3_setup_buffers(s)) goto err; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 295 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 296 | /* Read the initial 11 bytes of the input. This is sufficient to |
| 297 | * determine the client version for a ClientHello or a |
| 298 | * V2ClientHello. |
| 299 | * |
| 300 | * ClientHello (assuming client_version is unfragmented): |
| 301 | * Byte Content |
| 302 | * 0 type \ |
| 303 | * 1-2 version > record header |
| 304 | * 3-4 length / |
| 305 | * 5 msg_type \ |
| 306 | * 6-8 length > Client Hello message |
| 307 | * 9-10 client_version / |
| 308 | * |
| 309 | * V2ClientHello: |
| 310 | * Byte Content |
| 311 | * 0-1 msg_length |
| 312 | * 2 msg_type |
| 313 | * 3-4 version |
| 314 | * 5-6 cipher_spec_length |
| 315 | * 7-8 session_id_length |
| 316 | * 9-10 challenge_length |
| 317 | */ |
| 318 | n = ssl23_read_bytes(s, 11); |
| 319 | if (n <= 0) |
| 320 | return n; |
| 321 | assert(n == 11); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 323 | p = s->packet; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 324 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 325 | /* Some dedicated error codes for protocol mixups should the application |
| 326 | * wish to interpret them differently. (These do not overlap with |
| 327 | * ClientHello or V2ClientHello.) */ |
| 328 | if ((strncmp("GET ", (char *)p, 4) == 0) || |
| 329 | (strncmp("POST ",(char *)p, 5) == 0) || |
| 330 | (strncmp("HEAD ",(char *)p, 5) == 0) || |
| 331 | (strncmp("PUT ", (char *)p, 4) == 0)) |
| 332 | { |
| 333 | OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST); |
| 334 | goto err; |
| 335 | } |
| 336 | if (strncmp("CONNECT",(char *)p, 7) == 0) |
| 337 | { |
| 338 | OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST); |
| 339 | goto err; |
| 340 | } |
| 341 | |
| 342 | /* Determine if this is a ClientHello or V2ClientHello. */ |
| 343 | if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) |
| 344 | { |
| 345 | /* This is a V2ClientHello. Determine the version to |
| 346 | * use. */ |
| 347 | uint16_t client_version = (p[3] << 8) | p[4]; |
| 348 | if (!ssl23_get_mutual_version(s, &s->version, client_version)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | { |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 350 | OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 351 | goto err; |
| 352 | } |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 353 | /* Parse the entire V2ClientHello. */ |
| 354 | s->state = SSL23_ST_SR_V2_CLNT_HELLO; |
| 355 | } |
| 356 | else if ((p[0] == SSL3_RT_HANDSHAKE) && |
| 357 | (p[1] >= SSL3_VERSION_MAJOR) && |
| 358 | (p[5] == SSL3_MT_CLIENT_HELLO)) |
| 359 | { |
| 360 | /* This is a fragment of a ClientHello. We look at the |
| 361 | * client_hello to negotiate the version. However, this |
| 362 | * is difficult if we have only a pathologically small |
| 363 | * fragment. No known client fragments ClientHello like |
| 364 | * this, so we simply reject such connections to avoid |
| 365 | * protocol version downgrade attacks. */ |
| 366 | uint16_t record_length = (p[3] << 8) | p[4]; |
| 367 | uint16_t client_version; |
| 368 | if (record_length < 6) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | { |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 370 | OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_SMALL); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 371 | goto err; |
| 372 | } |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 373 | |
| 374 | client_version = (p[9] << 8) | p[10]; |
| 375 | if (!ssl23_get_mutual_version(s, &s->version, client_version)) |
| 376 | { |
| 377 | OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL); |
| 378 | goto err; |
| 379 | } |
| 380 | |
| 381 | /* Reset the record-layer state for SSL3. */ |
| 382 | assert(s->rstate == SSL_ST_READ_HEADER); |
| 383 | s->s3->rbuf.left = s->packet_length; |
| 384 | s->s3->rbuf.offset = 0; |
| 385 | s->packet_length = 0; |
| 386 | |
| 387 | /* Ready to switch versions. */ |
| 388 | s->state = SSL23_ST_SR_SWITCH_VERSION; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 389 | } |
| 390 | |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 391 | return 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 392 | err: |
David Benjamin | 98e882e | 2014-08-08 13:24:34 -0400 | [diff] [blame] | 393 | return -1; |
| 394 | } |
| 395 | |
| 396 | static int ssl23_get_v2_client_hello(SSL *s) |
| 397 | { |
| 398 | uint8_t *p; |
| 399 | size_t i; |
| 400 | int n = 0; |
| 401 | |
| 402 | CBS v2_client_hello, cipher_specs, session_id, challenge; |
| 403 | size_t msg_length, len; |
| 404 | uint8_t msg_type; |
| 405 | uint16_t version, cipher_spec_length, session_id_length, challenge_length; |
| 406 | CBB client_hello, hello_body, cipher_suites; |
| 407 | uint8_t random[SSL3_RANDOM_SIZE]; |
| 408 | |
| 409 | /* Read the remainder of the V2ClientHello. We have previously read 11 |
| 410 | * bytes in ssl23_get_client_hello. */ |
| 411 | p = s->packet; |
| 412 | msg_length = ((p[0] & 0x7f) << 8) | p[1]; |
| 413 | if (msg_length > (1024 * 4)) |
| 414 | { |
| 415 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE); |
| 416 | goto err; |
| 417 | } |
| 418 | if (msg_length < 11 - 2) |
| 419 | { |
| 420 | /* Reject lengths that are too short early. We have already read |
| 421 | * 11 bytes, so we should not attempt to process an (invalid) |
| 422 | * V2ClientHello which would be shorter than that. */ |
| 423 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_LENGTH_MISMATCH); |
| 424 | goto err; |
| 425 | } |
| 426 | n = ssl23_read_bytes(s, msg_length + 2); |
| 427 | if (n <= 0) |
| 428 | return n; |
| 429 | assert(n == s->packet_length); |
| 430 | |
| 431 | /* The V2ClientHello without the length is incorporated into the |
| 432 | * Finished hash. */ |
| 433 | ssl3_finish_mac(s, s->packet + 2, s->packet_length - 2); |
| 434 | if (s->msg_callback) |
| 435 | s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */ |
| 436 | |
| 437 | CBS_init(&v2_client_hello, s->packet + 2, s->packet_length - 2); |
| 438 | if (!CBS_get_u8(&v2_client_hello, &msg_type) || |
| 439 | !CBS_get_u16(&v2_client_hello, &version) || |
| 440 | !CBS_get_u16(&v2_client_hello, &cipher_spec_length) || |
| 441 | !CBS_get_u16(&v2_client_hello, &session_id_length) || |
| 442 | !CBS_get_u16(&v2_client_hello, &challenge_length) || |
| 443 | !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) || |
| 444 | !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) || |
| 445 | !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) || |
| 446 | CBS_len(&v2_client_hello) != 0) |
| 447 | { |
| 448 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR); |
| 449 | goto err; |
| 450 | } |
| 451 | |
| 452 | /* msg_type has already been checked. */ |
| 453 | assert(msg_type == SSL2_MT_CLIENT_HELLO); |
| 454 | |
| 455 | /* The client_random is the V2ClientHello challenge. Truncate or |
| 456 | * left-pad with zeros as needed. */ |
| 457 | memset(random, 0, SSL3_RANDOM_SIZE); |
| 458 | i = (CBS_len(&challenge) > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : CBS_len(&challenge); |
| 459 | memcpy(random, CBS_data(&challenge), i); |
| 460 | |
| 461 | /* Write out an equivalent SSLv3 ClientHello. */ |
| 462 | if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, s->init_buf->max)) |
| 463 | { |
| 464 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_MALLOC_FAILURE); |
| 465 | goto err; |
| 466 | } |
| 467 | if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) || |
| 468 | !CBB_add_u24_length_prefixed(&client_hello, &hello_body) || |
| 469 | !CBB_add_u16(&hello_body, version) || |
| 470 | !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) || |
| 471 | /* No session id. */ |
| 472 | !CBB_add_u8(&hello_body, 0) || |
| 473 | !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites)) |
| 474 | { |
| 475 | CBB_cleanup(&client_hello); |
| 476 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 477 | goto err; |
| 478 | } |
| 479 | |
| 480 | /* Copy the cipher suites. */ |
| 481 | while (CBS_len(&cipher_specs) > 0) |
| 482 | { |
| 483 | uint32_t cipher_spec; |
| 484 | if (!CBS_get_u24(&cipher_specs, &cipher_spec)) |
| 485 | { |
| 486 | CBB_cleanup(&client_hello); |
| 487 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR); |
| 488 | goto err; |
| 489 | } |
| 490 | |
| 491 | /* Skip SSLv2 ciphers. */ |
| 492 | if ((cipher_spec & 0xff0000) != 0) |
| 493 | continue; |
| 494 | if (!CBB_add_u16(&cipher_suites, cipher_spec)) |
| 495 | { |
| 496 | CBB_cleanup(&client_hello); |
| 497 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 498 | goto err; |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | /* Add the null compression scheme and finish. */ |
| 503 | if (!CBB_add_u8(&hello_body, 1) || |
| 504 | !CBB_add_u8(&hello_body, 0) || |
| 505 | !CBB_finish(&client_hello, NULL, &len)) |
| 506 | { |
| 507 | CBB_cleanup(&client_hello); |
| 508 | OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR); |
| 509 | goto err; |
| 510 | } |
| 511 | |
| 512 | /* Mark the message for "re"-use by the version-specific |
| 513 | * method. */ |
| 514 | s->s3->tmp.reuse_message = 1; |
| 515 | s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO; |
| 516 | /* The handshake message header is 4 bytes. */ |
| 517 | s->s3->tmp.message_size = len - 4; |
| 518 | |
| 519 | /* Reset the record layer for SSL3. */ |
| 520 | assert(s->rstate == SSL_ST_READ_HEADER); |
| 521 | s->packet_length = 0; |
| 522 | s->s3->rbuf.left = 0; |
| 523 | s->s3->rbuf.offset = 0; |
| 524 | |
| 525 | s->state = SSL23_ST_SR_SWITCH_VERSION; |
| 526 | return 1; |
| 527 | err: |
| 528 | return -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 529 | } |