bf_buff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. static int buffer_write(BIO *h, const char *buf, int num);
  62. static int buffer_read(BIO *h, char *buf, int size);
  63. static int buffer_puts(BIO *h, const char *str);
  64. static int buffer_gets(BIO *h, char *str, int size);
  65. static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  66. static int buffer_new(BIO *h);
  67. static int buffer_free(BIO *data);
  68. static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  69. #define DEFAULT_BUFFER_SIZE 4096
  70. static BIO_METHOD methods_buffer = {
  71. BIO_TYPE_BUFFER,
  72. "buffer",
  73. buffer_write,
  74. buffer_read,
  75. buffer_puts,
  76. buffer_gets,
  77. buffer_ctrl,
  78. buffer_new,
  79. buffer_free,
  80. buffer_callback_ctrl,
  81. };
  82. BIO_METHOD *BIO_f_buffer(void)
  83. {
  84. return (&methods_buffer);
  85. }
  86. static int buffer_new(BIO *bi)
  87. {
  88. BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  89. if (ctx == NULL)
  90. return (0);
  91. ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
  92. ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  93. if (ctx->ibuf == NULL) {
  94. OPENSSL_free(ctx);
  95. return (0);
  96. }
  97. ctx->obuf_size = DEFAULT_BUFFER_SIZE;
  98. ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  99. if (ctx->obuf == NULL) {
  100. OPENSSL_free(ctx->ibuf);
  101. OPENSSL_free(ctx);
  102. return (0);
  103. }
  104. bi->init = 1;
  105. bi->ptr = (char *)ctx;
  106. bi->flags = 0;
  107. return (1);
  108. }
  109. static int buffer_free(BIO *a)
  110. {
  111. BIO_F_BUFFER_CTX *b;
  112. if (a == NULL)
  113. return (0);
  114. b = (BIO_F_BUFFER_CTX *)a->ptr;
  115. OPENSSL_free(b->ibuf);
  116. OPENSSL_free(b->obuf);
  117. OPENSSL_free(a->ptr);
  118. a->ptr = NULL;
  119. a->init = 0;
  120. a->flags = 0;
  121. return (1);
  122. }
  123. static int buffer_read(BIO *b, char *out, int outl)
  124. {
  125. int i, num = 0;
  126. BIO_F_BUFFER_CTX *ctx;
  127. if (out == NULL)
  128. return (0);
  129. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  130. if ((ctx == NULL) || (b->next_bio == NULL))
  131. return (0);
  132. num = 0;
  133. BIO_clear_retry_flags(b);
  134. start:
  135. i = ctx->ibuf_len;
  136. /* If there is stuff left over, grab it */
  137. if (i != 0) {
  138. if (i > outl)
  139. i = outl;
  140. memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
  141. ctx->ibuf_off += i;
  142. ctx->ibuf_len -= i;
  143. num += i;
  144. if (outl == i)
  145. return (num);
  146. outl -= i;
  147. out += i;
  148. }
  149. /*
  150. * We may have done a partial read. try to do more. We have nothing in
  151. * the buffer. If we get an error and have read some data, just return it
  152. * and let them retry to get the error again. copy direct to parent
  153. * address space
  154. */
  155. if (outl > ctx->ibuf_size) {
  156. for (;;) {
  157. i = BIO_read(b->next_bio, out, outl);
  158. if (i <= 0) {
  159. BIO_copy_next_retry(b);
  160. if (i < 0)
  161. return ((num > 0) ? num : i);
  162. if (i == 0)
  163. return (num);
  164. }
  165. num += i;
  166. if (outl == i)
  167. return (num);
  168. out += i;
  169. outl -= i;
  170. }
  171. }
  172. /* else */
  173. /* we are going to be doing some buffering */
  174. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  175. if (i <= 0) {
  176. BIO_copy_next_retry(b);
  177. if (i < 0)
  178. return ((num > 0) ? num : i);
  179. if (i == 0)
  180. return (num);
  181. }
  182. ctx->ibuf_off = 0;
  183. ctx->ibuf_len = i;
  184. /* Lets re-read using ourselves :-) */
  185. goto start;
  186. }
  187. static int buffer_write(BIO *b, const char *in, int inl)
  188. {
  189. int i, num = 0;
  190. BIO_F_BUFFER_CTX *ctx;
  191. if ((in == NULL) || (inl <= 0))
  192. return (0);
  193. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  194. if ((ctx == NULL) || (b->next_bio == NULL))
  195. return (0);
  196. BIO_clear_retry_flags(b);
  197. start:
  198. i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
  199. /* add to buffer and return */
  200. if (i >= inl) {
  201. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
  202. ctx->obuf_len += inl;
  203. return (num + inl);
  204. }
  205. /* else */
  206. /* stuff already in buffer, so add to it first, then flush */
  207. if (ctx->obuf_len != 0) {
  208. if (i > 0) { /* lets fill it up if we can */
  209. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
  210. in += i;
  211. inl -= i;
  212. num += i;
  213. ctx->obuf_len += i;
  214. }
  215. /* we now have a full buffer needing flushing */
  216. for (;;) {
  217. i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
  218. ctx->obuf_len);
  219. if (i <= 0) {
  220. BIO_copy_next_retry(b);
  221. if (i < 0)
  222. return ((num > 0) ? num : i);
  223. if (i == 0)
  224. return (num);
  225. }
  226. ctx->obuf_off += i;
  227. ctx->obuf_len -= i;
  228. if (ctx->obuf_len == 0)
  229. break;
  230. }
  231. }
  232. /*
  233. * we only get here if the buffer has been flushed and we still have
  234. * stuff to write
  235. */
  236. ctx->obuf_off = 0;
  237. /* we now have inl bytes to write */
  238. while (inl >= ctx->obuf_size) {
  239. i = BIO_write(b->next_bio, in, inl);
  240. if (i <= 0) {
  241. BIO_copy_next_retry(b);
  242. if (i < 0)
  243. return ((num > 0) ? num : i);
  244. if (i == 0)
  245. return (num);
  246. }
  247. num += i;
  248. in += i;
  249. inl -= i;
  250. if (inl == 0)
  251. return (num);
  252. }
  253. /*
  254. * copy the rest into the buffer since we have only a small amount left
  255. */
  256. goto start;
  257. }
  258. static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  259. {
  260. BIO *dbio;
  261. BIO_F_BUFFER_CTX *ctx;
  262. long ret = 1;
  263. char *p1, *p2;
  264. int r, i, *ip;
  265. int ibs, obs;
  266. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  267. switch (cmd) {
  268. case BIO_CTRL_RESET:
  269. ctx->ibuf_off = 0;
  270. ctx->ibuf_len = 0;
  271. ctx->obuf_off = 0;
  272. ctx->obuf_len = 0;
  273. if (b->next_bio == NULL)
  274. return (0);
  275. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  276. break;
  277. case BIO_CTRL_INFO:
  278. ret = (long)ctx->obuf_len;
  279. break;
  280. case BIO_C_GET_BUFF_NUM_LINES:
  281. ret = 0;
  282. p1 = ctx->ibuf;
  283. for (i = 0; i < ctx->ibuf_len; i++) {
  284. if (p1[ctx->ibuf_off + i] == '\n')
  285. ret++;
  286. }
  287. break;
  288. case BIO_CTRL_WPENDING:
  289. ret = (long)ctx->obuf_len;
  290. if (ret == 0) {
  291. if (b->next_bio == NULL)
  292. return (0);
  293. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  294. }
  295. break;
  296. case BIO_CTRL_PENDING:
  297. ret = (long)ctx->ibuf_len;
  298. if (ret == 0) {
  299. if (b->next_bio == NULL)
  300. return (0);
  301. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  302. }
  303. break;
  304. case BIO_C_SET_BUFF_READ_DATA:
  305. if (num > ctx->ibuf_size) {
  306. p1 = OPENSSL_malloc((int)num);
  307. if (p1 == NULL)
  308. goto malloc_error;
  309. OPENSSL_free(ctx->ibuf);
  310. ctx->ibuf = p1;
  311. }
  312. ctx->ibuf_off = 0;
  313. ctx->ibuf_len = (int)num;
  314. memcpy(ctx->ibuf, ptr, (int)num);
  315. ret = 1;
  316. break;
  317. case BIO_C_SET_BUFF_SIZE:
  318. if (ptr != NULL) {
  319. ip = (int *)ptr;
  320. if (*ip == 0) {
  321. ibs = (int)num;
  322. obs = ctx->obuf_size;
  323. } else { /* if (*ip == 1) */
  324. ibs = ctx->ibuf_size;
  325. obs = (int)num;
  326. }
  327. } else {
  328. ibs = (int)num;
  329. obs = (int)num;
  330. }
  331. p1 = ctx->ibuf;
  332. p2 = ctx->obuf;
  333. if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
  334. p1 = OPENSSL_malloc((int)num);
  335. if (p1 == NULL)
  336. goto malloc_error;
  337. }
  338. if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
  339. p2 = OPENSSL_malloc((int)num);
  340. if (p2 == NULL) {
  341. if (p1 != ctx->ibuf)
  342. OPENSSL_free(p1);
  343. goto malloc_error;
  344. }
  345. }
  346. if (ctx->ibuf != p1) {
  347. OPENSSL_free(ctx->ibuf);
  348. ctx->ibuf = p1;
  349. ctx->ibuf_off = 0;
  350. ctx->ibuf_len = 0;
  351. ctx->ibuf_size = ibs;
  352. }
  353. if (ctx->obuf != p2) {
  354. OPENSSL_free(ctx->obuf);
  355. ctx->obuf = p2;
  356. ctx->obuf_off = 0;
  357. ctx->obuf_len = 0;
  358. ctx->obuf_size = obs;
  359. }
  360. break;
  361. case BIO_C_DO_STATE_MACHINE:
  362. if (b->next_bio == NULL)
  363. return (0);
  364. BIO_clear_retry_flags(b);
  365. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  366. BIO_copy_next_retry(b);
  367. break;
  368. case BIO_CTRL_FLUSH:
  369. if (b->next_bio == NULL)
  370. return (0);
  371. if (ctx->obuf_len <= 0) {
  372. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  373. break;
  374. }
  375. for (;;) {
  376. BIO_clear_retry_flags(b);
  377. if (ctx->obuf_len > 0) {
  378. r = BIO_write(b->next_bio,
  379. &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
  380. BIO_copy_next_retry(b);
  381. if (r <= 0)
  382. return ((long)r);
  383. ctx->obuf_off += r;
  384. ctx->obuf_len -= r;
  385. } else {
  386. ctx->obuf_len = 0;
  387. ctx->obuf_off = 0;
  388. ret = 1;
  389. break;
  390. }
  391. }
  392. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  393. break;
  394. case BIO_CTRL_DUP:
  395. dbio = (BIO *)ptr;
  396. if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
  397. !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  398. ret = 0;
  399. break;
  400. default:
  401. if (b->next_bio == NULL)
  402. return (0);
  403. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  404. break;
  405. }
  406. return (ret);
  407. malloc_error:
  408. BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
  409. return (0);
  410. }
  411. static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  412. {
  413. long ret = 1;
  414. if (b->next_bio == NULL)
  415. return (0);
  416. switch (cmd) {
  417. default:
  418. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  419. break;
  420. }
  421. return (ret);
  422. }
  423. static int buffer_gets(BIO *b, char *buf, int size)
  424. {
  425. BIO_F_BUFFER_CTX *ctx;
  426. int num = 0, i, flag;
  427. char *p;
  428. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  429. size--; /* reserve space for a '\0' */
  430. BIO_clear_retry_flags(b);
  431. for (;;) {
  432. if (ctx->ibuf_len > 0) {
  433. p = &(ctx->ibuf[ctx->ibuf_off]);
  434. flag = 0;
  435. for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
  436. *(buf++) = p[i];
  437. if (p[i] == '\n') {
  438. flag = 1;
  439. i++;
  440. break;
  441. }
  442. }
  443. num += i;
  444. size -= i;
  445. ctx->ibuf_len -= i;
  446. ctx->ibuf_off += i;
  447. if (flag || size == 0) {
  448. *buf = '\0';
  449. return (num);
  450. }
  451. } else { /* read another chunk */
  452. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  453. if (i <= 0) {
  454. BIO_copy_next_retry(b);
  455. *buf = '\0';
  456. if (i < 0)
  457. return ((num > 0) ? num : i);
  458. if (i == 0)
  459. return (num);
  460. }
  461. ctx->ibuf_len = i;
  462. ctx->ibuf_off = 0;
  463. }
  464. }
  465. }
  466. static int buffer_puts(BIO *b, const char *str)
  467. {
  468. return (buffer_write(b, str, strlen(str)));
  469. }