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