bss_conn.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  1. /*
  2. * Copyright 1995-2021 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. #include <stdio.h>
  10. #include <errno.h>
  11. #include "bio_local.h"
  12. #include "internal/ktls.h"
  13. #ifndef OPENSSL_NO_SOCK
  14. typedef struct bio_connect_st {
  15. int state;
  16. int connect_family;
  17. char *param_hostname;
  18. char *param_service;
  19. int connect_mode;
  20. # ifndef OPENSSL_NO_KTLS
  21. unsigned char record_type;
  22. # endif
  23. BIO_ADDRINFO *addr_first;
  24. const BIO_ADDRINFO *addr_iter;
  25. /*
  26. * int socket; this will be kept in bio->num so that it is compatible
  27. * with the bss_sock bio
  28. */
  29. /*
  30. * called when the connection is initially made callback(BIO,state,ret);
  31. * The callback should return 'ret'. state is for compatibility with the
  32. * ssl info_callback
  33. */
  34. BIO_info_cb *info_callback;
  35. } BIO_CONNECT;
  36. static int conn_write(BIO *h, const char *buf, int num);
  37. static int conn_read(BIO *h, char *buf, int size);
  38. static int conn_puts(BIO *h, const char *str);
  39. static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  40. static int conn_new(BIO *h);
  41. static int conn_free(BIO *data);
  42. static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
  43. static int conn_state(BIO *b, BIO_CONNECT *c);
  44. static void conn_close_socket(BIO *data);
  45. BIO_CONNECT *BIO_CONNECT_new(void);
  46. void BIO_CONNECT_free(BIO_CONNECT *a);
  47. #define BIO_CONN_S_BEFORE 1
  48. #define BIO_CONN_S_GET_ADDR 2
  49. #define BIO_CONN_S_CREATE_SOCKET 3
  50. #define BIO_CONN_S_CONNECT 4
  51. #define BIO_CONN_S_OK 5
  52. #define BIO_CONN_S_BLOCKED_CONNECT 6
  53. #define BIO_CONN_S_CONNECT_ERROR 7
  54. static const BIO_METHOD methods_connectp = {
  55. BIO_TYPE_CONNECT,
  56. "socket connect",
  57. bwrite_conv,
  58. conn_write,
  59. bread_conv,
  60. conn_read,
  61. conn_puts,
  62. NULL, /* conn_gets, */
  63. conn_ctrl,
  64. conn_new,
  65. conn_free,
  66. conn_callback_ctrl,
  67. };
  68. static int conn_state(BIO *b, BIO_CONNECT *c)
  69. {
  70. int ret = -1, i;
  71. BIO_info_cb *cb = NULL;
  72. if (c->info_callback != NULL)
  73. cb = c->info_callback;
  74. for (;;) {
  75. switch (c->state) {
  76. case BIO_CONN_S_BEFORE:
  77. if (c->param_hostname == NULL && c->param_service == NULL) {
  78. ERR_raise_data(ERR_LIB_BIO,
  79. BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
  80. "hostname=%s service=%s",
  81. c->param_hostname, c->param_service);
  82. goto exit_loop;
  83. }
  84. c->state = BIO_CONN_S_GET_ADDR;
  85. break;
  86. case BIO_CONN_S_GET_ADDR:
  87. {
  88. int family = AF_UNSPEC;
  89. switch (c->connect_family) {
  90. case BIO_FAMILY_IPV6:
  91. if (1) { /* This is a trick we use to avoid bit rot.
  92. * at least the "else" part will always be
  93. * compiled.
  94. */
  95. #ifdef AF_INET6
  96. family = AF_INET6;
  97. } else {
  98. #endif
  99. ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
  100. goto exit_loop;
  101. }
  102. break;
  103. case BIO_FAMILY_IPV4:
  104. family = AF_INET;
  105. break;
  106. case BIO_FAMILY_IPANY:
  107. family = AF_UNSPEC;
  108. break;
  109. default:
  110. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
  111. goto exit_loop;
  112. }
  113. if (BIO_lookup(c->param_hostname, c->param_service,
  114. BIO_LOOKUP_CLIENT,
  115. family, SOCK_STREAM, &c->addr_first) == 0)
  116. goto exit_loop;
  117. }
  118. if (c->addr_first == NULL) {
  119. ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
  120. goto exit_loop;
  121. }
  122. c->addr_iter = c->addr_first;
  123. c->state = BIO_CONN_S_CREATE_SOCKET;
  124. break;
  125. case BIO_CONN_S_CREATE_SOCKET:
  126. ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
  127. BIO_ADDRINFO_socktype(c->addr_iter),
  128. BIO_ADDRINFO_protocol(c->addr_iter), 0);
  129. if (ret == (int)INVALID_SOCKET) {
  130. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  131. "calling socket(%s, %s)",
  132. c->param_hostname, c->param_service);
  133. ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
  134. goto exit_loop;
  135. }
  136. b->num = ret;
  137. c->state = BIO_CONN_S_CONNECT;
  138. break;
  139. case BIO_CONN_S_CONNECT:
  140. BIO_clear_retry_flags(b);
  141. ERR_set_mark();
  142. ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
  143. BIO_SOCK_KEEPALIVE | c->connect_mode);
  144. b->retry_reason = 0;
  145. if (ret == 0) {
  146. if (BIO_sock_should_retry(ret)) {
  147. BIO_set_retry_special(b);
  148. c->state = BIO_CONN_S_BLOCKED_CONNECT;
  149. b->retry_reason = BIO_RR_CONNECT;
  150. ERR_pop_to_mark();
  151. } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
  152. != NULL) {
  153. /*
  154. * if there are more addresses to try, do that first
  155. */
  156. BIO_closesocket(b->num);
  157. c->state = BIO_CONN_S_CREATE_SOCKET;
  158. ERR_pop_to_mark();
  159. break;
  160. } else {
  161. ERR_clear_last_mark();
  162. ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
  163. "calling connect(%s, %s)",
  164. c->param_hostname, c->param_service);
  165. c->state = BIO_CONN_S_CONNECT_ERROR;
  166. break;
  167. }
  168. goto exit_loop;
  169. } else {
  170. ERR_clear_last_mark();
  171. c->state = BIO_CONN_S_OK;
  172. }
  173. break;
  174. case BIO_CONN_S_BLOCKED_CONNECT:
  175. i = BIO_sock_error(b->num);
  176. if (i != 0) {
  177. BIO_clear_retry_flags(b);
  178. if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
  179. /*
  180. * if there are more addresses to try, do that first
  181. */
  182. BIO_closesocket(b->num);
  183. c->state = BIO_CONN_S_CREATE_SOCKET;
  184. break;
  185. }
  186. ERR_raise_data(ERR_LIB_SYS, i,
  187. "calling connect(%s, %s)",
  188. c->param_hostname, c->param_service);
  189. ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
  190. ret = 0;
  191. goto exit_loop;
  192. } else
  193. c->state = BIO_CONN_S_OK;
  194. break;
  195. case BIO_CONN_S_CONNECT_ERROR:
  196. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  197. ret = 0;
  198. goto exit_loop;
  199. case BIO_CONN_S_OK:
  200. ret = 1;
  201. goto exit_loop;
  202. default:
  203. /* abort(); */
  204. goto exit_loop;
  205. }
  206. if (cb != NULL) {
  207. if ((ret = cb((BIO *)b, c->state, ret)) == 0)
  208. goto end;
  209. }
  210. }
  211. /* Loop does not exit */
  212. exit_loop:
  213. if (cb != NULL)
  214. ret = cb((BIO *)b, c->state, ret);
  215. end:
  216. return ret;
  217. }
  218. BIO_CONNECT *BIO_CONNECT_new(void)
  219. {
  220. BIO_CONNECT *ret;
  221. if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
  222. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  223. return NULL;
  224. }
  225. ret->state = BIO_CONN_S_BEFORE;
  226. ret->connect_family = BIO_FAMILY_IPANY;
  227. return ret;
  228. }
  229. void BIO_CONNECT_free(BIO_CONNECT *a)
  230. {
  231. if (a == NULL)
  232. return;
  233. OPENSSL_free(a->param_hostname);
  234. OPENSSL_free(a->param_service);
  235. BIO_ADDRINFO_free(a->addr_first);
  236. OPENSSL_free(a);
  237. }
  238. const BIO_METHOD *BIO_s_connect(void)
  239. {
  240. return &methods_connectp;
  241. }
  242. static int conn_new(BIO *bi)
  243. {
  244. bi->init = 0;
  245. bi->num = (int)INVALID_SOCKET;
  246. bi->flags = 0;
  247. if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
  248. return 0;
  249. else
  250. return 1;
  251. }
  252. static void conn_close_socket(BIO *bio)
  253. {
  254. BIO_CONNECT *c;
  255. c = (BIO_CONNECT *)bio->ptr;
  256. if (bio->num != (int)INVALID_SOCKET) {
  257. /* Only do a shutdown if things were established */
  258. if (c->state == BIO_CONN_S_OK)
  259. shutdown(bio->num, 2);
  260. BIO_closesocket(bio->num);
  261. bio->num = (int)INVALID_SOCKET;
  262. }
  263. }
  264. static int conn_free(BIO *a)
  265. {
  266. BIO_CONNECT *data;
  267. if (a == NULL)
  268. return 0;
  269. data = (BIO_CONNECT *)a->ptr;
  270. if (a->shutdown) {
  271. conn_close_socket(a);
  272. BIO_CONNECT_free(data);
  273. a->ptr = NULL;
  274. a->flags = 0;
  275. a->init = 0;
  276. }
  277. return 1;
  278. }
  279. static int conn_read(BIO *b, char *out, int outl)
  280. {
  281. int ret = 0;
  282. BIO_CONNECT *data;
  283. data = (BIO_CONNECT *)b->ptr;
  284. if (data->state != BIO_CONN_S_OK) {
  285. ret = conn_state(b, data);
  286. if (ret <= 0)
  287. return ret;
  288. }
  289. if (out != NULL) {
  290. clear_socket_error();
  291. # ifndef OPENSSL_NO_KTLS
  292. if (BIO_get_ktls_recv(b))
  293. ret = ktls_read_record(b->num, out, outl);
  294. else
  295. # endif
  296. ret = readsocket(b->num, out, outl);
  297. BIO_clear_retry_flags(b);
  298. if (ret <= 0) {
  299. if (BIO_sock_should_retry(ret))
  300. BIO_set_retry_read(b);
  301. else if (ret == 0)
  302. b->flags |= BIO_FLAGS_IN_EOF;
  303. }
  304. }
  305. return ret;
  306. }
  307. static int conn_write(BIO *b, const char *in, int inl)
  308. {
  309. int ret;
  310. BIO_CONNECT *data;
  311. data = (BIO_CONNECT *)b->ptr;
  312. if (data->state != BIO_CONN_S_OK) {
  313. ret = conn_state(b, data);
  314. if (ret <= 0)
  315. return ret;
  316. }
  317. clear_socket_error();
  318. # ifndef OPENSSL_NO_KTLS
  319. if (BIO_should_ktls_ctrl_msg_flag(b)) {
  320. ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
  321. if (ret >= 0) {
  322. ret = inl;
  323. BIO_clear_ktls_ctrl_msg_flag(b);
  324. }
  325. } else
  326. # endif
  327. ret = writesocket(b->num, in, inl);
  328. BIO_clear_retry_flags(b);
  329. if (ret <= 0) {
  330. if (BIO_sock_should_retry(ret))
  331. BIO_set_retry_write(b);
  332. }
  333. return ret;
  334. }
  335. static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
  336. {
  337. BIO *dbio;
  338. int *ip;
  339. const char **pptr = NULL;
  340. long ret = 1;
  341. BIO_CONNECT *data;
  342. # ifndef OPENSSL_NO_KTLS
  343. ktls_crypto_info_t *crypto_info;
  344. # endif
  345. data = (BIO_CONNECT *)b->ptr;
  346. switch (cmd) {
  347. case BIO_CTRL_RESET:
  348. ret = 0;
  349. data->state = BIO_CONN_S_BEFORE;
  350. conn_close_socket(b);
  351. BIO_ADDRINFO_free(data->addr_first);
  352. data->addr_first = NULL;
  353. b->flags = 0;
  354. break;
  355. case BIO_C_DO_STATE_MACHINE:
  356. /* use this one to start the connection */
  357. if (data->state != BIO_CONN_S_OK)
  358. ret = (long)conn_state(b, data);
  359. else
  360. ret = 1;
  361. break;
  362. case BIO_C_GET_CONNECT:
  363. if (ptr != NULL) {
  364. pptr = (const char **)ptr;
  365. if (num == 0) {
  366. *pptr = data->param_hostname;
  367. } else if (num == 1) {
  368. *pptr = data->param_service;
  369. } else if (num == 2) {
  370. *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
  371. } else if (num == 3) {
  372. switch (BIO_ADDRINFO_family(data->addr_iter)) {
  373. # ifdef AF_INET6
  374. case AF_INET6:
  375. ret = BIO_FAMILY_IPV6;
  376. break;
  377. # endif
  378. case AF_INET:
  379. ret = BIO_FAMILY_IPV4;
  380. break;
  381. case 0:
  382. ret = data->connect_family;
  383. break;
  384. default:
  385. ret = -1;
  386. break;
  387. }
  388. } else {
  389. ret = 0;
  390. }
  391. } else {
  392. ret = 0;
  393. }
  394. break;
  395. case BIO_C_SET_CONNECT:
  396. if (ptr != NULL) {
  397. b->init = 1;
  398. if (num == 0) { /* BIO_set_conn_hostname */
  399. char *hold_service = data->param_service;
  400. /* We affect the hostname regardless. However, the input
  401. * string might contain a host:service spec, so we must
  402. * parse it, which might or might not affect the service
  403. */
  404. OPENSSL_free(data->param_hostname);
  405. data->param_hostname = NULL;
  406. ret = BIO_parse_hostserv(ptr,
  407. &data->param_hostname,
  408. &data->param_service,
  409. BIO_PARSE_PRIO_HOST);
  410. if (hold_service != data->param_service)
  411. OPENSSL_free(hold_service);
  412. } else if (num == 1) { /* BIO_set_conn_port */
  413. OPENSSL_free(data->param_service);
  414. if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
  415. ret = 0;
  416. } else if (num == 2) { /* BIO_set_conn_address */
  417. const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
  418. char *host = BIO_ADDR_hostname_string(addr, 1);
  419. char *service = BIO_ADDR_service_string(addr, 1);
  420. ret = host != NULL && service != NULL;
  421. if (ret) {
  422. OPENSSL_free(data->param_hostname);
  423. data->param_hostname = host;
  424. OPENSSL_free(data->param_service);
  425. data->param_service = service;
  426. BIO_ADDRINFO_free(data->addr_first);
  427. data->addr_first = NULL;
  428. data->addr_iter = NULL;
  429. } else {
  430. OPENSSL_free(host);
  431. OPENSSL_free(service);
  432. }
  433. } else if (num == 3) { /* BIO_set_conn_ip_family */
  434. data->connect_family = *(int *)ptr;
  435. } else {
  436. ret = 0;
  437. }
  438. }
  439. break;
  440. case BIO_C_SET_NBIO:
  441. if (num != 0)
  442. data->connect_mode |= BIO_SOCK_NONBLOCK;
  443. else
  444. data->connect_mode &= ~BIO_SOCK_NONBLOCK;
  445. break;
  446. case BIO_C_SET_CONNECT_MODE:
  447. data->connect_mode = (int)num;
  448. break;
  449. case BIO_C_GET_FD:
  450. if (b->init) {
  451. ip = (int *)ptr;
  452. if (ip != NULL)
  453. *ip = b->num;
  454. ret = b->num;
  455. } else
  456. ret = -1;
  457. break;
  458. case BIO_CTRL_GET_CLOSE:
  459. ret = b->shutdown;
  460. break;
  461. case BIO_CTRL_SET_CLOSE:
  462. b->shutdown = (int)num;
  463. break;
  464. case BIO_CTRL_PENDING:
  465. case BIO_CTRL_WPENDING:
  466. ret = 0;
  467. break;
  468. case BIO_CTRL_FLUSH:
  469. break;
  470. case BIO_CTRL_DUP:
  471. {
  472. dbio = (BIO *)ptr;
  473. if (data->param_hostname)
  474. BIO_set_conn_hostname(dbio, data->param_hostname);
  475. if (data->param_service)
  476. BIO_set_conn_port(dbio, data->param_service);
  477. BIO_set_conn_ip_family(dbio, data->connect_family);
  478. BIO_set_conn_mode(dbio, data->connect_mode);
  479. /*
  480. * FIXME: the cast of the function seems unlikely to be a good
  481. * idea
  482. */
  483. (void)BIO_set_info_callback(dbio, data->info_callback);
  484. }
  485. break;
  486. case BIO_CTRL_SET_CALLBACK:
  487. ret = 0; /* use callback ctrl */
  488. break;
  489. case BIO_CTRL_GET_CALLBACK:
  490. {
  491. BIO_info_cb **fptr;
  492. fptr = (BIO_info_cb **)ptr;
  493. *fptr = data->info_callback;
  494. }
  495. break;
  496. case BIO_CTRL_EOF:
  497. ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
  498. break;
  499. # ifndef OPENSSL_NO_KTLS
  500. case BIO_CTRL_SET_KTLS:
  501. crypto_info = (ktls_crypto_info_t *)ptr;
  502. ret = ktls_start(b->num, crypto_info, num);
  503. if (ret)
  504. BIO_set_ktls_flag(b, num);
  505. break;
  506. case BIO_CTRL_GET_KTLS_SEND:
  507. return BIO_should_ktls_flag(b, 1) != 0;
  508. case BIO_CTRL_GET_KTLS_RECV:
  509. return BIO_should_ktls_flag(b, 0) != 0;
  510. case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
  511. BIO_set_ktls_ctrl_msg_flag(b);
  512. data->record_type = num;
  513. ret = 0;
  514. break;
  515. case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
  516. BIO_clear_ktls_ctrl_msg_flag(b);
  517. ret = 0;
  518. break;
  519. # endif
  520. default:
  521. ret = 0;
  522. break;
  523. }
  524. return ret;
  525. }
  526. static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  527. {
  528. long ret = 1;
  529. BIO_CONNECT *data;
  530. data = (BIO_CONNECT *)b->ptr;
  531. switch (cmd) {
  532. case BIO_CTRL_SET_CALLBACK:
  533. {
  534. data->info_callback = fp;
  535. }
  536. break;
  537. default:
  538. ret = 0;
  539. break;
  540. }
  541. return ret;
  542. }
  543. static int conn_puts(BIO *bp, const char *str)
  544. {
  545. int n, ret;
  546. n = strlen(str);
  547. ret = conn_write(bp, str, n);
  548. return ret;
  549. }
  550. BIO *BIO_new_connect(const char *str)
  551. {
  552. BIO *ret;
  553. ret = BIO_new(BIO_s_connect());
  554. if (ret == NULL)
  555. return NULL;
  556. if (BIO_set_conn_hostname(ret, str))
  557. return ret;
  558. BIO_free(ret);
  559. return NULL;
  560. }
  561. #endif