bf_lbuf.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  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. */
  57. #include <stdio.h>
  58. #include <errno.h>
  59. #include "internal/cryptlib.h"
  60. #include <openssl/bio.h>
  61. #include <openssl/evp.h>
  62. static int linebuffer_write(BIO *h, const char *buf, int num);
  63. static int linebuffer_read(BIO *h, char *buf, int size);
  64. static int linebuffer_puts(BIO *h, const char *str);
  65. static int linebuffer_gets(BIO *h, char *str, int size);
  66. static long linebuffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  67. static int linebuffer_new(BIO *h);
  68. static int linebuffer_free(BIO *data);
  69. static long linebuffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  70. /* A 10k maximum should be enough for most purposes */
  71. #define DEFAULT_LINEBUFFER_SIZE 1024*10
  72. /* #define DEBUG */
  73. static BIO_METHOD methods_linebuffer = {
  74. BIO_TYPE_LINEBUFFER,
  75. "linebuffer",
  76. linebuffer_write,
  77. linebuffer_read,
  78. linebuffer_puts,
  79. linebuffer_gets,
  80. linebuffer_ctrl,
  81. linebuffer_new,
  82. linebuffer_free,
  83. linebuffer_callback_ctrl,
  84. };
  85. BIO_METHOD *BIO_f_linebuffer(void)
  86. {
  87. return (&methods_linebuffer);
  88. }
  89. typedef struct bio_linebuffer_ctx_struct {
  90. char *obuf; /* the output char array */
  91. int obuf_size; /* how big is the output buffer */
  92. int obuf_len; /* how many bytes are in it */
  93. } BIO_LINEBUFFER_CTX;
  94. static int linebuffer_new(BIO *bi)
  95. {
  96. BIO_LINEBUFFER_CTX *ctx;
  97. ctx = OPENSSL_malloc(sizeof(*ctx));
  98. if (ctx == NULL)
  99. return (0);
  100. ctx->obuf = OPENSSL_malloc(DEFAULT_LINEBUFFER_SIZE);
  101. if (ctx->obuf == NULL) {
  102. OPENSSL_free(ctx);
  103. return (0);
  104. }
  105. ctx->obuf_size = DEFAULT_LINEBUFFER_SIZE;
  106. ctx->obuf_len = 0;
  107. bi->init = 1;
  108. bi->ptr = (char *)ctx;
  109. bi->flags = 0;
  110. return (1);
  111. }
  112. static int linebuffer_free(BIO *a)
  113. {
  114. BIO_LINEBUFFER_CTX *b;
  115. if (a == NULL)
  116. return (0);
  117. b = (BIO_LINEBUFFER_CTX *)a->ptr;
  118. OPENSSL_free(b->obuf);
  119. OPENSSL_free(a->ptr);
  120. a->ptr = NULL;
  121. a->init = 0;
  122. a->flags = 0;
  123. return (1);
  124. }
  125. static int linebuffer_read(BIO *b, char *out, int outl)
  126. {
  127. int ret = 0;
  128. if (out == NULL)
  129. return (0);
  130. if (b->next_bio == NULL)
  131. return (0);
  132. ret = BIO_read(b->next_bio, out, outl);
  133. BIO_clear_retry_flags(b);
  134. BIO_copy_next_retry(b);
  135. return (ret);
  136. }
  137. static int linebuffer_write(BIO *b, const char *in, int inl)
  138. {
  139. int i, num = 0, foundnl;
  140. BIO_LINEBUFFER_CTX *ctx;
  141. if ((in == NULL) || (inl <= 0))
  142. return (0);
  143. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  144. if ((ctx == NULL) || (b->next_bio == NULL))
  145. return (0);
  146. BIO_clear_retry_flags(b);
  147. do {
  148. const char *p;
  149. for (p = in; p < in + inl && *p != '\n'; p++) ;
  150. if (*p == '\n') {
  151. p++;
  152. foundnl = 1;
  153. } else
  154. foundnl = 0;
  155. /*
  156. * If a NL was found and we already have text in the save buffer,
  157. * concatenate them and write
  158. */
  159. while ((foundnl || p - in > ctx->obuf_size - ctx->obuf_len)
  160. && ctx->obuf_len > 0) {
  161. int orig_olen = ctx->obuf_len;
  162. i = ctx->obuf_size - ctx->obuf_len;
  163. if (p - in > 0) {
  164. if (i >= p - in) {
  165. memcpy(&(ctx->obuf[ctx->obuf_len]), in, p - in);
  166. ctx->obuf_len += p - in;
  167. inl -= p - in;
  168. num += p - in;
  169. in = p;
  170. } else {
  171. memcpy(&(ctx->obuf[ctx->obuf_len]), in, i);
  172. ctx->obuf_len += i;
  173. inl -= i;
  174. in += i;
  175. num += i;
  176. }
  177. }
  178. i = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  179. if (i <= 0) {
  180. ctx->obuf_len = orig_olen;
  181. BIO_copy_next_retry(b);
  182. if (i < 0)
  183. return ((num > 0) ? num : i);
  184. if (i == 0)
  185. return (num);
  186. }
  187. if (i < ctx->obuf_len)
  188. memmove(ctx->obuf, ctx->obuf + i, ctx->obuf_len - i);
  189. ctx->obuf_len -= i;
  190. }
  191. /*
  192. * Now that the save buffer is emptied, let's write the input buffer
  193. * if a NL was found and there is anything to write.
  194. */
  195. if ((foundnl || p - in > ctx->obuf_size) && p - in > 0) {
  196. i = BIO_write(b->next_bio, in, p - in);
  197. if (i <= 0) {
  198. BIO_copy_next_retry(b);
  199. if (i < 0)
  200. return ((num > 0) ? num : i);
  201. if (i == 0)
  202. return (num);
  203. }
  204. num += i;
  205. in += i;
  206. inl -= i;
  207. }
  208. }
  209. while (foundnl && inl > 0);
  210. /*
  211. * We've written as much as we can. The rest of the input buffer, if
  212. * any, is text that doesn't and with a NL and therefore needs to be
  213. * saved for the next trip.
  214. */
  215. if (inl > 0) {
  216. memcpy(&(ctx->obuf[ctx->obuf_len]), in, inl);
  217. ctx->obuf_len += inl;
  218. num += inl;
  219. }
  220. return num;
  221. }
  222. static long linebuffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  223. {
  224. BIO *dbio;
  225. BIO_LINEBUFFER_CTX *ctx;
  226. long ret = 1;
  227. char *p;
  228. int r;
  229. int obs;
  230. ctx = (BIO_LINEBUFFER_CTX *)b->ptr;
  231. switch (cmd) {
  232. case BIO_CTRL_RESET:
  233. ctx->obuf_len = 0;
  234. if (b->next_bio == NULL)
  235. return (0);
  236. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  237. break;
  238. case BIO_CTRL_INFO:
  239. ret = (long)ctx->obuf_len;
  240. break;
  241. case BIO_CTRL_WPENDING:
  242. ret = (long)ctx->obuf_len;
  243. if (ret == 0) {
  244. if (b->next_bio == NULL)
  245. return (0);
  246. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  247. }
  248. break;
  249. case BIO_C_SET_BUFF_SIZE:
  250. obs = (int)num;
  251. p = ctx->obuf;
  252. if ((obs > DEFAULT_LINEBUFFER_SIZE) && (obs != ctx->obuf_size)) {
  253. p = OPENSSL_malloc((int)num);
  254. if (p == NULL)
  255. goto malloc_error;
  256. }
  257. if (ctx->obuf != p) {
  258. if (ctx->obuf_len > obs) {
  259. ctx->obuf_len = obs;
  260. }
  261. memcpy(p, ctx->obuf, ctx->obuf_len);
  262. OPENSSL_free(ctx->obuf);
  263. ctx->obuf = p;
  264. ctx->obuf_size = obs;
  265. }
  266. break;
  267. case BIO_C_DO_STATE_MACHINE:
  268. if (b->next_bio == NULL)
  269. return (0);
  270. BIO_clear_retry_flags(b);
  271. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  272. BIO_copy_next_retry(b);
  273. break;
  274. case BIO_CTRL_FLUSH:
  275. if (b->next_bio == NULL)
  276. return (0);
  277. if (ctx->obuf_len <= 0) {
  278. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  279. break;
  280. }
  281. for (;;) {
  282. BIO_clear_retry_flags(b);
  283. if (ctx->obuf_len > 0) {
  284. r = BIO_write(b->next_bio, ctx->obuf, ctx->obuf_len);
  285. BIO_copy_next_retry(b);
  286. if (r <= 0)
  287. return ((long)r);
  288. if (r < ctx->obuf_len)
  289. memmove(ctx->obuf, ctx->obuf + r, ctx->obuf_len - r);
  290. ctx->obuf_len -= r;
  291. } else {
  292. ctx->obuf_len = 0;
  293. ret = 1;
  294. break;
  295. }
  296. }
  297. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  298. break;
  299. case BIO_CTRL_DUP:
  300. dbio = (BIO *)ptr;
  301. if (!BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  302. ret = 0;
  303. break;
  304. default:
  305. if (b->next_bio == NULL)
  306. return (0);
  307. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  308. break;
  309. }
  310. return (ret);
  311. malloc_error:
  312. BIOerr(BIO_F_LINEBUFFER_CTRL, ERR_R_MALLOC_FAILURE);
  313. return (0);
  314. }
  315. static long linebuffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  316. {
  317. long ret = 1;
  318. if (b->next_bio == NULL)
  319. return (0);
  320. switch (cmd) {
  321. default:
  322. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  323. break;
  324. }
  325. return (ret);
  326. }
  327. static int linebuffer_gets(BIO *b, char *buf, int size)
  328. {
  329. if (b->next_bio == NULL)
  330. return (0);
  331. return (BIO_gets(b->next_bio, buf, size));
  332. }
  333. static int linebuffer_puts(BIO *b, const char *str)
  334. {
  335. return (linebuffer_write(b, str, strlen(str)));
  336. }