2
0

b64.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /* demos/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 <stdlib.h>
  60. #include <string.h>
  61. #include "../apps/apps.h"
  62. #include <openssl/buffer.h>
  63. #include <openssl/err.h>
  64. #include <openssl/evp.h>
  65. #include <openssl/objects.h>
  66. #include <openssl/x509.h>
  67. #include <openssl/pem.h>
  68. #undef SIZE
  69. #undef BSIZE
  70. #undef PROG
  71. #define SIZE (512)
  72. #define BSIZE (8*1024)
  73. #define PROG enc_main
  74. int main(argc,argv)
  75. int argc;
  76. char **argv;
  77. {
  78. char *strbuf=NULL;
  79. unsigned char *buff=NULL,*bufsize=NULL;
  80. int bsize=BSIZE,verbose=0;
  81. int ret=1,inl;
  82. unsigned char key[24],iv[MD5_DIGEST_LENGTH];
  83. char *str=NULL;
  84. char *hkey=NULL,*hiv=NULL;
  85. int enc=1,printkey=0,i,base64=0;
  86. int debug=0;
  87. EVP_CIPHER *cipher=NULL,*c;
  88. char *inf=NULL,*outf=NULL;
  89. BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL;
  90. #define PROG_NAME_SIZE 16
  91. char pname[PROG_NAME_SIZE];
  92. apps_startup();
  93. if (bio_err == NULL)
  94. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  95. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE);
  96. base64=1;
  97. argc--;
  98. argv++;
  99. while (argc >= 1)
  100. {
  101. if (strcmp(*argv,"-e") == 0)
  102. enc=1;
  103. if (strcmp(*argv,"-in") == 0)
  104. {
  105. if (--argc < 1) goto bad;
  106. inf= *(++argv);
  107. }
  108. else if (strcmp(*argv,"-out") == 0)
  109. {
  110. if (--argc < 1) goto bad;
  111. outf= *(++argv);
  112. }
  113. else if (strcmp(*argv,"-d") == 0)
  114. enc=0;
  115. else if (strcmp(*argv,"-v") == 0)
  116. verbose=1;
  117. else if (strcmp(*argv,"-debug") == 0)
  118. debug=1;
  119. else if (strcmp(*argv,"-bufsize") == 0)
  120. {
  121. if (--argc < 1) goto bad;
  122. bufsize=(unsigned char *)*(++argv);
  123. }
  124. else
  125. {
  126. BIO_printf(bio_err,"unknown option '%s'\n",*argv);
  127. bad:
  128. BIO_printf(bio_err,"options are\n");
  129. BIO_printf(bio_err,"%-14s input file\n","-in <file>");
  130. BIO_printf(bio_err,"%-14s output file\n","-out <file>");
  131. BIO_printf(bio_err,"%-14s encode\n","-e");
  132. BIO_printf(bio_err,"%-14s decode\n","-d");
  133. BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>");
  134. goto end;
  135. }
  136. argc--;
  137. argv++;
  138. }
  139. if (bufsize != NULL)
  140. {
  141. int i;
  142. unsigned long n;
  143. for (n=0; *bufsize; bufsize++)
  144. {
  145. i= *bufsize;
  146. if ((i <= '9') && (i >= '0'))
  147. n=n*10+i-'0';
  148. else if (i == 'k')
  149. {
  150. n*=1024;
  151. bufsize++;
  152. break;
  153. }
  154. }
  155. if (*bufsize != '\0')
  156. {
  157. BIO_printf(bio_err,"invalid 'bufsize' specified.\n");
  158. goto end;
  159. }
  160. /* It must be large enough for a base64 encoded line */
  161. if (n < 80) n=80;
  162. bsize=(int)n;
  163. if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize);
  164. }
  165. strbuf=OPENSSL_malloc(SIZE);
  166. buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
  167. if ((buff == NULL) || (strbuf == NULL))
  168. {
  169. BIO_printf(bio_err,"OPENSSL_malloc failure\n");
  170. goto end;
  171. }
  172. in=BIO_new(BIO_s_file());
  173. out=BIO_new(BIO_s_file());
  174. if ((in == NULL) || (out == NULL))
  175. {
  176. ERR_print_errors(bio_err);
  177. goto end;
  178. }
  179. if (debug)
  180. {
  181. BIO_set_callback(in,BIO_debug_callback);
  182. BIO_set_callback(out,BIO_debug_callback);
  183. BIO_set_callback_arg(in,bio_err);
  184. BIO_set_callback_arg(out,bio_err);
  185. }
  186. if (inf == NULL)
  187. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  188. else
  189. {
  190. if (BIO_read_filename(in,inf) <= 0)
  191. {
  192. perror(inf);
  193. goto end;
  194. }
  195. }
  196. if (outf == NULL)
  197. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  198. else
  199. {
  200. if (BIO_write_filename(out,outf) <= 0)
  201. {
  202. perror(outf);
  203. goto end;
  204. }
  205. }
  206. rbio=in;
  207. wbio=out;
  208. if (base64)
  209. {
  210. if ((b64=BIO_new(BIO_f_base64())) == NULL)
  211. goto end;
  212. if (debug)
  213. {
  214. BIO_set_callback(b64,BIO_debug_callback);
  215. BIO_set_callback_arg(b64,bio_err);
  216. }
  217. if (enc)
  218. wbio=BIO_push(b64,wbio);
  219. else
  220. rbio=BIO_push(b64,rbio);
  221. }
  222. for (;;)
  223. {
  224. inl=BIO_read(rbio,(char *)buff,bsize);
  225. if (inl <= 0) break;
  226. if (BIO_write(wbio,(char *)buff,inl) != inl)
  227. {
  228. BIO_printf(bio_err,"error writing output file\n");
  229. goto end;
  230. }
  231. }
  232. BIO_flush(wbio);
  233. ret=0;
  234. if (verbose)
  235. {
  236. BIO_printf(bio_err,"bytes read :%8ld\n",BIO_number_read(in));
  237. BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out));
  238. }
  239. end:
  240. if (strbuf != NULL) OPENSSL_free(strbuf);
  241. if (buff != NULL) OPENSSL_free(buff);
  242. if (in != NULL) BIO_free(in);
  243. if (out != NULL) BIO_free(out);
  244. if (benc != NULL) BIO_free(benc);
  245. if (b64 != NULL) BIO_free(b64);
  246. EXIT(ret);
  247. }