Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / ssl / s23_clnt.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/obj.h>
115 #include <openssl/rand.h>
116
117 #include "ssl_locl.h"
118
119 static const SSL_METHOD *ssl23_get_client_method(int ver);
120 static int ssl23_client_hello(SSL *s);
121 static int ssl23_get_server_hello(SSL *s);
122 static const SSL_METHOD *ssl23_get_client_method(int ver)
123         {
124         if (ver == SSL3_VERSION)
125                 return(SSLv3_client_method());
126         else if (ver == TLS1_VERSION)
127                 return(TLSv1_client_method());
128         else if (ver == TLS1_1_VERSION)
129                 return(TLSv1_1_client_method());
130         else if (ver == TLS1_2_VERSION)
131                 return(TLSv1_2_client_method());
132         else
133                 return(NULL);
134         }
135
136 IMPLEMENT_ssl23_meth_func(SSLv23_client_method,
137                         ssl_undefined_function,
138                         ssl23_connect,
139                         ssl23_get_client_method)
140
141 int ssl23_connect(SSL *s)
142         {
143         BUF_MEM *buf=NULL;
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_CONNECT:
167                 case SSL_ST_BEFORE|SSL_ST_CONNECT:
168                 case SSL_ST_OK|SSL_ST_CONNECT:
169
170                         if (s->session != NULL)
171                                 {
172                                 OPENSSL_PUT_ERROR(SSL, ssl23_connect, SSL_R_SSL23_DOING_SESSION_ID_REUSE);
173                                 ret= -1;
174                                 goto end;
175                                 }
176                         s->server=0;
177                         if (cb != NULL) cb(s,SSL_CB_HANDSHAKE_START,1);
178
179                         /* s->version=TLS1_VERSION; */
180                         s->type=SSL_ST_CONNECT;
181
182                         if (s->init_buf == NULL)
183                                 {
184                                 if ((buf=BUF_MEM_new()) == NULL)
185                                         {
186                                         ret= -1;
187                                         goto end;
188                                         }
189                                 if (!BUF_MEM_grow(buf,SSL3_RT_MAX_PLAIN_LENGTH))
190                                         {
191                                         ret= -1;
192                                         goto end;
193                                         }
194                                 s->init_buf=buf;
195                                 buf=NULL;
196                                 }
197
198                         if (!ssl3_setup_buffers(s)) { ret= -1; goto end; }
199
200                         ssl3_init_finished_mac(s);
201
202                         s->state=SSL23_ST_CW_CLNT_HELLO_A;
203                         s->ctx->stats.sess_connect++;
204                         s->init_num=0;
205                         break;
206
207                 case SSL23_ST_CW_CLNT_HELLO_A:
208                 case SSL23_ST_CW_CLNT_HELLO_B:
209
210                         s->shutdown=0;
211                         ret=ssl23_client_hello(s);
212                         if (ret <= 0) goto end;
213                         s->state=SSL23_ST_CR_SRVR_HELLO_A;
214                         s->init_num=0;
215
216                         break;
217
218                 case SSL23_ST_CR_SRVR_HELLO_A:
219                 case SSL23_ST_CR_SRVR_HELLO_B:
220                         ret=ssl23_get_server_hello(s);
221                         if (ret >= 0) cb=NULL;
222                         goto end;
223                         /* break; */
224
225                 default:
226                         OPENSSL_PUT_ERROR(SSL, ssl23_connect, SSL_R_UNKNOWN_STATE);
227                         ret= -1;
228                         goto end;
229                         /* break; */
230                         }
231
232                 if (s->debug) { (void)BIO_flush(s->wbio); }
233
234                 if ((cb != NULL) && (s->state != state))
235                         {
236                         new_state=s->state;
237                         s->state=state;
238                         cb(s,SSL_CB_CONNECT_LOOP,1);
239                         s->state=new_state;
240                         }
241                 }
242 end:
243         s->in_handshake--;
244         if (buf != NULL)
245                 BUF_MEM_free(buf);
246         if (cb != NULL)
247                 cb(s,SSL_CB_CONNECT_EXIT,ret);
248         return(ret);
249         }
250
251 /* Fill a ClientRandom or ServerRandom field of length len. Returns <= 0
252  * on failure, 1 on success. */
253 int ssl_fill_hello_random(SSL *s, int server, unsigned char *result, int len)
254         {
255                 int send_time = 0;
256                 if (len < 4)
257                         return 0;
258                 if (server)
259                         send_time = (s->mode & SSL_MODE_SEND_SERVERHELLO_TIME) != 0;
260                 else
261                         send_time = (s->mode & SSL_MODE_SEND_CLIENTHELLO_TIME) != 0;
262                 if (send_time)
263                         {
264                         unsigned long Time = (unsigned long)time(NULL);
265                         unsigned char *p = result;
266                         l2n(Time, p);
267                         return RAND_pseudo_bytes(p, len-4);
268                         }
269                 else
270                         return RAND_pseudo_bytes(result, len);
271         }
272
273 static int ssl23_client_hello(SSL *s)
274         {
275         unsigned char *buf;
276         unsigned char *p,*d;
277         int i;
278         unsigned long l;
279         int version = 0, version_major, version_minor;
280         int ret;
281         unsigned long mask, options = s->options;
282
283         /*
284          * SSL_OP_NO_X disables all protocols above X *if* there are
285          * some protocols below X enabled. This is required in order
286          * to maintain "version capability" vector contiguous. So
287          * that if application wants to disable TLS1.0 in favour of
288          * TLS1>=1, it would be insufficient to pass SSL_NO_TLSv1, the
289          * answer is SSL_OP_NO_TLSv1|SSL_OP_NO_SSLv3|SSL_OP_NO_SSLv2.
290          */
291         mask =  SSL_OP_NO_TLSv1_1|SSL_OP_NO_TLSv1
292 #if !defined(OPENSSL_NO_SSL3)
293                 |SSL_OP_NO_SSLv3
294 #endif
295                 ;
296 #if !defined(OPENSSL_NO_TLS1_2_CLIENT)
297         version = TLS1_2_VERSION;
298
299         if ((options & SSL_OP_NO_TLSv1_2) && (options & mask) != mask)
300                 version = TLS1_1_VERSION;
301 #else
302         version = TLS1_1_VERSION;
303 #endif
304         mask &= ~SSL_OP_NO_TLSv1_1;
305         if ((options & SSL_OP_NO_TLSv1_1) && (options & mask) != mask)
306                 version = TLS1_VERSION;
307         mask &= ~SSL_OP_NO_TLSv1;
308 #if !defined(OPENSSL_NO_SSL3)
309         if ((options & SSL_OP_NO_TLSv1) && (options & mask) != mask)
310                 version = SSL3_VERSION;
311         mask &= ~SSL_OP_NO_SSLv3;
312 #endif
313
314         buf=(unsigned char *)s->init_buf->data;
315         if (s->state == SSL23_ST_CW_CLNT_HELLO_A)
316                 {
317 #if 0
318                 /* don't reuse session-id's */
319                 if (!ssl_get_new_session(s,0))
320                         {
321                         return(-1);
322                         }
323 #endif
324
325                 p=s->s3->client_random;
326                 if (ssl_fill_hello_random(s, 0, p, SSL3_RANDOM_SIZE) <= 0)
327                         return -1;
328
329                 if (version == TLS1_2_VERSION)
330                         {
331                         version_major = TLS1_2_VERSION_MAJOR;
332                         version_minor = TLS1_2_VERSION_MINOR;
333                         }
334                 else if (tls1_suiteb(s))
335                         {
336                         OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_ONLY_TLS_1_2_ALLOWED_IN_SUITEB_MODE);
337                         return -1;
338                         }
339                 else if (version == TLS1_1_VERSION)
340                         {
341                         version_major = TLS1_1_VERSION_MAJOR;
342                         version_minor = TLS1_1_VERSION_MINOR;
343                         }
344                 else if (version == TLS1_VERSION)
345                         {
346                         version_major = TLS1_VERSION_MAJOR;
347                         version_minor = TLS1_VERSION_MINOR;
348                         }
349                 else if (version == SSL3_VERSION)
350                         {
351                         version_major = SSL3_VERSION_MAJOR;
352                         version_minor = SSL3_VERSION_MINOR;
353                         }
354                 else if (version == SSL2_VERSION)
355                         {
356                         version_major = SSL2_VERSION_MAJOR;
357                         version_minor = SSL2_VERSION_MINOR;
358                         }
359                 else
360                         {
361                         OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_NO_PROTOCOLS_AVAILABLE);
362                         return(-1);
363                         }
364
365                 s->client_version = version;
366
367                 /* create Client Hello in SSL 3.0/TLS 1.0 format */
368
369                 /* do the record header (5 bytes) and handshake message
370                  * header (4 bytes) last. Note: the final argument to
371                  * ssl_add_clienthello_tlsext below depends on the size
372                  * of this prefix. */
373                 d = p = &(buf[9]);
374                         
375                 *(p++) = version_major;
376                 *(p++) = version_minor;
377
378                 /* Random stuff */
379                 memcpy(p, s->s3->client_random, SSL3_RANDOM_SIZE);
380                 p += SSL3_RANDOM_SIZE;
381
382                 /* Session ID (zero since there is no reuse) */
383                 *(p++) = 0;
384
385                 /* Ciphers supported (using SSL 3.0/TLS 1.0 format) */
386                 i = ssl_cipher_list_to_bytes(s, SSL_get_ciphers(s), &p[2]);
387                 if (i == 0)
388                         {
389                         OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_NO_CIPHERS_AVAILABLE);
390                         return -1;
391                         }
392                 s2n(i,p);
393                 p+=i;
394
395                 /* COMPRESSION */
396                 *(p++)=1;
397                 *(p++)=0; /* Add the NULL method */
398
399                 /* TLS extensions*/
400                 if (ssl_prepare_clienthello_tlsext(s) <= 0)
401                         {
402                         OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, SSL_R_CLIENTHELLO_TLSEXT);
403                         return -1;
404                         }
405
406                 /* The buffer includes the 5 byte record header, so
407                  * subtract it to compute hlen for
408                  * ssl_add_clienthello_tlsext. */
409                 if ((p = ssl_add_clienthello_tlsext(s, p, buf+SSL3_RT_MAX_PLAIN_LENGTH, p-buf-5)) == NULL)
410                         {
411                         OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR);
412                         return -1;
413                         }
414                         
415                 l = p-d;
416
417                 /* fill in 4-byte handshake header */
418                 d=&(buf[5]);
419                 *(d++)=SSL3_MT_CLIENT_HELLO;
420                 l2n3(l,d);
421
422                 l += 4;
423
424                 if (l > SSL3_RT_MAX_PLAIN_LENGTH)
425                         {
426                         OPENSSL_PUT_ERROR(SSL, ssl23_client_hello, ERR_R_INTERNAL_ERROR);
427                         return -1;
428                         }
429
430                 /* fill in 5-byte record header */
431                 d=buf;
432                 *(d++) = SSL3_RT_HANDSHAKE;
433                 *(d++) = version_major;
434                 /* Some servers hang if we use long client hellos
435                  * and a record number > TLS 1.0.
436                  */
437                 if (TLS1_get_client_version(s) > TLS1_VERSION)
438                         *(d++) = 1;
439                 else
440                         *(d++) = version_minor;
441                 s2n((int)l,d);
442
443                 /* number of bytes to write */
444                 s->init_num=p-buf;
445                 s->init_off=0;
446
447                 ssl3_finish_mac(s,&(buf[5]), s->init_num - 5);
448
449                 s->state=SSL23_ST_CW_CLNT_HELLO_B;
450                 s->init_off=0;
451                 }
452
453         /* SSL3_ST_CW_CLNT_HELLO_B */
454         ret = ssl23_write_bytes(s);
455
456         if ((ret >= 2) && s->msg_callback)
457                 {
458                 /* Client Hello has been sent; tell msg_callback */
459
460                 s->msg_callback(1, version, SSL3_RT_HEADER, s->init_buf->data, 5, s, s->msg_callback_arg);
461                 s->msg_callback(1, version, SSL3_RT_HANDSHAKE, s->init_buf->data+5, ret-5, s, s->msg_callback_arg);
462                 }
463
464         return ret;
465         }
466
467 static int ssl23_get_server_hello(SSL *s)
468         {
469         char buf[8];
470         unsigned char *p;
471         int i;
472         int n;
473
474         n=ssl23_read_bytes(s,7);
475
476         if (n != 7) return(n);
477         p=s->packet;
478
479         memcpy(buf,p,n);
480
481         if ((p[0] & 0x80) && (p[2] == SSL2_MT_SERVER_HELLO) &&
482                 (p[5] == 0x00) && (p[6] == 0x02))
483                 {
484                 OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
485                 goto err;
486                 }
487         else if (p[1] == SSL3_VERSION_MAJOR &&
488                  p[2] <= TLS1_2_VERSION_MINOR &&
489                  ((p[0] == SSL3_RT_HANDSHAKE && p[5] == SSL3_MT_SERVER_HELLO) ||
490                   (p[0] == SSL3_RT_ALERT && p[3] == 0 && p[4] == 2)))
491                 {
492                 /* we have sslv3 or tls1 (server hello or alert) */
493
494                 if ((p[2] == SSL3_VERSION_MINOR) &&
495                         !(s->options & SSL_OP_NO_SSLv3))
496                         {
497                         s->version=SSL3_VERSION;
498                         s->method=SSLv3_client_method();
499                         }
500                 else if ((p[2] == TLS1_VERSION_MINOR) &&
501                         !(s->options & SSL_OP_NO_TLSv1))
502                         {
503                         s->version=TLS1_VERSION;
504                         s->method=TLSv1_client_method();
505                         }
506                 else if ((p[2] == TLS1_1_VERSION_MINOR) &&
507                         !(s->options & SSL_OP_NO_TLSv1_1))
508                         {
509                         s->version=TLS1_1_VERSION;
510                         s->method=TLSv1_1_client_method();
511                         }
512                 else if ((p[2] == TLS1_2_VERSION_MINOR) &&
513                         !(s->options & SSL_OP_NO_TLSv1_2))
514                         {
515                         s->version=TLS1_2_VERSION;
516                         s->method=TLSv1_2_client_method();
517                         }
518                 else
519                         {
520                         OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNSUPPORTED_PROTOCOL);
521                         goto err;
522                         }
523
524                 if (p[0] == SSL3_RT_ALERT && p[5] != SSL3_AL_WARNING)
525                         {
526                         /* fatal alert */
527
528                         void (*cb)(const SSL *ssl,int type,int val)=NULL;
529                         int j;
530
531                         if (s->info_callback != NULL)
532                                 cb=s->info_callback;
533                         else if (s->ctx->info_callback != NULL)
534                                 cb=s->ctx->info_callback;
535  
536                         i=p[5];
537                         if (cb != NULL)
538                                 {
539                                 j=(i<<8)|p[6];
540                                 cb(s,SSL_CB_READ_ALERT,j);
541                                 }
542                         
543                         if (s->msg_callback)
544                                 {
545                                 s->msg_callback(0, s->version, SSL3_RT_HEADER, p, 5, s, s->msg_callback_arg);
546                                 s->msg_callback(0, s->version, SSL3_RT_ALERT, p+5, 2, s, s->msg_callback_arg);
547                                 }
548
549                         s->rwstate=SSL_NOTHING;
550                         OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_AD_REASON_OFFSET + p[6]);
551                         goto err;
552                         }
553
554                 if (!ssl_init_wbio_buffer(s,1)) goto err;
555
556                 /* we are in this state */
557                 s->state=SSL3_ST_CR_SRVR_HELLO_A;
558
559                 /* put the 7 bytes we have read into the input buffer
560                  * for SSLv3 */
561                 s->rstate=SSL_ST_READ_HEADER;
562                 s->packet_length=n;
563                 if (s->s3->rbuf.buf == NULL)
564                         if (!ssl3_setup_read_buffer(s))
565                                 goto err;
566                 s->packet= &(s->s3->rbuf.buf[0]);
567                 memcpy(s->packet,buf,n);
568                 s->s3->rbuf.left=n;
569                 s->s3->rbuf.offset=0;
570
571                 s->handshake_func=s->method->ssl_connect;
572                 }
573         else
574                 {
575                 OPENSSL_PUT_ERROR(SSL, ssl23_get_server_hello, SSL_R_UNKNOWN_PROTOCOL);
576                 goto err;
577                 }
578         s->init_num=0;
579
580         /* Since, if we are sending a ssl23 client hello, we are not
581          * reusing a session-id */
582         if (!ssl_get_new_session(s,0))
583                 goto err;
584
585         return(SSL_connect(s));
586 err:
587         return(-1);
588         }