bss_conn.c 18 KB

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