bio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/bio.h>
  57. #include <assert.h>
  58. #include <errno.h>
  59. #include <limits.h>
  60. #include <string.h>
  61. #include <openssl/err.h>
  62. #include <openssl/mem.h>
  63. #include <openssl/thread.h>
  64. #include "../internal.h"
  65. BIO *BIO_new(const BIO_METHOD *method) {
  66. BIO *ret = OPENSSL_malloc(sizeof(BIO));
  67. if (ret == NULL) {
  68. OPENSSL_PUT_ERROR(BIO, ERR_R_MALLOC_FAILURE);
  69. return NULL;
  70. }
  71. OPENSSL_memset(ret, 0, sizeof(BIO));
  72. ret->method = method;
  73. ret->shutdown = 1;
  74. ret->references = 1;
  75. if (method->create != NULL && !method->create(ret)) {
  76. OPENSSL_free(ret);
  77. return NULL;
  78. }
  79. return ret;
  80. }
  81. int BIO_free(BIO *bio) {
  82. BIO *next_bio;
  83. for (; bio != NULL; bio = next_bio) {
  84. if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) {
  85. return 0;
  86. }
  87. if (bio->callback != NULL) {
  88. int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1);
  89. if (i <= 0) {
  90. return i;
  91. }
  92. }
  93. next_bio = BIO_pop(bio);
  94. if (bio->method != NULL && bio->method->destroy != NULL) {
  95. bio->method->destroy(bio);
  96. }
  97. OPENSSL_free(bio);
  98. }
  99. return 1;
  100. }
  101. int BIO_up_ref(BIO *bio) {
  102. CRYPTO_refcount_inc(&bio->references);
  103. return 1;
  104. }
  105. void BIO_vfree(BIO *bio) {
  106. BIO_free(bio);
  107. }
  108. void BIO_free_all(BIO *bio) {
  109. BIO_free(bio);
  110. }
  111. static int bio_io(BIO *bio, void *buf, int len, size_t method_offset,
  112. int callback_flags, size_t *num) {
  113. int i;
  114. typedef int (*io_func_t)(BIO *, char *, int);
  115. io_func_t io_func = NULL;
  116. if (bio != NULL && bio->method != NULL) {
  117. io_func =
  118. *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset));
  119. }
  120. if (io_func == NULL) {
  121. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  122. return -2;
  123. }
  124. if (bio->callback != NULL) {
  125. i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L);
  126. if (i <= 0) {
  127. return i;
  128. }
  129. }
  130. if (!bio->init) {
  131. OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
  132. return -2;
  133. }
  134. i = 0;
  135. if (buf != NULL && len > 0) {
  136. i = io_func(bio, buf, len);
  137. }
  138. if (i > 0) {
  139. *num += i;
  140. }
  141. if (bio->callback != NULL) {
  142. i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L,
  143. (long)i));
  144. }
  145. return i;
  146. }
  147. int BIO_read(BIO *bio, void *buf, int len) {
  148. return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ,
  149. &bio->num_read);
  150. }
  151. int BIO_gets(BIO *bio, char *buf, int len) {
  152. return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS,
  153. &bio->num_read);
  154. }
  155. int BIO_write(BIO *bio, const void *in, int inl) {
  156. return bio_io(bio, (char *)in, inl, offsetof(BIO_METHOD, bwrite),
  157. BIO_CB_WRITE, &bio->num_write);
  158. }
  159. int BIO_puts(BIO *bio, const char *in) {
  160. return BIO_write(bio, in, strlen(in));
  161. }
  162. int BIO_flush(BIO *bio) {
  163. return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
  164. }
  165. long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
  166. long ret;
  167. if (bio == NULL) {
  168. return 0;
  169. }
  170. if (bio->method == NULL || bio->method->ctrl == NULL) {
  171. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  172. return -2;
  173. }
  174. if (bio->callback != NULL) {
  175. ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1);
  176. if (ret <= 0) {
  177. return ret;
  178. }
  179. }
  180. ret = bio->method->ctrl(bio, cmd, larg, parg);
  181. if (bio->callback != NULL) {
  182. ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
  183. }
  184. return ret;
  185. }
  186. char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
  187. char *p = NULL;
  188. if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
  189. return NULL;
  190. }
  191. return p;
  192. }
  193. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
  194. int i = iarg;
  195. return BIO_ctrl(b, cmd, larg, (void *)&i);
  196. }
  197. int BIO_reset(BIO *bio) {
  198. return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
  199. }
  200. int BIO_eof(BIO *bio) {
  201. return BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
  202. }
  203. void BIO_set_flags(BIO *bio, int flags) {
  204. bio->flags |= flags;
  205. }
  206. int BIO_test_flags(const BIO *bio, int flags) {
  207. return bio->flags & flags;
  208. }
  209. int BIO_should_read(const BIO *bio) {
  210. return BIO_test_flags(bio, BIO_FLAGS_READ);
  211. }
  212. int BIO_should_write(const BIO *bio) {
  213. return BIO_test_flags(bio, BIO_FLAGS_WRITE);
  214. }
  215. int BIO_should_retry(const BIO *bio) {
  216. return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
  217. }
  218. int BIO_should_io_special(const BIO *bio) {
  219. return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
  220. }
  221. int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
  222. void BIO_clear_flags(BIO *bio, int flags) {
  223. bio->flags &= ~flags;
  224. }
  225. void BIO_set_retry_read(BIO *bio) {
  226. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
  227. }
  228. void BIO_set_retry_write(BIO *bio) {
  229. bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
  230. }
  231. static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
  232. int BIO_get_retry_flags(BIO *bio) {
  233. return bio->flags & kRetryFlags;
  234. }
  235. void BIO_clear_retry_flags(BIO *bio) {
  236. bio->flags &= ~kRetryFlags;
  237. bio->retry_reason = 0;
  238. }
  239. int BIO_method_type(const BIO *bio) { return bio->method->type; }
  240. void BIO_copy_next_retry(BIO *bio) {
  241. BIO_clear_retry_flags(bio);
  242. BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
  243. bio->retry_reason = bio->next_bio->retry_reason;
  244. }
  245. long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  246. long ret;
  247. bio_info_cb cb;
  248. if (bio == NULL) {
  249. return 0;
  250. }
  251. if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
  252. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  253. return 0;
  254. }
  255. cb = bio->callback;
  256. if (cb != NULL) {
  257. ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L);
  258. if (ret <= 0) {
  259. return ret;
  260. }
  261. }
  262. ret = bio->method->callback_ctrl(bio, cmd, fp);
  263. if (cb != NULL) {
  264. ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
  265. }
  266. return ret;
  267. }
  268. size_t BIO_pending(const BIO *bio) {
  269. const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
  270. assert(r >= 0);
  271. if (r < 0) {
  272. return 0;
  273. }
  274. return r;
  275. }
  276. size_t BIO_ctrl_pending(const BIO *bio) {
  277. return BIO_pending(bio);
  278. }
  279. size_t BIO_wpending(const BIO *bio) {
  280. const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
  281. assert(r >= 0);
  282. if (r < 0) {
  283. return 0;
  284. }
  285. return r;
  286. }
  287. int BIO_set_close(BIO *bio, int close_flag) {
  288. return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
  289. }
  290. void BIO_set_callback(BIO *bio, bio_info_cb callback_func) {
  291. bio->callback = callback_func;
  292. }
  293. void BIO_set_callback_arg(BIO *bio, char *arg) {
  294. bio->cb_arg = arg;
  295. }
  296. char *BIO_get_callback_arg(const BIO *bio) {
  297. return bio->cb_arg;
  298. }
  299. OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
  300. return bio->num_read;
  301. }
  302. OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
  303. return bio->num_write;
  304. }
  305. BIO *BIO_push(BIO *bio, BIO *appended_bio) {
  306. BIO *last_bio;
  307. if (bio == NULL) {
  308. return bio;
  309. }
  310. last_bio = bio;
  311. while (last_bio->next_bio != NULL) {
  312. last_bio = last_bio->next_bio;
  313. }
  314. last_bio->next_bio = appended_bio;
  315. return bio;
  316. }
  317. BIO *BIO_pop(BIO *bio) {
  318. BIO *ret;
  319. if (bio == NULL) {
  320. return NULL;
  321. }
  322. ret = bio->next_bio;
  323. bio->next_bio = NULL;
  324. return ret;
  325. }
  326. BIO *BIO_next(BIO *bio) {
  327. if (!bio) {
  328. return NULL;
  329. }
  330. return bio->next_bio;
  331. }
  332. BIO *BIO_find_type(BIO *bio, int type) {
  333. int method_type, mask;
  334. if (!bio) {
  335. return NULL;
  336. }
  337. mask = type & 0xff;
  338. do {
  339. if (bio->method != NULL) {
  340. method_type = bio->method->type;
  341. if (!mask) {
  342. if (method_type & type) {
  343. return bio;
  344. }
  345. } else if (method_type == type) {
  346. return bio;
  347. }
  348. }
  349. bio = bio->next_bio;
  350. } while (bio != NULL);
  351. return NULL;
  352. }
  353. int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
  354. if (indent > max_indent) {
  355. indent = max_indent;
  356. }
  357. while (indent--) {
  358. if (BIO_puts(bio, " ") != 1) {
  359. return 0;
  360. }
  361. }
  362. return 1;
  363. }
  364. static int print_bio(const char *str, size_t len, void *bio) {
  365. return BIO_write((BIO *)bio, str, len);
  366. }
  367. void ERR_print_errors(BIO *bio) {
  368. ERR_print_errors_cb(print_bio, bio);
  369. }
  370. /* bio_read_all reads everything from |bio| and prepends |prefix| to it. On
  371. * success, |*out| is set to an allocated buffer (which should be freed with
  372. * |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
  373. * buffer will contain |prefix| followed by the contents of |bio|. On failure,
  374. * zero is returned.
  375. *
  376. * The function will fail if the size of the output would equal or exceed
  377. * |max_len|. */
  378. static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
  379. const uint8_t *prefix, size_t prefix_len,
  380. size_t max_len) {
  381. static const size_t kChunkSize = 4096;
  382. size_t len = prefix_len + kChunkSize;
  383. if (len > max_len) {
  384. len = max_len;
  385. }
  386. if (len < prefix_len) {
  387. return 0;
  388. }
  389. *out = OPENSSL_malloc(len);
  390. if (*out == NULL) {
  391. return 0;
  392. }
  393. OPENSSL_memcpy(*out, prefix, prefix_len);
  394. size_t done = prefix_len;
  395. for (;;) {
  396. if (done == len) {
  397. OPENSSL_free(*out);
  398. return 0;
  399. }
  400. const size_t todo = len - done;
  401. assert(todo < INT_MAX);
  402. const int n = BIO_read(bio, *out + done, todo);
  403. if (n == 0) {
  404. *out_len = done;
  405. return 1;
  406. } else if (n == -1) {
  407. OPENSSL_free(*out);
  408. return 0;
  409. }
  410. done += n;
  411. if (len < max_len && len - done < kChunkSize / 2) {
  412. len += kChunkSize;
  413. if (len < kChunkSize || len > max_len) {
  414. len = max_len;
  415. }
  416. uint8_t *new_buf = OPENSSL_realloc(*out, len);
  417. if (new_buf == NULL) {
  418. OPENSSL_free(*out);
  419. return 0;
  420. }
  421. *out = new_buf;
  422. }
  423. }
  424. }
  425. int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
  426. uint8_t header[6];
  427. static const size_t kInitialHeaderLen = 2;
  428. if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) {
  429. return 0;
  430. }
  431. const uint8_t tag = header[0];
  432. const uint8_t length_byte = header[1];
  433. if ((tag & 0x1f) == 0x1f) {
  434. /* Long form tags are not supported. */
  435. return 0;
  436. }
  437. size_t len, header_len;
  438. if ((length_byte & 0x80) == 0) {
  439. /* Short form length. */
  440. len = length_byte;
  441. header_len = kInitialHeaderLen;
  442. } else {
  443. const size_t num_bytes = length_byte & 0x7f;
  444. if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
  445. /* indefinite length. */
  446. return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
  447. max_len);
  448. }
  449. if (num_bytes == 0 || num_bytes > 4) {
  450. return 0;
  451. }
  452. if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) !=
  453. (int)num_bytes) {
  454. return 0;
  455. }
  456. header_len = kInitialHeaderLen + num_bytes;
  457. uint32_t len32 = 0;
  458. unsigned i;
  459. for (i = 0; i < num_bytes; i++) {
  460. len32 <<= 8;
  461. len32 |= header[kInitialHeaderLen + i];
  462. }
  463. if (len32 < 128) {
  464. /* Length should have used short-form encoding. */
  465. return 0;
  466. }
  467. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  468. /* Length should have been at least one byte shorter. */
  469. return 0;
  470. }
  471. len = len32;
  472. }
  473. if (len + header_len < len ||
  474. len + header_len > max_len ||
  475. len > INT_MAX) {
  476. return 0;
  477. }
  478. len += header_len;
  479. *out_len = len;
  480. *out = OPENSSL_malloc(len);
  481. if (*out == NULL) {
  482. return 0;
  483. }
  484. OPENSSL_memcpy(*out, header, header_len);
  485. if (BIO_read(bio, (*out) + header_len, len - header_len) !=
  486. (int) (len - header_len)) {
  487. OPENSSL_free(*out);
  488. return 0;
  489. }
  490. return 1;
  491. }
  492. void BIO_set_retry_special(BIO *bio) {
  493. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL;
  494. }
  495. int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }