bf_buff.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <errno.h>
  11. #include "bio_local.h"
  12. #include "internal/cryptlib.h"
  13. static int buffer_write(BIO *h, const char *buf, int num);
  14. static int buffer_read(BIO *h, char *buf, int size);
  15. static int buffer_puts(BIO *h, const char *str);
  16. static int buffer_gets(BIO *h, char *str, int size);
  17. static long buffer_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  18. static int buffer_new(BIO *h);
  19. static int buffer_free(BIO *data);
  20. static long buffer_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  21. #define DEFAULT_BUFFER_SIZE 4096
  22. static const BIO_METHOD methods_buffer = {
  23. BIO_TYPE_BUFFER,
  24. "buffer",
  25. bwrite_conv,
  26. buffer_write,
  27. bread_conv,
  28. buffer_read,
  29. buffer_puts,
  30. buffer_gets,
  31. buffer_ctrl,
  32. buffer_new,
  33. buffer_free,
  34. buffer_callback_ctrl,
  35. };
  36. const BIO_METHOD *BIO_f_buffer(void)
  37. {
  38. return &methods_buffer;
  39. }
  40. static int buffer_new(BIO *bi)
  41. {
  42. BIO_F_BUFFER_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  43. if (ctx == NULL)
  44. return 0;
  45. ctx->ibuf_size = DEFAULT_BUFFER_SIZE;
  46. ctx->ibuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  47. if (ctx->ibuf == NULL) {
  48. OPENSSL_free(ctx);
  49. return 0;
  50. }
  51. ctx->obuf_size = DEFAULT_BUFFER_SIZE;
  52. ctx->obuf = OPENSSL_malloc(DEFAULT_BUFFER_SIZE);
  53. if (ctx->obuf == NULL) {
  54. OPENSSL_free(ctx->ibuf);
  55. OPENSSL_free(ctx);
  56. return 0;
  57. }
  58. bi->init = 1;
  59. bi->ptr = (char *)ctx;
  60. bi->flags = 0;
  61. return 1;
  62. }
  63. static int buffer_free(BIO *a)
  64. {
  65. BIO_F_BUFFER_CTX *b;
  66. if (a == NULL)
  67. return 0;
  68. b = (BIO_F_BUFFER_CTX *)a->ptr;
  69. OPENSSL_free(b->ibuf);
  70. OPENSSL_free(b->obuf);
  71. OPENSSL_free(a->ptr);
  72. a->ptr = NULL;
  73. a->init = 0;
  74. a->flags = 0;
  75. return 1;
  76. }
  77. static int buffer_read(BIO *b, char *out, int outl)
  78. {
  79. int i, num = 0;
  80. BIO_F_BUFFER_CTX *ctx;
  81. if (out == NULL)
  82. return 0;
  83. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  84. if ((ctx == NULL) || (b->next_bio == NULL))
  85. return 0;
  86. num = 0;
  87. BIO_clear_retry_flags(b);
  88. start:
  89. i = ctx->ibuf_len;
  90. /* If there is stuff left over, grab it */
  91. if (i != 0) {
  92. if (i > outl)
  93. i = outl;
  94. memcpy(out, &(ctx->ibuf[ctx->ibuf_off]), i);
  95. ctx->ibuf_off += i;
  96. ctx->ibuf_len -= i;
  97. num += i;
  98. if (outl == i)
  99. return num;
  100. outl -= i;
  101. out += i;
  102. }
  103. /*
  104. * We may have done a partial read. try to do more. We have nothing in
  105. * the buffer. If we get an error and have read some data, just return it
  106. * and let them retry to get the error again. copy direct to parent
  107. * address space
  108. */
  109. if (outl > ctx->ibuf_size) {
  110. for (;;) {
  111. i = BIO_read(b->next_bio, out, outl);
  112. if (i <= 0) {
  113. BIO_copy_next_retry(b);
  114. if (i < 0)
  115. return ((num > 0) ? num : i);
  116. if (i == 0)
  117. return num;
  118. }
  119. num += i;
  120. if (outl == i)
  121. return num;
  122. out += i;
  123. outl -= i;
  124. }
  125. }
  126. /* else */
  127. /* we are going to be doing some buffering */
  128. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  129. if (i <= 0) {
  130. BIO_copy_next_retry(b);
  131. if (i < 0)
  132. return ((num > 0) ? num : i);
  133. if (i == 0)
  134. return num;
  135. }
  136. ctx->ibuf_off = 0;
  137. ctx->ibuf_len = i;
  138. /* Lets re-read using ourselves :-) */
  139. goto start;
  140. }
  141. static int buffer_write(BIO *b, const char *in, int inl)
  142. {
  143. int i, num = 0;
  144. BIO_F_BUFFER_CTX *ctx;
  145. if ((in == NULL) || (inl <= 0))
  146. return 0;
  147. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  148. if ((ctx == NULL) || (b->next_bio == NULL))
  149. return 0;
  150. BIO_clear_retry_flags(b);
  151. start:
  152. i = ctx->obuf_size - (ctx->obuf_len + ctx->obuf_off);
  153. /* add to buffer and return */
  154. if (i >= inl) {
  155. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, inl);
  156. ctx->obuf_len += inl;
  157. return (num + inl);
  158. }
  159. /* else */
  160. /* stuff already in buffer, so add to it first, then flush */
  161. if (ctx->obuf_len != 0) {
  162. if (i > 0) { /* lets fill it up if we can */
  163. memcpy(&(ctx->obuf[ctx->obuf_off + ctx->obuf_len]), in, i);
  164. in += i;
  165. inl -= i;
  166. num += i;
  167. ctx->obuf_len += i;
  168. }
  169. /* we now have a full buffer needing flushing */
  170. for (;;) {
  171. i = BIO_write(b->next_bio, &(ctx->obuf[ctx->obuf_off]),
  172. ctx->obuf_len);
  173. if (i <= 0) {
  174. BIO_copy_next_retry(b);
  175. if (i < 0)
  176. return ((num > 0) ? num : i);
  177. if (i == 0)
  178. return num;
  179. }
  180. ctx->obuf_off += i;
  181. ctx->obuf_len -= i;
  182. if (ctx->obuf_len == 0)
  183. break;
  184. }
  185. }
  186. /*
  187. * we only get here if the buffer has been flushed and we still have
  188. * stuff to write
  189. */
  190. ctx->obuf_off = 0;
  191. /* we now have inl bytes to write */
  192. while (inl >= ctx->obuf_size) {
  193. i = BIO_write(b->next_bio, in, inl);
  194. if (i <= 0) {
  195. BIO_copy_next_retry(b);
  196. if (i < 0)
  197. return ((num > 0) ? num : i);
  198. if (i == 0)
  199. return num;
  200. }
  201. num += i;
  202. in += i;
  203. inl -= i;
  204. if (inl == 0)
  205. return num;
  206. }
  207. /*
  208. * copy the rest into the buffer since we have only a small amount left
  209. */
  210. goto start;
  211. }
  212. static long buffer_ctrl(BIO *b, int cmd, long num, void *ptr)
  213. {
  214. BIO *dbio;
  215. BIO_F_BUFFER_CTX *ctx;
  216. long ret = 1;
  217. char *p1, *p2;
  218. int r, i, *ip;
  219. int ibs, obs;
  220. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  221. switch (cmd) {
  222. case BIO_CTRL_RESET:
  223. ctx->ibuf_off = 0;
  224. ctx->ibuf_len = 0;
  225. ctx->obuf_off = 0;
  226. ctx->obuf_len = 0;
  227. if (b->next_bio == NULL)
  228. return 0;
  229. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  230. break;
  231. case BIO_CTRL_EOF:
  232. if (ctx->ibuf_len > 0)
  233. return 0;
  234. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  235. break;
  236. case BIO_CTRL_INFO:
  237. ret = (long)ctx->obuf_len;
  238. break;
  239. case BIO_C_GET_BUFF_NUM_LINES:
  240. ret = 0;
  241. p1 = ctx->ibuf;
  242. for (i = 0; i < ctx->ibuf_len; i++) {
  243. if (p1[ctx->ibuf_off + i] == '\n')
  244. ret++;
  245. }
  246. break;
  247. case BIO_CTRL_WPENDING:
  248. ret = (long)ctx->obuf_len;
  249. if (ret == 0) {
  250. if (b->next_bio == NULL)
  251. return 0;
  252. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  253. }
  254. break;
  255. case BIO_CTRL_PENDING:
  256. ret = (long)ctx->ibuf_len;
  257. if (ret == 0) {
  258. if (b->next_bio == NULL)
  259. return 0;
  260. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  261. }
  262. break;
  263. case BIO_C_SET_BUFF_READ_DATA:
  264. if (num > ctx->ibuf_size) {
  265. if (num <= 0)
  266. return 0;
  267. p1 = OPENSSL_malloc((size_t)num);
  268. if (p1 == NULL)
  269. goto malloc_error;
  270. OPENSSL_free(ctx->ibuf);
  271. ctx->ibuf = p1;
  272. }
  273. ctx->ibuf_off = 0;
  274. ctx->ibuf_len = (int)num;
  275. memcpy(ctx->ibuf, ptr, (int)num);
  276. ret = 1;
  277. break;
  278. case BIO_C_SET_BUFF_SIZE:
  279. if (ptr != NULL) {
  280. ip = (int *)ptr;
  281. if (*ip == 0) {
  282. ibs = (int)num;
  283. obs = ctx->obuf_size;
  284. } else { /* if (*ip == 1) */
  285. ibs = ctx->ibuf_size;
  286. obs = (int)num;
  287. }
  288. } else {
  289. ibs = (int)num;
  290. obs = (int)num;
  291. }
  292. p1 = ctx->ibuf;
  293. p2 = ctx->obuf;
  294. if ((ibs > DEFAULT_BUFFER_SIZE) && (ibs != ctx->ibuf_size)) {
  295. if (num <= 0)
  296. return 0;
  297. p1 = OPENSSL_malloc((size_t)num);
  298. if (p1 == NULL)
  299. goto malloc_error;
  300. }
  301. if ((obs > DEFAULT_BUFFER_SIZE) && (obs != ctx->obuf_size)) {
  302. p2 = OPENSSL_malloc((size_t)num);
  303. if (p2 == NULL) {
  304. if (p1 != ctx->ibuf)
  305. OPENSSL_free(p1);
  306. goto malloc_error;
  307. }
  308. }
  309. if (ctx->ibuf != p1) {
  310. OPENSSL_free(ctx->ibuf);
  311. ctx->ibuf = p1;
  312. ctx->ibuf_off = 0;
  313. ctx->ibuf_len = 0;
  314. ctx->ibuf_size = ibs;
  315. }
  316. if (ctx->obuf != p2) {
  317. OPENSSL_free(ctx->obuf);
  318. ctx->obuf = p2;
  319. ctx->obuf_off = 0;
  320. ctx->obuf_len = 0;
  321. ctx->obuf_size = obs;
  322. }
  323. break;
  324. case BIO_C_DO_STATE_MACHINE:
  325. if (b->next_bio == NULL)
  326. return 0;
  327. BIO_clear_retry_flags(b);
  328. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  329. BIO_copy_next_retry(b);
  330. break;
  331. case BIO_CTRL_FLUSH:
  332. if (b->next_bio == NULL)
  333. return 0;
  334. if (ctx->obuf_len <= 0) {
  335. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  336. break;
  337. }
  338. for (;;) {
  339. BIO_clear_retry_flags(b);
  340. if (ctx->obuf_len > 0) {
  341. r = BIO_write(b->next_bio,
  342. &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
  343. BIO_copy_next_retry(b);
  344. if (r <= 0)
  345. return (long)r;
  346. ctx->obuf_off += r;
  347. ctx->obuf_len -= r;
  348. } else {
  349. ctx->obuf_len = 0;
  350. ctx->obuf_off = 0;
  351. break;
  352. }
  353. }
  354. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  355. break;
  356. case BIO_CTRL_DUP:
  357. dbio = (BIO *)ptr;
  358. if (!BIO_set_read_buffer_size(dbio, ctx->ibuf_size) ||
  359. !BIO_set_write_buffer_size(dbio, ctx->obuf_size))
  360. ret = 0;
  361. break;
  362. case BIO_CTRL_PEEK:
  363. /* Ensure there's stuff in the input buffer */
  364. {
  365. char fake_buf[1];
  366. (void)buffer_read(b, fake_buf, 0);
  367. }
  368. if (num > ctx->ibuf_len)
  369. num = ctx->ibuf_len;
  370. memcpy(ptr, &(ctx->ibuf[ctx->ibuf_off]), num);
  371. ret = num;
  372. break;
  373. default:
  374. if (b->next_bio == NULL)
  375. return 0;
  376. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  377. break;
  378. }
  379. return ret;
  380. malloc_error:
  381. ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
  382. return 0;
  383. }
  384. static long buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  385. {
  386. if (b->next_bio == NULL)
  387. return 0;
  388. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  389. }
  390. static int buffer_gets(BIO *b, char *buf, int size)
  391. {
  392. BIO_F_BUFFER_CTX *ctx;
  393. int num = 0, i, flag;
  394. char *p;
  395. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  396. size--; /* reserve space for a '\0' */
  397. BIO_clear_retry_flags(b);
  398. for (;;) {
  399. if (ctx->ibuf_len > 0) {
  400. p = &(ctx->ibuf[ctx->ibuf_off]);
  401. flag = 0;
  402. for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
  403. *(buf++) = p[i];
  404. if (p[i] == '\n') {
  405. flag = 1;
  406. i++;
  407. break;
  408. }
  409. }
  410. num += i;
  411. size -= i;
  412. ctx->ibuf_len -= i;
  413. ctx->ibuf_off += i;
  414. if (flag || size == 0) {
  415. *buf = '\0';
  416. return num;
  417. }
  418. } else { /* read another chunk */
  419. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  420. if (i <= 0) {
  421. BIO_copy_next_retry(b);
  422. *buf = '\0';
  423. if (i < 0)
  424. return ((num > 0) ? num : i);
  425. if (i == 0)
  426. return num;
  427. }
  428. ctx->ibuf_len = i;
  429. ctx->ibuf_off = 0;
  430. }
  431. }
  432. }
  433. static int buffer_puts(BIO *b, const char *str)
  434. {
  435. return buffer_write(b, str, strlen(str));
  436. }