Debian Codegraph @

external/boringssl/crypto/bio/connect.c

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