Upstream version 10.39.225.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 <assert.h>
110 #include <stdio.h>
111
112 #include <openssl/buf.h>
113 #include <openssl/err.h>
114 #include <openssl/evp.h>
115 #include <openssl/mem.h>
116 #include <openssl/obj.h>
117 #include <openssl/rand.h>
118
119 #include "ssl_locl.h"
120
121 static const SSL_METHOD *ssl23_get_server_method(int ver);
122 static int ssl23_get_client_hello(SSL *s);
123 static int ssl23_get_v2_client_hello(SSL *s);
124
125 static const SSL_METHOD *ssl23_get_server_method(int ver)
126         {
127         if (ver == SSL3_VERSION)
128                 return(SSLv3_server_method());
129         else if (ver == TLS1_VERSION)
130                 return(TLSv1_server_method());
131         else if (ver == TLS1_1_VERSION)
132                 return(TLSv1_1_server_method());
133         else if (ver == TLS1_2_VERSION)
134                 return(TLSv1_2_server_method());
135         else
136                 return(NULL);
137         }
138
139 IMPLEMENT_ssl23_meth_func(SSLv23_server_method,
140                         ssl23_accept,
141                         ssl_undefined_function,
142                         ssl23_get_server_method)
143
144 int ssl23_accept(SSL *s)
145         {
146         BUF_MEM *buf;
147         void (*cb)(const SSL *ssl,int type,int val)=NULL;
148         int ret= -1;
149         int new_state,state;
150
151         ERR_clear_error();
152         ERR_clear_system_error();
153
154         if (s->info_callback != NULL)
155                 cb=s->info_callback;
156         else if (s->ctx->info_callback != NULL)
157                 cb=s->ctx->info_callback;
158         
159         s->in_handshake++;
160         if (!SSL_in_init(s) || SSL_in_before(s)) SSL_clear(s); 
161
162         for (;;)
163                 {
164                 state=s->state;
165
166                 switch(s->state)
167                         {
168                 case SSL_ST_BEFORE:
169                 case SSL_ST_ACCEPT:
170                 case SSL_ST_BEFORE|SSL_ST_ACCEPT:
171                 case SSL_ST_OK|SSL_ST_ACCEPT:
172
173                         s->server=1;
174                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
175
176                         /* s->version=SSL3_VERSION; */
177                         s->type=SSL_ST_ACCEPT;
178
179                         if (s->init_buf == NULL)
180                                 {
181                                 if ((buf=BUF_MEM_new()) == NULL)
182                                         {
183                                         ret= -1;
184                                         goto end;
185                                         }
186                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
187                                         {
188                                         ret= -1;
189                                         goto end;
190                                         }
191                                 s->init_buf=buf;
192                                 }
193
194                         ssl3_init_finished_mac(s);
195
196                         s->state=SSL23_ST_SR_CLNT_HELLO;
197                         s->ctx->stats.sess_accept++;
198                         s->init_num=0;
199                         break;
200
201                 case SSL23_ST_SR_CLNT_HELLO:
202                         s->shutdown = 0;
203                         ret = ssl23_get_client_hello(s);
204                         if (ret <= 0) goto end;
205                         break;
206
207                 case SSL23_ST_SR_V2_CLNT_HELLO:
208                         ret = ssl23_get_v2_client_hello(s);
209                         if (ret <= 0) goto end;
210                         break;
211
212                 case SSL23_ST_SR_SWITCH_VERSION:
213                         if (!ssl_init_wbio_buffer(s, 1))
214                                 {
215                                 ret = -1;
216                                 goto end;
217                                 }
218
219                         s->state = SSL3_ST_SR_CLNT_HELLO_A;
220                         s->method = ssl23_get_server_method(s->version);
221                         assert(s->method != NULL);
222                         s->handshake_func = s->method->ssl_accept;
223                         s->init_num = 0;
224
225                         /* NULL the callback; SSL_accept will call it instead. */
226                         cb = NULL;
227                         ret = SSL_accept(s);
228                         goto end;
229                         /* break; */
230
231                 default:
232                         OPENSSL_PUT_ERROR(SSL, ssl23_accept, SSL_R_UNKNOWN_STATE);
233                         ret= -1;
234                         goto end;
235                         /* break; */
236                         }
237
238                 if ((cb != NULL) && (s->state != state))
239                         {
240                         new_state=s->state;
241                         s->state=state;
242                         cb(s,SSL_CB_ACCEPT_LOOP,1);
243                         s->state=new_state;
244                         }
245                 }
246 end:
247         s->in_handshake--;
248         if (cb != NULL)
249                 cb(s,SSL_CB_ACCEPT_EXIT,ret);
250         return(ret);
251         }
252
253 /* ssl23_get_mutual_version determines the highest supported version for a
254  * client which reports a highest version of |client_version|. On success, it
255  * returns 1 and sets |*out_version| to the negotiated version. Otherwise, it
256  * returns 0. */
257 static int ssl23_get_mutual_version(SSL *s, int *out_version, uint16_t client_version)
258         {
259         if (client_version >= TLS1_2_VERSION && !(s->options & SSL_OP_NO_TLSv1_2))
260                 {
261                 *out_version = TLS1_2_VERSION;
262                 return 1;
263                 }
264         if (client_version >= TLS1_1_VERSION && !(s->options & SSL_OP_NO_TLSv1_1))
265                 {
266                 *out_version = TLS1_1_VERSION;
267                 return 1;
268                 }
269         if (client_version >= TLS1_VERSION && !(s->options & SSL_OP_NO_TLSv1))
270                 {
271                 *out_version = TLS1_VERSION;
272                 return 1;
273                 }
274         if (client_version >= SSL3_VERSION && !(s->options & SSL_OP_NO_SSLv3))
275                 {
276                 *out_version = SSL3_VERSION;
277                 return 1;
278                 }
279         return 0;
280         }
281
282 static int ssl23_get_client_hello(SSL *s)
283         {
284         uint8_t *p;
285         int n = 0;
286
287         /* Sniff enough of the input to determine ClientHello type and the
288          * client version. */
289         if (!ssl3_setup_buffers(s)) goto err;
290
291         /* Read the initial 11 bytes of the input. This is sufficient to
292          * determine the client version for a ClientHello or a
293          * V2ClientHello.
294          *
295          * ClientHello (assuming client_version is unfragmented):
296          * Byte  Content
297          *  0     type            \
298          *  1-2   version          > record header
299          *  3-4   length          /
300          *  5     msg_type        \
301          *  6-8   length           > Client Hello message
302          *  9-10  client_version  /
303          *
304          * V2ClientHello:
305          * Byte  Content
306          *  0-1   msg_length
307          *  2     msg_type
308          *  3-4   version
309          *  5-6   cipher_spec_length
310          *  7-8   session_id_length
311          *  9-10  challenge_length
312          */
313         n = ssl23_read_bytes(s, 11);
314         if (n <= 0)
315                 return n;
316         assert(n == 11);
317
318         p = s->packet;
319
320         /* Some dedicated error codes for protocol mixups should the application
321          * wish to interpret them differently. (These do not overlap with
322          * ClientHello or V2ClientHello.) */
323         if ((strncmp("GET ", (char *)p, 4) == 0) ||
324                 (strncmp("POST ",(char *)p, 5) == 0) ||
325                 (strncmp("HEAD ",(char *)p, 5) == 0) ||
326                 (strncmp("PUT ", (char *)p, 4) == 0))
327                 {
328                 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTP_REQUEST);
329                 goto err;
330                 }
331         if (strncmp("CONNECT",(char *)p, 7) == 0)
332                 {
333                 OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_HTTPS_PROXY_REQUEST);
334                 goto err;
335                 }
336
337         /* Determine if this is a ClientHello or V2ClientHello. */
338         if ((p[0] & 0x80) && (p[2] == SSL2_MT_CLIENT_HELLO))
339                 {
340                 /* This is a V2ClientHello. Determine the version to
341                  * use. */
342                 uint16_t client_version = (p[3] << 8) | p[4];
343                 if (!ssl23_get_mutual_version(s, &s->version, client_version))
344                         {
345                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
346                         goto err;
347                         }
348                 /* Parse the entire V2ClientHello. */
349                 s->state = SSL23_ST_SR_V2_CLNT_HELLO;
350                 }
351         else if ((p[0] == SSL3_RT_HANDSHAKE) &&
352                  (p[1] >= SSL3_VERSION_MAJOR) &&
353                  (p[5] == SSL3_MT_CLIENT_HELLO))
354                 {
355                 /* This is a fragment of a ClientHello. We look at the
356                  * client_hello to negotiate the version. However, this
357                  * is difficult if we have only a pathologically small
358                  * fragment. No known client fragments ClientHello like
359                  * this, so we simply reject such connections to avoid
360                  * protocol version downgrade attacks. */
361                 uint16_t record_length = (p[3] << 8) | p[4];
362                 uint16_t client_version;
363                 if (record_length < 6)
364                         {
365                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_RECORD_TOO_SMALL);
366                         goto err;
367                         }
368
369                 client_version = (p[9] << 8) | p[10];
370                 if (!ssl23_get_mutual_version(s, &s->version, client_version))
371                         {
372                         OPENSSL_PUT_ERROR(SSL, ssl23_get_client_hello, SSL_R_UNSUPPORTED_PROTOCOL);
373                         goto err;
374                         }
375
376                 /* Reset the record-layer state for SSL3. */
377                 assert(s->rstate == SSL_ST_READ_HEADER);
378                 s->s3->rbuf.left = s->packet_length;
379                 s->s3->rbuf.offset = 0;
380                 s->packet_length = 0;
381
382                 /* Ready to switch versions. */
383                 s->state = SSL23_ST_SR_SWITCH_VERSION;
384                 }
385
386         return 1;
387 err:
388         return -1;
389         }
390
391 static int ssl23_get_v2_client_hello(SSL *s)
392         {
393         uint8_t *p;
394         size_t i;
395         int n = 0;
396
397         CBS v2_client_hello, cipher_specs, session_id, challenge;
398         size_t msg_length, len;
399         uint8_t msg_type;
400         uint16_t version, cipher_spec_length, session_id_length, challenge_length;
401         CBB client_hello, hello_body, cipher_suites;
402         uint8_t random[SSL3_RANDOM_SIZE];
403
404         /* Read the remainder of the V2ClientHello. We have previously read 11
405          * bytes in ssl23_get_client_hello. */
406         p = s->packet;
407         msg_length = ((p[0] & 0x7f) << 8) | p[1];
408         if (msg_length > (1024 * 4))
409                 {
410                 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_TOO_LARGE);
411                 goto err;
412                 }
413         if (msg_length < 11 - 2)
414                 {
415                 /* Reject lengths that are too short early. We have already read
416                  * 11 bytes, so we should not attempt to process an (invalid)
417                  * V2ClientHello which would be shorter than that. */
418                 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_RECORD_LENGTH_MISMATCH);
419                 goto err;
420                 }
421         n = ssl23_read_bytes(s, msg_length + 2);
422         if (n <= 0)
423                 return n;
424         assert(n == s->packet_length);
425
426         /* The V2ClientHello without the length is incorporated into the
427          * Finished hash. */
428         ssl3_finish_mac(s, s->packet + 2, s->packet_length - 2);
429         if (s->msg_callback)
430                 s->msg_callback(0, SSL2_VERSION, 0, s->packet+2, s->packet_length-2, s, s->msg_callback_arg); /* CLIENT-HELLO */
431
432         CBS_init(&v2_client_hello, s->packet + 2, s->packet_length - 2);
433         if (!CBS_get_u8(&v2_client_hello, &msg_type) ||
434                 !CBS_get_u16(&v2_client_hello, &version) ||
435                 !CBS_get_u16(&v2_client_hello, &cipher_spec_length) ||
436                 !CBS_get_u16(&v2_client_hello, &session_id_length) ||
437                 !CBS_get_u16(&v2_client_hello, &challenge_length) ||
438                 !CBS_get_bytes(&v2_client_hello, &cipher_specs, cipher_spec_length) ||
439                 !CBS_get_bytes(&v2_client_hello, &session_id, session_id_length) ||
440                 !CBS_get_bytes(&v2_client_hello, &challenge, challenge_length) ||
441                 CBS_len(&v2_client_hello) != 0)
442                 {
443                 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR);
444                 goto err;
445                 }
446
447         /* msg_type has already been checked. */
448         assert(msg_type == SSL2_MT_CLIENT_HELLO);
449
450         /* The client_random is the V2ClientHello challenge. Truncate or
451          * left-pad with zeros as needed. */
452         memset(random, 0, SSL3_RANDOM_SIZE);
453         i = (CBS_len(&challenge) > SSL3_RANDOM_SIZE) ? SSL3_RANDOM_SIZE : CBS_len(&challenge);
454         memcpy(random, CBS_data(&challenge), i);
455
456         /* Write out an equivalent SSLv3 ClientHello. */
457         if (!CBB_init_fixed(&client_hello, (uint8_t *)s->init_buf->data, s->init_buf->max))
458                 {
459                 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_MALLOC_FAILURE);
460                 goto err;
461                 }
462         if (!CBB_add_u8(&client_hello, SSL3_MT_CLIENT_HELLO) ||
463                 !CBB_add_u24_length_prefixed(&client_hello, &hello_body) ||
464                 !CBB_add_u16(&hello_body, version) ||
465                 !CBB_add_bytes(&hello_body, random, SSL3_RANDOM_SIZE) ||
466                 /* No session id. */
467                 !CBB_add_u8(&hello_body, 0) ||
468                 !CBB_add_u16_length_prefixed(&hello_body, &cipher_suites))
469                 {
470                 CBB_cleanup(&client_hello);
471                 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
472                 goto err;
473                 }
474
475         /* Copy the cipher suites. */
476         while (CBS_len(&cipher_specs) > 0)
477                 {
478                 uint32_t cipher_spec;
479                 if (!CBS_get_u24(&cipher_specs, &cipher_spec))
480                         {
481                         CBB_cleanup(&client_hello);
482                         OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, SSL_R_DECODE_ERROR);
483                         goto err;
484                         }
485
486                 /* Skip SSLv2 ciphers. */
487                 if ((cipher_spec & 0xff0000) != 0)
488                         continue;
489                 if (!CBB_add_u16(&cipher_suites, cipher_spec))
490                         {
491                         CBB_cleanup(&client_hello);
492                         OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
493                         goto err;
494                         }
495                 }
496
497         /* Add the null compression scheme and finish. */
498         if (!CBB_add_u8(&hello_body, 1) ||
499                 !CBB_add_u8(&hello_body, 0) ||
500                 !CBB_finish(&client_hello, NULL, &len))
501                 {
502                 CBB_cleanup(&client_hello);
503                 OPENSSL_PUT_ERROR(SSL, ssl23_get_v2_client_hello, ERR_R_INTERNAL_ERROR);
504                 goto err;
505                 }
506
507         /* Mark the message for "re"-use by the version-specific
508          * method. */
509         s->s3->tmp.reuse_message = 1;
510         s->s3->tmp.message_type = SSL3_MT_CLIENT_HELLO;
511         /* The handshake message header is 4 bytes. */
512         s->s3->tmp.message_size = len - 4;
513
514         /* Reset the record layer for SSL3. */
515         assert(s->rstate == SSL_ST_READ_HEADER);
516         s->packet_length = 0;
517         s->s3->rbuf.left = 0;
518         s->s3->rbuf.offset = 0;
519
520         s->state = SSL23_ST_SR_SWITCH_VERSION;
521         return 1;
522 err:
523         return -1;
524         }