enc.c 21 KB

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