bf_buff.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * Copyright 1995-2023 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. return 0;
  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. return 0;
  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. return 0;
  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. BIO_copy_next_retry(b);
  337. break;
  338. }
  339. for (;;) {
  340. BIO_clear_retry_flags(b);
  341. if (ctx->obuf_len > 0) {
  342. r = BIO_write(b->next_bio,
  343. &(ctx->obuf[ctx->obuf_off]), ctx->obuf_len);
  344. BIO_copy_next_retry(b);
  345. if (r <= 0)
  346. return (long)r;
  347. ctx->obuf_off += r;
  348. ctx->obuf_len -= r;
  349. } else {
  350. ctx->obuf_len = 0;
  351. ctx->obuf_off = 0;
  352. break;
  353. }
  354. }
  355. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  356. BIO_copy_next_retry(b);
  357. break;
  358. case BIO_CTRL_DUP:
  359. dbio = (BIO *)ptr;
  360. if (BIO_set_read_buffer_size(dbio, ctx->ibuf_size) <= 0 ||
  361. BIO_set_write_buffer_size(dbio, ctx->obuf_size) <= 0)
  362. ret = 0;
  363. break;
  364. case BIO_CTRL_PEEK:
  365. /* Ensure there's stuff in the input buffer */
  366. {
  367. char fake_buf[1];
  368. (void)buffer_read(b, fake_buf, 0);
  369. }
  370. if (num > ctx->ibuf_len)
  371. num = ctx->ibuf_len;
  372. memcpy(ptr, &(ctx->ibuf[ctx->ibuf_off]), num);
  373. ret = num;
  374. break;
  375. default:
  376. if (b->next_bio == NULL)
  377. return 0;
  378. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  379. break;
  380. }
  381. return ret;
  382. }
  383. static long buffer_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  384. {
  385. if (b->next_bio == NULL)
  386. return 0;
  387. return BIO_callback_ctrl(b->next_bio, cmd, fp);
  388. }
  389. static int buffer_gets(BIO *b, char *buf, int size)
  390. {
  391. BIO_F_BUFFER_CTX *ctx;
  392. int num = 0, i, flag;
  393. char *p;
  394. ctx = (BIO_F_BUFFER_CTX *)b->ptr;
  395. size--; /* reserve space for a '\0' */
  396. BIO_clear_retry_flags(b);
  397. for (;;) {
  398. if (ctx->ibuf_len > 0) {
  399. p = &(ctx->ibuf[ctx->ibuf_off]);
  400. flag = 0;
  401. for (i = 0; (i < ctx->ibuf_len) && (i < size); i++) {
  402. *(buf++) = p[i];
  403. if (p[i] == '\n') {
  404. flag = 1;
  405. i++;
  406. break;
  407. }
  408. }
  409. num += i;
  410. size -= i;
  411. ctx->ibuf_len -= i;
  412. ctx->ibuf_off += i;
  413. if (flag || size == 0) {
  414. *buf = '\0';
  415. return num;
  416. }
  417. } else { /* read another chunk */
  418. i = BIO_read(b->next_bio, ctx->ibuf, ctx->ibuf_size);
  419. if (i <= 0) {
  420. BIO_copy_next_retry(b);
  421. *buf = '\0';
  422. if (i < 0)
  423. return ((num > 0) ? num : i);
  424. if (i == 0)
  425. return num;
  426. }
  427. ctx->ibuf_len = i;
  428. ctx->ibuf_off = 0;
  429. }
  430. }
  431. }
  432. static int buffer_puts(BIO *b, const char *str)
  433. {
  434. return buffer_write(b, str, strlen(str));
  435. }