bf_buff.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509
  1. /* crypto/bio/bf_buff.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include "internal/cryptlib.h"
  61. #include <openssl/bio.h>
  62. static int buffer_write(BIO *h, const char *buf, int num);
  63. static int buffer_read(BIO *h, char *buf, int size);
  64. static int buffer_puts(BIO *h, const char *str);
  65. static int buffer_gets(BIO *h, char *str, int size);
  66. static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  67. static int buffer_new(BIO *h);
  68. static int buffer_free(BIO *data);
  69. static long buffer_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  70. #define DEFAULT_BUFFER_SIZE 4096
  71. static BIO_METHOD methods_buffer = {
  72. BIO_TYPE_BUFFER,
  73. "buffer",
  74. buffer_write,
  75. buffer_read,
  76. buffer_puts,
  77. buffer_gets,
  78. buffer_ctrl,
  79. buffer_new,
  80. buffer_free,
  81. buffer_callback_ctrl,
  82. };
  83. BIO_METHOD *BIO_f_buffer(void)
  84. {
  85. return (&methods_buffer);
  86. }
  87. static int buffer_new(BIO *bi)
  88. {
  89. BIO_F_BUFFER_CTX *ctx = OPENSSL_malloc(sizeof(*ctx));
  90. if (ctx == NULL)
  91. return (0);
  92. ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  93. if (ctx->ibuf == NULL) {
  94. OPENSSL_free(ctx);
  95. return (0);
  96. }
  97. ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  98. if (ctx->obuf == NULL) {
  99. OPENSSL_free(ctx->ibuf);
  100. OPENSSL_free(ctx);
  101. return (0);
  102. }
  103. ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
  104. ctx->obuf_size = DEFAULT_BUFFER_SIZE;
  105. ctx->ibuf_len = 0;
  106. ctx->ibuf_off = 0;
  107. ctx->obuf_len = 0;
  108. ctx->obuf_off = 0;
  109. bi->init = 1;
  110. bi->ptr = (char *)ctx;
  111. bi->flags = 0;
  112. return (1);
  113. }
  114. static int buffer_free(BIO *a)
  115. {
  116. BIO_F_BUFFER_CTX *b;
  117. if (a == NULL)
  118. return (0);
  119. b = (BIO_F_BUFFER_CTX *)a->ptr;
  120. OPENSSL_free(b->ibuf);
  121. OPENSSL_free(b->obuf);
  122. OPENSSL_free(a->ptr);
  123. a->ptr = NULL;
  124. a->init = 0;
  125. a->flags = 0;
  126. return (1);
  127. }
  128. static int buffer_read(BIO *b, char *out, int outl)
  129. {
  130. int i, num = 0;
  131. BIO_F_BUFFER_CTX *ctx;
  132. if (out == NULL)
  133. return (0);
  134. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  135. if ((ctx == NULL) || (b->next_bio == NULL))
  136. return (0);
  137. num = 0;
  138. BIO_clear_retry_flags(b);
  139. start:
  140. i = ctx->ibuf_len;
  141. /* If there is stuff left over, grab it */
  142. if (i != 0) {
  143. if (i > outl)
  144. i = outl;
  145. memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
  146. ctx->ibuf_off += i;
  147. ctx->ibuf_len -= i;
  148. num += i;
  149. if (outl == i)
  150. return (num);
  151. outl -= i;
  152. out += i;
  153. }
  154. /*
  155. * We may have done a partial read. try to do more. We have nothing in
  156. * the buffer. If we get an error and have read some data, just return it
  157. * and let them retry to get the error again. copy direct to parent
  158. * address space
  159. */
  160. if (outl > ctx->ibuf_size) {
  161. for (;;) {
  162. i = BIO_read(b->next_bio, out, outl);
  163. if (i <= 0) {
  164. BIO_copy_next_retry(b);
  165. if (i < 0)
  166. return ((num > 0) ? num : i);
  167. if (i == 0)
  168. return (num);
  169. }
  170. num += i;
  171. if (outl == i)
  172. return (num);
  173. out += i;
  174. outl -= i;
  175. }
  176. }
  177. /* else */
  178. /* we are going to be doing some buffering */
  179. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  180. if (i <= 0) {
  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. ctx->ibuf_off = 0;
  188. ctx->ibuf_len = i;
  189. /* Lets re-read using ourselves :-) */
  190. goto start;
  191. }
  192. static int buffer_write(BIO *b, const char *in, int inl)
  193. {
  194. int i, num = 0;
  195. BIO_F_BUFFER_CTX *ctx;
  196. if ((in == NULL) || (inl <= 0))
  197. return (0);
  198. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  199. if ((ctx == NULL) || (b->next_bio == NULL))
  200. return (0);
  201. BIO_clear_retry_flags(b);
  202. start:
  203. i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
  204. /* add to buffer and return */
  205. if (i >= inl) {
  206. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
  207. ctx->obuf_len += inl;
  208. return (num + inl);
  209. }
  210. /* else */
  211. /* stuff already in buffer, so add to it first, then flush */
  212. if (ctx->obuf_len != 0) {
  213. if (i > 0) { /* lets fill it up if we can */
  214. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
  215. in += i;
  216. inl -= i;
  217. num += i;
  218. ctx->obuf_len += i;
  219. }
  220. /* we now have a full buffer needing flushing */
  221. for (;;) {
  222. i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
  223. ctx->obuf_len);
  224. if (i <= 0) {
  225. BIO_copy_next_retry(b);
  226. if (i < 0)
  227. return ((num > 0) ? num : i);
  228. if (i == 0)
  229. return (num);
  230. }
  231. ctx->obuf_off += i;
  232. ctx->obuf_len -= i;
  233. if (ctx->obuf_len == 0)
  234. break;
  235. }
  236. }
  237. /*
  238. * we only get here if the buffer has been flushed and we still have
  239. * stuff to write
  240. */
  241. ctx->obuf_off = 0;
  242. /* we now have inl bytes to write */
  243. while (inl >= ctx->obuf_size) {
  244. i = BIO_write(b->next_bio, in, inl);
  245. if (i <= 0) {
  246. BIO_copy_next_retry(b);
  247. if (i < 0)
  248. return ((num > 0) ? num : i);
  249. if (i == 0)
  250. return (num);
  251. }
  252. num += i;
  253. in += i;
  254. inl -= i;
  255. if (inl == 0)
  256. return (num);
  257. }
  258. /*
  259. * copy the rest into the buffer since we have only a small amount left
  260. */
  261. goto start;
  262. }
  263. static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  264. {
  265. BIO *dbio;
  266. BIO_F_BUFFER_CTX *ctx;
  267. long ret = 1;
  268. char *p1, *p2;
  269. int r, i, *ip;
  270. int ibs, obs;
  271. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  272. switch (cmd) {
  273. case BIO_CTRL_RESET:
  274. ctx->ibuf_off = 0;
  275. ctx->ibuf_len = 0;
  276. ctx->obuf_off = 0;
  277. ctx->obuf_len = 0;
  278. if (b->next_bio == NULL)
  279. return (0);
  280. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  281. break;
  282. case BIO_CTRL_INFO:
  283. ret = (long)ctx->obuf_len;
  284. break;
  285. case BIO_C_GET_BUFF_NUM_LINES:
  286. ret = 0;
  287. p1 = ctx->ibuf;
  288. for (i = 0; i < ctx->ibuf_len; i++) {
  289. if (p1[ctx->ibuf_off + i] == '\n')
  290. ret++;
  291. }
  292. break;
  293. case BIO_CTRL_WPENDING:
  294. ret = (long)ctx->obuf_len;
  295. if (ret == 0) {
  296. if (b->next_bio == NULL)
  297. return (0);
  298. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  299. }
  300. break;
  301. case BIO_CTRL_PENDING:
  302. ret = (long)ctx->ibuf_len;
  303. if (ret == 0) {
  304. if (b->next_bio == NULL)
  305. return (0);
  306. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  307. }
  308. break;
  309. case BIO_C_SET_BUFF_READ_DATA:
  310. if (num > ctx->ibuf_size) {
  311. p1 = OPENSSL_malloc((int)num);
  312. if (p1 == NULL)
  313. goto malloc_error;
  314. OPENSSL_free(ctx->ibuf);
  315. ctx->ibuf = p1;
  316. }
  317. ctx->ibuf_off = 0;
  318. ctx->ibuf_len = (int)num;
  319. memcpy(ctx->ibuf, ptr, (int)num);
  320. ret = 1;
  321. break;
  322. case BIO_C_SET_BUFF_SIZE:
  323. if (ptr != NULL) {
  324. ip = (int *)ptr;
  325. if (*ip == 0) {
  326. ibs = (int)num;
  327. obs = ctx->obuf_size;
  328. } else { /* if (*ip == 1) */
  329. ibs = ctx->ibuf_size;
  330. obs = (int)num;
  331. }
  332. } else {
  333. ibs = (int)num;
  334. obs = (int)num;
  335. }
  336. p1 = ctx->ibuf;
  337. p2 = ctx->obuf;
  338. if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
  339. p1 = OPENSSL_malloc((int)num);
  340. if (p1 == NULL)
  341. goto malloc_error;
  342. }
  343. if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
  344. p2 = OPENSSL_malloc((int)num);
  345. if (p2 == NULL) {
  346. if (p1 != ctx->ibuf)
  347. OPENSSL_free(p1);
  348. goto malloc_error;
  349. }
  350. }
  351. if (ctx->ibuf != p1) {
  352. OPENSSL_free(ctx->ibuf);
  353. ctx->ibuf = p1;
  354. ctx->ibuf_off = 0;
  355. ctx->ibuf_len = 0;
  356. ctx->ibuf_size = ibs;
  357. }
  358. if (ctx->obuf != p2) {
  359. OPENSSL_free(ctx->obuf);
  360. ctx->obuf = p2;
  361. ctx->obuf_off = 0;
  362. ctx->obuf_len = 0;
  363. ctx->obuf_size = obs;
  364. }
  365. break;
  366. case BIO_C_DO_STATE_MACHINE:
  367. if (b->next_bio == NULL)
  368. return (0);
  369. BIO_clear_retry_flags(b);
  370. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  371. BIO_copy_next_retry(b);
  372. break;
  373. case BIO_CTRL_FLUSH:
  374. if (b->next_bio == NULL)
  375. return (0);
  376. if (ctx->obuf_len <= 0) {
  377. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  378. break;
  379. }
  380. for (;;) {
  381. BIO_clear_retry_flags(b);
  382. if (ctx->obuf_len > 0) {
  383. r = BIO_write(b->next_bio,
  384. &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
  385. BIO_copy_next_retry(b);
  386. if (r <= 0)
  387. return ((long)r);
  388. ctx->obuf_off += r;
  389. ctx->obuf_len -= r;
  390. } else {
  391. ctx->obuf_len = 0;
  392. ctx->obuf_off = 0;
  393. ret = 1;
  394. break;
  395. }
  396. }
  397. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  398. break;
  399. case BIO_CTRL_DUP:
  400. dbio = (BIO *)ptr;
  401. if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
  402. !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  403. ret = 0;
  404. break;
  405. default:
  406. if (b->next_bio == NULL)
  407. return (0);
  408. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  409. break;
  410. }
  411. return (ret);
  412. malloc_error:
  413. BIOerr(BIO_F_BUFFER_CTRL, ERR_R_MALLOC_FAILURE);
  414. return (0);
  415. }
  416. static long buffer_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  417. {
  418. long ret = 1;
  419. if (b->next_bio == NULL)
  420. return (0);
  421. switch (cmd) {
  422. default:
  423. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  424. break;
  425. }
  426. return (ret);
  427. }
  428. static int buffer_gets(BIO *b, char *buf, int size)
  429. {
  430. BIO_F_BUFFER_CTX *ctx;
  431. int num = 0, i, flag;
  432. char *p;
  433. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  434. size--; /* reserve space for a '\0' */
  435. BIO_clear_retry_flags(b);
  436. for (;;) {
  437. if (ctx->ibuf_len > 0) {
  438. p = &(ctx->ibuf[ctx->ibuf_off]);
  439. flag = 0;
  440. for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
  441. *(buf++) = p[i];
  442. if (p[i] == '\n') {
  443. flag = 1;
  444. i++;
  445. break;
  446. }
  447. }
  448. num += i;
  449. size -= i;
  450. ctx->ibuf_len -= i;
  451. ctx->ibuf_off += i;
  452. if (flag || size == 0) {
  453. *buf = '\0';
  454. return (num);
  455. }
  456. } else { /* read another chunk */
  457. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  458. if (i <= 0) {
  459. BIO_copy_next_retry(b);
  460. *buf = '\0';
  461. if (i < 0)
  462. return ((num > 0) ? num : i);
  463. if (i == 0)
  464. return (num);
  465. }
  466. ctx->ibuf_len = i;
  467. ctx->ibuf_off = 0;
  468. }
  469. }
  470. }
  471. static int buffer_puts(BIO *b, const char *str)
  472. {
  473. return (buffer_write(b, str, strlen(str)));
  474. }