2
0

enc.c 21 KB

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