b64.c 8.1 KB

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