1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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.
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).
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.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
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)"
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
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.]
57 /* ====================================================================
58 * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60 * Redistribution and use in source and binary forms, with or without
61 * modification, are permitted provided that the following conditions
64 * 1. Redistributions of source code must retain the above copyright
65 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
86 * 6. Redistributions of any form whatsoever must retain the following
88 * "This product includes software developed by the OpenSSL Project
89 * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
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 * ====================================================================
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). */
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>
119 #include "ssl_locl.h"
121 static const SSL_METHOD *ssl23_get_server_method(int ver);
122 static int ssl23_get_client_hello(SSL *s);
123 static int ssl23_get_v2_client_hello(SSL *s);
125 static const SSL_METHOD *ssl23_get_server_method(int ver)
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());
139 IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
141 ssl_undefined_function,
142 ssl23_get_server_method)
144 int ssl23_accept(SSL *s)
147 void (*cb)(const SSL *ssl,int type,int val)=NULL;
152 ERR_clear_system_error();
154 if (s->info_callback != NULL)
156 else if (s->ctx->info_callback != NULL)
157 cb=s->ctx->info_callback;
160 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
170 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
171 case SSL_ST_OK|SSL_ST_ACCEPT:
174 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
176 /* s->version=SSL3_VERSION; */
177 s->type=SSL_ST_ACCEPT;
179 if (s->init_buf == NULL)
181 if ((buf=BUF_MEM_new()) == NULL)
186 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
194 ssl3_init_finished_mac(s);
196 s->state=SSL23_ST_SR_CLNT_HELLO;
197 s->ctx->stats.sess_accept++;
201 case SSL23_ST_SR_CLNT_HELLO:
203 ret = ssl23_get_client_hello(s);
204 if (ret <= 0) goto end;
207 case SSL23_ST_SR_V2_CLNT_HELLO:
208 ret = ssl23_get_v2_client_hello(s);
209 if (ret <= 0) goto end;
212 case SSL23_ST_SR_SWITCH_VERSION:
213 if (!ssl_init_wbio_buffer(s, 1))
219 s->state = SSL3_ST_SR_CLNT_HELLO_A;
220 s->method = ssl23_get_server_method(s->version);
221 assert(s->method != NULL);
222 s->handshake_func = s->method->ssl_accept;
225 /* NULL the callback; SSL_accept will call it instead. */
232 OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE);
238 if ((cb != NULL) && (s->state != state))
242 cb(s,SSL_CB_ACCEPT_LOOP,1);
249 cb(s,SSL_CB_ACCEPT_EXIT,ret);
253 /* ssl23_get_mutual_version determines the highest supported version for a
254 * client which reports a highest version of |client_version|. On success, it
255 * returns 1 and sets |*out_version| to the negotiated version. Otherwise, it
257 static int ssl23_get_mutual_version(SSL *s, int *out_version, uint16_t client_version)
259 if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2))
261 *out_version = TLS1_2_VERSION;
264 if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1))
266 *out_version = TLS1_1_VERSION;
269 if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1))
271 *out_version = TLS1_VERSION;
274 if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3))
276 *out_version = SSL3_VERSION;
282 static int ssl23_get_client_hello(SSL *s)
287 /* Sniff enough of the input to determine ClientHello type and the
289 if (!ssl3_setup_buffers(s)) goto err;
291 /* Read the initial 11 bytes of the input. This is sufficient to
292 * determine the client version for a ClientHello or a
295 * ClientHello (assuming client_version is unfragmented):
298 * 1-2 version > record header
301 * 6-8 length > Client Hello message
302 * 9-10 client_version /
309 * 5-6 cipher_spec_length
310 * 7-8 session_id_length
311 * 9-10 challenge_length
313 n = ssl23_read_bytes(s, 11);
320 /* Some dedicated error codes for protocol mixups should the application
321 * wish to interpret them differently. (These do not overlap with
322 * ClientHello or V2ClientHello.) */
323 if ((strncmp("GET ", (char *)p, 4) == 0) ||
324 (strncmp("POST ",(char *)p, 5) == 0) ||
325 (strncmp("HEAD ",(char *)p, 5) == 0) ||
326 (strncmp("PUT ", (char *)p, 4) == 0))
328 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST);
331 if (strncmp("CONNECT",(char *)p, 7) == 0)
333 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST);
337 /* Determine if this is a ClientHello or V2ClientHello. */
338 if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
340 /* This is a V2ClientHello. Determine the version to
342 uint16_t client_version = (p[3] << 8) | p[4];
343 if (!ssl23_get_mutual_version(s, &s->version, client_version))
345 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
348 /* Parse the entire V2ClientHello. */
349 s->state = SSL23_ST_SR_V2_CLNT_HELLO;
351 else if ((p[0] == SSL3_RT_HANDSHAKE) &&
352 (p[1] >= SSL3_VERSION_MAJOR) &&
353 (p[5] == SSL3_MT_CLIENT_HELLO))
355 /* This is a fragment of a ClientHello. We look at the
356 * client_hello to negotiate the version. However, this
357 * is difficult if we have only a pathologically small
358 * fragment. No known client fragments ClientHello like
359 * this, so we simply reject such connections to avoid
360 * protocol version downgrade attacks. */
361 uint16_t record_length = (p[3] << 8) | p[4];
362 uint16_t client_version;
363 if (record_length < 6)
365 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_SMALL);
369 client_version = (p[9] << 8) | p[10];
370 if (!ssl23_get_mutual_version(s, &s->version, client_version))
372 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
376 /* Reset the record-layer state for SSL3. */
377 assert(s->rstate == SSL_ST_READ_HEADER);
378 s->s3->rbuf.left = s->packet_length;
379 s->s3->rbuf.offset = 0;
380 s->packet_length = 0;
382 /* Ready to switch versions. */
383 s->state = SSL23_ST_SR_SWITCH_VERSION;
391 static int ssl23_get_v2_client_hello(SSL *s)
397 CBS v2_client_hello, cipher_specs, session_id, challenge;
398 size_t msg_length, len;
400 uint16_t version, cipher_spec_length, session_id_length, challenge_length;
401 CBB client_hello, hello_body, cipher_suites;
402 uint8_t random[SSL3_RANDOM_SIZE];
404 /* Read the remainder of the V2ClientHello. We have previously read 11
405 * bytes in ssl23_get_client_hello. */
407 msg_length = ((p[0] & 0x7f) << 8) | p[1];
408 if (msg_length > (1024 * 4))
410 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
413 if (msg_length < 11 - 2)
415 /* Reject lengths that are too short early. We have already read
416 * 11 bytes, so we should not attempt to process an (invalid)
417 * V2ClientHello which would be shorter than that. */
418 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_LENGTH_MISMATCH);
421 n = ssl23_read_bytes(s, msg_length + 2);
424 assert(n == s->packet_length);
426 /* The V2ClientHello without the length is incorporated into the
428 ssl3_finish_mac(s, s->packet + 2, s->packet_length - 2);
430 s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
432 CBS_init(&v2_client_hello, s->packet + 2, s->packet_length - 2);
433 if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
434 !CBS_get_u16(&v2_client_hello, &version) ||
435 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
436 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
437 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
438 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
439 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
440 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
441 CBS_len(&v2_client_hello) != 0)
443 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR);
447 /* msg_type has already been checked. */
448 assert(msg_type == SSL2_MT_CLIENT_HELLO);
450 /* The client_random is the V2ClientHello challenge. Truncate or
451 * left-pad with zeros as needed. */
452 memset(random, 0, SSL3_RANDOM_SIZE);
453 i = (CBS_len(&challenge) > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : CBS_len(&challenge);
454 memcpy(random, CBS_data(&challenge), i);
456 /* Write out an equivalent SSLv3 ClientHello. */
457 if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, s->init_buf->max))
459 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
462 if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
463 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
464 !CBB_add_u16(&hello_body, version) ||
465 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
467 !CBB_add_u8(&hello_body, 0) ||
468 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites))
470 CBB_cleanup(&client_hello);
471 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
475 /* Copy the cipher suites. */
476 while (CBS_len(&cipher_specs) > 0)
478 uint32_t cipher_spec;
479 if (!CBS_get_u24(&cipher_specs, &cipher_spec))
481 CBB_cleanup(&client_hello);
482 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR);
486 /* Skip SSLv2 ciphers. */
487 if ((cipher_spec & 0xff0000) != 0)
489 if (!CBB_add_u16(&cipher_suites, cipher_spec))
491 CBB_cleanup(&client_hello);
492 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
497 /* Add the null compression scheme and finish. */
498 if (!CBB_add_u8(&hello_body, 1) ||
499 !CBB_add_u8(&hello_body, 0) ||
500 !CBB_finish(&client_hello, NULL, &len))
502 CBB_cleanup(&client_hello);
503 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
507 /* Mark the message for "re"-use by the version-specific
509 s->s3->tmp.reuse_message = 1;
510 s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
511 /* The handshake message header is 4 bytes. */
512 s->s3->tmp.message_size = len - 4;
514 /* Reset the record layer for SSL3. */
515 assert(s->rstate == SSL_ST_READ_HEADER);
516 s->packet_length = 0;
517 s->s3->rbuf.left = 0;
518 s->s3->rbuf.offset = 0;
520 s->state = SSL23_ST_SR_SWITCH_VERSION;