bio_b64.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  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. /*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. num = 0;
  208. for (j=0; j<i; j++)
  209. {
  210. if (*(q++) != '\n') continue;
  211. /* due to a previous very long line,
  212. * we need to keep on scanning for a '\n'
  213. * before we even start looking for
  214. * base64 encoded stuff. */
  215. if (ctx->tmp_nl)
  216. {
  217. p=q;
  218. ctx->tmp_nl=0;
  219. continue;
  220. }
  221. k=EVP_DecodeUpdate(&(ctx->base64),
  222. (unsigned char *)ctx->buf,
  223. &num,p,q-p);
  224. if ((k <= 0) && (num == 0) && (ctx->start))
  225. EVP_DecodeInit(&ctx->base64);
  226. else
  227. {
  228. if (p != (unsigned char *)
  229. &(ctx->tmp[0]))
  230. {
  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
  246. * reading until a new line. */
  247. if (p == (unsigned char *)&(ctx->tmp[0]))
  248. {
  249. /* Check buffer full */
  250. if (i == B64_BLOCK_SIZE)
  251. {
  252. ctx->tmp_nl=1;
  253. ctx->tmp_len=0;
  254. }
  255. }
  256. else if (p != q) /* finished on a '\n' */
  257. {
  258. n=q-p;
  259. for (ii=0; ii<n; ii++)
  260. ctx->tmp[ii]=p[ii];
  261. ctx->tmp_len=n;
  262. }
  263. /* else finished on a '\n' */
  264. continue;
  265. }
  266. else
  267. {
  268. ctx->tmp_len=0;
  269. }
  270. }
  271. else if ((i < B64_BLOCK_SIZE) && (ctx->cont > 0))
  272. {
  273. /* If buffer isn't full and we can retry then
  274. * restart to read in more data.
  275. */
  276. continue;
  277. }
  278. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
  279. {
  280. int z,jj;
  281. #if 0
  282. jj=(i>>2)<<2;
  283. #else
  284. jj = i & ~3; /* process per 4 */
  285. #endif
  286. z=EVP_DecodeBlock((unsigned char *)ctx->buf,
  287. (unsigned char *)ctx->tmp,jj);
  288. if (jj > 2)
  289. {
  290. if (ctx->tmp[jj-1] == '=')
  291. {
  292. z--;
  293. if (ctx->tmp[jj-2] == '=')
  294. z--;
  295. }
  296. }
  297. /* z is now number of output bytes and jj is the
  298. * number consumed */
  299. if (jj != i)
  300. {
  301. memmove(ctx->tmp, &ctx->tmp[jj], i-jj);
  302. ctx->tmp_len=i-jj;
  303. }
  304. ctx->buf_len=0;
  305. if (z > 0)
  306. {
  307. ctx->buf_len=z;
  308. }
  309. i=z;
  310. }
  311. else
  312. {
  313. i=EVP_DecodeUpdate(&(ctx->base64),
  314. (unsigned char *)ctx->buf,&ctx->buf_len,
  315. (unsigned char *)ctx->tmp,i);
  316. ctx->tmp_len = 0;
  317. }
  318. ctx->buf_off=0;
  319. if (i < 0)
  320. {
  321. ret_code=0;
  322. ctx->buf_len=0;
  323. break;
  324. }
  325. if (ctx->buf_len <= outl)
  326. i=ctx->buf_len;
  327. else
  328. i=outl;
  329. memcpy(out,ctx->buf,i);
  330. ret+=i;
  331. ctx->buf_off=i;
  332. if (ctx->buf_off == ctx->buf_len)
  333. {
  334. ctx->buf_len=0;
  335. ctx->buf_off=0;
  336. }
  337. outl-=i;
  338. out+=i;
  339. }
  340. /* BIO_clear_retry_flags(b); */
  341. BIO_copy_next_retry(b);
  342. return((ret == 0)?ret_code:ret);
  343. }
  344. static int b64_write(BIO *b, const char *in, int inl)
  345. {
  346. int ret=0;
  347. int n;
  348. int i;
  349. BIO_B64_CTX *ctx;
  350. ctx=(BIO_B64_CTX *)b->ptr;
  351. BIO_clear_retry_flags(b);
  352. if (ctx->encode != B64_ENCODE)
  353. {
  354. ctx->encode=B64_ENCODE;
  355. ctx->buf_len=0;
  356. ctx->buf_off=0;
  357. ctx->tmp_len=0;
  358. EVP_EncodeInit(&(ctx->base64));
  359. }
  360. OPENSSL_assert(ctx->buf_off < (int)sizeof(ctx->buf));
  361. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  362. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  363. n=ctx->buf_len-ctx->buf_off;
  364. while (n > 0)
  365. {
  366. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  367. if (i <= 0)
  368. {
  369. BIO_copy_next_retry(b);
  370. return(i);
  371. }
  372. OPENSSL_assert(i <= n);
  373. ctx->buf_off+=i;
  374. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  375. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  376. n-=i;
  377. }
  378. /* at this point all pending data has been written */
  379. ctx->buf_off=0;
  380. ctx->buf_len=0;
  381. if ((in == NULL) || (inl <= 0)) return(0);
  382. while (inl > 0)
  383. {
  384. n=(inl > B64_BLOCK_SIZE)?B64_BLOCK_SIZE:inl;
  385. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
  386. {
  387. if (ctx->tmp_len > 0)
  388. {
  389. OPENSSL_assert(ctx->tmp_len <= 3);
  390. n=3-ctx->tmp_len;
  391. /* There's a theoretical possibility for this */
  392. if (n > inl)
  393. n=inl;
  394. memcpy(&(ctx->tmp[ctx->tmp_len]),in,n);
  395. ctx->tmp_len+=n;
  396. ret += n;
  397. if (ctx->tmp_len < 3)
  398. break;
  399. ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(unsigned char *)ctx->tmp,ctx->tmp_len);
  400. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  401. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  402. /* Since we're now done using the temporary
  403. buffer, the length should be 0'd */
  404. ctx->tmp_len=0;
  405. }
  406. else
  407. {
  408. if (n < 3)
  409. {
  410. memcpy(ctx->tmp,in,n);
  411. ctx->tmp_len=n;
  412. ret += n;
  413. break;
  414. }
  415. n-=n%3;
  416. ctx->buf_len=EVP_EncodeBlock((unsigned char *)ctx->buf,(const unsigned char *)in,n);
  417. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  418. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  419. ret += n;
  420. }
  421. }
  422. else
  423. {
  424. EVP_EncodeUpdate(&(ctx->base64),
  425. (unsigned char *)ctx->buf,&ctx->buf_len,
  426. (unsigned char *)in,n);
  427. OPENSSL_assert(ctx->buf_len <= (int)sizeof(ctx->buf));
  428. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  429. ret += n;
  430. }
  431. inl-=n;
  432. in+=n;
  433. ctx->buf_off=0;
  434. n=ctx->buf_len;
  435. while (n > 0)
  436. {
  437. i=BIO_write(b->next_bio,&(ctx->buf[ctx->buf_off]),n);
  438. if (i <= 0)
  439. {
  440. BIO_copy_next_retry(b);
  441. return((ret == 0)?i:ret);
  442. }
  443. OPENSSL_assert(i <= n);
  444. n-=i;
  445. ctx->buf_off+=i;
  446. OPENSSL_assert(ctx->buf_off <= (int)sizeof(ctx->buf));
  447. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  448. }
  449. ctx->buf_len=0;
  450. ctx->buf_off=0;
  451. }
  452. return(ret);
  453. }
  454. static long b64_ctrl(BIO *b, int cmd, long num, void *ptr)
  455. {
  456. BIO_B64_CTX *ctx;
  457. long ret=1;
  458. int i;
  459. ctx=(BIO_B64_CTX *)b->ptr;
  460. switch (cmd)
  461. {
  462. case BIO_CTRL_RESET:
  463. ctx->cont=1;
  464. ctx->start=1;
  465. ctx->encode=B64_NONE;
  466. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  467. break;
  468. case BIO_CTRL_EOF: /* More to read */
  469. if (ctx->cont <= 0)
  470. ret=1;
  471. else
  472. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  473. break;
  474. case BIO_CTRL_WPENDING: /* More to write in buffer */
  475. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  476. ret=ctx->buf_len-ctx->buf_off;
  477. if ((ret == 0) && (ctx->encode != B64_NONE)
  478. && (ctx->base64.num != 0))
  479. ret=1;
  480. else if (ret <= 0)
  481. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  482. break;
  483. case BIO_CTRL_PENDING: /* More to read in buffer */
  484. OPENSSL_assert(ctx->buf_len >= ctx->buf_off);
  485. ret=ctx->buf_len-ctx->buf_off;
  486. if (ret <= 0)
  487. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  488. break;
  489. case BIO_CTRL_FLUSH:
  490. /* do a final write */
  491. again:
  492. while (ctx->buf_len != ctx->buf_off)
  493. {
  494. i=b64_write(b,NULL,0);
  495. if (i < 0)
  496. return i;
  497. }
  498. if (BIO_get_flags(b) & BIO_FLAGS_BASE64_NO_NL)
  499. {
  500. if (ctx->tmp_len != 0)
  501. {
  502. ctx->buf_len=EVP_EncodeBlock(
  503. (unsigned char *)ctx->buf,
  504. (unsigned char *)ctx->tmp,
  505. ctx->tmp_len);
  506. ctx->buf_off=0;
  507. ctx->tmp_len=0;
  508. goto again;
  509. }
  510. }
  511. else if (ctx->encode != B64_NONE && ctx->base64.num != 0)
  512. {
  513. ctx->buf_off=0;
  514. EVP_EncodeFinal(&(ctx->base64),
  515. (unsigned char *)ctx->buf,
  516. &(ctx->buf_len));
  517. /* push out the bytes */
  518. goto again;
  519. }
  520. /* Finally flush the underlying BIO */
  521. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  522. break;
  523. case BIO_C_DO_STATE_MACHINE:
  524. BIO_clear_retry_flags(b);
  525. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  526. BIO_copy_next_retry(b);
  527. break;
  528. case BIO_CTRL_DUP:
  529. break;
  530. case BIO_CTRL_INFO:
  531. case BIO_CTRL_GET:
  532. case BIO_CTRL_SET:
  533. default:
  534. ret=BIO_ctrl(b->next_bio,cmd,num,ptr);
  535. break;
  536. }
  537. return(ret);
  538. }
  539. static long b64_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
  540. {
  541. long ret=1;
  542. if (b->next_bio == NULL) return(0);
  543. switch (cmd)
  544. {
  545. default:
  546. ret=BIO_callback_ctrl(b->next_bio,cmd,fp);
  547. break;
  548. }
  549. return(ret);
  550. }
  551. static int b64_puts(BIO *b, const char *str)
  552. {
  553. return b64_write(b,str,strlen(str));
  554. }