1 /* $OpenBSD: bss_conn.c,v 1.37 2022/01/14 08:40:57 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 BIO_CONNECT *BIO_CONNECT_new(void); 109 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 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 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 330 static int 331 conn_new(BIO *bi) 332 { 333 bi->init = 0; 334 bi->num = -1; 335 bi->flags = 0; 336 if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL) 337 return (0); 338 else 339 return (1); 340 } 341 342 static void 343 conn_close_socket(BIO *bio) 344 { 345 BIO_CONNECT *c; 346 347 c = (BIO_CONNECT *)bio->ptr; 348 if (bio->num != -1) { 349 /* Only do a shutdown if things were established */ 350 if (c->state == BIO_CONN_S_OK) 351 shutdown(bio->num, SHUT_RDWR); 352 close(bio->num); 353 bio->num = -1; 354 } 355 } 356 357 static int 358 conn_free(BIO *a) 359 { 360 BIO_CONNECT *data; 361 362 if (a == NULL) 363 return (0); 364 data = (BIO_CONNECT *)a->ptr; 365 366 if (a->shutdown) { 367 conn_close_socket(a); 368 BIO_CONNECT_free(data); 369 a->ptr = NULL; 370 a->flags = 0; 371 a->init = 0; 372 } 373 return (1); 374 } 375 376 static int 377 conn_read(BIO *b, char *out, int outl) 378 { 379 int ret = 0; 380 BIO_CONNECT *data; 381 382 data = (BIO_CONNECT *)b->ptr; 383 if (data->state != BIO_CONN_S_OK) { 384 ret = conn_state(b, data); 385 if (ret <= 0) 386 return (ret); 387 } 388 389 if (out != NULL) { 390 errno = 0; 391 ret = read(b->num, out, outl); 392 BIO_clear_retry_flags(b); 393 if (ret <= 0) { 394 if (BIO_sock_should_retry(ret)) 395 BIO_set_retry_read(b); 396 } 397 } 398 return (ret); 399 } 400 401 static int 402 conn_write(BIO *b, const char *in, int inl) 403 { 404 int ret; 405 BIO_CONNECT *data; 406 407 data = (BIO_CONNECT *)b->ptr; 408 if (data->state != BIO_CONN_S_OK) { 409 ret = conn_state(b, data); 410 if (ret <= 0) 411 return (ret); 412 } 413 414 errno = 0; 415 ret = write(b->num, in, inl); 416 BIO_clear_retry_flags(b); 417 if (ret <= 0) { 418 if (BIO_sock_should_retry(ret)) 419 BIO_set_retry_write(b); 420 } 421 return (ret); 422 } 423 424 static long 425 conn_ctrl(BIO *b, int cmd, long num, void *ptr) 426 { 427 BIO *dbio; 428 int *ip; 429 const char **pptr; 430 long ret = 1; 431 BIO_CONNECT *data; 432 433 data = (BIO_CONNECT *)b->ptr; 434 435 switch (cmd) { 436 case BIO_CTRL_RESET: 437 ret = 0; 438 data->state = BIO_CONN_S_BEFORE; 439 conn_close_socket(b); 440 b->flags = 0; 441 break; 442 case BIO_C_DO_STATE_MACHINE: 443 /* use this one to start the connection */ 444 if (data->state != BIO_CONN_S_OK) 445 ret = (long)conn_state(b, data); 446 else 447 ret = 1; 448 break; 449 case BIO_C_GET_CONNECT: 450 if (ptr != NULL) { 451 pptr = (const char **)ptr; 452 if (num == 0) { 453 *pptr = data->param_hostname; 454 455 } else if (num == 1) { 456 *pptr = data->param_port; 457 } else if (num == 2) { 458 *pptr = (char *)&(data->ip[0]); 459 } else if (num == 3) { 460 *((int *)ptr) = data->port; 461 } 462 if ((!b->init) || (ptr == NULL)) 463 *pptr = "not initialized"; 464 ret = 1; 465 } 466 break; 467 case BIO_C_SET_CONNECT: 468 if (ptr != NULL) { 469 b->init = 1; 470 if (num == 0) { 471 free(data->param_hostname); 472 data->param_hostname = strdup(ptr); 473 } else if (num == 1) { 474 free(data->param_port); 475 data->param_port = strdup(ptr); 476 } else if (num == 2) { 477 unsigned char *p = ptr; 478 free(data->param_hostname); 479 if (asprintf(&data->param_hostname, 480 "%u.%u.%u.%u", p[0], p[1], 481 p[2], p[3]) == -1) 482 data->param_hostname = NULL; 483 memcpy(&(data->ip[0]), ptr, 4); 484 } else if (num == 3) { 485 free(data->param_port); 486 data->port= *(int *)ptr; 487 if (asprintf(&data->param_port, "%d", 488 data->port) == -1) 489 data->param_port = NULL; 490 } 491 } 492 break; 493 case BIO_C_SET_NBIO: 494 data->nbio = (int)num; 495 break; 496 case BIO_C_GET_FD: 497 if (b->init) { 498 ip = (int *)ptr; 499 if (ip != NULL) 500 *ip = b->num; 501 ret = b->num; 502 } else 503 ret = -1; 504 break; 505 case BIO_CTRL_GET_CLOSE: 506 ret = b->shutdown; 507 break; 508 case BIO_CTRL_SET_CLOSE: 509 b->shutdown = (int)num; 510 break; 511 case BIO_CTRL_PENDING: 512 case BIO_CTRL_WPENDING: 513 ret = 0; 514 break; 515 case BIO_CTRL_FLUSH: 516 break; 517 case BIO_CTRL_DUP: 518 { 519 dbio = (BIO *)ptr; 520 if (data->param_port) 521 BIO_set_conn_port(dbio, data->param_port); 522 if (data->param_hostname) 523 BIO_set_conn_hostname(dbio, 524 data->param_hostname); 525 BIO_set_nbio(dbio, data->nbio); 526 (void)BIO_set_info_callback(dbio, data->info_callback); 527 } 528 break; 529 case BIO_CTRL_SET_CALLBACK: 530 { 531 #if 0 /* FIXME: Should this be used? -- Richard Levitte */ 532 BIOerror(ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); 533 ret = -1; 534 #else 535 ret = 0; 536 #endif 537 } 538 break; 539 case BIO_CTRL_GET_CALLBACK: 540 { 541 BIO_info_cb **fptr = ptr; 542 543 *fptr = data->info_callback; 544 } 545 break; 546 default: 547 ret = 0; 548 break; 549 } 550 return (ret); 551 } 552 553 static long 554 conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp) 555 { 556 long ret = 1; 557 BIO_CONNECT *data; 558 559 data = (BIO_CONNECT *)b->ptr; 560 561 switch (cmd) { 562 case BIO_CTRL_SET_CALLBACK: 563 data->info_callback = (BIO_info_cb *)fp; 564 break; 565 default: 566 ret = 0; 567 break; 568 } 569 return (ret); 570 } 571 572 static int 573 conn_puts(BIO *bp, const char *str) 574 { 575 int n, ret; 576 577 n = strlen(str); 578 ret = conn_write(bp, str, n); 579 return (ret); 580 } 581 582 BIO * 583 BIO_new_connect(const char *str) 584 { 585 BIO *ret; 586 587 ret = BIO_new(BIO_s_connect()); 588 if (ret == NULL) 589 return (NULL); 590 if (BIO_set_conn_hostname(ret, str)) 591 return (ret); 592 else { 593 BIO_free(ret); 594 return (NULL); 595 } 596 } 597 598