bio_lib.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597
  1. /* crypto/bio/bio_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include <openssl/crypto.h>
  61. #include "cryptlib.h"
  62. #include <openssl/bio.h>
  63. #include <openssl/stack.h>
  64. BIO *BIO_new(BIO_METHOD *method)
  65. {
  66. BIO *ret = NULL;
  67. ret = (BIO *)OPENSSL_malloc(sizeof(BIO));
  68. if (ret == NULL) {
  69. BIOerr(BIO_F_BIO_NEW, ERR_R_MALLOC_FAILURE);
  70. return (NULL);
  71. }
  72. if (!BIO_set(ret, method)) {
  73. OPENSSL_free(ret);
  74. ret = NULL;
  75. }
  76. return (ret);
  77. }
  78. int BIO_set(BIO *bio, BIO_METHOD *method)
  79. {
  80. bio->method = method;
  81. bio->callback = NULL;
  82. bio->cb_arg = NULL;
  83. bio->init = 0;
  84. bio->shutdown = 1;
  85. bio->flags = 0;
  86. bio->retry_reason = 0;
  87. bio->num = 0;
  88. bio->ptr = NULL;
  89. bio->prev_bio = NULL;
  90. bio->next_bio = NULL;
  91. bio->references = 1;
  92. bio->num_read = 0L;
  93. bio->num_write = 0L;
  94. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  95. if (method->create != NULL)
  96. if (!method->create(bio)) {
  97. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  98. return (0);
  99. }
  100. return (1);
  101. }
  102. int BIO_free(BIO *a)
  103. {
  104. int i;
  105. if (a == NULL)
  106. return (0);
  107. i = CRYPTO_add(&a->references, -1, CRYPTO_LOCK_BIO);
  108. #ifdef REF_PRINT
  109. REF_PRINT("BIO", a);
  110. #endif
  111. if (i > 0)
  112. return (1);
  113. #ifdef REF_CHECK
  114. if (i < 0) {
  115. fprintf(stderr, "BIO_free, bad reference count\n");
  116. abort();
  117. }
  118. #endif
  119. if ((a->callback != NULL) &&
  120. ((i = (int)a->callback(a, BIO_CB_FREE, NULL, 0, 0L, 1L)) <= 0))
  121. return (i);
  122. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, a, &a->ex_data);
  123. if ((a->method != NULL) && (a->method->destroy != NULL))
  124. a->method->destroy(a);
  125. OPENSSL_free(a);
  126. return (1);
  127. }
  128. void BIO_vfree(BIO *a)
  129. {
  130. BIO_free(a);
  131. }
  132. void BIO_clear_flags(BIO *b, int flags)
  133. {
  134. b->flags &= ~flags;
  135. }
  136. int BIO_test_flags(const BIO *b, int flags)
  137. {
  138. return (b->flags & flags);
  139. }
  140. void BIO_set_flags(BIO *b, int flags)
  141. {
  142. b->flags |= flags;
  143. }
  144. long (*BIO_get_callback(const BIO *b)) (struct bio_st *, int, const char *,
  145. int, long, long) {
  146. return b->callback;
  147. }
  148. void BIO_set_callback(BIO *b,
  149. long (*cb) (struct bio_st *, int, const char *, int,
  150. long, long))
  151. {
  152. b->callback = cb;
  153. }
  154. void BIO_set_callback_arg(BIO *b, char *arg)
  155. {
  156. b->cb_arg = arg;
  157. }
  158. char *BIO_get_callback_arg(const BIO *b)
  159. {
  160. return b->cb_arg;
  161. }
  162. const char *BIO_method_name(const BIO *b)
  163. {
  164. return b->method->name;
  165. }
  166. int BIO_method_type(const BIO *b)
  167. {
  168. return b->method->type;
  169. }
  170. int BIO_read(BIO *b, void *out, int outl)
  171. {
  172. int i;
  173. long (*cb) (BIO *, int, const char *, int, long, long);
  174. if ((b == NULL) || (b->method == NULL) || (b->method->bread == NULL)) {
  175. BIOerr(BIO_F_BIO_READ, BIO_R_UNSUPPORTED_METHOD);
  176. return (-2);
  177. }
  178. cb = b->callback;
  179. if ((cb != NULL) &&
  180. ((i = (int)cb(b, BIO_CB_READ, out, outl, 0L, 1L)) <= 0))
  181. return (i);
  182. if (!b->init) {
  183. BIOerr(BIO_F_BIO_READ, BIO_R_UNINITIALIZED);
  184. return (-2);
  185. }
  186. i = b->method->bread(b, out, outl);
  187. if (i > 0)
  188. b->num_read += (unsigned long)i;
  189. if (cb != NULL)
  190. i = (int)cb(b, BIO_CB_READ | BIO_CB_RETURN, out, outl, 0L, (long)i);
  191. return (i);
  192. }
  193. int BIO_write(BIO *b, const void *in, int inl)
  194. {
  195. int i;
  196. long (*cb) (BIO *, int, const char *, int, long, long);
  197. if (b == NULL)
  198. return (0);
  199. cb = b->callback;
  200. if ((b->method == NULL) || (b->method->bwrite == NULL)) {
  201. BIOerr(BIO_F_BIO_WRITE, BIO_R_UNSUPPORTED_METHOD);
  202. return (-2);
  203. }
  204. if ((cb != NULL) &&
  205. ((i = (int)cb(b, BIO_CB_WRITE, in, inl, 0L, 1L)) <= 0))
  206. return (i);
  207. if (!b->init) {
  208. BIOerr(BIO_F_BIO_WRITE, BIO_R_UNINITIALIZED);
  209. return (-2);
  210. }
  211. i = b->method->bwrite(b, in, inl);
  212. if (i > 0)
  213. b->num_write += (unsigned long)i;
  214. if (cb != NULL)
  215. i = (int)cb(b, BIO_CB_WRITE | BIO_CB_RETURN, in, inl, 0L, (long)i);
  216. return (i);
  217. }
  218. int BIO_puts(BIO *b, const char *in)
  219. {
  220. int i;
  221. long (*cb) (BIO *, int, const char *, int, long, long);
  222. if ((b == NULL) || (b->method == NULL) || (b->method->bputs == NULL)) {
  223. BIOerr(BIO_F_BIO_PUTS, BIO_R_UNSUPPORTED_METHOD);
  224. return (-2);
  225. }
  226. cb = b->callback;
  227. if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_PUTS, in, 0, 0L, 1L)) <= 0))
  228. return (i);
  229. if (!b->init) {
  230. BIOerr(BIO_F_BIO_PUTS, BIO_R_UNINITIALIZED);
  231. return (-2);
  232. }
  233. i = b->method->bputs(b, in);
  234. if (i > 0)
  235. b->num_write += (unsigned long)i;
  236. if (cb != NULL)
  237. i = (int)cb(b, BIO_CB_PUTS | BIO_CB_RETURN, in, 0, 0L, (long)i);
  238. return (i);
  239. }
  240. int BIO_gets(BIO *b, char *in, int inl)
  241. {
  242. int i;
  243. long (*cb) (BIO *, int, const char *, int, long, long);
  244. if ((b == NULL) || (b->method == NULL) || (b->method->bgets == NULL)) {
  245. BIOerr(BIO_F_BIO_GETS, BIO_R_UNSUPPORTED_METHOD);
  246. return (-2);
  247. }
  248. cb = b->callback;
  249. if ((cb != NULL) && ((i = (int)cb(b, BIO_CB_GETS, in, inl, 0L, 1L)) <= 0))
  250. return (i);
  251. if (!b->init) {
  252. BIOerr(BIO_F_BIO_GETS, BIO_R_UNINITIALIZED);
  253. return (-2);
  254. }
  255. i = b->method->bgets(b, in, inl);
  256. if (cb != NULL)
  257. i = (int)cb(b, BIO_CB_GETS | BIO_CB_RETURN, in, inl, 0L, (long)i);
  258. return (i);
  259. }
  260. int BIO_indent(BIO *b, int indent, int max)
  261. {
  262. if (indent < 0)
  263. indent = 0;
  264. if (indent > max)
  265. indent = max;
  266. while (indent--)
  267. if (BIO_puts(b, " ") != 1)
  268. return 0;
  269. return 1;
  270. }
  271. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg)
  272. {
  273. int i;
  274. i = iarg;
  275. return (BIO_ctrl(b, cmd, larg, (char *)&i));
  276. }
  277. char *BIO_ptr_ctrl(BIO *b, int cmd, long larg)
  278. {
  279. char *p = NULL;
  280. if (BIO_ctrl(b, cmd, larg, (char *)&p) <= 0)
  281. return (NULL);
  282. else
  283. return (p);
  284. }
  285. long BIO_ctrl(BIO *b, int cmd, long larg, void *parg)
  286. {
  287. long ret;
  288. long (*cb) (BIO *, int, const char *, int, long, long);
  289. if (b == NULL)
  290. return (0);
  291. if ((b->method == NULL) || (b->method->ctrl == NULL)) {
  292. BIOerr(BIO_F_BIO_CTRL, BIO_R_UNSUPPORTED_METHOD);
  293. return (-2);
  294. }
  295. cb = b->callback;
  296. if ((cb != NULL) &&
  297. ((ret = cb(b, BIO_CB_CTRL, parg, cmd, larg, 1L)) <= 0))
  298. return (ret);
  299. ret = b->method->ctrl(b, cmd, larg, parg);
  300. if (cb != NULL)
  301. ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
  302. return (ret);
  303. }
  304. long BIO_callback_ctrl(BIO *b, int cmd,
  305. void (*fp) (struct bio_st *, int, const char *, int,
  306. long, long))
  307. {
  308. long ret;
  309. long (*cb) (BIO *, int, const char *, int, long, long);
  310. if (b == NULL)
  311. return (0);
  312. if ((b->method == NULL) || (b->method->callback_ctrl == NULL)) {
  313. BIOerr(BIO_F_BIO_CALLBACK_CTRL, BIO_R_UNSUPPORTED_METHOD);
  314. return (-2);
  315. }
  316. cb = b->callback;
  317. if ((cb != NULL) &&
  318. ((ret = cb(b, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L)) <= 0))
  319. return (ret);
  320. ret = b->method->callback_ctrl(b, cmd, fp);
  321. if (cb != NULL)
  322. ret = cb(b, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
  323. return (ret);
  324. }
  325. /*
  326. * It is unfortunate to duplicate in functions what the BIO_(w)pending macros
  327. * do; but those macros have inappropriate return type, and for interfacing
  328. * from other programming languages, C macros aren't much of a help anyway.
  329. */
  330. size_t BIO_ctrl_pending(BIO *bio)
  331. {
  332. return BIO_ctrl(bio, BIO_CTRL_PENDING, 0, NULL);
  333. }
  334. size_t BIO_ctrl_wpending(BIO *bio)
  335. {
  336. return BIO_ctrl(bio, BIO_CTRL_WPENDING, 0, NULL);
  337. }
  338. /* put the 'bio' on the end of b's list of operators */
  339. BIO *BIO_push(BIO *b, BIO *bio)
  340. {
  341. BIO *lb;
  342. if (b == NULL)
  343. return (bio);
  344. lb = b;
  345. while (lb->next_bio != NULL)
  346. lb = lb->next_bio;
  347. lb->next_bio = bio;
  348. if (bio != NULL)
  349. bio->prev_bio = lb;
  350. /* called to do internal processing */
  351. BIO_ctrl(b, BIO_CTRL_PUSH, 0, lb);
  352. return (b);
  353. }
  354. /* Remove the first and return the rest */
  355. BIO *BIO_pop(BIO *b)
  356. {
  357. BIO *ret;
  358. if (b == NULL)
  359. return (NULL);
  360. ret = b->next_bio;
  361. BIO_ctrl(b, BIO_CTRL_POP, 0, b);
  362. if (b->prev_bio != NULL)
  363. b->prev_bio->next_bio = b->next_bio;
  364. if (b->next_bio != NULL)
  365. b->next_bio->prev_bio = b->prev_bio;
  366. b->next_bio = NULL;
  367. b->prev_bio = NULL;
  368. return (ret);
  369. }
  370. BIO *BIO_get_retry_BIO(BIO *bio, int *reason)
  371. {
  372. BIO *b, *last;
  373. b = last = bio;
  374. for (;;) {
  375. if (!BIO_should_retry(b))
  376. break;
  377. last = b;
  378. b = b->next_bio;
  379. if (b == NULL)
  380. break;
  381. }
  382. if (reason != NULL)
  383. *reason = last->retry_reason;
  384. return (last);
  385. }
  386. int BIO_get_retry_reason(BIO *bio)
  387. {
  388. return (bio->retry_reason);
  389. }
  390. BIO *BIO_find_type(BIO *bio, int type)
  391. {
  392. int mt, mask;
  393. if (!bio)
  394. return NULL;
  395. mask = type & 0xff;
  396. do {
  397. if (bio->method != NULL) {
  398. mt = bio->method->type;
  399. if (!mask) {
  400. if (mt & type)
  401. return (bio);
  402. } else if (mt == type)
  403. return (bio);
  404. }
  405. bio = bio->next_bio;
  406. } while (bio != NULL);
  407. return (NULL);
  408. }
  409. BIO *BIO_next(BIO *b)
  410. {
  411. if (!b)
  412. return NULL;
  413. return b->next_bio;
  414. }
  415. void BIO_free_all(BIO *bio)
  416. {
  417. BIO *b;
  418. int ref;
  419. while (bio != NULL) {
  420. b = bio;
  421. ref = b->references;
  422. bio = bio->next_bio;
  423. BIO_free(b);
  424. /* Since ref count > 1, don't free anyone else. */
  425. if (ref > 1)
  426. break;
  427. }
  428. }
  429. BIO *BIO_dup_chain(BIO *in)
  430. {
  431. BIO *ret = NULL, *eoc = NULL, *bio, *new_bio;
  432. for (bio = in; bio != NULL; bio = bio->next_bio) {
  433. if ((new_bio = BIO_new(bio->method)) == NULL)
  434. goto err;
  435. new_bio->callback = bio->callback;
  436. new_bio->cb_arg = bio->cb_arg;
  437. new_bio->init = bio->init;
  438. new_bio->shutdown = bio->shutdown;
  439. new_bio->flags = bio->flags;
  440. /* This will let SSL_s_sock() work with stdin/stdout */
  441. new_bio->num = bio->num;
  442. if (!BIO_dup_state(bio, (char *)new_bio)) {
  443. BIO_free(new_bio);
  444. goto err;
  445. }
  446. /* copy app data */
  447. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_BIO, &new_bio->ex_data,
  448. &bio->ex_data)) {
  449. BIO_free(new_bio);
  450. goto err;
  451. }
  452. if (ret == NULL) {
  453. eoc = new_bio;
  454. ret = eoc;
  455. } else {
  456. BIO_push(eoc, new_bio);
  457. eoc = new_bio;
  458. }
  459. }
  460. return (ret);
  461. err:
  462. BIO_free_all(ret);
  463. return (NULL);
  464. }
  465. void BIO_copy_next_retry(BIO *b)
  466. {
  467. BIO_set_flags(b, BIO_get_retry_flags(b->next_bio));
  468. b->retry_reason = b->next_bio->retry_reason;
  469. }
  470. int BIO_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  471. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  472. {
  473. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_BIO, argl, argp,
  474. new_func, dup_func, free_func);
  475. }
  476. int BIO_set_ex_data(BIO *bio, int idx, void *data)
  477. {
  478. return (CRYPTO_set_ex_data(&(bio->ex_data), idx, data));
  479. }
  480. void *BIO_get_ex_data(BIO *bio, int idx)
  481. {
  482. return (CRYPTO_get_ex_data(&(bio->ex_data), idx));
  483. }
  484. unsigned long BIO_number_read(BIO *bio)
  485. {
  486. if (bio)
  487. return bio->num_read;
  488. return 0;
  489. }
  490. unsigned long BIO_number_written(BIO *bio)
  491. {
  492. if (bio)
  493. return bio->num_write;
  494. return 0;
  495. }
  496. IMPLEMENT_STACK_OF(BIO)