bio_lib.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078
  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 <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 & 0xff;
  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 && fd < FD_SETSIZE)
  789. return BIO_socket_wait(fd, BIO_should_read(bio), max_time);
  790. #endif
  791. /* fall back to polling since no sockets are available */
  792. sec_diff = (long)(max_time - time(NULL)); /* might overflow */
  793. if (sec_diff < 0)
  794. return 0; /* clearly timeout */
  795. /* now take a nap at most the given number of milliseconds */
  796. if (sec_diff == 0) { /* we are below the 1 seconds resolution of max_time */
  797. if (nap_milliseconds > 1000)
  798. nap_milliseconds = 1000;
  799. } else { /* for sec_diff > 0, take min(sec_diff * 1000, nap_milliseconds) */
  800. if ((unsigned long)sec_diff * 1000 < nap_milliseconds)
  801. nap_milliseconds = (unsigned int)sec_diff * 1000;
  802. }
  803. OSSL_sleep(nap_milliseconds);
  804. return 1;
  805. }
  806. /*-
  807. * Wait on (typically socket-based) BIO at most until max_time.
  808. * Succeed immediately if max_time == 0.
  809. * If sockets are not available support polling: succeed after waiting at most
  810. * the number of nap_milliseconds in order to avoid a tight busy loop.
  811. * Call ERR_raise(ERR_LIB_BIO, ...) on timeout or error.
  812. * Returns -1 on error, 0 on timeout, and 1 on success.
  813. */
  814. int BIO_wait(BIO *bio, time_t max_time, unsigned int nap_milliseconds)
  815. {
  816. int rv = bio_wait(bio, max_time, nap_milliseconds);
  817. if (rv <= 0)
  818. ERR_raise(ERR_LIB_BIO,
  819. rv == 0 ? BIO_R_TRANSFER_TIMEOUT : BIO_R_TRANSFER_ERROR);
  820. return rv;
  821. }
  822. /*
  823. * Connect via given BIO using BIO_do_connect() until success/timeout/error.
  824. * Parameter timeout == 0 means no timeout, < 0 means exactly one try.
  825. * For non-blocking and potentially even non-socket BIOs perform polling with
  826. * the given density: between polls sleep nap_milliseconds using BIO_wait()
  827. * in order to avoid a tight busy loop.
  828. * Returns -1 on error, 0 on timeout, and 1 on success.
  829. */
  830. int BIO_do_connect_retry(BIO *bio, int timeout, int nap_milliseconds)
  831. {
  832. int blocking = timeout <= 0;
  833. time_t max_time = timeout > 0 ? time(NULL) + timeout : 0;
  834. int rv;
  835. if (bio == NULL) {
  836. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  837. return -1;
  838. }
  839. if (nap_milliseconds < 0)
  840. nap_milliseconds = 100;
  841. BIO_set_nbio(bio, !blocking);
  842. retry:
  843. ERR_set_mark();
  844. rv = BIO_do_connect(bio);
  845. if (rv <= 0) { /* could be timeout or retryable error or fatal error */
  846. int err = ERR_peek_last_error();
  847. int reason = ERR_GET_REASON(err);
  848. int do_retry = BIO_should_retry(bio); /* may be 1 only if !blocking */
  849. if (ERR_GET_LIB(err) == ERR_LIB_BIO) {
  850. switch (reason) {
  851. case ERR_R_SYS_LIB:
  852. /*
  853. * likely retryable system error occurred, which may be
  854. * EAGAIN (resource temporarily unavailable) some 40 secs after
  855. * calling getaddrinfo(): Temporary failure in name resolution
  856. * or a premature ETIMEDOUT, some 30 seconds after connect()
  857. */
  858. case BIO_R_CONNECT_ERROR:
  859. case BIO_R_NBIO_CONNECT_ERROR:
  860. /* some likely retryable connection error occurred */
  861. (void)BIO_reset(bio); /* often needed to avoid retry failure */
  862. do_retry = 1;
  863. break;
  864. default:
  865. break;
  866. }
  867. }
  868. if (timeout >= 0 && do_retry) {
  869. ERR_pop_to_mark();
  870. /* will not actually wait if timeout == 0 (i.e., blocking BIO): */
  871. rv = bio_wait(bio, max_time, nap_milliseconds);
  872. if (rv > 0)
  873. goto retry;
  874. ERR_raise(ERR_LIB_BIO,
  875. rv == 0 ? BIO_R_CONNECT_TIMEOUT : BIO_R_CONNECT_ERROR);
  876. } else {
  877. ERR_clear_last_mark();
  878. rv = -1;
  879. if (err == 0) /* missing error queue entry */
  880. /* workaround: general error */
  881. ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
  882. }
  883. } else {
  884. ERR_clear_last_mark();
  885. }
  886. return rv;
  887. }