bio_b64.c 15 KB

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