bio_lib.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  1. /*
  2. * Copyright 1995-2024 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 <openssl/crypto.h>
  13. #include "internal/numbers.h"
  14. #include "bio_local.h"
  15. /*
  16. * Helper macro for the callback to determine whether an operator expects a
  17. * len parameter or not
  18. */
  19. #define HAS_LEN_OPER(o) ((o) == BIO_CB_READ || (o) == BIO_CB_WRITE \
  20. || (o) == BIO_CB_GETS)
  21. #ifndef OPENSSL_NO_DEPRECATED_3_0
  22. # define HAS_CALLBACK(b) ((b)->callback != NULL || (b)->callback_ex != NULL)
  23. #else
  24. # define HAS_CALLBACK(b) ((b)->callback_ex != NULL)
  25. #endif
  26. /*
  27. * Helper function to work out whether to call the new style callback or the old
  28. * one, and translate between the two.
  29. *
  30. * This has a long return type for consistency with the old callback. Similarly
  31. * for the "long" used for "inret"
  32. */
  33. static long bio_call_callback(BIO *b, int oper, const char *argp, size_t len,
  34. int argi, long argl, long inret,
  35. size_t *processed)
  36. {
  37. long ret = inret;
  38. #ifndef OPENSSL_NO_DEPRECATED_3_0
  39. int bareoper;
  40. if (b->callback_ex != NULL)
  41. #endif
  42. return b->callback_ex(b, oper, argp, len, argi, argl, inret, processed);
  43. #ifndef OPENSSL_NO_DEPRECATED_3_0
  44. /* Strip off any BIO_CB_RETURN flag */
  45. bareoper = oper & ~BIO_CB_RETURN;
  46. /*
  47. * We have an old style callback, so we will have to do nasty casts and
  48. * check for overflows.
  49. */
  50. if (HAS_LEN_OPER(bareoper)) {
  51. /* In this case |len| is set, and should be used instead of |argi| */
  52. if (len > INT_MAX)
  53. return -1;
  54. argi = (int)len;
  55. }
  56. if (inret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
  57. if (*processed > INT_MAX)
  58. return -1;
  59. inret = *processed;
  60. }
  61. ret = b->callback(b, oper, argp, argi, argl, inret);
  62. if (ret > 0 && (oper & BIO_CB_RETURN) && bareoper != BIO_CB_CTRL) {
  63. *processed = (size_t)ret;
  64. ret = 1;
  65. }
  66. #endif
  67. return ret;
  68. }
  69. BIO *BIO_new_ex(OSSL_LIB_CTX *libctx, const BIO_METHOD *method)
  70. {
  71. BIO *bio = OPENSSL_zalloc(sizeof(*bio));
  72. if (bio == NULL)
  73. return NULL;
  74. bio->libctx = libctx;
  75. bio->method = method;
  76. bio->shutdown = 1;
  77. if (!CRYPTO_NEW_REF(&bio->references, 1))
  78. goto err;
  79. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data))
  80. goto err;
  81. if (method->create != NULL && !method->create(bio)) {
  82. ERR_raise(ERR_LIB_BIO, ERR_R_INIT_FAIL);
  83. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  84. goto err;
  85. }
  86. if (method->create == NULL)
  87. bio->init = 1;
  88. return bio;
  89. err:
  90. CRYPTO_FREE_REF(&bio->references);
  91. OPENSSL_free(bio);
  92. return NULL;
  93. }
  94. BIO *BIO_new(const BIO_METHOD *method)
  95. {
  96. return BIO_new_ex(NULL, method);
  97. }
  98. int BIO_free(BIO *a)
  99. {
  100. int ret;
  101. if (a == NULL)
  102. return 0;
  103. if (CRYPTO_DOWN_REF(&a->references, &ret) <= 0)
  104. return 0;
  105. REF_PRINT_COUNT("BIO", a);
  106. if (ret > 0)
  107. return 1;
  108. REF_ASSERT_ISNT(ret < 0);
  109. if (HAS_CALLBACK(a)) {
  110. ret = (int)bio_call_callback(a, BIO_CB_FREE, NULL, 0, 0, 0L, 1L, NULL);
  111. if (ret <= 0)
  112. return 0;
  113. }
  114. if ((a->method != NULL) && (a->method->destroy != NULL))
  115. a->method->destroy(a);
  116. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
  117. CRYPTO_FREE_REF(&a->references);
  118. OPENSSL_free(a);
  119. return 1;
  120. }
  121. void BIO_set_data(BIO *a, void *ptr)
  122. {
  123. a->ptr = ptr;
  124. }
  125. void *BIO_get_data(BIO *a)
  126. {
  127. return a->ptr;
  128. }
  129. void BIO_set_init(BIO *a, int init)
  130. {
  131. a->init = init;
  132. }
  133. int BIO_get_init(BIO *a)
  134. {
  135. return a->init;
  136. }
  137. void BIO_set_shutdown(BIO *a, int shut)
  138. {
  139. a->shutdown = shut;
  140. }
  141. int BIO_get_shutdown(BIO *a)
  142. {
  143. return a->shutdown;
  144. }
  145. void BIO_vfree(BIO *a)
  146. {
  147. BIO_free(a);
  148. }
  149. int BIO_up_ref(BIO *a)
  150. {
  151. int i;
  152. if (CRYPTO_UP_REF(&a->references, &i) <= 0)
  153. return 0;
  154. REF_PRINT_COUNT("BIO", a);
  155. REF_ASSERT_ISNT(i < 2);
  156. return i > 1;
  157. }
  158. void BIO_clear_flags(BIO *b, int flags)
  159. {
  160. b->flags &= ~flags;
  161. }
  162. int BIO_test_flags(const BIO *b, int flags)
  163. {
  164. return (b->flags & flags);
  165. }
  166. void BIO_set_flags(BIO *b, int flags)
  167. {
  168. b->flags |= flags;
  169. }
  170. #ifndef OPENSSL_NO_DEPRECATED_3_0
  171. BIO_callback_fn BIO_get_callback(const BIO *b)
  172. {
  173. return b->callback;
  174. }
  175. void BIO_set_callback(BIO *b, BIO_callback_fn cb)
  176. {
  177. b->callback = cb;
  178. }
  179. #endif
  180. BIO_callback_fn_ex BIO_get_callback_ex(const BIO *b)
  181. {
  182. return b->callback_ex;
  183. }
  184. void BIO_set_callback_ex(BIO *b, BIO_callback_fn_ex cb)
  185. {
  186. b->callback_ex = cb;
  187. }
  188. void BIO_set_callback_arg(BIO *b, char *arg)
  189. {
  190. b->cb_arg = arg;
  191. }
  192. char *BIO_get_callback_arg(const BIO *b)
  193. {
  194. return b->cb_arg;
  195. }
  196. const char *BIO_method_name(const BIO *b)
  197. {
  198. return b->method->name;
  199. }
  200. int BIO_method_type(const BIO *b)
  201. {
  202. return b->method->type;
  203. }
  204. /*
  205. * This is essentially the same as BIO_read_ex() except that it allows
  206. * 0 or a negative value to indicate failure (retryable or not) in the return.
  207. * This is for compatibility with the old style BIO_read(), where existing code
  208. * may make assumptions about the return value that it might get.
  209. */
  210. static int bio_read_intern(BIO *b, void *data, size_t dlen, size_t *readbytes)
  211. {
  212. int ret;
  213. if (b == NULL) {
  214. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  215. return -1;
  216. }
  217. if (b->method == NULL || b->method->bread == NULL) {
  218. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  219. return -2;
  220. }
  221. if (HAS_CALLBACK(b) &&
  222. ((ret = (int)bio_call_callback(b, BIO_CB_READ, data, dlen, 0, 0L, 1L,
  223. NULL)) <= 0))
  224. return ret;
  225. if (!b->init) {
  226. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  227. return -1;
  228. }
  229. ret = b->method->bread(b, data, dlen, readbytes);
  230. if (ret > 0)
  231. b->num_read += (uint64_t)*readbytes;
  232. if (HAS_CALLBACK(b))
  233. ret = (int)bio_call_callback(b, BIO_CB_READ | BIO_CB_RETURN, data,
  234. dlen, 0, 0L, ret, readbytes);
  235. /* Shouldn't happen */
  236. if (ret > 0 && *readbytes > dlen) {
  237. ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR);
  238. return -1;
  239. }
  240. return ret;
  241. }
  242. int BIO_read(BIO *b, void *data, int dlen)
  243. {
  244. size_t readbytes;
  245. int ret;
  246. if (dlen < 0)
  247. return 0;
  248. ret = bio_read_intern(b, data, (size_t)dlen, &readbytes);
  249. if (ret > 0) {
  250. /* *readbytes should always be <= dlen */
  251. ret = (int)readbytes;
  252. }
  253. return ret;
  254. }
  255. int BIO_read_ex(BIO *b, void *data, size_t dlen, size_t *readbytes)
  256. {
  257. return bio_read_intern(b, data, dlen, readbytes) > 0;
  258. }
  259. static int bio_write_intern(BIO *b, const void *data, size_t dlen,
  260. size_t *written)
  261. {
  262. size_t local_written;
  263. int ret;
  264. if (written != NULL)
  265. *written = 0;
  266. /*
  267. * b == NULL is not an error but just means that zero bytes are written.
  268. * Do not raise an error here.
  269. */
  270. if (b == NULL)
  271. return 0;
  272. if (b->method == NULL || b->method->bwrite == NULL) {
  273. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  274. return -2;
  275. }
  276. if (HAS_CALLBACK(b) &&
  277. ((ret = (int)bio_call_callback(b, BIO_CB_WRITE, data, dlen, 0, 0L, 1L,
  278. NULL)) <= 0))
  279. return ret;
  280. if (!b->init) {
  281. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  282. return -1;
  283. }
  284. ret = b->method->bwrite(b, data, dlen, &local_written);
  285. if (ret > 0)
  286. b->num_write += (uint64_t)local_written;
  287. if (HAS_CALLBACK(b))
  288. ret = (int)bio_call_callback(b, BIO_CB_WRITE | BIO_CB_RETURN, data,
  289. dlen, 0, 0L, ret, &local_written);
  290. if (written != NULL)
  291. *written = local_written;
  292. return ret;
  293. }
  294. int BIO_write(BIO *b, const void *data, int dlen)
  295. {
  296. size_t written;
  297. int ret;
  298. if (dlen <= 0)
  299. return 0;
  300. ret = bio_write_intern(b, data, (size_t)dlen, &written);
  301. if (ret > 0) {
  302. /* written should always be <= dlen */
  303. ret = (int)written;
  304. }
  305. return ret;
  306. }
  307. int BIO_write_ex(BIO *b, const void *data, size_t dlen, size_t *written)
  308. {
  309. return bio_write_intern(b, data, dlen, written) > 0
  310. || (b != NULL && dlen == 0); /* order is important for *written */
  311. }
  312. int BIO_sendmmsg(BIO *b, BIO_MSG *msg,
  313. size_t stride, size_t num_msg, uint64_t flags,
  314. size_t *msgs_processed)
  315. {
  316. size_t ret;
  317. BIO_MMSG_CB_ARGS args;
  318. if (b == NULL) {
  319. *msgs_processed = 0;
  320. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  321. return 0;
  322. }
  323. if (b->method == NULL || b->method->bsendmmsg == NULL) {
  324. *msgs_processed = 0;
  325. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  326. return 0;
  327. }
  328. if (HAS_CALLBACK(b)) {
  329. args.msg = msg;
  330. args.stride = stride;
  331. args.num_msg = num_msg;
  332. args.flags = flags;
  333. args.msgs_processed = msgs_processed;
  334. ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG, (void *)&args,
  335. 0, 0, 0, 1, NULL);
  336. if (ret <= 0)
  337. return 0;
  338. }
  339. if (!b->init) {
  340. *msgs_processed = 0;
  341. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  342. return 0;
  343. }
  344. ret = b->method->bsendmmsg(b, msg, stride, num_msg, flags, msgs_processed);
  345. if (HAS_CALLBACK(b))
  346. ret = (size_t)bio_call_callback(b, BIO_CB_SENDMMSG | BIO_CB_RETURN,
  347. (void *)&args, ret, 0, 0, ret, NULL);
  348. return ret;
  349. }
  350. int BIO_recvmmsg(BIO *b, BIO_MSG *msg,
  351. size_t stride, size_t num_msg, uint64_t flags,
  352. size_t *msgs_processed)
  353. {
  354. size_t ret;
  355. BIO_MMSG_CB_ARGS args;
  356. if (b == NULL) {
  357. *msgs_processed = 0;
  358. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  359. return 0;
  360. }
  361. if (b->method == NULL || b->method->brecvmmsg == NULL) {
  362. *msgs_processed = 0;
  363. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  364. return 0;
  365. }
  366. if (HAS_CALLBACK(b)) {
  367. args.msg = msg;
  368. args.stride = stride;
  369. args.num_msg = num_msg;
  370. args.flags = flags;
  371. args.msgs_processed = msgs_processed;
  372. ret = bio_call_callback(b, BIO_CB_RECVMMSG, (void *)&args,
  373. 0, 0, 0, 1, NULL);
  374. if (ret <= 0)
  375. return 0;
  376. }
  377. if (!b->init) {
  378. *msgs_processed = 0;
  379. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  380. return 0;
  381. }
  382. ret = b->method->brecvmmsg(b, msg, stride, num_msg, flags, msgs_processed);
  383. if (HAS_CALLBACK(b))
  384. ret = (size_t)bio_call_callback(b, BIO_CB_RECVMMSG | BIO_CB_RETURN,
  385. (void *)&args, ret, 0, 0, ret, NULL);
  386. return ret;
  387. }
  388. int BIO_get_rpoll_descriptor(BIO *b, BIO_POLL_DESCRIPTOR *desc)
  389. {
  390. return BIO_ctrl(b, BIO_CTRL_GET_RPOLL_DESCRIPTOR, 0, desc);
  391. }
  392. int BIO_get_wpoll_descriptor(BIO *b, BIO_POLL_DESCRIPTOR *desc)
  393. {
  394. return BIO_ctrl(b, BIO_CTRL_GET_WPOLL_DESCRIPTOR, 0, desc);
  395. }
  396. int BIO_puts(BIO *b, const char *buf)
  397. {
  398. int ret;
  399. size_t written = 0;
  400. if (b == NULL) {
  401. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  402. return -1;
  403. }
  404. if (b->method == NULL || b->method->bputs == NULL) {
  405. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  406. return -2;
  407. }
  408. if (HAS_CALLBACK(b)) {
  409. ret = (int)bio_call_callback(b, BIO_CB_PUTS, buf, 0, 0, 0L, 1L, NULL);
  410. if (ret <= 0)
  411. return ret;
  412. }
  413. if (!b->init) {
  414. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  415. return -1;
  416. }
  417. ret = b->method->bputs(b, buf);
  418. if (ret > 0) {
  419. b->num_write += (uint64_t)ret;
  420. written = ret;
  421. ret = 1;
  422. }
  423. if (HAS_CALLBACK(b))
  424. ret = (int)bio_call_callback(b, BIO_CB_PUTS | BIO_CB_RETURN, buf, 0, 0,
  425. 0L, ret, &written);
  426. if (ret > 0) {
  427. if (written > INT_MAX) {
  428. ERR_raise(ERR_LIB_BIO, BIO_R_LENGTH_TOO_LONG);
  429. ret = -1;
  430. } else {
  431. ret = (int)written;
  432. }
  433. }
  434. return ret;
  435. }
  436. int BIO_gets(BIO *b, char *buf, int size)
  437. {
  438. int ret;
  439. size_t readbytes = 0;
  440. if (b == NULL) {
  441. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  442. return -1;
  443. }
  444. if (b->method == NULL || b->method->bgets == NULL) {
  445. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  446. return -2;
  447. }
  448. if (size < 0) {
  449. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
  450. return -1;
  451. }
  452. if (HAS_CALLBACK(b)) {
  453. ret = (int)bio_call_callback(b, BIO_CB_GETS, buf, size, 0, 0L, 1, NULL);
  454. if (ret <= 0)
  455. return ret;
  456. }
  457. if (!b->init) {
  458. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  459. return -1;
  460. }
  461. ret = b->method->bgets(b, buf, size);
  462. if (ret > 0) {
  463. readbytes = ret;
  464. ret = 1;
  465. }
  466. if (HAS_CALLBACK(b))
  467. ret = (int)bio_call_callback(b, BIO_CB_GETS | BIO_CB_RETURN, buf, size,
  468. 0, 0L, ret, &readbytes);
  469. if (ret > 0) {
  470. /* Shouldn't happen */
  471. if (readbytes > (size_t)size)
  472. ret = -1;
  473. else
  474. ret = (int)readbytes;
  475. }
  476. return ret;
  477. }
  478. int BIO_get_line(BIO *bio, char *buf, int size)
  479. {
  480. int ret = 0;
  481. char *ptr = buf;
  482. if (buf == NULL) {
  483. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  484. return -1;
  485. }
  486. if (size <= 0) {
  487. ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
  488. return -1;
  489. }
  490. *buf = '\0';
  491. if (bio == NULL) {
  492. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  493. return -1;
  494. }
  495. if (!bio->init) {
  496. ERR_raise(ERR_LIB_BIO, BIO_R_UNINITIALIZED);
  497. return -1;
  498. }
  499. while (size-- > 1 && (ret = BIO_read(bio, ptr, 1)) > 0)
  500. if (*ptr++ == '\n')
  501. break;
  502. *ptr = '\0';
  503. return ret > 0 || BIO_eof(bio) ? ptr - buf : ret;
  504. }
  505. int BIO_indent(BIO *b, int indent, int max)
  506. {
  507. if (indent < 0)
  508. indent = 0;
  509. if (indent > max)
  510. indent = max;
  511. while (indent--)
  512. if (BIO_puts(b, " ") != 1)
  513. return 0;
  514. return 1;
  515. }
  516. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
  517. {
  518. int i;
  519. i = iarg;
  520. return BIO_ctrl(b, cmd, larg, (char *)&i);
  521. }
  522. void *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
  523. {
  524. void *p = NULL;
  525. if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
  526. return NULL;
  527. else
  528. return p;
  529. }
  530. long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
  531. {
  532. long ret;
  533. if (b == NULL)
  534. return -1;
  535. if (b->method == NULL || b->method->ctrl == NULL) {
  536. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  537. return -2;
  538. }
  539. if (HAS_CALLBACK(b)) {
  540. ret = bio_call_callback(b, BIO_CB_CTRL, parg, 0, cmd, larg, 1L, NULL);
  541. if (ret <= 0)
  542. return ret;
  543. }
  544. ret = b->method->ctrl(b, cmd, larg, parg);
  545. if (HAS_CALLBACK(b))
  546. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, 0, cmd,
  547. larg, ret, NULL);
  548. return ret;
  549. }
  550. long BIO_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  551. {
  552. long ret;
  553. if (b == NULL)
  554. return -2;
  555. if (b->method == NULL || b->method->callback_ctrl == NULL
  556. || cmd != BIO_CTRL_SET_CALLBACK) {
  557. ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_METHOD);
  558. return -2;
  559. }
  560. if (HAS_CALLBACK(b)) {
  561. ret = bio_call_callback(b, BIO_CB_CTRL, (void *)&fp, 0, cmd, 0, 1L,
  562. NULL);
  563. if (ret <= 0)
  564. return ret;
  565. }
  566. ret = b->method->callback_ctrl(b, cmd, fp);
  567. if (HAS_CALLBACK(b))
  568. ret = bio_call_callback(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, 0,
  569. cmd, 0, ret, NULL);
  570. return ret;
  571. }
  572. /*
  573. * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
  574. * do; but those macros have inappropriate return type, and for interfacing
  575. * from other programming languages, C macros aren't much of a help anyway.
  576. */
  577. size_t BIO_ctrl_pending(BIO *bio)
  578. {
  579. long ret = BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
  580. if (ret < 0)
  581. ret = 0;
  582. #if LONG_MAX > SIZE_MAX
  583. if (ret > SIZE_MAX)
  584. ret = SIZE_MAX;
  585. #endif
  586. return (size_t)ret;
  587. }
  588. size_t BIO_ctrl_wpending(BIO *bio)
  589. {
  590. long ret = BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
  591. if (ret < 0)
  592. ret = 0;
  593. #if LONG_MAX > SIZE_MAX
  594. if (ret > SIZE_MAX)
  595. ret = SIZE_MAX;
  596. #endif
  597. return (size_t)ret;
  598. }
  599. /* put the 'bio' on the end of b's list of operators */
  600. BIO *BIO_push(BIO *b, BIO *bio)
  601. {
  602. BIO *lb;
  603. if (b == NULL)
  604. return bio;
  605. lb = b;
  606. while (lb->next_bio != NULL)
  607. lb = lb->next_bio;
  608. lb->next_bio = bio;
  609. if (bio != NULL)
  610. bio->prev_bio = lb;
  611. /* called to do internal processing */
  612. BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
  613. return b;
  614. }
  615. /* Remove the first and return the rest */
  616. BIO *BIO_pop(BIO *b)
  617. {
  618. BIO *ret;
  619. if (b == NULL)
  620. return NULL;
  621. ret = b->next_bio;
  622. BIO_ctrl(b, BIO_CTRL_POP, 0, b);
  623. if (b->prev_bio != NULL)
  624. b->prev_bio->next_bio = b->next_bio;
  625. if (b->next_bio != NULL)
  626. b->next_bio->prev_bio = b->prev_bio;
  627. b->next_bio = NULL;
  628. b->prev_bio = NULL;
  629. return ret;
  630. }
  631. BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
  632. {
  633. BIO *b, *last;
  634. b = last = bio;
  635. for (;;) {
  636. if (!BIO_should_retry(b))
  637. break;
  638. last = b;
  639. b = b->next_bio;
  640. if (b == NULL)
  641. break;
  642. }
  643. if (reason != NULL)
  644. *reason = last->retry_reason;
  645. return last;
  646. }
  647. int BIO_get_retry_reason(BIO *bio)
  648. {
  649. return bio->retry_reason;
  650. }
  651. void BIO_set_retry_reason(BIO *bio, int reason)
  652. {
  653. bio->retry_reason = reason;
  654. }
  655. BIO *BIO_find_type(BIO *bio, int type)
  656. {
  657. int mt, mask;
  658. if (bio == NULL) {
  659. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  660. return NULL;
  661. }
  662. mask = type & BIO_TYPE_MASK;
  663. do {
  664. if (bio->method != NULL) {
  665. mt = bio->method->type;
  666. if (!mask) {
  667. if (mt & type)
  668. return bio;
  669. } else if (mt == type) {
  670. return bio;
  671. }
  672. }
  673. bio = bio->next_bio;
  674. } while (bio != NULL);
  675. return NULL;
  676. }
  677. BIO *BIO_next(BIO *b)
  678. {
  679. if (b == NULL)
  680. return NULL;
  681. return b->next_bio;
  682. }
  683. void BIO_set_next(BIO *b, BIO *next)
  684. {
  685. b->next_bio = next;
  686. }
  687. void BIO_free_all(BIO *bio)
  688. {
  689. BIO *b;
  690. int ref;
  691. while (bio != NULL) {
  692. b = bio;
  693. CRYPTO_GET_REF(&b->references, &ref);
  694. bio = bio->next_bio;
  695. BIO_free(b);
  696. /* Since ref count > 1, don't free anyone else. */
  697. if (ref > 1)
  698. break;
  699. }
  700. }
  701. BIO *BIO_dup_chain(BIO *in)
  702. {
  703. BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
  704. for (bio = in; bio != NULL; bio = bio->next_bio) {
  705. if ((new_bio = BIO_new(bio->method)) == NULL)
  706. goto err;
  707. #ifndef OPENSSL_NO_DEPRECATED_3_0
  708. new_bio->callback = bio->callback;
  709. #endif
  710. new_bio->callback_ex = bio->callback_ex;
  711. new_bio->cb_arg = bio->cb_arg;
  712. new_bio->init = bio->init;
  713. new_bio->shutdown = bio->shutdown;
  714. new_bio->flags = bio->flags;
  715. /* This will let SSL_s_sock() work with stdin/stdout */
  716. new_bio->num = bio->num;
  717. if (BIO_dup_state(bio, (char *)new_bio) <= 0) {
  718. BIO_free(new_bio);
  719. goto err;
  720. }
  721. /* copy app data */
  722. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
  723. &bio->ex_data)) {
  724. BIO_free(new_bio);
  725. goto err;
  726. }
  727. if (ret == NULL) {
  728. eoc = new_bio;
  729. ret = eoc;
  730. } else {
  731. BIO_push(eoc, new_bio);
  732. eoc = new_bio;
  733. }
  734. }
  735. return ret;
  736. err:
  737. BIO_free_all(ret);
  738. return NULL;
  739. }
  740. void BIO_copy_next_retry(BIO *b)
  741. {
  742. BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
  743. b->retry_reason = b->next_bio->retry_reason;
  744. }
  745. int BIO_set_ex_data(BIO *bio, int idx, void *data)
  746. {
  747. return CRYPTO_set_ex_data(&(bio->ex_data), idx, data);
  748. }
  749. void *BIO_get_ex_data(const BIO *bio, int idx)
  750. {
  751. return CRYPTO_get_ex_data(&(bio->ex_data), idx);
  752. }
  753. uint64_t BIO_number_read(BIO *bio)
  754. {
  755. if (bio)
  756. return bio->num_read;
  757. return 0;
  758. }
  759. uint64_t BIO_number_written(BIO *bio)
  760. {
  761. if (bio)
  762. return bio->num_write;
  763. return 0;
  764. }
  765. void bio_free_ex_data(BIO *bio)
  766. {
  767. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  768. }
  769. void bio_cleanup(void)
  770. {
  771. #ifndef OPENSSL_NO_SOCK
  772. bio_sock_cleanup_int();
  773. CRYPTO_THREAD_lock_free(bio_lookup_lock);
  774. bio_lookup_lock = NULL;
  775. #endif
  776. CRYPTO_FREE_REF(&bio_type_count);
  777. }
  778. /* Internal variant of the below BIO_wait() not calling ERR_raise(...) */
  779. static int bio_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  780. {
  781. #ifndef OPENSSL_NO_SOCK
  782. int fd;
  783. #endif
  784. long sec_diff;
  785. if (max_time == 0) /* no timeout */
  786. return 1;
  787. #ifndef OPENSSL_NO_SOCK
  788. if (BIO_get_fd(bio, &fd) > 0) {
  789. int ret = BIO_socket_wait(fd, BIO_should_read(bio), max_time);
  790. if (ret != -1)
  791. return ret;
  792. }
  793. #endif
  794. /* fall back to polling since no sockets are available */
  795. sec_diff = (long)(max_time - time(NULL)); /* might overflow */
  796. if (sec_diff < 0)
  797. return 0; /* clearly timeout */
  798. /* now take a nap at most the given number of milliseconds */
  799. if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
  800. if (nap_milliseconds > 1000)
  801. nap_milliseconds = 1000;
  802. } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
  803. if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
  804. nap_milliseconds = (unsigned int)sec_diff * 1000;
  805. }
  806. OSSL_sleep(nap_milliseconds);
  807. return 1;
  808. }
  809. /*-
  810. * Wait on (typically socket-based) BIO at most until max_time.
  811. * Succeed immediately if max_time == 0.
  812. * If sockets are not available support polling: succeed after waiting at most
  813. * the number of nap_milliseconds in order to avoid a tight busy loop.
  814. * Call ERR_raise(ERR_LIB_BIO, ...) on timeout or error.
  815. * Returns -1 on error, 0 on timeout, and 1 on success.
  816. */
  817. int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  818. {
  819. int rv = bio_wait(bio, max_time, nap_milliseconds);
  820. if (rv <= 0)
  821. ERR_raise(ERR_LIB_BIO,
  822. rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
  823. return rv;
  824. }
  825. /*
  826. * Connect via given BIO using BIO_do_connect() until success/timeout/error.
  827. * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
  828. * For non-blocking and potentially even non-socket BIOs perform polling with
  829. * the given density: between polls sleep nap_milliseconds using BIO_wait()
  830. * in order to avoid a tight busy loop.
  831. * Returns -1 on error, 0 on timeout, and 1 on success.
  832. */
  833. int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
  834. {
  835. int blocking = timeout <= 0;
  836. time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
  837. int rv;
  838. if (bio == NULL) {
  839. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  840. return -1;
  841. }
  842. if (nap_milliseconds < 0)
  843. nap_milliseconds = 100;
  844. BIO_set_nbio(bio, !blocking);
  845. retry:
  846. ERR_set_mark();
  847. rv = BIO_do_connect(bio);
  848. if (rv <= 0) { /* could be timeout or retryable error or fatal error */
  849. int err = ERR_peek_last_error();
  850. int reason = ERR_GET_REASON(err);
  851. int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
  852. if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
  853. switch (reason) {
  854. case ERR_R_SYS_LIB:
  855. /*
  856. * likely retryable system error occurred, which may be
  857. * EAGAIN (resource temporarily unavailable) some 40 secs after
  858. * calling getaddrinfo(): Temporary failure in name resolution
  859. * or a premature ETIMEDOUT, some 30 seconds after connect()
  860. */
  861. case BIO_R_CONNECT_ERROR:
  862. case BIO_R_NBIO_CONNECT_ERROR:
  863. /* some likely retryable connection error occurred */
  864. (void)BIO_reset(bio); /* often needed to avoid retry failure */
  865. do_retry = 1;
  866. break;
  867. default:
  868. break;
  869. }
  870. }
  871. if (timeout >= 0 && do_retry) {
  872. ERR_pop_to_mark();
  873. /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
  874. rv = bio_wait(bio, max_time, nap_milliseconds);
  875. if (rv > 0)
  876. goto retry;
  877. ERR_raise(ERR_LIB_BIO,
  878. rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
  879. } else {
  880. ERR_clear_last_mark();
  881. rv = -1;
  882. if (err == 0) /* missing error queue entry */
  883. /* workaround: general error */
  884. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  885. }
  886. } else {
  887. ERR_clear_last_mark();
  888. }
  889. return rv;
  890. }