enc.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  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 <openssl/comp.h>
  70. #include <ctype.h>
  71. int set_hex(char *in,unsigned char *out,int size);
  72. #undef SIZE
  73. #undef BSIZE
  74. #undef PROG
  75. #define SIZE (512)
  76. #define BSIZE (8*1024)
  77. #define PROG enc_main
  78. static void show_ciphers(const OBJ_NAME *name,void *bio_)
  79. {
  80. BIO *bio=bio_;
  81. static int n;
  82. if(!islower((unsigned char)*name->name))
  83. return;
  84. BIO_printf(bio,"-%-25s",name->name);
  85. if(++n == 3)
  86. {
  87. BIO_printf(bio,"\n");
  88. n=0;
  89. }
  90. else
  91. BIO_printf(bio," ");
  92. }
  93. int MAIN(int, char **);
  94. int MAIN(int argc, char **argv)
  95. {
  96. #ifndef OPENSSL_NO_ENGINE
  97. ENGINE *e = NULL;
  98. #endif
  99. static const char magic[]="Salted__";
  100. char mbuf[sizeof magic-1];
  101. char *strbuf=NULL;
  102. unsigned char *buff=NULL,*bufsize=NULL;
  103. int bsize=BSIZE,verbose=0;
  104. int ret=1,inl;
  105. int nopad = 0;
  106. unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
  107. unsigned char salt[PKCS5_SALT_LEN];
  108. char *str=NULL, *passarg = NULL, *pass = NULL;
  109. char *hkey=NULL,*hiv=NULL,*hsalt = NULL;
  110. char *md=NULL;
  111. int enc=1,printkey=0,i,base64=0;
  112. #ifdef ZLIB
  113. int do_zlib=0;
  114. BIO *bzl = NULL;
  115. #endif
  116. int debug=0,olb64=0,nosalt=0;
  117. const EVP_CIPHER *cipher=NULL,*c;
  118. EVP_CIPHER_CTX *ctx = NULL;
  119. char *inf=NULL,*outf=NULL;
  120. BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL;
  121. #define PROG_NAME_SIZE 39
  122. char pname[PROG_NAME_SIZE+1];
  123. #ifndef OPENSSL_NO_ENGINE
  124. char *engine = NULL;
  125. #endif
  126. const EVP_MD *dgst=NULL;
  127. apps_startup();
  128. if (bio_err == NULL)
  129. if ((bio_err=BIO_new(BIO_s_file())) != NULL)
  130. BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
  131. if (!load_config(bio_err, NULL))
  132. goto end;
  133. /* first check the program name */
  134. program_name(argv[0],pname,sizeof pname);
  135. if (strcmp(pname,"base64") == 0)
  136. base64=1;
  137. #ifdef ZLIB
  138. if (strcmp(pname,"zlib") == 0)
  139. do_zlib=1;
  140. #endif
  141. cipher=EVP_get_cipherbyname(pname);
  142. #ifdef ZLIB
  143. if (!do_zlib && !base64 && (cipher == NULL)
  144. && (strcmp(pname,"enc") != 0))
  145. #else
  146. if (!base64 && (cipher == NULL) && (strcmp(pname,"enc") != 0))
  147. #endif
  148. {
  149. BIO_printf(bio_err,"%s is an unknown cipher\n",pname);
  150. goto bad;
  151. }
  152. argc--;
  153. argv++;
  154. while (argc >= 1)
  155. {
  156. if (strcmp(*argv,"-e") == 0)
  157. enc=1;
  158. else if (strcmp(*argv,"-in") == 0)
  159. {
  160. if (--argc < 1) goto bad;
  161. inf= *(++argv);
  162. }
  163. else if (strcmp(*argv,"-out") == 0)
  164. {
  165. if (--argc < 1) goto bad;
  166. outf= *(++argv);
  167. }
  168. else if (strcmp(*argv,"-pass") == 0)
  169. {
  170. if (--argc < 1) goto bad;
  171. passarg= *(++argv);
  172. }
  173. #ifndef OPENSSL_NO_ENGINE
  174. else if (strcmp(*argv,"-engine") == 0)
  175. {
  176. if (--argc < 1) goto bad;
  177. engine= *(++argv);
  178. }
  179. #endif
  180. else if (strcmp(*argv,"-d") == 0)
  181. enc=0;
  182. else if (strcmp(*argv,"-p") == 0)
  183. printkey=1;
  184. else if (strcmp(*argv,"-v") == 0)
  185. verbose=1;
  186. else if (strcmp(*argv,"-nopad") == 0)
  187. nopad=1;
  188. else if (strcmp(*argv,"-salt") == 0)
  189. nosalt=0;
  190. else if (strcmp(*argv,"-nosalt") == 0)
  191. nosalt=1;
  192. else if (strcmp(*argv,"-debug") == 0)
  193. debug=1;
  194. else if (strcmp(*argv,"-P") == 0)
  195. printkey=2;
  196. else if (strcmp(*argv,"-A") == 0)
  197. olb64=1;
  198. else if (strcmp(*argv,"-a") == 0)
  199. base64=1;
  200. else if (strcmp(*argv,"-base64") == 0)
  201. base64=1;
  202. #ifdef ZLIB
  203. else if (strcmp(*argv,"-z") == 0)
  204. do_zlib=1;
  205. #endif
  206. else if (strcmp(*argv,"-bufsize") == 0)
  207. {
  208. if (--argc < 1) goto bad;
  209. bufsize=(unsigned char *)*(++argv);
  210. }
  211. else if (strcmp(*argv,"-k") == 0)
  212. {
  213. if (--argc < 1) goto bad;
  214. str= *(++argv);
  215. }
  216. else if (strcmp(*argv,"-kfile") == 0)
  217. {
  218. static char buf[128];
  219. FILE *infile;
  220. char *file;
  221. if (--argc < 1) goto bad;
  222. file= *(++argv);
  223. infile=fopen(file,"r");
  224. if (infile == NULL)
  225. {
  226. BIO_printf(bio_err,"unable to read key from '%s'\n",
  227. file);
  228. goto bad;
  229. }
  230. buf[0]='\0';
  231. fgets(buf,sizeof buf,infile);
  232. fclose(infile);
  233. i=strlen(buf);
  234. if ((i > 0) &&
  235. ((buf[i-1] == '\n') || (buf[i-1] == '\r')))
  236. buf[--i]='\0';
  237. if ((i > 0) &&
  238. ((buf[i-1] == '\n') || (buf[i-1] == '\r')))
  239. buf[--i]='\0';
  240. if (i < 1)
  241. {
  242. BIO_printf(bio_err,"zero length password\n");
  243. goto bad;
  244. }
  245. str=buf;
  246. }
  247. else if (strcmp(*argv,"-K") == 0)
  248. {
  249. if (--argc < 1) goto bad;
  250. hkey= *(++argv);
  251. }
  252. else if (strcmp(*argv,"-S") == 0)
  253. {
  254. if (--argc < 1) goto bad;
  255. hsalt= *(++argv);
  256. }
  257. else if (strcmp(*argv,"-iv") == 0)
  258. {
  259. if (--argc < 1) goto bad;
  260. hiv= *(++argv);
  261. }
  262. else if (strcmp(*argv,"-md") == 0)
  263. {
  264. if (--argc < 1) goto bad;
  265. md= *(++argv);
  266. }
  267. else if ((argv[0][0] == '-') &&
  268. ((c=EVP_get_cipherbyname(&(argv[0][1]))) != NULL))
  269. {
  270. cipher=c;
  271. }
  272. else if (strcmp(*argv,"-none") == 0)
  273. cipher=NULL;
  274. else
  275. {
  276. BIO_printf(bio_err,"unknown option '%s'\n",*argv);
  277. bad:
  278. BIO_printf(bio_err,"options are\n");
  279. BIO_printf(bio_err,"%-14s input file\n","-in <file>");
  280. BIO_printf(bio_err,"%-14s output file\n","-out <file>");
  281. BIO_printf(bio_err,"%-14s pass phrase source\n","-pass <arg>");
  282. BIO_printf(bio_err,"%-14s encrypt\n","-e");
  283. BIO_printf(bio_err,"%-14s decrypt\n","-d");
  284. BIO_printf(bio_err,"%-14s base64 encode/decode, depending on encryption flag\n","-a/-base64");
  285. BIO_printf(bio_err,"%-14s passphrase is the next argument\n","-k");
  286. BIO_printf(bio_err,"%-14s passphrase is the first line of the file argument\n","-kfile");
  287. BIO_printf(bio_err,"%-14s the next argument is the md to use to create a key\n","-md");
  288. BIO_printf(bio_err,"%-14s from a passphrase. One of md2, md5, sha or sha1\n","");
  289. BIO_printf(bio_err,"%-14s salt in hex is the next argument\n","-S");
  290. BIO_printf(bio_err,"%-14s key/iv in hex is the next argument\n","-K/-iv");
  291. BIO_printf(bio_err,"%-14s print the iv/key (then exit if -P)\n","-[pP]");
  292. BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>");
  293. BIO_printf(bio_err,"%-14s disable standard block padding\n","-nopad");
  294. #ifndef OPENSSL_NO_ENGINE
  295. BIO_printf(bio_err,"%-14s use engine e, possibly a hardware device.\n","-engine e");
  296. #endif
  297. BIO_printf(bio_err,"Cipher Types\n");
  298. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
  299. show_ciphers,
  300. bio_err);
  301. BIO_printf(bio_err,"\n");
  302. goto end;
  303. }
  304. argc--;
  305. argv++;
  306. }
  307. #ifndef OPENSSL_NO_ENGINE
  308. e = setup_engine(bio_err, engine, 0);
  309. #endif
  310. if (md && (dgst=EVP_get_digestbyname(md)) == NULL)
  311. {
  312. BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
  313. goto end;
  314. }
  315. if (dgst == NULL)
  316. {
  317. dgst = EVP_md5();
  318. }
  319. if (bufsize != NULL)
  320. {
  321. unsigned long n;
  322. for (n=0; *bufsize; bufsize++)
  323. {
  324. i= *bufsize;
  325. if ((i <= '9') && (i >= '0'))
  326. n=n*10+i-'0';
  327. else if (i == 'k')
  328. {
  329. n*=1024;
  330. bufsize++;
  331. break;
  332. }
  333. }
  334. if (*bufsize != '\0')
  335. {
  336. BIO_printf(bio_err,"invalid 'bufsize' specified.\n");
  337. goto end;
  338. }
  339. /* It must be large enough for a base64 encoded line */
  340. if (base64 && n < 80) n=80;
  341. bsize=(int)n;
  342. if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize);
  343. }
  344. strbuf=OPENSSL_malloc(SIZE);
  345. buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
  346. if ((buff == NULL) || (strbuf == NULL))
  347. {
  348. BIO_printf(bio_err,"OPENSSL_malloc failure %ld\n",(long)EVP_ENCODE_LENGTH(bsize));
  349. goto end;
  350. }
  351. in=BIO_new(BIO_s_file());
  352. out=BIO_new(BIO_s_file());
  353. if ((in == NULL) || (out == NULL))
  354. {
  355. ERR_print_errors(bio_err);
  356. goto end;
  357. }
  358. if (debug)
  359. {
  360. BIO_set_callback(in,BIO_debug_callback);
  361. BIO_set_callback(out,BIO_debug_callback);
  362. BIO_set_callback_arg(in,(char *)bio_err);
  363. BIO_set_callback_arg(out,(char *)bio_err);
  364. }
  365. if (inf == NULL)
  366. {
  367. if (bufsize != NULL)
  368. setvbuf(stdin, (char *)NULL, _IONBF, 0);
  369. BIO_set_fp(in,stdin,BIO_NOCLOSE);
  370. }
  371. else
  372. {
  373. if (BIO_read_filename(in,inf) <= 0)
  374. {
  375. perror(inf);
  376. goto end;
  377. }
  378. }
  379. if(!str && passarg) {
  380. if(!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
  381. BIO_printf(bio_err, "Error getting password\n");
  382. goto end;
  383. }
  384. str = pass;
  385. }
  386. if ((str == NULL) && (cipher != NULL) && (hkey == NULL))
  387. {
  388. for (;;)
  389. {
  390. char buf[200];
  391. BIO_snprintf(buf,sizeof buf,"enter %s %s password:",
  392. OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
  393. (enc)?"encryption":"decryption");
  394. strbuf[0]='\0';
  395. i=EVP_read_pw_string((char *)strbuf,SIZE,buf,enc);
  396. if (i == 0)
  397. {
  398. if (strbuf[0] == '\0')
  399. {
  400. ret=1;
  401. goto end;
  402. }
  403. str=strbuf;
  404. break;
  405. }
  406. if (i < 0)
  407. {
  408. BIO_printf(bio_err,"bad password read\n");
  409. goto end;
  410. }
  411. }
  412. }
  413. if (outf == NULL)
  414. {
  415. BIO_set_fp(out,stdout,BIO_NOCLOSE);
  416. if (bufsize != NULL)
  417. setvbuf(stdout, (char *)NULL, _IONBF, 0);
  418. #ifdef OPENSSL_SYS_VMS
  419. {
  420. BIO *tmpbio = BIO_new(BIO_f_linebuffer());
  421. out = BIO_push(tmpbio, out);
  422. }
  423. #endif
  424. }
  425. else
  426. {
  427. if (BIO_write_filename(out,outf) <= 0)
  428. {
  429. perror(outf);
  430. goto end;
  431. }
  432. }
  433. rbio=in;
  434. wbio=out;
  435. #ifdef ZLIB
  436. if (do_zlib)
  437. {
  438. if ((bzl=BIO_new(BIO_f_zlib())) == NULL)
  439. goto end;
  440. if (enc)
  441. wbio=BIO_push(bzl,wbio);
  442. else
  443. rbio=BIO_push(bzl,rbio);
  444. }
  445. #endif
  446. if (base64)
  447. {
  448. if ((b64=BIO_new(BIO_f_base64())) == NULL)
  449. goto end;
  450. if (debug)
  451. {
  452. BIO_set_callback(b64,BIO_debug_callback);
  453. BIO_set_callback_arg(b64,(char *)bio_err);
  454. }
  455. if (olb64)
  456. BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
  457. if (enc)
  458. wbio=BIO_push(b64,wbio);
  459. else
  460. rbio=BIO_push(b64,rbio);
  461. }
  462. if (cipher != NULL)
  463. {
  464. /* Note that str is NULL if a key was passed on the command
  465. * line, so we get no salt in that case. Is this a bug?
  466. */
  467. if (str != NULL)
  468. {
  469. /* Salt handling: if encrypting generate a salt and
  470. * write to output BIO. If decrypting read salt from
  471. * input BIO.
  472. */
  473. unsigned char *sptr;
  474. if(nosalt) sptr = NULL;
  475. else {
  476. if(enc) {
  477. if(hsalt) {
  478. if(!set_hex(hsalt,salt,sizeof salt)) {
  479. BIO_printf(bio_err,
  480. "invalid hex salt value\n");
  481. goto end;
  482. }
  483. } else if (RAND_pseudo_bytes(salt, sizeof salt) < 0)
  484. goto end;
  485. /* If -P option then don't bother writing */
  486. if((printkey != 2)
  487. && (BIO_write(wbio,magic,
  488. sizeof magic-1) != sizeof magic-1
  489. || BIO_write(wbio,
  490. (char *)salt,
  491. sizeof salt) != sizeof salt)) {
  492. BIO_printf(bio_err,"error writing output file\n");
  493. goto end;
  494. }
  495. } else if(BIO_read(rbio,mbuf,sizeof mbuf) != sizeof mbuf
  496. || BIO_read(rbio,
  497. (unsigned char *)salt,
  498. sizeof salt) != sizeof salt) {
  499. BIO_printf(bio_err,"error reading input file\n");
  500. goto end;
  501. } else if(memcmp(mbuf,magic,sizeof magic-1)) {
  502. BIO_printf(bio_err,"bad magic number\n");
  503. goto end;
  504. }
  505. sptr = salt;
  506. }
  507. EVP_BytesToKey(cipher,dgst,sptr,
  508. (unsigned char *)str,
  509. strlen(str),1,key,iv);
  510. /* zero the complete buffer or the string
  511. * passed from the command line
  512. * bug picked up by
  513. * Larry J. Hughes Jr. <hughes@indiana.edu> */
  514. if (str == strbuf)
  515. OPENSSL_cleanse(str,SIZE);
  516. else
  517. OPENSSL_cleanse(str,strlen(str));
  518. }
  519. if ((hiv != NULL) && !set_hex(hiv,iv,sizeof iv))
  520. {
  521. BIO_printf(bio_err,"invalid hex iv value\n");
  522. goto end;
  523. }
  524. if ((hiv == NULL) && (str == NULL))
  525. {
  526. /* No IV was explicitly set and no IV was generated
  527. * during EVP_BytesToKey. Hence the IV is undefined,
  528. * making correct decryption impossible. */
  529. BIO_printf(bio_err, "iv undefined\n");
  530. goto end;
  531. }
  532. if ((hkey != NULL) && !set_hex(hkey,key,sizeof key))
  533. {
  534. BIO_printf(bio_err,"invalid hex key value\n");
  535. goto end;
  536. }
  537. if ((benc=BIO_new(BIO_f_cipher())) == NULL)
  538. goto end;
  539. /* Since we may be changing parameters work on the encryption
  540. * context rather than calling BIO_set_cipher().
  541. */
  542. BIO_get_cipher_ctx(benc, &ctx);
  543. if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc))
  544. {
  545. BIO_printf(bio_err, "Error setting cipher %s\n",
  546. EVP_CIPHER_name(cipher));
  547. ERR_print_errors(bio_err);
  548. goto end;
  549. }
  550. if (nopad)
  551. EVP_CIPHER_CTX_set_padding(ctx, 0);
  552. if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
  553. {
  554. BIO_printf(bio_err, "Error setting cipher %s\n",
  555. EVP_CIPHER_name(cipher));
  556. ERR_print_errors(bio_err);
  557. goto end;
  558. }
  559. if (debug)
  560. {
  561. BIO_set_callback(benc,BIO_debug_callback);
  562. BIO_set_callback_arg(benc,(char *)bio_err);
  563. }
  564. if (printkey)
  565. {
  566. if (!nosalt)
  567. {
  568. printf("salt=");
  569. for (i=0; i<(int)sizeof(salt); i++)
  570. printf("%02X",salt[i]);
  571. printf("\n");
  572. }
  573. if (cipher->key_len > 0)
  574. {
  575. printf("key=");
  576. for (i=0; i<cipher->key_len; i++)
  577. printf("%02X",key[i]);
  578. printf("\n");
  579. }
  580. if (cipher->iv_len > 0)
  581. {
  582. printf("iv =");
  583. for (i=0; i<cipher->iv_len; i++)
  584. printf("%02X",iv[i]);
  585. printf("\n");
  586. }
  587. if (printkey == 2)
  588. {
  589. ret=0;
  590. goto end;
  591. }
  592. }
  593. }
  594. /* Only encrypt/decrypt as we write the file */
  595. if (benc != NULL)
  596. wbio=BIO_push(benc,wbio);
  597. for (;;)
  598. {
  599. inl=BIO_read(rbio,(char *)buff,bsize);
  600. if (inl <= 0) break;
  601. if (BIO_write(wbio,(char *)buff,inl) != inl)
  602. {
  603. BIO_printf(bio_err,"error writing output file\n");
  604. goto end;
  605. }
  606. }
  607. if (!BIO_flush(wbio))
  608. {
  609. BIO_printf(bio_err,"bad decrypt\n");
  610. goto end;
  611. }
  612. ret=0;
  613. if (verbose)
  614. {
  615. BIO_printf(bio_err,"bytes read :%8ld\n",BIO_number_read(in));
  616. BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out));
  617. }
  618. end:
  619. ERR_print_errors(bio_err);
  620. if (strbuf != NULL) OPENSSL_free(strbuf);
  621. if (buff != NULL) OPENSSL_free(buff);
  622. if (in != NULL) BIO_free(in);
  623. if (out != NULL) BIO_free_all(out);
  624. if (benc != NULL) BIO_free(benc);
  625. if (b64 != NULL) BIO_free(b64);
  626. #ifdef ZLIB
  627. if (bzl != NULL) BIO_free(bzl);
  628. #endif
  629. if(pass) OPENSSL_free(pass);
  630. apps_shutdown();
  631. OPENSSL_EXIT(ret);
  632. }
  633. int set_hex(char *in, unsigned char *out, int size)
  634. {
  635. int i,n;
  636. unsigned char j;
  637. n=strlen(in);
  638. if (n > (size*2))
  639. {
  640. BIO_printf(bio_err,"hex string is too long\n");
  641. return(0);
  642. }
  643. memset(out,0,size);
  644. for (i=0; i<n; i++)
  645. {
  646. j=(unsigned char)*in;
  647. *(in++)='\0';
  648. if (j == 0) break;
  649. if ((j >= '0') && (j <= '9'))
  650. j-='0';
  651. else if ((j >= 'A') && (j <= 'F'))
  652. j=j-'A'+10;
  653. else if ((j >= 'a') && (j <= 'f'))
  654. j=j-'a'+10;
  655. else
  656. {
  657. BIO_printf(bio_err,"non-hex digit\n");
  658. return(0);
  659. }
  660. if (i&1)
  661. out[i/2]|=j;
  662. else
  663. out[i/2]=(j<<4);
  664. }
  665. return(1);
  666. }