1e71b7053SJung-uk Kim /*
2b077aed3SPierre Pronchery * Copyright 1995-2023 The OpenSSL Project Authors. All Rights Reserved.
374664626SKris Kennaway *
4b077aed3SPierre Pronchery * Licensed under the Apache License 2.0 (the "License"). You may not use
5e71b7053SJung-uk Kim * this file except in compliance with the License. You can obtain a copy
6e71b7053SJung-uk Kim * in the file LICENSE in the source distribution or at
7e71b7053SJung-uk Kim * https://www.openssl.org/source/license.html
874664626SKris Kennaway */
974664626SKris Kennaway
1074664626SKris Kennaway #include <stdio.h>
1174664626SKris Kennaway #include <stdlib.h>
1274664626SKris Kennaway #include <string.h>
13e71b7053SJung-uk Kim #include <limits.h>
1474664626SKris Kennaway #include "apps.h"
15e71b7053SJung-uk Kim #include "progs.h"
1674664626SKris Kennaway #include <openssl/bio.h>
1774664626SKris Kennaway #include <openssl/err.h>
1874664626SKris Kennaway #include <openssl/evp.h>
1974664626SKris Kennaway #include <openssl/objects.h>
2074664626SKris Kennaway #include <openssl/x509.h>
21f579bf8eSKris Kennaway #include <openssl/rand.h>
2274664626SKris Kennaway #include <openssl/pem.h>
23a93cbc2bSJung-uk Kim #ifndef OPENSSL_NO_COMP
241f13597dSJung-uk Kim # include <openssl/comp.h>
25a93cbc2bSJung-uk Kim #endif
265c87c606SMark Murray #include <ctype.h>
2774664626SKris Kennaway
2874664626SKris Kennaway #undef SIZE
2974664626SKris Kennaway #undef BSIZE
3074664626SKris Kennaway #define SIZE (512)
3174664626SKris Kennaway #define BSIZE (8*1024)
32e71b7053SJung-uk Kim
33b077aed3SPierre Pronchery #define PBKDF2_ITER_DEFAULT 10000
34b077aed3SPierre Pronchery #define STR(a) XSTR(a)
35b077aed3SPierre Pronchery #define XSTR(a) #a
36b077aed3SPierre Pronchery
37e71b7053SJung-uk Kim static int set_hex(const char *in, unsigned char *out, int size);
38e71b7053SJung-uk Kim static void show_ciphers(const OBJ_NAME *name, void *bio_);
3974664626SKris Kennaway
40ed7112f0SJung-uk Kim struct doall_enc_ciphers {
41ed7112f0SJung-uk Kim BIO *bio;
42ed7112f0SJung-uk Kim int n;
43ed7112f0SJung-uk Kim };
44ed7112f0SJung-uk Kim
45e71b7053SJung-uk Kim typedef enum OPTION_choice {
46b077aed3SPierre Pronchery OPT_COMMON,
47e71b7053SJung-uk Kim OPT_LIST,
48e71b7053SJung-uk Kim OPT_E, OPT_IN, OPT_OUT, OPT_PASS, OPT_ENGINE, OPT_D, OPT_P, OPT_V,
49e71b7053SJung-uk Kim OPT_NOPAD, OPT_SALT, OPT_NOSALT, OPT_DEBUG, OPT_UPPER_P, OPT_UPPER_A,
50e71b7053SJung-uk Kim OPT_A, OPT_Z, OPT_BUFSIZE, OPT_K, OPT_KFILE, OPT_UPPER_K, OPT_NONE,
51e71b7053SJung-uk Kim OPT_UPPER_S, OPT_IV, OPT_MD, OPT_ITER, OPT_PBKDF2, OPT_CIPHER,
52b077aed3SPierre Pronchery OPT_R_ENUM, OPT_PROV_ENUM
53e71b7053SJung-uk Kim } OPTION_CHOICE;
54e71b7053SJung-uk Kim
55e71b7053SJung-uk Kim const OPTIONS enc_options[] = {
56b077aed3SPierre Pronchery OPT_SECTION("General"),
57e71b7053SJung-uk Kim {"help", OPT_HELP, '-', "Display this summary"},
5817f01e99SJung-uk Kim {"list", OPT_LIST, '-', "List ciphers"},
59b077aed3SPierre Pronchery #ifndef OPENSSL_NO_DEPRECATED_3_0
6017f01e99SJung-uk Kim {"ciphers", OPT_LIST, '-', "Alias for -list"},
61b077aed3SPierre Pronchery #endif
62e71b7053SJung-uk Kim {"e", OPT_E, '-', "Encrypt"},
63e71b7053SJung-uk Kim {"d", OPT_D, '-', "Decrypt"},
64e71b7053SJung-uk Kim {"p", OPT_P, '-', "Print the iv/key"},
65e71b7053SJung-uk Kim {"P", OPT_UPPER_P, '-', "Print the iv/key and exit"},
66b077aed3SPierre Pronchery #ifndef OPENSSL_NO_ENGINE
67b077aed3SPierre Pronchery {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
68b077aed3SPierre Pronchery #endif
69b077aed3SPierre Pronchery
70b077aed3SPierre Pronchery OPT_SECTION("Input"),
71b077aed3SPierre Pronchery {"in", OPT_IN, '<', "Input file"},
72b077aed3SPierre Pronchery {"k", OPT_K, 's', "Passphrase"},
73b077aed3SPierre Pronchery {"kfile", OPT_KFILE, '<', "Read passphrase from file"},
74b077aed3SPierre Pronchery
75b077aed3SPierre Pronchery OPT_SECTION("Output"),
76b077aed3SPierre Pronchery {"out", OPT_OUT, '>', "Output file"},
77b077aed3SPierre Pronchery {"pass", OPT_PASS, 's', "Passphrase source"},
78e71b7053SJung-uk Kim {"v", OPT_V, '-', "Verbose output"},
79e71b7053SJung-uk Kim {"a", OPT_A, '-', "Base64 encode/decode, depending on encryption flag"},
80e71b7053SJung-uk Kim {"base64", OPT_A, '-', "Same as option -a"},
81e71b7053SJung-uk Kim {"A", OPT_UPPER_A, '-',
82e71b7053SJung-uk Kim "Used with -[base64|a] to specify base64 buffer as a single line"},
83b077aed3SPierre Pronchery
84b077aed3SPierre Pronchery OPT_SECTION("Encryption"),
85b077aed3SPierre Pronchery {"nopad", OPT_NOPAD, '-', "Disable standard block padding"},
86b077aed3SPierre Pronchery {"salt", OPT_SALT, '-', "Use salt in the KDF (default)"},
87b077aed3SPierre Pronchery {"nosalt", OPT_NOSALT, '-', "Do not use salt in the KDF"},
88b077aed3SPierre Pronchery {"debug", OPT_DEBUG, '-', "Print debug info"},
89b077aed3SPierre Pronchery
90e71b7053SJung-uk Kim {"bufsize", OPT_BUFSIZE, 's', "Buffer size"},
91e71b7053SJung-uk Kim {"K", OPT_UPPER_K, 's', "Raw key, in hex"},
92e71b7053SJung-uk Kim {"S", OPT_UPPER_S, 's', "Salt, in hex"},
93e71b7053SJung-uk Kim {"iv", OPT_IV, 's', "IV in hex"},
94e71b7053SJung-uk Kim {"md", OPT_MD, 's', "Use specified digest to create a key from the passphrase"},
95b077aed3SPierre Pronchery {"iter", OPT_ITER, 'p',
96b077aed3SPierre Pronchery "Specify the iteration count and force the use of PBKDF2"},
97b077aed3SPierre Pronchery {OPT_MORE_STR, 0, 0, "Default: " STR(PBKDF2_ITER_DEFAULT)},
98b077aed3SPierre Pronchery {"pbkdf2", OPT_PBKDF2, '-',
99b077aed3SPierre Pronchery "Use password-based key derivation function 2 (PBKDF2)"},
100b077aed3SPierre Pronchery {OPT_MORE_STR, 0, 0,
101b077aed3SPierre Pronchery "Use -iter to change the iteration count from " STR(PBKDF2_ITER_DEFAULT)},
102e71b7053SJung-uk Kim {"none", OPT_NONE, '-', "Don't encrypt"},
103e71b7053SJung-uk Kim #ifdef ZLIB
1049a3ae0cdSJung-uk Kim {"z", OPT_Z, '-', "Compress or decompress encrypted data using zlib"},
105e71b7053SJung-uk Kim #endif
106b077aed3SPierre Pronchery {"", OPT_CIPHER, '-', "Any supported cipher"},
107b077aed3SPierre Pronchery
108b077aed3SPierre Pronchery OPT_R_OPTIONS,
109b077aed3SPierre Pronchery OPT_PROV_OPTIONS,
110e71b7053SJung-uk Kim {NULL}
111e71b7053SJung-uk Kim };
112e71b7053SJung-uk Kim
enc_main(int argc,char ** argv)113e71b7053SJung-uk Kim int enc_main(int argc, char **argv)
1145c87c606SMark Murray {
115e71b7053SJung-uk Kim static char buf[128];
116f579bf8eSKris Kennaway static const char magic[] = "Salted__";
117e71b7053SJung-uk Kim ENGINE *e = NULL;
118e71b7053SJung-uk Kim BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
119e71b7053SJung-uk Kim NULL, *wbio = NULL;
120e71b7053SJung-uk Kim EVP_CIPHER_CTX *ctx = NULL;
121b077aed3SPierre Pronchery EVP_CIPHER *cipher = NULL;
122b077aed3SPierre Pronchery EVP_MD *dgst = NULL;
123b077aed3SPierre Pronchery const char *digestname = NULL;
124e71b7053SJung-uk Kim char *hkey = NULL, *hiv = NULL, *hsalt = NULL, *p;
125e71b7053SJung-uk Kim char *infile = NULL, *outfile = NULL, *prog;
126e71b7053SJung-uk Kim char *str = NULL, *passarg = NULL, *pass = NULL, *strbuf = NULL;
127b077aed3SPierre Pronchery const char *ciphername = NULL;
128dee36b4fSJung-uk Kim char mbuf[sizeof(magic) - 1];
129e71b7053SJung-uk Kim OPTION_CHOICE o;
130e71b7053SJung-uk Kim int bsize = BSIZE, verbose = 0, debug = 0, olb64 = 0, nosalt = 0;
131e71b7053SJung-uk Kim int enc = 1, printkey = 0, i, k;
132e71b7053SJung-uk Kim int base64 = 0, informat = FORMAT_BINARY, outformat = FORMAT_BINARY;
133e71b7053SJung-uk Kim int ret = 1, inl, nopad = 0;
1345c87c606SMark Murray unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
135e71b7053SJung-uk Kim unsigned char *buff = NULL, salt[PKCS5_SALT_LEN];
136e71b7053SJung-uk Kim int pbkdf2 = 0;
137e71b7053SJung-uk Kim int iter = 0;
138e71b7053SJung-uk Kim long n;
139e71b7053SJung-uk Kim struct doall_enc_ciphers dec;
1401f13597dSJung-uk Kim #ifdef ZLIB
1411f13597dSJung-uk Kim int do_zlib = 0;
1421f13597dSJung-uk Kim BIO *bzl = NULL;
1431f13597dSJung-uk Kim #endif
1445c87c606SMark Murray
145b077aed3SPierre Pronchery /* first check the command name */
146b077aed3SPierre Pronchery if (strcmp(argv[0], "base64") == 0)
14774664626SKris Kennaway base64 = 1;
1481f13597dSJung-uk Kim #ifdef ZLIB
149b077aed3SPierre Pronchery else if (strcmp(argv[0], "zlib") == 0)
1501f13597dSJung-uk Kim do_zlib = 1;
1511f13597dSJung-uk Kim #endif
152b077aed3SPierre Pronchery else if (strcmp(argv[0], "enc") != 0)
153b077aed3SPierre Pronchery ciphername = argv[0];
15474664626SKris Kennaway
155e71b7053SJung-uk Kim prog = opt_init(argc, argv, enc_options);
156e71b7053SJung-uk Kim while ((o = opt_next()) != OPT_EOF) {
157e71b7053SJung-uk Kim switch (o) {
158e71b7053SJung-uk Kim case OPT_EOF:
159e71b7053SJung-uk Kim case OPT_ERR:
160e71b7053SJung-uk Kim opthelp:
161e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
162e71b7053SJung-uk Kim goto end;
163e71b7053SJung-uk Kim case OPT_HELP:
164e71b7053SJung-uk Kim opt_help(enc_options);
165e71b7053SJung-uk Kim ret = 0;
166e71b7053SJung-uk Kim goto end;
167e71b7053SJung-uk Kim case OPT_LIST:
168e71b7053SJung-uk Kim BIO_printf(bio_out, "Supported ciphers:\n");
169e71b7053SJung-uk Kim dec.bio = bio_out;
170e71b7053SJung-uk Kim dec.n = 0;
171e71b7053SJung-uk Kim OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
172e71b7053SJung-uk Kim show_ciphers, &dec);
173e71b7053SJung-uk Kim BIO_printf(bio_out, "\n");
174e71b7053SJung-uk Kim ret = 0;
175e71b7053SJung-uk Kim goto end;
176e71b7053SJung-uk Kim case OPT_E:
177e71b7053SJung-uk Kim enc = 1;
178e71b7053SJung-uk Kim break;
179e71b7053SJung-uk Kim case OPT_IN:
180e71b7053SJung-uk Kim infile = opt_arg();
181e71b7053SJung-uk Kim break;
182e71b7053SJung-uk Kim case OPT_OUT:
183e71b7053SJung-uk Kim outfile = opt_arg();
184e71b7053SJung-uk Kim break;
185e71b7053SJung-uk Kim case OPT_PASS:
186e71b7053SJung-uk Kim passarg = opt_arg();
187e71b7053SJung-uk Kim break;
188e71b7053SJung-uk Kim case OPT_ENGINE:
189e71b7053SJung-uk Kim e = setup_engine(opt_arg(), 0);
190e71b7053SJung-uk Kim break;
191e71b7053SJung-uk Kim case OPT_D:
192e71b7053SJung-uk Kim enc = 0;
193e71b7053SJung-uk Kim break;
194e71b7053SJung-uk Kim case OPT_P:
195e71b7053SJung-uk Kim printkey = 1;
196e71b7053SJung-uk Kim break;
197e71b7053SJung-uk Kim case OPT_V:
198e71b7053SJung-uk Kim verbose = 1;
199e71b7053SJung-uk Kim break;
200e71b7053SJung-uk Kim case OPT_NOPAD:
201e71b7053SJung-uk Kim nopad = 1;
202e71b7053SJung-uk Kim break;
203e71b7053SJung-uk Kim case OPT_SALT:
204e71b7053SJung-uk Kim nosalt = 0;
205e71b7053SJung-uk Kim break;
206e71b7053SJung-uk Kim case OPT_NOSALT:
207e71b7053SJung-uk Kim nosalt = 1;
208e71b7053SJung-uk Kim break;
209e71b7053SJung-uk Kim case OPT_DEBUG:
210e71b7053SJung-uk Kim debug = 1;
211e71b7053SJung-uk Kim break;
212e71b7053SJung-uk Kim case OPT_UPPER_P:
213e71b7053SJung-uk Kim printkey = 2;
214e71b7053SJung-uk Kim break;
215e71b7053SJung-uk Kim case OPT_UPPER_A:
216e71b7053SJung-uk Kim olb64 = 1;
217e71b7053SJung-uk Kim break;
218e71b7053SJung-uk Kim case OPT_A:
219e71b7053SJung-uk Kim base64 = 1;
220e71b7053SJung-uk Kim break;
221e71b7053SJung-uk Kim case OPT_Z:
222e71b7053SJung-uk Kim #ifdef ZLIB
223e71b7053SJung-uk Kim do_zlib = 1;
224e71b7053SJung-uk Kim #endif
225e71b7053SJung-uk Kim break;
226e71b7053SJung-uk Kim case OPT_BUFSIZE:
227e71b7053SJung-uk Kim p = opt_arg();
228e71b7053SJung-uk Kim i = (int)strlen(p) - 1;
229e71b7053SJung-uk Kim k = i >= 1 && p[i] == 'k';
230e71b7053SJung-uk Kim if (k)
231e71b7053SJung-uk Kim p[i] = '\0';
232e71b7053SJung-uk Kim if (!opt_long(opt_arg(), &n)
233e71b7053SJung-uk Kim || n < 0 || (k && n >= LONG_MAX / 1024))
234e71b7053SJung-uk Kim goto opthelp;
235e71b7053SJung-uk Kim if (k)
236e71b7053SJung-uk Kim n *= 1024;
237e71b7053SJung-uk Kim bsize = (int)n;
238e71b7053SJung-uk Kim break;
239e71b7053SJung-uk Kim case OPT_K:
240e71b7053SJung-uk Kim str = opt_arg();
241e71b7053SJung-uk Kim break;
242e71b7053SJung-uk Kim case OPT_KFILE:
243e71b7053SJung-uk Kim in = bio_open_default(opt_arg(), 'r', FORMAT_TEXT);
244e71b7053SJung-uk Kim if (in == NULL)
245e71b7053SJung-uk Kim goto opthelp;
246e71b7053SJung-uk Kim i = BIO_gets(in, buf, sizeof(buf));
247e71b7053SJung-uk Kim BIO_free(in);
248e71b7053SJung-uk Kim in = NULL;
249e71b7053SJung-uk Kim if (i <= 0) {
250e71b7053SJung-uk Kim BIO_printf(bio_err,
251e71b7053SJung-uk Kim "%s Can't read key from %s\n", prog, opt_arg());
252e71b7053SJung-uk Kim goto opthelp;
253e71b7053SJung-uk Kim }
254e71b7053SJung-uk Kim while (--i > 0 && (buf[i] == '\r' || buf[i] == '\n'))
255e71b7053SJung-uk Kim buf[i] = '\0';
256e71b7053SJung-uk Kim if (i <= 0) {
257e71b7053SJung-uk Kim BIO_printf(bio_err, "%s: zero length password\n", prog);
258e71b7053SJung-uk Kim goto opthelp;
259e71b7053SJung-uk Kim }
260e71b7053SJung-uk Kim str = buf;
261e71b7053SJung-uk Kim break;
262e71b7053SJung-uk Kim case OPT_UPPER_K:
263e71b7053SJung-uk Kim hkey = opt_arg();
264e71b7053SJung-uk Kim break;
265e71b7053SJung-uk Kim case OPT_UPPER_S:
266e71b7053SJung-uk Kim hsalt = opt_arg();
267e71b7053SJung-uk Kim break;
268e71b7053SJung-uk Kim case OPT_IV:
269e71b7053SJung-uk Kim hiv = opt_arg();
270e71b7053SJung-uk Kim break;
271e71b7053SJung-uk Kim case OPT_MD:
272b077aed3SPierre Pronchery digestname = opt_arg();
273e71b7053SJung-uk Kim break;
274e71b7053SJung-uk Kim case OPT_CIPHER:
275b077aed3SPierre Pronchery ciphername = opt_unknown();
276e71b7053SJung-uk Kim break;
277e71b7053SJung-uk Kim case OPT_ITER:
278b077aed3SPierre Pronchery iter = opt_int_arg();
279e71b7053SJung-uk Kim pbkdf2 = 1;
280e71b7053SJung-uk Kim break;
281e71b7053SJung-uk Kim case OPT_PBKDF2:
282e71b7053SJung-uk Kim pbkdf2 = 1;
283e71b7053SJung-uk Kim if (iter == 0) /* do not overwrite a chosen value */
284b077aed3SPierre Pronchery iter = PBKDF2_ITER_DEFAULT;
285e71b7053SJung-uk Kim break;
286e71b7053SJung-uk Kim case OPT_NONE:
287e71b7053SJung-uk Kim cipher = NULL;
288e71b7053SJung-uk Kim break;
289e71b7053SJung-uk Kim case OPT_R_CASES:
290e71b7053SJung-uk Kim if (!opt_rand(o))
291e71b7053SJung-uk Kim goto end;
292e71b7053SJung-uk Kim break;
293b077aed3SPierre Pronchery case OPT_PROV_CASES:
294b077aed3SPierre Pronchery if (!opt_provider(o))
295b077aed3SPierre Pronchery goto end;
296b077aed3SPierre Pronchery break;
297e71b7053SJung-uk Kim }
298e71b7053SJung-uk Kim }
299b077aed3SPierre Pronchery
300b077aed3SPierre Pronchery /* No extra arguments. */
301b077aed3SPierre Pronchery argc = opt_num_rest();
302b077aed3SPierre Pronchery if (argc != 0)
303b077aed3SPierre Pronchery goto opthelp;
304b077aed3SPierre Pronchery if (!app_RAND_load())
305b077aed3SPierre Pronchery goto end;
306b077aed3SPierre Pronchery
307b077aed3SPierre Pronchery /* Get the cipher name, either from progname (if set) or flag. */
308b077aed3SPierre Pronchery if (ciphername != NULL) {
309b077aed3SPierre Pronchery if (!opt_cipher(ciphername, &cipher))
310e71b7053SJung-uk Kim goto opthelp;
311e71b7053SJung-uk Kim }
312b077aed3SPierre Pronchery if (digestname != NULL) {
313b077aed3SPierre Pronchery if (!opt_md(digestname, &dgst))
314b077aed3SPierre Pronchery goto opthelp;
31594ad176cSJung-uk Kim }
316e71b7053SJung-uk Kim if (dgst == NULL)
317b077aed3SPierre Pronchery dgst = (EVP_MD *)EVP_sha256();
3186be8ae07SJacques Vidrine
319e71b7053SJung-uk Kim if (iter == 0)
320e71b7053SJung-uk Kim iter = 1;
32174664626SKris Kennaway
32274664626SKris Kennaway /* It must be large enough for a base64 encoded line */
323e71b7053SJung-uk Kim if (base64 && bsize < 80)
324e71b7053SJung-uk Kim bsize = 80;
3256f9291ceSJung-uk Kim if (verbose)
3266f9291ceSJung-uk Kim BIO_printf(bio_err, "bufsize=%d\n", bsize);
327e71b7053SJung-uk Kim
328e71b7053SJung-uk Kim #ifdef ZLIB
329e71b7053SJung-uk Kim if (!do_zlib)
330e71b7053SJung-uk Kim #endif
331e71b7053SJung-uk Kim if (base64) {
332e71b7053SJung-uk Kim if (enc)
333e71b7053SJung-uk Kim outformat = FORMAT_BASE64;
334e71b7053SJung-uk Kim else
335e71b7053SJung-uk Kim informat = FORMAT_BASE64;
33674664626SKris Kennaway }
33774664626SKris Kennaway
338e71b7053SJung-uk Kim strbuf = app_malloc(SIZE, "strbuf");
339e71b7053SJung-uk Kim buff = app_malloc(EVP_ENCODE_LENGTH(bsize), "evp buffer");
34074664626SKris Kennaway
341e71b7053SJung-uk Kim if (infile == NULL) {
342e71b7053SJung-uk Kim in = dup_bio_in(informat);
3436f9291ceSJung-uk Kim } else {
344e71b7053SJung-uk Kim in = bio_open_default(infile, 'r', informat);
345e71b7053SJung-uk Kim }
346e71b7053SJung-uk Kim if (in == NULL)
34774664626SKris Kennaway goto end;
34874664626SKris Kennaway
349e71b7053SJung-uk Kim if (str == NULL && passarg != NULL) {
350e71b7053SJung-uk Kim if (!app_passwd(passarg, NULL, &pass, NULL)) {
351f579bf8eSKris Kennaway BIO_printf(bio_err, "Error getting password\n");
352f579bf8eSKris Kennaway goto end;
353f579bf8eSKris Kennaway }
354f579bf8eSKris Kennaway str = pass;
355f579bf8eSKris Kennaway }
356f579bf8eSKris Kennaway
3576f9291ceSJung-uk Kim if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) {
358e71b7053SJung-uk Kim if (1) {
359e71b7053SJung-uk Kim #ifndef OPENSSL_NO_UI_CONSOLE
3606f9291ceSJung-uk Kim for (;;) {
361e71b7053SJung-uk Kim char prompt[200];
36274664626SKris Kennaway
363e71b7053SJung-uk Kim BIO_snprintf(prompt, sizeof(prompt), "enter %s %s password:",
364b077aed3SPierre Pronchery EVP_CIPHER_get0_name(cipher),
36574664626SKris Kennaway (enc) ? "encryption" : "decryption");
36674664626SKris Kennaway strbuf[0] = '\0';
367e71b7053SJung-uk Kim i = EVP_read_pw_string((char *)strbuf, SIZE, prompt, enc);
3686f9291ceSJung-uk Kim if (i == 0) {
3696f9291ceSJung-uk Kim if (strbuf[0] == '\0') {
37074664626SKris Kennaway ret = 1;
37174664626SKris Kennaway goto end;
37274664626SKris Kennaway }
37374664626SKris Kennaway str = strbuf;
37474664626SKris Kennaway break;
37574664626SKris Kennaway }
3766f9291ceSJung-uk Kim if (i < 0) {
37774664626SKris Kennaway BIO_printf(bio_err, "bad password read\n");
37874664626SKris Kennaway goto end;
37974664626SKris Kennaway }
38074664626SKris Kennaway }
3816f9291ceSJung-uk Kim } else {
382e71b7053SJung-uk Kim #endif
383e71b7053SJung-uk Kim BIO_printf(bio_err, "password required\n");
38474664626SKris Kennaway goto end;
38574664626SKris Kennaway }
38674664626SKris Kennaway }
38774664626SKris Kennaway
388e71b7053SJung-uk Kim out = bio_open_default(outfile, 'w', outformat);
389e71b7053SJung-uk Kim if (out == NULL)
390e71b7053SJung-uk Kim goto end;
391e71b7053SJung-uk Kim
392e71b7053SJung-uk Kim if (debug) {
393b077aed3SPierre Pronchery BIO_set_callback_ex(in, BIO_debug_callback_ex);
394b077aed3SPierre Pronchery BIO_set_callback_ex(out, BIO_debug_callback_ex);
395e71b7053SJung-uk Kim BIO_set_callback_arg(in, (char *)bio_err);
396e71b7053SJung-uk Kim BIO_set_callback_arg(out, (char *)bio_err);
397e71b7053SJung-uk Kim }
398e71b7053SJung-uk Kim
39974664626SKris Kennaway rbio = in;
40074664626SKris Kennaway wbio = out;
40174664626SKris Kennaway
4021f13597dSJung-uk Kim #ifdef ZLIB
4036f9291ceSJung-uk Kim if (do_zlib) {
4041f13597dSJung-uk Kim if ((bzl = BIO_new(BIO_f_zlib())) == NULL)
4051f13597dSJung-uk Kim goto end;
406e71b7053SJung-uk Kim if (debug) {
407b077aed3SPierre Pronchery BIO_set_callback_ex(bzl, BIO_debug_callback_ex);
408e71b7053SJung-uk Kim BIO_set_callback_arg(bzl, (char *)bio_err);
409e71b7053SJung-uk Kim }
4101f13597dSJung-uk Kim if (enc)
4111f13597dSJung-uk Kim wbio = BIO_push(bzl, wbio);
4121f13597dSJung-uk Kim else
4131f13597dSJung-uk Kim rbio = BIO_push(bzl, rbio);
4141f13597dSJung-uk Kim }
4151f13597dSJung-uk Kim #endif
4161f13597dSJung-uk Kim
4176f9291ceSJung-uk Kim if (base64) {
41874664626SKris Kennaway if ((b64 = BIO_new(BIO_f_base64())) == NULL)
41974664626SKris Kennaway goto end;
4206f9291ceSJung-uk Kim if (debug) {
421b077aed3SPierre Pronchery BIO_set_callback_ex(b64, BIO_debug_callback_ex);
4225471f83eSSimon L. B. Nielsen BIO_set_callback_arg(b64, (char *)bio_err);
42374664626SKris Kennaway }
42474664626SKris Kennaway if (olb64)
42574664626SKris Kennaway BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
42674664626SKris Kennaway if (enc)
42774664626SKris Kennaway wbio = BIO_push(b64, wbio);
42874664626SKris Kennaway else
42974664626SKris Kennaway rbio = BIO_push(b64, rbio);
43074664626SKris Kennaway }
43174664626SKris Kennaway
4326f9291ceSJung-uk Kim if (cipher != NULL) {
433b077aed3SPierre Pronchery if (str != NULL) { /* a passphrase is available */
4346f9291ceSJung-uk Kim /*
435b077aed3SPierre Pronchery * Salt handling: if encrypting generate a salt if not supplied,
436b077aed3SPierre Pronchery * and write to output BIO. If decrypting use salt from input BIO
437b077aed3SPierre Pronchery * if not given with args
438f579bf8eSKris Kennaway */
439f579bf8eSKris Kennaway unsigned char *sptr;
440e71b7053SJung-uk Kim size_t str_len = strlen(str);
441e71b7053SJung-uk Kim
442e71b7053SJung-uk Kim if (nosalt) {
4436f9291ceSJung-uk Kim sptr = NULL;
444e71b7053SJung-uk Kim } else {
445b077aed3SPierre Pronchery if (hsalt != NULL && !set_hex(hsalt, salt, sizeof(salt))) {
4466f9291ceSJung-uk Kim BIO_printf(bio_err, "invalid hex salt value\n");
447f579bf8eSKris Kennaway goto end;
448f579bf8eSKris Kennaway }
449b077aed3SPierre Pronchery if (enc) { /* encryption */
450b077aed3SPierre Pronchery if (hsalt == NULL) {
451b077aed3SPierre Pronchery if (RAND_bytes(salt, sizeof(salt)) <= 0) {
452b077aed3SPierre Pronchery BIO_printf(bio_err, "RAND_bytes failed\n");
453f579bf8eSKris Kennaway goto end;
454e71b7053SJung-uk Kim }
4556f9291ceSJung-uk Kim /*
456b077aed3SPierre Pronchery * If -P option then don't bother writing.
457b077aed3SPierre Pronchery * If salt is given, shouldn't either ?
4586f9291ceSJung-uk Kim */
459f579bf8eSKris Kennaway if ((printkey != 2)
460f579bf8eSKris Kennaway && (BIO_write(wbio, magic,
461dee36b4fSJung-uk Kim sizeof(magic) - 1) != sizeof(magic) - 1
462f579bf8eSKris Kennaway || BIO_write(wbio,
463f579bf8eSKris Kennaway (char *)salt,
464dee36b4fSJung-uk Kim sizeof(salt)) != sizeof(salt))) {
465f579bf8eSKris Kennaway BIO_printf(bio_err, "error writing output file\n");
466f579bf8eSKris Kennaway goto end;
467f579bf8eSKris Kennaway }
468b077aed3SPierre Pronchery }
469b077aed3SPierre Pronchery } else { /* decryption */
470b077aed3SPierre Pronchery if (hsalt == NULL) {
471b077aed3SPierre Pronchery if (BIO_read(rbio, mbuf, sizeof(mbuf)) != sizeof(mbuf)) {
472b077aed3SPierre Pronchery BIO_printf(bio_err, "error reading input file\n");
473b077aed3SPierre Pronchery goto end;
474b077aed3SPierre Pronchery }
475b077aed3SPierre Pronchery if (memcmp(mbuf, magic, sizeof(mbuf)) == 0) { /* file IS salted */
476b077aed3SPierre Pronchery if (BIO_read(rbio, salt,
477dee36b4fSJung-uk Kim sizeof(salt)) != sizeof(salt)) {
478f579bf8eSKris Kennaway BIO_printf(bio_err, "error reading input file\n");
479f579bf8eSKris Kennaway goto end;
480b077aed3SPierre Pronchery }
481b077aed3SPierre Pronchery } else { /* file is NOT salted, NO salt available */
482f579bf8eSKris Kennaway BIO_printf(bio_err, "bad magic number\n");
483f579bf8eSKris Kennaway goto end;
484f579bf8eSKris Kennaway }
485b077aed3SPierre Pronchery }
486b077aed3SPierre Pronchery }
487f579bf8eSKris Kennaway sptr = salt;
488f579bf8eSKris Kennaway }
489f579bf8eSKris Kennaway
490e71b7053SJung-uk Kim if (pbkdf2 == 1) {
491e71b7053SJung-uk Kim /*
492e71b7053SJung-uk Kim * derive key and default iv
493e71b7053SJung-uk Kim * concatenated into a temporary buffer
494e71b7053SJung-uk Kim */
495e71b7053SJung-uk Kim unsigned char tmpkeyiv[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH];
496b077aed3SPierre Pronchery int iklen = EVP_CIPHER_get_key_length(cipher);
497b077aed3SPierre Pronchery int ivlen = EVP_CIPHER_get_iv_length(cipher);
498e71b7053SJung-uk Kim /* not needed if HASH_UPDATE() is fixed : */
499e71b7053SJung-uk Kim int islen = (sptr != NULL ? sizeof(salt) : 0);
500e71b7053SJung-uk Kim if (!PKCS5_PBKDF2_HMAC(str, str_len, sptr, islen,
501e71b7053SJung-uk Kim iter, dgst, iklen+ivlen, tmpkeyiv)) {
502e71b7053SJung-uk Kim BIO_printf(bio_err, "PKCS5_PBKDF2_HMAC failed\n");
503e71b7053SJung-uk Kim goto end;
504e71b7053SJung-uk Kim }
505e71b7053SJung-uk Kim /* split and move data back to global buffer */
506e71b7053SJung-uk Kim memcpy(key, tmpkeyiv, iklen);
507e71b7053SJung-uk Kim memcpy(iv, tmpkeyiv+iklen, ivlen);
508e71b7053SJung-uk Kim } else {
509e71b7053SJung-uk Kim BIO_printf(bio_err, "*** WARNING : "
510e71b7053SJung-uk Kim "deprecated key derivation used.\n"
511e71b7053SJung-uk Kim "Using -iter or -pbkdf2 would be better.\n");
512e71b7053SJung-uk Kim if (!EVP_BytesToKey(cipher, dgst, sptr,
513e71b7053SJung-uk Kim (unsigned char *)str, str_len,
514e71b7053SJung-uk Kim 1, key, iv)) {
515e71b7053SJung-uk Kim BIO_printf(bio_err, "EVP_BytesToKey failed\n");
516e71b7053SJung-uk Kim goto end;
517e71b7053SJung-uk Kim }
518e71b7053SJung-uk Kim }
5196f9291ceSJung-uk Kim /*
5206f9291ceSJung-uk Kim * zero the complete buffer or the string passed from the command
521e71b7053SJung-uk Kim * line.
5226f9291ceSJung-uk Kim */
523f579bf8eSKris Kennaway if (str == strbuf)
5245c87c606SMark Murray OPENSSL_cleanse(str, SIZE);
525f579bf8eSKris Kennaway else
526e71b7053SJung-uk Kim OPENSSL_cleanse(str, str_len);
527f579bf8eSKris Kennaway }
528ed6b93beSJung-uk Kim if (hiv != NULL) {
529b077aed3SPierre Pronchery int siz = EVP_CIPHER_get_iv_length(cipher);
530ed6b93beSJung-uk Kim if (siz == 0) {
531610a21fdSJung-uk Kim BIO_printf(bio_err, "warning: iv not used by this cipher\n");
532e71b7053SJung-uk Kim } else if (!set_hex(hiv, iv, siz)) {
533f579bf8eSKris Kennaway BIO_printf(bio_err, "invalid hex iv value\n");
534f579bf8eSKris Kennaway goto end;
535f579bf8eSKris Kennaway }
536ed6b93beSJung-uk Kim }
537db522d3aSSimon L. B. Nielsen if ((hiv == NULL) && (str == NULL)
538b077aed3SPierre Pronchery && EVP_CIPHER_get_iv_length(cipher) != 0) {
5396f9291ceSJung-uk Kim /*
540e71b7053SJung-uk Kim * No IV was explicitly set and no IV was generated.
541e71b7053SJung-uk Kim * Hence the IV is undefined, making correct decryption impossible.
5426f9291ceSJung-uk Kim */
54326d191b4SKris Kennaway BIO_printf(bio_err, "iv undefined\n");
54426d191b4SKris Kennaway goto end;
54526d191b4SKris Kennaway }
546e71b7053SJung-uk Kim if (hkey != NULL) {
547b077aed3SPierre Pronchery if (!set_hex(hkey, key, EVP_CIPHER_get_key_length(cipher))) {
548f579bf8eSKris Kennaway BIO_printf(bio_err, "invalid hex key value\n");
549f579bf8eSKris Kennaway goto end;
550f579bf8eSKris Kennaway }
551e71b7053SJung-uk Kim /* wiping secret data as we no longer need it */
552b077aed3SPierre Pronchery cleanse(hkey);
553e71b7053SJung-uk Kim }
554f579bf8eSKris Kennaway
555f579bf8eSKris Kennaway if ((benc = BIO_new(BIO_f_cipher())) == NULL)
556f579bf8eSKris Kennaway goto end;
5573b4e3dcbSSimon L. B. Nielsen
5586f9291ceSJung-uk Kim /*
5596f9291ceSJung-uk Kim * Since we may be changing parameters work on the encryption context
5606f9291ceSJung-uk Kim * rather than calling BIO_set_cipher().
5613b4e3dcbSSimon L. B. Nielsen */
5623b4e3dcbSSimon L. B. Nielsen
5635c87c606SMark Murray BIO_get_cipher_ctx(benc, &ctx);
564db522d3aSSimon L. B. Nielsen
565b077aed3SPierre Pronchery if (!EVP_CipherInit_ex(ctx, cipher, e, NULL, NULL, enc)) {
5663b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error setting cipher %s\n",
567b077aed3SPierre Pronchery EVP_CIPHER_get0_name(cipher));
5683b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err);
5693b4e3dcbSSimon L. B. Nielsen goto end;
5705c87c606SMark Murray }
5713b4e3dcbSSimon L. B. Nielsen
5723b4e3dcbSSimon L. B. Nielsen if (nopad)
5733b4e3dcbSSimon L. B. Nielsen EVP_CIPHER_CTX_set_padding(ctx, 0);
5743b4e3dcbSSimon L. B. Nielsen
5756f9291ceSJung-uk Kim if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) {
5763b4e3dcbSSimon L. B. Nielsen BIO_printf(bio_err, "Error setting cipher %s\n",
577b077aed3SPierre Pronchery EVP_CIPHER_get0_name(cipher));
5783b4e3dcbSSimon L. B. Nielsen ERR_print_errors(bio_err);
5793b4e3dcbSSimon L. B. Nielsen goto end;
5803b4e3dcbSSimon L. B. Nielsen }
5813b4e3dcbSSimon L. B. Nielsen
5826f9291ceSJung-uk Kim if (debug) {
583b077aed3SPierre Pronchery BIO_set_callback_ex(benc, BIO_debug_callback_ex);
5845471f83eSSimon L. B. Nielsen BIO_set_callback_arg(benc, (char *)bio_err);
585f579bf8eSKris Kennaway }
586f579bf8eSKris Kennaway
5876f9291ceSJung-uk Kim if (printkey) {
5886f9291ceSJung-uk Kim if (!nosalt) {
589f579bf8eSKris Kennaway printf("salt=");
5903b4e3dcbSSimon L. B. Nielsen for (i = 0; i < (int)sizeof(salt); i++)
591f579bf8eSKris Kennaway printf("%02X", salt[i]);
592f579bf8eSKris Kennaway printf("\n");
593f579bf8eSKris Kennaway }
594b077aed3SPierre Pronchery if (EVP_CIPHER_get_key_length(cipher) > 0) {
595f579bf8eSKris Kennaway printf("key=");
596b077aed3SPierre Pronchery for (i = 0; i < EVP_CIPHER_get_key_length(cipher); i++)
597f579bf8eSKris Kennaway printf("%02X", key[i]);
598f579bf8eSKris Kennaway printf("\n");
599f579bf8eSKris Kennaway }
600b077aed3SPierre Pronchery if (EVP_CIPHER_get_iv_length(cipher) > 0) {
601f579bf8eSKris Kennaway printf("iv =");
602b077aed3SPierre Pronchery for (i = 0; i < EVP_CIPHER_get_iv_length(cipher); i++)
603f579bf8eSKris Kennaway printf("%02X", iv[i]);
604f579bf8eSKris Kennaway printf("\n");
605f579bf8eSKris Kennaway }
6066f9291ceSJung-uk Kim if (printkey == 2) {
607f579bf8eSKris Kennaway ret = 0;
608f579bf8eSKris Kennaway goto end;
609f579bf8eSKris Kennaway }
610f579bf8eSKris Kennaway }
611f579bf8eSKris Kennaway }
612f579bf8eSKris Kennaway
61374664626SKris Kennaway /* Only encrypt/decrypt as we write the file */
61474664626SKris Kennaway if (benc != NULL)
61574664626SKris Kennaway wbio = BIO_push(benc, wbio);
61674664626SKris Kennaway
617da327cd2SJung-uk Kim while (BIO_pending(rbio) || !BIO_eof(rbio)) {
61874664626SKris Kennaway inl = BIO_read(rbio, (char *)buff, bsize);
6196f9291ceSJung-uk Kim if (inl <= 0)
6206f9291ceSJung-uk Kim break;
6216f9291ceSJung-uk Kim if (BIO_write(wbio, (char *)buff, inl) != inl) {
62274664626SKris Kennaway BIO_printf(bio_err, "error writing output file\n");
62374664626SKris Kennaway goto end;
62474664626SKris Kennaway }
62574664626SKris Kennaway }
6266f9291ceSJung-uk Kim if (!BIO_flush(wbio)) {
627*ad991e4cSEd Maste if (enc)
628*ad991e4cSEd Maste BIO_printf(bio_err, "bad encrypt\n");
629*ad991e4cSEd Maste else
63074664626SKris Kennaway BIO_printf(bio_err, "bad decrypt\n");
63174664626SKris Kennaway goto end;
63274664626SKris Kennaway }
63374664626SKris Kennaway
63474664626SKris Kennaway ret = 0;
6356f9291ceSJung-uk Kim if (verbose) {
636e71b7053SJung-uk Kim BIO_printf(bio_err, "bytes read : %8ju\n", BIO_number_read(in));
637e71b7053SJung-uk Kim BIO_printf(bio_err, "bytes written: %8ju\n", BIO_number_written(out));
63874664626SKris Kennaway }
63974664626SKris Kennaway end:
640f579bf8eSKris Kennaway ERR_print_errors(bio_err);
6416f9291ceSJung-uk Kim OPENSSL_free(strbuf);
6426f9291ceSJung-uk Kim OPENSSL_free(buff);
6436f9291ceSJung-uk Kim BIO_free(in);
6446f9291ceSJung-uk Kim BIO_free_all(out);
6456f9291ceSJung-uk Kim BIO_free(benc);
6466f9291ceSJung-uk Kim BIO_free(b64);
647b077aed3SPierre Pronchery EVP_MD_free(dgst);
648b077aed3SPierre Pronchery EVP_CIPHER_free(cipher);
6491f13597dSJung-uk Kim #ifdef ZLIB
6506f9291ceSJung-uk Kim BIO_free(bzl);
6511f13597dSJung-uk Kim #endif
6526cf8931aSJung-uk Kim release_engine(e);
6536f9291ceSJung-uk Kim OPENSSL_free(pass);
654e71b7053SJung-uk Kim return ret;
65574664626SKris Kennaway }
65674664626SKris Kennaway
show_ciphers(const OBJ_NAME * name,void * arg)657e71b7053SJung-uk Kim static void show_ciphers(const OBJ_NAME *name, void *arg)
658e71b7053SJung-uk Kim {
659e71b7053SJung-uk Kim struct doall_enc_ciphers *dec = (struct doall_enc_ciphers *)arg;
660e71b7053SJung-uk Kim const EVP_CIPHER *cipher;
661e71b7053SJung-uk Kim
662e71b7053SJung-uk Kim if (!islower((unsigned char)*name->name))
663e71b7053SJung-uk Kim return;
664e71b7053SJung-uk Kim
665e71b7053SJung-uk Kim /* Filter out ciphers that we cannot use */
666e71b7053SJung-uk Kim cipher = EVP_get_cipherbyname(name->name);
667b077aed3SPierre Pronchery if (cipher == NULL
668b077aed3SPierre Pronchery || (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0
669b077aed3SPierre Pronchery || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE)
670e71b7053SJung-uk Kim return;
671e71b7053SJung-uk Kim
672e71b7053SJung-uk Kim BIO_printf(dec->bio, "-%-25s", name->name);
673e71b7053SJung-uk Kim if (++dec->n == 3) {
674e71b7053SJung-uk Kim BIO_printf(dec->bio, "\n");
675e71b7053SJung-uk Kim dec->n = 0;
676e71b7053SJung-uk Kim } else
677e71b7053SJung-uk Kim BIO_printf(dec->bio, " ");
678e71b7053SJung-uk Kim }
679e71b7053SJung-uk Kim
set_hex(const char * in,unsigned char * out,int size)680e71b7053SJung-uk Kim static int set_hex(const char *in, unsigned char *out, int size)
68174664626SKris Kennaway {
68274664626SKris Kennaway int i, n;
68374664626SKris Kennaway unsigned char j;
68474664626SKris Kennaway
685e71b7053SJung-uk Kim i = size * 2;
68674664626SKris Kennaway n = strlen(in);
687e71b7053SJung-uk Kim if (n > i) {
688e71b7053SJung-uk Kim BIO_printf(bio_err, "hex string is too long, ignoring excess\n");
689e71b7053SJung-uk Kim n = i; /* ignore exceeding part */
690e71b7053SJung-uk Kim } else if (n < i) {
691e71b7053SJung-uk Kim BIO_printf(bio_err, "hex string is too short, padding with zero bytes to length\n");
69274664626SKris Kennaway }
693e71b7053SJung-uk Kim
69474664626SKris Kennaway memset(out, 0, size);
6956f9291ceSJung-uk Kim for (i = 0; i < n; i++) {
696e71b7053SJung-uk Kim j = (unsigned char)*in++;
697e71b7053SJung-uk Kim if (!isxdigit(j)) {
69874664626SKris Kennaway BIO_printf(bio_err, "non-hex digit\n");
699e71b7053SJung-uk Kim return 0;
70074664626SKris Kennaway }
701e71b7053SJung-uk Kim j = (unsigned char)OPENSSL_hexchar2int(j);
70274664626SKris Kennaway if (i & 1)
70374664626SKris Kennaway out[i / 2] |= j;
70474664626SKris Kennaway else
70574664626SKris Kennaway out[i / 2] = (j << 4);
70674664626SKris Kennaway }
707e71b7053SJung-uk Kim return 1;
70874664626SKris Kennaway }
709