b64.c 7.1 KB

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