2
0

bio_b64.c 13 KB

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