bio_b64.c 15 KB

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