Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / bio / connect.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 #include <openssl/bio.h>
58
59 #include <assert.h>
60 #include <errno.h>
61 #include <stdio.h>
62
63 #if !defined(OPENSSL_WINDOWS)
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 #include <unistd.h>
68 #endif
69
70 #include <openssl/buf.h>
71 #include <openssl/err.h>
72 #include <openssl/mem.h>
73
74 #include "internal.h"
75
76
77 enum {
78   BIO_CONN_S_BEFORE,
79   BIO_CONN_S_BLOCKED_CONNECT,
80   BIO_CONN_S_OK,
81 };
82
83 typedef struct bio_connect_st {
84   int state;
85
86   char *param_hostname;
87   char *param_port;
88   int nbio;
89
90   uint8_t ip[4];
91   unsigned short port;
92
93   struct sockaddr_storage them;
94   socklen_t them_length;
95
96   /* the file descriptor is kept in bio->num in order to match the socket
97    * BIO. */
98
99   /* info_callback is called when the connection is initially made
100    * callback(BIO,state,ret);  The callback should return 'ret', state is for
101    * compatibility with the SSL info_callback. */
102   int (*info_callback)(const BIO *bio, int state, int ret);
103 } BIO_CONNECT;
104
105 #if !defined(OPENSSL_WINDOWS)
106 static int closesocket(int sock) {
107   return close(sock);
108 }
109 #endif
110
111 /* maybe_copy_ipv4_address sets |*ipv4| to the IPv4 address from |ss| (in
112  * big-endian order), if |ss| contains an IPv4 socket address. */
113 static void maybe_copy_ipv4_address(uint8_t *ipv4,
114                                     const struct sockaddr_storage *ss) {
115   const struct sockaddr_in *sin;
116
117   if (ss->ss_family != AF_INET) {
118     return;
119   }
120
121   sin = (const struct sockaddr_in*) ss;
122   memcpy(ipv4, &sin->sin_addr, 4);
123 }
124
125 static int conn_state(BIO *bio, BIO_CONNECT *c) {
126   int ret = -1, i;
127   char *p, *q;
128   int (*cb)(const BIO *, int, int) = NULL;
129
130   if (c->info_callback != NULL) {
131     cb = c->info_callback;
132   }
133
134   for (;;) {
135     switch (c->state) {
136       case BIO_CONN_S_BEFORE:
137         p = c->param_hostname;
138         if (p == NULL) {
139           OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_HOSTNAME_SPECIFIED);
140           goto exit_loop;
141         }
142         for (; *p != 0; p++) {
143           if (*p == ':' || *p == '/') {
144             break;
145           }
146         }
147
148         i = *p;
149         if (i == ':' || i == '/') {
150           *(p++) = 0;
151           if (i == ':') {
152             for (q = p; *q; q++) {
153               if (*q == '/') {
154                 *q = 0;
155                 break;
156               }
157             }
158             if (c->param_port != NULL) {
159               OPENSSL_free(c->param_port);
160             }
161             c->param_port = BUF_strdup(p);
162           }
163         }
164
165         if (c->param_port == NULL) {
166           OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NO_PORT_SPECIFIED);
167           ERR_add_error_data(2, "host=", c->param_hostname);
168           goto exit_loop;
169         }
170
171         if (!bio_ip_and_port_to_socket_and_addr(
172                 &bio->num, &c->them, &c->them_length, c->param_hostname,
173                 c->param_port)) {
174           OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_UNABLE_TO_CREATE_SOCKET);
175           ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
176           goto exit_loop;
177         }
178
179         memset(c->ip, 0, 4);
180         maybe_copy_ipv4_address(c->ip, &c->them);
181
182         if (c->nbio) {
183           if (!bio_socket_nbio(bio->num, 1)) {
184             OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_ERROR_SETTING_NBIO);
185             ERR_add_error_data(4, "host=", c->param_hostname, ":",
186                                c->param_port);
187             goto exit_loop;
188           }
189         }
190
191         i = 1;
192         ret = setsockopt(bio->num, SOL_SOCKET, SO_KEEPALIVE, (char *)&i,
193                          sizeof(i));
194         if (ret < 0) {
195           OPENSSL_PUT_SYSTEM_ERROR(setsockopt);
196           OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_KEEPALIVE);
197           ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
198           goto exit_loop;
199         }
200
201         BIO_clear_retry_flags(bio);
202         ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length);
203         if (ret < 0) {
204           if (bio_fd_should_retry(ret)) {
205             BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
206             c->state = BIO_CONN_S_BLOCKED_CONNECT;
207             bio->retry_reason = BIO_RR_CONNECT;
208           } else {
209             OPENSSL_PUT_SYSTEM_ERROR(connect);
210             OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_CONNECT_ERROR);
211             ERR_add_error_data(4, "host=", c->param_hostname, ":",
212                                c->param_port);
213           }
214           goto exit_loop;
215         } else {
216           c->state = BIO_CONN_S_OK;
217         }
218         break;
219
220       case BIO_CONN_S_BLOCKED_CONNECT:
221         i = bio_sock_error(bio->num);
222         if (i) {
223           if (bio_fd_should_retry(ret)) {
224             BIO_set_flags(bio, (BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY));
225             c->state = BIO_CONN_S_BLOCKED_CONNECT;
226             bio->retry_reason = BIO_RR_CONNECT;
227             ret = -1;
228           } else {
229             BIO_clear_retry_flags(bio);
230             OPENSSL_PUT_SYSTEM_ERROR(connect);
231             OPENSSL_PUT_ERROR(BIO, conn_state, BIO_R_NBIO_CONNECT_ERROR);
232             ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port);
233             ret = 0;
234           }
235           goto exit_loop;
236         } else {
237           c->state = BIO_CONN_S_OK;
238         }
239         break;
240
241       case BIO_CONN_S_OK:
242         ret = 1;
243         goto exit_loop;
244       default:
245         assert(0);
246         goto exit_loop;
247     }
248
249     if (cb != NULL) {
250       ret = cb((BIO *)bio, c->state, ret);
251       if (ret == 0) {
252         goto end;
253       }
254     }
255   }
256
257 exit_loop:
258   if (cb != NULL) {
259     ret = cb((BIO *)bio, c->state, ret);
260   }
261
262 end:
263   return ret;
264 }
265
266 static BIO_CONNECT *BIO_CONNECT_new(void) {
267   BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT));
268
269   if (ret == NULL) {
270     return NULL;
271   }
272   memset(ret, 0, sizeof(BIO_CONNECT));
273
274   ret->state = BIO_CONN_S_BEFORE;
275   return ret;
276 }
277
278 static void BIO_CONNECT_free(BIO_CONNECT *c) {
279   if (c == NULL) {
280     return;
281   }
282
283   if (c->param_hostname != NULL) {
284     OPENSSL_free(c->param_hostname);
285   }
286   if (c->param_port != NULL) {
287     OPENSSL_free(c->param_port);
288   }
289   OPENSSL_free(c);
290 }
291
292 static int conn_new(BIO *bio) {
293   bio->init = 0;
294   bio->num = -1;
295   bio->flags = 0;
296   bio->ptr = (char *)BIO_CONNECT_new();
297   return bio->ptr != NULL;
298 }
299
300 static void conn_close_socket(BIO *bio) {
301   BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr;
302
303   if (bio->num == -1) {
304     return;
305   }
306
307   /* Only do a shutdown if things were established */
308   if (c->state == BIO_CONN_S_OK) {
309     shutdown(bio->num, 2);
310   }
311   closesocket(bio->num);
312   bio->num = -1;
313 }
314
315 static int conn_free(BIO *bio) {
316   if (bio == NULL) {
317     return 0;
318   }
319
320   if (bio->shutdown) {
321     conn_close_socket(bio);
322   }
323
324   BIO_CONNECT_free((BIO_CONNECT*) bio->ptr);
325
326   return 1;
327 }
328
329 static int conn_read(BIO *bio, char *out, int out_len) {
330   int ret = 0;
331   BIO_CONNECT *data;
332
333   data = (BIO_CONNECT *)bio->ptr;
334   if (data->state != BIO_CONN_S_OK) {
335     ret = conn_state(bio, data);
336     if (ret <= 0) {
337       return ret;
338     }
339   }
340
341   bio_clear_socket_error();
342   ret = recv(bio->num, out, out_len, 0);
343   BIO_clear_retry_flags(bio);
344   if (ret <= 0) {
345     if (bio_fd_should_retry(ret)) {
346       BIO_set_retry_read(bio);
347     }
348   }
349
350   return ret;
351 }
352
353 static int conn_write(BIO *bio, const char *in, int in_len) {
354   int ret;
355   BIO_CONNECT *data;
356
357   data = (BIO_CONNECT *)bio->ptr;
358   if (data->state != BIO_CONN_S_OK) {
359     ret = conn_state(bio, data);
360     if (ret <= 0) {
361       return ret;
362     }
363   }
364
365   bio_clear_socket_error();
366   ret = send(bio->num, in, in_len, 0);
367   BIO_clear_retry_flags(bio);
368   if (ret <= 0) {
369     if (bio_fd_should_retry(ret)) {
370       BIO_set_retry_write(bio);
371     }
372   }
373
374   return ret;
375 }
376
377 static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) {
378   int *ip;
379   const char **pptr;
380   long ret = 1;
381   BIO_CONNECT *data;
382
383   data = (BIO_CONNECT *)bio->ptr;
384
385   switch (cmd) {
386     case BIO_CTRL_RESET:
387       ret = 0;
388       data->state = BIO_CONN_S_BEFORE;
389       conn_close_socket(bio);
390       bio->flags = 0;
391       break;
392     case BIO_C_DO_STATE_MACHINE:
393       /* use this one to start the connection */
394       if (data->state != BIO_CONN_S_OK)
395         ret = (long)conn_state(bio, data);
396       else
397         ret = 1;
398       break;
399     case BIO_C_GET_CONNECT:
400       /* TODO(fork): can this be removed? (Or maybe this whole file). */
401       if (ptr != NULL) {
402         pptr = (const char **)ptr;
403         if (num == 0) {
404           *pptr = data->param_hostname;
405         } else if (num == 1) {
406           *pptr = data->param_port;
407         } else if (num == 2) {
408           *pptr = (char *) &data->ip[0];
409         } else if (num == 3) {
410           *((int *)ptr) = data->port;
411         }
412         if (!bio->init) {
413           *pptr = "not initialized";
414         }
415         ret = 1;
416       }
417       break;
418     case BIO_C_SET_CONNECT:
419       if (ptr != NULL) {
420         bio->init = 1;
421         if (num == 0) {
422           if (data->param_hostname != NULL) {
423             OPENSSL_free(data->param_hostname);
424           }
425           data->param_hostname = BUF_strdup(ptr);
426         } else if (num == 1) {
427           if (data->param_port != NULL) {
428             OPENSSL_free(data->param_port);
429           }
430           data->param_port = BUF_strdup(ptr);
431         } else {
432           ret = 0;
433         }
434       }
435       break;
436     case BIO_C_SET_NBIO:
437       data->nbio = (int)num;
438       break;
439     case BIO_C_GET_FD:
440       if (bio->init) {
441         ip = (int *)ptr;
442         if (ip != NULL) {
443           *ip = bio->num;
444         }
445         ret = 1;
446       } else {
447         ret = 0;
448       }
449       break;
450     case BIO_CTRL_GET_CLOSE:
451       ret = bio->shutdown;
452       break;
453     case BIO_CTRL_SET_CLOSE:
454       bio->shutdown = (int)num;
455       break;
456     case BIO_CTRL_PENDING:
457     case BIO_CTRL_WPENDING:
458       ret = 0;
459       break;
460     case BIO_CTRL_FLUSH:
461       break;
462     case BIO_CTRL_SET_CALLBACK: {
463 #if 0 /* FIXME: Should this be used?  -- Richard Levitte */
464                 OPENSSL_PUT_ERROR(BIO, XXX, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
465                 ret = -1;
466 #else
467       ret = 0;
468 #endif
469     } break;
470     case BIO_CTRL_GET_CALLBACK: {
471       int (**fptr)(const BIO *bio, int state, int xret);
472       fptr = (int (**)(const BIO *bio, int state, int xret))ptr;
473       *fptr = data->info_callback;
474     } break;
475     default:
476       ret = 0;
477       break;
478   }
479   return (ret);
480 }
481
482 static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
483   long ret = 1;
484   BIO_CONNECT *data;
485
486   data = (BIO_CONNECT *)bio->ptr;
487
488   switch (cmd) {
489     case BIO_CTRL_SET_CALLBACK: {
490       data->info_callback = (int (*)(const struct bio_st *, int, int))fp;
491     } break;
492     default:
493       ret = 0;
494       break;
495   }
496   return ret;
497 }
498
499 static int conn_puts(BIO *bp, const char *str) {
500   return conn_write(bp, str, strlen(str));
501 }
502
503 BIO *BIO_new_connect(const char *hostname) {
504   BIO *ret;
505
506   ret = BIO_new(BIO_s_connect());
507   if (ret == NULL) {
508     return NULL;
509   }
510   if (!BIO_set_conn_hostname(ret, hostname)) {
511     BIO_free(ret);
512     return NULL;
513   }
514   return ret;
515 }
516
517 static const BIO_METHOD methods_connectp = {
518     BIO_TYPE_CONNECT, "socket connect",         conn_write, conn_read,
519     conn_puts,        NULL /* connect_gets, */, conn_ctrl,  conn_new,
520     conn_free,        conn_callback_ctrl,
521 };
522
523 const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; }
524
525 int BIO_set_conn_hostname(BIO *bio, const char *name) {
526   return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name);
527 }
528
529 int BIO_set_conn_port(BIO *bio, const char *port_str) {
530   return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str);
531 }
532
533 int BIO_set_nbio(BIO *bio, int on) {
534   return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL);
535 }