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