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