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