enc.c 17 KB

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