Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 61 | #include <string.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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> |
Brian Smith | dc94b54 | 2015-01-27 23:06:00 -0800 | [diff] [blame] | 68 | #else |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 69 | OPENSSL_MSVC_PRAGMA(warning(push, 3)) |
Adam Langley | 3e71931 | 2015-03-20 16:32:23 -0700 | [diff] [blame] | 70 | #include <winsock2.h> |
| 71 | #include <ws2tcpip.h> |
David Benjamin | a353cdb | 2016-06-09 16:48:33 -0400 | [diff] [blame] | 72 | OPENSSL_MSVC_PRAGMA(warning(pop)) |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | #include <openssl/buf.h> |
| 76 | #include <openssl/err.h> |
| 77 | #include <openssl/mem.h> |
| 78 | |
| 79 | #include "internal.h" |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 80 | #include "../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 81 | |
| 82 | |
| 83 | enum { |
| 84 | BIO_CONN_S_BEFORE, |
| 85 | BIO_CONN_S_BLOCKED_CONNECT, |
| 86 | BIO_CONN_S_OK, |
| 87 | }; |
| 88 | |
| 89 | typedef struct bio_connect_st { |
| 90 | int state; |
| 91 | |
| 92 | char *param_hostname; |
| 93 | char *param_port; |
| 94 | int nbio; |
| 95 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 96 | unsigned short port; |
| 97 | |
| 98 | struct sockaddr_storage them; |
David Benjamin | e77dff6 | 2014-07-17 17:23:22 -0400 | [diff] [blame] | 99 | socklen_t them_length; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 100 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 101 | // the file descriptor is kept in bio->num in order to match the socket |
| 102 | // BIO. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 103 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 104 | // info_callback is called when the connection is initially made |
| 105 | // callback(BIO,state,ret); The callback should return 'ret', state is for |
| 106 | // compatibility with the SSL info_callback. |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 107 | int (*info_callback)(const BIO *bio, int state, int ret); |
| 108 | } BIO_CONNECT; |
| 109 | |
David Benjamin | 04284b8 | 2014-09-30 20:51:57 -0400 | [diff] [blame] | 110 | #if !defined(OPENSSL_WINDOWS) |
| 111 | static int closesocket(int sock) { |
| 112 | return close(sock); |
| 113 | } |
| 114 | #endif |
| 115 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 116 | // split_host_and_port sets |*out_host| and |*out_port| to the host and port |
| 117 | // parsed from |name|. It returns one on success or zero on error. Even when |
| 118 | // successful, |*out_port| may be NULL on return if no port was specified. |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 119 | static int split_host_and_port(char **out_host, char **out_port, const char *name) { |
| 120 | const char *host, *port = NULL; |
| 121 | size_t host_len = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 123 | *out_host = NULL; |
| 124 | *out_port = NULL; |
| 125 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 126 | if (name[0] == '[') { // bracketed IPv6 address |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 127 | const char *close = strchr(name, ']'); |
| 128 | if (close == NULL) { |
| 129 | return 0; |
| 130 | } |
| 131 | host = name + 1; |
| 132 | host_len = close - host; |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 133 | if (close[1] == ':') { // [IP]:port |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 134 | port = close + 2; |
| 135 | } else if (close[1] != 0) { |
| 136 | return 0; |
| 137 | } |
| 138 | } else { |
| 139 | const char *colon = strchr(name, ':'); |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 140 | if (colon == NULL || strchr(colon + 1, ':') != NULL) { // IPv6 address |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 141 | host = name; |
| 142 | host_len = strlen(name); |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 143 | } else { // host:port |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 144 | host = name; |
| 145 | host_len = colon - name; |
| 146 | port = colon + 1; |
| 147 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 150 | *out_host = BUF_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; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int conn_state(BIO *bio, BIO_CONNECT *c) { |
| 168 | int ret = -1, i; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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: |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 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. |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 181 | |
| 182 | if (c->param_hostname == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 183 | OPENSSL_PUT_ERROR(BIO, BIO_R_NO_HOSTNAME_SPECIFIED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | goto exit_loop; |
| 185 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 186 | |
| 187 | if (c->param_port == NULL) { |
Matt Braithwaite | 29d8adb | 2015-09-28 19:45:54 -0700 | [diff] [blame] | 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; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | if (!bio_ip_and_port_to_socket_and_addr( |
David Benjamin | e77dff6 | 2014-07-17 17:23:22 -0400 | [diff] [blame] | 205 | &bio->num, &c->them, &c->them_length, c->param_hostname, |
| 206 | c->param_port)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 207 | OPENSSL_PUT_ERROR(BIO, BIO_R_UNABLE_TO_CREATE_SOCKET); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 208 | ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); |
| 209 | goto exit_loop; |
| 210 | } |
| 211 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 212 | if (c->nbio) { |
| 213 | if (!bio_socket_nbio(bio->num, 1)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 214 | OPENSSL_PUT_ERROR(BIO, BIO_R_ERROR_SETTING_NBIO); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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) { |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 225 | OPENSSL_PUT_SYSTEM_ERROR(); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 226 | OPENSSL_PUT_ERROR(BIO, BIO_R_KEEPALIVE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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); |
David Benjamin | e77dff6 | 2014-07-17 17:23:22 -0400 | [diff] [blame] | 232 | ret = connect(bio->num, (struct sockaddr*) &c->them, c->them_length); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 233 | if (ret < 0) { |
| 234 | if (bio_fd_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 { |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 239 | OPENSSL_PUT_SYSTEM_ERROR(); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 240 | OPENSSL_PUT_ERROR(BIO, BIO_R_CONNECT_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 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_fd_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); |
David Benjamin | 3fc138e | 2015-10-28 18:03:21 -0400 | [diff] [blame] | 260 | OPENSSL_PUT_SYSTEM_ERROR(); |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 261 | OPENSSL_PUT_ERROR(BIO, BIO_R_NBIO_CONNECT_ERROR); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 262 | ERR_add_error_data(4, "host=", c->param_hostname, ":", c->param_port); |
| 263 | ret = 0; |
| 264 | } |
| 265 | goto exit_loop; |
| 266 | } else { |
| 267 | c->state = BIO_CONN_S_OK; |
| 268 | } |
| 269 | break; |
| 270 | |
| 271 | case BIO_CONN_S_OK: |
| 272 | ret = 1; |
| 273 | goto exit_loop; |
| 274 | default: |
| 275 | assert(0); |
| 276 | goto exit_loop; |
| 277 | } |
| 278 | |
| 279 | if (cb != NULL) { |
| 280 | ret = cb((BIO *)bio, c->state, ret); |
| 281 | if (ret == 0) { |
| 282 | goto end; |
| 283 | } |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | exit_loop: |
| 288 | if (cb != NULL) { |
| 289 | ret = cb((BIO *)bio, c->state, ret); |
| 290 | } |
| 291 | |
| 292 | end: |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | static BIO_CONNECT *BIO_CONNECT_new(void) { |
| 297 | BIO_CONNECT *ret = OPENSSL_malloc(sizeof(BIO_CONNECT)); |
| 298 | |
| 299 | if (ret == NULL) { |
| 300 | return NULL; |
| 301 | } |
David Benjamin | 17cf2cb | 2016-12-13 01:07:13 -0500 | [diff] [blame] | 302 | OPENSSL_memset(ret, 0, sizeof(BIO_CONNECT)); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 303 | |
| 304 | ret->state = BIO_CONN_S_BEFORE; |
| 305 | return ret; |
| 306 | } |
| 307 | |
| 308 | static void BIO_CONNECT_free(BIO_CONNECT *c) { |
| 309 | if (c == NULL) { |
| 310 | return; |
| 311 | } |
| 312 | |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 313 | OPENSSL_free(c->param_hostname); |
| 314 | OPENSSL_free(c->param_port); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 315 | OPENSSL_free(c); |
| 316 | } |
| 317 | |
| 318 | static int conn_new(BIO *bio) { |
| 319 | bio->init = 0; |
| 320 | bio->num = -1; |
| 321 | bio->flags = 0; |
| 322 | bio->ptr = (char *)BIO_CONNECT_new(); |
| 323 | return bio->ptr != NULL; |
| 324 | } |
| 325 | |
| 326 | static void conn_close_socket(BIO *bio) { |
| 327 | BIO_CONNECT *c = (BIO_CONNECT *) bio->ptr; |
| 328 | |
| 329 | if (bio->num == -1) { |
| 330 | return; |
| 331 | } |
| 332 | |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 333 | // Only do a shutdown if things were established |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 334 | if (c->state == BIO_CONN_S_OK) { |
| 335 | shutdown(bio->num, 2); |
| 336 | } |
David Benjamin | 04284b8 | 2014-09-30 20:51:57 -0400 | [diff] [blame] | 337 | closesocket(bio->num); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 338 | bio->num = -1; |
| 339 | } |
| 340 | |
| 341 | static int conn_free(BIO *bio) { |
| 342 | if (bio == NULL) { |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | if (bio->shutdown) { |
| 347 | conn_close_socket(bio); |
| 348 | } |
| 349 | |
| 350 | BIO_CONNECT_free((BIO_CONNECT*) bio->ptr); |
| 351 | |
| 352 | return 1; |
| 353 | } |
| 354 | |
| 355 | static int conn_read(BIO *bio, char *out, int out_len) { |
| 356 | int ret = 0; |
| 357 | BIO_CONNECT *data; |
| 358 | |
| 359 | data = (BIO_CONNECT *)bio->ptr; |
| 360 | if (data->state != BIO_CONN_S_OK) { |
| 361 | ret = conn_state(bio, data); |
| 362 | if (ret <= 0) { |
| 363 | return ret; |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | bio_clear_socket_error(); |
| 368 | ret = recv(bio->num, out, out_len, 0); |
| 369 | BIO_clear_retry_flags(bio); |
| 370 | if (ret <= 0) { |
| 371 | if (bio_fd_should_retry(ret)) { |
| 372 | BIO_set_retry_read(bio); |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | return ret; |
| 377 | } |
| 378 | |
| 379 | static int conn_write(BIO *bio, const char *in, int in_len) { |
| 380 | int ret; |
| 381 | BIO_CONNECT *data; |
| 382 | |
| 383 | data = (BIO_CONNECT *)bio->ptr; |
| 384 | if (data->state != BIO_CONN_S_OK) { |
| 385 | ret = conn_state(bio, data); |
| 386 | if (ret <= 0) { |
| 387 | return ret; |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | bio_clear_socket_error(); |
| 392 | ret = send(bio->num, in, in_len, 0); |
| 393 | BIO_clear_retry_flags(bio); |
| 394 | if (ret <= 0) { |
| 395 | if (bio_fd_should_retry(ret)) { |
| 396 | BIO_set_retry_write(bio); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | return ret; |
| 401 | } |
| 402 | |
| 403 | static long conn_ctrl(BIO *bio, int cmd, long num, void *ptr) { |
| 404 | int *ip; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 405 | long ret = 1; |
| 406 | BIO_CONNECT *data; |
| 407 | |
| 408 | data = (BIO_CONNECT *)bio->ptr; |
| 409 | |
| 410 | switch (cmd) { |
| 411 | case BIO_CTRL_RESET: |
| 412 | ret = 0; |
| 413 | data->state = BIO_CONN_S_BEFORE; |
| 414 | conn_close_socket(bio); |
| 415 | bio->flags = 0; |
| 416 | break; |
| 417 | case BIO_C_DO_STATE_MACHINE: |
David Benjamin | 808f832 | 2017-08-18 14:06:02 -0400 | [diff] [blame] | 418 | // use this one to start the connection |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 419 | if (data->state != BIO_CONN_S_OK) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 420 | ret = (long)conn_state(bio, data); |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 421 | } else { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 422 | ret = 1; |
David Benjamin | c9a202f | 2015-02-11 01:16:26 -0500 | [diff] [blame] | 423 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 424 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 425 | case BIO_C_SET_CONNECT: |
| 426 | if (ptr != NULL) { |
| 427 | bio->init = 1; |
| 428 | if (num == 0) { |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 429 | OPENSSL_free(data->param_hostname); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 430 | data->param_hostname = BUF_strdup(ptr); |
David Benjamin | de95d26 | 2015-04-22 13:44:42 -0400 | [diff] [blame] | 431 | if (data->param_hostname == NULL) { |
| 432 | ret = 0; |
| 433 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 434 | } else if (num == 1) { |
David Benjamin | 22ccc2d | 2015-04-22 13:50:28 -0400 | [diff] [blame] | 435 | OPENSSL_free(data->param_port); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 436 | data->param_port = BUF_strdup(ptr); |
David Benjamin | de95d26 | 2015-04-22 13:44:42 -0400 | [diff] [blame] | 437 | if (data->param_port == NULL) { |
| 438 | ret = 0; |
| 439 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 440 | } else { |
| 441 | ret = 0; |
| 442 | } |
| 443 | } |
| 444 | break; |
| 445 | case BIO_C_SET_NBIO: |
| 446 | data->nbio = (int)num; |
| 447 | break; |
| 448 | case BIO_C_GET_FD: |
| 449 | if (bio->init) { |
| 450 | ip = (int *)ptr; |
| 451 | if (ip != NULL) { |
| 452 | *ip = bio->num; |
| 453 | } |
David Benjamin | 617eac6 | 2015-10-02 14:54:20 -0400 | [diff] [blame] | 454 | ret = bio->num; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 455 | } else { |
David Benjamin | 617eac6 | 2015-10-02 14:54:20 -0400 | [diff] [blame] | 456 | ret = -1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 457 | } |
| 458 | break; |
| 459 | case BIO_CTRL_GET_CLOSE: |
| 460 | ret = bio->shutdown; |
| 461 | break; |
| 462 | case BIO_CTRL_SET_CLOSE: |
| 463 | bio->shutdown = (int)num; |
| 464 | break; |
| 465 | case BIO_CTRL_PENDING: |
| 466 | case BIO_CTRL_WPENDING: |
| 467 | ret = 0; |
| 468 | break; |
| 469 | case BIO_CTRL_FLUSH: |
| 470 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 471 | case BIO_CTRL_GET_CALLBACK: { |
| 472 | int (**fptr)(const BIO *bio, int state, int xret); |
| 473 | fptr = (int (**)(const BIO *bio, int state, int xret))ptr; |
| 474 | *fptr = data->info_callback; |
| 475 | } break; |
| 476 | default: |
| 477 | ret = 0; |
| 478 | break; |
| 479 | } |
David Benjamin | 6342111 | 2017-01-26 21:50:04 -0500 | [diff] [blame] | 480 | return ret; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | static long conn_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) { |
| 484 | long ret = 1; |
| 485 | BIO_CONNECT *data; |
| 486 | |
| 487 | data = (BIO_CONNECT *)bio->ptr; |
| 488 | |
| 489 | switch (cmd) { |
David Benjamin | 6342111 | 2017-01-26 21:50:04 -0500 | [diff] [blame] | 490 | case BIO_CTRL_SET_CALLBACK: |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 491 | data->info_callback = (int (*)(const struct bio_st *, int, int))fp; |
David Benjamin | 6342111 | 2017-01-26 21:50:04 -0500 | [diff] [blame] | 492 | break; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 493 | default: |
| 494 | ret = 0; |
| 495 | break; |
| 496 | } |
| 497 | return ret; |
| 498 | } |
| 499 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 500 | BIO *BIO_new_connect(const char *hostname) { |
| 501 | BIO *ret; |
| 502 | |
| 503 | ret = BIO_new(BIO_s_connect()); |
| 504 | if (ret == NULL) { |
| 505 | return NULL; |
| 506 | } |
| 507 | if (!BIO_set_conn_hostname(ret, hostname)) { |
| 508 | BIO_free(ret); |
| 509 | return NULL; |
| 510 | } |
| 511 | return ret; |
| 512 | } |
| 513 | |
| 514 | static const BIO_METHOD methods_connectp = { |
David Benjamin | 6342111 | 2017-01-26 21:50:04 -0500 | [diff] [blame] | 515 | BIO_TYPE_CONNECT, "socket connect", conn_write, conn_read, |
| 516 | NULL /* puts */, NULL /* gets */, conn_ctrl, conn_new, |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 517 | conn_free, conn_callback_ctrl, |
| 518 | }; |
| 519 | |
| 520 | const BIO_METHOD *BIO_s_connect(void) { return &methods_connectp; } |
| 521 | |
| 522 | int BIO_set_conn_hostname(BIO *bio, const char *name) { |
| 523 | return BIO_ctrl(bio, BIO_C_SET_CONNECT, 0, (void*) name); |
| 524 | } |
| 525 | |
| 526 | int BIO_set_conn_port(BIO *bio, const char *port_str) { |
| 527 | return BIO_ctrl(bio, BIO_C_SET_CONNECT, 1, (void*) port_str); |
| 528 | } |
| 529 | |
David Benjamin | e7b3ce5 | 2016-06-03 16:45:30 -0400 | [diff] [blame] | 530 | int BIO_set_conn_int_port(BIO *bio, const int *port) { |
| 531 | char buf[DECIMAL_SIZE(int) + 1]; |
| 532 | BIO_snprintf(buf, sizeof(buf), "%d", *port); |
| 533 | return BIO_set_conn_port(bio, buf); |
| 534 | } |
| 535 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 536 | int BIO_set_nbio(BIO *bio, int on) { |
| 537 | return BIO_ctrl(bio, BIO_C_SET_NBIO, on, NULL); |
| 538 | } |
David Benjamin | a473e55 | 2016-02-25 12:04:37 -0500 | [diff] [blame] | 539 | |
| 540 | int BIO_do_connect(BIO *bio) { |
| 541 | return BIO_ctrl(bio, BIO_C_DO_STATE_MACHINE, 0, NULL); |
| 542 | } |