bio_b64.c 16 KB

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