Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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