xref: /minix3/crypto/external/bsd/openssl/dist/apps/enc.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/enc.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.h"
63ebfedea0SLionel Sambuc #include <openssl/bio.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/rand.h>
69ebfedea0SLionel Sambuc #include <openssl/pem.h>
70*0a6a1f1dSLionel Sambuc #ifndef OPENSSL_NO_COMP
71ebfedea0SLionel Sambuc # include <openssl/comp.h>
72*0a6a1f1dSLionel Sambuc #endif
73ebfedea0SLionel Sambuc #include <ctype.h>
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc int set_hex(char *in, unsigned char *out, int size);
76ebfedea0SLionel Sambuc #undef SIZE
77ebfedea0SLionel Sambuc #undef BSIZE
78ebfedea0SLionel Sambuc #undef PROG
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc #define SIZE    (512)
81ebfedea0SLionel Sambuc #define BSIZE   (8*1024)
82ebfedea0SLionel Sambuc #define PROG    enc_main
83ebfedea0SLionel Sambuc 
show_ciphers(const OBJ_NAME * name,void * bio_)84ebfedea0SLionel Sambuc static void show_ciphers(const OBJ_NAME *name, void *bio_)
85ebfedea0SLionel Sambuc {
86ebfedea0SLionel Sambuc     BIO *bio = bio_;
87ebfedea0SLionel Sambuc     static int n;
88ebfedea0SLionel Sambuc 
89ebfedea0SLionel Sambuc     if (!islower((unsigned char)*name->name))
90ebfedea0SLionel Sambuc         return;
91ebfedea0SLionel Sambuc 
92ebfedea0SLionel Sambuc     BIO_printf(bio, "-%-25s", name->name);
93*0a6a1f1dSLionel Sambuc     if (++n == 3) {
94ebfedea0SLionel Sambuc         BIO_printf(bio, "\n");
95ebfedea0SLionel Sambuc         n = 0;
96*0a6a1f1dSLionel Sambuc     } else
97ebfedea0SLionel Sambuc         BIO_printf(bio, " ");
98ebfedea0SLionel Sambuc }
99ebfedea0SLionel Sambuc 
100ebfedea0SLionel Sambuc int MAIN(int, char **);
101ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)102ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc     static const char magic[] = "Salted__";
105ebfedea0SLionel Sambuc     char mbuf[sizeof magic - 1];
106ebfedea0SLionel Sambuc     char *strbuf = NULL;
107ebfedea0SLionel Sambuc     unsigned char *buff = NULL, *bufsize = NULL;
108ebfedea0SLionel Sambuc     int bsize = BSIZE, verbose = 0;
109ebfedea0SLionel Sambuc     int ret = 1, inl;
110ebfedea0SLionel Sambuc     int nopad = 0;
111ebfedea0SLionel Sambuc     unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
112ebfedea0SLionel Sambuc     unsigned char salt[PKCS5_SALT_LEN];
113ebfedea0SLionel Sambuc     char *str = NULL, *passarg = NULL, *pass = NULL;
114ebfedea0SLionel Sambuc     char *hkey = NULL, *hiv = NULL, *hsalt = NULL;
115ebfedea0SLionel Sambuc     char *md = NULL;
116ebfedea0SLionel Sambuc     int enc = 1, printkey = 0, i, base64 = 0;
117ebfedea0SLionel Sambuc #ifdef ZLIB
118ebfedea0SLionel Sambuc     int do_zlib = 0;
119ebfedea0SLionel Sambuc     BIO *bzl = NULL;
120ebfedea0SLionel Sambuc #endif
121ebfedea0SLionel Sambuc     int debug = 0, olb64 = 0, nosalt = 0;
122ebfedea0SLionel Sambuc     const EVP_CIPHER *cipher = NULL, *c;
123ebfedea0SLionel Sambuc     EVP_CIPHER_CTX *ctx = NULL;
124ebfedea0SLionel Sambuc     char *inf = NULL, *outf = NULL;
125*0a6a1f1dSLionel Sambuc     BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
126*0a6a1f1dSLionel Sambuc         NULL, *wbio = NULL;
127ebfedea0SLionel Sambuc #define PROG_NAME_SIZE  39
128ebfedea0SLionel Sambuc     char pname[PROG_NAME_SIZE + 1];
129ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
130ebfedea0SLionel Sambuc     char *engine = NULL;
131ebfedea0SLionel Sambuc #endif
132ebfedea0SLionel Sambuc     const EVP_MD *dgst = NULL;
133ebfedea0SLionel Sambuc     int non_fips_allow = 0;
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     apps_startup();
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     if (bio_err == NULL)
138ebfedea0SLionel Sambuc         if ((bio_err = BIO_new(BIO_s_file())) != NULL)
139ebfedea0SLionel Sambuc             BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
140ebfedea0SLionel Sambuc 
141ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
142ebfedea0SLionel Sambuc         goto end;
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc     /* first check the program name */
145ebfedea0SLionel Sambuc     program_name(argv[0], pname, sizeof pname);
146ebfedea0SLionel Sambuc     if (strcmp(pname, "base64") == 0)
147ebfedea0SLionel Sambuc         base64 = 1;
148ebfedea0SLionel Sambuc #ifdef ZLIB
149ebfedea0SLionel Sambuc     if (strcmp(pname, "zlib") == 0)
150ebfedea0SLionel Sambuc         do_zlib = 1;
151ebfedea0SLionel Sambuc #endif
152ebfedea0SLionel Sambuc 
153ebfedea0SLionel Sambuc     cipher = EVP_get_cipherbyname(pname);
154ebfedea0SLionel Sambuc #ifdef ZLIB
155ebfedea0SLionel Sambuc     if (!do_zlib && !base64 && (cipher == NULL)
156ebfedea0SLionel Sambuc         && (strcmp(pname, "enc") != 0))
157ebfedea0SLionel Sambuc #else
158ebfedea0SLionel Sambuc     if (!base64 && (cipher == NULL) && (strcmp(pname, "enc") != 0))
159ebfedea0SLionel Sambuc #endif
160ebfedea0SLionel Sambuc     {
161ebfedea0SLionel Sambuc         BIO_printf(bio_err, "%s is an unknown cipher\n", pname);
162ebfedea0SLionel Sambuc         goto bad;
163ebfedea0SLionel Sambuc     }
164ebfedea0SLionel Sambuc 
165ebfedea0SLionel Sambuc     argc--;
166ebfedea0SLionel Sambuc     argv++;
167*0a6a1f1dSLionel Sambuc     while (argc >= 1) {
168ebfedea0SLionel Sambuc         if (strcmp(*argv, "-e") == 0)
169ebfedea0SLionel Sambuc             enc = 1;
170*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-in") == 0) {
171*0a6a1f1dSLionel Sambuc             if (--argc < 1)
172*0a6a1f1dSLionel Sambuc                 goto bad;
173ebfedea0SLionel Sambuc             inf = *(++argv);
174*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-out") == 0) {
175*0a6a1f1dSLionel Sambuc             if (--argc < 1)
176*0a6a1f1dSLionel Sambuc                 goto bad;
177ebfedea0SLionel Sambuc             outf = *(++argv);
178*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-pass") == 0) {
179*0a6a1f1dSLionel Sambuc             if (--argc < 1)
180*0a6a1f1dSLionel Sambuc                 goto bad;
181ebfedea0SLionel Sambuc             passarg = *(++argv);
182ebfedea0SLionel Sambuc         }
183ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
184*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-engine") == 0) {
185*0a6a1f1dSLionel Sambuc             if (--argc < 1)
186*0a6a1f1dSLionel Sambuc                 goto bad;
187ebfedea0SLionel Sambuc             engine = *(++argv);
188ebfedea0SLionel Sambuc         }
189ebfedea0SLionel Sambuc #endif
190ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-d") == 0)
191ebfedea0SLionel Sambuc             enc = 0;
192ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-p") == 0)
193ebfedea0SLionel Sambuc             printkey = 1;
194ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-v") == 0)
195ebfedea0SLionel Sambuc             verbose = 1;
196ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-nopad") == 0)
197ebfedea0SLionel Sambuc             nopad = 1;
198ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-salt") == 0)
199ebfedea0SLionel Sambuc             nosalt = 0;
200ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-nosalt") == 0)
201ebfedea0SLionel Sambuc             nosalt = 1;
202ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-debug") == 0)
203ebfedea0SLionel Sambuc             debug = 1;
204ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-P") == 0)
205ebfedea0SLionel Sambuc             printkey = 2;
206ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-A") == 0)
207ebfedea0SLionel Sambuc             olb64 = 1;
208ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-a") == 0)
209ebfedea0SLionel Sambuc             base64 = 1;
210ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-base64") == 0)
211ebfedea0SLionel Sambuc             base64 = 1;
212ebfedea0SLionel Sambuc #ifdef ZLIB
213ebfedea0SLionel Sambuc         else if (strcmp(*argv, "-z") == 0)
214ebfedea0SLionel Sambuc             do_zlib = 1;
215ebfedea0SLionel Sambuc #endif
216*0a6a1f1dSLionel Sambuc         else if (strcmp(*argv, "-bufsize") == 0) {
217*0a6a1f1dSLionel Sambuc             if (--argc < 1)
218*0a6a1f1dSLionel Sambuc                 goto bad;
219ebfedea0SLionel Sambuc             bufsize = (unsigned char *)*(++argv);
220*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-k") == 0) {
221*0a6a1f1dSLionel Sambuc             if (--argc < 1)
222*0a6a1f1dSLionel Sambuc                 goto bad;
223ebfedea0SLionel Sambuc             str = *(++argv);
224*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-kfile") == 0) {
225ebfedea0SLionel Sambuc             static char buf[128];
226ebfedea0SLionel Sambuc             FILE *infile;
227ebfedea0SLionel Sambuc             char *file;
228ebfedea0SLionel Sambuc 
229*0a6a1f1dSLionel Sambuc             if (--argc < 1)
230*0a6a1f1dSLionel Sambuc                 goto bad;
231ebfedea0SLionel Sambuc             file = *(++argv);
232ebfedea0SLionel Sambuc             infile = fopen(file, "r");
233*0a6a1f1dSLionel Sambuc             if (infile == NULL) {
234*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "unable to read key from '%s'\n", file);
235ebfedea0SLionel Sambuc                 goto bad;
236ebfedea0SLionel Sambuc             }
237ebfedea0SLionel Sambuc             buf[0] = '\0';
238*0a6a1f1dSLionel Sambuc             if (!fgets(buf, sizeof buf, infile)) {
239*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "unable to read key from '%s'\n", file);
240ebfedea0SLionel Sambuc                 goto bad;
241ebfedea0SLionel Sambuc             }
242ebfedea0SLionel Sambuc             fclose(infile);
243ebfedea0SLionel Sambuc             i = strlen(buf);
244*0a6a1f1dSLionel Sambuc             if ((i > 0) && ((buf[i - 1] == '\n') || (buf[i - 1] == '\r')))
245ebfedea0SLionel Sambuc                 buf[--i] = '\0';
246*0a6a1f1dSLionel Sambuc             if ((i > 0) && ((buf[i - 1] == '\n') || (buf[i - 1] == '\r')))
247ebfedea0SLionel Sambuc                 buf[--i] = '\0';
248*0a6a1f1dSLionel Sambuc             if (i < 1) {
249ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "zero length password\n");
250ebfedea0SLionel Sambuc                 goto bad;
251ebfedea0SLionel Sambuc             }
252ebfedea0SLionel Sambuc             str = buf;
253*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-K") == 0) {
254*0a6a1f1dSLionel Sambuc             if (--argc < 1)
255*0a6a1f1dSLionel Sambuc                 goto bad;
256ebfedea0SLionel Sambuc             hkey = *(++argv);
257*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-S") == 0) {
258*0a6a1f1dSLionel Sambuc             if (--argc < 1)
259*0a6a1f1dSLionel Sambuc                 goto bad;
260ebfedea0SLionel Sambuc             hsalt = *(++argv);
261*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-iv") == 0) {
262*0a6a1f1dSLionel Sambuc             if (--argc < 1)
263*0a6a1f1dSLionel Sambuc                 goto bad;
264ebfedea0SLionel Sambuc             hiv = *(++argv);
265*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-md") == 0) {
266*0a6a1f1dSLionel Sambuc             if (--argc < 1)
267*0a6a1f1dSLionel Sambuc                 goto bad;
268ebfedea0SLionel Sambuc             md = *(++argv);
269*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-non-fips-allow") == 0)
270ebfedea0SLionel Sambuc             non_fips_allow = 1;
271ebfedea0SLionel Sambuc         else if ((argv[0][0] == '-') &&
272*0a6a1f1dSLionel Sambuc                  ((c = EVP_get_cipherbyname(&(argv[0][1]))) != NULL)) {
273ebfedea0SLionel Sambuc             cipher = c;
274*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-none") == 0)
275ebfedea0SLionel Sambuc             cipher = NULL;
276*0a6a1f1dSLionel Sambuc         else {
277ebfedea0SLionel Sambuc             BIO_printf(bio_err, "unknown option '%s'\n", *argv);
278ebfedea0SLionel Sambuc  bad:
279ebfedea0SLionel Sambuc             BIO_printf(bio_err, "options are\n");
280ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s input file\n", "-in <file>");
281ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s output file\n", "-out <file>");
282ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s pass phrase source\n", "-pass <arg>");
283ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s encrypt\n", "-e");
284ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s decrypt\n", "-d");
285*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err,
286*0a6a1f1dSLionel Sambuc                        "%-14s base64 encode/decode, depending on encryption flag\n",
287*0a6a1f1dSLionel Sambuc                        "-a/-base64");
288*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "%-14s passphrase is the next argument\n",
289*0a6a1f1dSLionel Sambuc                        "-k");
290*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err,
291*0a6a1f1dSLionel Sambuc                        "%-14s passphrase is the first line of the file argument\n",
292*0a6a1f1dSLionel Sambuc                        "-kfile");
293*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err,
294*0a6a1f1dSLionel Sambuc                        "%-14s the next argument is the md to use to create a key\n",
295*0a6a1f1dSLionel Sambuc                        "-md");
296*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err,
297*0a6a1f1dSLionel Sambuc                        "%-14s   from a passphrase.  One of md2, md5, sha or sha1\n",
298*0a6a1f1dSLionel Sambuc                        "");
299*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "%-14s salt in hex is the next argument\n",
300*0a6a1f1dSLionel Sambuc                        "-S");
301*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "%-14s key/iv in hex is the next argument\n",
302*0a6a1f1dSLionel Sambuc                        "-K/-iv");
303*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "%-14s print the iv/key (then exit if -P)\n",
304*0a6a1f1dSLionel Sambuc                        "-[pP]");
305ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%-14s buffer size\n", "-bufsize <n>");
306*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "%-14s disable standard block padding\n",
307*0a6a1f1dSLionel Sambuc                        "-nopad");
308ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
309*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err,
310*0a6a1f1dSLionel Sambuc                        "%-14s use engine e, possibly a hardware device.\n",
311*0a6a1f1dSLionel Sambuc                        "-engine e");
312ebfedea0SLionel Sambuc #endif
313ebfedea0SLionel Sambuc 
314ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Cipher Types\n");
315ebfedea0SLionel Sambuc             OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
316*0a6a1f1dSLionel Sambuc                                    show_ciphers, bio_err);
317ebfedea0SLionel Sambuc             BIO_printf(bio_err, "\n");
318ebfedea0SLionel Sambuc 
319ebfedea0SLionel Sambuc             goto end;
320ebfedea0SLionel Sambuc         }
321ebfedea0SLionel Sambuc         argc--;
322ebfedea0SLionel Sambuc         argv++;
323ebfedea0SLionel Sambuc     }
324ebfedea0SLionel Sambuc 
325ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
326ebfedea0SLionel Sambuc     setup_engine(bio_err, engine, 0);
327ebfedea0SLionel Sambuc #endif
328ebfedea0SLionel Sambuc 
329*0a6a1f1dSLionel Sambuc     if (cipher && EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) {
330*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
331*0a6a1f1dSLionel Sambuc                    "AEAD ciphers not supported by the enc utility\n");
332*0a6a1f1dSLionel Sambuc         goto end;
333*0a6a1f1dSLionel Sambuc     }
334*0a6a1f1dSLionel Sambuc 
335*0a6a1f1dSLionel Sambuc     if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_XTS_MODE)) {
336*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err,
337*0a6a1f1dSLionel Sambuc                    "Ciphers in XTS mode are not supported by the enc utility\n");
338*0a6a1f1dSLionel Sambuc         goto end;
339*0a6a1f1dSLionel Sambuc     }
340*0a6a1f1dSLionel Sambuc 
341*0a6a1f1dSLionel Sambuc     if (md && (dgst = EVP_get_digestbyname(md)) == NULL) {
342ebfedea0SLionel Sambuc         BIO_printf(bio_err, "%s is an unsupported message digest type\n", md);
343ebfedea0SLionel Sambuc         goto end;
344ebfedea0SLionel Sambuc     }
345ebfedea0SLionel Sambuc 
346*0a6a1f1dSLionel Sambuc     if (dgst == NULL) {
347ebfedea0SLionel Sambuc         dgst = EVP_md5();
348ebfedea0SLionel Sambuc     }
349ebfedea0SLionel Sambuc 
350*0a6a1f1dSLionel Sambuc     if (bufsize != NULL) {
351ebfedea0SLionel Sambuc         unsigned long n;
352ebfedea0SLionel Sambuc 
353*0a6a1f1dSLionel Sambuc         for (n = 0; *bufsize; bufsize++) {
354ebfedea0SLionel Sambuc             i = *bufsize;
355ebfedea0SLionel Sambuc             if ((i <= '9') && (i >= '0'))
356ebfedea0SLionel Sambuc                 n = n * 10 + i - '0';
357*0a6a1f1dSLionel Sambuc             else if (i == 'k') {
358ebfedea0SLionel Sambuc                 n *= 1024;
359ebfedea0SLionel Sambuc                 bufsize++;
360ebfedea0SLionel Sambuc                 break;
361ebfedea0SLionel Sambuc             }
362ebfedea0SLionel Sambuc         }
363*0a6a1f1dSLionel Sambuc         if (*bufsize != '\0') {
364ebfedea0SLionel Sambuc             BIO_printf(bio_err, "invalid 'bufsize' specified.\n");
365ebfedea0SLionel Sambuc             goto end;
366ebfedea0SLionel Sambuc         }
367ebfedea0SLionel Sambuc 
368ebfedea0SLionel Sambuc         /* It must be large enough for a base64 encoded line */
369*0a6a1f1dSLionel Sambuc         if (base64 && n < 80)
370*0a6a1f1dSLionel Sambuc             n = 80;
371ebfedea0SLionel Sambuc 
372ebfedea0SLionel Sambuc         bsize = (int)n;
373*0a6a1f1dSLionel Sambuc         if (verbose)
374*0a6a1f1dSLionel Sambuc             BIO_printf(bio_err, "bufsize=%d\n", bsize);
375ebfedea0SLionel Sambuc     }
376ebfedea0SLionel Sambuc 
377ebfedea0SLionel Sambuc     strbuf = OPENSSL_malloc(SIZE);
378ebfedea0SLionel Sambuc     buff = (unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
379*0a6a1f1dSLionel Sambuc     if ((buff == NULL) || (strbuf == NULL)) {
380*0a6a1f1dSLionel Sambuc         BIO_printf(bio_err, "OPENSSL_malloc failure %ld\n",
381*0a6a1f1dSLionel Sambuc                    (long)EVP_ENCODE_LENGTH(bsize));
382ebfedea0SLionel Sambuc         goto end;
383ebfedea0SLionel Sambuc     }
384ebfedea0SLionel Sambuc 
385ebfedea0SLionel Sambuc     in = BIO_new(BIO_s_file());
386ebfedea0SLionel Sambuc     out = BIO_new(BIO_s_file());
387*0a6a1f1dSLionel Sambuc     if ((in == NULL) || (out == NULL)) {
388ebfedea0SLionel Sambuc         ERR_print_errors(bio_err);
389ebfedea0SLionel Sambuc         goto end;
390ebfedea0SLionel Sambuc     }
391*0a6a1f1dSLionel Sambuc     if (debug) {
392ebfedea0SLionel Sambuc         BIO_set_callback(in, BIO_debug_callback);
393ebfedea0SLionel Sambuc         BIO_set_callback(out, BIO_debug_callback);
394ebfedea0SLionel Sambuc         BIO_set_callback_arg(in, (char *)bio_err);
395ebfedea0SLionel Sambuc         BIO_set_callback_arg(out, (char *)bio_err);
396ebfedea0SLionel Sambuc     }
397ebfedea0SLionel Sambuc 
398*0a6a1f1dSLionel Sambuc     if (inf == NULL) {
399ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SETVBUF_IONBF
400ebfedea0SLionel Sambuc         if (bufsize != NULL)
401ebfedea0SLionel Sambuc             setvbuf(stdin, (char *)NULL, _IONBF, 0);
402ebfedea0SLionel Sambuc #endif                          /* ndef OPENSSL_NO_SETVBUF_IONBF */
403ebfedea0SLionel Sambuc         BIO_set_fp(in, stdin, BIO_NOCLOSE);
404*0a6a1f1dSLionel Sambuc     } else {
405*0a6a1f1dSLionel Sambuc         if (BIO_read_filename(in, inf) <= 0) {
406ebfedea0SLionel Sambuc             perror(inf);
407ebfedea0SLionel Sambuc             goto end;
408ebfedea0SLionel Sambuc         }
409ebfedea0SLionel Sambuc     }
410ebfedea0SLionel Sambuc 
411ebfedea0SLionel Sambuc     if (!str && passarg) {
412ebfedea0SLionel Sambuc         if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
413ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Error getting password\n");
414ebfedea0SLionel Sambuc             goto end;
415ebfedea0SLionel Sambuc         }
416ebfedea0SLionel Sambuc         str = pass;
417ebfedea0SLionel Sambuc     }
418ebfedea0SLionel Sambuc 
419*0a6a1f1dSLionel Sambuc     if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) {
420*0a6a1f1dSLionel Sambuc         for (;;) {
421ebfedea0SLionel Sambuc             char buf[200];
422ebfedea0SLionel Sambuc 
423ebfedea0SLionel Sambuc             BIO_snprintf(buf, sizeof buf, "enter %s %s password:",
424ebfedea0SLionel Sambuc                          OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
425ebfedea0SLionel Sambuc                          (enc) ? "encryption" : "decryption");
426ebfedea0SLionel Sambuc             strbuf[0] = '\0';
427ebfedea0SLionel Sambuc             i = EVP_read_pw_string((char *)strbuf, SIZE, buf, enc);
428*0a6a1f1dSLionel Sambuc             if (i == 0) {
429*0a6a1f1dSLionel Sambuc                 if (strbuf[0] == '\0') {
430ebfedea0SLionel Sambuc                     ret = 1;
431ebfedea0SLionel Sambuc                     goto end;
432ebfedea0SLionel Sambuc                 }
433ebfedea0SLionel Sambuc                 str = strbuf;
434ebfedea0SLionel Sambuc                 break;
435ebfedea0SLionel Sambuc             }
436*0a6a1f1dSLionel Sambuc             if (i < 0) {
437ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "bad password read\n");
438ebfedea0SLionel Sambuc                 goto end;
439ebfedea0SLionel Sambuc             }
440ebfedea0SLionel Sambuc         }
441ebfedea0SLionel Sambuc     }
442ebfedea0SLionel Sambuc 
443*0a6a1f1dSLionel Sambuc     if (outf == NULL) {
444ebfedea0SLionel Sambuc         BIO_set_fp(out, stdout, BIO_NOCLOSE);
445ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SETVBUF_IONBF
446ebfedea0SLionel Sambuc         if (bufsize != NULL)
447ebfedea0SLionel Sambuc             setvbuf(stdout, (char *)NULL, _IONBF, 0);
448ebfedea0SLionel Sambuc #endif                          /* ndef OPENSSL_NO_SETVBUF_IONBF */
449ebfedea0SLionel Sambuc #ifdef OPENSSL_SYS_VMS
450ebfedea0SLionel Sambuc         {
451ebfedea0SLionel Sambuc             BIO *tmpbio = BIO_new(BIO_f_linebuffer());
452ebfedea0SLionel Sambuc             out = BIO_push(tmpbio, out);
453ebfedea0SLionel Sambuc         }
454ebfedea0SLionel Sambuc #endif
455*0a6a1f1dSLionel Sambuc     } else {
456*0a6a1f1dSLionel Sambuc         if (BIO_write_filename(out, outf) <= 0) {
457ebfedea0SLionel Sambuc             perror(outf);
458ebfedea0SLionel Sambuc             goto end;
459ebfedea0SLionel Sambuc         }
460ebfedea0SLionel Sambuc     }
461ebfedea0SLionel Sambuc 
462ebfedea0SLionel Sambuc     rbio = in;
463ebfedea0SLionel Sambuc     wbio = out;
464ebfedea0SLionel Sambuc 
465ebfedea0SLionel Sambuc #ifdef ZLIB
466ebfedea0SLionel Sambuc 
467*0a6a1f1dSLionel Sambuc     if (do_zlib) {
468ebfedea0SLionel Sambuc         if ((bzl = BIO_new(BIO_f_zlib())) == NULL)
469ebfedea0SLionel Sambuc             goto end;
470ebfedea0SLionel Sambuc         if (enc)
471ebfedea0SLionel Sambuc             wbio = BIO_push(bzl, wbio);
472ebfedea0SLionel Sambuc         else
473ebfedea0SLionel Sambuc             rbio = BIO_push(bzl, rbio);
474ebfedea0SLionel Sambuc     }
475ebfedea0SLionel Sambuc #endif
476ebfedea0SLionel Sambuc 
477*0a6a1f1dSLionel Sambuc     if (base64) {
478ebfedea0SLionel Sambuc         if ((b64 = BIO_new(BIO_f_base64())) == NULL)
479ebfedea0SLionel Sambuc             goto end;
480*0a6a1f1dSLionel Sambuc         if (debug) {
481ebfedea0SLionel Sambuc             BIO_set_callback(b64, BIO_debug_callback);
482ebfedea0SLionel Sambuc             BIO_set_callback_arg(b64, (char *)bio_err);
483ebfedea0SLionel Sambuc         }
484ebfedea0SLionel Sambuc         if (olb64)
485ebfedea0SLionel Sambuc             BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
486ebfedea0SLionel Sambuc         if (enc)
487ebfedea0SLionel Sambuc             wbio = BIO_push(b64, wbio);
488ebfedea0SLionel Sambuc         else
489ebfedea0SLionel Sambuc             rbio = BIO_push(b64, rbio);
490ebfedea0SLionel Sambuc     }
491ebfedea0SLionel Sambuc 
492*0a6a1f1dSLionel Sambuc     if (cipher != NULL) {
493*0a6a1f1dSLionel Sambuc         /*
494*0a6a1f1dSLionel Sambuc          * Note that str is NULL if a key was passed on the command line, so
495*0a6a1f1dSLionel Sambuc          * we get no salt in that case. Is this a bug?
496ebfedea0SLionel Sambuc          */
497*0a6a1f1dSLionel Sambuc         if (str != NULL) {
498*0a6a1f1dSLionel Sambuc             /*
499*0a6a1f1dSLionel Sambuc              * Salt handling: if encrypting generate a salt and write to
500*0a6a1f1dSLionel Sambuc              * output BIO. If decrypting read salt from input BIO.
501ebfedea0SLionel Sambuc              */
502ebfedea0SLionel Sambuc             unsigned char *sptr;
503*0a6a1f1dSLionel Sambuc             if (nosalt)
504*0a6a1f1dSLionel Sambuc                 sptr = NULL;
505ebfedea0SLionel Sambuc             else {
506ebfedea0SLionel Sambuc                 if (enc) {
507ebfedea0SLionel Sambuc                     if (hsalt) {
508ebfedea0SLionel Sambuc                         if (!set_hex(hsalt, salt, sizeof salt)) {
509*0a6a1f1dSLionel Sambuc                             BIO_printf(bio_err, "invalid hex salt value\n");
510ebfedea0SLionel Sambuc                             goto end;
511ebfedea0SLionel Sambuc                         }
512ebfedea0SLionel Sambuc                     } else if (RAND_pseudo_bytes(salt, sizeof salt) < 0)
513ebfedea0SLionel Sambuc                         goto end;
514*0a6a1f1dSLionel Sambuc                     /*
515*0a6a1f1dSLionel Sambuc                      * If -P option then don't bother writing
516*0a6a1f1dSLionel Sambuc                      */
517ebfedea0SLionel Sambuc                     if ((printkey != 2)
518ebfedea0SLionel Sambuc                         && (BIO_write(wbio, magic,
519ebfedea0SLionel Sambuc                                       sizeof magic - 1) != sizeof magic - 1
520ebfedea0SLionel Sambuc                             || BIO_write(wbio,
521ebfedea0SLionel Sambuc                                          (char *)salt,
522ebfedea0SLionel Sambuc                                          sizeof salt) != sizeof salt)) {
523ebfedea0SLionel Sambuc                         BIO_printf(bio_err, "error writing output file\n");
524ebfedea0SLionel Sambuc                         goto end;
525ebfedea0SLionel Sambuc                     }
526ebfedea0SLionel Sambuc                 } else if (BIO_read(rbio, mbuf, sizeof mbuf) != sizeof mbuf
527ebfedea0SLionel Sambuc                            || BIO_read(rbio,
528ebfedea0SLionel Sambuc                                        (unsigned char *)salt,
529ebfedea0SLionel Sambuc                                        sizeof salt) != sizeof salt) {
530ebfedea0SLionel Sambuc                     BIO_printf(bio_err, "error reading input file\n");
531ebfedea0SLionel Sambuc                     goto end;
532ebfedea0SLionel Sambuc                 } else if (memcmp(mbuf, magic, sizeof magic - 1)) {
533ebfedea0SLionel Sambuc                     BIO_printf(bio_err, "bad magic number\n");
534ebfedea0SLionel Sambuc                     goto end;
535ebfedea0SLionel Sambuc                 }
536ebfedea0SLionel Sambuc 
537ebfedea0SLionel Sambuc                 sptr = salt;
538ebfedea0SLionel Sambuc             }
539ebfedea0SLionel Sambuc 
540ebfedea0SLionel Sambuc             EVP_BytesToKey(cipher, dgst, sptr,
541*0a6a1f1dSLionel Sambuc                            (unsigned char *)str, strlen(str), 1, key, iv);
542*0a6a1f1dSLionel Sambuc             /*
543*0a6a1f1dSLionel Sambuc              * zero the complete buffer or the string passed from the command
544*0a6a1f1dSLionel Sambuc              * line bug picked up by Larry J. Hughes Jr. <hughes@indiana.edu>
545*0a6a1f1dSLionel Sambuc              */
546ebfedea0SLionel Sambuc             if (str == strbuf)
547ebfedea0SLionel Sambuc                 OPENSSL_cleanse(str, SIZE);
548ebfedea0SLionel Sambuc             else
549ebfedea0SLionel Sambuc                 OPENSSL_cleanse(str, strlen(str));
550ebfedea0SLionel Sambuc         }
551*0a6a1f1dSLionel Sambuc         if (hiv != NULL) {
552*0a6a1f1dSLionel Sambuc             int siz = EVP_CIPHER_iv_length(cipher);
553*0a6a1f1dSLionel Sambuc             if (siz == 0) {
554*0a6a1f1dSLionel Sambuc                 BIO_printf(bio_err, "warning: iv not use by this cipher\n");
555*0a6a1f1dSLionel Sambuc             } else if (!set_hex(hiv, iv, sizeof iv)) {
556ebfedea0SLionel Sambuc                 BIO_printf(bio_err, "invalid hex iv value\n");
557ebfedea0SLionel Sambuc                 goto end;
558ebfedea0SLionel Sambuc             }
559*0a6a1f1dSLionel Sambuc         }
560ebfedea0SLionel Sambuc         if ((hiv == NULL) && (str == NULL)
561*0a6a1f1dSLionel Sambuc             && EVP_CIPHER_iv_length(cipher) != 0) {
562*0a6a1f1dSLionel Sambuc             /*
563*0a6a1f1dSLionel Sambuc              * No IV was explicitly set and no IV was generated during
564*0a6a1f1dSLionel Sambuc              * EVP_BytesToKey. Hence the IV is undefined, making correct
565*0a6a1f1dSLionel Sambuc              * decryption impossible.
566*0a6a1f1dSLionel Sambuc              */
567ebfedea0SLionel Sambuc             BIO_printf(bio_err, "iv undefined\n");
568ebfedea0SLionel Sambuc             goto end;
569ebfedea0SLionel Sambuc         }
570*0a6a1f1dSLionel Sambuc         if ((hkey != NULL) && !set_hex(hkey, key, EVP_CIPHER_key_length(cipher))) {
571ebfedea0SLionel Sambuc             BIO_printf(bio_err, "invalid hex key value\n");
572ebfedea0SLionel Sambuc             goto end;
573ebfedea0SLionel Sambuc         }
574ebfedea0SLionel Sambuc 
575ebfedea0SLionel Sambuc         if ((benc = BIO_new(BIO_f_cipher())) == NULL)
576ebfedea0SLionel Sambuc             goto end;
577ebfedea0SLionel Sambuc 
578*0a6a1f1dSLionel Sambuc         /*
579*0a6a1f1dSLionel Sambuc          * Since we may be changing parameters work on the encryption context
580*0a6a1f1dSLionel Sambuc          * rather than calling BIO_set_cipher().
581ebfedea0SLionel Sambuc          */
582ebfedea0SLionel Sambuc 
583ebfedea0SLionel Sambuc         BIO_get_cipher_ctx(benc, &ctx);
584ebfedea0SLionel Sambuc 
585ebfedea0SLionel Sambuc         if (non_fips_allow)
586*0a6a1f1dSLionel Sambuc             EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
587ebfedea0SLionel Sambuc 
588*0a6a1f1dSLionel Sambuc         if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) {
589ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Error setting cipher %s\n",
590ebfedea0SLionel Sambuc                        EVP_CIPHER_name(cipher));
591ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
592ebfedea0SLionel Sambuc             goto end;
593ebfedea0SLionel Sambuc         }
594ebfedea0SLionel Sambuc 
595ebfedea0SLionel Sambuc         if (nopad)
596ebfedea0SLionel Sambuc             EVP_CIPHER_CTX_set_padding(ctx, 0);
597ebfedea0SLionel Sambuc 
598*0a6a1f1dSLionel Sambuc         if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) {
599ebfedea0SLionel Sambuc             BIO_printf(bio_err, "Error setting cipher %s\n",
600ebfedea0SLionel Sambuc                        EVP_CIPHER_name(cipher));
601ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
602ebfedea0SLionel Sambuc             goto end;
603ebfedea0SLionel Sambuc         }
604ebfedea0SLionel Sambuc 
605*0a6a1f1dSLionel Sambuc         if (debug) {
606ebfedea0SLionel Sambuc             BIO_set_callback(benc, BIO_debug_callback);
607ebfedea0SLionel Sambuc             BIO_set_callback_arg(benc, (char *)bio_err);
608ebfedea0SLionel Sambuc         }
609ebfedea0SLionel Sambuc 
610*0a6a1f1dSLionel Sambuc         if (printkey) {
611*0a6a1f1dSLionel Sambuc             if (!nosalt) {
612ebfedea0SLionel Sambuc                 printf("salt=");
613ebfedea0SLionel Sambuc                 for (i = 0; i < (int)sizeof(salt); i++)
614ebfedea0SLionel Sambuc                     printf("%02X", salt[i]);
615ebfedea0SLionel Sambuc                 printf("\n");
616ebfedea0SLionel Sambuc             }
617*0a6a1f1dSLionel Sambuc             if (cipher->key_len > 0) {
618ebfedea0SLionel Sambuc                 printf("key=");
619ebfedea0SLionel Sambuc                 for (i = 0; i < cipher->key_len; i++)
620ebfedea0SLionel Sambuc                     printf("%02X", key[i]);
621ebfedea0SLionel Sambuc                 printf("\n");
622ebfedea0SLionel Sambuc             }
623*0a6a1f1dSLionel Sambuc             if (cipher->iv_len > 0) {
624ebfedea0SLionel Sambuc                 printf("iv =");
625ebfedea0SLionel Sambuc                 for (i = 0; i < cipher->iv_len; i++)
626ebfedea0SLionel Sambuc                     printf("%02X", iv[i]);
627ebfedea0SLionel Sambuc                 printf("\n");
628ebfedea0SLionel Sambuc             }
629*0a6a1f1dSLionel Sambuc             if (printkey == 2) {
630ebfedea0SLionel Sambuc                 ret = 0;
631ebfedea0SLionel Sambuc                 goto end;
632ebfedea0SLionel Sambuc             }
633ebfedea0SLionel Sambuc         }
634ebfedea0SLionel Sambuc     }
635ebfedea0SLionel Sambuc 
636ebfedea0SLionel Sambuc     /* Only encrypt/decrypt as we write the file */
637ebfedea0SLionel Sambuc     if (benc != NULL)
638ebfedea0SLionel Sambuc         wbio = BIO_push(benc, wbio);
639ebfedea0SLionel Sambuc 
640*0a6a1f1dSLionel Sambuc     for (;;) {
641ebfedea0SLionel Sambuc         inl = BIO_read(rbio, (char *)buff, bsize);
642*0a6a1f1dSLionel Sambuc         if (inl <= 0)
643*0a6a1f1dSLionel Sambuc             break;
644*0a6a1f1dSLionel Sambuc         if (BIO_write(wbio, (char *)buff, inl) != inl) {
645ebfedea0SLionel Sambuc             BIO_printf(bio_err, "error writing output file\n");
646ebfedea0SLionel Sambuc             goto end;
647ebfedea0SLionel Sambuc         }
648ebfedea0SLionel Sambuc     }
649*0a6a1f1dSLionel Sambuc     if (!BIO_flush(wbio)) {
650ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bad decrypt\n");
651ebfedea0SLionel Sambuc         goto end;
652ebfedea0SLionel Sambuc     }
653ebfedea0SLionel Sambuc 
654ebfedea0SLionel Sambuc     ret = 0;
655*0a6a1f1dSLionel Sambuc     if (verbose) {
656ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bytes read   :%8ld\n", BIO_number_read(in));
657ebfedea0SLionel Sambuc         BIO_printf(bio_err, "bytes written:%8ld\n", BIO_number_written(out));
658ebfedea0SLionel Sambuc     }
659ebfedea0SLionel Sambuc  end:
660ebfedea0SLionel Sambuc     ERR_print_errors(bio_err);
661*0a6a1f1dSLionel Sambuc     if (strbuf != NULL)
662*0a6a1f1dSLionel Sambuc         OPENSSL_free(strbuf);
663*0a6a1f1dSLionel Sambuc     if (buff != NULL)
664*0a6a1f1dSLionel Sambuc         OPENSSL_free(buff);
665*0a6a1f1dSLionel Sambuc     if (in != NULL)
666*0a6a1f1dSLionel Sambuc         BIO_free(in);
667*0a6a1f1dSLionel Sambuc     if (out != NULL)
668*0a6a1f1dSLionel Sambuc         BIO_free_all(out);
669*0a6a1f1dSLionel Sambuc     if (benc != NULL)
670*0a6a1f1dSLionel Sambuc         BIO_free(benc);
671*0a6a1f1dSLionel Sambuc     if (b64 != NULL)
672*0a6a1f1dSLionel Sambuc         BIO_free(b64);
673ebfedea0SLionel Sambuc #ifdef ZLIB
674*0a6a1f1dSLionel Sambuc     if (bzl != NULL)
675*0a6a1f1dSLionel Sambuc         BIO_free(bzl);
676ebfedea0SLionel Sambuc #endif
677*0a6a1f1dSLionel Sambuc     if (pass)
678*0a6a1f1dSLionel Sambuc         OPENSSL_free(pass);
679ebfedea0SLionel Sambuc     apps_shutdown();
680ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
681ebfedea0SLionel Sambuc }
682ebfedea0SLionel Sambuc 
set_hex(char * in,unsigned char * out,int size)683ebfedea0SLionel Sambuc int set_hex(char *in, unsigned char *out, int size)
684ebfedea0SLionel Sambuc {
685ebfedea0SLionel Sambuc     int i, n;
686ebfedea0SLionel Sambuc     unsigned char j;
687ebfedea0SLionel Sambuc 
688ebfedea0SLionel Sambuc     n = strlen(in);
689*0a6a1f1dSLionel Sambuc     if (n > (size * 2)) {
690ebfedea0SLionel Sambuc         BIO_printf(bio_err, "hex string is too long\n");
691ebfedea0SLionel Sambuc         return (0);
692ebfedea0SLionel Sambuc     }
693ebfedea0SLionel Sambuc     memset(out, 0, size);
694*0a6a1f1dSLionel Sambuc     for (i = 0; i < n; i++) {
695ebfedea0SLionel Sambuc         j = (unsigned char)*in;
696ebfedea0SLionel Sambuc         *(in++) = '\0';
697*0a6a1f1dSLionel Sambuc         if (j == 0)
698*0a6a1f1dSLionel Sambuc             break;
699ebfedea0SLionel Sambuc         if ((j >= '0') && (j <= '9'))
700ebfedea0SLionel Sambuc             j -= '0';
701ebfedea0SLionel Sambuc         else if ((j >= 'A') && (j <= 'F'))
702ebfedea0SLionel Sambuc             j = j - 'A' + 10;
703ebfedea0SLionel Sambuc         else if ((j >= 'a') && (j <= 'f'))
704ebfedea0SLionel Sambuc             j = j - 'a' + 10;
705*0a6a1f1dSLionel Sambuc         else {
706ebfedea0SLionel Sambuc             BIO_printf(bio_err, "non-hex digit\n");
707ebfedea0SLionel Sambuc             return (0);
708ebfedea0SLionel Sambuc         }
709ebfedea0SLionel Sambuc         if (i & 1)
710ebfedea0SLionel Sambuc             out[i / 2] |= j;
711ebfedea0SLionel Sambuc         else
712ebfedea0SLionel Sambuc             out[i / 2] = (j << 4);
713ebfedea0SLionel Sambuc     }
714ebfedea0SLionel Sambuc     return (1);
715ebfedea0SLionel Sambuc }
716