1 /* $OpenBSD: bss_conn.c,v 1.41 2024/04/19 09:54:36 tb Exp $ */ 2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 3 * All rights reserved. 4 * 5 * This package is an SSL implementation written 6 * by Eric Young (eay@cryptsoft.com). 7 * The implementation was written so as to conform with Netscapes SSL. 8 * 9 * This library is free for commercial and non-commercial use as long as 10 * the following conditions are aheared to. The following conditions 11 * apply to all code found in this distribution, be it the RC4, RSA, 12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation 13 * included with this distribution is covered by the same copyright terms 14 * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15 * 16 * Copyright remains Eric Young's, and as such any Copyright notices in 17 * the code are not to be removed. 18 * If this package is used in a product, Eric Young should be given attribution 19 * as the author of the parts of the library used. 20 * This can be in the form of a textual message at program startup or 21 * in documentation (online or textual) provided with the package. 22 * 23 * Redistribution and use in source and binary forms, with or without 24 * modification, are permitted provided that the following conditions 25 * are met: 26 * 1. Redistributions of source code must retain the copyright 27 * notice, this list of conditions and the following disclaimer. 28 * 2. Redistributions in binary form must reproduce the above copyright 29 * notice, this list of conditions and the following disclaimer in the 30 * documentation and/or other materials provided with the distribution. 31 * 3. All advertising materials mentioning features or use of this software 32 * must display the following acknowledgement: 33 * "This product includes cryptographic software written by 34 * Eric Young (eay@cryptsoft.com)" 35 * The word 'cryptographic' can be left out if the rouines from the library 36 * being used are not cryptographic related :-). 37 * 4. If you include any Windows specific code (or a derivative thereof) from 38 * the apps directory (application code) you must include an acknowledgement: 39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40 * 41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * 53 * The licence and distribution terms for any publically available version or 54 * derivative of this code cannot be changed. i.e. this code cannot simply be 55 * copied and put under another distribution licence 56 * [including the GNU Public Licence.] 57 */ 58 59 #include <sys/socket.h> 60 61 #include <netinet/in.h> 62 63 #include <errno.h> 64 #include <netdb.h> 65 #include <stdio.h> 66 #include <string.h> 67 #include <unistd.h> 68 69 #include <openssl/bio.h> 70 #include <openssl/buffer.h> 71 #include <openssl/err.h> 72 73 #include "bio_local.h" 74 75 #define SOCKET_PROTOCOL IPPROTO_TCP 76 77 typedef struct bio_connect_st { 78 int state; 79 80 char *param_hostname; 81 char *param_port; 82 int nbio; 83 84 unsigned char ip[4]; 85 unsigned short port; 86 87 struct sockaddr_in them; 88 89 /* int socket; this will be kept in bio->num so that it is 90 * compatible with the bss_sock bio */ 91 92 /* called when the connection is initially made 93 * callback(BIO,state,ret); The callback should return 94 * 'ret'. state is for compatibility with the ssl info_callback */ 95 BIO_info_cb *info_callback; 96 } BIO_CONNECT; 97 98 static int conn_write(BIO *h, const char *buf, int num); 99 static int conn_read(BIO *h, char *buf, int size); 100 static int conn_puts(BIO *h, const char *str); 101 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2); 102 static int conn_new(BIO *h); 103 static int conn_free(BIO *data); 104 static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *); 105 106 static int conn_state(BIO *b, BIO_CONNECT *c); 107 static void conn_close_socket(BIO *data); 108 static BIO_CONNECT *BIO_CONNECT_new(void); 109 static void BIO_CONNECT_free(BIO_CONNECT *a); 110 111 static const BIO_METHOD methods_connectp = { 112 .type = BIO_TYPE_CONNECT, 113 .name = "socket connect", 114 .bwrite = conn_write, 115 .bread = conn_read, 116 .bputs = conn_puts, 117 .ctrl = conn_ctrl, 118 .create = conn_new, 119 .destroy = conn_free, 120 .callback_ctrl = conn_callback_ctrl 121 }; 122 123 static int 124 conn_state(BIO *b, BIO_CONNECT *c) 125 { 126 int ret = -1, i; 127 unsigned long l; 128 char *p, *q; 129 BIO_info_cb *cb = NULL; 130 131 if (c->info_callback != NULL) 132 cb = c->info_callback; 133 134 for (;;) { 135 switch (c->state) { 136 case BIO_CONN_S_BEFORE: 137 p = c->param_hostname; 138 if (p == NULL) { 139 BIOerror(BIO_R_NO_HOSTNAME_SPECIFIED); 140 goto exit_loop; 141 } 142 for (; *p != '\0'; p++) { 143 if ((*p == ':') || (*p == '/')) 144 break; 145 } 146 147 i= *p; 148 if ((i == ':') || (i == '/')) { 149 *(p++) = '\0'; 150 if (i == ':') { 151 for (q = p; *q; q++) 152 if (*q == '/') { 153 *q = '\0'; 154 break; 155 } 156 free(c->param_port); 157 c->param_port = strdup(p); 158 } 159 } 160 161 if (c->param_port == NULL) { 162 BIOerror(BIO_R_NO_PORT_SPECIFIED); 163 ERR_asprintf_error_data("host=%s", 164 c->param_hostname); 165 goto exit_loop; 166 } 167 c->state = BIO_CONN_S_GET_IP; 168 break; 169 170 case BIO_CONN_S_GET_IP: 171 if (BIO_get_host_ip(c->param_hostname, &(c->ip[0])) <= 0) 172 goto exit_loop; 173 c->state = BIO_CONN_S_GET_PORT; 174 break; 175 176 case BIO_CONN_S_GET_PORT: 177 if (c->param_port == NULL) { 178 /* abort(); */ 179 goto exit_loop; 180 } else if (BIO_get_port(c->param_port, &c->port) <= 0) 181 goto exit_loop; 182 c->state = BIO_CONN_S_CREATE_SOCKET; 183 break; 184 185 case BIO_CONN_S_CREATE_SOCKET: 186 /* now setup address */ 187 memset((char *)&c->them, 0, sizeof(c->them)); 188 c->them.sin_family = AF_INET; 189 c->them.sin_port = htons((unsigned short)c->port); 190 l = (unsigned long) 191 ((unsigned long)c->ip[0] << 24L)| 192 ((unsigned long)c->ip[1] << 16L)| 193 ((unsigned long)c->ip[2] << 8L)| 194 ((unsigned long)c->ip[3]); 195 c->them.sin_addr.s_addr = htonl(l); 196 c->state = BIO_CONN_S_CREATE_SOCKET; 197 198 ret = socket(AF_INET, SOCK_STREAM, SOCKET_PROTOCOL); 199 if (ret == -1) { 200 SYSerror(errno); 201 ERR_asprintf_error_data("host=%s:%s", 202 c->param_hostname, c->param_port); 203 BIOerror(BIO_R_UNABLE_TO_CREATE_SOCKET); 204 goto exit_loop; 205 } 206 b->num = ret; 207 c->state = BIO_CONN_S_NBIO; 208 break; 209 210 case BIO_CONN_S_NBIO: 211 if (c->nbio) { 212 if (!BIO_socket_nbio(b->num, 1)) { 213 BIOerror(BIO_R_ERROR_SETTING_NBIO); 214 ERR_asprintf_error_data("host=%s:%s", 215 c->param_hostname, c->param_port); 216 goto exit_loop; 217 } 218 } 219 c->state = BIO_CONN_S_CONNECT; 220 221 #if defined(SO_KEEPALIVE) 222 i = 1; 223 i = setsockopt(b->num, SOL_SOCKET, SO_KEEPALIVE, &i, sizeof(i)); 224 if (i < 0) { 225 SYSerror(errno); 226 ERR_asprintf_error_data("host=%s:%s", 227 c->param_hostname, c->param_port); 228 BIOerror(BIO_R_KEEPALIVE); 229 goto exit_loop; 230 } 231 #endif 232 break; 233 234 case BIO_CONN_S_CONNECT: 235 BIO_clear_retry_flags(b); 236 ret = connect(b->num, 237 (struct sockaddr *)&c->them, 238 sizeof(c->them)); 239 b->retry_reason = 0; 240 if (ret < 0) { 241 if (BIO_sock_should_retry(ret)) { 242 BIO_set_retry_special(b); 243 c->state = BIO_CONN_S_BLOCKED_CONNECT; 244 b->retry_reason = BIO_RR_CONNECT; 245 } else { 246 SYSerror(errno); 247 ERR_asprintf_error_data("host=%s:%s", 248 c->param_hostname, c->param_port); 249 BIOerror(BIO_R_CONNECT_ERROR); 250 } 251 goto exit_loop; 252 } else 253 c->state = BIO_CONN_S_OK; 254 break; 255 256 case BIO_CONN_S_BLOCKED_CONNECT: 257 i = BIO_sock_error(b->num); 258 if (i) { 259 BIO_clear_retry_flags(b); 260 SYSerror(i); 261 ERR_asprintf_error_data("host=%s:%s", 262 c->param_hostname, c->param_port); 263 BIOerror(BIO_R_NBIO_CONNECT_ERROR); 264 ret = 0; 265 goto exit_loop; 266 } else 267 c->state = BIO_CONN_S_OK; 268 break; 269 270 case BIO_CONN_S_OK: 271 ret = 1; 272 goto exit_loop; 273 default: 274 /* abort(); */ 275 goto exit_loop; 276 } 277 278 if (cb != NULL) { 279 if (!(ret = cb((BIO *)b, c->state, ret))) 280 goto end; 281 } 282 } 283 284 /* Loop does not exit */ 285 exit_loop: 286 if (cb != NULL) 287 ret = cb((BIO *)b, c->state, ret); 288 end: 289 return (ret); 290 } 291 292 static BIO_CONNECT * 293 BIO_CONNECT_new(void) 294 { 295 BIO_CONNECT *ret; 296 297 if ((ret = malloc(sizeof(BIO_CONNECT))) == NULL) 298 return (NULL); 299 ret->state = BIO_CONN_S_BEFORE; 300 ret->param_hostname = NULL; 301 ret->param_port = NULL; 302 ret->info_callback = NULL; 303 ret->nbio = 0; 304 ret->ip[0] = 0; 305 ret->ip[1] = 0; 306 ret->ip[2] = 0; 307 ret->ip[3] = 0; 308 ret->port = 0; 309 memset((char *)&ret->them, 0, sizeof(ret->them)); 310 return (ret); 311 } 312 313 static void 314 BIO_CONNECT_free(BIO_CONNECT *a) 315 { 316 if (a == NULL) 317 return; 318 319 free(a->param_hostname); 320 free(a->param_port); 321 free(a); 322 } 323 324 const BIO_METHOD * 325 BIO_s_connect(void) 326 { 327 return (&methods_connectp); 328 } 329 LCRYPTO_ALIAS(BIO_s_connect); 330 331 static int 332 conn_new(BIO *bi) 333 { 334 bi->init = 0; 335 bi->num = -1; 336 bi->flags = 0; 337 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) 338 return (0); 339 else 340 return (1); 341 } 342 343 static void 344 conn_close_socket(BIO *bio) 345 { 346 BIO_CONNECT *c; 347 348 c = (BIO_CONNECT *)bio->ptr; 349 if (bio->num != -1) { 350 /* Only do a shutdown if things were established */ 351 if (c->state == BIO_CONN_S_OK) 352 shutdown(bio->num, SHUT_RDWR); 353 close(bio->num); 354 bio->num = -1; 355 } 356 } 357 358 static int 359 conn_free(BIO *a) 360 { 361 BIO_CONNECT *data; 362 363 if (a == NULL) 364 return (0); 365 data = (BIO_CONNECT *)a->ptr; 366 367 if (a->shutdown) { 368 conn_close_socket(a); 369 BIO_CONNECT_free(data); 370 a->ptr = NULL; 371 a->flags = 0; 372 a->init = 0; 373 } 374 return (1); 375 } 376 377 static int 378 conn_read(BIO *b, char *out, int outl) 379 { 380 int ret = 0; 381 BIO_CONNECT *data; 382 383 data = (BIO_CONNECT *)b->ptr; 384 if (data->state != BIO_CONN_S_OK) { 385 ret = conn_state(b, data); 386 if (ret <= 0) 387 return (ret); 388 } 389 390 if (out != NULL) { 391 errno = 0; 392 ret = read(b->num, out, outl); 393 BIO_clear_retry_flags(b); 394 if (ret <= 0) { 395 if (BIO_sock_should_retry(ret)) 396 BIO_set_retry_read(b); 397 } 398 } 399 return (ret); 400 } 401 402 static int 403 conn_write(BIO *b, const char *in, int inl) 404 { 405 int ret; 406 BIO_CONNECT *data; 407 408 data = (BIO_CONNECT *)b->ptr; 409 if (data->state != BIO_CONN_S_OK) { 410 ret = conn_state(b, data); 411 if (ret <= 0) 412 return (ret); 413 } 414 415 errno = 0; 416 ret = write(b->num, in, inl); 417 BIO_clear_retry_flags(b); 418 if (ret <= 0) { 419 if (BIO_sock_should_retry(ret)) 420 BIO_set_retry_write(b); 421 } 422 return (ret); 423 } 424 425 static long 426 conn_ctrl(BIO *b, int cmd, long num, void *ptr) 427 { 428 BIO *dbio; 429 int *ip; 430 const char **pptr; 431 long ret = 1; 432 BIO_CONNECT *data; 433 434 data = (BIO_CONNECT *)b->ptr; 435 436 switch (cmd) { 437 case BIO_CTRL_RESET: 438 ret = 0; 439 data->state = BIO_CONN_S_BEFORE; 440 conn_close_socket(b); 441 b->flags = 0; 442 break; 443 case BIO_C_DO_STATE_MACHINE: 444 /* use this one to start the connection */ 445 if (data->state != BIO_CONN_S_OK) 446 ret = (long)conn_state(b, data); 447 else 448 ret = 1; 449 break; 450 case BIO_C_GET_CONNECT: 451 if (ptr != NULL) { 452 pptr = (const char **)ptr; 453 if (num == 0) { 454 *pptr = data->param_hostname; 455 456 } else if (num == 1) { 457 *pptr = data->param_port; 458 } else if (num == 2) { 459 *pptr = (char *)&(data->ip[0]); 460 } else if (num == 3) { 461 *((int *)ptr) = data->port; 462 } 463 if ((!b->init) || (ptr == NULL)) 464 *pptr = "not initialized"; 465 ret = 1; 466 } 467 break; 468 case BIO_C_SET_CONNECT: 469 if (ptr != NULL) { 470 b->init = 1; 471 if (num == 0) { 472 free(data->param_hostname); 473 data->param_hostname = strdup(ptr); 474 } else if (num == 1) { 475 free(data->param_port); 476 data->param_port = strdup(ptr); 477 } else if (num == 2) { 478 unsigned char *p = ptr; 479 free(data->param_hostname); 480 if (asprintf(&data->param_hostname, 481 "%u.%u.%u.%u", p[0], p[1], 482 p[2], p[3]) == -1) 483 data->param_hostname = NULL; 484 memcpy(&(data->ip[0]), ptr, 4); 485 } else if (num == 3) { 486 free(data->param_port); 487 data->port= *(int *)ptr; 488 if (asprintf(&data->param_port, "%d", 489 data->port) == -1) 490 data->param_port = NULL; 491 } 492 } 493 break; 494 case BIO_C_SET_NBIO: 495 data->nbio = (int)num; 496 break; 497 case BIO_C_GET_FD: 498 if (b->init) { 499 ip = (int *)ptr; 500 if (ip != NULL) 501 *ip = b->num; 502 ret = b->num; 503 } else 504 ret = -1; 505 break; 506 case BIO_CTRL_GET_CLOSE: 507 ret = b->shutdown; 508 break; 509 case BIO_CTRL_SET_CLOSE: 510 b->shutdown = (int)num; 511 break; 512 case BIO_CTRL_PENDING: 513 case BIO_CTRL_WPENDING: 514 ret = 0; 515 break; 516 case BIO_CTRL_FLUSH: 517 break; 518 case BIO_CTRL_DUP: 519 { 520 dbio = (BIO *)ptr; 521 if (data->param_port) 522 BIO_set_conn_port(dbio, data->param_port); 523 if (data->param_hostname) 524 BIO_set_conn_hostname(dbio, 525 data->param_hostname); 526 BIO_set_nbio(dbio, data->nbio); 527 (void)BIO_set_info_callback(dbio, data->info_callback); 528 } 529 break; 530 case BIO_CTRL_SET_CALLBACK: 531 { 532 #if 0 /* FIXME: Should this be used? -- Richard Levitte */ 533 BIOerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 534 ret = -1; 535 #else 536 ret = 0; 537 #endif 538 } 539 break; 540 case BIO_CTRL_GET_CALLBACK: 541 { 542 BIO_info_cb **fptr = ptr; 543 544 *fptr = data->info_callback; 545 } 546 break; 547 default: 548 ret = 0; 549 break; 550 } 551 return (ret); 552 } 553 554 static long 555 conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) 556 { 557 long ret = 1; 558 BIO_CONNECT *data; 559 560 data = (BIO_CONNECT *)b->ptr; 561 562 switch (cmd) { 563 case BIO_CTRL_SET_CALLBACK: 564 data->info_callback = (BIO_info_cb *)fp; 565 break; 566 default: 567 ret = 0; 568 break; 569 } 570 return (ret); 571 } 572 573 static int 574 conn_puts(BIO *bp, const char *str) 575 { 576 int n, ret; 577 578 n = strlen(str); 579 ret = conn_write(bp, str, n); 580 return (ret); 581 } 582 583 BIO * 584 BIO_new_connect(const char *str) 585 { 586 BIO *ret; 587 588 ret = BIO_new(BIO_s_connect()); 589 if (ret == NULL) 590 return (NULL); 591 if (BIO_set_conn_hostname(ret, str)) 592 return (ret); 593 else { 594 BIO_free(ret); 595 return (NULL); 596 } 597 } 598 LCRYPTO_ALIAS(BIO_new_connect); 599