enc.c 16 KB

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