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). */
111 #include <openssl/buf.h>
112 #include <openssl/err.h>
113 #include <openssl/evp.h>
114 #include <openssl/mem.h>
115 #include <openssl/obj.h>
116 #include <openssl/rand.h>
118 #include "ssl_locl.h"
120 static const SSL_METHOD *ssl23_get_server_method(int ver);
121 int ssl23_get_client_hello(SSL *s);
122 static const SSL_METHOD *ssl23_get_server_method(int ver)
124 if (ver == SSL3_VERSION)
125 return(SSLv3_server_method());
126 else if (ver == TLS1_VERSION)
127 return(TLSv1_server_method());
128 else if (ver == TLS1_1_VERSION)
129 return(TLSv1_1_server_method());
130 else if (ver == TLS1_2_VERSION)
131 return(TLSv1_2_server_method());
136 IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
138 ssl_undefined_function,
139 ssl23_get_server_method)
141 int ssl23_accept(SSL *s)
144 void (*cb)(const SSL *ssl,int type,int val)=NULL;
149 ERR_clear_system_error();
151 if (s->info_callback != NULL)
153 else if (s->ctx->info_callback != NULL)
154 cb=s->ctx->info_callback;
157 if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
167 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
168 case SSL_ST_OK|SSL_ST_ACCEPT:
171 if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
173 /* s->version=SSL3_VERSION; */
174 s->type=SSL_ST_ACCEPT;
176 if (s->init_buf == NULL)
178 if ((buf=BUF_MEM_new()) == NULL)
183 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
191 ssl3_init_finished_mac(s);
193 s->state=SSL23_ST_SR_CLNT_HELLO_A;
194 s->ctx->stats.sess_accept++;
198 case SSL23_ST_SR_CLNT_HELLO_A:
199 case SSL23_ST_SR_CLNT_HELLO_B:
202 ret=ssl23_get_client_hello(s);
203 if (ret >= 0) cb=NULL;
208 OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE);
214 if ((cb != NULL) && (s->state != state))
218 cb(s,SSL_CB_ACCEPT_LOOP,1);
225 cb(s,SSL_CB_ACCEPT_EXIT,ret);
230 int ssl23_get_client_hello(SSL *s)
232 char buf_space[11]; /* Request this many bytes in initial read.
233 * We can detect SSL 3.0/TLS 1.0 Client Hellos
234 * ('type == 3') correctly only when the following
235 * is in a single record, which is not guaranteed by
236 * the protocol specification:
239 * 1/2 version > record header
242 * 6-8 length > Client Hello message
243 * 9/10 client_version /
245 char *buf= &(buf_space[0]);
246 unsigned char *p,*d,*d_len,*dd;
248 unsigned int csl,sil,cl;
253 if (s->state == SSL23_ST_SR_CLNT_HELLO_A)
255 /* read the initial header */
258 if (!ssl3_setup_buffers(s)) goto err;
260 n=ssl23_read_bytes(s, sizeof buf_space);
261 if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
267 if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
272 if ((p[3] == 0x00) && (p[4] == 0x02))
274 v[0]=p[3]; v[1]=p[4];
276 if (!(s->options & SSL_OP_NO_SSLv2))
279 else if (p[3] == SSL3_VERSION_MAJOR)
281 v[0]=p[3]; v[1]=p[4];
283 if (p[4] >= TLS1_VERSION_MINOR)
285 if (p[4] >= TLS1_2_VERSION_MINOR &&
286 !(s->options & SSL_OP_NO_TLSv1_2))
288 s->version=TLS1_2_VERSION;
289 s->state=SSL23_ST_SR_CLNT_HELLO_B;
291 else if (p[4] >= TLS1_1_VERSION_MINOR &&
292 !(s->options & SSL_OP_NO_TLSv1_1))
294 s->version=TLS1_1_VERSION;
295 /* type=2; */ /* done later to survive restarts */
296 s->state=SSL23_ST_SR_CLNT_HELLO_B;
298 else if (!(s->options & SSL_OP_NO_TLSv1))
300 s->version=TLS1_VERSION;
301 /* type=2; */ /* done later to survive restarts */
302 s->state=SSL23_ST_SR_CLNT_HELLO_B;
304 else if (!(s->options & SSL_OP_NO_SSLv3))
306 s->version=SSL3_VERSION;
308 s->state=SSL23_ST_SR_CLNT_HELLO_B;
310 else if (!(s->options & SSL_OP_NO_SSLv2))
315 else if (!(s->options & SSL_OP_NO_SSLv3))
317 s->version=SSL3_VERSION;
319 s->state=SSL23_ST_SR_CLNT_HELLO_B;
321 else if (!(s->options & SSL_OP_NO_SSLv2))
326 else if ((p[0] == SSL3_RT_HANDSHAKE) &&
327 (p[1] == SSL3_VERSION_MAJOR) &&
328 (p[5] == SSL3_MT_CLIENT_HELLO) &&
329 ((p[3] == 0 && p[4] < 5 /* silly record length? */)
333 * SSLv3 or tls1 header
336 v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
337 /* We must look at client_version inside the Client Hello message
338 * to get the correct minor version.
339 * However if we have only a pathologically small fragment of the
340 * Client Hello message, this would be difficult, and we'd have
341 * to read more records to find out.
342 * No known SSL 3.0 client fragments ClientHello like this,
343 * so we simply assume TLS 1.0 to avoid protocol version downgrade
345 if (p[3] == 0 && p[4] < 6)
348 OPENSSL_PUT_ERROR(SSL, XXX, SSL_R_RECORD_TOO_SMALL);
351 v[1] = TLS1_VERSION_MINOR;
354 /* if major version number > 3 set minor to a value
355 * which will use the highest version 3 we support.
356 * If TLS 2.0 ever appears we will need to revise
359 else if (p[9] > SSL3_VERSION_MAJOR)
362 v[1]=p[10]; /* minor version according to client_version */
363 if (v[1] >= TLS1_VERSION_MINOR)
365 if (v[1] >= TLS1_2_VERSION_MINOR &&
366 !(s->options & SSL_OP_NO_TLSv1_2))
368 s->version=TLS1_2_VERSION;
371 else if (v[1] >= TLS1_1_VERSION_MINOR &&
372 !(s->options & SSL_OP_NO_TLSv1_1))
374 s->version=TLS1_1_VERSION;
377 else if (!(s->options & SSL_OP_NO_TLSv1))
379 s->version=TLS1_VERSION;
382 else if (!(s->options & SSL_OP_NO_SSLv3))
384 s->version=SSL3_VERSION;
390 /* client requests SSL 3.0 */
391 if (!(s->options & SSL_OP_NO_SSLv3))
393 s->version=SSL3_VERSION;
396 else if (!(s->options & SSL_OP_NO_TLSv1))
398 /* we won't be able to use TLS of course,
399 * but this will send an appropriate alert */
400 s->version=TLS1_VERSION;
405 else if ((strncmp("GET ", (char *)p,4) == 0) ||
406 (strncmp("POST ",(char *)p,5) == 0) ||
407 (strncmp("HEAD ",(char *)p,5) == 0) ||
408 (strncmp("PUT ", (char *)p,4) == 0))
410 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST);
413 else if (strncmp("CONNECT",(char *)p,7) == 0)
415 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST);
420 if (s->version < TLS1_2_VERSION && tls1_suiteb(s))
422 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
426 if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
428 /* we have SSLv3/TLSv1 in an SSLv2 header
429 * (other cases skip this state) */
433 v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
436 n=((p[0]&0x7f)<<8)|p[1];
439 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_LARGE);
443 j=ssl23_read_bytes(s,n+2);
444 if (j <= 0) return(j);
446 ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
448 s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
455 d=(unsigned char *)s->init_buf->data;
456 if ((csl+sil+cl+11) != s->packet_length) /* We can't have TLS extensions in SSL 2.0 format
457 * Client Hello, can we? Error condition should be
460 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_LENGTH_MISMATCH);
464 /* record header: msg_type ... */
465 *(d++) = SSL3_MT_CLIENT_HELLO;
466 /* ... and length (actual value will be written later) */
471 *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
474 /* lets populate the random area */
475 /* get the challenge_length */
476 i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
477 memset(d,0,SSL3_RANDOM_SIZE);
478 memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
481 /* no session-id reuse */
488 for (i=0; i<csl; i+=3)
490 if (p[i] != 0) continue;
502 /* copy any remaining data with may be extensions */
504 while (p < s->packet+s->packet_length)
510 i = (d-(unsigned char *)s->init_buf->data) - 4;
511 l2n3((long)i, d_len);
513 /* get the data reused from the init_buf */
514 s->s3->tmp.reuse_message=1;
515 s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
516 s->s3->tmp.message_size=i;
519 /* imaginary new state (for program structure): */
520 /* s->state = SSL23_SR_CLNT_HELLO_C */
524 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
528 if ((type == 2) || (type == 3))
530 /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
532 if (!ssl_init_wbio_buffer(s,1)) goto err;
534 /* we are in this state */
535 s->state=SSL3_ST_SR_CLNT_HELLO_A;
539 /* put the 'n' bytes we have read into the input buffer
541 s->rstate=SSL_ST_READ_HEADER;
543 if (s->s3->rbuf.buf == NULL)
544 if (!ssl3_setup_read_buffer(s))
547 s->packet= &(s->s3->rbuf.buf[0]);
548 memcpy(s->packet,buf,n);
550 s->s3->rbuf.offset=0;
556 s->s3->rbuf.offset=0;
558 if (s->version == TLS1_2_VERSION)
559 s->method = TLSv1_2_server_method();
560 else if (s->version == TLS1_1_VERSION)
561 s->method = TLSv1_1_server_method();
562 else if (s->version == TLS1_VERSION)
563 s->method = TLSv1_server_method();
565 s->method = SSLv3_server_method();
566 #if 0 /* ssl3_get_client_hello does this */
567 s->client_version=(v[0]<<8)|v[1];
569 s->handshake_func=s->method->ssl_accept;
572 if ((type < 1) || (type > 3))
575 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNKNOWN_PROTOCOL);
580 if (buf != buf_space) OPENSSL_free(buf);
581 return(SSL_accept(s));
583 if (buf != buf_space) OPENSSL_free(buf);