bio_b64.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /* crypto/evp/bio_b64.c */
  2. /* Copyright (C) 1995-1998 Eric Young ([email protected])
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. /*
  307. * If eof or an error was signalled, then the condition
  308. * 'ctx->cont <= 0' will prevent b64_read() from reading
  309. * more data on subsequent calls. This assignment was
  310. * deleted accidentally in commit 5562cfaca4f3.
  311. */
  312. ctx->cont = i;
  313. ctx->buf_off = 0;
  314. if (i < 0) {
  315. ret_code = 0;
  316. ctx->buf_len = 0;
  317. break;
  318. }
  319. if (ctx->buf_len <= outl)
  320. i = ctx->buf_len;
  321. else
  322. i = outl;
  323. memcpy(out, ctx->buf, i);
  324. ret += i;
  325. ctx->buf_off = i;
  326. if (ctx->buf_off == ctx->buf_len) {
  327. ctx->buf_len = 0;
  328. ctx->buf_off = 0;
  329. }
  330. outl -= i;
  331. out += i;
  332. }
  333. /* BIO_clear_retry_flags(b); */
  334. BIO_copy_next_retry(b);
  335. return ((ret == 0) ? ret_code : ret);
  336. }
  337. static int b64_write(BIO *b, const char *in, int inl)
  338. {
  339. int ret = 0;
  340. int n;
  341. int i;
  342. BIO_B64_CTX *ctx;
  343. ctx = (BIO_B64_CTX *)b->ptr;
  344. BIO_clear_retry_flags(b);
  345. if (ctx->encode != B64_ENCODE) {
  346. ctx->encode = B64_ENCODE;
  347. ctx->buf_len = 0;
  348. ctx->buf_off = 0;
  349. ctx->tmp_len = 0;
  350. EVP_EncodeInit(&(ctx->base64));
  351. }
  352. OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
  353. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  354. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  355. n = ctx->buf_len - ctx->buf_off;
  356. while (n > 0) {
  357. i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
  358. if (i <= 0) {
  359. BIO_copy_next_retry(b);
  360. return (i);
  361. }
  362. OPENSSL_assert(i <= n);
  363. ctx->buf_off += i;
  364. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  365. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  366. n -= i;
  367. }
  368. /* at this point all pending data has been written */
  369. ctx->buf_off = 0;
  370. ctx->buf_len = 0;
  371. if ((in == NULL) || (inl <= 0))
  372. return (0);
  373. while (inl > 0) {
  374. n = (inl > B64_BLOCK_SIZE) ? B64_BLOCK_SIZE : inl;
  375. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  376. if (ctx->tmp_len > 0) {
  377. OPENSSL_assert(ctx->tmp_len <= 3);
  378. n = 3 - ctx->tmp_len;
  379. /*
  380. * There's a theoretical possibility for this
  381. */
  382. if (n > inl)
  383. n = inl;
  384. memcpy(&(ctx->tmp[ctx->tmp_len]), in, n);
  385. ctx->tmp_len += n;
  386. ret += n;
  387. if (ctx->tmp_len < 3)
  388. break;
  389. ctx->buf_len =
  390. EVP_EncodeBlock((unsigned char *)ctx->buf,
  391. (unsigned char *)ctx->tmp, ctx->tmp_len);
  392. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  393. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  394. /*
  395. * Since we're now done using the temporary buffer, the
  396. * length should be 0'd
  397. */
  398. ctx->tmp_len = 0;
  399. } else {
  400. if (n < 3) {
  401. memcpy(ctx->tmp, in, n);
  402. ctx->tmp_len = n;
  403. ret += n;
  404. break;
  405. }
  406. n -= n % 3;
  407. ctx->buf_len =
  408. EVP_EncodeBlock((unsigned char *)ctx->buf,
  409. (const unsigned char *)in, n);
  410. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  411. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  412. ret += n;
  413. }
  414. } else {
  415. EVP_EncodeUpdate(&(ctx->base64),
  416. (unsigned char *)ctx->buf, &ctx->buf_len,
  417. (unsigned char *)in, n);
  418. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  419. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  420. ret += n;
  421. }
  422. inl -= n;
  423. in += n;
  424. ctx->buf_off = 0;
  425. n = ctx->buf_len;
  426. while (n > 0) {
  427. i = BIO_write(b->next_bio, &(ctx->buf[ctx->buf_off]), n);
  428. if (i <= 0) {
  429. BIO_copy_next_retry(b);
  430. return ((ret == 0) ? i : ret);
  431. }
  432. OPENSSL_assert(i <= n);
  433. n -= i;
  434. ctx->buf_off += i;
  435. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  436. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  437. }
  438. ctx->buf_len = 0;
  439. ctx->buf_off = 0;
  440. }
  441. return (ret);
  442. }
  443. static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
  444. {
  445. BIO_B64_CTX *ctx;
  446. long ret = 1;
  447. int i;
  448. ctx = (BIO_B64_CTX *)b->ptr;
  449. switch (cmd) {
  450. case BIO_CTRL_RESET:
  451. ctx->cont = 1;
  452. ctx->start = 1;
  453. ctx->encode = B64_NONE;
  454. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  455. break;
  456. case BIO_CTRL_EOF: /* More to read */
  457. if (ctx->cont <= 0)
  458. ret = 1;
  459. else
  460. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  461. break;
  462. case BIO_CTRL_WPENDING: /* More to write in buffer */
  463. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  464. ret = ctx->buf_len - ctx->buf_off;
  465. if ((ret == 0) && (ctx->encode != B64_NONE)
  466. && (ctx->base64.num != 0))
  467. ret = 1;
  468. else if (ret <= 0)
  469. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  470. break;
  471. case BIO_CTRL_PENDING: /* More to read in buffer */
  472. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  473. ret = ctx->buf_len - ctx->buf_off;
  474. if (ret <= 0)
  475. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  476. break;
  477. case BIO_CTRL_FLUSH:
  478. /* do a final write */
  479. again:
  480. while (ctx->buf_len != ctx->buf_off) {
  481. i = b64_write(b, NULL, 0);
  482. if (i < 0)
  483. return i;
  484. }
  485. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL) {
  486. if (ctx->tmp_len != 0) {
  487. ctx->buf_len = EVP_EncodeBlock((unsigned char *)ctx->buf,
  488. (unsigned char *)ctx->tmp,
  489. ctx->tmp_len);
  490. ctx->buf_off = 0;
  491. ctx->tmp_len = 0;
  492. goto again;
  493. }
  494. } else if (ctx->encode != B64_NONE && ctx->base64.num != 0) {
  495. ctx->buf_off = 0;
  496. EVP_EncodeFinal(&(ctx->base64),
  497. (unsigned char *)ctx->buf, &(ctx->buf_len));
  498. /* push out the bytes */
  499. goto again;
  500. }
  501. /* Finally flush the underlying BIO */
  502. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  503. break;
  504. case BIO_C_DO_STATE_MACHINE:
  505. BIO_clear_retry_flags(b);
  506. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  507. BIO_copy_next_retry(b);
  508. break;
  509. case BIO_CTRL_DUP:
  510. break;
  511. case BIO_CTRL_INFO:
  512. case BIO_CTRL_GET:
  513. case BIO_CTRL_SET:
  514. default:
  515. ret = BIO_ctrl(b->next_bio, cmd, num, ptr);
  516. break;
  517. }
  518. return (ret);
  519. }
  520. static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  521. {
  522. long ret = 1;
  523. if (b->next_bio == NULL)
  524. return (0);
  525. switch (cmd) {
  526. default:
  527. ret = BIO_callback_ctrl(b->next_bio, cmd, fp);
  528. break;
  529. }
  530. return (ret);
  531. }
  532. static int b64_puts(BIO *b, const char *str)
  533. {
  534. return b64_write(b, str, strlen(str));
  535. }