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

477 строки
12 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 <errno.h>
  58. #include <stddef.h>
  59. #include <limits.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/thread.h>
  63. /* BIO_set initialises a BIO structure to have the given type and sets the
  64. * reference count to one. It returns one on success or zero on error. */
  65. static int bio_set(BIO *bio, const BIO_METHOD *method) {
  66. /* This function can be called with a stack allocated |BIO| so we have to
  67. * assume that the contents of |BIO| are arbitary. This also means that it'll
  68. * leak memory if you call |BIO_set| twice on the same BIO. */
  69. memset(bio, 0, sizeof(BIO));
  70. bio->method = method;
  71. bio->shutdown = 1;
  72. bio->references = 1;
  73. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data)) {
  74. return 0;
  75. }
  76. if (method->create != NULL) {
  77. if (!method->create(bio)) {
  78. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  79. return 0;
  80. }
  81. }
  82. return 1;
  83. }
  84. BIO *BIO_new(const BIO_METHOD *method) {
  85. BIO *ret = OPENSSL_malloc(sizeof(BIO));
  86. if (ret == NULL) {
  87. OPENSSL_PUT_ERROR(BIO, BIO_new, ERR_R_MALLOC_FAILURE);
  88. return NULL;
  89. }
  90. if (!bio_set(ret, method)) {
  91. OPENSSL_free(ret);
  92. ret = NULL;
  93. }
  94. return ret;
  95. }
  96. int BIO_free(BIO *bio) {
  97. BIO *next_bio;
  98. for (; bio != NULL; bio = next_bio) {
  99. int refs = CRYPTO_add(&bio->references, -1, CRYPTO_LOCK_BIO);
  100. if (refs > 0) {
  101. return 0;
  102. }
  103. if (bio->callback != NULL) {
  104. int i = (int)bio->callback(bio, BIO_CB_FREE, NULL, 0, 0, 1);
  105. if (i <= 0) {
  106. return i;
  107. }
  108. }
  109. next_bio = BIO_pop(bio);
  110. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_BIO, bio, &bio->ex_data);
  111. if (bio->method != NULL && bio->method->destroy != NULL) {
  112. bio->method->destroy(bio);
  113. }
  114. OPENSSL_free(bio);
  115. }
  116. return 1;
  117. }
  118. void BIO_vfree(BIO *bio) {
  119. BIO_free(bio);
  120. }
  121. void BIO_free_all(BIO *bio) {
  122. BIO_free(bio);
  123. }
  124. static int bio_io(BIO *bio, void *buf, int len, size_t method_offset,
  125. int callback_flags, size_t *num) {
  126. int i;
  127. typedef int (*io_func_t)(BIO *, char *, int);
  128. io_func_t io_func = NULL;
  129. if (bio != NULL && bio->method != NULL) {
  130. io_func =
  131. *((const io_func_t *)(((const uint8_t *)bio->method) + method_offset));
  132. }
  133. if (io_func == NULL) {
  134. OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNSUPPORTED_METHOD);
  135. return -2;
  136. }
  137. if (bio->callback != NULL) {
  138. i = (int) bio->callback(bio, callback_flags, buf, len, 0L, 1L);
  139. if (i <= 0) {
  140. return i;
  141. }
  142. }
  143. if (!bio->init) {
  144. OPENSSL_PUT_ERROR(BIO, bio_io, BIO_R_UNINITIALIZED);
  145. return -2;
  146. }
  147. i = 0;
  148. if (buf != NULL && len > 0) {
  149. i = io_func(bio, buf, len);
  150. }
  151. if (i > 0) {
  152. *num += i;
  153. }
  154. if (bio->callback != NULL) {
  155. i = (int)(bio->callback(bio, callback_flags | BIO_CB_RETURN, buf, len, 0L,
  156. (long)i));
  157. }
  158. return i;
  159. }
  160. int BIO_read(BIO *bio, void *buf, int len) {
  161. return bio_io(bio, buf, len, offsetof(BIO_METHOD, bread), BIO_CB_READ,
  162. &bio->num_read);
  163. }
  164. int BIO_gets(BIO *bio, char *buf, int len) {
  165. return bio_io(bio, buf, len, offsetof(BIO_METHOD, bgets), BIO_CB_GETS,
  166. &bio->num_read);
  167. }
  168. int BIO_write(BIO *bio, const void *in, int inl) {
  169. return bio_io(bio, (char *)in, inl, offsetof(BIO_METHOD, bwrite),
  170. BIO_CB_WRITE, &bio->num_write);
  171. }
  172. int BIO_puts(BIO *bio, const char *in) {
  173. return BIO_write(bio, in, strlen(in));
  174. }
  175. int BIO_flush(BIO *bio) {
  176. return BIO_ctrl(bio, BIO_CTRL_FLUSH, 0, NULL);
  177. }
  178. long BIO_ctrl(BIO *bio, int cmd, long larg, void *parg) {
  179. long ret;
  180. if (bio == NULL) {
  181. return 0;
  182. }
  183. if (bio->method == NULL || bio->method->ctrl == NULL) {
  184. OPENSSL_PUT_ERROR(BIO, BIO_ctrl, BIO_R_UNSUPPORTED_METHOD);
  185. return -2;
  186. }
  187. if (bio->callback != NULL) {
  188. ret = bio->callback(bio, BIO_CB_CTRL, parg, cmd, larg, 1);
  189. if (ret <= 0) {
  190. return ret;
  191. }
  192. }
  193. ret = bio->method->ctrl(bio, cmd, larg, parg);
  194. if (bio->callback != NULL) {
  195. ret = bio->callback(bio, BIO_CB_CTRL | BIO_CB_RETURN, parg, cmd, larg, ret);
  196. }
  197. return ret;
  198. }
  199. char *BIO_ptr_ctrl(BIO *b, int cmd, long larg) {
  200. char *p = NULL;
  201. if (BIO_ctrl(b, cmd, larg, (void *)&p) <= 0) {
  202. return NULL;
  203. }
  204. return p;
  205. }
  206. long BIO_int_ctrl(BIO *b, int cmd, long larg, int iarg) {
  207. int i = iarg;
  208. return BIO_ctrl(b, cmd, larg, (void *)&i);
  209. }
  210. int BIO_reset(BIO *bio) {
  211. return BIO_ctrl(bio, BIO_CTRL_RESET, 0, NULL);
  212. }
  213. void BIO_set_flags(BIO *bio, int flags) {
  214. bio->flags |= flags;
  215. }
  216. int BIO_test_flags(const BIO *bio, int flags) {
  217. return bio->flags & flags;
  218. }
  219. int BIO_should_read(const BIO *bio) {
  220. return BIO_test_flags(bio, BIO_FLAGS_READ);
  221. }
  222. int BIO_should_write(const BIO *bio) {
  223. return BIO_test_flags(bio, BIO_FLAGS_WRITE);
  224. }
  225. int BIO_should_retry(const BIO *bio) {
  226. return BIO_test_flags(bio, BIO_FLAGS_SHOULD_RETRY);
  227. }
  228. int BIO_should_io_special(const BIO *bio) {
  229. return BIO_test_flags(bio, BIO_FLAGS_IO_SPECIAL);
  230. }
  231. int BIO_get_retry_reason(const BIO *bio) { return bio->retry_reason; }
  232. void BIO_clear_flags(BIO *bio, int flags) {
  233. bio->flags &= ~flags;
  234. }
  235. void BIO_set_retry_read(BIO *bio) {
  236. bio->flags |= BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY;
  237. }
  238. void BIO_set_retry_write(BIO *bio) {
  239. bio->flags |= BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY;
  240. }
  241. static const int kRetryFlags = BIO_FLAGS_RWS | BIO_FLAGS_SHOULD_RETRY;
  242. int BIO_get_retry_flags(BIO *bio) {
  243. return bio->flags & kRetryFlags;
  244. }
  245. void BIO_clear_retry_flags(BIO *bio) {
  246. bio->flags &= ~kRetryFlags;
  247. bio->retry_reason = 0;
  248. }
  249. int BIO_method_type(const BIO *bio) { return bio->method->type; }
  250. void BIO_copy_next_retry(BIO *bio) {
  251. BIO_clear_retry_flags(bio);
  252. BIO_set_flags(bio, BIO_get_retry_flags(bio->next_bio));
  253. bio->retry_reason = bio->next_bio->retry_reason;
  254. }
  255. long BIO_callback_ctrl(BIO *bio, int cmd, bio_info_cb fp) {
  256. long ret;
  257. bio_info_cb cb;
  258. if (bio == NULL) {
  259. return 0;
  260. }
  261. if (bio->method == NULL || bio->method->callback_ctrl == NULL) {
  262. OPENSSL_PUT_ERROR(BIO, BIO_callback_ctrl, BIO_R_UNSUPPORTED_METHOD);
  263. return 0;
  264. }
  265. cb = bio->callback;
  266. if (cb != NULL) {
  267. ret = cb(bio, BIO_CB_CTRL, (void *)&fp, cmd, 0, 1L);
  268. if (ret <= 0) {
  269. return ret;
  270. }
  271. }
  272. ret = bio->method->callback_ctrl(bio, cmd, fp);
  273. if (cb != NULL) {
  274. ret = cb(bio, BIO_CB_CTRL | BIO_CB_RETURN, (void *)&fp, cmd, 0, ret);
  275. }
  276. return ret;
  277. }
  278. size_t BIO_pending(const BIO *bio) {
  279. return BIO_ctrl((BIO *) bio, BIO_CTRL_PENDING, 0, NULL);
  280. }
  281. size_t BIO_ctrl_pending(const BIO *bio) {
  282. return BIO_pending(bio);
  283. }
  284. size_t BIO_wpending(const BIO *bio) {
  285. return BIO_ctrl((BIO *) bio, BIO_CTRL_WPENDING, 0, NULL);
  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. /* TODO(fork): this seems very suspect. If we got rid of BIO SSL, we could
  316. * get rid of this. */
  317. BIO_ctrl(bio, BIO_CTRL_PUSH, 0, bio);
  318. return bio;
  319. }
  320. BIO *BIO_pop(BIO *bio) {
  321. BIO *ret;
  322. if (bio == NULL) {
  323. return NULL;
  324. }
  325. ret = bio->next_bio;
  326. BIO_ctrl(bio, BIO_CTRL_POP, 0, bio);
  327. bio->next_bio = NULL;
  328. return ret;
  329. }
  330. BIO *BIO_next(BIO *bio) {
  331. if (!bio) {
  332. return NULL;
  333. }
  334. return bio->next_bio;
  335. }
  336. BIO *BIO_find_type(BIO *bio, int type) {
  337. int method_type, mask;
  338. if (!bio) {
  339. return NULL;
  340. }
  341. mask = type & 0xff;
  342. do {
  343. if (bio->method != NULL) {
  344. method_type = bio->method->type;
  345. if (!mask) {
  346. if (method_type & type) {
  347. return bio;
  348. }
  349. } else if (method_type == type) {
  350. return bio;
  351. }
  352. }
  353. bio = bio->next_bio;
  354. } while (bio != NULL);
  355. return NULL;
  356. }
  357. int BIO_indent(BIO *bio, unsigned indent, unsigned max_indent) {
  358. if (indent > max_indent) {
  359. indent = max_indent;
  360. }
  361. while (indent--) {
  362. if (BIO_puts(bio, " ") != 1) {
  363. return 0;
  364. }
  365. }
  366. return 1;
  367. }
  368. void BIO_print_errors_fp(FILE *out) {
  369. BIO *bio = BIO_new_fp(out, BIO_NOCLOSE);
  370. BIO_print_errors(bio);
  371. BIO_free(bio);
  372. }
  373. static int print_bio(const char *str, size_t len, void *bio) {
  374. return BIO_write((BIO *)bio, str, len);
  375. }
  376. void BIO_print_errors(BIO *bio) {
  377. ERR_print_errors_cb(print_bio, bio);
  378. }