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