enc.c 24 KB

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