bio_lib.c 25 KB

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