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