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.
 
 
 
 
 
 

599 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. 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. return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
  270. }
  271. size_t BIO_ctrl_pending(const BIO *bio) {
  272. return BIO_pending(bio);
  273. }
  274. size_t BIO_wpending(const BIO *bio) {
  275. return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
  276. }
  277. int BIO_set_close(BIO *bio, int close_flag) {
  278. return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
  279. }
  280. void BIO_set_callback(BIO *bio, bio_info_cb callback_func) {
  281. bio->callback = callback_func;
  282. }
  283. void BIO_set_callback_arg(BIO *bio, char *arg) {
  284. bio->cb_arg = arg;
  285. }
  286. char *BIO_get_callback_arg(const BIO *bio) {
  287. return bio->cb_arg;
  288. }
  289. OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
  290. return bio->num_read;
  291. }
  292. OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
  293. return bio->num_write;
  294. }
  295. BIO *BIO_push(BIO *bio, BIO *appended_bio) {
  296. BIO *last_bio;
  297. if (bio == NULL) {
  298. return bio;
  299. }
  300. last_bio = bio;
  301. while (last_bio->next_bio != NULL) {
  302. last_bio = last_bio->next_bio;
  303. }
  304. last_bio->next_bio = appended_bio;
  305. return bio;
  306. }
  307. BIO *BIO_pop(BIO *bio) {
  308. BIO *ret;
  309. if (bio == NULL) {
  310. return NULL;
  311. }
  312. ret = bio->next_bio;
  313. bio->next_bio = NULL;
  314. return ret;
  315. }
  316. BIO *BIO_next(BIO *bio) {
  317. if (!bio) {
  318. return NULL;
  319. }
  320. return bio->next_bio;
  321. }
  322. BIO *BIO_find_type(BIO *bio, int type) {
  323. int method_type, mask;
  324. if (!bio) {
  325. return NULL;
  326. }
  327. mask = type & 0xff;
  328. do {
  329. if (bio->method != NULL) {
  330. method_type = bio->method->type;
  331. if (!mask) {
  332. if (method_type & type) {
  333. return bio;
  334. }
  335. } else if (method_type == type) {
  336. return bio;
  337. }
  338. }
  339. bio = bio->next_bio;
  340. } while (bio != NULL);
  341. return NULL;
  342. }
  343. int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
  344. if (indent > max_indent) {
  345. indent = max_indent;
  346. }
  347. while (indent--) {
  348. if (BIO_puts(bio, " ") != 1) {
  349. return 0;
  350. }
  351. }
  352. return 1;
  353. }
  354. static int print_bio(const char *str, size_t len, void *bio) {
  355. return BIO_write((BIO *)bio, str, len);
  356. }
  357. void BIO_print_errors(BIO *bio) {
  358. ERR_print_errors_cb(print_bio, bio);
  359. }
  360. void ERR_print_errors(BIO *bio) {
  361. BIO_print_errors(bio);
  362. }
  363. /* bio_read_all reads everything from |bio| and prepends |prefix| to it. On
  364. * success, |*out| is set to an allocated buffer (which should be freed with
  365. * |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
  366. * buffer will contain |prefix| followed by the contents of |bio|. On failure,
  367. * zero is returned.
  368. *
  369. * The function will fail if the size of the output would equal or exceed
  370. * |max_len|. */
  371. static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
  372. const uint8_t *prefix, size_t prefix_len,
  373. size_t max_len) {
  374. static const size_t kChunkSize = 4096;
  375. size_t len = prefix_len + kChunkSize;
  376. if (len > max_len) {
  377. len = max_len;
  378. }
  379. if (len < prefix_len) {
  380. return 0;
  381. }
  382. *out = OPENSSL_malloc(len);
  383. if (*out == NULL) {
  384. return 0;
  385. }
  386. memcpy(*out, prefix, prefix_len);
  387. size_t done = prefix_len;
  388. for (;;) {
  389. if (done == len) {
  390. OPENSSL_free(*out);
  391. return 0;
  392. }
  393. const size_t todo = len - done;
  394. assert(todo < INT_MAX);
  395. const int n = BIO_read(bio, *out + done, todo);
  396. if (n == 0) {
  397. *out_len = done;
  398. return 1;
  399. } else if (n == -1) {
  400. OPENSSL_free(*out);
  401. return 0;
  402. }
  403. done += n;
  404. if (len < max_len && len - done < kChunkSize / 2) {
  405. len += kChunkSize;
  406. if (len < kChunkSize || len > max_len) {
  407. len = max_len;
  408. }
  409. uint8_t *new_buf = OPENSSL_realloc(*out, len);
  410. if (new_buf == NULL) {
  411. OPENSSL_free(*out);
  412. return 0;
  413. }
  414. *out = new_buf;
  415. }
  416. }
  417. }
  418. int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
  419. uint8_t header[6];
  420. static const size_t kInitialHeaderLen = 2;
  421. if (BIO_read(bio, header, kInitialHeaderLen) != (int) kInitialHeaderLen) {
  422. return 0;
  423. }
  424. const uint8_t tag = header[0];
  425. const uint8_t length_byte = header[1];
  426. if ((tag & 0x1f) == 0x1f) {
  427. /* Long form tags are not supported. */
  428. return 0;
  429. }
  430. size_t len, header_len;
  431. if ((length_byte & 0x80) == 0) {
  432. /* Short form length. */
  433. len = length_byte;
  434. header_len = kInitialHeaderLen;
  435. } else {
  436. const size_t num_bytes = length_byte & 0x7f;
  437. if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
  438. /* indefinite length. */
  439. return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
  440. max_len);
  441. }
  442. if (num_bytes == 0 || num_bytes > 4) {
  443. return 0;
  444. }
  445. if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) !=
  446. (int)num_bytes) {
  447. return 0;
  448. }
  449. header_len = kInitialHeaderLen + num_bytes;
  450. uint32_t len32 = 0;
  451. unsigned i;
  452. for (i = 0; i < num_bytes; i++) {
  453. len32 <<= 8;
  454. len32 |= header[kInitialHeaderLen + i];
  455. }
  456. if (len32 < 128) {
  457. /* Length should have used short-form encoding. */
  458. return 0;
  459. }
  460. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  461. /* Length should have been at least one byte shorter. */
  462. return 0;
  463. }
  464. len = len32;
  465. }
  466. if (len + header_len < len ||
  467. len + header_len > max_len ||
  468. len > INT_MAX) {
  469. return 0;
  470. }
  471. len += header_len;
  472. *out_len = len;
  473. *out = OPENSSL_malloc(len);
  474. if (*out == NULL) {
  475. return 0;
  476. }
  477. memcpy(*out, header, header_len);
  478. if (BIO_read(bio, (*out) + header_len, len - header_len) !=
  479. (int) (len - header_len)) {
  480. OPENSSL_free(*out);
  481. return 0;
  482. }
  483. return 1;
  484. }