You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

595 lines
14 KiB

  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. 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. BIO *BIO_up_ref(BIO *bio) {
  102. CRYPTO_refcount_inc(&bio->references);
  103. return bio;
  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. void BIO_set_flags(BIO *bio, int flags) {
  201. bio->flags |= flags;
  202. }
  203. int BIO_test_flags(const BIO *bio, int flags) {
  204. return bio->flags & flags;
  205. }
  206. int BIO_should_read(const BIO *bio) {
  207. return BIO_test_flags(bio, BIO_FLAGS_READ);
  208. }
  209. int BIO_should_write(const BIO *bio) {
  210. return BIO_test_flags(bio, BIO_FLAGS_WRITE);
  211. }
  212. int BIO_should_retry(const BIO *bio) {
  213. return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
  214. }
  215. int BIO_should_io_special(const BIO *bio) {
  216. return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
  217. }
  218. int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
  219. void BIO_clear_flags(BIO *bio, int flags) {
  220. bio->flags &= ~flags;
  221. }
  222. void BIO_set_retry_read(BIO *bio) {
  223. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
  224. }
  225. void BIO_set_retry_write(BIO *bio) {
  226. bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
  227. }
  228. static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
  229. int BIO_get_retry_flags(BIO *bio) {
  230. return bio->flags & kRetryFlags;
  231. }
  232. void BIO_clear_retry_flags(BIO *bio) {
  233. bio->flags &= ~kRetryFlags;
  234. bio->retry_reason = 0;
  235. }
  236. int BIO_method_type(const BIO *bio) { return bio->method->type; }
  237. void BIO_copy_next_retry(BIO *bio) {
  238. BIO_clear_retry_flags(bio);
  239. BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
  240. bio->retry_reason = bio->next_bio->retry_reason;
  241. }
  242. long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  243. long ret;
  244. bio_info_cb cb;
  245. if (bio == NULL) {
  246. return 0;
  247. }
  248. if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
  249. OPENSSL_PUT_ERROR(BIO, BIO_R_UNSUPPORTED_METHOD);
  250. return 0;
  251. }
  252. cb = bio->callback;
  253. if (cb != NULL) {
  254. ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L);
  255. if (ret <= 0) {
  256. return ret;
  257. }
  258. }
  259. ret = bio->method->callback_ctrl(bio, cmd, fp);
  260. if (cb != NULL) {
  261. ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
  262. }
  263. return ret;
  264. }
  265. size_t BIO_pending(const BIO *bio) {
  266. return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
  267. }
  268. size_t BIO_ctrl_pending(const BIO *bio) {
  269. return BIO_pending(bio);
  270. }
  271. size_t BIO_wpending(const BIO *bio) {
  272. return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
  273. }
  274. int BIO_set_close(BIO *bio, int close_flag) {
  275. return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
  276. }
  277. void BIO_set_callback(BIO *bio, bio_info_cb callback_func) {
  278. bio->callback = callback_func;
  279. }
  280. void BIO_set_callback_arg(BIO *bio, char *arg) {
  281. bio->cb_arg = arg;
  282. }
  283. char *BIO_get_callback_arg(const BIO *bio) {
  284. return bio->cb_arg;
  285. }
  286. OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
  287. return bio->num_read;
  288. }
  289. OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
  290. return bio->num_write;
  291. }
  292. BIO *BIO_push(BIO *bio, BIO *appended_bio) {
  293. BIO *last_bio;
  294. if (bio == NULL) {
  295. return bio;
  296. }
  297. last_bio = bio;
  298. while (last_bio->next_bio != NULL) {
  299. last_bio = last_bio->next_bio;
  300. }
  301. last_bio->next_bio = appended_bio;
  302. return bio;
  303. }
  304. BIO *BIO_pop(BIO *bio) {
  305. BIO *ret;
  306. if (bio == NULL) {
  307. return NULL;
  308. }
  309. ret = bio->next_bio;
  310. bio->next_bio = NULL;
  311. return ret;
  312. }
  313. BIO *BIO_next(BIO *bio) {
  314. if (!bio) {
  315. return NULL;
  316. }
  317. return bio->next_bio;
  318. }
  319. BIO *BIO_find_type(BIO *bio, int type) {
  320. int method_type, mask;
  321. if (!bio) {
  322. return NULL;
  323. }
  324. mask = type & 0xff;
  325. do {
  326. if (bio->method != NULL) {
  327. method_type = bio->method->type;
  328. if (!mask) {
  329. if (method_type & type) {
  330. return bio;
  331. }
  332. } else if (method_type == type) {
  333. return bio;
  334. }
  335. }
  336. bio = bio->next_bio;
  337. } while (bio != NULL);
  338. return NULL;
  339. }
  340. int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
  341. if (indent > max_indent) {
  342. indent = max_indent;
  343. }
  344. while (indent--) {
  345. if (BIO_puts(bio, " ") != 1) {
  346. return 0;
  347. }
  348. }
  349. return 1;
  350. }
  351. static int print_bio(const char *str, size_t len, void *bio) {
  352. return BIO_write((BIO *)bio, str, len);
  353. }
  354. void BIO_print_errors(BIO *bio) {
  355. ERR_print_errors_cb(print_bio, bio);
  356. }
  357. void ERR_print_errors(BIO *bio) {
  358. BIO_print_errors(bio);
  359. }
  360. /* bio_read_all reads everything from |bio| and prepends |prefix| to it. On
  361. * success, |*out| is set to an allocated buffer (which should be freed with
  362. * |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
  363. * buffer will contain |prefix| followed by the contents of |bio|. On failure,
  364. * zero is returned.
  365. *
  366. * The function will fail if the size of the output would equal or exceed
  367. * |max_len|. */
  368. static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
  369. const uint8_t *prefix, size_t prefix_len,
  370. size_t max_len) {
  371. static const size_t kChunkSize = 4096;
  372. size_t len = prefix_len + kChunkSize;
  373. if (len > max_len) {
  374. len = max_len;
  375. }
  376. if (len < prefix_len) {
  377. return 0;
  378. }
  379. *out = OPENSSL_malloc(len);
  380. if (*out == NULL) {
  381. return 0;
  382. }
  383. memcpy(*out, prefix, prefix_len);
  384. size_t done = prefix_len;
  385. for (;;) {
  386. if (done == len) {
  387. OPENSSL_free(*out);
  388. return 0;
  389. }
  390. const size_t todo = len - done;
  391. assert(todo < INT_MAX);
  392. const int n = BIO_read(bio, *out + done, todo);
  393. if (n == 0) {
  394. *out_len = done;
  395. return 1;
  396. } else if (n == -1) {
  397. OPENSSL_free(*out);
  398. return 0;
  399. }
  400. done += n;
  401. if (len < max_len && len - done < kChunkSize / 2) {
  402. len += kChunkSize;
  403. if (len < kChunkSize || len > max_len) {
  404. len = max_len;
  405. }
  406. uint8_t *new_buf = OPENSSL_realloc(*out, len);
  407. if (new_buf == NULL) {
  408. OPENSSL_free(*out);
  409. return 0;
  410. }
  411. *out = new_buf;
  412. }
  413. }
  414. }
  415. int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
  416. uint8_t header[6];
  417. static const size_t kInitialHeaderLen = 2;
  418. if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) {
  419. return 0;
  420. }
  421. const uint8_t tag = header[0];
  422. const uint8_t length_byte = header[1];
  423. if ((tag & 0x1f) == 0x1f) {
  424. /* Long form tags are not supported. */
  425. return 0;
  426. }
  427. size_t len, header_len;
  428. if ((length_byte & 0x80) == 0) {
  429. /* Short form length. */
  430. len = length_byte;
  431. header_len = kInitialHeaderLen;
  432. } else {
  433. const size_t num_bytes = length_byte & 0x7f;
  434. if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
  435. /* indefinite length. */
  436. return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
  437. max_len);
  438. }
  439. if (num_bytes == 0 || num_bytes > 4) {
  440. return 0;
  441. }
  442. if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) !=
  443. (int)num_bytes) {
  444. return 0;
  445. }
  446. header_len = kInitialHeaderLen + num_bytes;
  447. uint32_t len32 = 0;
  448. unsigned i;
  449. for (i = 0; i < num_bytes; i++) {
  450. len32 <<= 8;
  451. len32 |= header[kInitialHeaderLen + i];
  452. }
  453. if (len32 < 128) {
  454. /* Length should have used short-form encoding. */
  455. return 0;
  456. }
  457. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  458. /* Length should have been at least one byte shorter. */
  459. return 0;
  460. }
  461. len = len32;
  462. }
  463. if (len + header_len < len ||
  464. len + header_len > max_len ||
  465. len > INT_MAX) {
  466. return 0;
  467. }
  468. len += header_len;
  469. *out_len = len;
  470. *out = OPENSSL_malloc(len);
  471. if (*out == NULL) {
  472. return 0;
  473. }
  474. memcpy(*out, header, header_len);
  475. if (BIO_read(bio, (*out) + header_len, len - header_len) !=
  476. (int) (len - header_len)) {
  477. OPENSSL_free(*out);
  478. return 0;
  479. }
  480. return 1;
  481. }