Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

bio_mem.c 8.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 <limits.h>
  58. #include <string.h>
  59. #include <openssl/buf.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. BIO *BIO_new_mem_buf(void *buf, int len) {
  63. BIO *ret;
  64. BUF_MEM *b;
  65. const size_t size = len < 0 ? strlen((char *)buf) : (size_t)len;
  66. if (!buf && len != 0) {
  67. OPENSSL_PUT_ERROR(BIO, BIO_new_mem_buf, BIO_R_NULL_PARAMETER);
  68. return NULL;
  69. }
  70. ret = BIO_new(BIO_s_mem());
  71. if (ret == NULL) {
  72. return NULL;
  73. }
  74. b = (BUF_MEM *)ret->ptr;
  75. b->data = buf;
  76. b->length = size;
  77. b->max = size;
  78. ret->flags |= BIO_FLAGS_MEM_RDONLY;
  79. /* |num| is used to store the value that this BIO will return when it runs
  80. * out of data. If it's negative then the retry flags will also be set. Since
  81. * this is static data, retrying wont help */
  82. ret->num = 0;
  83. return ret;
  84. }
  85. static int mem_new(BIO *bio) {
  86. BUF_MEM *b;
  87. b = BUF_MEM_new();
  88. if (b == NULL) {
  89. return 0;
  90. }
  91. /* |shutdown| is used to store the close flag: whether the BIO has ownership
  92. * of the BUF_MEM. */
  93. bio->shutdown = 1;
  94. bio->init = 1;
  95. bio->num = -1;
  96. bio->ptr = (char *)b;
  97. return 1;
  98. }
  99. static int mem_free(BIO *bio) {
  100. BUF_MEM *b;
  101. if (bio == NULL) {
  102. return 0;
  103. }
  104. if (!bio->shutdown || !bio->init || bio->ptr == NULL) {
  105. return 1;
  106. }
  107. b = (BUF_MEM *)bio->ptr;
  108. if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
  109. b->data = NULL;
  110. }
  111. BUF_MEM_free(b);
  112. bio->ptr = NULL;
  113. return 1;
  114. }
  115. static int mem_read(BIO *bio, char *out, int outl) {
  116. int ret;
  117. BUF_MEM *b = (BUF_MEM*) bio->ptr;
  118. BIO_clear_retry_flags(bio);
  119. ret = outl;
  120. if (b->length < INT_MAX && ret > (int)b->length) {
  121. ret = b->length;
  122. }
  123. if (ret > 0) {
  124. memcpy(out, b->data, ret);
  125. b->length -= ret;
  126. if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
  127. b->data += ret;
  128. } else {
  129. memmove(b->data, &b->data[ret], b->length);
  130. }
  131. } else if (b->length == 0) {
  132. ret = bio->num;
  133. if (ret != 0) {
  134. BIO_set_retry_read(bio);
  135. }
  136. }
  137. return ret;
  138. }
  139. static int mem_write(BIO *bio, const char *in, int inl) {
  140. int ret = -1;
  141. int blen;
  142. BUF_MEM *b;
  143. b = (BUF_MEM *)bio->ptr;
  144. if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
  145. OPENSSL_PUT_ERROR(BIO, mem_write, BIO_R_WRITE_TO_READ_ONLY_BIO);
  146. goto err;
  147. }
  148. BIO_clear_retry_flags(bio);
  149. blen = b->length;
  150. if (INT_MAX - blen < inl) {
  151. goto err;
  152. }
  153. if (BUF_MEM_grow_clean(b, blen + inl) != (blen + inl)) {
  154. goto err;
  155. }
  156. memcpy(&b->data[blen], in, inl);
  157. ret = inl;
  158. err:
  159. return ret;
  160. }
  161. static int mem_puts(BIO *bp, const char *str) {
  162. return mem_write(bp, str, strlen(str));
  163. }
  164. static int mem_gets(BIO *bio, char *buf, int size) {
  165. int i, j;
  166. char *p;
  167. BUF_MEM *b = (BUF_MEM *)bio->ptr;
  168. BIO_clear_retry_flags(bio);
  169. j = b->length;
  170. if (size - 1 < j) {
  171. j = size - 1;
  172. }
  173. if (j <= 0) {
  174. if (size > 0) {
  175. *buf = 0;
  176. }
  177. return 0;
  178. }
  179. p = b->data;
  180. for (i = 0; i < j; i++) {
  181. if (p[i] == '\n') {
  182. i++;
  183. break;
  184. }
  185. }
  186. /* i is now the max num of bytes to copy, either j or up to and including the
  187. * first newline */
  188. i = mem_read(bio, buf, i);
  189. if (i > 0) {
  190. buf[i] = '\0';
  191. }
  192. return i;
  193. }
  194. static long mem_ctrl(BIO *bio, int cmd, long num, void *ptr) {
  195. long ret = 1;
  196. char **pptr;
  197. BUF_MEM *b = (BUF_MEM *)bio->ptr;
  198. switch (cmd) {
  199. case BIO_CTRL_RESET:
  200. if (b->data != NULL) {
  201. /* For read only case reset to the start again */
  202. if (bio->flags & BIO_FLAGS_MEM_RDONLY) {
  203. b->data -= b->max - b->length;
  204. b->length = b->max;
  205. } else {
  206. memset(b->data, 0, b->max);
  207. b->length = 0;
  208. }
  209. }
  210. break;
  211. case BIO_CTRL_EOF:
  212. ret = (long)(b->length == 0);
  213. break;
  214. case BIO_C_SET_BUF_MEM_EOF_RETURN:
  215. bio->num = (int)num;
  216. break;
  217. case BIO_CTRL_INFO:
  218. ret = (long)b->length;
  219. if (ptr != NULL) {
  220. pptr = (char **)ptr;
  221. *pptr = (char *)&b->data[0];
  222. }
  223. break;
  224. case BIO_C_SET_BUF_MEM:
  225. mem_free(bio);
  226. bio->shutdown = (int)num;
  227. bio->ptr = ptr;
  228. break;
  229. case BIO_C_GET_BUF_MEM_PTR:
  230. if (ptr != NULL) {
  231. pptr = (char **)ptr;
  232. *pptr = (char *)b;
  233. }
  234. break;
  235. case BIO_CTRL_GET_CLOSE:
  236. ret = (long)bio->shutdown;
  237. break;
  238. case BIO_CTRL_SET_CLOSE:
  239. bio->shutdown = (int)num;
  240. break;
  241. case BIO_CTRL_WPENDING:
  242. ret = 0L;
  243. break;
  244. case BIO_CTRL_PENDING:
  245. ret = (long)b->length;
  246. break;
  247. case BIO_CTRL_FLUSH:
  248. ret = 1;
  249. break;
  250. default:
  251. ret = 0;
  252. break;
  253. }
  254. return ret;
  255. }
  256. static const BIO_METHOD mem_method = {
  257. BIO_TYPE_MEM, "memory buffer", mem_write, mem_read, mem_puts,
  258. mem_gets, mem_ctrl, mem_new, mem_free, NULL, };
  259. const BIO_METHOD *BIO_s_mem(void) { return &mem_method; }
  260. int BIO_mem_contents(const BIO *bio, const uint8_t **out_contents,
  261. size_t *out_len) {
  262. const BUF_MEM *b;
  263. if (bio->method != &mem_method) {
  264. return 0;
  265. }
  266. b = (BUF_MEM *)bio->ptr;
  267. *out_contents = (uint8_t *)b->data;
  268. *out_len = b->length;
  269. return 1;
  270. }
  271. long BIO_get_mem_data(BIO *bio, char **contents) {
  272. return BIO_ctrl(bio, BIO_CTRL_INFO, 0, (char *) contents);
  273. }
  274. int BIO_get_mem_ptr(BIO *bio, BUF_MEM **out) {
  275. return BIO_ctrl(bio, BIO_C_GET_BUF_MEM_PTR, 0, (char *) out);
  276. }
  277. int BIO_set_mem_buf(BIO *bio, BUF_MEM *b, int take_ownership) {
  278. return BIO_ctrl(bio, BIO_C_SET_BUF_MEM, take_ownership, (char *) b);
  279. }
  280. int BIO_set_mem_eof_return(BIO *bio, int eof_value) {
  281. return BIO_ctrl(bio, BIO_C_SET_BUF_MEM_EOF_RETURN, eof_value, NULL);
  282. }