bf_buff.c 12 KB

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