enc.c 22 KB

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