2
0

bss_acpt.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. /*
  2. * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #define OPENSSL_SUPPRESS_DEPRECATED
  10. #include <stdio.h>
  11. #include <errno.h>
  12. #include "bio_local.h"
  13. #ifndef OPENSSL_NO_SOCK
  14. typedef struct bio_accept_st {
  15. int state;
  16. int accept_family;
  17. int bind_mode; /* Socket mode for BIO_listen */
  18. int accepted_mode; /* Socket mode for BIO_accept (set on accepted sock) */
  19. char *param_addr;
  20. char *param_serv;
  21. int accept_sock;
  22. BIO_ADDRINFO *addr_first;
  23. const BIO_ADDRINFO *addr_iter;
  24. BIO_ADDR cache_accepting_addr; /* Useful if we asked for port 0 */
  25. char *cache_accepting_name, *cache_accepting_serv;
  26. BIO_ADDR cache_peer_addr;
  27. char *cache_peer_name, *cache_peer_serv;
  28. BIO *bio_chain;
  29. } BIO_ACCEPT;
  30. static int acpt_write(BIO *h, const char *buf, int num);
  31. static int acpt_read(BIO *h, char *buf, int size);
  32. static int acpt_puts(BIO *h, const char *str);
  33. static long acpt_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  34. static int acpt_new(BIO *h);
  35. static int acpt_free(BIO *data);
  36. static int acpt_state(BIO *b, BIO_ACCEPT *c);
  37. static void acpt_close_socket(BIO *data);
  38. static BIO_ACCEPT *BIO_ACCEPT_new(void);
  39. static void BIO_ACCEPT_free(BIO_ACCEPT *a);
  40. # define ACPT_S_BEFORE 1
  41. # define ACPT_S_GET_ADDR 2
  42. # define ACPT_S_CREATE_SOCKET 3
  43. # define ACPT_S_LISTEN 4
  44. # define ACPT_S_ACCEPT 5
  45. # define ACPT_S_OK 6
  46. static const BIO_METHOD methods_acceptp = {
  47. BIO_TYPE_ACCEPT,
  48. "socket accept",
  49. bwrite_conv,
  50. acpt_write,
  51. bread_conv,
  52. acpt_read,
  53. acpt_puts,
  54. NULL, /* connect_gets, */
  55. acpt_ctrl,
  56. acpt_new,
  57. acpt_free,
  58. NULL, /* connect_callback_ctrl */
  59. };
  60. const BIO_METHOD *BIO_s_accept(void)
  61. {
  62. return &methods_acceptp;
  63. }
  64. static int acpt_new(BIO *bi)
  65. {
  66. BIO_ACCEPT *ba;
  67. bi->init = 0;
  68. bi->num = (int)INVALID_SOCKET;
  69. bi->flags = 0;
  70. if ((ba = BIO_ACCEPT_new()) == NULL)
  71. return 0;
  72. bi->ptr = (char *)ba;
  73. ba->state = ACPT_S_BEFORE;
  74. bi->shutdown = 1;
  75. return 1;
  76. }
  77. static BIO_ACCEPT *BIO_ACCEPT_new(void)
  78. {
  79. BIO_ACCEPT *ret;
  80. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL)
  81. return NULL;
  82. ret->accept_family = BIO_FAMILY_IPANY;
  83. ret->accept_sock = (int)INVALID_SOCKET;
  84. return ret;
  85. }
  86. static void BIO_ACCEPT_free(BIO_ACCEPT *a)
  87. {
  88. if (a == NULL)
  89. return;
  90. OPENSSL_free(a->param_addr);
  91. OPENSSL_free(a->param_serv);
  92. BIO_ADDRINFO_free(a->addr_first);
  93. OPENSSL_free(a->cache_accepting_name);
  94. OPENSSL_free(a->cache_accepting_serv);
  95. OPENSSL_free(a->cache_peer_name);
  96. OPENSSL_free(a->cache_peer_serv);
  97. BIO_free(a->bio_chain);
  98. OPENSSL_free(a);
  99. }
  100. static void acpt_close_socket(BIO *bio)
  101. {
  102. BIO_ACCEPT *c;
  103. c = (BIO_ACCEPT *)bio->ptr;
  104. if (c->accept_sock != (int)INVALID_SOCKET) {
  105. shutdown(c->accept_sock, 2);
  106. closesocket(c->accept_sock);
  107. c->accept_sock = (int)INVALID_SOCKET;
  108. bio->num = (int)INVALID_SOCKET;
  109. }
  110. }
  111. static int acpt_free(BIO *a)
  112. {
  113. BIO_ACCEPT *data;
  114. if (a == NULL)
  115. return 0;
  116. data = (BIO_ACCEPT *)a->ptr;
  117. if (a->shutdown) {
  118. acpt_close_socket(a);
  119. BIO_ACCEPT_free(data);
  120. a->ptr = NULL;
  121. a->flags = 0;
  122. a->init = 0;
  123. }
  124. return 1;
  125. }
  126. static int acpt_state(BIO *b, BIO_ACCEPT *c)
  127. {
  128. BIO *bio = NULL, *dbio;
  129. int s = -1, ret = -1;
  130. for (;;) {
  131. switch (c->state) {
  132. case ACPT_S_BEFORE:
  133. if (c->param_addr == NULL && c->param_serv == NULL) {
  134. ERR_raise_data(ERR_LIB_BIO,
  135. BIO_R_NO_ACCEPT_ADDR_OR_SERVICE_SPECIFIED,
  136. "hostname=%s, service=%s",
  137. c->param_addr, c->param_serv);
  138. goto exit_loop;
  139. }
  140. /* Because we're starting a new bind, any cached name and serv
  141. * are now obsolete and need to be cleaned out.
  142. * QUESTION: should this be done in acpt_close_socket() instead?
  143. */
  144. OPENSSL_free(c->cache_accepting_name);
  145. c->cache_accepting_name = NULL;
  146. OPENSSL_free(c->cache_accepting_serv);
  147. c->cache_accepting_serv = NULL;
  148. OPENSSL_free(c->cache_peer_name);
  149. c->cache_peer_name = NULL;
  150. OPENSSL_free(c->cache_peer_serv);
  151. c->cache_peer_serv = NULL;
  152. c->state = ACPT_S_GET_ADDR;
  153. break;
  154. case ACPT_S_GET_ADDR:
  155. {
  156. int family = AF_UNSPEC;
  157. switch (c->accept_family) {
  158. case BIO_FAMILY_IPV6:
  159. if (1) { /* This is a trick we use to avoid bit rot.
  160. * at least the "else" part will always be
  161. * compiled.
  162. */
  163. #if OPENSSL_USE_IPV6
  164. family = AF_INET6;
  165. } else {
  166. #endif
  167. ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
  168. goto exit_loop;
  169. }
  170. break;
  171. case BIO_FAMILY_IPV4:
  172. family = AF_INET;
  173. break;
  174. case BIO_FAMILY_IPANY:
  175. family = AF_UNSPEC;
  176. break;
  177. default:
  178. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
  179. goto exit_loop;
  180. }
  181. if (BIO_lookup(c->param_addr, c->param_serv, BIO_LOOKUP_SERVER,
  182. family, SOCK_STREAM, &c->addr_first) == 0)
  183. goto exit_loop;
  184. }
  185. if (c->addr_first == NULL) {
  186. ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
  187. goto exit_loop;
  188. }
  189. c->addr_iter = c->addr_first;
  190. c->state = ACPT_S_CREATE_SOCKET;
  191. break;
  192. case ACPT_S_CREATE_SOCKET:
  193. ERR_set_mark();
  194. s = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
  195. BIO_ADDRINFO_socktype(c->addr_iter),
  196. BIO_ADDRINFO_protocol(c->addr_iter), 0);
  197. if (s == (int)INVALID_SOCKET) {
  198. if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
  199. /*
  200. * if there are more addresses to try, do that first
  201. */
  202. ERR_pop_to_mark();
  203. break;
  204. }
  205. ERR_clear_last_mark();
  206. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  207. "calling socket(%s, %s)",
  208. c->param_addr, c->param_serv);
  209. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
  210. goto exit_loop;
  211. }
  212. c->accept_sock = s;
  213. b->num = s;
  214. c->state = ACPT_S_LISTEN;
  215. s = -1;
  216. break;
  217. case ACPT_S_LISTEN:
  218. {
  219. if (!BIO_listen(c->accept_sock,
  220. BIO_ADDRINFO_address(c->addr_iter),
  221. c->bind_mode)) {
  222. BIO_closesocket(c->accept_sock);
  223. goto exit_loop;
  224. }
  225. }
  226. {
  227. union BIO_sock_info_u info;
  228. info.addr = &c->cache_accepting_addr;
  229. if (!BIO_sock_info(c->accept_sock, BIO_SOCK_INFO_ADDRESS,
  230. &info)) {
  231. BIO_closesocket(c->accept_sock);
  232. goto exit_loop;
  233. }
  234. }
  235. c->cache_accepting_name =
  236. BIO_ADDR_hostname_string(&c->cache_accepting_addr, 1);
  237. c->cache_accepting_serv =
  238. BIO_ADDR_service_string(&c->cache_accepting_addr, 1);
  239. c->state = ACPT_S_ACCEPT;
  240. s = -1;
  241. ret = 1;
  242. goto end;
  243. case ACPT_S_ACCEPT:
  244. if (b->next_bio != NULL) {
  245. c->state = ACPT_S_OK;
  246. break;
  247. }
  248. BIO_clear_retry_flags(b);
  249. b->retry_reason = 0;
  250. OPENSSL_free(c->cache_peer_name);
  251. c->cache_peer_name = NULL;
  252. OPENSSL_free(c->cache_peer_serv);
  253. c->cache_peer_serv = NULL;
  254. s = BIO_accept_ex(c->accept_sock, &c->cache_peer_addr,
  255. c->accepted_mode);
  256. /* If the returned socket is invalid, this might still be
  257. * retryable
  258. */
  259. if (s < 0) {
  260. if (BIO_sock_should_retry(s)) {
  261. BIO_set_retry_special(b);
  262. b->retry_reason = BIO_RR_ACCEPT;
  263. goto end;
  264. }
  265. }
  266. /* If it wasn't retryable, we fail */
  267. if (s < 0) {
  268. ret = s;
  269. goto exit_loop;
  270. }
  271. bio = BIO_new_socket(s, BIO_CLOSE);
  272. if (bio == NULL)
  273. goto exit_loop;
  274. BIO_set_callback_ex(bio, BIO_get_callback_ex(b));
  275. #ifndef OPENSSL_NO_DEPRECATED_3_0
  276. BIO_set_callback(bio, BIO_get_callback(b));
  277. #endif
  278. BIO_set_callback_arg(bio, BIO_get_callback_arg(b));
  279. /*
  280. * If the accept BIO has an bio_chain, we dup it and put the new
  281. * socket at the end.
  282. */
  283. if (c->bio_chain != NULL) {
  284. if ((dbio = BIO_dup_chain(c->bio_chain)) == NULL)
  285. goto exit_loop;
  286. if (!BIO_push(dbio, bio))
  287. goto exit_loop;
  288. bio = dbio;
  289. }
  290. if (BIO_push(b, bio) == NULL)
  291. goto exit_loop;
  292. c->cache_peer_name =
  293. BIO_ADDR_hostname_string(&c->cache_peer_addr, 1);
  294. c->cache_peer_serv =
  295. BIO_ADDR_service_string(&c->cache_peer_addr, 1);
  296. c->state = ACPT_S_OK;
  297. bio = NULL;
  298. ret = 1;
  299. goto end;
  300. case ACPT_S_OK:
  301. if (b->next_bio == NULL) {
  302. c->state = ACPT_S_ACCEPT;
  303. break;
  304. }
  305. ret = 1;
  306. goto end;
  307. default:
  308. ret = 0;
  309. goto end;
  310. }
  311. }
  312. exit_loop:
  313. if (bio != NULL)
  314. BIO_free(bio);
  315. else if (s >= 0)
  316. BIO_closesocket(s);
  317. end:
  318. return ret;
  319. }
  320. static int acpt_read(BIO *b, char *out, int outl)
  321. {
  322. int ret = 0;
  323. BIO_ACCEPT *data;
  324. BIO_clear_retry_flags(b);
  325. data = (BIO_ACCEPT *)b->ptr;
  326. while (b->next_bio == NULL) {
  327. ret = acpt_state(b, data);
  328. if (ret <= 0)
  329. return ret;
  330. }
  331. ret = BIO_read(b->next_bio, out, outl);
  332. BIO_copy_next_retry(b);
  333. return ret;
  334. }
  335. static int acpt_write(BIO *b, const char *in, int inl)
  336. {
  337. int ret;
  338. BIO_ACCEPT *data;
  339. BIO_clear_retry_flags(b);
  340. data = (BIO_ACCEPT *)b->ptr;
  341. while (b->next_bio == NULL) {
  342. ret = acpt_state(b, data);
  343. if (ret <= 0)
  344. return ret;
  345. }
  346. ret = BIO_write(b->next_bio, in, inl);
  347. BIO_copy_next_retry(b);
  348. return ret;
  349. }
  350. static long acpt_ctrl(BIO *b, int cmd, long num, void *ptr)
  351. {
  352. int *ip;
  353. long ret = 1;
  354. BIO_ACCEPT *data;
  355. char **pp;
  356. data = (BIO_ACCEPT *)b->ptr;
  357. switch (cmd) {
  358. case BIO_CTRL_RESET:
  359. ret = 0;
  360. data->state = ACPT_S_BEFORE;
  361. acpt_close_socket(b);
  362. BIO_ADDRINFO_free(data->addr_first);
  363. data->addr_first = NULL;
  364. b->flags = 0;
  365. break;
  366. case BIO_C_DO_STATE_MACHINE:
  367. /* use this one to start the connection */
  368. ret = (long)acpt_state(b, data);
  369. break;
  370. case BIO_C_SET_ACCEPT:
  371. if (ptr != NULL) {
  372. if (num == 0) {
  373. char *hold_serv = data->param_serv;
  374. /* We affect the hostname regardless. However, the input
  375. * string might contain a host:service spec, so we must
  376. * parse it, which might or might not affect the service
  377. */
  378. OPENSSL_free(data->param_addr);
  379. data->param_addr = NULL;
  380. ret = BIO_parse_hostserv(ptr,
  381. &data->param_addr,
  382. &data->param_serv,
  383. BIO_PARSE_PRIO_SERV);
  384. if (hold_serv != data->param_serv)
  385. OPENSSL_free(hold_serv);
  386. b->init = 1;
  387. } else if (num == 1) {
  388. OPENSSL_free(data->param_serv);
  389. if ((data->param_serv = OPENSSL_strdup(ptr)) == NULL)
  390. ret = 0;
  391. else
  392. b->init = 1;
  393. } else if (num == 2) {
  394. data->bind_mode |= BIO_SOCK_NONBLOCK;
  395. } else if (num == 3) {
  396. BIO_free(data->bio_chain);
  397. data->bio_chain = (BIO *)ptr;
  398. } else if (num == 4) {
  399. data->accept_family = *(int *)ptr;
  400. } else if (num == 5) {
  401. data->bind_mode |= BIO_SOCK_TFO;
  402. }
  403. } else {
  404. if (num == 2) {
  405. data->bind_mode &= ~BIO_SOCK_NONBLOCK;
  406. } else if (num == 5) {
  407. data->bind_mode &= ~BIO_SOCK_TFO;
  408. }
  409. }
  410. break;
  411. case BIO_C_SET_NBIO:
  412. if (num != 0)
  413. data->accepted_mode |= BIO_SOCK_NONBLOCK;
  414. else
  415. data->accepted_mode &= ~BIO_SOCK_NONBLOCK;
  416. break;
  417. case BIO_C_SET_FD:
  418. b->num = *((int *)ptr);
  419. data->accept_sock = b->num;
  420. data->state = ACPT_S_ACCEPT;
  421. b->shutdown = (int)num;
  422. b->init = 1;
  423. break;
  424. case BIO_C_GET_FD:
  425. if (b->init) {
  426. ip = (int *)ptr;
  427. if (ip != NULL)
  428. *ip = data->accept_sock;
  429. ret = data->accept_sock;
  430. } else
  431. ret = -1;
  432. break;
  433. case BIO_C_GET_ACCEPT:
  434. if (b->init) {
  435. if (num == 0 && ptr != NULL) {
  436. pp = (char **)ptr;
  437. *pp = data->cache_accepting_name;
  438. } else if (num == 1 && ptr != NULL) {
  439. pp = (char **)ptr;
  440. *pp = data->cache_accepting_serv;
  441. } else if (num == 2 && ptr != NULL) {
  442. pp = (char **)ptr;
  443. *pp = data->cache_peer_name;
  444. } else if (num == 3 && ptr != NULL) {
  445. pp = (char **)ptr;
  446. *pp = data->cache_peer_serv;
  447. } else if (num == 4) {
  448. switch (BIO_ADDRINFO_family(data->addr_iter)) {
  449. #if OPENSSL_USE_IPV6
  450. case AF_INET6:
  451. ret = BIO_FAMILY_IPV6;
  452. break;
  453. #endif
  454. case AF_INET:
  455. ret = BIO_FAMILY_IPV4;
  456. break;
  457. case 0:
  458. ret = data->accept_family;
  459. break;
  460. default:
  461. ret = -1;
  462. break;
  463. }
  464. } else
  465. ret = -1;
  466. } else
  467. ret = -1;
  468. break;
  469. case BIO_CTRL_GET_CLOSE:
  470. ret = b->shutdown;
  471. break;
  472. case BIO_CTRL_SET_CLOSE:
  473. b->shutdown = (int)num;
  474. break;
  475. case BIO_CTRL_PENDING:
  476. case BIO_CTRL_WPENDING:
  477. ret = 0;
  478. break;
  479. case BIO_CTRL_FLUSH:
  480. break;
  481. case BIO_C_SET_BIND_MODE:
  482. data->bind_mode = (int)num;
  483. break;
  484. case BIO_C_GET_BIND_MODE:
  485. ret = (long)data->bind_mode;
  486. break;
  487. case BIO_CTRL_DUP:
  488. break;
  489. case BIO_CTRL_EOF:
  490. if (b->next_bio == NULL)
  491. ret = 0;
  492. else
  493. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  494. break;
  495. default:
  496. ret = 0;
  497. break;
  498. }
  499. return ret;
  500. }
  501. static int acpt_puts(BIO *bp, const char *str)
  502. {
  503. int n, ret;
  504. n = strlen(str);
  505. ret = acpt_write(bp, str, n);
  506. return ret;
  507. }
  508. BIO *BIO_new_accept(const char *str)
  509. {
  510. BIO *ret;
  511. ret = BIO_new(BIO_s_accept());
  512. if (ret == NULL)
  513. return NULL;
  514. if (BIO_set_accept_name(ret, str) > 0)
  515. return ret;
  516. BIO_free(ret);
  517. return NULL;
  518. }
  519. #endif