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