2
0

enc.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* apps/enc.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include "apps.h"
  62. #include <openssl/bio.h>
  63. #include <openssl/err.h>
  64. #include <openssl/evp.h>
  65. #include <openssl/objects.h>
  66. #include <openssl/x509.h>
  67. #include <openssl/rand.h>
  68. #include <openssl/pem.h>
  69. #include <ctype.h>
  70. int set_hex(char *in, unsigned char *out, int size);
  71. #undef SIZE
  72. #undef BSIZE
  73. #undef PROG
  74. #define SIZE (512)
  75. #define BSIZE (8*1024)
  76. #define PROG enc_main
  77. static void show_ciphers(const OBJ_NAME *name, void *bio_)
  78. {
  79. BIO *bio = bio_;
  80. static int n;
  81. if (!islower((unsigned char)*name->name))
  82. return;
  83. BIO_printf(bio, "-%-25s", name->name);
  84. if (++n == 3) {
  85. BIO_printf(bio, "\n");
  86. n = 0;
  87. } else
  88. BIO_printf(bio, " ");
  89. }
  90. int MAIN(int, char **);
  91. int MAIN(int argc, char **argv)
  92. {
  93. static const char magic[] = "Salted__";
  94. char mbuf[sizeof magic - 1];
  95. char *strbuf = NULL;
  96. unsigned char *buff = NULL, *bufsize = NULL;
  97. int bsize = BSIZE, verbose = 0;
  98. int ret = 1, inl;
  99. int nopad = 0;
  100. unsigned char key[EVP_MAX_KEY_LENGTH], iv[EVP_MAX_IV_LENGTH];
  101. unsigned char salt[PKCS5_SALT_LEN];
  102. char *str = NULL, *passarg = NULL, *pass = NULL;
  103. char *hkey = NULL, *hiv = NULL, *hsalt = NULL;
  104. char *md = NULL;
  105. int enc = 1, printkey = 0, i, base64 = 0;
  106. int debug = 0, olb64 = 0, nosalt = 0;
  107. const EVP_CIPHER *cipher = NULL, *c;
  108. EVP_CIPHER_CTX *ctx = NULL;
  109. char *inf = NULL, *outf = NULL;
  110. BIO *in = NULL, *out = NULL, *b64 = NULL, *benc = NULL, *rbio =
  111. NULL, *wbio = NULL;
  112. #define PROG_NAME_SIZE 39
  113. char pname[PROG_NAME_SIZE + 1];
  114. #ifndef OPENSSL_NO_ENGINE
  115. char *engine = NULL;
  116. #endif
  117. const EVP_MD *dgst = NULL;
  118. int non_fips_allow = 0;
  119. apps_startup();
  120. if (bio_err == NULL)
  121. if ((bio_err = BIO_new(BIO_s_file())) != NULL)
  122. BIO_set_fp(bio_err, stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  123. if (!load_config(bio_err, NULL))
  124. goto end;
  125. /* first check the program name */
  126. program_name(argv[0], pname, sizeof pname);
  127. if (strcmp(pname, "base64") == 0)
  128. base64 = 1;
  129. cipher = EVP_get_cipherbyname(pname);
  130. if (!base64 && (cipher == NULL) && (strcmp(pname, "enc") != 0)) {
  131. BIO_printf(bio_err, "%s is an unknown cipher\n", pname);
  132. goto bad;
  133. }
  134. argc--;
  135. argv++;
  136. while (argc >= 1) {
  137. if (strcmp(*argv, "-e") == 0)
  138. enc = 1;
  139. else if (strcmp(*argv, "-in") == 0) {
  140. if (--argc < 1)
  141. goto bad;
  142. inf = *(++argv);
  143. } else if (strcmp(*argv, "-out") == 0) {
  144. if (--argc < 1)
  145. goto bad;
  146. outf = *(++argv);
  147. } else if (strcmp(*argv, "-pass") == 0) {
  148. if (--argc < 1)
  149. goto bad;
  150. passarg = *(++argv);
  151. }
  152. #ifndef OPENSSL_NO_ENGINE
  153. else if (strcmp(*argv, "-engine") == 0) {
  154. if (--argc < 1)
  155. goto bad;
  156. engine = *(++argv);
  157. }
  158. #endif
  159. else if (strcmp(*argv, "-d") == 0)
  160. enc = 0;
  161. else if (strcmp(*argv, "-p") == 0)
  162. printkey = 1;
  163. else if (strcmp(*argv, "-v") == 0)
  164. verbose = 1;
  165. else if (strcmp(*argv, "-nopad") == 0)
  166. nopad = 1;
  167. else if (strcmp(*argv, "-salt") == 0)
  168. nosalt = 0;
  169. else if (strcmp(*argv, "-nosalt") == 0)
  170. nosalt = 1;
  171. else if (strcmp(*argv, "-debug") == 0)
  172. debug = 1;
  173. else if (strcmp(*argv, "-P") == 0)
  174. printkey = 2;
  175. else if (strcmp(*argv, "-A") == 0)
  176. olb64 = 1;
  177. else if (strcmp(*argv, "-a") == 0)
  178. base64 = 1;
  179. else if (strcmp(*argv, "-base64") == 0)
  180. base64 = 1;
  181. else if (strcmp(*argv, "-bufsize") == 0) {
  182. if (--argc < 1)
  183. goto bad;
  184. bufsize = (unsigned char *)*(++argv);
  185. } else if (strcmp(*argv, "-k") == 0) {
  186. if (--argc < 1)
  187. goto bad;
  188. str = *(++argv);
  189. } else if (strcmp(*argv, "-kfile") == 0) {
  190. static char buf[128];
  191. FILE *infile;
  192. char *file;
  193. if (--argc < 1)
  194. goto bad;
  195. file = *(++argv);
  196. infile = fopen(file, "r");
  197. if (infile == NULL) {
  198. BIO_printf(bio_err, "unable to read key from '%s'\n", file);
  199. goto bad;
  200. }
  201. buf[0] = '\0';
  202. if (!fgets(buf, sizeof buf, infile)) {
  203. BIO_printf(bio_err, "unable to read key from '%s'\n", file);
  204. goto bad;
  205. }
  206. fclose(infile);
  207. i = strlen(buf);
  208. if ((i > 0) && ((buf[i - 1] == '\n') || (buf[i - 1] == '\r')))
  209. buf[--i] = '\0';
  210. if ((i > 0) && ((buf[i - 1] == '\n') || (buf[i - 1] == '\r')))
  211. buf[--i] = '\0';
  212. if (i < 1) {
  213. BIO_printf(bio_err, "zero length password\n");
  214. goto bad;
  215. }
  216. str = buf;
  217. } else if (strcmp(*argv, "-K") == 0) {
  218. if (--argc < 1)
  219. goto bad;
  220. hkey = *(++argv);
  221. } else if (strcmp(*argv, "-S") == 0) {
  222. if (--argc < 1)
  223. goto bad;
  224. hsalt = *(++argv);
  225. } else if (strcmp(*argv, "-iv") == 0) {
  226. if (--argc < 1)
  227. goto bad;
  228. hiv = *(++argv);
  229. } else if (strcmp(*argv, "-md") == 0) {
  230. if (--argc < 1)
  231. goto bad;
  232. md = *(++argv);
  233. } else if (strcmp(*argv, "-non-fips-allow") == 0)
  234. non_fips_allow = 1;
  235. else if ((argv[0][0] == '-') &&
  236. ((c = EVP_get_cipherbyname(&(argv[0][1]))) != NULL)) {
  237. cipher = c;
  238. } else if (strcmp(*argv, "-none") == 0)
  239. cipher = NULL;
  240. else {
  241. BIO_printf(bio_err, "unknown option '%s'\n", *argv);
  242. bad:
  243. BIO_printf(bio_err, "options are\n");
  244. BIO_printf(bio_err, "%-14s input file\n", "-in <file>");
  245. BIO_printf(bio_err, "%-14s output file\n", "-out <file>");
  246. BIO_printf(bio_err, "%-14s pass phrase source\n", "-pass <arg>");
  247. BIO_printf(bio_err, "%-14s encrypt\n", "-e");
  248. BIO_printf(bio_err, "%-14s decrypt\n", "-d");
  249. BIO_printf(bio_err,
  250. "%-14s base64 encode/decode, depending on encryption flag\n",
  251. "-a/-base64");
  252. BIO_printf(bio_err, "%-14s passphrase is the next argument\n",
  253. "-k");
  254. BIO_printf(bio_err,
  255. "%-14s passphrase is the first line of the file argument\n",
  256. "-kfile");
  257. BIO_printf(bio_err,
  258. "%-14s the next argument is the md to use to create a key\n",
  259. "-md");
  260. BIO_printf(bio_err,
  261. "%-14s from a passphrase. One of md2, md5, sha or sha1\n",
  262. "");
  263. BIO_printf(bio_err, "%-14s key/iv in hex is the next argument\n",
  264. "-K/-iv");
  265. BIO_printf(bio_err, "%-14s print the iv/key (then exit if -P)\n",
  266. "-[pP]");
  267. BIO_printf(bio_err, "%-14s buffer size\n", "-bufsize <n>");
  268. #ifndef OPENSSL_NO_ENGINE
  269. BIO_printf(bio_err,
  270. "%-14s use engine e, possibly a hardware device.\n",
  271. "-engine e");
  272. #endif
  273. BIO_printf(bio_err, "Cipher Types\n");
  274. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
  275. show_ciphers, bio_err);
  276. BIO_printf(bio_err, "\n");
  277. goto end;
  278. }
  279. argc--;
  280. argv++;
  281. }
  282. #ifndef OPENSSL_NO_ENGINE
  283. setup_engine(bio_err, engine, 0);
  284. #endif
  285. if (md && (dgst = EVP_get_digestbyname(md)) == NULL) {
  286. BIO_printf(bio_err, "%s is an unsupported message digest type\n", md);
  287. goto end;
  288. }
  289. if (dgst == NULL) {
  290. if (in_FIPS_mode)
  291. dgst = EVP_sha1();
  292. else
  293. dgst = EVP_md5();
  294. }
  295. if (bufsize != NULL) {
  296. unsigned long n;
  297. for (n = 0; *bufsize; bufsize++) {
  298. i = *bufsize;
  299. if ((i <= '9') && (i >= '0'))
  300. n = n * 10 + i - '0';
  301. else if (i == 'k') {
  302. n *= 1024;
  303. bufsize++;
  304. break;
  305. }
  306. }
  307. if (*bufsize != '\0') {
  308. BIO_printf(bio_err, "invalid 'bufsize' specified.\n");
  309. goto end;
  310. }
  311. /* It must be large enough for a base64 encoded line */
  312. if (base64 && n < 80)
  313. n = 80;
  314. bsize = (int)n;
  315. if (verbose)
  316. BIO_printf(bio_err, "bufsize=%d\n", bsize);
  317. }
  318. strbuf = OPENSSL_malloc(SIZE);
  319. buff = (unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
  320. if ((buff == NULL) || (strbuf == NULL)) {
  321. BIO_printf(bio_err, "OPENSSL_malloc failure %ld\n",
  322. (long)EVP_ENCODE_LENGTH(bsize));
  323. goto end;
  324. }
  325. in = BIO_new(BIO_s_file());
  326. out = BIO_new(BIO_s_file());
  327. if ((in == NULL) || (out == NULL)) {
  328. ERR_print_errors(bio_err);
  329. goto end;
  330. }
  331. if (debug) {
  332. BIO_set_callback(in, BIO_debug_callback);
  333. BIO_set_callback(out, BIO_debug_callback);
  334. BIO_set_callback_arg(in, (char *)bio_err);
  335. BIO_set_callback_arg(out, (char *)bio_err);
  336. }
  337. if (inf == NULL) {
  338. if (bufsize != NULL)
  339. setvbuf(stdin, (char *)NULL, _IONBF, 0);
  340. BIO_set_fp(in, stdin, BIO_NOCLOSE);
  341. } else {
  342. if (BIO_read_filename(in, inf) <= 0) {
  343. perror(inf);
  344. goto end;
  345. }
  346. }
  347. if (!str && passarg) {
  348. if (!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
  349. BIO_printf(bio_err, "Error getting password\n");
  350. goto end;
  351. }
  352. str = pass;
  353. }
  354. if ((str == NULL) && (cipher != NULL) && (hkey == NULL)) {
  355. for (;;) {
  356. char buf[200];
  357. BIO_snprintf(buf, sizeof buf, "enter %s %s password:",
  358. OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
  359. (enc) ? "encryption" : "decryption");
  360. strbuf[0] = '\0';
  361. i = EVP_read_pw_string((char *)strbuf, SIZE, buf, enc);
  362. if (i == 0) {
  363. if (strbuf[0] == '\0') {
  364. ret = 1;
  365. goto end;
  366. }
  367. str = strbuf;
  368. break;
  369. }
  370. if (i < 0) {
  371. BIO_printf(bio_err, "bad password read\n");
  372. goto end;
  373. }
  374. }
  375. }
  376. if (outf == NULL) {
  377. BIO_set_fp(out, stdout, BIO_NOCLOSE);
  378. if (bufsize != NULL)
  379. setvbuf(stdout, (char *)NULL, _IONBF, 0);
  380. #ifdef OPENSSL_SYS_VMS
  381. {
  382. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  383. out = BIO_push(tmpbio, out);
  384. }
  385. #endif
  386. } else {
  387. if (BIO_write_filename(out, outf) <= 0) {
  388. perror(outf);
  389. goto end;
  390. }
  391. }
  392. rbio = in;
  393. wbio = out;
  394. if (base64) {
  395. if ((b64 = BIO_new(BIO_f_base64())) == NULL)
  396. goto end;
  397. if (debug) {
  398. BIO_set_callback(b64, BIO_debug_callback);
  399. BIO_set_callback_arg(b64, (char *)bio_err);
  400. }
  401. if (olb64)
  402. BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL);
  403. if (enc)
  404. wbio = BIO_push(b64, wbio);
  405. else
  406. rbio = BIO_push(b64, rbio);
  407. }
  408. if (cipher != NULL) {
  409. /*
  410. * Note that str is NULL if a key was passed on the command line, so
  411. * we get no salt in that case. Is this a bug?
  412. */
  413. if (str != NULL) {
  414. /*
  415. * Salt handling: if encrypting generate a salt and write to
  416. * output BIO. If decrypting read salt from input BIO.
  417. */
  418. unsigned char *sptr;
  419. if (nosalt)
  420. sptr = NULL;
  421. else {
  422. if (enc) {
  423. if (hsalt) {
  424. if (!set_hex(hsalt, salt, sizeof salt)) {
  425. BIO_printf(bio_err, "invalid hex salt value\n");
  426. goto end;
  427. }
  428. } else if (RAND_pseudo_bytes(salt, sizeof salt) < 0)
  429. goto end;
  430. /*
  431. * If -P option then don't bother writing
  432. */
  433. if ((printkey != 2)
  434. && (BIO_write(wbio, magic,
  435. sizeof magic - 1) != sizeof magic - 1
  436. || BIO_write(wbio,
  437. (char *)salt,
  438. sizeof salt) != sizeof salt)) {
  439. BIO_printf(bio_err, "error writing output file\n");
  440. goto end;
  441. }
  442. } else if (BIO_read(rbio, mbuf, sizeof mbuf) != sizeof mbuf
  443. || BIO_read(rbio,
  444. (unsigned char *)salt,
  445. sizeof salt) != sizeof salt) {
  446. BIO_printf(bio_err, "error reading input file\n");
  447. goto end;
  448. } else if (memcmp(mbuf, magic, sizeof magic - 1)) {
  449. BIO_printf(bio_err, "bad magic number\n");
  450. goto end;
  451. }
  452. sptr = salt;
  453. }
  454. EVP_BytesToKey(cipher, dgst, sptr,
  455. (unsigned char *)str, strlen(str), 1, key, iv);
  456. /*
  457. * zero the complete buffer or the string passed from the command
  458. * line bug picked up by Larry J. Hughes Jr. <hughes@indiana.edu>
  459. */
  460. if (str == strbuf)
  461. OPENSSL_cleanse(str, SIZE);
  462. else
  463. OPENSSL_cleanse(str, strlen(str));
  464. }
  465. if ((hiv != NULL) && !set_hex(hiv, iv, sizeof iv)) {
  466. BIO_printf(bio_err, "invalid hex iv value\n");
  467. goto end;
  468. }
  469. if ((hiv == NULL) && (str == NULL)
  470. && EVP_CIPHER_iv_length(cipher) != 0) {
  471. /*
  472. * No IV was explicitly set and no IV was generated during
  473. * EVP_BytesToKey. Hence the IV is undefined, making correct
  474. * decryption impossible.
  475. */
  476. BIO_printf(bio_err, "iv undefined\n");
  477. goto end;
  478. }
  479. if ((hkey != NULL) && !set_hex(hkey, key, sizeof key)) {
  480. BIO_printf(bio_err, "invalid hex key value\n");
  481. goto end;
  482. }
  483. if ((benc = BIO_new(BIO_f_cipher())) == NULL)
  484. goto end;
  485. /*
  486. * Since we may be changing parameters work on the encryption context
  487. * rather than calling BIO_set_cipher().
  488. */
  489. BIO_get_cipher_ctx(benc, &ctx);
  490. if (non_fips_allow)
  491. EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPH_FLAG_NON_FIPS_ALLOW);
  492. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc)) {
  493. BIO_printf(bio_err, "Error setting cipher %s\n",
  494. EVP_CIPHER_name(cipher));
  495. ERR_print_errors(bio_err);
  496. goto end;
  497. }
  498. if (nopad)
  499. EVP_CIPHER_CTX_set_padding(ctx, 0);
  500. if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc)) {
  501. BIO_printf(bio_err, "Error setting cipher %s\n",
  502. EVP_CIPHER_name(cipher));
  503. ERR_print_errors(bio_err);
  504. goto end;
  505. }
  506. if (debug) {
  507. BIO_set_callback(benc, BIO_debug_callback);
  508. BIO_set_callback_arg(benc, (char *)bio_err);
  509. }
  510. if (printkey) {
  511. if (!nosalt) {
  512. printf("salt=");
  513. for (i = 0; i < (int)sizeof(salt); i++)
  514. printf("%02X", salt[i]);
  515. printf("\n");
  516. }
  517. if (cipher->key_len > 0) {
  518. printf("key=");
  519. for (i = 0; i < cipher->key_len; i++)
  520. printf("%02X", key[i]);
  521. printf("\n");
  522. }
  523. if (cipher->iv_len > 0) {
  524. printf("iv =");
  525. for (i = 0; i < cipher->iv_len; i++)
  526. printf("%02X", iv[i]);
  527. printf("\n");
  528. }
  529. if (printkey == 2) {
  530. ret = 0;
  531. goto end;
  532. }
  533. }
  534. }
  535. /* Only encrypt/decrypt as we write the file */
  536. if (benc != NULL)
  537. wbio = BIO_push(benc, wbio);
  538. for (;;) {
  539. inl = BIO_read(rbio, (char *)buff, bsize);
  540. if (inl <= 0)
  541. break;
  542. if (BIO_write(wbio, (char *)buff, inl) != inl) {
  543. BIO_printf(bio_err, "error writing output file\n");
  544. goto end;
  545. }
  546. }
  547. if (!BIO_flush(wbio)) {
  548. BIO_printf(bio_err, "bad decrypt\n");
  549. goto end;
  550. }
  551. ret = 0;
  552. if (verbose) {
  553. BIO_printf(bio_err, "bytes read :%8ld\n", BIO_number_read(in));
  554. BIO_printf(bio_err, "bytes written:%8ld\n", BIO_number_written(out));
  555. }
  556. end:
  557. ERR_print_errors(bio_err);
  558. if (strbuf != NULL)
  559. OPENSSL_free(strbuf);
  560. if (buff != NULL)
  561. OPENSSL_free(buff);
  562. if (in != NULL)
  563. BIO_free(in);
  564. if (out != NULL)
  565. BIO_free_all(out);
  566. if (benc != NULL)
  567. BIO_free(benc);
  568. if (b64 != NULL)
  569. BIO_free(b64);
  570. if (pass)
  571. OPENSSL_free(pass);
  572. apps_shutdown();
  573. OPENSSL_EXIT(ret);
  574. }
  575. int set_hex(char *in, unsigned char *out, int size)
  576. {
  577. int i, n;
  578. unsigned char j;
  579. n = strlen(in);
  580. if (n > (size * 2)) {
  581. BIO_printf(bio_err, "hex string is too long\n");
  582. return (0);
  583. }
  584. memset(out, 0, size);
  585. for (i = 0; i < n; i++) {
  586. j = (unsigned char)*in;
  587. *(in++) = '\0';
  588. if (j == 0)
  589. break;
  590. if ((j >= '0') && (j <= '9'))
  591. j -= '0';
  592. else if ((j >= 'A') && (j <= 'F'))
  593. j = j - 'A' + 10;
  594. else if ((j >= 'a') && (j <= 'f'))
  595. j = j - 'a' + 10;
  596. else {
  597. BIO_printf(bio_err, "non-hex digit\n");
  598. return (0);
  599. }
  600. if (i & 1)
  601. out[i / 2] |= j;
  602. else
  603. out[i / 2] = (j << 4);
  604. }
  605. return (1);
  606. }