Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / ssl / s23_srvr.c
1 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
2  * All rights reserved.
3  *
4  * This package is an SSL implementation written
5  * by Eric Young (eay@cryptsoft.com).
6  * The implementation was written so as to conform with Netscapes SSL.
7  *
8  * This library is free for commercial and non-commercial use as long as
9  * the following conditions are aheared to.  The following conditions
10  * apply to all code found in this distribution, be it the RC4, RSA,
11  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
12  * included with this distribution is covered by the same copyright terms
13  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
14  *
15  * Copyright remains Eric Young's, and as such any Copyright notices in
16  * the code are not to be removed.
17  * If this package is used in a product, Eric Young should be given attribution
18  * as the author of the parts of the library used.
19  * This can be in the form of a textual message at program startup or
20  * in documentation (online or textual) provided with the package.
21  *
22  * Redistribution and use in source and binary forms, with or without
23  * modification, are permitted provided that the following conditions
24  * are met:
25  * 1. Redistributions of source code must retain the copyright
26  *    notice, this list of conditions and the following disclaimer.
27  * 2. Redistributions in binary form must reproduce the above copyright
28  *    notice, this list of conditions and the following disclaimer in the
29  *    documentation and/or other materials provided with the distribution.
30  * 3. All advertising materials mentioning features or use of this software
31  *    must display the following acknowledgement:
32  *    "This product includes cryptographic software written by
33  *     Eric Young (eay@cryptsoft.com)"
34  *    The word 'cryptographic' can be left out if the rouines from the library
35  *    being used are not cryptographic related :-).
36  * 4. If you include any Windows specific code (or a derivative thereof) from
37  *    the apps directory (application code) you must include an acknowledgement:
38  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
41  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
44  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
45  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
46  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
48  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
49  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  *
52  * The licence and distribution terms for any publically available version or
53  * derivative of this code cannot be changed.  i.e. this code cannot simply be
54  * copied and put under another distribution licence
55  * [including the GNU Public Licence.]
56  */
57 /* ====================================================================
58  * Copyright (c) 1998-2006 The OpenSSL Project.  All rights reserved.
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  *    notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  *    notice, this list of conditions and the following disclaimer in
69  *    the documentation and/or other materials provided with the
70  *    distribution.
71  *
72  * 3. All advertising materials mentioning features or use of this
73  *    software must display the following acknowledgment:
74  *    "This product includes software developed by the OpenSSL Project
75  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
76  *
77  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
78  *    endorse or promote products derived from this software without
79  *    prior written permission. For written permission, please contact
80  *    openssl-core@openssl.org.
81  *
82  * 5. Products derived from this software may not be called "OpenSSL"
83  *    nor may "OpenSSL" appear in their names without prior written
84  *    permission of the OpenSSL Project.
85  *
86  * 6. Redistributions of any form whatsoever must retain the following
87  *    acknowledgment:
88  *    "This product includes software developed by the OpenSSL Project
89  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
92  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
94  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
95  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
96  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
97  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
98  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
99  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
100  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
101  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
102  * OF THE POSSIBILITY OF SUCH DAMAGE.
103  * ====================================================================
104  *
105  * This product includes cryptographic software written by Eric Young
106  * (eay@cryptsoft.com).  This product includes software written by Tim
107  * Hudson (tjh@cryptsoft.com). */
108
109 #include <stdio.h>
110
111 #include <openssl/buf.h>
112 #include <openssl/err.h>
113 #include <openssl/evp.h>
114 #include <openssl/mem.h>
115 #include <openssl/obj.h>
116 #include <openssl/rand.h>
117
118 #include "ssl_locl.h"
119
120 static const SSL_METHOD *ssl23_get_server_method(int ver);
121 int ssl23_get_client_hello(SSL *s);
122 static const SSL_METHOD *ssl23_get_server_method(int ver)
123         {
124         if (ver == SSL3_VERSION)
125                 return(SSLv3_server_method());
126         else if (ver == TLS1_VERSION)
127                 return(TLSv1_server_method());
128         else if (ver == TLS1_1_VERSION)
129                 return(TLSv1_1_server_method());
130         else if (ver == TLS1_2_VERSION)
131                 return(TLSv1_2_server_method());
132         else
133                 return(NULL);
134         }
135
136 IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
137                         ssl23_accept,
138                         ssl_undefined_function,
139                         ssl23_get_server_method)
140
141 int ssl23_accept(SSL *s)
142         {
143         BUF_MEM *buf;
144         void (*cb)(const SSL *ssl,int type,int val)=NULL;
145         int ret= -1;
146         int new_state,state;
147
148         ERR_clear_error();
149         ERR_clear_system_error();
150
151         if (s->info_callback != NULL)
152                 cb=s->info_callback;
153         else if (s->ctx->info_callback != NULL)
154                 cb=s->ctx->info_callback;
155         
156         s->in_handshake++;
157         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
158
159         for (;;)
160                 {
161                 state=s->state;
162
163                 switch(s->state)
164                         {
165                 case SSL_ST_BEFORE:
166                 case SSL_ST_ACCEPT:
167                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
168                 case SSL_ST_OK|SSL_ST_ACCEPT:
169
170                         s->server=1;
171                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
172
173                         /* s->version=SSL3_VERSION; */
174                         s->type=SSL_ST_ACCEPT;
175
176                         if (s->init_buf == NULL)
177                                 {
178                                 if ((buf=BUF_MEM_new()) == NULL)
179                                         {
180                                         ret= -1;
181                                         goto end;
182                                         }
183                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
184                                         {
185                                         ret= -1;
186                                         goto end;
187                                         }
188                                 s->init_buf=buf;
189                                 }
190
191                         ssl3_init_finished_mac(s);
192
193                         s->state=SSL23_ST_SR_CLNT_HELLO_A;
194                         s->ctx->stats.sess_accept++;
195                         s->init_num=0;
196                         break;
197
198                 case SSL23_ST_SR_CLNT_HELLO_A:
199                 case SSL23_ST_SR_CLNT_HELLO_B:
200
201                         s->shutdown=0;
202                         ret=ssl23_get_client_hello(s);
203                         if (ret >= 0) cb=NULL;
204                         goto end;
205                         /* break; */
206
207                 default:
208                         OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE);
209                         ret= -1;
210                         goto end;
211                         /* break; */
212                         }
213
214                 if ((cb != NULL) && (s->state != state))
215                         {
216                         new_state=s->state;
217                         s->state=state;
218                         cb(s,SSL_CB_ACCEPT_LOOP,1);
219                         s->state=new_state;
220                         }
221                 }
222 end:
223         s->in_handshake--;
224         if (cb != NULL)
225                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
226         return(ret);
227         }
228
229
230 int ssl23_get_client_hello(SSL *s)
231         {
232         char buf_space[11]; /* Request this many bytes in initial read.
233                              * We can detect SSL 3.0/TLS 1.0 Client Hellos
234                              * ('type == 3') correctly only when the following
235                              * is in a single record, which is not guaranteed by
236                              * the protocol specification:
237                              * Byte  Content
238                              *  0     type            \
239                              *  1/2   version          > record header
240                              *  3/4   length          /
241                              *  5     msg_type        \
242                              *  6-8   length           > Client Hello message
243                              *  9/10  client_version  /
244                              */
245         char *buf= &(buf_space[0]);
246         unsigned char *p,*d,*d_len,*dd;
247         unsigned int i;
248         unsigned int csl,sil,cl;
249         int n=0,j;
250         int type=0;
251         int v[2];
252
253         if (s->state == SSL23_ST_SR_CLNT_HELLO_A)
254                 {
255                 /* read the initial header */
256                 v[0]=v[1]=0;
257
258                 if (!ssl3_setup_buffers(s)) goto err;
259
260                 n=ssl23_read_bytes(s, sizeof buf_space);
261                 if (n != sizeof buf_space) return(n); /* n == -1 || n == 0 */
262
263                 p=s->packet;
264
265                 memcpy(buf,p,n);
266
267                 if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
268                         {
269                         /*
270                          * SSLv2 header
271                          */
272                         if ((p[3] == 0x00) && (p[4] == 0x02))
273                                 {
274                                 v[0]=p[3]; v[1]=p[4];
275                                 /* SSLv2 */
276                                 if (!(s->options & SSL_OP_NO_SSLv2))
277                                         type=1;
278                                 }
279                         else if (p[3] == SSL3_VERSION_MAJOR)
280                                 {
281                                 v[0]=p[3]; v[1]=p[4];
282                                 /* SSLv3/TLSv1 */
283                                 if (p[4] >= TLS1_VERSION_MINOR)
284                                         {
285                                         if (p[4] >= TLS1_2_VERSION_MINOR &&
286                                            !(s->options & SSL_OP_NO_TLSv1_2))
287                                                 {
288                                                 s->version=TLS1_2_VERSION;
289                                                 s->state=SSL23_ST_SR_CLNT_HELLO_B;
290                                                 }
291                                         else if (p[4] >= TLS1_1_VERSION_MINOR &&
292                                            !(s->options & SSL_OP_NO_TLSv1_1))
293                                                 {
294                                                 s->version=TLS1_1_VERSION;
295                                                 /* type=2; */ /* done later to survive restarts */
296                                                 s->state=SSL23_ST_SR_CLNT_HELLO_B;
297                                                 }
298                                         else if (!(s->options & SSL_OP_NO_TLSv1))
299                                                 {
300                                                 s->version=TLS1_VERSION;
301                                                 /* type=2; */ /* done later to survive restarts */
302                                                 s->state=SSL23_ST_SR_CLNT_HELLO_B;
303                                                 }
304                                         else if (!(s->options & SSL_OP_NO_SSLv3))
305                                                 {
306                                                 s->version=SSL3_VERSION;
307                                                 /* type=2; */
308                                                 s->state=SSL23_ST_SR_CLNT_HELLO_B;
309                                                 }
310                                         else if (!(s->options & SSL_OP_NO_SSLv2))
311                                                 {
312                                                 type=1;
313                                                 }
314                                         }
315                                 else if (!(s->options & SSL_OP_NO_SSLv3))
316                                         {
317                                         s->version=SSL3_VERSION;
318                                         /* type=2; */
319                                         s->state=SSL23_ST_SR_CLNT_HELLO_B;
320                                         }
321                                 else if (!(s->options & SSL_OP_NO_SSLv2))
322                                         type=1;
323
324                                 }
325                         }
326                 else if ((p[0] == SSL3_RT_HANDSHAKE) &&
327                          (p[1] == SSL3_VERSION_MAJOR) &&
328                          (p[5] == SSL3_MT_CLIENT_HELLO) &&
329                          ((p[3] == 0 && p[4] < 5 /* silly record length? */)
330                                 || (p[9] >= p[1])))
331                         {
332                         /*
333                          * SSLv3 or tls1 header
334                          */
335                         
336                         v[0]=p[1]; /* major version (= SSL3_VERSION_MAJOR) */
337                         /* We must look at client_version inside the Client Hello message
338                          * to get the correct minor version.
339                          * However if we have only a pathologically small fragment of the
340                          * Client Hello message, this would be difficult, and we'd have
341                          * to read more records to find out.
342                          * No known SSL 3.0 client fragments ClientHello like this,
343                          * so we simply assume TLS 1.0 to avoid protocol version downgrade
344                          * attacks. */
345                         if (p[3] == 0 && p[4] < 6)
346                                 {
347 #if 0
348                                 OPENSSL_PUT_ERROR(SSL, XXX, SSL_R_RECORD_TOO_SMALL);
349                                 goto err;
350 #else
351                                 v[1] = TLS1_VERSION_MINOR;
352 #endif
353                                 }
354                         /* if major version number > 3 set minor to a value
355                          * which will use the highest version 3 we support.
356                          * If TLS 2.0 ever appears we will need to revise
357                          * this....
358                          */
359                         else if (p[9] > SSL3_VERSION_MAJOR)
360                                 v[1]=0xff;
361                         else
362                                 v[1]=p[10]; /* minor version according to client_version */
363                         if (v[1] >= TLS1_VERSION_MINOR)
364                                 {
365                                 if (v[1] >= TLS1_2_VERSION_MINOR &&
366                                         !(s->options & SSL_OP_NO_TLSv1_2))
367                                         {
368                                         s->version=TLS1_2_VERSION;
369                                         type=3;
370                                         }
371                                 else if (v[1] >= TLS1_1_VERSION_MINOR &&
372                                         !(s->options & SSL_OP_NO_TLSv1_1))
373                                         {
374                                         s->version=TLS1_1_VERSION;
375                                         type=3;
376                                         }
377                                 else if (!(s->options & SSL_OP_NO_TLSv1))
378                                         {
379                                         s->version=TLS1_VERSION;
380                                         type=3;
381                                         }
382                                 else if (!(s->options & SSL_OP_NO_SSLv3))
383                                         {
384                                         s->version=SSL3_VERSION;
385                                         type=3;
386                                         }
387                                 }
388                         else
389                                 {
390                                 /* client requests SSL 3.0 */
391                                 if (!(s->options & SSL_OP_NO_SSLv3))
392                                         {
393                                         s->version=SSL3_VERSION;
394                                         type=3;
395                                         }
396                                 else if (!(s->options & SSL_OP_NO_TLSv1))
397                                         {
398                                         /* we won't be able to use TLS of course,
399                                          * but this will send an appropriate alert */
400                                         s->version=TLS1_VERSION;
401                                         type=3;
402                                         }
403                                 }
404                         }
405                 else if ((strncmp("GET ", (char *)p,4) == 0) ||
406                          (strncmp("POST ",(char *)p,5) == 0) ||
407                          (strncmp("HEAD ",(char *)p,5) == 0) ||
408                          (strncmp("PUT ", (char *)p,4) == 0))
409                         {
410                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST);
411                         goto err;
412                         }
413                 else if (strncmp("CONNECT",(char *)p,7) == 0)
414                         {
415                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST);
416                         goto err;
417                         }
418                 }
419
420         if (s->version < TLS1_2_VERSION && tls1_suiteb(s))
421                 {
422                 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
423                 goto err;
424                 }
425
426         if (s->state == SSL23_ST_SR_CLNT_HELLO_B)
427                 {
428                 /* we have SSLv3/TLSv1 in an SSLv2 header
429                  * (other cases skip this state) */
430
431                 type=2;
432                 p=s->packet;
433                 v[0] = p[3]; /* == SSL3_VERSION_MAJOR */
434                 v[1] = p[4];
435
436                 n=((p[0]&0x7f)<<8)|p[1];
437                 if (n > (1024*4))
438                         {
439                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_LARGE);
440                         goto err;
441                         }
442
443                 j=ssl23_read_bytes(s,n+2);
444                 if (j <= 0) return(j);
445
446                 ssl3_finish_mac(s, s->packet+2, s->packet_length-2);
447                 if (s->msg_callback)
448                         s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
449
450                 p=s->packet;
451                 p+=5;
452                 n2s(p,csl);
453                 n2s(p,sil);
454                 n2s(p,cl);
455                 d=(unsigned char *)s->init_buf->data;
456                 if ((csl+sil+cl+11) != s->packet_length) /* We can't have TLS extensions in SSL 2.0 format
457                                                           * Client Hello, can we? Error condition should be
458                                                           * '>' otherweise */
459                         {
460                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_LENGTH_MISMATCH);
461                         goto err;
462                         }
463
464                 /* record header: msg_type ... */
465                 *(d++) = SSL3_MT_CLIENT_HELLO;
466                 /* ... and length (actual value will be written later) */
467                 d_len = d;
468                 d += 3;
469
470                 /* client_version */
471                 *(d++) = SSL3_VERSION_MAJOR; /* == v[0] */
472                 *(d++) = v[1];
473
474                 /* lets populate the random area */
475                 /* get the challenge_length */
476                 i=(cl > SSL3_RANDOM_SIZE)?SSL3_RANDOM_SIZE:cl;
477                 memset(d,0,SSL3_RANDOM_SIZE);
478                 memcpy(&(d[SSL3_RANDOM_SIZE-i]),&(p[csl+sil]),i);
479                 d+=SSL3_RANDOM_SIZE;
480
481                 /* no session-id reuse */
482                 *(d++)=0;
483
484                 /* ciphers */
485                 j=0;
486                 dd=d;
487                 d+=2;
488                 for (i=0; i<csl; i+=3)
489                         {
490                         if (p[i] != 0) continue;
491                         *(d++)=p[i+1];
492                         *(d++)=p[i+2];
493                         j+=2;
494                         }
495                 s2n(j,dd);
496
497                 /* COMPRESSION */
498                 *(d++)=1;
499                 *(d++)=0;
500                 
501 #if 0
502                 /* copy any remaining data with may be extensions */
503                 p = p+csl+sil+cl;
504                 while (p <  s->packet+s->packet_length)
505                         {
506                         *(d++)=*(p++);
507                         }
508 #endif
509
510                 i = (d-(unsigned char *)s->init_buf->data) - 4;
511                 l2n3((long)i, d_len);
512
513                 /* get the data reused from the init_buf */
514                 s->s3->tmp.reuse_message=1;
515                 s->s3->tmp.message_type=SSL3_MT_CLIENT_HELLO;
516                 s->s3->tmp.message_size=i;
517                 }
518
519         /* imaginary new state (for program structure): */
520         /* s->state = SSL23_SR_CLNT_HELLO_C */
521
522         if (type == 1)
523                 {
524                 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
525                 goto err;
526                 }
527
528         if ((type == 2) || (type == 3))
529                 {
530                 /* we have SSLv3/TLSv1 (type 2: SSL2 style, type 3: SSL3/TLS style) */
531
532                 if (!ssl_init_wbio_buffer(s,1)) goto err;
533
534                 /* we are in this state */
535                 s->state=SSL3_ST_SR_CLNT_HELLO_A;
536
537                 if (type == 3)
538                         {
539                         /* put the 'n' bytes we have read into the input buffer
540                          * for SSLv3 */
541                         s->rstate=SSL_ST_READ_HEADER;
542                         s->packet_length=n;
543                         if (s->s3->rbuf.buf == NULL)
544                                 if (!ssl3_setup_read_buffer(s))
545                                         goto err;
546
547                         s->packet= &(s->s3->rbuf.buf[0]);
548                         memcpy(s->packet,buf,n);
549                         s->s3->rbuf.left=n;
550                         s->s3->rbuf.offset=0;
551                         }
552                 else
553                         {
554                         s->packet_length=0;
555                         s->s3->rbuf.left=0;
556                         s->s3->rbuf.offset=0;
557                         }
558                 if (s->version == TLS1_2_VERSION)
559                         s->method = TLSv1_2_server_method();
560                 else if (s->version == TLS1_1_VERSION)
561                         s->method = TLSv1_1_server_method();
562                 else if (s->version == TLS1_VERSION)
563                         s->method = TLSv1_server_method();
564                 else
565                         s->method = SSLv3_server_method();
566 #if 0 /* ssl3_get_client_hello does this */
567                 s->client_version=(v[0]<<8)|v[1];
568 #endif
569                 s->handshake_func=s->method->ssl_accept;
570                 }
571         
572         if ((type < 1) || (type > 3))
573                 {
574                 /* bad, very bad */
575                 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNKNOWN_PROTOCOL);
576                 goto err;
577                 }
578         s->init_num=0;
579
580         if (buf != buf_space) OPENSSL_free(buf);
581         return(SSL_accept(s));
582 err:
583         if (buf != buf_space) OPENSSL_free(buf);
584         return(-1);
585         }