1 /*
2 * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <limits.h>
14 #include "apps.h"
15 #include "progs.h"
16 #include <openssl/bio.h>
17 #include <openssl/err.h>
18 #include <openssl/evp.h>
19 #include <openssl/objects.h>
20 #include <openssl/x509.h>
21 #include <openssl/rand.h>
22 #include <openssl/pem.h>
23 #ifndef OPENSSL_NO_COMP
24 # include <openssl/comp.h>
25 #endif
26 #include <ctype.h>
27
28 #undef SIZE
29 #undef BSIZE
30 #define SIZE (512)
31 #define BSIZE (8*1024)
32
33 #define PBKDF2_ITER_DEFAULT 10000
34 #define STR(a) XSTR(a)
35 #define XSTR(a) #a
36
37 static int set_hex(const char *in, unsigned char *out, int size);
38 static void show_ciphers(const OBJ_NAME *name, void *bio_);
39
40 struct doall_enc_ciphers {
41 BIO *bio;
42 int n;
43 };
44
45 typedef enum OPTION_choice {
46 OPT_COMMON,
47 OPT_LIST,
48 OPT_E, OPT_IN, OPT_OUT, OPT_PASS, OPT_ENGINE, OPT_D, OPT_P, OPT_V,
49 OPT_NOPAD, OPT_SALT, OPT_NOSALT, OPT_DEBUG, OPT_UPPER_P, OPT_UPPER_A,
50 OPT_A, OPT_Z, OPT_BUFSIZE, OPT_K, OPT_KFILE, OPT_UPPER_K, OPT_NONE,
51 OPT_UPPER_S, OPT_IV, OPT_MD, OPT_ITER, OPT_PBKDF2, OPT_CIPHER,
52 OPT_SALTLEN, OPT_R_ENUM, OPT_PROV_ENUM,
53 OPT_SKEYOPT, OPT_SKEYMGMT
54 } OPTION_CHOICE;
55
56 const OPTIONS enc_options[] = {
57 OPT_SECTION("General"),
58 {"help", OPT_HELP, '-', "Display this summary"},
59 {"list", OPT_LIST, '-', "List ciphers"},
60 #ifndef OPENSSL_NO_DEPRECATED_3_0
61 {"ciphers", OPT_LIST, '-', "Alias for -list"},
62 #endif
63 {"e", OPT_E, '-', "Encrypt"},
64 {"d", OPT_D, '-', "Decrypt"},
65 {"p", OPT_P, '-', "Print the iv/key"},
66 {"P", OPT_UPPER_P, '-', "Print the iv/key and exit"},
67 #ifndef OPENSSL_NO_ENGINE
68 {"engine", OPT_ENGINE, 's', "Use engine, possibly a hardware device"},
69 #endif
70
71 OPT_SECTION("Input"),
72 {"in", OPT_IN, '<', "Input file"},
73 {"k", OPT_K, 's', "Passphrase"},
74 {"kfile", OPT_KFILE, '<', "Read passphrase from file"},
75
76 OPT_SECTION("Output"),
77 {"out", OPT_OUT, '>', "Output file"},
78 {"pass", OPT_PASS, 's', "Passphrase source"},
79 {"v", OPT_V, '-', "Verbose output"},
80 {"a", OPT_A, '-', "Base64 encode/decode, depending on encryption flag"},
81 {"base64", OPT_A, '-', "Same as option -a"},
82 {"A", OPT_UPPER_A, '-',
83 "Used with -[base64|a] to specify base64 buffer as a single line"},
84
85 OPT_SECTION("Encryption"),
86 {"nopad", OPT_NOPAD, '-', "Disable standard block padding"},
87 {"salt", OPT_SALT, '-', "Use salt in the KDF (default)"},
88 {"nosalt", OPT_NOSALT, '-', "Do not use salt in the KDF"},
89 {"debug", OPT_DEBUG, '-', "Print debug info"},
90
91 {"bufsize", OPT_BUFSIZE, 's', "Buffer size"},
92 {"K", OPT_UPPER_K, 's', "Raw key, in hex"},
93 {"S", OPT_UPPER_S, 's', "Salt, in hex"},
94 {"iv", OPT_IV, 's', "IV in hex"},
95 {"md", OPT_MD, 's', "Use specified digest to create a key from the passphrase"},
96 {"iter", OPT_ITER, 'p',
97 "Specify the iteration count and force the use of PBKDF2"},
98 {OPT_MORE_STR, 0, 0, "Default: " STR(PBKDF2_ITER_DEFAULT)},
99 {"pbkdf2", OPT_PBKDF2, '-',
100 "Use password-based key derivation function 2 (PBKDF2)"},
101 {OPT_MORE_STR, 0, 0,
102 "Use -iter to change the iteration count from " STR(PBKDF2_ITER_DEFAULT)},
103 {"none", OPT_NONE, '-', "Don't encrypt"},
104 {"saltlen", OPT_SALTLEN, 'p', "Specify the PBKDF2 salt length (in bytes)"},
105 {OPT_MORE_STR, 0, 0, "Default: 16"},
106 #ifndef OPENSSL_NO_ZLIB
107 {"z", OPT_Z, '-', "Compress or decompress encrypted data using zlib"},
108 #endif
109 {"skeyopt", OPT_SKEYOPT, 's', "Key options as opt:value for opaque symmetric key handling"},
110 {"skeymgmt", OPT_SKEYMGMT, 's', "Symmetric key management name for opaque symmetric key handling"},
111 {"", OPT_CIPHER, '-', "Any supported cipher"},
112
113 OPT_R_OPTIONS,
114 OPT_PROV_OPTIONS,
115 {NULL}
116 };
117
enc_main(int argc,char ** argv)118 int enc_main(int argc, char **argv)
119 {
120 static char buf[128];
121 static const char magic[] = "Salted__";
122 ENGINE *e = NULL;
123 BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
124 NULL, *wbio = NULL;
125 EVP_CIPHER_CTX *ctx = NULL;
126 EVP_CIPHER *cipher = NULL;
127 EVP_MD *dgst = NULL;
128 const char *digestname = NULL;
129 char *hkey = NULL, *hiv = NULL, *hsalt = NULL, *p;
130 char *infile = NULL, *outfile = NULL, *prog;
131 char *str = NULL, *passarg = NULL, *pass = NULL, *strbuf = NULL;
132 const char *ciphername = NULL;
133 char mbuf[sizeof(magic) - 1];
134 OPTION_CHOICE o;
135 int bsize = BSIZE, verbose = 0, debug = 0, olb64 = 0, nosalt = 0;
136 int enc = 1, printkey = 0, i, k;
137 int base64 = 0, informat = FORMAT_BINARY, outformat = FORMAT_BINARY;
138 int ret = 1, inl, nopad = 0;
139 unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
140 int rawkey_set = 0;
141 unsigned char *buff = NULL, salt[EVP_MAX_IV_LENGTH];
142 int saltlen = 0;
143 int pbkdf2 = 0;
144 int iter = 0;
145 long n;
146 int streamable = 1;
147 int wrap = 0;
148 struct doall_enc_ciphers dec;
149 #ifndef OPENSSL_NO_ZLIB
150 int do_zlib = 0;
151 BIO *bzl = NULL;
152 #endif
153 int do_brotli = 0;
154 BIO *bbrot = NULL;
155 int do_zstd = 0;
156 BIO *bzstd = NULL;
157 STACK_OF(OPENSSL_STRING) *skeyopts = NULL;
158 const char *skeymgmt = NULL;
159 EVP_SKEY *skey = NULL;
160 EVP_SKEYMGMT *mgmt = NULL;
161
162 /* first check the command name */
163 if (strcmp(argv[0], "base64") == 0)
164 base64 = 1;
165 #ifndef OPENSSL_NO_ZLIB
166 else if (strcmp(argv[0], "zlib") == 0)
167 do_zlib = 1;
168 #endif
169 #ifndef OPENSSL_NO_BROTLI
170 else if (strcmp(argv[0], "brotli") == 0)
171 do_brotli = 1;
172 #endif
173 #ifndef OPENSSL_NO_ZSTD
174 else if (strcmp(argv[0], "zstd") == 0)
175 do_zstd = 1;
176 #endif
177 else if (strcmp(argv[0], "enc") != 0)
178 ciphername = argv[0];
179
180 opt_set_unknown_name("cipher");
181 prog = opt_init(argc, argv, enc_options);
182 while ((o = opt_next()) != OPT_EOF) {
183 switch (o) {
184 case OPT_EOF:
185 case OPT_ERR:
186 opthelp:
187 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
188 goto end;
189 case OPT_HELP:
190 opt_help(enc_options);
191 ret = 0;
192 goto end;
193 case OPT_LIST:
194 BIO_printf(bio_out, "Supported ciphers:\n");
195 dec.bio = bio_out;
196 dec.n = 0;
197 OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
198 show_ciphers, &dec);
199 BIO_printf(bio_out, "\n");
200 ret = 0;
201 goto end;
202 case OPT_E:
203 enc = 1;
204 break;
205 case OPT_IN:
206 infile = opt_arg();
207 break;
208 case OPT_OUT:
209 outfile = opt_arg();
210 break;
211 case OPT_PASS:
212 passarg = opt_arg();
213 break;
214 case OPT_ENGINE:
215 e = setup_engine(opt_arg(), 0);
216 break;
217 case OPT_D:
218 enc = 0;
219 break;
220 case OPT_P:
221 printkey = 1;
222 break;
223 case OPT_V:
224 verbose = 1;
225 break;
226 case OPT_NOPAD:
227 nopad = 1;
228 break;
229 case OPT_SALT:
230 nosalt = 0;
231 break;
232 case OPT_NOSALT:
233 nosalt = 1;
234 break;
235 case OPT_DEBUG:
236 debug = 1;
237 break;
238 case OPT_UPPER_P:
239 printkey = 2;
240 break;
241 case OPT_UPPER_A:
242 olb64 = 1;
243 break;
244 case OPT_A:
245 base64 = 1;
246 break;
247 case OPT_Z:
248 #ifndef OPENSSL_NO_ZLIB
249 do_zlib = 1;
250 #endif
251 break;
252 case OPT_BUFSIZE:
253 p = opt_arg();
254 i = (int)strlen(p) - 1;
255 k = i >= 1 && p[i] == 'k';
256 if (k)
257 p[i] = '\0';
258 if (!opt_long(opt_arg(), &n)
259 || n < 0 || (k && n >= LONG_MAX / 1024))
260 goto opthelp;
261 if (k)
262 n *= 1024;
263 bsize = (int)n;
264 break;
265 case OPT_K:
266 str = opt_arg();
267 break;
268 case OPT_KFILE:
269 in = bio_open_default(opt_arg(), 'r', FORMAT_TEXT);
270 if (in == NULL)
271 goto opthelp;
272 i = BIO_gets(in, buf, sizeof(buf));
273 BIO_free(in);
274 in = NULL;
275 if (i <= 0) {
276 BIO_printf(bio_err,
277 "%s Can't read key from %s\n", prog, opt_arg());
278 goto opthelp;
279 }
280 while (--i > 0 && (buf[i] == '\r' || buf[i] == '\n'))
281 buf[i] = '\0';
282 if (i <= 0) {
283 BIO_printf(bio_err, "%s: zero length password\n", prog);
284 goto opthelp;
285 }
286 str = buf;
287 break;
288 case OPT_UPPER_K:
289 hkey = opt_arg();
290 break;
291 case OPT_UPPER_S:
292 hsalt = opt_arg();
293 break;
294 case OPT_IV:
295 hiv = opt_arg();
296 break;
297 case OPT_MD:
298 digestname = opt_arg();
299 break;
300 case OPT_CIPHER:
301 ciphername = opt_unknown();
302 break;
303 case OPT_ITER:
304 iter = opt_int_arg();
305 pbkdf2 = 1;
306 break;
307 case OPT_SALTLEN:
308 if (!opt_int(opt_arg(), &saltlen))
309 goto opthelp;
310 if (saltlen > (int)sizeof(salt))
311 saltlen = (int)sizeof(salt);
312 break;
313 case OPT_PBKDF2:
314 pbkdf2 = 1;
315 if (iter == 0) /* do not overwrite a chosen value */
316 iter = PBKDF2_ITER_DEFAULT;
317 break;
318 case OPT_NONE:
319 cipher = NULL;
320 break;
321 case OPT_SKEYOPT:
322 if ((skeyopts == NULL &&
323 (skeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
324 sk_OPENSSL_STRING_push(skeyopts, opt_arg()) == 0) {
325 BIO_printf(bio_err, "%s: out of memory\n", prog);
326 goto end;
327 }
328 break;
329 case OPT_SKEYMGMT:
330 skeymgmt = opt_arg();
331 break;
332 case OPT_R_CASES:
333 if (!opt_rand(o))
334 goto end;
335 break;
336 case OPT_PROV_CASES:
337 if (!opt_provider(o))
338 goto end;
339 break;
340 }
341 }
342
343 /* No extra arguments. */
344 if (!opt_check_rest_arg(NULL))
345 goto opthelp;
346 if (!app_RAND_load())
347 goto end;
348 if (saltlen == 0 || pbkdf2 == 0)
349 saltlen = PKCS5_SALT_LEN;
350
351 /* Get the cipher name, either from progname (if set) or flag. */
352 if (!opt_cipher(ciphername, &cipher))
353 goto opthelp;
354 if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_WRAP_MODE)) {
355 wrap = 1;
356 streamable = 0;
357 }
358 if (digestname != NULL) {
359 if (!opt_md(digestname, &dgst))
360 goto opthelp;
361 }
362 if (dgst == NULL)
363 dgst = (EVP_MD *)EVP_sha256();
364
365 if (iter == 0)
366 iter = 1;
367
368 /* It must be large enough for a base64 encoded line */
369 if (base64 && bsize < 80)
370 bsize = 80;
371 if (verbose)
372 BIO_printf(bio_err, "bufsize=%d\n", bsize);
373
374 #ifndef OPENSSL_NO_ZLIB
375 if (do_zlib)
376 base64 = 0;
377 #endif
378 if (do_brotli)
379 base64 = 0;
380 if (do_zstd)
381 base64 = 0;
382
383 if (base64) {
384 if (enc)
385 outformat = FORMAT_BASE64;
386 else
387 informat = FORMAT_BASE64;
388 }
389
390 strbuf = app_malloc(SIZE, "strbuf");
391 buff = app_malloc(EVP_ENCODE_LENGTH(bsize), "evp buffer");
392
393 if (infile == NULL) {
394 if (!streamable && printkey != 2) { /* if just print key and exit, it's ok */
395 BIO_printf(bio_err, "Unstreamable cipher mode\n");
396 goto end;
397 }
398 in = dup_bio_in(informat);
399 } else {
400 in = bio_open_default(infile, 'r', informat);
401 }
402 if (in == NULL)
403 goto end;
404
405 if (str == NULL && passarg != NULL) {
406 if (!app_passwd(passarg, NULL, &pass, NULL)) {
407 BIO_printf(bio_err, "Error getting password\n");
408 goto end;
409 }
410 str = pass;
411 }
412
413 if ((str == NULL) && (cipher != NULL) && (hkey == NULL) && (skeyopts == NULL)) {
414 if (1) {
415 #ifndef OPENSSL_NO_UI_CONSOLE
416 for (;;) {
417 char prompt[200];
418
419 BIO_snprintf(prompt, sizeof(prompt), "enter %s %s password:",
420 EVP_CIPHER_get0_name(cipher),
421 (enc) ? "encryption" : "decryption");
422 strbuf[0] = '\0';
423 i = EVP_read_pw_string((char *)strbuf, SIZE, prompt, enc);
424 if (i == 0) {
425 if (strbuf[0] == '\0') {
426 ret = 1;
427 goto end;
428 }
429 str = strbuf;
430 break;
431 }
432 if (i < 0) {
433 BIO_printf(bio_err, "bad password read\n");
434 goto end;
435 }
436 }
437 } else {
438 #endif
439 BIO_printf(bio_err, "password required\n");
440 goto end;
441 }
442 }
443
444 out = bio_open_default(outfile, 'w', outformat);
445 if (out == NULL)
446 goto end;
447
448 if (debug) {
449 BIO_set_callback_ex(in, BIO_debug_callback_ex);
450 BIO_set_callback_ex(out, BIO_debug_callback_ex);
451 BIO_set_callback_arg(in, (char *)bio_err);
452 BIO_set_callback_arg(out, (char *)bio_err);
453 }
454
455 rbio = in;
456 wbio = out;
457
458 #ifndef OPENSSL_NO_COMP
459 # ifndef OPENSSL_NO_ZLIB
460 if (do_zlib) {
461 if ((bzl = BIO_new(BIO_f_zlib())) == NULL)
462 goto end;
463 if (debug) {
464 BIO_set_callback_ex(bzl, BIO_debug_callback_ex);
465 BIO_set_callback_arg(bzl, (char *)bio_err);
466 }
467 if (enc)
468 wbio = BIO_push(bzl, wbio);
469 else
470 rbio = BIO_push(bzl, rbio);
471 }
472 # endif
473
474 if (do_brotli) {
475 if ((bbrot = BIO_new(BIO_f_brotli())) == NULL)
476 goto end;
477 if (debug) {
478 BIO_set_callback_ex(bbrot, BIO_debug_callback_ex);
479 BIO_set_callback_arg(bbrot, (char *)bio_err);
480 }
481 if (enc)
482 wbio = BIO_push(bbrot, wbio);
483 else
484 rbio = BIO_push(bbrot, rbio);
485 }
486
487 if (do_zstd) {
488 if ((bzstd = BIO_new(BIO_f_zstd())) == NULL)
489 goto end;
490 if (debug) {
491 BIO_set_callback_ex(bzstd, BIO_debug_callback_ex);
492 BIO_set_callback_arg(bzstd, (char *)bio_err);
493 }
494 if (enc)
495 wbio = BIO_push(bzstd, wbio);
496 else
497 rbio = BIO_push(bzstd, rbio);
498 }
499 #endif
500
501 if (base64) {
502 if ((b64 = BIO_new(BIO_f_base64())) == NULL)
503 goto end;
504 if (debug) {
505 BIO_set_callback_ex(b64, BIO_debug_callback_ex);
506 BIO_set_callback_arg(b64, (char *)bio_err);
507 }
508 if (olb64)
509 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
510 if (enc)
511 wbio = BIO_push(b64, wbio);
512 else
513 rbio = BIO_push(b64, rbio);
514 }
515
516 if (cipher != NULL) {
517 if (str != NULL) { /* a passphrase is available */
518 /*
519 * Salt handling: if encrypting generate a salt if not supplied,
520 * and write to output BIO. If decrypting use salt from input BIO
521 * if not given with args
522 */
523 unsigned char *sptr;
524 size_t str_len = strlen(str);
525
526 if (nosalt) {
527 sptr = NULL;
528 } else {
529 if (hsalt != NULL && !set_hex(hsalt, salt, saltlen)) {
530 BIO_printf(bio_err, "invalid hex salt value\n");
531 goto end;
532 }
533 if (enc) { /* encryption */
534 if (hsalt == NULL) {
535 if (RAND_bytes(salt, saltlen) <= 0) {
536 BIO_printf(bio_err, "RAND_bytes failed\n");
537 goto end;
538 }
539 /*
540 * If -P option then don't bother writing.
541 * If salt is given, shouldn't either ?
542 */
543 if ((printkey != 2)
544 && (BIO_write(wbio, magic,
545 sizeof(magic) - 1) != sizeof(magic) - 1
546 || BIO_write(wbio,
547 (char *)salt,
548 saltlen) != saltlen)) {
549 BIO_printf(bio_err, "error writing output file\n");
550 goto end;
551 }
552 }
553 } else { /* decryption */
554 if (hsalt == NULL) {
555 if (BIO_read(rbio, mbuf, sizeof(mbuf)) != sizeof(mbuf)) {
556 BIO_printf(bio_err, "error reading input file\n");
557 goto end;
558 }
559 if (memcmp(mbuf, magic, sizeof(mbuf)) == 0) { /* file IS salted */
560 if (BIO_read(rbio, salt,
561 saltlen) != saltlen) {
562 BIO_printf(bio_err, "error reading input file\n");
563 goto end;
564 }
565 } else { /* file is NOT salted, NO salt available */
566 BIO_printf(bio_err, "bad magic number\n");
567 goto end;
568 }
569 }
570 }
571 sptr = salt;
572 }
573
574 if (pbkdf2 == 1) {
575 /*
576 * derive key and default iv
577 * concatenated into a temporary buffer
578 */
579 unsigned char tmpkeyiv[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH];
580 int iklen = EVP_CIPHER_get_key_length(cipher);
581 int ivlen = EVP_CIPHER_get_iv_length(cipher);
582 /* not needed if HASH_UPDATE() is fixed : */
583 int islen = (sptr != NULL ? saltlen : 0);
584
585 if (!PKCS5_PBKDF2_HMAC(str, str_len, sptr, islen,
586 iter, dgst, iklen+ivlen, tmpkeyiv)) {
587 BIO_printf(bio_err, "PKCS5_PBKDF2_HMAC failed\n");
588 goto end;
589 }
590 /* split and move data back to global buffer */
591 memcpy(key, tmpkeyiv, iklen);
592 memcpy(iv, tmpkeyiv+iklen, ivlen);
593 rawkey_set = 1;
594 } else {
595 BIO_printf(bio_err, "*** WARNING : "
596 "deprecated key derivation used.\n"
597 "Using -iter or -pbkdf2 would be better.\n");
598 if (!EVP_BytesToKey(cipher, dgst, sptr,
599 (unsigned char *)str, str_len,
600 1, key, iv)) {
601 BIO_printf(bio_err, "EVP_BytesToKey failed\n");
602 goto end;
603 }
604 rawkey_set = 1;
605 }
606 /*
607 * zero the complete buffer or the string passed from the command
608 * line.
609 */
610 if (str == strbuf)
611 OPENSSL_cleanse(str, SIZE);
612 else
613 OPENSSL_cleanse(str, str_len);
614 }
615 if (hiv != NULL) {
616 int siz = EVP_CIPHER_get_iv_length(cipher);
617
618 if (siz == 0) {
619 BIO_printf(bio_err, "warning: iv not used by this cipher\n");
620 } else if (!set_hex(hiv, iv, siz)) {
621 BIO_printf(bio_err, "invalid hex iv value\n");
622 goto end;
623 }
624 }
625 if ((hiv == NULL) && (str == NULL)
626 && EVP_CIPHER_get_iv_length(cipher) != 0
627 && wrap == 0) {
628 /*
629 * No IV was explicitly set and no IV was generated.
630 * Hence the IV is undefined, making correct decryption impossible.
631 */
632 BIO_printf(bio_err, "iv undefined\n");
633 goto end;
634 }
635 if (hkey != NULL) {
636 if (!set_hex(hkey, key, EVP_CIPHER_get_key_length(cipher))) {
637 BIO_printf(bio_err, "invalid hex key value\n");
638 goto end;
639 }
640 /* wiping secret data as we no longer need it */
641 cleanse(hkey);
642 rawkey_set = 1;
643 }
644
645 /*
646 * At this moment we know whether we trying to use raw bytes as the key
647 * or an opaque symmetric key. We do not allow both options simultaneously.
648 */
649 if (rawkey_set > 0 && skeyopts != NULL) {
650 BIO_printf(bio_err, "Either a raw key or the 'skeyopt' args must be used.\n");
651 goto end;
652 }
653
654 if ((benc = BIO_new(BIO_f_cipher())) == NULL)
655 goto end;
656
657 /*
658 * Since we may be changing parameters work on the encryption context
659 * rather than calling BIO_set_cipher().
660 */
661
662 BIO_get_cipher_ctx(benc, &ctx);
663
664 if (wrap == 1)
665 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
666
667 if (rawkey_set) {
668 if (!EVP_CipherInit_ex(ctx, cipher, e, key,
669 (hiv == NULL && wrap == 1 ? NULL : iv), enc)) {
670 BIO_printf(bio_err, "Error setting cipher %s\n",
671 EVP_CIPHER_get0_name(cipher));
672 ERR_print_errors(bio_err);
673 goto end;
674 }
675 } else {
676 OSSL_PARAM *params = NULL;
677
678 mgmt = EVP_SKEYMGMT_fetch(app_get0_libctx(),
679 skeymgmt != NULL ? skeymgmt : EVP_CIPHER_name(cipher),
680 app_get0_propq());
681 if (mgmt == NULL)
682 goto end;
683
684 params = app_params_new_from_opts(skeyopts,
685 EVP_SKEYMGMT_get0_imp_settable_params(mgmt));
686 if (params == NULL)
687 goto end;
688
689 skey = EVP_SKEY_import(app_get0_libctx(), EVP_SKEYMGMT_get0_name(mgmt),
690 app_get0_propq(), OSSL_SKEYMGMT_SELECT_ALL, params);
691 OSSL_PARAM_free(params);
692 if (skey == NULL) {
693 BIO_printf(bio_err, "Error creating opaque key object for skeymgmt %s\n",
694 skeymgmt ? skeymgmt : EVP_CIPHER_name(cipher));
695 ERR_print_errors(bio_err);
696 goto end;
697 }
698
699 if (!EVP_CipherInit_SKEY(ctx, cipher, skey,
700 (hiv == NULL && wrap == 1 ? NULL : iv),
701 EVP_CIPHER_get_iv_length(cipher), enc, NULL)) {
702 BIO_printf(bio_err, "Error setting an opaque key for cipher %s\n",
703 EVP_CIPHER_get0_name(cipher));
704 ERR_print_errors(bio_err);
705 goto end;
706 }
707 }
708
709 if (nopad)
710 EVP_CIPHER_CTX_set_padding(ctx, 0);
711
712 if (debug) {
713 BIO_set_callback_ex(benc, BIO_debug_callback_ex);
714 BIO_set_callback_arg(benc, (char *)bio_err);
715 }
716
717 if (printkey) {
718 if (!nosalt) {
719 printf("salt=");
720 for (i = 0; i < (int)saltlen; i++)
721 printf("%02X", salt[i]);
722 printf("\n");
723 }
724 if (EVP_CIPHER_get_key_length(cipher) > 0) {
725 printf("key=");
726 for (i = 0; i < EVP_CIPHER_get_key_length(cipher); i++)
727 printf("%02X", key[i]);
728 printf("\n");
729 }
730 if (EVP_CIPHER_get_iv_length(cipher) > 0) {
731 printf("iv =");
732 for (i = 0; i < EVP_CIPHER_get_iv_length(cipher); i++)
733 printf("%02X", iv[i]);
734 printf("\n");
735 }
736 if (printkey == 2) {
737 ret = 0;
738 goto end;
739 }
740 }
741 }
742
743 /* Only encrypt/decrypt as we write the file */
744 if (benc != NULL)
745 wbio = BIO_push(benc, wbio);
746
747 while (BIO_pending(rbio) || !BIO_eof(rbio)) {
748 inl = BIO_read(rbio, (char *)buff, bsize);
749 if (inl <= 0)
750 break;
751 if (!streamable && !BIO_eof(rbio)) { /* do not output data */
752 BIO_printf(bio_err, "Unstreamable cipher mode\n");
753 goto end;
754 }
755 if (BIO_write(wbio, (char *)buff, inl) != inl) {
756 BIO_printf(bio_err, "error writing output file\n");
757 goto end;
758 }
759 if (!streamable)
760 break;
761 }
762 if (!BIO_flush(wbio)) {
763 if (enc)
764 BIO_printf(bio_err, "bad encrypt\n");
765 else
766 BIO_printf(bio_err, "bad decrypt\n");
767 goto end;
768 }
769
770 ret = 0;
771 if (verbose) {
772 BIO_printf(bio_err, "bytes read : %8ju\n", BIO_number_read(in));
773 BIO_printf(bio_err, "bytes written: %8ju\n", BIO_number_written(out));
774 }
775 end:
776 ERR_print_errors(bio_err);
777 sk_OPENSSL_STRING_free(skeyopts);
778 EVP_SKEYMGMT_free(mgmt);
779 EVP_SKEY_free(skey);
780 OPENSSL_free(strbuf);
781 OPENSSL_free(buff);
782 BIO_free(in);
783 BIO_free_all(out);
784 BIO_free(benc);
785 BIO_free(b64);
786 EVP_MD_free(dgst);
787 EVP_CIPHER_free(cipher);
788 #ifndef OPENSSL_NO_ZLIB
789 BIO_free(bzl);
790 #endif
791 BIO_free(bbrot);
792 BIO_free(bzstd);
793 release_engine(e);
794 OPENSSL_free(pass);
795 return ret;
796 }
797
show_ciphers(const OBJ_NAME * name,void * arg)798 static void show_ciphers(const OBJ_NAME *name, void *arg)
799 {
800 struct doall_enc_ciphers *dec = (struct doall_enc_ciphers *)arg;
801 const EVP_CIPHER *cipher;
802
803 if (!islower((unsigned char)*name->name))
804 return;
805
806 /* Filter out ciphers that we cannot use */
807 cipher = EVP_get_cipherbyname(name->name);
808 if (cipher == NULL
809 || (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0
810 || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE)
811 return;
812
813 BIO_printf(dec->bio, "-%-25s", name->name);
814 if (++dec->n == 3) {
815 BIO_printf(dec->bio, "\n");
816 dec->n = 0;
817 } else
818 BIO_printf(dec->bio, " ");
819 }
820
set_hex(const char * in,unsigned char * out,int size)821 static int set_hex(const char *in, unsigned char *out, int size)
822 {
823 int i, n;
824 unsigned char j;
825
826 i = size * 2;
827 n = strlen(in);
828 if (n > i) {
829 BIO_printf(bio_err, "hex string is too long, ignoring excess\n");
830 n = i; /* ignore exceeding part */
831 } else if (n < i) {
832 BIO_printf(bio_err, "hex string is too short, padding with zero bytes to length\n");
833 }
834
835 memset(out, 0, size);
836 for (i = 0; i < n; i++) {
837 j = (unsigned char)*in++;
838 if (!isxdigit(j)) {
839 BIO_printf(bio_err, "non-hex digit\n");
840 return 0;
841 }
842 j = (unsigned char)OPENSSL_hexchar2int(j);
843 if (i & 1)
844 out[i / 2] |= j;
845 else
846 out[i / 2] = (j << 4);
847 }
848 return 1;
849 }
850