enc.c 21 KB

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