xref: /minix3/crypto/external/bsd/openssl/dist/demos/b64.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* demos/b64.c */
2ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3ebfedea0SLionel Sambuc  * All rights reserved.
4ebfedea0SLionel Sambuc  *
5ebfedea0SLionel Sambuc  * This package is an SSL implementation written
6ebfedea0SLionel Sambuc  * by Eric Young (eay@cryptsoft.com).
7ebfedea0SLionel Sambuc  * The implementation was written so as to conform with Netscapes SSL.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * This library is free for commercial and non-commercial use as long as
10ebfedea0SLionel Sambuc  * the following conditions are aheared to.  The following conditions
11ebfedea0SLionel Sambuc  * apply to all code found in this distribution, be it the RC4, RSA,
12ebfedea0SLionel Sambuc  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13ebfedea0SLionel Sambuc  * included with this distribution is covered by the same copyright terms
14ebfedea0SLionel Sambuc  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * Copyright remains Eric Young's, and as such any Copyright notices in
17ebfedea0SLionel Sambuc  * the code are not to be removed.
18ebfedea0SLionel Sambuc  * If this package is used in a product, Eric Young should be given attribution
19ebfedea0SLionel Sambuc  * as the author of the parts of the library used.
20ebfedea0SLionel Sambuc  * This can be in the form of a textual message at program startup or
21ebfedea0SLionel Sambuc  * in documentation (online or textual) provided with the package.
22ebfedea0SLionel Sambuc  *
23ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
24ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
25ebfedea0SLionel Sambuc  * are met:
26ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the copyright
27ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
28ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
29ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
30ebfedea0SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
31ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this software
32ebfedea0SLionel Sambuc  *    must display the following acknowledgement:
33ebfedea0SLionel Sambuc  *    "This product includes cryptographic software written by
34ebfedea0SLionel Sambuc  *     Eric Young (eay@cryptsoft.com)"
35ebfedea0SLionel Sambuc  *    The word 'cryptographic' can be left out if the rouines from the library
36ebfedea0SLionel Sambuc  *    being used are not cryptographic related :-).
37ebfedea0SLionel Sambuc  * 4. If you include any Windows specific code (or a derivative thereof) from
38ebfedea0SLionel Sambuc  *    the apps directory (application code) you must include an acknowledgement:
39ebfedea0SLionel Sambuc  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42ebfedea0SLionel Sambuc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44ebfedea0SLionel Sambuc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45ebfedea0SLionel Sambuc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46ebfedea0SLionel Sambuc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47ebfedea0SLionel Sambuc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49ebfedea0SLionel Sambuc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50ebfedea0SLionel Sambuc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51ebfedea0SLionel Sambuc  * SUCH DAMAGE.
52ebfedea0SLionel Sambuc  *
53ebfedea0SLionel Sambuc  * The licence and distribution terms for any publically available version or
54ebfedea0SLionel Sambuc  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55ebfedea0SLionel Sambuc  * copied and put under another distribution licence
56ebfedea0SLionel Sambuc  * [including the GNU Public Licence.]
57ebfedea0SLionel Sambuc  */
58ebfedea0SLionel Sambuc 
59ebfedea0SLionel Sambuc #include <stdio.h>
60ebfedea0SLionel Sambuc #include <stdlib.h>
61ebfedea0SLionel Sambuc #include <string.h>
62ebfedea0SLionel Sambuc #include "../apps/apps.h"
63ebfedea0SLionel Sambuc #include <openssl/buffer.h>
64ebfedea0SLionel Sambuc #include <openssl/err.h>
65ebfedea0SLionel Sambuc #include <openssl/evp.h>
66ebfedea0SLionel Sambuc #include <openssl/objects.h>
67ebfedea0SLionel Sambuc #include <openssl/x509.h>
68ebfedea0SLionel Sambuc #include <openssl/pem.h>
69ebfedea0SLionel Sambuc 
70ebfedea0SLionel Sambuc #undef SIZE
71ebfedea0SLionel Sambuc #undef BSIZE
72ebfedea0SLionel Sambuc #undef PROG
73ebfedea0SLionel Sambuc 
74ebfedea0SLionel Sambuc #define SIZE    (512)
75ebfedea0SLionel Sambuc #define BSIZE   (8*1024)
76ebfedea0SLionel Sambuc #define PROG    enc_main
77ebfedea0SLionel Sambuc 
main(argc,argv)78ebfedea0SLionel Sambuc int main(argc, argv)
79ebfedea0SLionel Sambuc int argc;
80ebfedea0SLionel Sambuc char **argv;
81ebfedea0SLionel Sambuc {
82ebfedea0SLionel Sambuc     char *strbuf = NULL;
83ebfedea0SLionel Sambuc     unsigned char *buff = NULL, *bufsize = NULL;
84ebfedea0SLionel Sambuc     int bsize = BSIZE, verbose = 0;
85ebfedea0SLionel Sambuc     int ret = 1, inl;
86ebfedea0SLionel Sambuc     char *str = NULL;
87ebfedea0SLionel Sambuc     char *hkey = NULL, *hiv = NULL;
88ebfedea0SLionel Sambuc     int enc = 1, printkey = 0, i, base64 = 0;
89ebfedea0SLionel Sambuc     int debug = 0;
90ebfedea0SLionel Sambuc     EVP_CIPHER *cipher = NULL, *c;
91ebfedea0SLionel Sambuc     char *inf = NULL, *outf = NULL;
92*0a6a1f1dSLionel Sambuc     BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
93*0a6a1f1dSLionel Sambuc         NULL, *wbio = NULL;
94ebfedea0SLionel Sambuc #define PROG_NAME_SIZE  39
95ebfedea0SLionel Sambuc 
96ebfedea0SLionel Sambuc     apps_startup();
97ebfedea0SLionel Sambuc 
98ebfedea0SLionel Sambuc     if (bio_err == NULL)
99ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
100ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE);
101ebfedea0SLionel Sambuc 
102ebfedea0SLionel Sambuc     base64 = 1;
103ebfedea0SLionel Sambuc 
104ebfedea0SLionel Sambuc     argc--;
105ebfedea0SLionel Sambuc     argv++;
106*0a6a1f1dSLionel Sambuc     while (argc >= 1) {
107ebfedea0SLionel Sambuc         if (strcmp(*argv, "-e") == 0)
108ebfedea0SLionel Sambuc             enc = 1;
109*0a6a1f1dSLionel Sambuc         if (strcmp(*argv, "-in") == 0) {
110*0a6a1f1dSLionel Sambuc             if (--argc < 1)
111*0a6a1f1dSLionel Sambuc                 goto bad;
112ebfedea0SLionel Sambuc             inf = *(++argv);
113*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-out") == 0) {
114*0a6a1f1dSLionel Sambuc             if (--argc < 1)
115*0a6a1f1dSLionel Sambuc                 goto bad;
116ebfedea0SLionel Sambuc             outf = *(++argv);
117*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-d") == 0)
118ebfedea0SLionel Sambuc             enc = 0;
119ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-v") == 0)
120ebfedea0SLionel Sambuc             verbose = 1;
121ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-debug") == 0)
122ebfedea0SLionel Sambuc             debug = 1;
123*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-bufsize") == 0) {
124*0a6a1f1dSLionel Sambuc             if (--argc < 1)
125*0a6a1f1dSLionel Sambuc                 goto bad;
126ebfedea0SLionel Sambuc             bufsize = (unsigned char *)*(++argv);
127*0a6a1f1dSLionel Sambuc         } else {
128ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unknown option '%s'\n", *argv);
129ebfedea0SLionel Sambuc  bad:
130ebfedea0SLionel Sambuc             BIO_printf(bio_err, "options are\n");
131ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s input file\n", "-in <file>");
132ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s output file\n", "-out <file>");
133ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s encode\n", "-e");
134ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s decode\n", "-d");
135ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s buffer size\n", "-bufsize <n>");
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc             goto end;
138ebfedea0SLionel Sambuc         }
139ebfedea0SLionel Sambuc         argc--;
140ebfedea0SLionel Sambuc         argv++;
141ebfedea0SLionel Sambuc     }
142ebfedea0SLionel Sambuc 
143*0a6a1f1dSLionel Sambuc     if (bufsize != NULL) {
144ebfedea0SLionel Sambuc         int i;
145ebfedea0SLionel Sambuc         unsigned long n;
146ebfedea0SLionel Sambuc 
147*0a6a1f1dSLionel Sambuc         for (n = 0; *bufsize; bufsize++) {
148ebfedea0SLionel Sambuc             i = *bufsize;
149ebfedea0SLionel Sambuc             if ((i <= '9') && (i >= '0'))
150ebfedea0SLionel Sambuc                 n = n * 10 + i - '0';
151*0a6a1f1dSLionel Sambuc             else if (i == 'k') {
152ebfedea0SLionel Sambuc                 n *= 1024;
153ebfedea0SLionel Sambuc                 bufsize++;
154ebfedea0SLionel Sambuc                 break;
155ebfedea0SLionel Sambuc             }
156ebfedea0SLionel Sambuc         }
157*0a6a1f1dSLionel Sambuc         if (*bufsize != '\0') {
158ebfedea0SLionel Sambuc             BIO_printf(bio_err, "invalid 'bufsize' specified.\n");
159ebfedea0SLionel Sambuc             goto end;
160ebfedea0SLionel Sambuc         }
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc         /* It must be large enough for a base64 encoded line */
163*0a6a1f1dSLionel Sambuc         if (n < 80)
164*0a6a1f1dSLionel Sambuc             n = 80;
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc         bsize = (int)n;
167*0a6a1f1dSLionel Sambuc         if (verbose)
168*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "bufsize=%d\n", bsize);
169ebfedea0SLionel Sambuc     }
170ebfedea0SLionel Sambuc 
171ebfedea0SLionel Sambuc     strbuf = OPENSSL_malloc(SIZE);
172ebfedea0SLionel Sambuc     buff = (unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
173*0a6a1f1dSLionel Sambuc     if ((buff == NULL) || (strbuf == NULL)) {
174ebfedea0SLionel Sambuc         BIO_printf(bio_err, "OPENSSL_malloc failure\n");
175ebfedea0SLionel Sambuc         goto end;
176ebfedea0SLionel Sambuc     }
177ebfedea0SLionel Sambuc 
178ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file());
179ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
180*0a6a1f1dSLionel Sambuc     if ((in == NULL) || (out == NULL)) {
181ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
182ebfedea0SLionel Sambuc         goto end;
183ebfedea0SLionel Sambuc     }
184*0a6a1f1dSLionel Sambuc     if (debug) {
185ebfedea0SLionel Sambuc         BIO_set_callback(in, BIO_debug_callback);
186ebfedea0SLionel Sambuc         BIO_set_callback(out, BIO_debug_callback);
187ebfedea0SLionel Sambuc         BIO_set_callback_arg(in, bio_err);
188ebfedea0SLionel Sambuc         BIO_set_callback_arg(out, bio_err);
189ebfedea0SLionel Sambuc     }
190ebfedea0SLionel Sambuc 
191ebfedea0SLionel Sambuc     if (inf == NULL)
192ebfedea0SLionel Sambuc         BIO_set_fp(in, stdin, BIO_NOCLOSE);
193*0a6a1f1dSLionel Sambuc     else {
194*0a6a1f1dSLionel Sambuc         if (BIO_read_filename(in, inf) <= 0) {
195ebfedea0SLionel Sambuc             perror(inf);
196ebfedea0SLionel Sambuc             goto end;
197ebfedea0SLionel Sambuc         }
198ebfedea0SLionel Sambuc     }
199ebfedea0SLionel Sambuc 
200ebfedea0SLionel Sambuc     if (outf == NULL)
201ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
202*0a6a1f1dSLionel Sambuc     else {
203*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outf) <= 0) {
204ebfedea0SLionel Sambuc             perror(outf);
205ebfedea0SLionel Sambuc             goto end;
206ebfedea0SLionel Sambuc         }
207ebfedea0SLionel Sambuc     }
208ebfedea0SLionel Sambuc 
209ebfedea0SLionel Sambuc     rbio = in;
210ebfedea0SLionel Sambuc     wbio = out;
211ebfedea0SLionel Sambuc 
212*0a6a1f1dSLionel Sambuc     if (base64) {
213ebfedea0SLionel Sambuc         if ((b64 = BIO_new(BIO_f_base64())) == NULL)
214ebfedea0SLionel Sambuc             goto end;
215*0a6a1f1dSLionel Sambuc         if (debug) {
216ebfedea0SLionel Sambuc             BIO_set_callback(b64, BIO_debug_callback);
217ebfedea0SLionel Sambuc             BIO_set_callback_arg(b64, bio_err);
218ebfedea0SLionel Sambuc         }
219ebfedea0SLionel Sambuc         if (enc)
220ebfedea0SLionel Sambuc             wbio = BIO_push(b64, wbio);
221ebfedea0SLionel Sambuc         else
222ebfedea0SLionel Sambuc             rbio = BIO_push(b64, rbio);
223ebfedea0SLionel Sambuc     }
224ebfedea0SLionel Sambuc 
225*0a6a1f1dSLionel Sambuc     for (;;) {
226ebfedea0SLionel Sambuc         inl = BIO_read(rbio, (char *)buff, bsize);
227*0a6a1f1dSLionel Sambuc         if (inl <= 0)
228*0a6a1f1dSLionel Sambuc             break;
229*0a6a1f1dSLionel Sambuc         if (BIO_write(wbio, (char *)buff, inl) != inl) {
230ebfedea0SLionel Sambuc             BIO_printf(bio_err, "error writing output file\n");
231ebfedea0SLionel Sambuc             goto end;
232ebfedea0SLionel Sambuc         }
233ebfedea0SLionel Sambuc     }
234ebfedea0SLionel Sambuc     BIO_flush(wbio);
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     ret = 0;
237*0a6a1f1dSLionel Sambuc     if (verbose) {
238ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bytes read   :%8ld\n", BIO_number_read(in));
239ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bytes written:%8ld\n", BIO_number_written(out));
240ebfedea0SLionel Sambuc     }
241ebfedea0SLionel Sambuc  end:
242*0a6a1f1dSLionel Sambuc     if (strbuf != NULL)
243*0a6a1f1dSLionel Sambuc         OPENSSL_free(strbuf);
244*0a6a1f1dSLionel Sambuc     if (buff != NULL)
245*0a6a1f1dSLionel Sambuc         OPENSSL_free(buff);
246*0a6a1f1dSLionel Sambuc     if (in != NULL)
247*0a6a1f1dSLionel Sambuc         BIO_free(in);
248*0a6a1f1dSLionel Sambuc     if (out != NULL)
249*0a6a1f1dSLionel Sambuc         BIO_free(out);
250*0a6a1f1dSLionel Sambuc     if (benc != NULL)
251*0a6a1f1dSLionel Sambuc         BIO_free(benc);
252*0a6a1f1dSLionel Sambuc     if (b64 != NULL)
253*0a6a1f1dSLionel Sambuc         BIO_free(b64);
254ebfedea0SLionel Sambuc     EXIT(ret);
255ebfedea0SLionel Sambuc }
256