bio_b64.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. 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, "base64 encoding",
  44. /* TODO: Convert to new style write function */
  45. bwrite_conv,
  46. b64_write,
  47. /* TODO: Convert to new style read function */
  48. bread_conv,
  49. b64_read,
  50. b64_puts,
  51. NULL, /* b64_gets, */
  52. b64_ctrl,
  53. b64_new,
  54. b64_free,
  55. b64_callback_ctrl,
  56. };
  57. const BIO_METHOD *BIO_f_base64(void)
  58. {
  59. return &methods_b64;
  60. }
  61. static int b64_new(BIO *bi)
  62. {
  63. BIO_B64_CTX *ctx;
  64. ctx = OPENSSL_zalloc(sizeof(*ctx));
  65. if (ctx == NULL)
  66. return 0;
  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. 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) {
  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((unsigned char *)ctx->buf,
  341. (unsigned char *)ctx->tmp, ctx->tmp_len);
  342. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  343. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  344. /*
  345. * Since we're now done using the temporary buffer, the
  346. * length should be 0'd
  347. */
  348. ctx->tmp_len = 0;
  349. } else {
  350. if (n < 3) {
  351. memcpy(ctx->tmp, in, n);
  352. ctx->tmp_len = n;
  353. ret += n;
  354. break;
  355. }
  356. n -= n % 3;
  357. ctx->buf_len =
  358. EVP_EncodeBlock((unsigned char *)ctx->buf,
  359. (const unsigned char *)in, n);
  360. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  361. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  362. ret += n;
  363. }
  364. } else {
  365. if (!EVP_EncodeUpdate(ctx->base64,
  366. (unsigned char *)ctx->buf, &ctx->buf_len,
  367. (unsigned char *)in, n))
  368. return ((ret == 0) ? -1 : ret);
  369. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  370. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  371. ret += n;
  372. }
  373. inl -= n;
  374. in += n;
  375. ctx->buf_off = 0;
  376. n = ctx->buf_len;
  377. while (n > 0) {
  378. i = BIO_write(next, &(ctx->buf[ctx->buf_off]), n);
  379. if (i <= 0) {
  380. BIO_copy_next_retry(b);
  381. return ((ret == 0) ? i : ret);
  382. }
  383. OPENSSL_assert(i <= n);
  384. n -= i;
  385. ctx->buf_off += i;
  386. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  387. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  388. }
  389. ctx->buf_len = 0;
  390. ctx->buf_off = 0;
  391. }
  392. return (ret);
  393. }
  394. static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
  395. {
  396. BIO_B64_CTX *ctx;
  397. long ret = 1;
  398. int i;
  399. BIO *next;
  400. ctx = (BIO_B64_CTX *)BIO_get_data(b);
  401. next = BIO_next(b);
  402. if ((ctx == NULL) || (next == NULL))
  403. return 0;
  404. switch (cmd) {
  405. case BIO_CTRL_RESET:
  406. ctx->cont = 1;
  407. ctx->start = 1;
  408. ctx->encode = B64_NONE;
  409. ret = BIO_ctrl(next, cmd, num, ptr);
  410. break;
  411. case BIO_CTRL_EOF: /* More to read */
  412. if (ctx->cont <= 0)
  413. ret = 1;
  414. else
  415. ret = BIO_ctrl(next, cmd, num, ptr);
  416. break;
  417. case BIO_CTRL_WPENDING: /* More to write in buffer */
  418. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  419. ret = ctx->buf_len - ctx->buf_off;
  420. if ((ret == 0) && (ctx->encode != B64_NONE)
  421. && (EVP_ENCODE_CTX_num(ctx->base64) != 0))
  422. ret = 1;
  423. else if (ret <= 0)
  424. ret = BIO_ctrl(next, cmd, num, ptr);
  425. break;
  426. case BIO_CTRL_PENDING: /* More to read in buffer */
  427. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  428. ret = ctx->buf_len - ctx->buf_off;
  429. if (ret <= 0)
  430. ret = BIO_ctrl(next, cmd, num, ptr);
  431. break;
  432. case BIO_CTRL_FLUSH:
  433. /* do a final write */
  434. again:
  435. while (ctx->buf_len != ctx->buf_off) {
  436. i = b64_write(b, NULL, 0);
  437. if (i < 0)
  438. return i;
  439. }
  440. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  441. if (ctx->tmp_len != 0) {
  442. ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
  443. (unsigned char *)ctx->tmp,
  444. ctx->tmp_len);
  445. ctx->buf_off = 0;
  446. ctx->tmp_len = 0;
  447. goto again;
  448. }
  449. } else if (ctx->encode != B64_NONE
  450. && EVP_ENCODE_CTX_num(ctx->base64) != 0) {
  451. ctx->buf_off = 0;
  452. EVP_EncodeFinal(ctx->base64,
  453. (unsigned char *)ctx->buf, &(ctx->buf_len));
  454. /* push out the bytes */
  455. goto again;
  456. }
  457. /* Finally flush the underlying BIO */
  458. ret = BIO_ctrl(next, cmd, num, ptr);
  459. break;
  460. case BIO_C_DO_STATE_MACHINE:
  461. BIO_clear_retry_flags(b);
  462. ret = BIO_ctrl(next, cmd, num, ptr);
  463. BIO_copy_next_retry(b);
  464. break;
  465. case BIO_CTRL_DUP:
  466. break;
  467. case BIO_CTRL_INFO:
  468. case BIO_CTRL_GET:
  469. case BIO_CTRL_SET:
  470. default:
  471. ret = BIO_ctrl(next, cmd, num, ptr);
  472. break;
  473. }
  474. return ret;
  475. }
  476. static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  477. {
  478. long ret = 1;
  479. BIO *next = BIO_next(b);
  480. if (next == NULL)
  481. return 0;
  482. switch (cmd) {
  483. default:
  484. ret = BIO_callback_ctrl(next, cmd, fp);
  485. break;
  486. }
  487. return (ret);
  488. }
  489. static int b64_puts(BIO *b, const char *str)
  490. {
  491. return b64_write(b, str, strlen(str));
  492. }