bss_mem.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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 mem_write(BIO *h, const char *buf, int num);
  14. static int mem_read(BIO *h, char *buf, int size);
  15. static int mem_puts(BIO *h, const char *str);
  16. static int mem_gets(BIO *h, char *str, int size);
  17. static long mem_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  18. static int mem_new(BIO *h);
  19. static int dgram_mem_new(BIO *h);
  20. static int secmem_new(BIO *h);
  21. static int mem_free(BIO *data);
  22. static int mem_buf_free(BIO *data);
  23. static int mem_buf_sync(BIO *h);
  24. static const BIO_METHOD mem_method = {
  25. BIO_TYPE_MEM,
  26. "memory buffer",
  27. bwrite_conv,
  28. mem_write,
  29. bread_conv,
  30. mem_read,
  31. mem_puts,
  32. mem_gets,
  33. mem_ctrl,
  34. mem_new,
  35. mem_free,
  36. NULL, /* mem_callback_ctrl */
  37. };
  38. static const BIO_METHOD dgram_mem_method = {
  39. BIO_TYPE_MEM,
  40. "datagram memory buffer",
  41. bwrite_conv,
  42. mem_write,
  43. bread_conv,
  44. mem_read,
  45. mem_puts,
  46. mem_gets,
  47. mem_ctrl,
  48. dgram_mem_new,
  49. mem_free,
  50. NULL, /* mem_callback_ctrl */
  51. };
  52. static const BIO_METHOD secmem_method = {
  53. BIO_TYPE_MEM,
  54. "secure memory buffer",
  55. bwrite_conv,
  56. mem_write,
  57. bread_conv,
  58. mem_read,
  59. mem_puts,
  60. mem_gets,
  61. mem_ctrl,
  62. secmem_new,
  63. mem_free,
  64. NULL, /* mem_callback_ctrl */
  65. };
  66. struct buf_mem_dgram_st {
  67. char *dgram; /* Pointer into the buffer for where the dgram starts */
  68. size_t dgramlen; /* Length of the dgram */
  69. struct buf_mem_dgram_st *next; /* Next dgram to read */
  70. };
  71. /*
  72. * BIO memory stores buffer and read pointer
  73. * however the roles are different for read only BIOs.
  74. * In that case the readp just stores the original state
  75. * to be used for reset.
  76. */
  77. typedef struct bio_buf_mem_st {
  78. struct buf_mem_st *buf; /* allocated buffer */
  79. struct buf_mem_st *readp; /* read pointer */
  80. struct buf_mem_dgram_st *dgrams; /* linked list of dgram data */
  81. struct buf_mem_dgram_st *last; /* last dgram in the linked list */
  82. int use_dgrams;
  83. } BIO_BUF_MEM;
  84. /*
  85. * bio->num is used to hold the value to return on 'empty', if it is 0,
  86. * should_retry is not set
  87. */
  88. const BIO_METHOD *BIO_s_mem(void)
  89. {
  90. return &mem_method;
  91. }
  92. const BIO_METHOD *BIO_s_dgram_mem(void)
  93. {
  94. return &dgram_mem_method;
  95. }
  96. const BIO_METHOD *BIO_s_secmem(void)
  97. {
  98. return(&secmem_method);
  99. }
  100. BIO *BIO_new_mem_buf(const void *buf, int len)
  101. {
  102. BIO *ret;
  103. BUF_MEM *b;
  104. BIO_BUF_MEM *bb;
  105. size_t sz;
  106. if (buf == NULL) {
  107. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  108. return NULL;
  109. }
  110. sz = (len < 0) ? strlen(buf) : (size_t)len;
  111. if ((ret = BIO_new(BIO_s_mem())) == NULL)
  112. return NULL;
  113. bb = (BIO_BUF_MEM *)ret->ptr;
  114. b = bb->buf;
  115. /* Cast away const and trust in the MEM_RDONLY flag. */
  116. b->data = (void *)buf;
  117. b->length = sz;
  118. b->max = sz;
  119. *bb->readp = *bb->buf;
  120. ret->flags |= BIO_FLAGS_MEM_RDONLY;
  121. /* Since this is static data retrying won't help */
  122. ret->num = 0;
  123. return ret;
  124. }
  125. static int mem_init(BIO *bi, unsigned long flags)
  126. {
  127. BIO_BUF_MEM *bb = OPENSSL_zalloc(sizeof(*bb));
  128. if (bb == NULL)
  129. return 0;
  130. if ((bb->buf = BUF_MEM_new_ex(flags)) == NULL) {
  131. OPENSSL_free(bb);
  132. return 0;
  133. }
  134. if ((bb->readp = OPENSSL_zalloc(sizeof(*bb->readp))) == NULL) {
  135. BUF_MEM_free(bb->buf);
  136. OPENSSL_free(bb);
  137. return 0;
  138. }
  139. *bb->readp = *bb->buf;
  140. bi->shutdown = 1;
  141. bi->init = 1;
  142. bi->num = -1;
  143. bi->ptr = (char *)bb;
  144. return 1;
  145. }
  146. static int mem_new(BIO *bi)
  147. {
  148. return mem_init(bi, 0L);
  149. }
  150. static int dgram_mem_new(BIO *bi)
  151. {
  152. BIO_BUF_MEM *bbm;
  153. if (!mem_init(bi, 0L))
  154. return 0;
  155. bbm = (BIO_BUF_MEM *)bi->ptr;
  156. bbm->use_dgrams = 1;
  157. bi->num = -1;
  158. return 1;
  159. }
  160. static int secmem_new(BIO *bi)
  161. {
  162. return mem_init(bi, BUF_MEM_FLAG_SECURE);
  163. }
  164. static void clear_all_dgrams(BIO_BUF_MEM *bbm)
  165. {
  166. struct buf_mem_dgram_st *dgrams = bbm->dgrams;
  167. while (dgrams != NULL) {
  168. struct buf_mem_dgram_st *tmp = dgrams;
  169. dgrams = dgrams->next;
  170. OPENSSL_free(tmp);
  171. }
  172. bbm->dgrams = NULL;
  173. }
  174. static int mem_free(BIO *a)
  175. {
  176. BIO_BUF_MEM *bb;
  177. if (a == NULL)
  178. return 0;
  179. bb = (BIO_BUF_MEM *)a->ptr;
  180. if (!mem_buf_free(a))
  181. return 0;
  182. OPENSSL_free(bb->readp);
  183. clear_all_dgrams(bb);
  184. OPENSSL_free(bb);
  185. return 1;
  186. }
  187. static int mem_buf_free(BIO *a)
  188. {
  189. if (a == NULL)
  190. return 0;
  191. if (a->shutdown && a->init && a->ptr != NULL) {
  192. BIO_BUF_MEM *bb = (BIO_BUF_MEM *)a->ptr;
  193. BUF_MEM *b = bb->buf;
  194. if (a->flags & BIO_FLAGS_MEM_RDONLY)
  195. b->data = NULL;
  196. BUF_MEM_free(b);
  197. }
  198. return 1;
  199. }
  200. /*
  201. * Reallocate memory buffer if read pointer differs
  202. * NOT FOR RDONLY
  203. */
  204. static int mem_buf_sync(BIO *b)
  205. {
  206. if (b != NULL && b->init != 0 && b->ptr != NULL) {
  207. BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
  208. if (bbm->readp->data != bbm->buf->data) {
  209. memmove(bbm->buf->data, bbm->readp->data, bbm->readp->length);
  210. bbm->buf->length = bbm->readp->length;
  211. bbm->readp->data = bbm->buf->data;
  212. }
  213. }
  214. return 0;
  215. }
  216. static int mem_read(BIO *b, char *out, int outl)
  217. {
  218. int ret = -1;
  219. BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
  220. BUF_MEM *bm = bbm->readp;
  221. size_t maxreadlen = 0;
  222. int eof = 0;
  223. if (b->flags & BIO_FLAGS_MEM_RDONLY)
  224. bm = bbm->buf;
  225. BIO_clear_retry_flags(b);
  226. if (bbm->use_dgrams) {
  227. if (bbm->dgrams != NULL) {
  228. maxreadlen = bbm->dgrams->dgramlen;
  229. if (!ossl_assert(maxreadlen <= bm->length))
  230. return 0;
  231. } else {
  232. eof = 1;
  233. }
  234. } else {
  235. maxreadlen = bm->length;
  236. eof = (maxreadlen == 0);
  237. }
  238. ret = (outl >= 0 && (size_t)outl > maxreadlen) ? (int)maxreadlen : outl;
  239. if ((out != NULL) && (ret > 0)) {
  240. size_t flushlen;
  241. memcpy(out, bm->data, ret);
  242. flushlen = bbm->use_dgrams ? maxreadlen : (size_t)ret;
  243. bm->length -= flushlen;
  244. bm->max -= flushlen;
  245. bm->data += flushlen;
  246. if (bbm->use_dgrams) {
  247. struct buf_mem_dgram_st *tmp = bbm->dgrams;
  248. bbm->dgrams = tmp->next;
  249. OPENSSL_free(tmp);
  250. if (bbm->dgrams == NULL)
  251. bbm->last = NULL;
  252. }
  253. } else if (eof) {
  254. ret = b->num;
  255. if (ret != 0)
  256. BIO_set_retry_read(b);
  257. }
  258. return ret;
  259. }
  260. static int mem_write(BIO *b, const char *in, int inl)
  261. {
  262. int ret = -1;
  263. int blen;
  264. BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
  265. if (b->flags & BIO_FLAGS_MEM_RDONLY) {
  266. ERR_raise(ERR_LIB_BIO, BIO_R_WRITE_TO_READ_ONLY_BIO);
  267. goto end;
  268. }
  269. BIO_clear_retry_flags(b);
  270. if (inl == 0)
  271. return 0;
  272. if (in == NULL) {
  273. ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
  274. goto end;
  275. }
  276. blen = bbm->readp->length;
  277. mem_buf_sync(b);
  278. if (BUF_MEM_grow_clean(bbm->buf, blen + inl) == 0)
  279. goto end;
  280. memcpy(bbm->buf->data + blen, in, inl);
  281. *bbm->readp = *bbm->buf;
  282. if (bbm->use_dgrams) {
  283. struct buf_mem_dgram_st *dgram = OPENSSL_malloc(sizeof(*dgram));
  284. if (dgram == NULL)
  285. goto end;
  286. dgram->dgram = bbm->buf->data + blen;
  287. dgram->dgramlen = inl;
  288. dgram->next = NULL;
  289. if (bbm->dgrams == NULL)
  290. bbm->dgrams = dgram;
  291. else
  292. bbm->last->next = dgram;
  293. bbm->last = dgram;
  294. }
  295. ret = inl;
  296. end:
  297. return ret;
  298. }
  299. static long mem_ctrl(BIO *b, int cmd, long num, void *ptr)
  300. {
  301. long ret = 1;
  302. char **pptr;
  303. BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)b->ptr;
  304. BUF_MEM *bm, *bo; /* bio_mem, bio_other */
  305. long off, remain;
  306. if (b->flags & BIO_FLAGS_MEM_RDONLY) {
  307. bm = bbm->buf;
  308. bo = bbm->readp;
  309. } else {
  310. bm = bbm->readp;
  311. bo = bbm->buf;
  312. }
  313. off = (bm->data == bo->data) ? 0 : bm->data - bo->data;
  314. remain = bm->length;
  315. switch (cmd) {
  316. case BIO_CTRL_RESET:
  317. bm = bbm->buf;
  318. if (bm->data != NULL) {
  319. if (!(b->flags & BIO_FLAGS_MEM_RDONLY)) {
  320. if (!(b->flags & BIO_FLAGS_NONCLEAR_RST)) {
  321. memset(bm->data, 0, bm->max);
  322. bm->length = 0;
  323. }
  324. *bbm->readp = *bbm->buf;
  325. } else {
  326. /* For read only case just reset to the start again */
  327. *bbm->buf = *bbm->readp;
  328. }
  329. }
  330. clear_all_dgrams(bbm);
  331. break;
  332. case BIO_C_FILE_SEEK:
  333. if (num < 0 || num > off + remain)
  334. return -1; /* Can't see outside of the current buffer */
  335. bm->data = (num != 0) ? bo->data + num : bo->data;
  336. bm->length = bo->length - num;
  337. bm->max = bo->max - num;
  338. off = num;
  339. /* FALLTHRU */
  340. case BIO_C_FILE_TELL:
  341. ret = off;
  342. break;
  343. case BIO_CTRL_EOF:
  344. ret = (long)(bm->length == 0 && bbm->use_dgrams == 0);
  345. break;
  346. case BIO_C_SET_BUF_MEM_EOF_RETURN:
  347. if (!bbm->use_dgrams)
  348. b->num = (int)num;
  349. else
  350. ret = -1;
  351. break;
  352. case BIO_CTRL_INFO:
  353. ret = (long)bm->length;
  354. if (ptr != NULL) {
  355. pptr = (char **)ptr;
  356. *pptr = (char *)(bm->data);
  357. }
  358. break;
  359. case BIO_C_SET_BUF_MEM:
  360. mem_buf_free(b);
  361. b->shutdown = (int)num;
  362. bbm->buf = ptr;
  363. *bbm->readp = *bbm->buf;
  364. break;
  365. case BIO_C_GET_BUF_MEM_PTR:
  366. if (ptr != NULL) {
  367. if (!(b->flags & BIO_FLAGS_MEM_RDONLY))
  368. mem_buf_sync(b);
  369. bm = bbm->buf;
  370. pptr = (char **)ptr;
  371. *pptr = (char *)bm;
  372. }
  373. break;
  374. case BIO_CTRL_GET_CLOSE:
  375. ret = (long)b->shutdown;
  376. break;
  377. case BIO_CTRL_SET_CLOSE:
  378. b->shutdown = (int)num;
  379. break;
  380. case BIO_CTRL_WPENDING:
  381. ret = 0L;
  382. break;
  383. case BIO_CTRL_PENDING:
  384. ret = (long)bm->length;
  385. break;
  386. case BIO_CTRL_DUP:
  387. case BIO_CTRL_FLUSH:
  388. ret = 1;
  389. break;
  390. case BIO_CTRL_PUSH:
  391. case BIO_CTRL_POP:
  392. default:
  393. ret = 0;
  394. break;
  395. }
  396. return ret;
  397. }
  398. static int mem_gets(BIO *bp, char *buf, int size)
  399. {
  400. int i, j;
  401. int ret = -1;
  402. char *p;
  403. BIO_BUF_MEM *bbm = (BIO_BUF_MEM *)bp->ptr;
  404. BUF_MEM *bm = bbm->readp;
  405. if (bp->flags & BIO_FLAGS_MEM_RDONLY)
  406. bm = bbm->buf;
  407. BIO_clear_retry_flags(bp);
  408. j = (!bbm->use_dgrams || bbm->dgrams == NULL) ? bm->length
  409. : bbm->dgrams->dgramlen;
  410. if ((size - 1) < j)
  411. j = size - 1;
  412. if (j <= 0) {
  413. *buf = '\0';
  414. return 0;
  415. }
  416. p = bm->data;
  417. for (i = 0; i < j; i++) {
  418. if (p[i] == '\n') {
  419. i++;
  420. break;
  421. }
  422. }
  423. /*
  424. * i is now the max num of bytes to copy, either j or up to
  425. * and including the first newline
  426. */
  427. i = mem_read(bp, buf, i);
  428. if (i > 0)
  429. buf[i] = '\0';
  430. ret = i;
  431. return ret;
  432. }
  433. static int mem_puts(BIO *bp, const char *str)
  434. {
  435. int n, ret;
  436. n = strlen(str);
  437. ret = mem_write(bp, str, n);
  438. /* memory semantics is that it will always work */
  439. return ret;
  440. }