bio_b64.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 "internal/cryptlib.h"
  12. #include <openssl/buffer.h>
  13. #include <openssl/evp.h>
  14. #include "internal/bio.h"
  15. static int b64_write(BIO *h, const char *buf, int num);
  16. static int b64_read(BIO *h, char *buf, int size);
  17. static int b64_puts(BIO *h, const char *str);
  18. static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  19. static int b64_new(BIO *h);
  20. static int b64_free(BIO *data);
  21. static long b64_callback_ctrl(BIO *h, int cmd, BIO_info_cb *fp);
  22. #define B64_BLOCK_SIZE 1024
  23. #define B64_BLOCK_SIZE2 768
  24. #define B64_NONE 0
  25. #define B64_ENCODE 1
  26. #define B64_DECODE 2
  27. typedef struct b64_struct {
  28. /*
  29. * BIO *bio; moved to the BIO structure
  30. */
  31. int buf_len;
  32. int buf_off;
  33. int tmp_len; /* used to find the start when decoding */
  34. int tmp_nl; /* If true, scan until '\n' */
  35. int encode;
  36. int start; /* have we started decoding yet? */
  37. int cont; /* <= 0 when finished */
  38. EVP_ENCODE_CTX *base64;
  39. char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
  40. char tmp[B64_BLOCK_SIZE];
  41. } BIO_B64_CTX;
  42. static const BIO_METHOD methods_b64 = {
  43. BIO_TYPE_BASE64,
  44. "base64 encoding",
  45. bwrite_conv,
  46. b64_write,
  47. bread_conv,
  48. b64_read,
  49. b64_puts,
  50. NULL, /* b64_gets, */
  51. b64_ctrl,
  52. b64_new,
  53. b64_free,
  54. b64_callback_ctrl,
  55. };
  56. const BIO_METHOD *BIO_f_base64(void)
  57. {
  58. return &methods_b64;
  59. }
  60. static int b64_new(BIO *bi)
  61. {
  62. BIO_B64_CTX *ctx;
  63. if ((ctx = OPENSSL_zalloc(sizeof(*ctx))) == NULL) {
  64. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  65. return 0;
  66. }
  67. ctx->cont = 1;
  68. ctx->start = 1;
  69. ctx->base64 = EVP_ENCODE_CTX_new();
  70. if (ctx->base64 == NULL) {
  71. OPENSSL_free(ctx);
  72. return 0;
  73. }
  74. BIO_set_data(bi, ctx);
  75. BIO_set_init(bi, 1);
  76. return 1;
  77. }
  78. static int b64_free(BIO *a)
  79. {
  80. BIO_B64_CTX *ctx;
  81. if (a == NULL)
  82. return 0;
  83. ctx = BIO_get_data(a);
  84. if (ctx == NULL)
  85. return 0;
  86. EVP_ENCODE_CTX_free(ctx->base64);
  87. OPENSSL_free(ctx);
  88. BIO_set_data(a, NULL);
  89. BIO_set_init(a, 0);
  90. return 1;
  91. }
  92. static int b64_read(BIO *b, char *out, int outl)
  93. {
  94. int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
  95. BIO_B64_CTX *ctx;
  96. unsigned char *p, *q;
  97. BIO *next;
  98. if (out == NULL)
  99. return 0;
  100. ctx = (BIO_B64_CTX *)BIO_get_data(b);
  101. next = BIO_next(b);
  102. if ((ctx == NULL) || (next == NULL))
  103. return 0;
  104. BIO_clear_retry_flags(b);
  105. if (ctx->encode != B64_DECODE) {
  106. ctx->encode = B64_DECODE;
  107. ctx->buf_len = 0;
  108. ctx->buf_off = 0;
  109. ctx->tmp_len = 0;
  110. EVP_DecodeInit(ctx->base64);
  111. }
  112. /* First check if there are bytes decoded/encoded */
  113. if (ctx->buf_len > 0) {
  114. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  115. i = ctx->buf_len - ctx->buf_off;
  116. if (i > outl)
  117. i = outl;
  118. OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
  119. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  120. ret = i;
  121. out += i;
  122. outl -= i;
  123. ctx->buf_off += i;
  124. if (ctx->buf_len == ctx->buf_off) {
  125. ctx->buf_len = 0;
  126. ctx->buf_off = 0;
  127. }
  128. }
  129. /*
  130. * At this point, we have room of outl bytes and an empty buffer, so we
  131. * should read in some more.
  132. */
  133. ret_code = 0;
  134. while (outl > 0) {
  135. if (ctx->cont <= 0)
  136. break;
  137. i = BIO_read(next, &(ctx->tmp[ctx->tmp_len]),
  138. B64_BLOCK_SIZE - ctx->tmp_len);
  139. if (i <= 0) {
  140. ret_code = i;
  141. /* Should we continue next time we are called? */
  142. if (!BIO_should_retry(next)) {
  143. ctx->cont = i;
  144. /* If buffer empty break */
  145. if (ctx->tmp_len == 0)
  146. break;
  147. /* Fall through and process what we have */
  148. else
  149. i = 0;
  150. }
  151. /* else we retry and add more data to buffer */
  152. else
  153. break;
  154. }
  155. i += ctx->tmp_len;
  156. ctx->tmp_len = i;
  157. /*
  158. * We need to scan, a line at a time until we have a valid line if we
  159. * are starting.
  160. */
  161. if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
  162. /* ctx->start=1; */
  163. ctx->tmp_len = 0;
  164. } else if (ctx->start) {
  165. q = p = (unsigned char *)ctx->tmp;
  166. num = 0;
  167. for (j = 0; j < i; j++) {
  168. if (*(q++) != '\n')
  169. continue;
  170. /*
  171. * due to a previous very long line, we need to keep on
  172. * scanning for a '\n' before we even start looking for
  173. * base64 encoded stuff.
  174. */
  175. if (ctx->tmp_nl) {
  176. p = q;
  177. ctx->tmp_nl = 0;
  178. continue;
  179. }
  180. k = EVP_DecodeUpdate(ctx->base64,
  181. (unsigned char *)ctx->buf,
  182. &num, p, q - p);
  183. if ((k <= 0) && (num == 0) && (ctx->start))
  184. EVP_DecodeInit(ctx->base64);
  185. else {
  186. if (p != (unsigned char *)
  187. &(ctx->tmp[0])) {
  188. i -= (p - (unsigned char *)
  189. &(ctx->tmp[0]));
  190. for (x = 0; x < i; x++)
  191. ctx->tmp[x] = p[x];
  192. }
  193. EVP_DecodeInit(ctx->base64);
  194. ctx->start = 0;
  195. break;
  196. }
  197. p = q;
  198. }
  199. /* we fell off the end without starting */
  200. if ((j == i) && (num == 0)) {
  201. /*
  202. * Is this is one long chunk?, if so, keep on reading until a
  203. * new line.
  204. */
  205. if (p == (unsigned char *)&(ctx->tmp[0])) {
  206. /* Check buffer full */
  207. if (i == B64_BLOCK_SIZE) {
  208. ctx->tmp_nl = 1;
  209. ctx->tmp_len = 0;
  210. }
  211. } else if (p != q) { /* finished on a '\n' */
  212. n = q - p;
  213. for (ii = 0; ii < n; ii++)
  214. ctx->tmp[ii] = p[ii];
  215. ctx->tmp_len = n;
  216. }
  217. /* else finished on a '\n' */
  218. continue;
  219. } else {
  220. ctx->tmp_len = 0;
  221. }
  222. } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
  223. /*
  224. * If buffer isn't full and we can retry then restart to read in
  225. * more data.
  226. */
  227. continue;
  228. }
  229. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  230. int z, jj;
  231. jj = i & ~3; /* process per 4 */
  232. z = EVP_DecodeBlock((unsigned char *)ctx->buf,
  233. (unsigned char *)ctx->tmp, jj);
  234. if (jj > 2) {
  235. if (ctx->tmp[jj - 1] == '=') {
  236. z--;
  237. if (ctx->tmp[jj - 2] == '=')
  238. z--;
  239. }
  240. }
  241. /*
  242. * z is now number of output bytes and jj is the number consumed
  243. */
  244. if (jj != i) {
  245. memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
  246. ctx->tmp_len = i - jj;
  247. }
  248. ctx->buf_len = 0;
  249. if (z > 0) {
  250. ctx->buf_len = z;
  251. }
  252. i = z;
  253. } else {
  254. i = EVP_DecodeUpdate(ctx->base64,
  255. (unsigned char *)ctx->buf, &ctx->buf_len,
  256. (unsigned char *)ctx->tmp, i);
  257. ctx->tmp_len = 0;
  258. }
  259. /*
  260. * If eof or an error was signalled, then the condition
  261. * 'ctx->cont <= 0' will prevent b64_read() from reading
  262. * more data on subsequent calls. This assignment was
  263. * deleted accidentally in commit 5562cfaca4f3.
  264. */
  265. ctx->cont = i;
  266. ctx->buf_off = 0;
  267. if (i < 0) {
  268. ret_code = 0;
  269. ctx->buf_len = 0;
  270. break;
  271. }
  272. if (ctx->buf_len <= outl)
  273. i = ctx->buf_len;
  274. else
  275. i = outl;
  276. memcpy(out, ctx->buf, i);
  277. ret += i;
  278. ctx->buf_off = i;
  279. if (ctx->buf_off == ctx->buf_len) {
  280. ctx->buf_len = 0;
  281. ctx->buf_off = 0;
  282. }
  283. outl -= i;
  284. out += i;
  285. }
  286. /* BIO_clear_retry_flags(b); */
  287. BIO_copy_next_retry(b);
  288. return ((ret == 0) ? ret_code : ret);
  289. }
  290. static int b64_write(BIO *b, const char *in, int inl)
  291. {
  292. int ret = 0;
  293. int n;
  294. int i;
  295. BIO_B64_CTX *ctx;
  296. BIO *next;
  297. ctx = (BIO_B64_CTX *)BIO_get_data(b);
  298. next = BIO_next(b);
  299. if ((ctx == NULL) || (next == NULL))
  300. return 0;
  301. BIO_clear_retry_flags(b);
  302. if (ctx->encode != B64_ENCODE) {
  303. ctx->encode = B64_ENCODE;
  304. ctx->buf_len = 0;
  305. ctx->buf_off = 0;
  306. ctx->tmp_len = 0;
  307. EVP_EncodeInit(ctx->base64);
  308. }
  309. OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
  310. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  311. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  312. n = ctx->buf_len - ctx->buf_off;
  313. while (n > 0) {
  314. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  315. if (i <= 0) {
  316. BIO_copy_next_retry(b);
  317. return i;
  318. }
  319. OPENSSL_assert(i <= n);
  320. ctx->buf_off += i;
  321. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  322. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  323. n -= i;
  324. }
  325. /* at this point all pending data has been written */
  326. ctx->buf_off = 0;
  327. ctx->buf_len = 0;
  328. if ((in == NULL) || (inl <= 0))
  329. return 0;
  330. while (inl > 0) {
  331. n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
  332. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  333. if (ctx->tmp_len > 0) {
  334. OPENSSL_assert(ctx->tmp_len <= 3);
  335. n = 3 - ctx->tmp_len;
  336. /*
  337. * There's a theoretical possibility for this
  338. */
  339. if (n > inl)
  340. n = inl;
  341. memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
  342. ctx->tmp_len += n;
  343. ret += n;
  344. if (ctx->tmp_len < 3)
  345. break;
  346. ctx->buf_len =
  347. EVP_EncodeBlock((unsigned char *)ctx->buf,
  348. (unsigned char *)ctx->tmp, ctx->tmp_len);
  349. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  350. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  351. /*
  352. * Since we're now done using the temporary buffer, the
  353. * length should be 0'd
  354. */
  355. ctx->tmp_len = 0;
  356. } else {
  357. if (n < 3) {
  358. memcpy(ctx->tmp, in, n);
  359. ctx->tmp_len = n;
  360. ret += n;
  361. break;
  362. }
  363. n -= n % 3;
  364. ctx->buf_len =
  365. EVP_EncodeBlock((unsigned char *)ctx->buf,
  366. (const unsigned char *)in, n);
  367. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  368. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  369. ret += n;
  370. }
  371. } else {
  372. if (!EVP_EncodeUpdate(ctx->base64,
  373. (unsigned char *)ctx->buf, &ctx->buf_len,
  374. (unsigned char *)in, n))
  375. return ((ret == 0) ? -1 : ret);
  376. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  377. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  378. ret += n;
  379. }
  380. inl -= n;
  381. in += n;
  382. ctx->buf_off = 0;
  383. n = ctx->buf_len;
  384. while (n > 0) {
  385. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  386. if (i <= 0) {
  387. BIO_copy_next_retry(b);
  388. return ((ret == 0) ? i : ret);
  389. }
  390. OPENSSL_assert(i <= n);
  391. n -= i;
  392. ctx->buf_off += i;
  393. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  394. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  395. }
  396. ctx->buf_len = 0;
  397. ctx->buf_off = 0;
  398. }
  399. return ret;
  400. }
  401. static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
  402. {
  403. BIO_B64_CTX *ctx;
  404. long ret = 1;
  405. int i;
  406. BIO *next;
  407. ctx = (BIO_B64_CTX *)BIO_get_data(b);
  408. next = BIO_next(b);
  409. if ((ctx == NULL) || (next == NULL))
  410. return 0;
  411. switch (cmd) {
  412. case BIO_CTRL_RESET:
  413. ctx->cont = 1;
  414. ctx->start = 1;
  415. ctx->encode = B64_NONE;
  416. ret = BIO_ctrl(next, cmd, num, ptr);
  417. break;
  418. case BIO_CTRL_EOF: /* More to read */
  419. if (ctx->cont <= 0)
  420. ret = 1;
  421. else
  422. ret = BIO_ctrl(next, cmd, num, ptr);
  423. break;
  424. case BIO_CTRL_WPENDING: /* More to write in buffer */
  425. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  426. ret = ctx->buf_len - ctx->buf_off;
  427. if ((ret == 0) && (ctx->encode != B64_NONE)
  428. && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
  429. ret = 1;
  430. else if (ret <= 0)
  431. ret = BIO_ctrl(next, cmd, num, ptr);
  432. break;
  433. case BIO_CTRL_PENDING: /* More to read in buffer */
  434. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  435. ret = ctx->buf_len - ctx->buf_off;
  436. if (ret <= 0)
  437. ret = BIO_ctrl(next, cmd, num, ptr);
  438. break;
  439. case BIO_CTRL_FLUSH:
  440. /* do a final write */
  441. again:
  442. while (ctx->buf_len != ctx->buf_off) {
  443. i = b64_write(b, NULL, 0);
  444. if (i < 0)
  445. return i;
  446. }
  447. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  448. if (ctx->tmp_len != 0) {
  449. ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
  450. (unsigned char *)ctx->tmp,
  451. ctx->tmp_len);
  452. ctx->buf_off = 0;
  453. ctx->tmp_len = 0;
  454. goto again;
  455. }
  456. } else if (ctx->encode != B64_NONE
  457. && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
  458. ctx->buf_off = 0;
  459. EVP_EncodeFinal(ctx->base64,
  460. (unsigned char *)ctx->buf, &(ctx->buf_len));
  461. /* push out the bytes */
  462. goto again;
  463. }
  464. /* Finally flush the underlying BIO */
  465. ret = BIO_ctrl(next, cmd, num, ptr);
  466. break;
  467. case BIO_C_DO_STATE_MACHINE:
  468. BIO_clear_retry_flags(b);
  469. ret = BIO_ctrl(next, cmd, num, ptr);
  470. BIO_copy_next_retry(b);
  471. break;
  472. case BIO_CTRL_DUP:
  473. break;
  474. case BIO_CTRL_INFO:
  475. case BIO_CTRL_GET:
  476. case BIO_CTRL_SET:
  477. default:
  478. ret = BIO_ctrl(next, cmd, num, ptr);
  479. break;
  480. }
  481. return ret;
  482. }
  483. static long b64_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
  484. {
  485. BIO *next = BIO_next(b);
  486. if (next == NULL)
  487. return 0;
  488. return BIO_callback_ctrl(next, cmd, fp);
  489. }
  490. static int b64_puts(BIO *b, const char *str)
  491. {
  492. return b64_write(b, str, strlen(str));
  493. }