Fix server behaviour when facing backwards-compatible client hellos.
[openssl.git] / ssl / s23_srvr.c
1 /* ssl/s23_srvr.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_server_method(int ver);
67 int ssl23_get_client_hello(SSL *s);
68 static SSL_METHOD *ssl23_get_server_method(int ver)
69         {
70         if (ver == SSL2_VERSION)
71                 return(SSLv2_server_method());
72         if (ver == SSL3_VERSION)
73                 return(SSLv3_server_method());
74         else if (ver == TLS1_VERSION)
75                 return(TLSv1_server_method());
76         else
77                 return(NULL);
78         }
79
80 SSL_METHOD *SSLv23_server_method(void)
81         {
82         static int init=1;
83         static SSL_METHOD SSLv23_server_data;
84
85         if (init)
86                 {
87                 memcpy((char *)&SSLv23_server_data,
88                         (char *)sslv23_base_method(),sizeof(SSL_METHOD));
89                 SSLv23_server_data.ssl_accept=ssl23_accept;
90                 SSLv23_server_data.get_ssl_method=ssl23_get_server_method;
91                 init=0;
92                 }
93         return(&SSLv23_server_data);
94         }
95
96 int ssl23_accept(SSL *s)
97         {
98         BUF_MEM *buf;
99         unsigned long Time=time(NULL);
100         void (*cb)()=NULL;
101         int ret= -1;
102         int new_state,state;
103
104         RAND_seed(&Time,sizeof(Time));
105         ERR_clear_error();
106         clear_sys_error();
107
108         if (s->info_callback != NULL)
109                 cb=s->info_callback;
110         else if (s->ctx->info_callback != NULL)
111                 cb=s->ctx->info_callback;
112         
113         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
114         s->in_handshake++;
115
116         for (;;)
117                 {
118                 state=s->state;
119
120                 switch(s->state)
121                         {
122                 case SSL_ST_BEFORE:
123                 case SSL_ST_ACCEPT:
124                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
125                 case SSL_ST_OK|SSL_ST_ACCEPT:
126
127                         s->server=1;
128                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
129
130                         /* s->version=SSL3_VERSION; */
131                         s->type=SSL_ST_ACCEPT;
132
133                         if (s->init_buf == NULL)
134                                 {
135                                 if ((buf=BUF_MEM_new()) == NULL)
136                                         {
137                                         ret= -1;
138                                         goto end;
139                                         }
140                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
141                                         {
142                                         ret= -1;
143                                         goto end;
144                                         }
145                                 s->init_buf=buf;
146                                 }
147
148                         ssl3_init_finished_mac(s);
149
150                         s->state=SSL23_ST_SR_CLNT_HELLO_A;
151                         s->ctx->stats.sess_accept++;
152                         s->init_num=0;
153                         break;
154
155                 case SSL23_ST_SR_CLNT_HELLO_A:
156                 case SSL23_ST_SR_CLNT_HELLO_B:
157
158                         s->shutdown=0;
159                         ret=ssl23_get_client_hello(s);
160                         if (ret >= 0) cb=NULL;
161                         goto end;
162                         /* break; */
163
164                 default:
165                         SSLerr(SSL_F_SSL23_ACCEPT,SSL_R_UNKNOWN_STATE);
166                         ret= -1;
167                         goto end;
168                         /* break; */
169                         }
170
171                 if ((cb != NULL) && (s->state != state))
172                         {
173                         new_state=s->state;
174                         s->state=state;
175                         cb(s,SSL_CB_ACCEPT_LOOP,1);
176                         s->state=new_state;
177                         }
178                 }
179 end:
180         if (cb != NULL)
181                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
182         s->in_handshake--;
183         return(ret);
184         }
185
186
187 int ssl23_get_client_hello(SSL *s)
188         {
189         char buf_space[10];
190         char *buf= &(buf_space[0]);
191         unsigned char *p,*d,*dd;
192         unsigned int i;
193         unsigned int csl,sil,cl;
194         int n=0,j,tls1=0;
195         int type=0,use_sslv2_strong=0;
196         int v[2];
197
198         if (s->state == SSL23_ST_SR_CLNT_HELLO_A)
199                 {
200                 /* read the initial header */
201                 v[0]=v[1]=0;
202
203                 if (!ssl3_setup_buffers(s)) goto err;
204
205                 n=ssl23_read_bytes(s,10);
206                 if (n != 10) return(n); /* n == -1 || n == 0 */
207
208                 p=s->packet;
209
210                 memcpy(buf,p,n);
211
212                 if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
213                         {
214                         /*
215                          * SSLv2 header
216                          */
217                         if ((p[3] == 0x00) && (p[4] == 0x02))
218                                 {
219                                 v[0]=p[3]; v[1]=p[4];
220                                 /* SSLv2 */
221                                 if (!(s->options & SSL_OP_NO_SSLv2))
222                                         type=1;
223                                 }
224                         else if (p[3] == SSL3_VERSION_MAJOR)
225                                 {
226                                 v[0]=p[3]; v[1]=p[4];
227                                 /* SSLv3/TLSv1 */
228                                 if (p[4] >= TLS1_VERSION_MINOR)
229                                         {
230                                         if (!(s->options & SSL_OP_NO_TLSv1))
231                                                 {
232                                                 tls1=1;
233                                                 /* type=2; */ /* done later to survive restarts */
234                                                 s->state=SSL23_ST_SR_CLNT_HELLO_B;
235                                                 }
236                                         else if (!(s->options & SSL_OP_NO_SSLv3))
237                                                 {
238                                                 /* type=2; */
239                                                 s->state=SSL23_ST_SR_CLNT_HELLO_B;
240                                                 }
241                                         else if (!(s->options & SSL_OP_NO_SSLv2))
242                                                 {
243                                                 type=1;
244                                                 }
245                                         }
246                                 else if (!(s->options & SSL_OP_NO_SSLv3))
247                                         {
248                                         /* type=2; */
249                                         s->state=SSL23_ST_SR_CLNT_HELLO_B;
250                                         }
251                                 else if (!(s->options & SSL_OP_NO_SSLv2))
252                                         type=1;
253
254                                 if (s->options & SSL_OP_NON_EXPORT_FIRST)
255                                         /* Not only utterly confusing, but broken
256                                          * ('fractured programming'?) -- the details
257                                          * of this block nearly make it work
258                                          * as intended in this environment, but on one
259                                          * of the fine points (w.r.t. restarts) it fails.
260                                          * The obvious fix would be even more devastating
261                                          * to program structure; if you want the functionality,
262                                          * throw this away and implement it in a way
263                                          * that makes sense */
264                                         {
265                                         STACK_OF(SSL_CIPHER) *sk;
266                                         SSL_CIPHER *c;
267                                         int ne2,ne3;
268
269                                         j=((p[0]&0x7f)<<8)|p[1];
270                                         if (j > (1024*4))
271                                                 {
272                                                 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
273                                                 goto err;
274                                                 }
275
276                                         n=ssl23_read_bytes(s,j+2);
277                                         if (n <= 0) return(n);
278                                         p=s->packet;
279
280                                         if ((buf=Malloc(n)) == NULL)
281                                                 {
282                                                 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,ERR_R_MALLOC_FAILURE);
283                                                 goto err;
284                                                 }
285                                         memcpy(buf,p,n);
286
287                                         p+=5;
288                                         n2s(p,csl);
289                                         p+=4;
290
291                                         sk=ssl_bytes_to_cipher_list(
292                                                 s,p,csl,NULL);
293                                         if (sk != NULL)
294                                                 {
295                                                 ne2=ne3=0;
296                                                 for (j=0; j<sk_SSL_CIPHER_num(sk); j++)
297                                                         {
298                                                         c=sk_SSL_CIPHER_value(sk,j);
299                                                         if (!SSL_C_IS_EXPORT(c))
300                                                                 {
301                                                                 if ((c->id>>24L) == 2L)
302                                                                         ne2=1;
303                                                                 else
304                                                                         ne3=1;
305                                                                 }
306                                                         }
307                                                 if (ne2 && !ne3)
308                                                         {
309                                                         type=1;
310                                                         use_sslv2_strong=1;
311                                                         goto next_bit;
312                                                         }
313                                                 }
314                                         }
315                                 }
316                         }
317                 else if ((p[0] == SSL3_RT_HANDSHAKE) &&
318                          (p[1] == SSL3_VERSION_MAJOR) &&
319                          (p[5] == SSL3_MT_CLIENT_HELLO) &&
320                          (p[9] == p[1]))
321                         {
322                         /*
323                          * SSLv3 or tls1 header
324                          */
325                         
326                         /* we must look at client_version inside the client hello: */
327                         n=ssl23_read_bytes(s,11);
328                         /* restarts are no problem here, stay in initial state */
329                         if (n != 11)
330                                 return(n); /* n == -1 || n == 0 */
331
332                         v[0]=p[9]; v[1]=p[10];
333                         if (p[2] >= TLS1_VERSION_MINOR)
334                                 {
335                                 if (!(s->options & SSL_OP_NO_TLSv1))
336                                         {
337                                         type=3;
338                                         tls1=1;
339                                         }
340                                 else if (!(s->options & SSL_OP_NO_SSLv3))
341                                         type=3;
342                                 }
343                         else if (!(s->options & SSL_OP_NO_SSLv3))
344                                 type=3;
345                         }
346                 else if ((strncmp("GET ", (char *)p,4) == 0) ||
347                          (strncmp("POST ",(char *)p,5) == 0) ||
348                          (strncmp("HEAD ",(char *)p,5) == 0) ||
349                          (strncmp("PUT ", (char *)p,4) == 0))
350                         {
351                         SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTP_REQUEST);
352                         goto err;
353                         }
354                 else if (strncmp("CONNECT",(char *)p,7) == 0)
355                         {
356                         SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_HTTPS_PROXY_REQUEST);
357                         goto err;
358                         }
359                 }
360
361 next_bit:
362         if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
363                 {
364                 /* we have a SSLv3/TLSv1 in a SSLv2 header
365                  * (other cases skip this state)* */
366                 type=2;
367                 p=s->packet;
368                 v[0] = p[3];
369                 v[1] = p[4];
370                 n=((p[0]&0x7f)<<8)|p[1];
371                 if (n > (1024*4))
372                         {
373                         SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_TOO_LARGE);
374                         goto err;
375                         }
376
377                 j=ssl23_read_bytes(s,n+2);
378                 if (j <= 0) return(j);
379
380                 ssl3_finish_mac(s,&(s->packet[2]),s->packet_length-2);
381
382                 p=s->packet;
383                 p+=5;
384                 n2s(p,csl);
385                 n2s(p,sil);
386                 n2s(p,cl);
387                 d=(unsigned char *)s->init_buf->data;
388                 if ((csl+sil+cl+11) != s->packet_length)
389                         {
390                         SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_RECORD_LENGTH_MISMATCH);
391                         goto err;
392                         }
393
394                 *(d++)=SSL3_VERSION_MAJOR;
395                 if (tls1)
396                         *(d++)=TLS1_VERSION_MINOR;
397                 else
398                         *(d++)=SSL3_VERSION_MINOR;
399
400                 /* lets populate the random area */
401                 /* get the chalenge_length */
402                 i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
403                 memset(d,0,SSL3_RANDOM_SIZE);
404                 memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
405                 d+=SSL3_RANDOM_SIZE;
406
407                 /* no session-id reuse */
408                 *(d++)=0;
409
410                 /* ciphers */
411                 j=0;
412                 dd=d;
413                 d+=2;
414                 for (i=0; i<csl; i+=3)
415                         {
416                         if (p[i] != 0) continue;
417                         *(d++)=p[i+1];
418                         *(d++)=p[i+2];
419                         j+=2;
420                         }
421                 s2n(j,dd);
422
423                 /* COMPRESSION */
424                 *(d++)=1;
425                 *(d++)=0;
426                 
427                 i=(d-(unsigned char *)s->init_buf->data);
428
429                 /* get the data reused from the init_buf */
430                 s->s3->tmp.reuse_message=1;
431                 s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
432                 s->s3->tmp.message_size=i;
433                 }
434
435         /* imaginary new state (for program structure): */
436         /* s->state = SSL23_SR_CLNT_HELLO_C */
437
438         if (type == 1)
439                 {
440                 /* we are talking sslv2 */
441                 /* we need to clean up the SSLv3/TLSv1 setup and put in the
442                  * sslv2 stuff. */
443
444                 if (s->s2 == NULL)
445                         {
446                         if (!ssl2_new(s))
447                                 goto err;
448                         }
449                 else
450                         ssl2_clear(s);
451
452                 if (s->s3 != NULL) ssl3_free(s);
453
454                 if (!BUF_MEM_grow(s->init_buf,
455                         SSL2_MAX_RECORD_LENGTH_3_BYTE_HEADER))
456                         {
457                         goto err;
458                         }
459
460                 s->state=SSL2_ST_GET_CLIENT_HELLO_A;
461                 if ((s->options & SSL_OP_MSIE_SSLV2_RSA_PADDING) ||
462                         use_sslv2_strong)
463                         s->s2->ssl2_rollback=0;
464                 else
465                         s->s2->ssl2_rollback=1;
466
467                 /* setup the n bytes we have read so we get them from
468                  * the sslv2 buffer */
469                 s->rstate=SSL_ST_READ_HEADER;
470                 s->packet_length=n;
471                 s->packet= &(s->s2->rbuf[0]);
472                 memcpy(s->packet,buf,n);
473                 s->s2->rbuf_left=n;
474                 s->s2->rbuf_offs=0;
475
476                 s->method=SSLv2_server_method();
477                 s->handshake_func=s->method->ssl_accept;
478                 }
479
480         if ((type == 2) || (type == 3))
481                 {
482                 /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
483
484                 if (!ssl_init_wbio_buffer(s,1)) goto err;
485
486                 /* we are in this state */
487                 s->state=SSL3_ST_SR_CLNT_HELLO_A;
488
489                 if (type == 3)
490                         {
491                         /* put the 'n' bytes we have read into the input buffer
492                          * for SSLv3 */
493                         s->rstate=SSL_ST_READ_HEADER;
494                         s->packet_length=n;
495                         s->packet= &(s->s3->rbuf.buf[0]);
496                         memcpy(s->packet,buf,n);
497                         s->s3->rbuf.left=n;
498                         s->s3->rbuf.offset=0;
499                         }
500                 else
501                         {
502                         s->packet_length=0;
503                         s->s3->rbuf.left=0;
504                         s->s3->rbuf.offset=0;
505                         }
506
507                 if (tls1)
508                         {
509                         s->version=TLS1_VERSION;
510                         s->method=TLSv1_server_method();
511                         }
512                 else
513                         {
514                         s->version=SSL3_VERSION;
515                         s->method=SSLv3_server_method();
516                         }
517 #if 0 /* ssl3_get_client_hello does this */
518                 s->client_version=(v[0]<<8)|v[1];
519 #endif
520                 s->handshake_func=s->method->ssl_accept;
521                 }
522         
523         if ((type < 1) || (type > 3))
524                 {
525                 /* bad, very bad */
526                 SSLerr(SSL_F_SSL23_GET_CLIENT_HELLO,SSL_R_UNKNOWN_PROTOCOL);
527                 goto err;
528                 }
529         s->init_num=0;
530
531         if (buf != buf_space) Free(buf);
532         s->first_packet=1;
533         return(SSL_accept(s));
534 err:
535         if (buf != buf_space) Free(buf);
536         return(-1);
537         }
538