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