1ebfedea0SLionel Sambuc /* ssl/s23_srvr.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc * All rights reserved.
4ebfedea0SLionel Sambuc *
5ebfedea0SLionel Sambuc * This package is an SSL implementation written
6ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
11ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc * the code are not to be removed.
18ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc *
23ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc * are met:
26ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc * must display the following acknowledgement:
33ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc *
41ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc * SUCH DAMAGE.
52ebfedea0SLionel Sambuc *
53ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
55ebfedea0SLionel Sambuc * copied and put under another distribution licence
56ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc */
58ebfedea0SLionel Sambuc /* ====================================================================
59ebfedea0SLionel Sambuc * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
60ebfedea0SLionel Sambuc *
61ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
62ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
63ebfedea0SLionel Sambuc * are met:
64ebfedea0SLionel Sambuc *
65ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
66ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
67ebfedea0SLionel Sambuc *
68ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
69ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
70ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
71ebfedea0SLionel Sambuc * distribution.
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
74ebfedea0SLionel Sambuc * software must display the following acknowledgment:
75ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
76ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77ebfedea0SLionel Sambuc *
78ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
80ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
81ebfedea0SLionel Sambuc * openssl-core@openssl.org.
82ebfedea0SLionel Sambuc *
83ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
84ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
85ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
86ebfedea0SLionel Sambuc *
87ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
88ebfedea0SLionel Sambuc * acknowledgment:
89ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
90ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91ebfedea0SLionel Sambuc *
92ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
96ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
104ebfedea0SLionel Sambuc * ====================================================================
105ebfedea0SLionel Sambuc *
106ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
107ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
108ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc */
111ebfedea0SLionel Sambuc
112ebfedea0SLionel Sambuc #include <stdio.h>
113ebfedea0SLionel Sambuc #include "ssl_locl.h"
114ebfedea0SLionel Sambuc #include <openssl/buffer.h>
115ebfedea0SLionel Sambuc #include <openssl/rand.h>
116ebfedea0SLionel Sambuc #include <openssl/objects.h>
117ebfedea0SLionel Sambuc #include <openssl/evp.h>
118ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
119ebfedea0SLionel Sambuc # include <openssl/fips.h>
120ebfedea0SLionel Sambuc #endif
121ebfedea0SLionel Sambuc
122ebfedea0SLionel Sambuc static const SSL_METHOD *ssl23_get_server_method(int ver);
123ebfedea0SLionel Sambuc int ssl23_get_client_hello(SSL *s);
ssl23_get_server_method(int ver)124ebfedea0SLionel Sambuc static const SSL_METHOD *ssl23_get_server_method(int ver)
125ebfedea0SLionel Sambuc {
126ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SSL2
127ebfedea0SLionel Sambuc if (ver == SSL2_VERSION)
128ebfedea0SLionel Sambuc return (SSLv2_server_method());
129ebfedea0SLionel Sambuc #endif
130*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_SSL3
131ebfedea0SLionel Sambuc if (ver == SSL3_VERSION)
132ebfedea0SLionel Sambuc return (SSLv3_server_method());
133*0a6a1f1dSLionel Sambuc #endif
134*0a6a1f1dSLionel Sambuc if (ver == TLS1_VERSION)
135ebfedea0SLionel Sambuc return (TLSv1_server_method());
136ebfedea0SLionel Sambuc else if (ver == TLS1_1_VERSION)
137ebfedea0SLionel Sambuc return (TLSv1_1_server_method());
138ebfedea0SLionel Sambuc else if (ver == TLS1_2_VERSION)
139ebfedea0SLionel Sambuc return (TLSv1_2_server_method());
140ebfedea0SLionel Sambuc else
141ebfedea0SLionel Sambuc return (NULL);
142ebfedea0SLionel Sambuc }
143ebfedea0SLionel Sambuc
IMPLEMENT_ssl23_meth_func(SSLv23_server_method,ssl23_accept,ssl_undefined_function,ssl23_get_server_method)144ebfedea0SLionel Sambuc IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
145ebfedea0SLionel Sambuc ssl23_accept,
146*0a6a1f1dSLionel Sambuc ssl_undefined_function, ssl23_get_server_method)
147ebfedea0SLionel Sambuc
148ebfedea0SLionel Sambuc int ssl23_accept(SSL *s)
149ebfedea0SLionel Sambuc {
150ebfedea0SLionel Sambuc BUF_MEM *buf;
151ebfedea0SLionel Sambuc unsigned long Time = (unsigned long)time(NULL);
152ebfedea0SLionel Sambuc void (*cb) (const SSL *ssl, int type, int val) = NULL;
153ebfedea0SLionel Sambuc int ret = -1;
154ebfedea0SLionel Sambuc int new_state, state;
155ebfedea0SLionel Sambuc
156ebfedea0SLionel Sambuc RAND_add(&Time, sizeof(Time), 0);
157ebfedea0SLionel Sambuc ERR_clear_error();
158ebfedea0SLionel Sambuc clear_sys_error();
159ebfedea0SLionel Sambuc
160ebfedea0SLionel Sambuc if (s->info_callback != NULL)
161ebfedea0SLionel Sambuc cb = s->info_callback;
162ebfedea0SLionel Sambuc else if (s->ctx->info_callback != NULL)
163ebfedea0SLionel Sambuc cb = s->ctx->info_callback;
164ebfedea0SLionel Sambuc
165ebfedea0SLionel Sambuc s->in_handshake++;
166*0a6a1f1dSLionel Sambuc if (!SSL_in_init(s) || SSL_in_before(s))
167*0a6a1f1dSLionel Sambuc SSL_clear(s);
168ebfedea0SLionel Sambuc
169*0a6a1f1dSLionel Sambuc for (;;) {
170ebfedea0SLionel Sambuc state = s->state;
171ebfedea0SLionel Sambuc
172*0a6a1f1dSLionel Sambuc switch (s->state) {
173ebfedea0SLionel Sambuc case SSL_ST_BEFORE:
174ebfedea0SLionel Sambuc case SSL_ST_ACCEPT:
175ebfedea0SLionel Sambuc case SSL_ST_BEFORE | SSL_ST_ACCEPT:
176ebfedea0SLionel Sambuc case SSL_ST_OK | SSL_ST_ACCEPT:
177ebfedea0SLionel Sambuc
178ebfedea0SLionel Sambuc s->server = 1;
179*0a6a1f1dSLionel Sambuc if (cb != NULL)
180*0a6a1f1dSLionel Sambuc cb(s, SSL_CB_HANDSHAKE_START, 1);
181ebfedea0SLionel Sambuc
182ebfedea0SLionel Sambuc /* s->version=SSL3_VERSION; */
183ebfedea0SLionel Sambuc s->type = SSL_ST_ACCEPT;
184ebfedea0SLionel Sambuc
185*0a6a1f1dSLionel Sambuc if (s->init_buf == NULL) {
186*0a6a1f1dSLionel Sambuc if ((buf = BUF_MEM_new()) == NULL) {
187ebfedea0SLionel Sambuc ret = -1;
188ebfedea0SLionel Sambuc goto end;
189ebfedea0SLionel Sambuc }
190*0a6a1f1dSLionel Sambuc if (!BUF_MEM_grow(buf, SSL3_RT_MAX_PLAIN_LENGTH)) {
191*0a6a1f1dSLionel Sambuc BUF_MEM_free(buf);
192ebfedea0SLionel Sambuc ret = -1;
193ebfedea0SLionel Sambuc goto end;
194ebfedea0SLionel Sambuc }
195ebfedea0SLionel Sambuc s->init_buf = buf;
196ebfedea0SLionel Sambuc }
197ebfedea0SLionel Sambuc
198ebfedea0SLionel Sambuc ssl3_init_finished_mac(s);
199ebfedea0SLionel Sambuc
200ebfedea0SLionel Sambuc s->state = SSL23_ST_SR_CLNT_HELLO_A;
201ebfedea0SLionel Sambuc s->ctx->stats.sess_accept++;
202ebfedea0SLionel Sambuc s->init_num = 0;
203ebfedea0SLionel Sambuc break;
204ebfedea0SLionel Sambuc
205ebfedea0SLionel Sambuc case SSL23_ST_SR_CLNT_HELLO_A:
206ebfedea0SLionel Sambuc case SSL23_ST_SR_CLNT_HELLO_B:
207ebfedea0SLionel Sambuc
208ebfedea0SLionel Sambuc s->shutdown = 0;
209ebfedea0SLionel Sambuc ret = ssl23_get_client_hello(s);
210*0a6a1f1dSLionel Sambuc if (ret >= 0)
211*0a6a1f1dSLionel Sambuc cb = NULL;
212ebfedea0SLionel Sambuc goto end;
213ebfedea0SLionel Sambuc /* break; */
214ebfedea0SLionel Sambuc
215ebfedea0SLionel Sambuc default:
216ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_ACCEPT, SSL_R_UNKNOWN_STATE);
217ebfedea0SLionel Sambuc ret = -1;
218ebfedea0SLionel Sambuc goto end;
219ebfedea0SLionel Sambuc /* break; */
220ebfedea0SLionel Sambuc }
221ebfedea0SLionel Sambuc
222*0a6a1f1dSLionel Sambuc if ((cb != NULL) && (s->state != state)) {
223ebfedea0SLionel Sambuc new_state = s->state;
224ebfedea0SLionel Sambuc s->state = state;
225ebfedea0SLionel Sambuc cb(s, SSL_CB_ACCEPT_LOOP, 1);
226ebfedea0SLionel Sambuc s->state = new_state;
227ebfedea0SLionel Sambuc }
228ebfedea0SLionel Sambuc }
229ebfedea0SLionel Sambuc end:
230ebfedea0SLionel Sambuc s->in_handshake--;
231ebfedea0SLionel Sambuc if (cb != NULL)
232ebfedea0SLionel Sambuc cb(s, SSL_CB_ACCEPT_EXIT, ret);
233ebfedea0SLionel Sambuc return (ret);
234ebfedea0SLionel Sambuc }
235ebfedea0SLionel Sambuc
ssl23_get_client_hello(SSL * s)236ebfedea0SLionel Sambuc int ssl23_get_client_hello(SSL *s)
237ebfedea0SLionel Sambuc {
238*0a6a1f1dSLionel Sambuc /*-
239*0a6a1f1dSLionel Sambuc * Request this many bytes in initial read.
240ebfedea0SLionel Sambuc * We can detect SSL 3.0/TLS 1.0 Client Hellos
241ebfedea0SLionel Sambuc * ('type == 3') correctly only when the following
242ebfedea0SLionel Sambuc * is in a single record, which is not guaranteed by
243ebfedea0SLionel Sambuc * the protocol specification:
244ebfedea0SLionel Sambuc * Byte Content
245ebfedea0SLionel Sambuc * 0 type \
246ebfedea0SLionel Sambuc * 1/2 version > record header
247ebfedea0SLionel Sambuc * 3/4 length /
248ebfedea0SLionel Sambuc * 5 msg_type \
249ebfedea0SLionel Sambuc * 6-8 length > Client Hello message
250ebfedea0SLionel Sambuc * 9/10 client_version /
251ebfedea0SLionel Sambuc */
252*0a6a1f1dSLionel Sambuc char buf_space[11];
253ebfedea0SLionel Sambuc char *buf = &(buf_space[0]);
254ebfedea0SLionel Sambuc unsigned char *p, *d, *d_len, *dd;
255ebfedea0SLionel Sambuc unsigned int i;
256ebfedea0SLionel Sambuc unsigned int csl, sil, cl;
257ebfedea0SLionel Sambuc int n = 0, j;
258ebfedea0SLionel Sambuc int type = 0;
259ebfedea0SLionel Sambuc int v[2];
260ebfedea0SLionel Sambuc
261*0a6a1f1dSLionel Sambuc if (s->state == SSL23_ST_SR_CLNT_HELLO_A) {
262ebfedea0SLionel Sambuc /* read the initial header */
263ebfedea0SLionel Sambuc v[0] = v[1] = 0;
264ebfedea0SLionel Sambuc
265*0a6a1f1dSLionel Sambuc if (!ssl3_setup_buffers(s))
266*0a6a1f1dSLionel Sambuc goto err;
267ebfedea0SLionel Sambuc
268ebfedea0SLionel Sambuc n = ssl23_read_bytes(s, sizeof buf_space);
269*0a6a1f1dSLionel Sambuc if (n != sizeof buf_space)
270*0a6a1f1dSLionel Sambuc return (n); /* n == -1 || n == 0 */
271ebfedea0SLionel Sambuc
272ebfedea0SLionel Sambuc p = s->packet;
273ebfedea0SLionel Sambuc
274ebfedea0SLionel Sambuc memcpy(buf, p, n);
275ebfedea0SLionel Sambuc
276*0a6a1f1dSLionel Sambuc if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO)) {
277ebfedea0SLionel Sambuc /*
278ebfedea0SLionel Sambuc * SSLv2 header
279ebfedea0SLionel Sambuc */
280*0a6a1f1dSLionel Sambuc if ((p[3] == 0x00) && (p[4] == 0x02)) {
281*0a6a1f1dSLionel Sambuc v[0] = p[3];
282*0a6a1f1dSLionel Sambuc v[1] = p[4];
283ebfedea0SLionel Sambuc /* SSLv2 */
284ebfedea0SLionel Sambuc if (!(s->options & SSL_OP_NO_SSLv2))
285ebfedea0SLionel Sambuc type = 1;
286*0a6a1f1dSLionel Sambuc } else if (p[3] == SSL3_VERSION_MAJOR) {
287*0a6a1f1dSLionel Sambuc v[0] = p[3];
288*0a6a1f1dSLionel Sambuc v[1] = p[4];
289ebfedea0SLionel Sambuc /* SSLv3/TLSv1 */
290*0a6a1f1dSLionel Sambuc if (p[4] >= TLS1_VERSION_MINOR) {
291ebfedea0SLionel Sambuc if (p[4] >= TLS1_2_VERSION_MINOR &&
292*0a6a1f1dSLionel Sambuc !(s->options & SSL_OP_NO_TLSv1_2)) {
293ebfedea0SLionel Sambuc s->version = TLS1_2_VERSION;
294ebfedea0SLionel Sambuc s->state = SSL23_ST_SR_CLNT_HELLO_B;
295*0a6a1f1dSLionel Sambuc } else if (p[4] >= TLS1_1_VERSION_MINOR &&
296*0a6a1f1dSLionel Sambuc !(s->options & SSL_OP_NO_TLSv1_1)) {
297ebfedea0SLionel Sambuc s->version = TLS1_1_VERSION;
298*0a6a1f1dSLionel Sambuc /*
299*0a6a1f1dSLionel Sambuc * type=2;
300*0a6a1f1dSLionel Sambuc *//*
301*0a6a1f1dSLionel Sambuc * done later to survive restarts
302*0a6a1f1dSLionel Sambuc */
303ebfedea0SLionel Sambuc s->state = SSL23_ST_SR_CLNT_HELLO_B;
304*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_TLSv1)) {
305ebfedea0SLionel Sambuc s->version = TLS1_VERSION;
306*0a6a1f1dSLionel Sambuc /*
307*0a6a1f1dSLionel Sambuc * type=2;
308*0a6a1f1dSLionel Sambuc *//*
309*0a6a1f1dSLionel Sambuc * done later to survive restarts
310*0a6a1f1dSLionel Sambuc */
311ebfedea0SLionel Sambuc s->state = SSL23_ST_SR_CLNT_HELLO_B;
312*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_SSLv3)) {
313ebfedea0SLionel Sambuc s->version = SSL3_VERSION;
314ebfedea0SLionel Sambuc /* type=2; */
315ebfedea0SLionel Sambuc s->state = SSL23_ST_SR_CLNT_HELLO_B;
316*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_SSLv2)) {
317ebfedea0SLionel Sambuc type = 1;
318ebfedea0SLionel Sambuc }
319*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_SSLv3)) {
320ebfedea0SLionel Sambuc s->version = SSL3_VERSION;
321ebfedea0SLionel Sambuc /* type=2; */
322ebfedea0SLionel Sambuc s->state = SSL23_ST_SR_CLNT_HELLO_B;
323*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_SSLv2))
324ebfedea0SLionel Sambuc type = 1;
325ebfedea0SLionel Sambuc
326ebfedea0SLionel Sambuc }
327ebfedea0SLionel Sambuc }
328*0a6a1f1dSLionel Sambuc /* p[4] < 5 ... silly record length? */
329ebfedea0SLionel Sambuc else if ((p[0] == SSL3_RT_HANDSHAKE) &&
330ebfedea0SLionel Sambuc (p[1] == SSL3_VERSION_MAJOR) &&
331*0a6a1f1dSLionel Sambuc (p[5] == SSL3_MT_CLIENT_HELLO) && ((p[3] == 0 && p[4] < 5)
332*0a6a1f1dSLionel Sambuc || (p[9] >= p[1]))) {
333ebfedea0SLionel Sambuc /*
334ebfedea0SLionel Sambuc * SSLv3 or tls1 header
335ebfedea0SLionel Sambuc */
336ebfedea0SLionel Sambuc
337ebfedea0SLionel Sambuc v[0] = p[1]; /* major version (= SSL3_VERSION_MAJOR) */
338*0a6a1f1dSLionel Sambuc /*
339*0a6a1f1dSLionel Sambuc * We must look at client_version inside the Client Hello message
340*0a6a1f1dSLionel Sambuc * to get the correct minor version. However if we have only a
341*0a6a1f1dSLionel Sambuc * pathologically small fragment of the Client Hello message, this
342*0a6a1f1dSLionel Sambuc * would be difficult, and we'd have to read more records to find
343*0a6a1f1dSLionel Sambuc * out. No known SSL 3.0 client fragments ClientHello like this,
344*0a6a1f1dSLionel Sambuc * so we simply reject such connections to avoid protocol version
345*0a6a1f1dSLionel Sambuc * downgrade attacks.
346*0a6a1f1dSLionel Sambuc */
347*0a6a1f1dSLionel Sambuc if (p[3] == 0 && p[4] < 6) {
348ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_RECORD_TOO_SMALL);
349ebfedea0SLionel Sambuc goto err;
350ebfedea0SLionel Sambuc }
351*0a6a1f1dSLionel Sambuc /*
352*0a6a1f1dSLionel Sambuc * if major version number > 3 set minor to a value which will
353*0a6a1f1dSLionel Sambuc * use the highest version 3 we support. If TLS 2.0 ever appears
354*0a6a1f1dSLionel Sambuc * we will need to revise this....
355ebfedea0SLionel Sambuc */
356*0a6a1f1dSLionel Sambuc if (p[9] > SSL3_VERSION_MAJOR)
357ebfedea0SLionel Sambuc v[1] = 0xff;
358ebfedea0SLionel Sambuc else
359ebfedea0SLionel Sambuc v[1] = p[10]; /* minor version according to client_version */
360*0a6a1f1dSLionel Sambuc if (v[1] >= TLS1_VERSION_MINOR) {
361ebfedea0SLionel Sambuc if (v[1] >= TLS1_2_VERSION_MINOR &&
362*0a6a1f1dSLionel Sambuc !(s->options & SSL_OP_NO_TLSv1_2)) {
363ebfedea0SLionel Sambuc s->version = TLS1_2_VERSION;
364ebfedea0SLionel Sambuc type = 3;
365*0a6a1f1dSLionel Sambuc } else if (v[1] >= TLS1_1_VERSION_MINOR &&
366*0a6a1f1dSLionel Sambuc !(s->options & SSL_OP_NO_TLSv1_1)) {
367ebfedea0SLionel Sambuc s->version = TLS1_1_VERSION;
368ebfedea0SLionel Sambuc type = 3;
369*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_TLSv1)) {
370ebfedea0SLionel Sambuc s->version = TLS1_VERSION;
371ebfedea0SLionel Sambuc type = 3;
372*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_SSLv3)) {
373ebfedea0SLionel Sambuc s->version = SSL3_VERSION;
374ebfedea0SLionel Sambuc type = 3;
375ebfedea0SLionel Sambuc }
376*0a6a1f1dSLionel Sambuc } else {
377ebfedea0SLionel Sambuc /* client requests SSL 3.0 */
378*0a6a1f1dSLionel Sambuc if (!(s->options & SSL_OP_NO_SSLv3)) {
379ebfedea0SLionel Sambuc s->version = SSL3_VERSION;
380ebfedea0SLionel Sambuc type = 3;
381*0a6a1f1dSLionel Sambuc } else if (!(s->options & SSL_OP_NO_TLSv1)) {
382*0a6a1f1dSLionel Sambuc /*
383*0a6a1f1dSLionel Sambuc * we won't be able to use TLS of course, but this will
384*0a6a1f1dSLionel Sambuc * send an appropriate alert
385*0a6a1f1dSLionel Sambuc */
386ebfedea0SLionel Sambuc s->version = TLS1_VERSION;
387ebfedea0SLionel Sambuc type = 3;
388ebfedea0SLionel Sambuc }
389ebfedea0SLionel Sambuc }
390*0a6a1f1dSLionel Sambuc } else if ((strncmp("GET ", (char *)p, 4) == 0) ||
391ebfedea0SLionel Sambuc (strncmp("POST ", (char *)p, 5) == 0) ||
392ebfedea0SLionel Sambuc (strncmp("HEAD ", (char *)p, 5) == 0) ||
393*0a6a1f1dSLionel Sambuc (strncmp("PUT ", (char *)p, 4) == 0)) {
394ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_HTTP_REQUEST);
395ebfedea0SLionel Sambuc goto err;
396*0a6a1f1dSLionel Sambuc } else if (strncmp("CONNECT", (char *)p, 7) == 0) {
397ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_HTTPS_PROXY_REQUEST);
398ebfedea0SLionel Sambuc goto err;
399ebfedea0SLionel Sambuc }
400ebfedea0SLionel Sambuc }
401ebfedea0SLionel Sambuc
402*0a6a1f1dSLionel Sambuc /* ensure that TLS_MAX_VERSION is up-to-date */
403*0a6a1f1dSLionel Sambuc OPENSSL_assert(s->version <= TLS_MAX_VERSION);
404*0a6a1f1dSLionel Sambuc
405ebfedea0SLionel Sambuc #ifdef OPENSSL_FIPS
406*0a6a1f1dSLionel Sambuc if (FIPS_mode() && (s->version < TLS1_VERSION)) {
407ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
408ebfedea0SLionel Sambuc SSL_R_ONLY_TLS_ALLOWED_IN_FIPS_MODE);
409ebfedea0SLionel Sambuc goto err;
410ebfedea0SLionel Sambuc }
411ebfedea0SLionel Sambuc #endif
412ebfedea0SLionel Sambuc
413*0a6a1f1dSLionel Sambuc if (s->state == SSL23_ST_SR_CLNT_HELLO_B) {
414*0a6a1f1dSLionel Sambuc /*
415*0a6a1f1dSLionel Sambuc * we have SSLv3/TLSv1 in an SSLv2 header (other cases skip this
416*0a6a1f1dSLionel Sambuc * state)
417*0a6a1f1dSLionel Sambuc */
418ebfedea0SLionel Sambuc
419ebfedea0SLionel Sambuc type = 2;
420ebfedea0SLionel Sambuc p = s->packet;
421ebfedea0SLionel Sambuc v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
422ebfedea0SLionel Sambuc v[1] = p[4];
423ebfedea0SLionel Sambuc
424*0a6a1f1dSLionel Sambuc /*-
425*0a6a1f1dSLionel Sambuc * An SSLv3/TLSv1 backwards-compatible CLIENT-HELLO in an SSLv2
426*0a6a1f1dSLionel Sambuc * header is sent directly on the wire, not wrapped as a TLS
427*0a6a1f1dSLionel Sambuc * record. It's format is:
428*0a6a1f1dSLionel Sambuc * Byte Content
429*0a6a1f1dSLionel Sambuc * 0-1 msg_length
430*0a6a1f1dSLionel Sambuc * 2 msg_type
431*0a6a1f1dSLionel Sambuc * 3-4 version
432*0a6a1f1dSLionel Sambuc * 5-6 cipher_spec_length
433*0a6a1f1dSLionel Sambuc * 7-8 session_id_length
434*0a6a1f1dSLionel Sambuc * 9-10 challenge_length
435*0a6a1f1dSLionel Sambuc * ... ...
436*0a6a1f1dSLionel Sambuc */
437ebfedea0SLionel Sambuc n = ((p[0] & 0x7f) << 8) | p[1];
438*0a6a1f1dSLionel Sambuc if (n > (1024 * 4)) {
439ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_RECORD_TOO_LARGE);
440ebfedea0SLionel Sambuc goto err;
441ebfedea0SLionel Sambuc }
442*0a6a1f1dSLionel Sambuc if (n < 9) {
443*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
444*0a6a1f1dSLionel Sambuc SSL_R_RECORD_LENGTH_MISMATCH);
445*0a6a1f1dSLionel Sambuc goto err;
446*0a6a1f1dSLionel Sambuc }
447ebfedea0SLionel Sambuc
448ebfedea0SLionel Sambuc j = ssl23_read_bytes(s, n + 2);
449*0a6a1f1dSLionel Sambuc /*
450*0a6a1f1dSLionel Sambuc * We previously read 11 bytes, so if j > 0, we must have j == n+2 ==
451*0a6a1f1dSLionel Sambuc * s->packet_length. We have at least 11 valid packet bytes.
452*0a6a1f1dSLionel Sambuc */
453*0a6a1f1dSLionel Sambuc if (j <= 0)
454*0a6a1f1dSLionel Sambuc return (j);
455ebfedea0SLionel Sambuc
456ebfedea0SLionel Sambuc ssl3_finish_mac(s, s->packet + 2, s->packet_length - 2);
457*0a6a1f1dSLionel Sambuc
458*0a6a1f1dSLionel Sambuc /* CLIENT-HELLO */
459ebfedea0SLionel Sambuc if (s->msg_callback)
460*0a6a1f1dSLionel Sambuc s->msg_callback(0, SSL2_VERSION, 0, s->packet + 2,
461*0a6a1f1dSLionel Sambuc s->packet_length - 2, s, s->msg_callback_arg);
462ebfedea0SLionel Sambuc
463ebfedea0SLionel Sambuc p = s->packet;
464ebfedea0SLionel Sambuc p += 5;
465ebfedea0SLionel Sambuc n2s(p, csl);
466ebfedea0SLionel Sambuc n2s(p, sil);
467ebfedea0SLionel Sambuc n2s(p, cl);
468ebfedea0SLionel Sambuc d = (unsigned char *)s->init_buf->data;
469*0a6a1f1dSLionel Sambuc if ((csl + sil + cl + 11) != s->packet_length) { /* We can't have TLS
470*0a6a1f1dSLionel Sambuc * extensions in SSL
471*0a6a1f1dSLionel Sambuc * 2.0 format *
472*0a6a1f1dSLionel Sambuc * Client Hello, can
473*0a6a1f1dSLionel Sambuc * we? Error
474*0a6a1f1dSLionel Sambuc * condition should
475*0a6a1f1dSLionel Sambuc * be * '>'
476*0a6a1f1dSLionel Sambuc * otherweise */
477*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,
478*0a6a1f1dSLionel Sambuc SSL_R_RECORD_LENGTH_MISMATCH);
479ebfedea0SLionel Sambuc goto err;
480ebfedea0SLionel Sambuc }
481ebfedea0SLionel Sambuc
482ebfedea0SLionel Sambuc /* record header: msg_type ... */
483ebfedea0SLionel Sambuc *(d++) = SSL3_MT_CLIENT_HELLO;
484ebfedea0SLionel Sambuc /* ... and length (actual value will be written later) */
485ebfedea0SLionel Sambuc d_len = d;
486ebfedea0SLionel Sambuc d += 3;
487ebfedea0SLionel Sambuc
488ebfedea0SLionel Sambuc /* client_version */
489ebfedea0SLionel Sambuc *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
490ebfedea0SLionel Sambuc *(d++) = v[1];
491ebfedea0SLionel Sambuc
492ebfedea0SLionel Sambuc /* lets populate the random area */
493ebfedea0SLionel Sambuc /* get the challenge_length */
494ebfedea0SLionel Sambuc i = (cl > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : cl;
495ebfedea0SLionel Sambuc memset(d, 0, SSL3_RANDOM_SIZE);
496ebfedea0SLionel Sambuc memcpy(&(d[SSL3_RANDOM_SIZE - i]), &(p[csl + sil]), i);
497ebfedea0SLionel Sambuc d += SSL3_RANDOM_SIZE;
498ebfedea0SLionel Sambuc
499ebfedea0SLionel Sambuc /* no session-id reuse */
500ebfedea0SLionel Sambuc *(d++) = 0;
501ebfedea0SLionel Sambuc
502ebfedea0SLionel Sambuc /* ciphers */
503ebfedea0SLionel Sambuc j = 0;
504ebfedea0SLionel Sambuc dd = d;
505ebfedea0SLionel Sambuc d += 2;
506*0a6a1f1dSLionel Sambuc for (i = 0; i < csl; i += 3) {
507*0a6a1f1dSLionel Sambuc if (p[i] != 0)
508*0a6a1f1dSLionel Sambuc continue;
509ebfedea0SLionel Sambuc *(d++) = p[i + 1];
510ebfedea0SLionel Sambuc *(d++) = p[i + 2];
511ebfedea0SLionel Sambuc j += 2;
512ebfedea0SLionel Sambuc }
513ebfedea0SLionel Sambuc s2n(j, dd);
514ebfedea0SLionel Sambuc
515ebfedea0SLionel Sambuc /* COMPRESSION */
516ebfedea0SLionel Sambuc *(d++) = 1;
517ebfedea0SLionel Sambuc *(d++) = 0;
518ebfedea0SLionel Sambuc
519ebfedea0SLionel Sambuc #if 0
520ebfedea0SLionel Sambuc /* copy any remaining data with may be extensions */
521ebfedea0SLionel Sambuc p = p + csl + sil + cl;
522*0a6a1f1dSLionel Sambuc while (p < s->packet + s->packet_length) {
523ebfedea0SLionel Sambuc *(d++) = *(p++);
524ebfedea0SLionel Sambuc }
525ebfedea0SLionel Sambuc #endif
526ebfedea0SLionel Sambuc
527ebfedea0SLionel Sambuc i = (d - (unsigned char *)s->init_buf->data) - 4;
528ebfedea0SLionel Sambuc l2n3((long)i, d_len);
529ebfedea0SLionel Sambuc
530ebfedea0SLionel Sambuc /* get the data reused from the init_buf */
531ebfedea0SLionel Sambuc s->s3->tmp.reuse_message = 1;
532ebfedea0SLionel Sambuc s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
533ebfedea0SLionel Sambuc s->s3->tmp.message_size = i;
534ebfedea0SLionel Sambuc }
535ebfedea0SLionel Sambuc
536ebfedea0SLionel Sambuc /* imaginary new state (for program structure): */
537ebfedea0SLionel Sambuc /* s->state = SSL23_SR_CLNT_HELLO_C */
538ebfedea0SLionel Sambuc
539*0a6a1f1dSLionel Sambuc if (type == 1) {
540ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_SSL2
541ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
542ebfedea0SLionel Sambuc goto err;
543ebfedea0SLionel Sambuc #else
544ebfedea0SLionel Sambuc /* we are talking sslv2 */
545*0a6a1f1dSLionel Sambuc /*
546*0a6a1f1dSLionel Sambuc * we need to clean up the SSLv3/TLSv1 setup and put in the sslv2
547*0a6a1f1dSLionel Sambuc * stuff.
548*0a6a1f1dSLionel Sambuc */
549ebfedea0SLionel Sambuc
550*0a6a1f1dSLionel Sambuc if (s->s2 == NULL) {
551ebfedea0SLionel Sambuc if (!ssl2_new(s))
552ebfedea0SLionel Sambuc goto err;
553*0a6a1f1dSLionel Sambuc } else
554ebfedea0SLionel Sambuc ssl2_clear(s);
555ebfedea0SLionel Sambuc
556*0a6a1f1dSLionel Sambuc if (s->s3 != NULL)
557*0a6a1f1dSLionel Sambuc ssl3_free(s);
558ebfedea0SLionel Sambuc
559ebfedea0SLionel Sambuc if (!BUF_MEM_grow_clean(s->init_buf,
560*0a6a1f1dSLionel Sambuc SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER)) {
561ebfedea0SLionel Sambuc goto err;
562ebfedea0SLionel Sambuc }
563ebfedea0SLionel Sambuc
564ebfedea0SLionel Sambuc s->state = SSL2_ST_GET_CLIENT_HELLO_A;
565ebfedea0SLionel Sambuc if (s->options & SSL_OP_NO_TLSv1 && s->options & SSL_OP_NO_SSLv3)
566ebfedea0SLionel Sambuc s->s2->ssl2_rollback = 0;
567ebfedea0SLionel Sambuc else
568*0a6a1f1dSLionel Sambuc /*
569*0a6a1f1dSLionel Sambuc * reject SSL 2.0 session if client supports SSL 3.0 or TLS 1.0
570*0a6a1f1dSLionel Sambuc * (SSL 3.0 draft/RFC 2246, App. E.2)
571*0a6a1f1dSLionel Sambuc */
572ebfedea0SLionel Sambuc s->s2->ssl2_rollback = 1;
573ebfedea0SLionel Sambuc
574*0a6a1f1dSLionel Sambuc /*
575*0a6a1f1dSLionel Sambuc * setup the n bytes we have read so we get them from the sslv2
576*0a6a1f1dSLionel Sambuc * buffer
577*0a6a1f1dSLionel Sambuc */
578ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER;
579ebfedea0SLionel Sambuc s->packet_length = n;
580ebfedea0SLionel Sambuc s->packet = &(s->s2->rbuf[0]);
581ebfedea0SLionel Sambuc memcpy(s->packet, buf, n);
582ebfedea0SLionel Sambuc s->s2->rbuf_left = n;
583ebfedea0SLionel Sambuc s->s2->rbuf_offs = 0;
584ebfedea0SLionel Sambuc
585ebfedea0SLionel Sambuc s->method = SSLv2_server_method();
586ebfedea0SLionel Sambuc s->handshake_func = s->method->ssl_accept;
587ebfedea0SLionel Sambuc #endif
588ebfedea0SLionel Sambuc }
589ebfedea0SLionel Sambuc
590*0a6a1f1dSLionel Sambuc if ((type == 2) || (type == 3)) {
591*0a6a1f1dSLionel Sambuc /*
592*0a6a1f1dSLionel Sambuc * we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style)
593*0a6a1f1dSLionel Sambuc */
594*0a6a1f1dSLionel Sambuc const SSL_METHOD *new_method;
595*0a6a1f1dSLionel Sambuc new_method = ssl23_get_server_method(s->version);
596*0a6a1f1dSLionel Sambuc if (new_method == NULL) {
597*0a6a1f1dSLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNSUPPORTED_PROTOCOL);
598*0a6a1f1dSLionel Sambuc goto err;
599*0a6a1f1dSLionel Sambuc }
600*0a6a1f1dSLionel Sambuc s->method = new_method;
601ebfedea0SLionel Sambuc
602*0a6a1f1dSLionel Sambuc if (!ssl_init_wbio_buffer(s, 1))
603*0a6a1f1dSLionel Sambuc goto err;
604ebfedea0SLionel Sambuc
605ebfedea0SLionel Sambuc /* we are in this state */
606ebfedea0SLionel Sambuc s->state = SSL3_ST_SR_CLNT_HELLO_A;
607ebfedea0SLionel Sambuc
608*0a6a1f1dSLionel Sambuc if (type == 3) {
609*0a6a1f1dSLionel Sambuc /*
610*0a6a1f1dSLionel Sambuc * put the 'n' bytes we have read into the input buffer for SSLv3
611*0a6a1f1dSLionel Sambuc */
612ebfedea0SLionel Sambuc s->rstate = SSL_ST_READ_HEADER;
613ebfedea0SLionel Sambuc s->packet_length = n;
614ebfedea0SLionel Sambuc if (s->s3->rbuf.buf == NULL)
615ebfedea0SLionel Sambuc if (!ssl3_setup_read_buffer(s))
616ebfedea0SLionel Sambuc goto err;
617ebfedea0SLionel Sambuc
618ebfedea0SLionel Sambuc s->packet = &(s->s3->rbuf.buf[0]);
619ebfedea0SLionel Sambuc memcpy(s->packet, buf, n);
620ebfedea0SLionel Sambuc s->s3->rbuf.left = n;
621ebfedea0SLionel Sambuc s->s3->rbuf.offset = 0;
622*0a6a1f1dSLionel Sambuc } else {
623ebfedea0SLionel Sambuc s->packet_length = 0;
624ebfedea0SLionel Sambuc s->s3->rbuf.left = 0;
625ebfedea0SLionel Sambuc s->s3->rbuf.offset = 0;
626ebfedea0SLionel Sambuc }
627ebfedea0SLionel Sambuc #if 0 /* ssl3_get_client_hello does this */
628ebfedea0SLionel Sambuc s->client_version = (v[0] << 8) | v[1];
629ebfedea0SLionel Sambuc #endif
630ebfedea0SLionel Sambuc s->handshake_func = s->method->ssl_accept;
631ebfedea0SLionel Sambuc }
632ebfedea0SLionel Sambuc
633*0a6a1f1dSLionel Sambuc if ((type < 1) || (type > 3)) {
634ebfedea0SLionel Sambuc /* bad, very bad */
635ebfedea0SLionel Sambuc SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO, SSL_R_UNKNOWN_PROTOCOL);
636ebfedea0SLionel Sambuc goto err;
637ebfedea0SLionel Sambuc }
638ebfedea0SLionel Sambuc s->init_num = 0;
639ebfedea0SLionel Sambuc
640*0a6a1f1dSLionel Sambuc if (buf != buf_space)
641*0a6a1f1dSLionel Sambuc OPENSSL_free(buf);
642ebfedea0SLionel Sambuc return (SSL_accept(s));
643ebfedea0SLionel Sambuc err:
644*0a6a1f1dSLionel Sambuc if (buf != buf_space)
645*0a6a1f1dSLionel Sambuc OPENSSL_free(buf);
646ebfedea0SLionel Sambuc return (-1);
647ebfedea0SLionel Sambuc }
648