bio_b64.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. /* crypto/evp/bio_b64.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <errno.h>
  60. #include "cryptlib.h"
  61. #include <openssl/buffer.h>
  62. #include <openssl/evp.h>
  63. static int b64_write(BIO *h, const char *buf, int num);
  64. static int b64_read(BIO *h, char *buf, int size);
  65. static int b64_puts(BIO *h, const char *str);
  66. /*
  67. * static int b64_gets(BIO *h, char *str, int size);
  68. */
  69. static long b64_ctrl(BIO *h, int cmd, long arg1, void *arg2);
  70. static int b64_new(BIO *h);
  71. static int b64_free(BIO *data);
  72. static long b64_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
  73. #define B64_BLOCK_SIZE 1024
  74. #define B64_BLOCK_SIZE2 768
  75. #define B64_NONE 0
  76. #define B64_ENCODE 1
  77. #define B64_DECODE 2
  78. typedef struct b64_struct {
  79. /*
  80. * BIO *bio; moved to the BIO structure
  81. */
  82. int buf_len;
  83. int buf_off;
  84. int tmp_len; /* used to find the start when decoding */
  85. int tmp_nl; /* If true, scan until '\n' */
  86. int encode;
  87. int start; /* have we started decoding yet? */
  88. int cont; /* <= 0 when finished */
  89. EVP_ENCODE_CTX base64;
  90. char buf[EVP_ENCODE_LENGTH(B64_BLOCK_SIZE) + 10];
  91. char tmp[B64_BLOCK_SIZE];
  92. } BIO_B64_CTX;
  93. static BIO_METHOD methods_b64 = {
  94. BIO_TYPE_BASE64, "base64 encoding",
  95. b64_write,
  96. b64_read,
  97. b64_puts,
  98. NULL, /* b64_gets, */
  99. b64_ctrl,
  100. b64_new,
  101. b64_free,
  102. b64_callback_ctrl,
  103. };
  104. BIO_METHOD *BIO_f_base64(void)
  105. {
  106. return (&methods_b64);
  107. }
  108. static int b64_new(BIO *bi)
  109. {
  110. BIO_B64_CTX *ctx;
  111. ctx = (BIO_B64_CTX *)OPENSSL_malloc(sizeof(BIO_B64_CTX));
  112. if (ctx == NULL)
  113. return (0);
  114. ctx->buf_len = 0;
  115. ctx->tmp_len = 0;
  116. ctx->tmp_nl = 0;
  117. ctx->buf_off = 0;
  118. ctx->cont = 1;
  119. ctx->start = 1;
  120. ctx->encode = 0;
  121. bi->init = 1;
  122. bi->ptr = (char *)ctx;
  123. bi->flags = 0;
  124. bi->num = 0;
  125. return (1);
  126. }
  127. static int b64_free(BIO *a)
  128. {
  129. if (a == NULL)
  130. return (0);
  131. OPENSSL_free(a->ptr);
  132. a->ptr = NULL;
  133. a->init = 0;
  134. a->flags = 0;
  135. return (1);
  136. }
  137. static int b64_read(BIO *b, char *out, int outl)
  138. {
  139. int ret = 0, i, ii, j, k, x, n, num, ret_code = 0;
  140. BIO_B64_CTX *ctx;
  141. unsigned char *p, *q;
  142. if (out == NULL)
  143. return (0);
  144. ctx = (BIO_B64_CTX *)b->ptr;
  145. if ((ctx == NULL) || (b->next_bio == NULL))
  146. return (0);
  147. BIO_clear_retry_flags(b);
  148. if (ctx->encode != B64_DECODE) {
  149. ctx->encode = B64_DECODE;
  150. ctx->buf_len = 0;
  151. ctx->buf_off = 0;
  152. ctx->tmp_len = 0;
  153. EVP_DecodeInit(&(ctx->base64));
  154. }
  155. /* First check if there are bytes decoded/encoded */
  156. if (ctx->buf_len > 0) {
  157. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  158. i = ctx->buf_len - ctx->buf_off;
  159. if (i > outl)
  160. i = outl;
  161. OPENSSL_assert(ctx->buf_off + i < (int)sizeof(ctx->buf));
  162. memcpy(out, &(ctx->buf[ctx->buf_off]), i);
  163. ret = i;
  164. out += i;
  165. outl -= i;
  166. ctx->buf_off += i;
  167. if (ctx->buf_len == ctx->buf_off) {
  168. ctx->buf_len = 0;
  169. ctx->buf_off = 0;
  170. }
  171. }
  172. /*
  173. * At this point, we have room of outl bytes and an empty buffer, so we
  174. * should read in some more.
  175. */
  176. ret_code = 0;
  177. while (outl > 0) {
  178. if (ctx->cont <= 0)
  179. break;
  180. i = BIO_read(b->next_bio, &(ctx->tmp[ctx->tmp_len]),
  181. B64_BLOCK_SIZE - ctx->tmp_len);
  182. if (i <= 0) {
  183. ret_code = i;
  184. /* Should we continue next time we are called? */
  185. if (!BIO_should_retry(b->next_bio)) {
  186. ctx->cont = i;
  187. /* If buffer empty break */
  188. if (ctx->tmp_len == 0)
  189. break;
  190. /* Fall through and process what we have */
  191. else
  192. i = 0;
  193. }
  194. /* else we retry and add more data to buffer */
  195. else
  196. break;
  197. }
  198. i += ctx->tmp_len;
  199. ctx->tmp_len = i;
  200. /*
  201. * We need to scan, a line at a time until we have a valid line if we
  202. * are starting.
  203. */
  204. if (ctx->start && (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)) {
  205. /* ctx->start=1; */
  206. ctx->tmp_len = 0;
  207. } else if (ctx->start) {
  208. q = p = (unsigned char *)ctx->tmp;
  209. num = 0;
  210. for (j = 0; j < i; j++) {
  211. if (*(q++) != '\n')
  212. continue;
  213. /*
  214. * due to a previous very long line, we need to keep on
  215. * scanning for a '\n' before we even start looking for
  216. * base64 encoded stuff.
  217. */
  218. if (ctx->tmp_nl) {
  219. p = q;
  220. ctx->tmp_nl = 0;
  221. continue;
  222. }
  223. k = EVP_DecodeUpdate(&(ctx->base64),
  224. (unsigned char *)ctx->buf,
  225. &num, p, q - p);
  226. if ((k <= 0) && (num == 0) && (ctx->start))
  227. EVP_DecodeInit(&ctx->base64);
  228. else {
  229. if (p != (unsigned char *)
  230. &(ctx->tmp[0])) {
  231. i -= (p - (unsigned char *)
  232. &(ctx->tmp[0]));
  233. for (x = 0; x < i; x++)
  234. ctx->tmp[x] = p[x];
  235. }
  236. EVP_DecodeInit(&ctx->base64);
  237. ctx->start = 0;
  238. break;
  239. }
  240. p = q;
  241. }
  242. /* we fell off the end without starting */
  243. if ((j == i) && (num == 0)) {
  244. /*
  245. * Is this is one long chunk?, if so, keep on reading until a
  246. * new line.
  247. */
  248. if (p == (unsigned char *)&(ctx->tmp[0])) {
  249. /* Check buffer full */
  250. if (i == B64_BLOCK_SIZE) {
  251. ctx->tmp_nl = 1;
  252. ctx->tmp_len = 0;
  253. }
  254. } else if (p != q) { /* finished on a '\n' */
  255. n = q - p;
  256. for (ii = 0; ii < n; ii++)
  257. ctx->tmp[ii] = p[ii];
  258. ctx->tmp_len = n;
  259. }
  260. /* else finished on a '\n' */
  261. continue;
  262. } else {
  263. ctx->tmp_len = 0;
  264. }
  265. } else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0)) {
  266. /*
  267. * If buffer isn't full and we can retry then restart to read in
  268. * more data.
  269. */
  270. continue;
  271. }
  272. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  273. int z, jj;
  274. #if 0
  275. jj = (i >> 2) << 2;
  276. #else
  277. jj = i & ~3; /* process per 4 */
  278. #endif
  279. z = EVP_DecodeBlock((unsigned char *)ctx->buf,
  280. (unsigned char *)ctx->tmp, jj);
  281. if (jj > 2) {
  282. if (ctx->tmp[jj - 1] == '=') {
  283. z--;
  284. if (ctx->tmp[jj - 2] == '=')
  285. z--;
  286. }
  287. }
  288. /*
  289. * z is now number of output bytes and jj is the number consumed
  290. */
  291. if (jj != i) {
  292. memmove(ctx->tmp, &ctx->tmp[jj], i - jj);
  293. ctx->tmp_len = i - jj;
  294. }
  295. ctx->buf_len = 0;
  296. if (z > 0) {
  297. ctx->buf_len = z;
  298. }
  299. i = z;
  300. } else {
  301. i = EVP_DecodeUpdate(&(ctx->base64),
  302. (unsigned char *)ctx->buf, &ctx->buf_len,
  303. (unsigned char *)ctx->tmp, i);
  304. ctx->tmp_len = 0;
  305. }
  306. ctx->buf_off = 0;
  307. if (i < 0) {
  308. ret_code = 0;
  309. ctx->buf_len = 0;
  310. break;
  311. }
  312. if (ctx->buf_len <= outl)
  313. i = ctx->buf_len;
  314. else
  315. i = outl;
  316. memcpy(out, ctx->buf, i);
  317. ret += i;
  318. ctx->buf_off = i;
  319. if (ctx->buf_off == ctx->buf_len) {
  320. ctx->buf_len = 0;
  321. ctx->buf_off = 0;
  322. }
  323. outl -= i;
  324. out += i;
  325. }
  326. /* BIO_clear_retry_flags(b); */
  327. BIO_copy_next_retry(b);
  328. return ((ret == 0) ? ret_code : ret);
  329. }
  330. static int b64_write(BIO *b, const char *in, int inl)
  331. {
  332. int ret = 0;
  333. int n;
  334. int i;
  335. BIO_B64_CTX *ctx;
  336. ctx = (BIO_B64_CTX *)b->ptr;
  337. BIO_clear_retry_flags(b);
  338. if (ctx->encode != B64_ENCODE) {
  339. ctx->encode = B64_ENCODE;
  340. ctx->buf_len = 0;
  341. ctx->buf_off = 0;
  342. ctx->tmp_len = 0;
  343. EVP_EncodeInit(&(ctx->base64));
  344. }
  345. OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
  346. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  347. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  348. n = ctx->buf_len - ctx->buf_off;
  349. while (n > 0) {
  350. i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
  351. if (i <= 0) {
  352. BIO_copy_next_retry(b);
  353. return (i);
  354. }
  355. OPENSSL_assert(i <= n);
  356. ctx->buf_off += i;
  357. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  358. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  359. n -= i;
  360. }
  361. /* at this point all pending data has been written */
  362. ctx->buf_off = 0;
  363. ctx->buf_len = 0;
  364. if ((in == NULL) || (inl <= 0))
  365. return (0);
  366. while (inl > 0) {
  367. n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
  368. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  369. if (ctx->tmp_len > 0) {
  370. OPENSSL_assert(ctx->tmp_len <= 3);
  371. n = 3 - ctx->tmp_len;
  372. /*
  373. * There's a theoretical possibility for this
  374. */
  375. if (n > inl)
  376. n = inl;
  377. memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
  378. ctx->tmp_len += n;
  379. ret += n;
  380. if (ctx->tmp_len < 3)
  381. break;
  382. ctx->buf_len =
  383. EVP_EncodeBlock((unsigned char *)ctx->buf,
  384. (unsigned char *)ctx->tmp, ctx->tmp_len);
  385. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  386. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  387. /*
  388. * Since we're now done using the temporary buffer, the
  389. * length should be 0'd
  390. */
  391. ctx->tmp_len = 0;
  392. } else {
  393. if (n < 3) {
  394. memcpy(ctx->tmp, in, n);
  395. ctx->tmp_len = n;
  396. ret += n;
  397. break;
  398. }
  399. n -= n % 3;
  400. ctx->buf_len =
  401. EVP_EncodeBlock((unsigned char *)ctx->buf,
  402. (const unsigned char *)in, n);
  403. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  404. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  405. ret += n;
  406. }
  407. } else {
  408. EVP_EncodeUpdate(&(ctx->base64),
  409. (unsigned char *)ctx->buf, &ctx->buf_len,
  410. (unsigned char *)in, n);
  411. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  412. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  413. ret += n;
  414. }
  415. inl -= n;
  416. in += n;
  417. ctx->buf_off = 0;
  418. n = ctx->buf_len;
  419. while (n > 0) {
  420. i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
  421. if (i <= 0) {
  422. BIO_copy_next_retry(b);
  423. return ((ret == 0) ? i : ret);
  424. }
  425. OPENSSL_assert(i <= n);
  426. n -= i;
  427. ctx->buf_off += i;
  428. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  429. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  430. }
  431. ctx->buf_len = 0;
  432. ctx->buf_off = 0;
  433. }
  434. return (ret);
  435. }
  436. static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
  437. {
  438. BIO_B64_CTX *ctx;
  439. long ret = 1;
  440. int i;
  441. ctx = (BIO_B64_CTX *)b->ptr;
  442. switch (cmd) {
  443. case BIO_CTRL_RESET:
  444. ctx->cont = 1;
  445. ctx->start = 1;
  446. ctx->encode = B64_NONE;
  447. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  448. break;
  449. case BIO_CTRL_EOF: /* More to read */
  450. if (ctx->cont <= 0)
  451. ret = 1;
  452. else
  453. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  454. break;
  455. case BIO_CTRL_WPENDING: /* More to write in buffer */
  456. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  457. ret = ctx->buf_len - ctx->buf_off;
  458. if ((ret == 0) && (ctx->encode != B64_NONE)
  459. && (ctx->base64.num != 0))
  460. ret = 1;
  461. else if (ret <= 0)
  462. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  463. break;
  464. case BIO_CTRL_PENDING: /* More to read in buffer */
  465. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  466. ret = ctx->buf_len - ctx->buf_off;
  467. if (ret <= 0)
  468. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  469. break;
  470. case BIO_CTRL_FLUSH:
  471. /* do a final write */
  472. again:
  473. while (ctx->buf_len != ctx->buf_off) {
  474. i = b64_write(b, NULL, 0);
  475. if (i < 0)
  476. return i;
  477. }
  478. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  479. if (ctx->tmp_len != 0) {
  480. ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
  481. (unsigned char *)ctx->tmp,
  482. ctx->tmp_len);
  483. ctx->buf_off = 0;
  484. ctx->tmp_len = 0;
  485. goto again;
  486. }
  487. } else if (ctx->encode != B64_NONE && ctx->base64.num != 0) {
  488. ctx->buf_off = 0;
  489. EVP_EncodeFinal(&(ctx->base64),
  490. (unsigned char *)ctx->buf, &(ctx->buf_len));
  491. /* push out the bytes */
  492. goto again;
  493. }
  494. /* Finally flush the underlying BIO */
  495. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  496. break;
  497. case BIO_C_DO_STATE_MACHINE:
  498. BIO_clear_retry_flags(b);
  499. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  500. BIO_copy_next_retry(b);
  501. break;
  502. case BIO_CTRL_DUP:
  503. break;
  504. case BIO_CTRL_INFO:
  505. case BIO_CTRL_GET:
  506. case BIO_CTRL_SET:
  507. default:
  508. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  509. break;
  510. }
  511. return (ret);
  512. }
  513. static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  514. {
  515. long ret = 1;
  516. if (b->next_bio == NULL)
  517. return (0);
  518. switch (cmd) {
  519. default:
  520. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  521. break;
  522. }
  523. return (ret);
  524. }
  525. static int b64_puts(BIO *b, const char *str)
  526. {
  527. return b64_write(b, str, strlen(str));
  528. }