sl@0
|
1 |
/* ssl/s23_clnt.c */
|
sl@0
|
2 |
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
*
|
sl@0
|
5 |
* This package is an SSL implementation written
|
sl@0
|
6 |
* by Eric Young (eay@cryptsoft.com).
|
sl@0
|
7 |
* The implementation was written so as to conform with Netscapes SSL.
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* This library is free for commercial and non-commercial use as long as
|
sl@0
|
10 |
* the following conditions are aheared to. The following conditions
|
sl@0
|
11 |
* apply to all code found in this distribution, be it the RC4, RSA,
|
sl@0
|
12 |
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
sl@0
|
13 |
* included with this distribution is covered by the same copyright terms
|
sl@0
|
14 |
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
sl@0
|
15 |
*
|
sl@0
|
16 |
* Copyright remains Eric Young's, and as such any Copyright notices in
|
sl@0
|
17 |
* the code are not to be removed.
|
sl@0
|
18 |
* If this package is used in a product, Eric Young should be given attribution
|
sl@0
|
19 |
* as the author of the parts of the library used.
|
sl@0
|
20 |
* This can be in the form of a textual message at program startup or
|
sl@0
|
21 |
* in documentation (online or textual) provided with the package.
|
sl@0
|
22 |
*
|
sl@0
|
23 |
* Redistribution and use in source and binary forms, with or without
|
sl@0
|
24 |
* modification, are permitted provided that the following conditions
|
sl@0
|
25 |
* are met:
|
sl@0
|
26 |
* 1. Redistributions of source code must retain the copyright
|
sl@0
|
27 |
* notice, this list of conditions and the following disclaimer.
|
sl@0
|
28 |
* 2. Redistributions in binary form must reproduce the above copyright
|
sl@0
|
29 |
* notice, this list of conditions and the following disclaimer in the
|
sl@0
|
30 |
* documentation and/or other materials provided with the distribution.
|
sl@0
|
31 |
* 3. All advertising materials mentioning features or use of this software
|
sl@0
|
32 |
* must display the following acknowledgement:
|
sl@0
|
33 |
* "This product includes cryptographic software written by
|
sl@0
|
34 |
* Eric Young (eay@cryptsoft.com)"
|
sl@0
|
35 |
* The word 'cryptographic' can be left out if the rouines from the library
|
sl@0
|
36 |
* being used are not cryptographic related :-).
|
sl@0
|
37 |
* 4. If you include any Windows specific code (or a derivative thereof) from
|
sl@0
|
38 |
* the apps directory (application code) you must include an acknowledgement:
|
sl@0
|
39 |
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
sl@0
|
40 |
*
|
sl@0
|
41 |
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
sl@0
|
42 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
sl@0
|
43 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
sl@0
|
44 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
sl@0
|
45 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
sl@0
|
46 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
sl@0
|
47 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
sl@0
|
48 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
sl@0
|
49 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
sl@0
|
50 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
sl@0
|
51 |
* SUCH DAMAGE.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* The licence and distribution terms for any publically available version or
|
sl@0
|
54 |
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
sl@0
|
55 |
* copied and put under another distribution licence
|
sl@0
|
56 |
* [including the GNU Public Licence.]
|
sl@0
|
57 |
*/
|
sl@0
|
58 |
/*
|
sl@0
|
59 |
© Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
|
sl@0
|
62 |
#include <stdio.h>
|
sl@0
|
63 |
#include "ssl_locl.h"
|
sl@0
|
64 |
#include <openssl/buffer.h>
|
sl@0
|
65 |
#include <openssl/rand.h>
|
sl@0
|
66 |
#include <openssl/objects.h>
|
sl@0
|
67 |
#include <openssl/evp.h>
|
sl@0
|
68 |
|
sl@0
|
69 |
#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
|
sl@0
|
70 |
#include "libssl_wsd.h"
|
sl@0
|
71 |
#endif
|
sl@0
|
72 |
|
sl@0
|
73 |
#ifdef EMULATOR
|
sl@0
|
74 |
|
sl@0
|
75 |
GET_STATIC_VAR_FROM_TLS(SSLv23_client_method_data,s23_clnt,SSL_METHOD)
|
sl@0
|
76 |
|
sl@0
|
77 |
#define SSLv23_client_method_data (*GET_WSD_VAR_NAME(SSLv23_client_method_data,s23_clnt,s)())
|
sl@0
|
78 |
|
sl@0
|
79 |
#endif
|
sl@0
|
80 |
static SSL_METHOD *ssl23_get_client_method(int ver);
|
sl@0
|
81 |
static int ssl23_client_hello(SSL *s);
|
sl@0
|
82 |
static int ssl23_get_server_hello(SSL *s);
|
sl@0
|
83 |
static SSL_METHOD *ssl23_get_client_method(int ver)
|
sl@0
|
84 |
{
|
sl@0
|
85 |
#ifndef OPENSSL_NO_SSL2
|
sl@0
|
86 |
if (ver == SSL2_VERSION)
|
sl@0
|
87 |
return(SSLv2_client_method());
|
sl@0
|
88 |
#endif
|
sl@0
|
89 |
if (ver == SSL3_VERSION)
|
sl@0
|
90 |
return(SSLv3_client_method());
|
sl@0
|
91 |
else if (ver == TLS1_VERSION)
|
sl@0
|
92 |
return(TLSv1_client_method());
|
sl@0
|
93 |
else
|
sl@0
|
94 |
return(NULL);
|
sl@0
|
95 |
}
|
sl@0
|
96 |
|
sl@0
|
97 |
EXPORT_C IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
|
sl@0
|
98 |
ssl_undefined_function,
|
sl@0
|
99 |
ssl23_connect,
|
sl@0
|
100 |
ssl23_get_client_method)
|
sl@0
|
101 |
|
sl@0
|
102 |
int ssl23_connect(SSL *s)
|
sl@0
|
103 |
{
|
sl@0
|
104 |
BUF_MEM *buf=NULL;
|
sl@0
|
105 |
unsigned long Time=(unsigned long)time(NULL);
|
sl@0
|
106 |
void (*cb)(const SSL *ssl,int type,int val)=NULL;
|
sl@0
|
107 |
int ret= -1;
|
sl@0
|
108 |
int new_state,state;
|
sl@0
|
109 |
|
sl@0
|
110 |
RAND_add(&Time,sizeof(Time),0);
|
sl@0
|
111 |
ERR_clear_error();
|
sl@0
|
112 |
clear_sys_error();
|
sl@0
|
113 |
|
sl@0
|
114 |
if (s->info_callback != NULL)
|
sl@0
|
115 |
cb=s->info_callback;
|
sl@0
|
116 |
else if (s->ctx->info_callback != NULL)
|
sl@0
|
117 |
cb=s->ctx->info_callback;
|
sl@0
|
118 |
|
sl@0
|
119 |
s->in_handshake++;
|
sl@0
|
120 |
if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s);
|
sl@0
|
121 |
|
sl@0
|
122 |
for (;;)
|
sl@0
|
123 |
{
|
sl@0
|
124 |
state=s->state;
|
sl@0
|
125 |
|
sl@0
|
126 |
switch(s->state)
|
sl@0
|
127 |
{
|
sl@0
|
128 |
case SSL_ST_BEFORE:
|
sl@0
|
129 |
case SSL_ST_CONNECT:
|
sl@0
|
130 |
case SSL_ST_BEFORE|SSL_ST_CONNECT:
|
sl@0
|
131 |
case SSL_ST_OK|SSL_ST_CONNECT:
|
sl@0
|
132 |
|
sl@0
|
133 |
if (s->session != NULL)
|
sl@0
|
134 |
{
|
sl@0
|
135 |
SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
|
sl@0
|
136 |
ret= -1;
|
sl@0
|
137 |
goto end;
|
sl@0
|
138 |
}
|
sl@0
|
139 |
s->server=0;
|
sl@0
|
140 |
if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
|
sl@0
|
141 |
|
sl@0
|
142 |
/* s->version=TLS1_VERSION; */
|
sl@0
|
143 |
s->type=SSL_ST_CONNECT;
|
sl@0
|
144 |
|
sl@0
|
145 |
if (s->init_buf == NULL)
|
sl@0
|
146 |
{
|
sl@0
|
147 |
if ((buf=BUF_MEM_new()) == NULL)
|
sl@0
|
148 |
{
|
sl@0
|
149 |
ret= -1;
|
sl@0
|
150 |
goto end;
|
sl@0
|
151 |
}
|
sl@0
|
152 |
if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
|
sl@0
|
153 |
{
|
sl@0
|
154 |
ret= -1;
|
sl@0
|
155 |
goto end;
|
sl@0
|
156 |
}
|
sl@0
|
157 |
s->init_buf=buf;
|
sl@0
|
158 |
buf=NULL;
|
sl@0
|
159 |
}
|
sl@0
|
160 |
|
sl@0
|
161 |
if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
|
sl@0
|
162 |
|
sl@0
|
163 |
ssl3_init_finished_mac(s);
|
sl@0
|
164 |
|
sl@0
|
165 |
s->state=SSL23_ST_CW_CLNT_HELLO_A;
|
sl@0
|
166 |
s->ctx->stats.sess_connect++;
|
sl@0
|
167 |
s->init_num=0;
|
sl@0
|
168 |
break;
|
sl@0
|
169 |
|
sl@0
|
170 |
case SSL23_ST_CW_CLNT_HELLO_A:
|
sl@0
|
171 |
case SSL23_ST_CW_CLNT_HELLO_B:
|
sl@0
|
172 |
|
sl@0
|
173 |
s->shutdown=0;
|
sl@0
|
174 |
ret=ssl23_client_hello(s);
|
sl@0
|
175 |
if (ret <= 0) goto end;
|
sl@0
|
176 |
s->state=SSL23_ST_CR_SRVR_HELLO_A;
|
sl@0
|
177 |
s->init_num=0;
|
sl@0
|
178 |
|
sl@0
|
179 |
break;
|
sl@0
|
180 |
|
sl@0
|
181 |
case SSL23_ST_CR_SRVR_HELLO_A:
|
sl@0
|
182 |
case SSL23_ST_CR_SRVR_HELLO_B:
|
sl@0
|
183 |
ret=ssl23_get_server_hello(s);
|
sl@0
|
184 |
if (ret >= 0) cb=NULL;
|
sl@0
|
185 |
goto end;
|
sl@0
|
186 |
/* break; */
|
sl@0
|
187 |
|
sl@0
|
188 |
default:
|
sl@0
|
189 |
SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
|
sl@0
|
190 |
ret= -1;
|
sl@0
|
191 |
goto end;
|
sl@0
|
192 |
/* break; */
|
sl@0
|
193 |
}
|
sl@0
|
194 |
|
sl@0
|
195 |
if (s->debug) { (void)BIO_flush(s->wbio); }
|
sl@0
|
196 |
|
sl@0
|
197 |
if ((cb != NULL) && (s->state != state))
|
sl@0
|
198 |
{
|
sl@0
|
199 |
new_state=s->state;
|
sl@0
|
200 |
s->state=state;
|
sl@0
|
201 |
cb(s,SSL_CB_CONNECT_LOOP,1);
|
sl@0
|
202 |
s->state=new_state;
|
sl@0
|
203 |
}
|
sl@0
|
204 |
}
|
sl@0
|
205 |
end:
|
sl@0
|
206 |
s->in_handshake--;
|
sl@0
|
207 |
if (buf != NULL)
|
sl@0
|
208 |
BUF_MEM_free(buf);
|
sl@0
|
209 |
if (cb != NULL)
|
sl@0
|
210 |
cb(s,SSL_CB_CONNECT_EXIT,ret);
|
sl@0
|
211 |
return(ret);
|
sl@0
|
212 |
}
|
sl@0
|
213 |
|
sl@0
|
214 |
|
sl@0
|
215 |
static int ssl23_client_hello(SSL *s)
|
sl@0
|
216 |
{
|
sl@0
|
217 |
unsigned char *buf;
|
sl@0
|
218 |
unsigned char *p,*d;
|
sl@0
|
219 |
int i,j,ch_len;
|
sl@0
|
220 |
unsigned long Time,l;
|
sl@0
|
221 |
int ssl2_compat;
|
sl@0
|
222 |
int version = 0, version_major, version_minor;
|
sl@0
|
223 |
SSL_COMP *comp;
|
sl@0
|
224 |
int ret;
|
sl@0
|
225 |
|
sl@0
|
226 |
ssl2_compat = (s->options & SSL_OP_NO_SSLv2) ? 0 : 1;
|
sl@0
|
227 |
|
sl@0
|
228 |
if (!(s->options & SSL_OP_NO_TLSv1))
|
sl@0
|
229 |
{
|
sl@0
|
230 |
version = TLS1_VERSION;
|
sl@0
|
231 |
}
|
sl@0
|
232 |
else if (!(s->options & SSL_OP_NO_SSLv3))
|
sl@0
|
233 |
{
|
sl@0
|
234 |
version = SSL3_VERSION;
|
sl@0
|
235 |
}
|
sl@0
|
236 |
else if (!(s->options & SSL_OP_NO_SSLv2))
|
sl@0
|
237 |
{
|
sl@0
|
238 |
version = SSL2_VERSION;
|
sl@0
|
239 |
}
|
sl@0
|
240 |
|
sl@0
|
241 |
buf=(unsigned char *)s->init_buf->data;
|
sl@0
|
242 |
if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
|
sl@0
|
243 |
{
|
sl@0
|
244 |
#if 0
|
sl@0
|
245 |
/* don't reuse session-id's */
|
sl@0
|
246 |
if (!ssl_get_new_session(s,0))
|
sl@0
|
247 |
{
|
sl@0
|
248 |
return(-1);
|
sl@0
|
249 |
}
|
sl@0
|
250 |
#endif
|
sl@0
|
251 |
|
sl@0
|
252 |
p=s->s3->client_random;
|
sl@0
|
253 |
Time=(unsigned long)time(NULL); /* Time */
|
sl@0
|
254 |
l2n(Time,p);
|
sl@0
|
255 |
if (RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE-4) <= 0)
|
sl@0
|
256 |
return -1;
|
sl@0
|
257 |
|
sl@0
|
258 |
if (version == TLS1_VERSION)
|
sl@0
|
259 |
{
|
sl@0
|
260 |
version_major = TLS1_VERSION_MAJOR;
|
sl@0
|
261 |
version_minor = TLS1_VERSION_MINOR;
|
sl@0
|
262 |
}
|
sl@0
|
263 |
else if (version == SSL3_VERSION)
|
sl@0
|
264 |
{
|
sl@0
|
265 |
version_major = SSL3_VERSION_MAJOR;
|
sl@0
|
266 |
version_minor = SSL3_VERSION_MINOR;
|
sl@0
|
267 |
}
|
sl@0
|
268 |
else if (version == SSL2_VERSION)
|
sl@0
|
269 |
{
|
sl@0
|
270 |
version_major = SSL2_VERSION_MAJOR;
|
sl@0
|
271 |
version_minor = SSL2_VERSION_MINOR;
|
sl@0
|
272 |
}
|
sl@0
|
273 |
else
|
sl@0
|
274 |
{
|
sl@0
|
275 |
SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
|
sl@0
|
276 |
return(-1);
|
sl@0
|
277 |
}
|
sl@0
|
278 |
|
sl@0
|
279 |
s->client_version = version;
|
sl@0
|
280 |
|
sl@0
|
281 |
if (ssl2_compat)
|
sl@0
|
282 |
{
|
sl@0
|
283 |
/* create SSL 2.0 compatible Client Hello */
|
sl@0
|
284 |
|
sl@0
|
285 |
/* two byte record header will be written last */
|
sl@0
|
286 |
d = &(buf[2]);
|
sl@0
|
287 |
p = d + 9; /* leave space for message type, version, individual length fields */
|
sl@0
|
288 |
|
sl@0
|
289 |
*(d++) = SSL2_MT_CLIENT_HELLO;
|
sl@0
|
290 |
*(d++) = version_major;
|
sl@0
|
291 |
*(d++) = version_minor;
|
sl@0
|
292 |
|
sl@0
|
293 |
/* Ciphers supported */
|
sl@0
|
294 |
i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p,0);
|
sl@0
|
295 |
if (i == 0)
|
sl@0
|
296 |
{
|
sl@0
|
297 |
/* no ciphers */
|
sl@0
|
298 |
SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
|
sl@0
|
299 |
return -1;
|
sl@0
|
300 |
}
|
sl@0
|
301 |
s2n(i,d);
|
sl@0
|
302 |
p+=i;
|
sl@0
|
303 |
|
sl@0
|
304 |
/* put in the session-id length (zero since there is no reuse) */
|
sl@0
|
305 |
#if 0
|
sl@0
|
306 |
s->session->session_id_length=0;
|
sl@0
|
307 |
#endif
|
sl@0
|
308 |
s2n(0,d);
|
sl@0
|
309 |
|
sl@0
|
310 |
if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
|
sl@0
|
311 |
ch_len=SSL2_CHALLENGE_LENGTH;
|
sl@0
|
312 |
else
|
sl@0
|
313 |
ch_len=SSL2_MAX_CHALLENGE_LENGTH;
|
sl@0
|
314 |
|
sl@0
|
315 |
/* write out sslv2 challenge */
|
sl@0
|
316 |
if (SSL3_RANDOM_SIZE < ch_len)
|
sl@0
|
317 |
i=SSL3_RANDOM_SIZE;
|
sl@0
|
318 |
else
|
sl@0
|
319 |
i=ch_len;
|
sl@0
|
320 |
s2n(i,d);
|
sl@0
|
321 |
memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
|
sl@0
|
322 |
if (RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i) <= 0)
|
sl@0
|
323 |
return -1;
|
sl@0
|
324 |
|
sl@0
|
325 |
memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
|
sl@0
|
326 |
p+=i;
|
sl@0
|
327 |
|
sl@0
|
328 |
i= p- &(buf[2]);
|
sl@0
|
329 |
buf[0]=((i>>8)&0xff)|0x80;
|
sl@0
|
330 |
buf[1]=(i&0xff);
|
sl@0
|
331 |
|
sl@0
|
332 |
/* number of bytes to write */
|
sl@0
|
333 |
s->init_num=i+2;
|
sl@0
|
334 |
s->init_off=0;
|
sl@0
|
335 |
|
sl@0
|
336 |
ssl3_finish_mac(s,&(buf[2]),i);
|
sl@0
|
337 |
}
|
sl@0
|
338 |
else
|
sl@0
|
339 |
{
|
sl@0
|
340 |
/* create Client Hello in SSL 3.0/TLS 1.0 format */
|
sl@0
|
341 |
|
sl@0
|
342 |
/* do the record header (5 bytes) and handshake message header (4 bytes) last */
|
sl@0
|
343 |
d = p = &(buf[9]);
|
sl@0
|
344 |
|
sl@0
|
345 |
*(p++) = version_major;
|
sl@0
|
346 |
*(p++) = version_minor;
|
sl@0
|
347 |
|
sl@0
|
348 |
/* Random stuff */
|
sl@0
|
349 |
memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
|
sl@0
|
350 |
p += SSL3_RANDOM_SIZE;
|
sl@0
|
351 |
|
sl@0
|
352 |
/* Session ID (zero since there is no reuse) */
|
sl@0
|
353 |
*(p++) = 0;
|
sl@0
|
354 |
|
sl@0
|
355 |
/* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
|
sl@0
|
356 |
i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),&(p[2]),ssl3_put_cipher_by_char);
|
sl@0
|
357 |
if (i == 0)
|
sl@0
|
358 |
{
|
sl@0
|
359 |
SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
|
sl@0
|
360 |
return -1;
|
sl@0
|
361 |
}
|
sl@0
|
362 |
s2n(i,p);
|
sl@0
|
363 |
p+=i;
|
sl@0
|
364 |
|
sl@0
|
365 |
/* COMPRESSION */
|
sl@0
|
366 |
if (s->ctx->comp_methods == NULL)
|
sl@0
|
367 |
j=0;
|
sl@0
|
368 |
else
|
sl@0
|
369 |
j=sk_SSL_COMP_num(s->ctx->comp_methods);
|
sl@0
|
370 |
*(p++)=1+j;
|
sl@0
|
371 |
for (i=0; i<j; i++)
|
sl@0
|
372 |
{
|
sl@0
|
373 |
comp=sk_SSL_COMP_value(s->ctx->comp_methods,i);
|
sl@0
|
374 |
*(p++)=comp->id;
|
sl@0
|
375 |
}
|
sl@0
|
376 |
*(p++)=0; /* Add the NULL method */
|
sl@0
|
377 |
|
sl@0
|
378 |
l = p-d;
|
sl@0
|
379 |
*p = 42;
|
sl@0
|
380 |
|
sl@0
|
381 |
/* fill in 4-byte handshake header */
|
sl@0
|
382 |
d=&(buf[5]);
|
sl@0
|
383 |
*(d++)=SSL3_MT_CLIENT_HELLO;
|
sl@0
|
384 |
l2n3(l,d);
|
sl@0
|
385 |
|
sl@0
|
386 |
l += 4;
|
sl@0
|
387 |
|
sl@0
|
388 |
if (l > SSL3_RT_MAX_PLAIN_LENGTH)
|
sl@0
|
389 |
{
|
sl@0
|
390 |
SSLerr(SSL_F_SSL23_CLIENT_HELLO,ERR_R_INTERNAL_ERROR);
|
sl@0
|
391 |
return -1;
|
sl@0
|
392 |
}
|
sl@0
|
393 |
|
sl@0
|
394 |
/* fill in 5-byte record header */
|
sl@0
|
395 |
d=buf;
|
sl@0
|
396 |
*(d++) = SSL3_RT_HANDSHAKE;
|
sl@0
|
397 |
*(d++) = version_major;
|
sl@0
|
398 |
*(d++) = version_minor; /* arguably we should send the *lowest* suported version here
|
sl@0
|
399 |
* (indicating, e.g., TLS 1.0 in "SSL 3.0 format") */
|
sl@0
|
400 |
s2n((int)l,d);
|
sl@0
|
401 |
|
sl@0
|
402 |
/* number of bytes to write */
|
sl@0
|
403 |
s->init_num=p-buf;
|
sl@0
|
404 |
s->init_off=0;
|
sl@0
|
405 |
|
sl@0
|
406 |
ssl3_finish_mac(s,&(buf[5]), s->init_num - 5);
|
sl@0
|
407 |
}
|
sl@0
|
408 |
|
sl@0
|
409 |
s->state=SSL23_ST_CW_CLNT_HELLO_B;
|
sl@0
|
410 |
s->init_off=0;
|
sl@0
|
411 |
}
|
sl@0
|
412 |
|
sl@0
|
413 |
/* SSL3_ST_CW_CLNT_HELLO_B */
|
sl@0
|
414 |
ret = ssl23_write_bytes(s);
|
sl@0
|
415 |
|
sl@0
|
416 |
if ((ret >= 2) && s->msg_callback)
|
sl@0
|
417 |
{
|
sl@0
|
418 |
/* Client Hello has been sent; tell msg_callback */
|
sl@0
|
419 |
|
sl@0
|
420 |
if (ssl2_compat)
|
sl@0
|
421 |
s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg);
|
sl@0
|
422 |
else
|
sl@0
|
423 |
s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg);
|
sl@0
|
424 |
}
|
sl@0
|
425 |
|
sl@0
|
426 |
return ret;
|
sl@0
|
427 |
}
|
sl@0
|
428 |
|
sl@0
|
429 |
static int ssl23_get_server_hello(SSL *s)
|
sl@0
|
430 |
{
|
sl@0
|
431 |
char buf[8];
|
sl@0
|
432 |
unsigned char *p;
|
sl@0
|
433 |
int i;
|
sl@0
|
434 |
int n;
|
sl@0
|
435 |
|
sl@0
|
436 |
n=ssl23_read_bytes(s,7);
|
sl@0
|
437 |
|
sl@0
|
438 |
if (n != 7) return(n);
|
sl@0
|
439 |
p=s->packet;
|
sl@0
|
440 |
|
sl@0
|
441 |
memcpy(buf,p,n);
|
sl@0
|
442 |
|
sl@0
|
443 |
if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
|
sl@0
|
444 |
(p[5] == 0x00) && (p[6] == 0x02))
|
sl@0
|
445 |
{
|
sl@0
|
446 |
#ifdef OPENSSL_NO_SSL2
|
sl@0
|
447 |
SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
|
sl@0
|
448 |
goto err;
|
sl@0
|
449 |
#else
|
sl@0
|
450 |
/* we are talking sslv2 */
|
sl@0
|
451 |
/* we need to clean up the SSLv3 setup and put in the
|
sl@0
|
452 |
* sslv2 stuff. */
|
sl@0
|
453 |
int ch_len;
|
sl@0
|
454 |
|
sl@0
|
455 |
if (s->options & SSL_OP_NO_SSLv2)
|
sl@0
|
456 |
{
|
sl@0
|
457 |
SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
|
sl@0
|
458 |
goto err;
|
sl@0
|
459 |
}
|
sl@0
|
460 |
if (s->s2 == NULL)
|
sl@0
|
461 |
{
|
sl@0
|
462 |
if (!ssl2_new(s))
|
sl@0
|
463 |
goto err;
|
sl@0
|
464 |
}
|
sl@0
|
465 |
else
|
sl@0
|
466 |
ssl2_clear(s);
|
sl@0
|
467 |
|
sl@0
|
468 |
if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
|
sl@0
|
469 |
ch_len=SSL2_CHALLENGE_LENGTH;
|
sl@0
|
470 |
else
|
sl@0
|
471 |
ch_len=SSL2_MAX_CHALLENGE_LENGTH;
|
sl@0
|
472 |
|
sl@0
|
473 |
/* write out sslv2 challenge */
|
sl@0
|
474 |
i=(SSL3_RANDOM_SIZE < ch_len)
|
sl@0
|
475 |
?SSL3_RANDOM_SIZE:ch_len;
|
sl@0
|
476 |
s->s2->challenge_length=i;
|
sl@0
|
477 |
memcpy(s->s2->challenge,
|
sl@0
|
478 |
&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
|
sl@0
|
479 |
|
sl@0
|
480 |
if (s->s3 != NULL) ssl3_free(s);
|
sl@0
|
481 |
|
sl@0
|
482 |
if (!BUF_MEM_grow_clean(s->init_buf,
|
sl@0
|
483 |
SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
|
sl@0
|
484 |
{
|
sl@0
|
485 |
SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
|
sl@0
|
486 |
goto err;
|
sl@0
|
487 |
}
|
sl@0
|
488 |
|
sl@0
|
489 |
s->state=SSL2_ST_GET_SERVER_HELLO_A;
|
sl@0
|
490 |
if (!(s->client_version == SSL2_VERSION))
|
sl@0
|
491 |
/* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
|
sl@0
|
492 |
s->s2->ssl2_rollback=1;
|
sl@0
|
493 |
|
sl@0
|
494 |
/* setup the 5 bytes we have read so we get them from
|
sl@0
|
495 |
* the sslv2 buffer */
|
sl@0
|
496 |
s->rstate=SSL_ST_READ_HEADER;
|
sl@0
|
497 |
s->packet_length=n;
|
sl@0
|
498 |
s->packet= &(s->s2->rbuf[0]);
|
sl@0
|
499 |
memcpy(s->packet,buf,n);
|
sl@0
|
500 |
s->s2->rbuf_left=n;
|
sl@0
|
501 |
s->s2->rbuf_offs=0;
|
sl@0
|
502 |
|
sl@0
|
503 |
/* we have already written one */
|
sl@0
|
504 |
s->s2->write_sequence=1;
|
sl@0
|
505 |
|
sl@0
|
506 |
s->method=SSLv2_client_method();
|
sl@0
|
507 |
s->handshake_func=s->method->ssl_connect;
|
sl@0
|
508 |
#endif
|
sl@0
|
509 |
}
|
sl@0
|
510 |
else if ((p[0] == SSL3_RT_HANDSHAKE) &&
|
sl@0
|
511 |
(p[1] == SSL3_VERSION_MAJOR) &&
|
sl@0
|
512 |
((p[2] == SSL3_VERSION_MINOR) ||
|
sl@0
|
513 |
(p[2] == TLS1_VERSION_MINOR)) &&
|
sl@0
|
514 |
(p[5] == SSL3_MT_SERVER_HELLO))
|
sl@0
|
515 |
{
|
sl@0
|
516 |
/* we have sslv3 or tls1 */
|
sl@0
|
517 |
|
sl@0
|
518 |
if (!ssl_init_wbio_buffer(s,1)) goto err;
|
sl@0
|
519 |
|
sl@0
|
520 |
/* we are in this state */
|
sl@0
|
521 |
s->state=SSL3_ST_CR_SRVR_HELLO_A;
|
sl@0
|
522 |
|
sl@0
|
523 |
/* put the 5 bytes we have read into the input buffer
|
sl@0
|
524 |
* for SSLv3 */
|
sl@0
|
525 |
s->rstate=SSL_ST_READ_HEADER;
|
sl@0
|
526 |
s->packet_length=n;
|
sl@0
|
527 |
s->packet= &(s->s3->rbuf.buf[0]);
|
sl@0
|
528 |
memcpy(s->packet,buf,n);
|
sl@0
|
529 |
s->s3->rbuf.left=n;
|
sl@0
|
530 |
s->s3->rbuf.offset=0;
|
sl@0
|
531 |
|
sl@0
|
532 |
if ((p[2] == SSL3_VERSION_MINOR) &&
|
sl@0
|
533 |
!(s->options & SSL_OP_NO_SSLv3))
|
sl@0
|
534 |
{
|
sl@0
|
535 |
s->version=SSL3_VERSION;
|
sl@0
|
536 |
s->method=SSLv3_client_method();
|
sl@0
|
537 |
}
|
sl@0
|
538 |
else if ((p[2] == TLS1_VERSION_MINOR) &&
|
sl@0
|
539 |
!(s->options & SSL_OP_NO_TLSv1))
|
sl@0
|
540 |
{
|
sl@0
|
541 |
s->version=TLS1_VERSION;
|
sl@0
|
542 |
s->method=TLSv1_client_method();
|
sl@0
|
543 |
}
|
sl@0
|
544 |
else
|
sl@0
|
545 |
{
|
sl@0
|
546 |
SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
|
sl@0
|
547 |
goto err;
|
sl@0
|
548 |
}
|
sl@0
|
549 |
|
sl@0
|
550 |
s->handshake_func=s->method->ssl_connect;
|
sl@0
|
551 |
}
|
sl@0
|
552 |
else if ((p[0] == SSL3_RT_ALERT) &&
|
sl@0
|
553 |
(p[1] == SSL3_VERSION_MAJOR) &&
|
sl@0
|
554 |
((p[2] == SSL3_VERSION_MINOR) ||
|
sl@0
|
555 |
(p[2] == TLS1_VERSION_MINOR)) &&
|
sl@0
|
556 |
(p[3] == 0) &&
|
sl@0
|
557 |
(p[4] == 2))
|
sl@0
|
558 |
{
|
sl@0
|
559 |
void (*cb)(const SSL *ssl,int type,int val)=NULL;
|
sl@0
|
560 |
int j;
|
sl@0
|
561 |
|
sl@0
|
562 |
/* An alert */
|
sl@0
|
563 |
if (s->info_callback != NULL)
|
sl@0
|
564 |
cb=s->info_callback;
|
sl@0
|
565 |
else if (s->ctx->info_callback != NULL)
|
sl@0
|
566 |
cb=s->ctx->info_callback;
|
sl@0
|
567 |
|
sl@0
|
568 |
i=p[5];
|
sl@0
|
569 |
if (cb != NULL)
|
sl@0
|
570 |
{
|
sl@0
|
571 |
j=(i<<8)|p[6];
|
sl@0
|
572 |
cb(s,SSL_CB_READ_ALERT,j);
|
sl@0
|
573 |
}
|
sl@0
|
574 |
|
sl@0
|
575 |
s->rwstate=SSL_NOTHING;
|
sl@0
|
576 |
SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
|
sl@0
|
577 |
goto err;
|
sl@0
|
578 |
}
|
sl@0
|
579 |
else
|
sl@0
|
580 |
{
|
sl@0
|
581 |
SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
|
sl@0
|
582 |
goto err;
|
sl@0
|
583 |
}
|
sl@0
|
584 |
s->init_num=0;
|
sl@0
|
585 |
|
sl@0
|
586 |
/* Since, if we are sending a ssl23 client hello, we are not
|
sl@0
|
587 |
* reusing a session-id */
|
sl@0
|
588 |
if (!ssl_get_new_session(s,0))
|
sl@0
|
589 |
goto err;
|
sl@0
|
590 |
|
sl@0
|
591 |
return(SSL_connect(s));
|
sl@0
|
592 |
err:
|
sl@0
|
593 |
return(-1);
|
sl@0
|
594 |
}
|
sl@0
|
595 |
|