2
0

enc.c 21 KB

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