2
0

bio_b64.c 15 KB

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