bio.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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. next_bio = BIO_pop(bio);
  88. if (bio->method != NULL && bio->method->destroy != NULL) {
  89. bio->method->destroy(bio);
  90. }
  91. OPENSSL_free(bio);
  92. }
  93. return 1;
  94. }
  95. int BIO_up_ref(BIO *bio) {
  96. CRYPTO_refcount_inc(&bio->references);
  97. return 1;
  98. }
  99. void BIO_vfree(BIO *bio) {
  100. BIO_free(bio);
  101. }
  102. void BIO_free_all(BIO *bio) {
  103. BIO_free(bio);
  104. }
  105. int BIO_read(BIO *bio, void *buf, int len) {
  106. if (bio == NULL || bio->method == NULL || bio->method->bread == NULL) {
  107. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  108. return -2;
  109. }
  110. if (!bio->init) {
  111. OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
  112. return -2;
  113. }
  114. if (len <= 0) {
  115. return 0;
  116. }
  117. int ret = bio->method->bread(bio, buf, len);
  118. if (ret > 0) {
  119. bio->num_read += ret;
  120. }
  121. return ret;
  122. }
  123. int BIO_gets(BIO *bio, char *buf, int len) {
  124. if (bio == NULL || bio->method == NULL || bio->method->bgets == NULL) {
  125. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  126. return -2;
  127. }
  128. if (!bio->init) {
  129. OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
  130. return -2;
  131. }
  132. if (len <= 0) {
  133. return 0;
  134. }
  135. int ret = bio->method->bgets(bio, buf, len);
  136. if (ret > 0) {
  137. bio->num_read += ret;
  138. }
  139. return ret;
  140. }
  141. int BIO_write(BIO *bio, const void *in, int inl) {
  142. if (bio == NULL || bio->method == NULL || bio->method->bwrite == NULL) {
  143. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  144. return -2;
  145. }
  146. if (!bio->init) {
  147. OPENSSL_PUT_ERROR(BIO, BIO_R_UNINITIALIZED);
  148. return -2;
  149. }
  150. if (inl <= 0) {
  151. return 0;
  152. }
  153. int ret = bio->method->bwrite(bio, in, inl);
  154. if (ret > 0) {
  155. bio->num_write += ret;
  156. }
  157. return ret;
  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. if (bio == NULL) {
  167. return 0;
  168. }
  169. if (bio->method == NULL || bio->method->ctrl == NULL) {
  170. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  171. return -2;
  172. }
  173. return bio->method->ctrl(bio, cmd, larg, parg);
  174. }
  175. char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
  176. char *p = NULL;
  177. if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
  178. return NULL;
  179. }
  180. return p;
  181. }
  182. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
  183. int i = iarg;
  184. return BIO_ctrl(b, cmd, larg, (void *)&i);
  185. }
  186. int BIO_reset(BIO *bio) {
  187. return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
  188. }
  189. int BIO_eof(BIO *bio) {
  190. return BIO_ctrl(bio, BIO_CTRL_EOF, 0, NULL);
  191. }
  192. void BIO_set_flags(BIO *bio, int flags) {
  193. bio->flags |= flags;
  194. }
  195. int BIO_test_flags(const BIO *bio, int flags) {
  196. return bio->flags & flags;
  197. }
  198. int BIO_should_read(const BIO *bio) {
  199. return BIO_test_flags(bio, BIO_FLAGS_READ);
  200. }
  201. int BIO_should_write(const BIO *bio) {
  202. return BIO_test_flags(bio, BIO_FLAGS_WRITE);
  203. }
  204. int BIO_should_retry(const BIO *bio) {
  205. return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
  206. }
  207. int BIO_should_io_special(const BIO *bio) {
  208. return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
  209. }
  210. int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
  211. void BIO_clear_flags(BIO *bio, int flags) {
  212. bio->flags &= ~flags;
  213. }
  214. void BIO_set_retry_read(BIO *bio) {
  215. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
  216. }
  217. void BIO_set_retry_write(BIO *bio) {
  218. bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
  219. }
  220. static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
  221. int BIO_get_retry_flags(BIO *bio) {
  222. return bio->flags & kRetryFlags;
  223. }
  224. void BIO_clear_retry_flags(BIO *bio) {
  225. bio->flags &= ~kRetryFlags;
  226. bio->retry_reason = 0;
  227. }
  228. int BIO_method_type(const BIO *bio) { return bio->method->type; }
  229. void BIO_copy_next_retry(BIO *bio) {
  230. BIO_clear_retry_flags(bio);
  231. BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
  232. bio->retry_reason = bio->next_bio->retry_reason;
  233. }
  234. long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  235. if (bio == NULL) {
  236. return 0;
  237. }
  238. if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
  239. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  240. return 0;
  241. }
  242. return bio->method->callback_ctrl(bio, cmd, fp);
  243. }
  244. size_t BIO_pending(const BIO *bio) {
  245. const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
  246. assert(r >= 0);
  247. if (r < 0) {
  248. return 0;
  249. }
  250. return r;
  251. }
  252. size_t BIO_ctrl_pending(const BIO *bio) {
  253. return BIO_pending(bio);
  254. }
  255. size_t BIO_wpending(const BIO *bio) {
  256. const long r = BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
  257. assert(r >= 0);
  258. if (r < 0) {
  259. return 0;
  260. }
  261. return r;
  262. }
  263. int BIO_set_close(BIO *bio, int close_flag) {
  264. return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
  265. }
  266. OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
  267. return bio->num_read;
  268. }
  269. OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
  270. return bio->num_write;
  271. }
  272. BIO *BIO_push(BIO *bio, BIO *appended_bio) {
  273. BIO *last_bio;
  274. if (bio == NULL) {
  275. return bio;
  276. }
  277. last_bio = bio;
  278. while (last_bio->next_bio != NULL) {
  279. last_bio = last_bio->next_bio;
  280. }
  281. last_bio->next_bio = appended_bio;
  282. return bio;
  283. }
  284. BIO *BIO_pop(BIO *bio) {
  285. BIO *ret;
  286. if (bio == NULL) {
  287. return NULL;
  288. }
  289. ret = bio->next_bio;
  290. bio->next_bio = NULL;
  291. return ret;
  292. }
  293. BIO *BIO_next(BIO *bio) {
  294. if (!bio) {
  295. return NULL;
  296. }
  297. return bio->next_bio;
  298. }
  299. BIO *BIO_find_type(BIO *bio, int type) {
  300. int method_type, mask;
  301. if (!bio) {
  302. return NULL;
  303. }
  304. mask = type & 0xff;
  305. do {
  306. if (bio->method != NULL) {
  307. method_type = bio->method->type;
  308. if (!mask) {
  309. if (method_type & type) {
  310. return bio;
  311. }
  312. } else if (method_type == type) {
  313. return bio;
  314. }
  315. }
  316. bio = bio->next_bio;
  317. } while (bio != NULL);
  318. return NULL;
  319. }
  320. int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
  321. if (indent > max_indent) {
  322. indent = max_indent;
  323. }
  324. while (indent--) {
  325. if (BIO_puts(bio, " ") != 1) {
  326. return 0;
  327. }
  328. }
  329. return 1;
  330. }
  331. static int print_bio(const char *str, size_t len, void *bio) {
  332. return BIO_write((BIO *)bio, str, len);
  333. }
  334. void ERR_print_errors(BIO *bio) {
  335. ERR_print_errors_cb(print_bio, bio);
  336. }
  337. // bio_read_all reads everything from |bio| and prepends |prefix| to it. On
  338. // success, |*out| is set to an allocated buffer (which should be freed with
  339. // |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
  340. // buffer will contain |prefix| followed by the contents of |bio|. On failure,
  341. // zero is returned.
  342. //
  343. // The function will fail if the size of the output would equal or exceed
  344. // |max_len|.
  345. static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
  346. const uint8_t *prefix, size_t prefix_len,
  347. size_t max_len) {
  348. static const size_t kChunkSize = 4096;
  349. size_t len = prefix_len + kChunkSize;
  350. if (len > max_len) {
  351. len = max_len;
  352. }
  353. if (len < prefix_len) {
  354. return 0;
  355. }
  356. *out = OPENSSL_malloc(len);
  357. if (*out == NULL) {
  358. return 0;
  359. }
  360. OPENSSL_memcpy(*out, prefix, prefix_len);
  361. size_t done = prefix_len;
  362. for (;;) {
  363. if (done == len) {
  364. OPENSSL_free(*out);
  365. return 0;
  366. }
  367. const size_t todo = len - done;
  368. assert(todo < INT_MAX);
  369. const int n = BIO_read(bio, *out + done, todo);
  370. if (n == 0) {
  371. *out_len = done;
  372. return 1;
  373. } else if (n == -1) {
  374. OPENSSL_free(*out);
  375. return 0;
  376. }
  377. done += n;
  378. if (len < max_len && len - done < kChunkSize / 2) {
  379. len += kChunkSize;
  380. if (len < kChunkSize || len > max_len) {
  381. len = max_len;
  382. }
  383. uint8_t *new_buf = OPENSSL_realloc(*out, len);
  384. if (new_buf == NULL) {
  385. OPENSSL_free(*out);
  386. return 0;
  387. }
  388. *out = new_buf;
  389. }
  390. }
  391. }
  392. int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
  393. uint8_t header[6];
  394. static const size_t kInitialHeaderLen = 2;
  395. if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) {
  396. return 0;
  397. }
  398. const uint8_t tag = header[0];
  399. const uint8_t length_byte = header[1];
  400. if ((tag & 0x1f) == 0x1f) {
  401. // Long form tags are not supported.
  402. return 0;
  403. }
  404. size_t len, header_len;
  405. if ((length_byte & 0x80) == 0) {
  406. // Short form length.
  407. len = length_byte;
  408. header_len = kInitialHeaderLen;
  409. } else {
  410. const size_t num_bytes = length_byte & 0x7f;
  411. if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
  412. // indefinite length.
  413. return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
  414. max_len);
  415. }
  416. if (num_bytes == 0 || num_bytes > 4) {
  417. return 0;
  418. }
  419. if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) !=
  420. (int)num_bytes) {
  421. return 0;
  422. }
  423. header_len = kInitialHeaderLen + num_bytes;
  424. uint32_t len32 = 0;
  425. unsigned i;
  426. for (i = 0; i < num_bytes; i++) {
  427. len32 <<= 8;
  428. len32 |= header[kInitialHeaderLen + i];
  429. }
  430. if (len32 < 128) {
  431. // Length should have used short-form encoding.
  432. return 0;
  433. }
  434. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  435. // Length should have been at least one byte shorter.
  436. return 0;
  437. }
  438. len = len32;
  439. }
  440. if (len + header_len < len ||
  441. len + header_len > max_len ||
  442. len > INT_MAX) {
  443. return 0;
  444. }
  445. len += header_len;
  446. *out_len = len;
  447. *out = OPENSSL_malloc(len);
  448. if (*out == NULL) {
  449. return 0;
  450. }
  451. OPENSSL_memcpy(*out, header, header_len);
  452. if (BIO_read(bio, (*out) + header_len, len - header_len) !=
  453. (int) (len - header_len)) {
  454. OPENSSL_free(*out);
  455. return 0;
  456. }
  457. return 1;
  458. }
  459. void BIO_set_retry_special(BIO *bio) {
  460. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_IO_SPECIAL;
  461. }
  462. int BIO_set_write_buffer_size(BIO *bio, int buffer_size) { return 0; }
  463. static struct CRYPTO_STATIC_MUTEX g_index_lock = CRYPTO_STATIC_MUTEX_INIT;
  464. static int g_index = BIO_TYPE_START;
  465. int BIO_get_new_index(void) {
  466. CRYPTO_STATIC_MUTEX_lock_write(&g_index_lock);
  467. // If |g_index| exceeds 255, it will collide with the flags bits.
  468. int ret = g_index > 255 ? -1 : g_index++;
  469. CRYPTO_STATIC_MUTEX_unlock_write(&g_index_lock);
  470. return ret;
  471. }
  472. BIO_METHOD *BIO_meth_new(int type, const char *name) {
  473. BIO_METHOD *method = OPENSSL_malloc(sizeof(BIO_METHOD));
  474. if (method == NULL) {
  475. return NULL;
  476. }
  477. OPENSSL_memset(method, 0, sizeof(BIO_METHOD));
  478. method->type = type;
  479. method->name = name;
  480. return method;
  481. }
  482. void BIO_meth_free(BIO_METHOD *method) {
  483. OPENSSL_free(method);
  484. }
  485. int BIO_meth_set_create(BIO_METHOD *method,
  486. int (*create)(BIO *)) {
  487. method->create = create;
  488. return 1;
  489. }
  490. int BIO_meth_set_destroy(BIO_METHOD *method,
  491. int (*destroy)(BIO *)) {
  492. method->destroy = destroy;
  493. return 1;
  494. }
  495. int BIO_meth_set_write(BIO_METHOD *method,
  496. int (*write)(BIO *, const char *, int)) {
  497. method->bwrite = write;
  498. return 1;
  499. }
  500. int BIO_meth_set_read(BIO_METHOD *method,
  501. int (*read)(BIO *, char *, int)) {
  502. method->bread = read;
  503. return 1;
  504. }
  505. int BIO_meth_set_gets(BIO_METHOD *method,
  506. int (*gets)(BIO *, char *, int)) {
  507. method->bgets = gets;
  508. return 1;
  509. }
  510. int BIO_meth_set_ctrl(BIO_METHOD *method,
  511. long (*ctrl)(BIO *, int, long, void *)) {
  512. method->ctrl = ctrl;
  513. return 1;
  514. }
  515. void BIO_set_data(BIO *bio, void *ptr) { bio->ptr = ptr; }
  516. void *BIO_get_data(BIO *bio) { return bio->ptr; }
  517. void BIO_set_init(BIO *bio, int init) { bio->init = init; }
  518. int BIO_get_init(BIO *bio) { return bio->init; }
  519. void BIO_set_shutdown(BIO *bio, int shutdown) { bio->shutdown = shutdown; }
  520. int BIO_get_shutdown(BIO *bio) { return bio->shutdown; }
  521. int BIO_meth_set_puts(BIO_METHOD *method, int (*puts)(BIO *, const char *)) {
  522. // Ignore the parameter. We implement |BIO_puts| using |BIO_write|.
  523. return 1;
  524. }