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 if (n > INT_MAX)
264 goto opthelp;
265 bsize = (int)n;
266 break;
267 case OPT_K:
268 str = opt_arg();
269 break;
270 case OPT_KFILE:
271 in = bio_open_default(opt_arg(), 'r', FORMAT_TEXT);
272 if (in == NULL)
273 goto opthelp;
274 i = BIO_gets(in, buf, sizeof(buf));
275 BIO_free(in);
276 in = NULL;
277 if (i <= 0) {
278 BIO_printf(bio_err,
279 "%s Can't read key from %s\n", prog, opt_arg());
280 goto opthelp;
281 }
282 while (--i > 0 && (buf[i] == '\r' || buf[i] == '\n'))
283 buf[i] = '\0';
284 if (i <= 0) {
285 BIO_printf(bio_err, "%s: zero length password\n", prog);
286 goto opthelp;
287 }
288 str = buf;
289 break;
290 case OPT_UPPER_K:
291 hkey = opt_arg();
292 break;
293 case OPT_UPPER_S:
294 hsalt = opt_arg();
295 break;
296 case OPT_IV:
297 hiv = opt_arg();
298 break;
299 case OPT_MD:
300 digestname = opt_arg();
301 break;
302 case OPT_CIPHER:
303 ciphername = opt_unknown();
304 break;
305 case OPT_ITER:
306 iter = opt_int_arg();
307 pbkdf2 = 1;
308 break;
309 case OPT_SALTLEN:
310 if (!opt_int(opt_arg(), &saltlen))
311 goto opthelp;
312 if (saltlen > (int)sizeof(salt))
313 saltlen = (int)sizeof(salt);
314 break;
315 case OPT_PBKDF2:
316 pbkdf2 = 1;
317 if (iter == 0) /* do not overwrite a chosen value */
318 iter = PBKDF2_ITER_DEFAULT;
319 break;
320 case OPT_NONE:
321 cipher = NULL;
322 break;
323 case OPT_SKEYOPT:
324 if ((skeyopts == NULL &&
325 (skeyopts = sk_OPENSSL_STRING_new_null()) == NULL) ||
326 sk_OPENSSL_STRING_push(skeyopts, opt_arg()) == 0) {
327 BIO_printf(bio_err, "%s: out of memory\n", prog);
328 goto end;
329 }
330 break;
331 case OPT_SKEYMGMT:
332 skeymgmt = opt_arg();
333 break;
334 case OPT_R_CASES:
335 if (!opt_rand(o))
336 goto end;
337 break;
338 case OPT_PROV_CASES:
339 if (!opt_provider(o))
340 goto end;
341 break;
342 }
343 }
344
345 /* No extra arguments. */
346 if (!opt_check_rest_arg(NULL))
347 goto opthelp;
348 if (!app_RAND_load())
349 goto end;
350 if (saltlen == 0 || pbkdf2 == 0)
351 saltlen = PKCS5_SALT_LEN;
352
353 /* Get the cipher name, either from progname (if set) or flag. */
354 if (!opt_cipher(ciphername, &cipher))
355 goto opthelp;
356 if (cipher && (EVP_CIPHER_mode(cipher) == EVP_CIPH_WRAP_MODE)) {
357 wrap = 1;
358 streamable = 0;
359 }
360 if (digestname != NULL) {
361 if (!opt_md(digestname, &dgst))
362 goto opthelp;
363 }
364 if (dgst == NULL)
365 dgst = (EVP_MD *)EVP_sha256();
366
367 if (iter == 0)
368 iter = 1;
369
370 /* It must be large enough for a base64 encoded line */
371 if (base64 && bsize < 80)
372 bsize = 80;
373 if (verbose)
374 BIO_printf(bio_err, "bufsize=%d\n", bsize);
375
376 #ifndef OPENSSL_NO_ZLIB
377 if (do_zlib)
378 base64 = 0;
379 #endif
380 if (do_brotli)
381 base64 = 0;
382 if (do_zstd)
383 base64 = 0;
384
385 if (base64) {
386 if (enc)
387 outformat = FORMAT_BASE64;
388 else
389 informat = FORMAT_BASE64;
390 }
391
392 strbuf = app_malloc(SIZE, "strbuf");
393 buff = app_malloc(EVP_ENCODE_LENGTH(bsize), "evp buffer");
394
395 if (infile == NULL) {
396 if (!streamable && printkey != 2) { /* if just print key and exit, it's ok */
397 BIO_printf(bio_err, "Unstreamable cipher mode\n");
398 goto end;
399 }
400 in = dup_bio_in(informat);
401 } else {
402 in = bio_open_default(infile, 'r', informat);
403 }
404 if (in == NULL)
405 goto end;
406
407 if (str == NULL && passarg != NULL) {
408 if (!app_passwd(passarg, NULL, &pass, NULL)) {
409 BIO_printf(bio_err, "Error getting password\n");
410 goto end;
411 }
412 str = pass;
413 }
414
415 if ((str == NULL) && (cipher != NULL) && (hkey == NULL) && (skeyopts == NULL)) {
416 if (1) {
417 #ifndef OPENSSL_NO_UI_CONSOLE
418 for (;;) {
419 char prompt[200];
420
421 BIO_snprintf(prompt, sizeof(prompt), "enter %s %s password:",
422 EVP_CIPHER_get0_name(cipher),
423 (enc) ? "encryption" : "decryption");
424 strbuf[0] = '\0';
425 i = EVP_read_pw_string((char *)strbuf, SIZE, prompt, enc);
426 if (i == 0) {
427 if (strbuf[0] == '\0') {
428 ret = 1;
429 goto end;
430 }
431 str = strbuf;
432 break;
433 }
434 if (i < 0) {
435 BIO_printf(bio_err, "bad password read\n");
436 goto end;
437 }
438 }
439 } else {
440 #endif
441 BIO_printf(bio_err, "password required\n");
442 goto end;
443 }
444 }
445
446 out = bio_open_default(outfile, 'w', outformat);
447 if (out == NULL)
448 goto end;
449
450 if (debug) {
451 BIO_set_callback_ex(in, BIO_debug_callback_ex);
452 BIO_set_callback_ex(out, BIO_debug_callback_ex);
453 BIO_set_callback_arg(in, (char *)bio_err);
454 BIO_set_callback_arg(out, (char *)bio_err);
455 }
456
457 rbio = in;
458 wbio = out;
459
460 #ifndef OPENSSL_NO_COMP
461 # ifndef OPENSSL_NO_ZLIB
462 if (do_zlib) {
463 if ((bzl = BIO_new(BIO_f_zlib())) == NULL)
464 goto end;
465 if (debug) {
466 BIO_set_callback_ex(bzl, BIO_debug_callback_ex);
467 BIO_set_callback_arg(bzl, (char *)bio_err);
468 }
469 if (enc)
470 wbio = BIO_push(bzl, wbio);
471 else
472 rbio = BIO_push(bzl, rbio);
473 }
474 # endif
475
476 if (do_brotli) {
477 if ((bbrot = BIO_new(BIO_f_brotli())) == NULL)
478 goto end;
479 if (debug) {
480 BIO_set_callback_ex(bbrot, BIO_debug_callback_ex);
481 BIO_set_callback_arg(bbrot, (char *)bio_err);
482 }
483 if (enc)
484 wbio = BIO_push(bbrot, wbio);
485 else
486 rbio = BIO_push(bbrot, rbio);
487 }
488
489 if (do_zstd) {
490 if ((bzstd = BIO_new(BIO_f_zstd())) == NULL)
491 goto end;
492 if (debug) {
493 BIO_set_callback_ex(bzstd, BIO_debug_callback_ex);
494 BIO_set_callback_arg(bzstd, (char *)bio_err);
495 }
496 if (enc)
497 wbio = BIO_push(bzstd, wbio);
498 else
499 rbio = BIO_push(bzstd, rbio);
500 }
501 #endif
502
503 if (base64) {
504 if ((b64 = BIO_new(BIO_f_base64())) == NULL)
505 goto end;
506 if (debug) {
507 BIO_set_callback_ex(b64, BIO_debug_callback_ex);
508 BIO_set_callback_arg(b64, (char *)bio_err);
509 }
510 if (olb64)
511 BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
512 if (enc)
513 wbio = BIO_push(b64, wbio);
514 else
515 rbio = BIO_push(b64, rbio);
516 }
517
518 if (cipher != NULL) {
519 if (str != NULL) { /* a passphrase is available */
520 /*
521 * Salt handling: if encrypting generate a salt if not supplied,
522 * and write to output BIO. If decrypting use salt from input BIO
523 * if not given with args
524 */
525 unsigned char *sptr;
526 size_t str_len = strlen(str);
527
528 if (nosalt) {
529 sptr = NULL;
530 } else {
531 if (hsalt != NULL && !set_hex(hsalt, salt, saltlen)) {
532 BIO_printf(bio_err, "invalid hex salt value\n");
533 goto end;
534 }
535 if (enc) { /* encryption */
536 if (hsalt == NULL) {
537 if (RAND_bytes(salt, saltlen) <= 0) {
538 BIO_printf(bio_err, "RAND_bytes failed\n");
539 goto end;
540 }
541 /*
542 * If -P option then don't bother writing.
543 * If salt is given, shouldn't either ?
544 */
545 if ((printkey != 2)
546 && (BIO_write(wbio, magic,
547 sizeof(magic) - 1) != sizeof(magic) - 1
548 || BIO_write(wbio,
549 (char *)salt,
550 saltlen) != saltlen)) {
551 BIO_printf(bio_err, "error writing output file\n");
552 goto end;
553 }
554 }
555 } else { /* decryption */
556 if (hsalt == NULL) {
557 if (BIO_read(rbio, mbuf, sizeof(mbuf)) != sizeof(mbuf)) {
558 BIO_printf(bio_err, "error reading input file\n");
559 goto end;
560 }
561 if (memcmp(mbuf, magic, sizeof(mbuf)) == 0) { /* file IS salted */
562 if (BIO_read(rbio, salt,
563 saltlen) != saltlen) {
564 BIO_printf(bio_err, "error reading input file\n");
565 goto end;
566 }
567 } else { /* file is NOT salted, NO salt available */
568 BIO_printf(bio_err, "bad magic number\n");
569 goto end;
570 }
571 }
572 }
573 sptr = salt;
574 }
575
576 if (pbkdf2 == 1) {
577 /*
578 * derive key and default iv
579 * concatenated into a temporary buffer
580 */
581 unsigned char tmpkeyiv[EVP_MAX_KEY_LENGTH + EVP_MAX_IV_LENGTH];
582 int iklen = EVP_CIPHER_get_key_length(cipher);
583 int ivlen = EVP_CIPHER_get_iv_length(cipher);
584 /* not needed if HASH_UPDATE() is fixed : */
585 int islen = (sptr != NULL ? saltlen : 0);
586
587 if (!PKCS5_PBKDF2_HMAC(str, str_len, sptr, islen,
588 iter, dgst, iklen+ivlen, tmpkeyiv)) {
589 BIO_printf(bio_err, "PKCS5_PBKDF2_HMAC failed\n");
590 goto end;
591 }
592 /* split and move data back to global buffer */
593 memcpy(key, tmpkeyiv, iklen);
594 memcpy(iv, tmpkeyiv+iklen, ivlen);
595 rawkey_set = 1;
596 } else {
597 BIO_printf(bio_err, "*** WARNING : "
598 "deprecated key derivation used.\n"
599 "Using -iter or -pbkdf2 would be better.\n");
600 if (!EVP_BytesToKey(cipher, dgst, sptr,
601 (unsigned char *)str, str_len,
602 1, key, iv)) {
603 BIO_printf(bio_err, "EVP_BytesToKey failed\n");
604 goto end;
605 }
606 rawkey_set = 1;
607 }
608 /*
609 * zero the complete buffer or the string passed from the command
610 * line.
611 */
612 if (str == strbuf)
613 OPENSSL_cleanse(str, SIZE);
614 else
615 OPENSSL_cleanse(str, str_len);
616 }
617 if (hiv != NULL) {
618 int siz = EVP_CIPHER_get_iv_length(cipher);
619
620 if (siz == 0) {
621 BIO_printf(bio_err, "warning: iv not used by this cipher\n");
622 } else if (!set_hex(hiv, iv, siz)) {
623 BIO_printf(bio_err, "invalid hex iv value\n");
624 goto end;
625 }
626 }
627 if ((hiv == NULL) && (str == NULL)
628 && EVP_CIPHER_get_iv_length(cipher) != 0
629 && wrap == 0) {
630 /*
631 * No IV was explicitly set and no IV was generated.
632 * Hence the IV is undefined, making correct decryption impossible.
633 */
634 BIO_printf(bio_err, "iv undefined\n");
635 goto end;
636 }
637 if (hkey != NULL) {
638 if (!set_hex(hkey, key, EVP_CIPHER_get_key_length(cipher))) {
639 BIO_printf(bio_err, "invalid hex key value\n");
640 goto end;
641 }
642 /* wiping secret data as we no longer need it */
643 cleanse(hkey);
644 rawkey_set = 1;
645 }
646
647 /*
648 * At this moment we know whether we trying to use raw bytes as the key
649 * or an opaque symmetric key. We do not allow both options simultaneously.
650 */
651 if (rawkey_set > 0 && skeyopts != NULL) {
652 BIO_printf(bio_err, "Either a raw key or the 'skeyopt' args must be used.\n");
653 goto end;
654 }
655
656 if ((benc = BIO_new(BIO_f_cipher())) == NULL)
657 goto end;
658
659 /*
660 * Since we may be changing parameters work on the encryption context
661 * rather than calling BIO_set_cipher().
662 */
663
664 BIO_get_cipher_ctx(benc, &ctx);
665
666 if (wrap == 1)
667 EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
668
669 if (rawkey_set) {
670 if (!EVP_CipherInit_ex(ctx, cipher, e, key,
671 (hiv == NULL && wrap == 1 ? NULL : iv), enc)) {
672 BIO_printf(bio_err, "Error setting cipher %s\n",
673 EVP_CIPHER_get0_name(cipher));
674 ERR_print_errors(bio_err);
675 goto end;
676 }
677 } else {
678 OSSL_PARAM *params = NULL;
679
680 mgmt = EVP_SKEYMGMT_fetch(app_get0_libctx(),
681 skeymgmt != NULL ? skeymgmt : EVP_CIPHER_name(cipher),
682 app_get0_propq());
683 if (mgmt == NULL)
684 goto end;
685
686 params = app_params_new_from_opts(skeyopts,
687 EVP_SKEYMGMT_get0_imp_settable_params(mgmt));
688 if (params == NULL)
689 goto end;
690
691 skey = EVP_SKEY_import(app_get0_libctx(), EVP_SKEYMGMT_get0_name(mgmt),
692 app_get0_propq(), OSSL_SKEYMGMT_SELECT_ALL, params);
693 OSSL_PARAM_free(params);
694 if (skey == NULL) {
695 BIO_printf(bio_err, "Error creating opaque key object for skeymgmt %s\n",
696 skeymgmt ? skeymgmt : EVP_CIPHER_name(cipher));
697 ERR_print_errors(bio_err);
698 goto end;
699 }
700
701 if (!EVP_CipherInit_SKEY(ctx, cipher, skey,
702 (hiv == NULL && wrap == 1 ? NULL : iv),
703 EVP_CIPHER_get_iv_length(cipher), enc, NULL)) {
704 BIO_printf(bio_err, "Error setting an opaque key for cipher %s\n",
705 EVP_CIPHER_get0_name(cipher));
706 ERR_print_errors(bio_err);
707 goto end;
708 }
709 }
710
711 if (nopad)
712 EVP_CIPHER_CTX_set_padding(ctx, 0);
713
714 if (debug) {
715 BIO_set_callback_ex(benc, BIO_debug_callback_ex);
716 BIO_set_callback_arg(benc, (char *)bio_err);
717 }
718
719 if (printkey) {
720 if (!nosalt) {
721 printf("salt=");
722 for (i = 0; i < (int)saltlen; i++)
723 printf("%02X", salt[i]);
724 printf("\n");
725 }
726 if (EVP_CIPHER_get_key_length(cipher) > 0) {
727 printf("key=");
728 for (i = 0; i < EVP_CIPHER_get_key_length(cipher); i++)
729 printf("%02X", key[i]);
730 printf("\n");
731 }
732 if (EVP_CIPHER_get_iv_length(cipher) > 0) {
733 printf("iv =");
734 for (i = 0; i < EVP_CIPHER_get_iv_length(cipher); i++)
735 printf("%02X", iv[i]);
736 printf("\n");
737 }
738 if (printkey == 2) {
739 ret = 0;
740 goto end;
741 }
742 }
743 }
744
745 /* Only encrypt/decrypt as we write the file */
746 if (benc != NULL)
747 wbio = BIO_push(benc, wbio);
748
749 while (BIO_pending(rbio) || !BIO_eof(rbio)) {
750 inl = BIO_read(rbio, (char *)buff, bsize);
751 if (inl <= 0)
752 break;
753 if (!streamable && !BIO_eof(rbio)) { /* do not output data */
754 BIO_printf(bio_err, "Unstreamable cipher mode\n");
755 goto end;
756 }
757 if (BIO_write(wbio, (char *)buff, inl) != inl) {
758 BIO_printf(bio_err, "error writing output file\n");
759 goto end;
760 }
761 if (!streamable)
762 break;
763 }
764 if (!BIO_flush(wbio)) {
765 if (enc)
766 BIO_printf(bio_err, "bad encrypt\n");
767 else
768 BIO_printf(bio_err, "bad decrypt\n");
769 goto end;
770 }
771
772 ret = 0;
773 if (verbose) {
774 BIO_printf(bio_err, "bytes read : %8ju\n", BIO_number_read(in));
775 BIO_printf(bio_err, "bytes written: %8ju\n", BIO_number_written(out));
776 }
777 end:
778 ERR_print_errors(bio_err);
779 sk_OPENSSL_STRING_free(skeyopts);
780 EVP_SKEYMGMT_free(mgmt);
781 EVP_SKEY_free(skey);
782 OPENSSL_free(strbuf);
783 OPENSSL_free(buff);
784 BIO_free(in);
785 BIO_free_all(out);
786 BIO_free(benc);
787 BIO_free(b64);
788 EVP_MD_free(dgst);
789 EVP_CIPHER_free(cipher);
790 #ifndef OPENSSL_NO_ZLIB
791 BIO_free(bzl);
792 #endif
793 BIO_free(bbrot);
794 BIO_free(bzstd);
795 release_engine(e);
796 OPENSSL_free(pass);
797 return ret;
798 }
799
show_ciphers(const OBJ_NAME * name,void * arg)800 static void show_ciphers(const OBJ_NAME *name, void *arg)
801 {
802 struct doall_enc_ciphers *dec = (struct doall_enc_ciphers *)arg;
803 const EVP_CIPHER *cipher;
804
805 if (!islower((unsigned char)*name->name))
806 return;
807
808 /* Filter out ciphers that we cannot use */
809 cipher = EVP_get_cipherbyname(name->name);
810 if (cipher == NULL
811 || (EVP_CIPHER_get_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0
812 || EVP_CIPHER_get_mode(cipher) == EVP_CIPH_XTS_MODE)
813 return;
814
815 BIO_printf(dec->bio, "-%-25s", name->name);
816 if (++dec->n == 3) {
817 BIO_printf(dec->bio, "\n");
818 dec->n = 0;
819 } else
820 BIO_printf(dec->bio, " ");
821 }
822
set_hex(const char * in,unsigned char * out,int size)823 static int set_hex(const char *in, unsigned char *out, int size)
824 {
825 int i, n;
826 unsigned char j;
827
828 i = size * 2;
829 n = strlen(in);
830 if (n > i) {
831 BIO_printf(bio_err, "hex string is too long, ignoring excess\n");
832 n = i; /* ignore exceeding part */
833 } else if (n < i) {
834 BIO_printf(bio_err, "hex string is too short, padding with zero bytes to length\n");
835 }
836
837 memset(out, 0, size);
838 for (i = 0; i < n; i++) {
839 j = (unsigned char)*in++;
840 if (!isxdigit(j)) {
841 BIO_printf(bio_err, "non-hex digit\n");
842 return 0;
843 }
844 j = (unsigned char)OPENSSL_hexchar2int(j);
845 if (i & 1)
846 out[i / 2] |= j;
847 else
848 out[i / 2] = (j << 4);
849 }
850 return 1;
851 }
852