bss_conn.c 20 KB

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