Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 
 
 

603 строки
15 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_set initialises a BIO structure to have the given type and sets the
  66. * reference count to one. It returns one on success or zero on error. */
  67. static int bio_set(BIO *bio, const BIO_METHOD *method) {
  68. /* This function can be called with a stack allocated |BIO| so we have to
  69. * assume that the contents of |BIO| are arbitary. This also means that it'll
  70. * leak memory if you call |BIO_set| twice on the same BIO. */
  71. memset(bio, 0, sizeof(BIO));
  72. bio->method = method;
  73. bio->shutdown = 1;
  74. bio->references = 1;
  75. if (method->create != NULL && !method->create(bio)) {
  76. return 0;
  77. }
  78. return 1;
  79. }
  80. BIO *BIO_new(const BIO_METHOD *method) {
  81. BIO *ret = OPENSSL_malloc(sizeof(BIO));
  82. if (ret == NULL) {
  83. OPENSSL_PUT_ERROR(BIO, BIO_new, ERR_R_MALLOC_FAILURE);
  84. return NULL;
  85. }
  86. if (!bio_set(ret, method)) {
  87. OPENSSL_free(ret);
  88. ret = NULL;
  89. }
  90. return ret;
  91. }
  92. int BIO_free(BIO *bio) {
  93. BIO *next_bio;
  94. for (; bio != NULL; bio = next_bio) {
  95. if (!CRYPTO_refcount_dec_and_test_zero(&bio->references)) {
  96. return 0;
  97. }
  98. if (bio->callback != NULL) {
  99. int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1);
  100. if (i <= 0) {
  101. return i;
  102. }
  103. }
  104. next_bio = BIO_pop(bio);
  105. if (bio->method != NULL && bio->method->destroy != NULL) {
  106. bio->method->destroy(bio);
  107. }
  108. OPENSSL_free(bio);
  109. }
  110. return 1;
  111. }
  112. BIO *BIO_up_ref(BIO *bio) {
  113. CRYPTO_refcount_inc(&bio->references);
  114. return bio;
  115. }
  116. void BIO_vfree(BIO *bio) {
  117. BIO_free(bio);
  118. }
  119. void BIO_free_all(BIO *bio) {
  120. BIO_free(bio);
  121. }
  122. static int bio_io(BIO *bio, void *buf, int len, size_t method_offset,
  123. int callback_flags, size_t *num) {
  124. int i;
  125. typedef int (*io_func_t)(BIO *, char *, int);
  126. io_func_t io_func = NULL;
  127. if (bio != NULL && bio->method != NULL) {
  128. io_func =
  129. *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset));
  130. }
  131. if (io_func == NULL) {
  132. OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNSUPPORTED_METHOD);
  133. return -2;
  134. }
  135. if (bio->callback != NULL) {
  136. i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L);
  137. if (i <= 0) {
  138. return i;
  139. }
  140. }
  141. if (!bio->init) {
  142. OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNINITIALIZED);
  143. return -2;
  144. }
  145. i = 0;
  146. if (buf != NULL && len > 0) {
  147. i = io_func(bio, buf, len);
  148. }
  149. if (i > 0) {
  150. *num += i;
  151. }
  152. if (bio->callback != NULL) {
  153. i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L,
  154. (long)i));
  155. }
  156. return i;
  157. }
  158. int BIO_read(BIO *bio, void *buf, int len) {
  159. return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ,
  160. &bio->num_read);
  161. }
  162. int BIO_gets(BIO *bio, char *buf, int len) {
  163. return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS,
  164. &bio->num_read);
  165. }
  166. int BIO_write(BIO *bio, const void *in, int inl) {
  167. return bio_io(bio, (char *)in, inl, offsetof(BIO_METHOD, bwrite),
  168. BIO_CB_WRITE, &bio->num_write);
  169. }
  170. int BIO_puts(BIO *bio, const char *in) {
  171. return BIO_write(bio, in, strlen(in));
  172. }
  173. int BIO_flush(BIO *bio) {
  174. return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
  175. }
  176. long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
  177. long ret;
  178. if (bio == NULL) {
  179. return 0;
  180. }
  181. if (bio->method == NULL || bio->method->ctrl == NULL) {
  182. OPENSSL_PUT_ERROR(BIO, BIO_ctrl, BIO_R_UNSUPPORTED_METHOD);
  183. return -2;
  184. }
  185. if (bio->callback != NULL) {
  186. ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1);
  187. if (ret <= 0) {
  188. return ret;
  189. }
  190. }
  191. ret = bio->method->ctrl(bio, cmd, larg, parg);
  192. if (bio->callback != NULL) {
  193. ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
  194. }
  195. return ret;
  196. }
  197. char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
  198. char *p = NULL;
  199. if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
  200. return NULL;
  201. }
  202. return p;
  203. }
  204. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
  205. int i = iarg;
  206. return BIO_ctrl(b, cmd, larg, (void *)&i);
  207. }
  208. int BIO_reset(BIO *bio) {
  209. return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
  210. }
  211. void BIO_set_flags(BIO *bio, int flags) {
  212. bio->flags |= flags;
  213. }
  214. int BIO_test_flags(const BIO *bio, int flags) {
  215. return bio->flags & flags;
  216. }
  217. int BIO_should_read(const BIO *bio) {
  218. return BIO_test_flags(bio, BIO_FLAGS_READ);
  219. }
  220. int BIO_should_write(const BIO *bio) {
  221. return BIO_test_flags(bio, BIO_FLAGS_WRITE);
  222. }
  223. int BIO_should_retry(const BIO *bio) {
  224. return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
  225. }
  226. int BIO_should_io_special(const BIO *bio) {
  227. return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
  228. }
  229. int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
  230. void BIO_clear_flags(BIO *bio, int flags) {
  231. bio->flags &= ~flags;
  232. }
  233. void BIO_set_retry_read(BIO *bio) {
  234. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
  235. }
  236. void BIO_set_retry_write(BIO *bio) {
  237. bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
  238. }
  239. static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
  240. int BIO_get_retry_flags(BIO *bio) {
  241. return bio->flags & kRetryFlags;
  242. }
  243. void BIO_clear_retry_flags(BIO *bio) {
  244. bio->flags &= ~kRetryFlags;
  245. bio->retry_reason = 0;
  246. }
  247. int BIO_method_type(const BIO *bio) { return bio->method->type; }
  248. void BIO_copy_next_retry(BIO *bio) {
  249. BIO_clear_retry_flags(bio);
  250. BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
  251. bio->retry_reason = bio->next_bio->retry_reason;
  252. }
  253. long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  254. long ret;
  255. bio_info_cb cb;
  256. if (bio == NULL) {
  257. return 0;
  258. }
  259. if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
  260. OPENSSL_PUT_ERROR(BIO, BIO_callback_ctrl, BIO_R_UNSUPPORTED_METHOD);
  261. return 0;
  262. }
  263. cb = bio->callback;
  264. if (cb != NULL) {
  265. ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L);
  266. if (ret <= 0) {
  267. return ret;
  268. }
  269. }
  270. ret = bio->method->callback_ctrl(bio, cmd, fp);
  271. if (cb != NULL) {
  272. ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
  273. }
  274. return ret;
  275. }
  276. size_t BIO_pending(const BIO *bio) {
  277. return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
  278. }
  279. size_t BIO_ctrl_pending(const BIO *bio) {
  280. return BIO_pending(bio);
  281. }
  282. size_t BIO_wpending(const BIO *bio) {
  283. return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
  284. }
  285. int BIO_set_close(BIO *bio, int close_flag) {
  286. return BIO_ctrl(bio, BIO_CTRL_SET_CLOSE, close_flag, NULL);
  287. }
  288. void BIO_set_callback(BIO *bio, bio_info_cb callback_func) {
  289. bio->callback = callback_func;
  290. }
  291. void BIO_set_callback_arg(BIO *bio, char *arg) {
  292. bio->cb_arg = arg;
  293. }
  294. char *BIO_get_callback_arg(const BIO *bio) {
  295. return bio->cb_arg;
  296. }
  297. OPENSSL_EXPORT size_t BIO_number_read(const BIO *bio) {
  298. return bio->num_read;
  299. }
  300. OPENSSL_EXPORT size_t BIO_number_written(const BIO *bio) {
  301. return bio->num_write;
  302. }
  303. BIO *BIO_push(BIO *bio, BIO *appended_bio) {
  304. BIO *last_bio;
  305. if (bio == NULL) {
  306. return bio;
  307. }
  308. last_bio = bio;
  309. while (last_bio->next_bio != NULL) {
  310. last_bio = last_bio->next_bio;
  311. }
  312. last_bio->next_bio = appended_bio;
  313. return bio;
  314. }
  315. BIO *BIO_pop(BIO *bio) {
  316. BIO *ret;
  317. if (bio == NULL) {
  318. return NULL;
  319. }
  320. ret = bio->next_bio;
  321. bio->next_bio = NULL;
  322. return ret;
  323. }
  324. BIO *BIO_next(BIO *bio) {
  325. if (!bio) {
  326. return NULL;
  327. }
  328. return bio->next_bio;
  329. }
  330. BIO *BIO_find_type(BIO *bio, int type) {
  331. int method_type, mask;
  332. if (!bio) {
  333. return NULL;
  334. }
  335. mask = type & 0xff;
  336. do {
  337. if (bio->method != NULL) {
  338. method_type = bio->method->type;
  339. if (!mask) {
  340. if (method_type & type) {
  341. return bio;
  342. }
  343. } else if (method_type == type) {
  344. return bio;
  345. }
  346. }
  347. bio = bio->next_bio;
  348. } while (bio != NULL);
  349. return NULL;
  350. }
  351. int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
  352. if (indent > max_indent) {
  353. indent = max_indent;
  354. }
  355. while (indent--) {
  356. if (BIO_puts(bio, " ") != 1) {
  357. return 0;
  358. }
  359. }
  360. return 1;
  361. }
  362. static int print_bio(const char *str, size_t len, void *bio) {
  363. return BIO_write((BIO *)bio, str, len);
  364. }
  365. void BIO_print_errors(BIO *bio) {
  366. ERR_print_errors_cb(print_bio, bio);
  367. }
  368. /* bio_read_all reads everything from |bio| and prepends |prefix| to it. On
  369. * success, |*out| is set to an allocated buffer (which should be freed with
  370. * |OPENSSL_free|), |*out_len| is set to its length and one is returned. The
  371. * buffer will contain |prefix| followed by the contents of |bio|. On failure,
  372. * zero is returned.
  373. *
  374. * The function will fail if the size of the output would equal or exceed
  375. * |max_len|. */
  376. static int bio_read_all(BIO *bio, uint8_t **out, size_t *out_len,
  377. const uint8_t *prefix, size_t prefix_len,
  378. size_t max_len) {
  379. static const size_t kChunkSize = 4096;
  380. size_t len = prefix_len + kChunkSize;
  381. if (len > max_len) {
  382. len = max_len;
  383. }
  384. if (len < prefix_len) {
  385. return 0;
  386. }
  387. *out = OPENSSL_malloc(len);
  388. if (*out == NULL) {
  389. return 0;
  390. }
  391. memcpy(*out, prefix, prefix_len);
  392. size_t done = prefix_len;
  393. for (;;) {
  394. if (done == len) {
  395. OPENSSL_free(*out);
  396. return 0;
  397. }
  398. const size_t todo = len - done;
  399. assert(todo < INT_MAX);
  400. const int n = BIO_read(bio, *out + done, todo);
  401. if (n == 0) {
  402. *out_len = done;
  403. return 1;
  404. } else if (n == -1) {
  405. OPENSSL_free(*out);
  406. return 0;
  407. }
  408. done += n;
  409. if (len < max_len && len - done < kChunkSize / 2) {
  410. len += kChunkSize;
  411. if (len < kChunkSize || len > max_len) {
  412. len = max_len;
  413. }
  414. uint8_t *new_buf = OPENSSL_realloc(*out, len);
  415. if (new_buf == NULL) {
  416. OPENSSL_free(*out);
  417. return 0;
  418. }
  419. *out = new_buf;
  420. }
  421. }
  422. }
  423. int BIO_read_asn1(BIO *bio, uint8_t **out, size_t *out_len, size_t max_len) {
  424. uint8_t header[6];
  425. static const size_t kInitialHeaderLen = 2;
  426. if (BIO_read(bio, header, kInitialHeaderLen) != kInitialHeaderLen) {
  427. return 0;
  428. }
  429. const uint8_t tag = header[0];
  430. const uint8_t length_byte = header[1];
  431. if ((tag & 0x1f) == 0x1f) {
  432. /* Long form tags are not supported. */
  433. return 0;
  434. }
  435. size_t len, header_len;
  436. if ((length_byte & 0x80) == 0) {
  437. /* Short form length. */
  438. len = length_byte;
  439. header_len = kInitialHeaderLen;
  440. } else {
  441. const size_t num_bytes = length_byte & 0x7f;
  442. if ((tag & 0x20 /* constructed */) != 0 && num_bytes == 0) {
  443. /* indefinite length. */
  444. return bio_read_all(bio, out, out_len, header, kInitialHeaderLen,
  445. max_len);
  446. }
  447. if (num_bytes == 0 || num_bytes > 4) {
  448. return 0;
  449. }
  450. if (BIO_read(bio, header + kInitialHeaderLen, num_bytes) != num_bytes) {
  451. return 0;
  452. }
  453. header_len = kInitialHeaderLen + num_bytes;
  454. uint32_t len32 = 0;
  455. unsigned i;
  456. for (i = 0; i < num_bytes; i++) {
  457. len32 <<= 8;
  458. len32 |= header[kInitialHeaderLen + i];
  459. }
  460. if (len32 < 128) {
  461. /* Length should have used short-form encoding. */
  462. return 0;
  463. }
  464. if ((len32 >> ((num_bytes-1)*8)) == 0) {
  465. /* Length should have been at least one byte shorter. */
  466. return 0;
  467. }
  468. len = len32;
  469. }
  470. if (len + header_len < len ||
  471. len + header_len > max_len) {
  472. return 0;
  473. }
  474. len += header_len;
  475. *out_len = len;
  476. *out = OPENSSL_malloc(len);
  477. if (*out == NULL) {
  478. return 0;
  479. }
  480. memcpy(*out, header, header_len);
  481. if (BIO_read(bio, (*out) + header_len, len - header_len) !=
  482. len - header_len) {
  483. OPENSSL_free(*out);
  484. return 0;
  485. }
  486. return 1;
  487. }