enc.c 24 KB

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