bio_b64.c 14 KB

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