really fix race condition
[openssl.git] / ssl / s23_clnt.c
1 /* ssl/s23_clnt.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
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>
60 #include "ssl_locl.h"
61 #include <openssl/buffer.h>
62 #include <openssl/rand.h>
63 #include <openssl/objects.h>
64 #include <openssl/evp.h>
65
66 static SSL_METHOD *ssl23_get_client_method(int ver);
67 static int ssl23_client_hello(SSL *s);
68 static int ssl23_get_server_hello(SSL *s);
69 static SSL_METHOD *ssl23_get_client_method(int ver)
70         {
71 #ifndef OPENSSL_NO_SSL2
72         if (ver == SSL2_VERSION)
73                 return(SSLv2_client_method());
74 #endif
75         if (ver == SSL3_VERSION)
76                 return(SSLv3_client_method());
77         else if (ver == TLS1_VERSION)
78                 return(TLSv1_client_method());
79         else
80                 return(NULL);
81         }
82
83 SSL_METHOD *SSLv23_client_method(void)
84         {
85         static int init=1;
86         static SSL_METHOD SSLv23_client_data;
87
88         if (init)
89                 {
90                 CRYPTO_w_lock(CRYPTO_LOCK_SSL_METHOD);
91
92                 memcpy((char *)&SSLv23_client_data,
93                         (char *)sslv23_base_method(),sizeof(SSL_METHOD));
94                 SSLv23_client_data.ssl_connect=ssl23_connect;
95                 SSLv23_client_data.get_ssl_method=ssl23_get_client_method;
96                 init=0;
97
98                 CRYPTO_w_unlock(CRYPTO_LOCK_SSL_METHOD);
99                 }
100         return(&SSLv23_client_data);
101         }
102
103 int ssl23_connect(SSL *s)
104         {
105         BUF_MEM *buf;
106         unsigned long Time=time(NULL);
107         void (*cb)(const SSL *ssl,int type,int val)=NULL;
108         int ret= -1;
109         int new_state,state;
110
111         RAND_add(&Time,sizeof(Time),0);
112         ERR_clear_error();
113         clear_sys_error();
114
115         if (s->info_callback != NULL)
116                 cb=s->info_callback;
117         else if (s->ctx->info_callback != NULL)
118                 cb=s->ctx->info_callback;
119         
120         s->in_handshake++;
121         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
122
123         for (;;)
124                 {
125                 state=s->state;
126
127                 switch(s->state)
128                         {
129                 case SSL_ST_BEFORE:
130                 case SSL_ST_CONNECT:
131                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
132                 case SSL_ST_OK|SSL_ST_CONNECT:
133
134                         if (s->session != NULL)
135                                 {
136                                 SSLerr(SSL_F_SSL23_CONNECT,SSL_R_SSL23_DOING_SESSION_ID_REUSE);
137                                 ret= -1;
138                                 goto end;
139                                 }
140                         s->server=0;
141                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
142
143                         /* s->version=TLS1_VERSION; */
144                         s->type=SSL_ST_CONNECT;
145
146                         if (s->init_buf == NULL)
147                                 {
148                                 if ((buf=BUF_MEM_new()) == NULL)
149                                         {
150                                         ret= -1;
151                                         goto end;
152                                         }
153                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
154                                         {
155                                         ret= -1;
156                                         goto end;
157                                         }
158                                 s->init_buf=buf;
159                                 }
160
161                         if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
162
163                         ssl3_init_finished_mac(s);
164
165                         s->state=SSL23_ST_CW_CLNT_HELLO_A;
166                         s->ctx->stats.sess_connect++;
167                         s->init_num=0;
168                         break;
169
170                 case SSL23_ST_CW_CLNT_HELLO_A:
171                 case SSL23_ST_CW_CLNT_HELLO_B:
172
173                         s->shutdown=0;
174                         ret=ssl23_client_hello(s);
175                         if (ret <= 0) goto end;
176                         s->state=SSL23_ST_CR_SRVR_HELLO_A;
177                         s->init_num=0;
178
179                         break;
180
181                 case SSL23_ST_CR_SRVR_HELLO_A:
182                 case SSL23_ST_CR_SRVR_HELLO_B:
183                         ret=ssl23_get_server_hello(s);
184                         if (ret >= 0) cb=NULL;
185                         goto end;
186                         /* break; */
187
188                 default:
189                         SSLerr(SSL_F_SSL23_CONNECT,SSL_R_UNKNOWN_STATE);
190                         ret= -1;
191                         goto end;
192                         /* break; */
193                         }
194
195                 if (s->debug) { (void)BIO_flush(s->wbio); }
196
197                 if ((cb != NULL) && (s->state != state))
198                         {
199                         new_state=s->state;
200                         s->state=state;
201                         cb(s,SSL_CB_CONNECT_LOOP,1);
202                         s->state=new_state;
203                         }
204                 }
205 end:
206         s->in_handshake--;
207         if (cb != NULL)
208                 cb(s,SSL_CB_CONNECT_EXIT,ret);
209         return(ret);
210         }
211
212
213 static int ssl23_client_hello(SSL *s)
214         {
215         unsigned char *buf;
216         unsigned char *p,*d;
217         int i,ch_len;
218         int ret;
219
220         buf=(unsigned char *)s->init_buf->data;
221         if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
222                 {
223 #if 0
224                 /* don't reuse session-id's */
225                 if (!ssl_get_new_session(s,0))
226                         {
227                         return(-1);
228                         }
229 #endif
230
231                 p=s->s3->client_random;
232                 RAND_pseudo_bytes(p,SSL3_RANDOM_SIZE);
233
234                 /* Do the message type and length last */
235                 d= &(buf[2]);
236                 p=d+9;
237
238                 *(d++)=SSL2_MT_CLIENT_HELLO;
239                 if (!(s->options & SSL_OP_NO_TLSv1))
240                         {
241                         *(d++)=TLS1_VERSION_MAJOR;
242                         *(d++)=TLS1_VERSION_MINOR;
243                         s->client_version=TLS1_VERSION;
244                         }
245                 else if (!(s->options & SSL_OP_NO_SSLv3))
246                         {
247                         *(d++)=SSL3_VERSION_MAJOR;
248                         *(d++)=SSL3_VERSION_MINOR;
249                         s->client_version=SSL3_VERSION;
250                         }
251                 else if (!(s->options & SSL_OP_NO_SSLv2))
252                         {
253                         *(d++)=SSL2_VERSION_MAJOR;
254                         *(d++)=SSL2_VERSION_MINOR;
255                         s->client_version=SSL2_VERSION;
256                         }
257                 else
258                         {
259                         SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_PROTOCOLS_AVAILABLE);
260                         return(-1);
261                         }
262
263                 /* Ciphers supported */
264                 i=ssl_cipher_list_to_bytes(s,SSL_get_ciphers(s),p);
265                 if (i == 0)
266                         {
267                         /* no ciphers */
268                         SSLerr(SSL_F_SSL23_CLIENT_HELLO,SSL_R_NO_CIPHERS_AVAILABLE);
269                         return(-1);
270                         }
271                 s2n(i,d);
272                 p+=i;
273
274                 /* put in the session-id, zero since there is no
275                  * reuse. */
276 #if 0
277                 s->session->session_id_length=0;
278 #endif
279                 s2n(0,d);
280
281                 if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
282                         ch_len=SSL2_CHALLENGE_LENGTH;
283                 else
284                         ch_len=SSL2_MAX_CHALLENGE_LENGTH;
285
286                 /* write out sslv2 challenge */
287                 if (SSL3_RANDOM_SIZE < ch_len)
288                         i=SSL3_RANDOM_SIZE;
289                 else
290                         i=ch_len;
291                 s2n(i,d);
292                 memset(&(s->s3->client_random[0]),0,SSL3_RANDOM_SIZE);
293                 RAND_pseudo_bytes(&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
294                 memcpy(p,&(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
295                 p+=i;
296
297                 i= p- &(buf[2]);
298                 buf[0]=((i>>8)&0xff)|0x80;
299                 buf[1]=(i&0xff);
300
301                 s->state=SSL23_ST_CW_CLNT_HELLO_B;
302                 /* number of bytes to write */
303                 s->init_num=i+2;
304                 s->init_off=0;
305
306                 ssl3_finish_mac(s,&(buf[2]),i);
307                 }
308
309         /* SSL3_ST_CW_CLNT_HELLO_B */
310         ret = ssl23_write_bytes(s);
311         if (ret >= 2)
312                 if (s->msg_callback)
313                         s->msg_callback(1, SSL2_VERSION, 0, s->init_buf->data+2, ret-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
314         return ret;
315         }
316
317 static int ssl23_get_server_hello(SSL *s)
318         {
319         char buf[8];
320         unsigned char *p;
321         int i;
322         int n;
323
324         n=ssl23_read_bytes(s,7);
325
326         if (n != 7) return(n);
327         p=s->packet;
328
329         memcpy(buf,p,n);
330
331         if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
332                 (p[5] == 0x00) && (p[6] == 0x02))
333                 {
334 #ifdef OPENSSL_NO_SSL2
335                 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
336                 goto err;
337 #else
338                 /* we are talking sslv2 */
339                 /* we need to clean up the SSLv3 setup and put in the
340                  * sslv2 stuff. */
341                 int ch_len;
342
343                 if (s->options & SSL_OP_NO_SSLv2)
344                         {
345                         SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
346                         goto err;
347                         }
348                 if (s->s2 == NULL)
349                         {
350                         if (!ssl2_new(s))
351                                 goto err;
352                         }
353                 else
354                         ssl2_clear(s);
355
356                 if (s->options & SSL_OP_NETSCAPE_CHALLENGE_BUG)
357                         ch_len=SSL2_CHALLENGE_LENGTH;
358                 else
359                         ch_len=SSL2_MAX_CHALLENGE_LENGTH;
360
361                 /* write out sslv2 challenge */
362                 i=(SSL3_RANDOM_SIZE < ch_len)
363                         ?SSL3_RANDOM_SIZE:ch_len;
364                 s->s2->challenge_length=i;
365                 memcpy(s->s2->challenge,
366                         &(s->s3->client_random[SSL3_RANDOM_SIZE-i]),i);
367
368                 if (s->s3 != NULL) ssl3_free(s);
369
370                 if (!BUF_MEM_grow(s->init_buf,
371                         SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
372                         {
373                         SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,ERR_R_BUF_LIB);
374                         goto err;
375                         }
376
377                 s->state=SSL2_ST_GET_SERVER_HELLO_A;
378                 if (!(s->client_version == SSL2_VERSION))
379                         /* use special padding (SSL 3.0 draft/RFC 2246, App. E.2) */
380                         s->s2->ssl2_rollback=1;
381
382                 /* setup the 5 bytes we have read so we get them from
383                  * the sslv2 buffer */
384                 s->rstate=SSL_ST_READ_HEADER;
385                 s->packet_length=n;
386                 s->packet= &(s->s2->rbuf[0]);
387                 memcpy(s->packet,buf,n);
388                 s->s2->rbuf_left=n;
389                 s->s2->rbuf_offs=0;
390
391                 /* we have already written one */
392                 s->s2->write_sequence=1;
393
394                 s->method=SSLv2_client_method();
395                 s->handshake_func=s->method->ssl_connect;
396 #endif
397                 }
398         else if ((p[0] == SSL3_RT_HANDSHAKE) &&
399                  (p[1] == SSL3_VERSION_MAJOR) &&
400                  ((p[2] == SSL3_VERSION_MINOR) ||
401                   (p[2] == TLS1_VERSION_MINOR)) &&
402                  (p[5] == SSL3_MT_SERVER_HELLO))
403                 {
404                 /* we have sslv3 or tls1 */
405
406                 if (!ssl_init_wbio_buffer(s,1)) goto err;
407
408                 /* we are in this state */
409                 s->state=SSL3_ST_CR_SRVR_HELLO_A;
410
411                 /* put the 5 bytes we have read into the input buffer
412                  * for SSLv3 */
413                 s->rstate=SSL_ST_READ_HEADER;
414                 s->packet_length=n;
415                 s->packet= &(s->s3->rbuf.buf[0]);
416                 memcpy(s->packet,buf,n);
417                 s->s3->rbuf.left=n;
418                 s->s3->rbuf.offset=0;
419
420                 if ((p[2] == SSL3_VERSION_MINOR) &&
421                         !(s->options & SSL_OP_NO_SSLv3))
422                         {
423                         s->version=SSL3_VERSION;
424                         s->method=SSLv3_client_method();
425                         }
426                 else if ((p[2] == TLS1_VERSION_MINOR) &&
427                         !(s->options & SSL_OP_NO_TLSv1))
428                         {
429                         s->version=TLS1_VERSION;
430                         s->method=TLSv1_client_method();
431                         }
432                 else
433                         {
434                         SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNSUPPORTED_PROTOCOL);
435                         goto err;
436                         }
437                         
438                 s->handshake_func=s->method->ssl_connect;
439                 }
440         else if ((p[0] == SSL3_RT_ALERT) &&
441                  (p[1] == SSL3_VERSION_MAJOR) &&
442                  ((p[2] == SSL3_VERSION_MINOR) ||
443                   (p[2] == TLS1_VERSION_MINOR)) &&
444                  (p[3] == 0) &&
445                  (p[4] == 2))
446                 {
447                 void (*cb)(const SSL *ssl,int type,int val)=NULL;
448                 int j;
449
450                 /* An alert */
451                 if (s->info_callback != NULL)
452                         cb=s->info_callback;
453                 else if (s->ctx->info_callback != NULL)
454                         cb=s->ctx->info_callback;
455  
456                 i=p[5];
457                 if (cb != NULL)
458                         {
459                         j=(i<<8)|p[6];
460                         cb(s,SSL_CB_READ_ALERT,j);
461                         }
462
463                 s->rwstate=SSL_NOTHING;
464                 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_AD_REASON_OFFSET+p[6]);
465                 goto err;
466                 }
467         else
468                 {
469                 SSLerr(SSL_F_SSL23_GET_SERVER_HELLO,SSL_R_UNKNOWN_PROTOCOL);
470                 goto err;
471                 }
472         s->init_num=0;
473
474         /* Since, if we are sending a ssl23 client hello, we are not
475          * reusing a session-id */
476         if (!ssl_get_new_session(s,0))
477                 goto err;
478
479         s->first_packet=1;
480         return(SSL_connect(s));
481 err:
482         return(-1);
483         }
484