xref: /onnv-gate/usr/src/common/openssl/apps/enc.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/enc.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate  * All rights reserved.
40Sstevel@tonic-gate  *
50Sstevel@tonic-gate  * This package is an SSL implementation written
60Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
110Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
130Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate  *
160Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate  * the code are not to be removed.
180Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate  * as the author of the parts of the library used.
200Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate  *
230Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate  * are met:
260Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate  *    must display the following acknowledgement:
330Sstevel@tonic-gate  *    "This product includes cryptographic software written by
340Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate  *    being used are not cryptographic related :-).
370Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate  *
410Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate  * SUCH DAMAGE.
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
550Sstevel@tonic-gate  * copied and put under another distribution licence
560Sstevel@tonic-gate  * [including the GNU Public Licence.]
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include <stdlib.h>
610Sstevel@tonic-gate #include <string.h>
620Sstevel@tonic-gate #include "apps.h"
630Sstevel@tonic-gate #include <openssl/bio.h>
640Sstevel@tonic-gate #include <openssl/err.h>
650Sstevel@tonic-gate #include <openssl/evp.h>
660Sstevel@tonic-gate #include <openssl/objects.h>
670Sstevel@tonic-gate #include <openssl/x509.h>
680Sstevel@tonic-gate #include <openssl/rand.h>
690Sstevel@tonic-gate #include <openssl/pem.h>
700Sstevel@tonic-gate #include <ctype.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int set_hex(char *in,unsigned char *out,int size);
730Sstevel@tonic-gate #undef SIZE
740Sstevel@tonic-gate #undef BSIZE
750Sstevel@tonic-gate #undef PROG
760Sstevel@tonic-gate 
770Sstevel@tonic-gate #define SIZE	(512)
780Sstevel@tonic-gate #define BSIZE	(8*1024)
790Sstevel@tonic-gate #define	PROG	enc_main
800Sstevel@tonic-gate 
show_ciphers(const OBJ_NAME * name,void * bio_)810Sstevel@tonic-gate static void show_ciphers(const OBJ_NAME *name,void *bio_)
820Sstevel@tonic-gate 	{
830Sstevel@tonic-gate 	BIO *bio=bio_;
840Sstevel@tonic-gate 	static int n;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if(!islower((unsigned char)*name->name))
870Sstevel@tonic-gate 		return;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	BIO_printf(bio,"-%-25s",name->name);
900Sstevel@tonic-gate 	if(++n == 3)
910Sstevel@tonic-gate 		{
920Sstevel@tonic-gate 		BIO_printf(bio,"\n");
930Sstevel@tonic-gate 		n=0;
940Sstevel@tonic-gate 		}
950Sstevel@tonic-gate 	else
960Sstevel@tonic-gate 		BIO_printf(bio," ");
970Sstevel@tonic-gate 	}
980Sstevel@tonic-gate 
990Sstevel@tonic-gate int MAIN(int, char **);
1000Sstevel@tonic-gate 
MAIN(int argc,char ** argv)1010Sstevel@tonic-gate int MAIN(int argc, char **argv)
1020Sstevel@tonic-gate 	{
1030Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1040Sstevel@tonic-gate 	ENGINE *e = NULL;
1050Sstevel@tonic-gate #endif
1060Sstevel@tonic-gate 	static const char magic[]="Salted__";
1070Sstevel@tonic-gate 	char mbuf[sizeof magic-1];
1080Sstevel@tonic-gate 	char *strbuf=NULL;
1090Sstevel@tonic-gate 	unsigned char *buff=NULL,*bufsize=NULL;
1100Sstevel@tonic-gate 	int bsize=BSIZE,verbose=0;
1110Sstevel@tonic-gate 	int ret=1,inl;
1120Sstevel@tonic-gate 	int nopad = 0;
1130Sstevel@tonic-gate 	unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH];
1140Sstevel@tonic-gate 	unsigned char salt[PKCS5_SALT_LEN];
1150Sstevel@tonic-gate 	char *str=NULL, *passarg = NULL, *pass = NULL;
1160Sstevel@tonic-gate 	char *hkey=NULL,*hiv=NULL,*hsalt = NULL;
117*2139Sjp161948 	char *md=NULL;
1180Sstevel@tonic-gate 	int enc=1,printkey=0,i,base64=0;
1190Sstevel@tonic-gate 	int debug=0,olb64=0,nosalt=0;
1200Sstevel@tonic-gate 	const EVP_CIPHER *cipher=NULL,*c;
121*2139Sjp161948 	EVP_CIPHER_CTX *ctx = NULL;
1220Sstevel@tonic-gate 	char *inf=NULL,*outf=NULL;
1230Sstevel@tonic-gate 	BIO *in=NULL,*out=NULL,*b64=NULL,*benc=NULL,*rbio=NULL,*wbio=NULL;
1240Sstevel@tonic-gate #define PROG_NAME_SIZE  39
1250Sstevel@tonic-gate 	char pname[PROG_NAME_SIZE+1];
1260Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1270Sstevel@tonic-gate 	char *engine = NULL;
1280Sstevel@tonic-gate #endif
129*2139Sjp161948 	const EVP_MD *dgst=NULL;
1300Sstevel@tonic-gate 
1310Sstevel@tonic-gate 	apps_startup();
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	if (bio_err == NULL)
1340Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
1350Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
1380Sstevel@tonic-gate 		goto end;
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	/* first check the program name */
1410Sstevel@tonic-gate 	program_name(argv[0],pname,sizeof pname);
1420Sstevel@tonic-gate 	if (strcmp(pname,"base64") == 0)
1430Sstevel@tonic-gate 		base64=1;
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	cipher=EVP_get_cipherbyname(pname);
1460Sstevel@tonic-gate 	if (!base64 && (cipher == NULL) && (strcmp(pname,"enc") != 0))
1470Sstevel@tonic-gate 		{
1480Sstevel@tonic-gate 		BIO_printf(bio_err,"%s is an unknown cipher\n",pname);
1490Sstevel@tonic-gate 		goto bad;
1500Sstevel@tonic-gate 		}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	argc--;
1530Sstevel@tonic-gate 	argv++;
1540Sstevel@tonic-gate 	while (argc >= 1)
1550Sstevel@tonic-gate 		{
1560Sstevel@tonic-gate 		if	(strcmp(*argv,"-e") == 0)
1570Sstevel@tonic-gate 			enc=1;
1580Sstevel@tonic-gate 		else if (strcmp(*argv,"-in") == 0)
1590Sstevel@tonic-gate 			{
1600Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1610Sstevel@tonic-gate 			inf= *(++argv);
1620Sstevel@tonic-gate 			}
1630Sstevel@tonic-gate 		else if (strcmp(*argv,"-out") == 0)
1640Sstevel@tonic-gate 			{
1650Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1660Sstevel@tonic-gate 			outf= *(++argv);
1670Sstevel@tonic-gate 			}
1680Sstevel@tonic-gate 		else if (strcmp(*argv,"-pass") == 0)
1690Sstevel@tonic-gate 			{
1700Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1710Sstevel@tonic-gate 			passarg= *(++argv);
1720Sstevel@tonic-gate 			}
1730Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1740Sstevel@tonic-gate 		else if (strcmp(*argv,"-engine") == 0)
1750Sstevel@tonic-gate 			{
1760Sstevel@tonic-gate 			if (--argc < 1) goto bad;
1770Sstevel@tonic-gate 			engine= *(++argv);
1780Sstevel@tonic-gate 			}
1790Sstevel@tonic-gate #endif
1800Sstevel@tonic-gate 		else if	(strcmp(*argv,"-d") == 0)
1810Sstevel@tonic-gate 			enc=0;
1820Sstevel@tonic-gate 		else if	(strcmp(*argv,"-p") == 0)
1830Sstevel@tonic-gate 			printkey=1;
1840Sstevel@tonic-gate 		else if	(strcmp(*argv,"-v") == 0)
1850Sstevel@tonic-gate 			verbose=1;
1860Sstevel@tonic-gate 		else if	(strcmp(*argv,"-nopad") == 0)
1870Sstevel@tonic-gate 			nopad=1;
1880Sstevel@tonic-gate 		else if	(strcmp(*argv,"-salt") == 0)
1890Sstevel@tonic-gate 			nosalt=0;
1900Sstevel@tonic-gate 		else if	(strcmp(*argv,"-nosalt") == 0)
1910Sstevel@tonic-gate 			nosalt=1;
1920Sstevel@tonic-gate 		else if	(strcmp(*argv,"-debug") == 0)
1930Sstevel@tonic-gate 			debug=1;
1940Sstevel@tonic-gate 		else if	(strcmp(*argv,"-P") == 0)
1950Sstevel@tonic-gate 			printkey=2;
1960Sstevel@tonic-gate 		else if	(strcmp(*argv,"-A") == 0)
1970Sstevel@tonic-gate 			olb64=1;
1980Sstevel@tonic-gate 		else if	(strcmp(*argv,"-a") == 0)
1990Sstevel@tonic-gate 			base64=1;
2000Sstevel@tonic-gate 		else if	(strcmp(*argv,"-base64") == 0)
2010Sstevel@tonic-gate 			base64=1;
2020Sstevel@tonic-gate 		else if (strcmp(*argv,"-bufsize") == 0)
2030Sstevel@tonic-gate 			{
2040Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2050Sstevel@tonic-gate 			bufsize=(unsigned char *)*(++argv);
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 		else if (strcmp(*argv,"-k") == 0)
2080Sstevel@tonic-gate 			{
2090Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2100Sstevel@tonic-gate 			str= *(++argv);
2110Sstevel@tonic-gate 			}
2120Sstevel@tonic-gate 		else if (strcmp(*argv,"-kfile") == 0)
2130Sstevel@tonic-gate 			{
2140Sstevel@tonic-gate 			static char buf[128];
2150Sstevel@tonic-gate 			FILE *infile;
2160Sstevel@tonic-gate 			char *file;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2190Sstevel@tonic-gate 			file= *(++argv);
2200Sstevel@tonic-gate 			infile=fopen(file,"r");
2210Sstevel@tonic-gate 			if (infile == NULL)
2220Sstevel@tonic-gate 				{
2230Sstevel@tonic-gate 				BIO_printf(bio_err,"unable to read key from '%s'\n",
2240Sstevel@tonic-gate 					file);
2250Sstevel@tonic-gate 				goto bad;
2260Sstevel@tonic-gate 				}
2270Sstevel@tonic-gate 			buf[0]='\0';
2280Sstevel@tonic-gate 			fgets(buf,sizeof buf,infile);
2290Sstevel@tonic-gate 			fclose(infile);
2300Sstevel@tonic-gate 			i=strlen(buf);
2310Sstevel@tonic-gate 			if ((i > 0) &&
2320Sstevel@tonic-gate 				((buf[i-1] == '\n') || (buf[i-1] == '\r')))
2330Sstevel@tonic-gate 				buf[--i]='\0';
2340Sstevel@tonic-gate 			if ((i > 0) &&
2350Sstevel@tonic-gate 				((buf[i-1] == '\n') || (buf[i-1] == '\r')))
2360Sstevel@tonic-gate 				buf[--i]='\0';
2370Sstevel@tonic-gate 			if (i < 1)
2380Sstevel@tonic-gate 				{
2390Sstevel@tonic-gate 				BIO_printf(bio_err,"zero length password\n");
2400Sstevel@tonic-gate 				goto bad;
2410Sstevel@tonic-gate 				}
2420Sstevel@tonic-gate 			str=buf;
2430Sstevel@tonic-gate 			}
2440Sstevel@tonic-gate 		else if (strcmp(*argv,"-K") == 0)
2450Sstevel@tonic-gate 			{
2460Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2470Sstevel@tonic-gate 			hkey= *(++argv);
2480Sstevel@tonic-gate 			}
2490Sstevel@tonic-gate 		else if (strcmp(*argv,"-S") == 0)
2500Sstevel@tonic-gate 			{
2510Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2520Sstevel@tonic-gate 			hsalt= *(++argv);
2530Sstevel@tonic-gate 			}
2540Sstevel@tonic-gate 		else if (strcmp(*argv,"-iv") == 0)
2550Sstevel@tonic-gate 			{
2560Sstevel@tonic-gate 			if (--argc < 1) goto bad;
2570Sstevel@tonic-gate 			hiv= *(++argv);
2580Sstevel@tonic-gate 			}
259*2139Sjp161948 		else if (strcmp(*argv,"-md") == 0)
260*2139Sjp161948 			{
261*2139Sjp161948 			if (--argc < 1) goto bad;
262*2139Sjp161948 			md= *(++argv);
263*2139Sjp161948 			}
2640Sstevel@tonic-gate 		else if	((argv[0][0] == '-') &&
2650Sstevel@tonic-gate 			((c=EVP_get_cipherbyname(&(argv[0][1]))) != NULL))
2660Sstevel@tonic-gate 			{
2670Sstevel@tonic-gate 			cipher=c;
2680Sstevel@tonic-gate 			}
2690Sstevel@tonic-gate 		else if (strcmp(*argv,"-none") == 0)
2700Sstevel@tonic-gate 			cipher=NULL;
2710Sstevel@tonic-gate 		else
2720Sstevel@tonic-gate 			{
2730Sstevel@tonic-gate 			BIO_printf(bio_err,"unknown option '%s'\n",*argv);
2740Sstevel@tonic-gate bad:
2750Sstevel@tonic-gate 			BIO_printf(bio_err,"options are\n");
2760Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s input file\n","-in <file>");
2770Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s output file\n","-out <file>");
2780Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s pass phrase source\n","-pass <arg>");
2790Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s encrypt\n","-e");
2800Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s decrypt\n","-d");
2810Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s base64 encode/decode, depending on encryption flag\n","-a/-base64");
282*2139Sjp161948 			BIO_printf(bio_err,"%-14s passphrase is the next argument\n","-k");
283*2139Sjp161948 			BIO_printf(bio_err,"%-14s passphrase is the first line of the file argument\n","-kfile");
284*2139Sjp161948 			BIO_printf(bio_err,"%-14s the next argument is the md to use to create a key\n","-md");
285*2139Sjp161948 			BIO_printf(bio_err,"%-14s   from a passphrase.  One of md2, md5, sha or sha1\n","");
2860Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s key/iv in hex is the next argument\n","-K/-iv");
2870Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s print the iv/key (then exit if -P)\n","-[pP]");
2880Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s buffer size\n","-bufsize <n>");
2890Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
2900Sstevel@tonic-gate 			BIO_printf(bio_err,"%-14s use engine e, possibly a hardware device.\n","-engine e");
2910Sstevel@tonic-gate #endif
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 			BIO_printf(bio_err,"Cipher Types\n");
2940Sstevel@tonic-gate 			OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH,
2950Sstevel@tonic-gate 					       show_ciphers,
2960Sstevel@tonic-gate 					       bio_err);
2970Sstevel@tonic-gate 			BIO_printf(bio_err,"\n");
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 			goto end;
3000Sstevel@tonic-gate 			}
3010Sstevel@tonic-gate 		argc--;
3020Sstevel@tonic-gate 		argv++;
3030Sstevel@tonic-gate 		}
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
3060Sstevel@tonic-gate         e = setup_engine(bio_err, engine, 0);
3070Sstevel@tonic-gate #endif
3080Sstevel@tonic-gate 
309*2139Sjp161948 	if (md && (dgst=EVP_get_digestbyname(md)) == NULL)
310*2139Sjp161948 		{
311*2139Sjp161948 		BIO_printf(bio_err,"%s is an unsupported message digest type\n",md);
312*2139Sjp161948 		goto end;
313*2139Sjp161948 		}
314*2139Sjp161948 
315*2139Sjp161948 	if (dgst == NULL)
316*2139Sjp161948 		{
317*2139Sjp161948 		dgst = EVP_md5();
318*2139Sjp161948 		}
319*2139Sjp161948 
3200Sstevel@tonic-gate 	if (bufsize != NULL)
3210Sstevel@tonic-gate 		{
3220Sstevel@tonic-gate 		unsigned long n;
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 		for (n=0; *bufsize; bufsize++)
3250Sstevel@tonic-gate 			{
3260Sstevel@tonic-gate 			i= *bufsize;
3270Sstevel@tonic-gate 			if ((i <= '9') && (i >= '0'))
3280Sstevel@tonic-gate 				n=n*10+i-'0';
3290Sstevel@tonic-gate 			else if (i == 'k')
3300Sstevel@tonic-gate 				{
3310Sstevel@tonic-gate 				n*=1024;
3320Sstevel@tonic-gate 				bufsize++;
3330Sstevel@tonic-gate 				break;
3340Sstevel@tonic-gate 				}
3350Sstevel@tonic-gate 			}
3360Sstevel@tonic-gate 		if (*bufsize != '\0')
3370Sstevel@tonic-gate 			{
3380Sstevel@tonic-gate 			BIO_printf(bio_err,"invalid 'bufsize' specified.\n");
3390Sstevel@tonic-gate 			goto end;
3400Sstevel@tonic-gate 			}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		/* It must be large enough for a base64 encoded line */
3430Sstevel@tonic-gate 		if (n < 80) n=80;
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 		bsize=(int)n;
3460Sstevel@tonic-gate 		if (verbose) BIO_printf(bio_err,"bufsize=%d\n",bsize);
3470Sstevel@tonic-gate 		}
3480Sstevel@tonic-gate 
3490Sstevel@tonic-gate 	strbuf=OPENSSL_malloc(SIZE);
3500Sstevel@tonic-gate 	buff=(unsigned char *)OPENSSL_malloc(EVP_ENCODE_LENGTH(bsize));
3510Sstevel@tonic-gate 	if ((buff == NULL) || (strbuf == NULL))
3520Sstevel@tonic-gate 		{
3530Sstevel@tonic-gate 		BIO_printf(bio_err,"OPENSSL_malloc failure %ld\n",(long)EVP_ENCODE_LENGTH(bsize));
3540Sstevel@tonic-gate 		goto end;
3550Sstevel@tonic-gate 		}
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	in=BIO_new(BIO_s_file());
3580Sstevel@tonic-gate 	out=BIO_new(BIO_s_file());
3590Sstevel@tonic-gate 	if ((in == NULL) || (out == NULL))
3600Sstevel@tonic-gate 		{
3610Sstevel@tonic-gate 		ERR_print_errors(bio_err);
3620Sstevel@tonic-gate 		goto end;
3630Sstevel@tonic-gate 		}
3640Sstevel@tonic-gate 	if (debug)
3650Sstevel@tonic-gate 		{
3660Sstevel@tonic-gate 		BIO_set_callback(in,BIO_debug_callback);
3670Sstevel@tonic-gate 		BIO_set_callback(out,BIO_debug_callback);
3680Sstevel@tonic-gate 		BIO_set_callback_arg(in,bio_err);
3690Sstevel@tonic-gate 		BIO_set_callback_arg(out,bio_err);
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	if (inf == NULL)
3730Sstevel@tonic-gate 		BIO_set_fp(in,stdin,BIO_NOCLOSE);
3740Sstevel@tonic-gate 	else
3750Sstevel@tonic-gate 		{
3760Sstevel@tonic-gate 		if (BIO_read_filename(in,inf) <= 0)
3770Sstevel@tonic-gate 			{
3780Sstevel@tonic-gate 			perror(inf);
3790Sstevel@tonic-gate 			goto end;
3800Sstevel@tonic-gate 			}
3810Sstevel@tonic-gate 		}
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if(!str && passarg) {
3840Sstevel@tonic-gate 		if(!app_passwd(bio_err, passarg, NULL, &pass, NULL)) {
3850Sstevel@tonic-gate 			BIO_printf(bio_err, "Error getting password\n");
3860Sstevel@tonic-gate 			goto end;
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 		str = pass;
3890Sstevel@tonic-gate 	}
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate 	if ((str == NULL) && (cipher != NULL) && (hkey == NULL))
3920Sstevel@tonic-gate 		{
3930Sstevel@tonic-gate 		for (;;)
3940Sstevel@tonic-gate 			{
3950Sstevel@tonic-gate 			char buf[200];
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 			BIO_snprintf(buf,sizeof buf,"enter %s %s password:",
3980Sstevel@tonic-gate 				     OBJ_nid2ln(EVP_CIPHER_nid(cipher)),
3990Sstevel@tonic-gate 				     (enc)?"encryption":"decryption");
4000Sstevel@tonic-gate 			strbuf[0]='\0';
4010Sstevel@tonic-gate 			i=EVP_read_pw_string((char *)strbuf,SIZE,buf,enc);
4020Sstevel@tonic-gate 			if (i == 0)
4030Sstevel@tonic-gate 				{
4040Sstevel@tonic-gate 				if (strbuf[0] == '\0')
4050Sstevel@tonic-gate 					{
4060Sstevel@tonic-gate 					ret=1;
4070Sstevel@tonic-gate 					goto end;
4080Sstevel@tonic-gate 					}
4090Sstevel@tonic-gate 				str=strbuf;
4100Sstevel@tonic-gate 				break;
4110Sstevel@tonic-gate 				}
4120Sstevel@tonic-gate 			if (i < 0)
4130Sstevel@tonic-gate 				{
4140Sstevel@tonic-gate 				BIO_printf(bio_err,"bad password read\n");
4150Sstevel@tonic-gate 				goto end;
4160Sstevel@tonic-gate 				}
4170Sstevel@tonic-gate 			}
4180Sstevel@tonic-gate 		}
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 	if (outf == NULL)
4220Sstevel@tonic-gate 		{
4230Sstevel@tonic-gate 		BIO_set_fp(out,stdout,BIO_NOCLOSE);
4240Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
4250Sstevel@tonic-gate 		{
4260Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
4270Sstevel@tonic-gate 		out = BIO_push(tmpbio, out);
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate #endif
4300Sstevel@tonic-gate 		}
4310Sstevel@tonic-gate 	else
4320Sstevel@tonic-gate 		{
4330Sstevel@tonic-gate 		if (BIO_write_filename(out,outf) <= 0)
4340Sstevel@tonic-gate 			{
4350Sstevel@tonic-gate 			perror(outf);
4360Sstevel@tonic-gate 			goto end;
4370Sstevel@tonic-gate 			}
4380Sstevel@tonic-gate 		}
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate 	rbio=in;
4410Sstevel@tonic-gate 	wbio=out;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 	if (base64)
4440Sstevel@tonic-gate 		{
4450Sstevel@tonic-gate 		if ((b64=BIO_new(BIO_f_base64())) == NULL)
4460Sstevel@tonic-gate 			goto end;
4470Sstevel@tonic-gate 		if (debug)
4480Sstevel@tonic-gate 			{
4490Sstevel@tonic-gate 			BIO_set_callback(b64,BIO_debug_callback);
4500Sstevel@tonic-gate 			BIO_set_callback_arg(b64,bio_err);
4510Sstevel@tonic-gate 			}
4520Sstevel@tonic-gate 		if (olb64)
4530Sstevel@tonic-gate 			BIO_set_flags(b64,BIO_FLAGS_BASE64_NO_NL);
4540Sstevel@tonic-gate 		if (enc)
4550Sstevel@tonic-gate 			wbio=BIO_push(b64,wbio);
4560Sstevel@tonic-gate 		else
4570Sstevel@tonic-gate 			rbio=BIO_push(b64,rbio);
4580Sstevel@tonic-gate 		}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	if (cipher != NULL)
4610Sstevel@tonic-gate 		{
4620Sstevel@tonic-gate 		/* Note that str is NULL if a key was passed on the command
4630Sstevel@tonic-gate 		 * line, so we get no salt in that case. Is this a bug?
4640Sstevel@tonic-gate 		 */
4650Sstevel@tonic-gate 		if (str != NULL)
4660Sstevel@tonic-gate 			{
4670Sstevel@tonic-gate 			/* Salt handling: if encrypting generate a salt and
4680Sstevel@tonic-gate 			 * write to output BIO. If decrypting read salt from
4690Sstevel@tonic-gate 			 * input BIO.
4700Sstevel@tonic-gate 			 */
4710Sstevel@tonic-gate 			unsigned char *sptr;
4720Sstevel@tonic-gate 			if(nosalt) sptr = NULL;
4730Sstevel@tonic-gate 			else {
4740Sstevel@tonic-gate 				if(enc) {
4750Sstevel@tonic-gate 					if(hsalt) {
4760Sstevel@tonic-gate 						if(!set_hex(hsalt,salt,sizeof salt)) {
4770Sstevel@tonic-gate 							BIO_printf(bio_err,
4780Sstevel@tonic-gate 								"invalid hex salt value\n");
4790Sstevel@tonic-gate 							goto end;
4800Sstevel@tonic-gate 						}
4810Sstevel@tonic-gate 					} else if (RAND_pseudo_bytes(salt, sizeof salt) < 0)
4820Sstevel@tonic-gate 						goto end;
4830Sstevel@tonic-gate 					/* If -P option then don't bother writing */
4840Sstevel@tonic-gate 					if((printkey != 2)
4850Sstevel@tonic-gate 					   && (BIO_write(wbio,magic,
4860Sstevel@tonic-gate 							 sizeof magic-1) != sizeof magic-1
4870Sstevel@tonic-gate 					       || BIO_write(wbio,
4880Sstevel@tonic-gate 							    (char *)salt,
4890Sstevel@tonic-gate 							    sizeof salt) != sizeof salt)) {
4900Sstevel@tonic-gate 						BIO_printf(bio_err,"error writing output file\n");
4910Sstevel@tonic-gate 						goto end;
4920Sstevel@tonic-gate 					}
4930Sstevel@tonic-gate 				} else if(BIO_read(rbio,mbuf,sizeof mbuf) != sizeof mbuf
4940Sstevel@tonic-gate 					  || BIO_read(rbio,
4950Sstevel@tonic-gate 						      (unsigned char *)salt,
4960Sstevel@tonic-gate 				    sizeof salt) != sizeof salt) {
4970Sstevel@tonic-gate 					BIO_printf(bio_err,"error reading input file\n");
4980Sstevel@tonic-gate 					goto end;
4990Sstevel@tonic-gate 				} else if(memcmp(mbuf,magic,sizeof magic-1)) {
5000Sstevel@tonic-gate 				    BIO_printf(bio_err,"bad magic number\n");
5010Sstevel@tonic-gate 				    goto end;
5020Sstevel@tonic-gate 				}
5030Sstevel@tonic-gate 
5040Sstevel@tonic-gate 				sptr = salt;
5050Sstevel@tonic-gate 			}
5060Sstevel@tonic-gate 
507*2139Sjp161948 			EVP_BytesToKey(cipher,dgst,sptr,
5080Sstevel@tonic-gate 				(unsigned char *)str,
5090Sstevel@tonic-gate 				strlen(str),1,key,iv);
5100Sstevel@tonic-gate 			/* zero the complete buffer or the string
5110Sstevel@tonic-gate 			 * passed from the command line
5120Sstevel@tonic-gate 			 * bug picked up by
5130Sstevel@tonic-gate 			 * Larry J. Hughes Jr. <hughes@indiana.edu> */
5140Sstevel@tonic-gate 			if (str == strbuf)
5150Sstevel@tonic-gate 				OPENSSL_cleanse(str,SIZE);
5160Sstevel@tonic-gate 			else
5170Sstevel@tonic-gate 				OPENSSL_cleanse(str,strlen(str));
5180Sstevel@tonic-gate 			}
5190Sstevel@tonic-gate 		if ((hiv != NULL) && !set_hex(hiv,iv,sizeof iv))
5200Sstevel@tonic-gate 			{
5210Sstevel@tonic-gate 			BIO_printf(bio_err,"invalid hex iv value\n");
5220Sstevel@tonic-gate 			goto end;
5230Sstevel@tonic-gate 			}
5240Sstevel@tonic-gate 		if ((hiv == NULL) && (str == NULL))
5250Sstevel@tonic-gate 			{
5260Sstevel@tonic-gate 			/* No IV was explicitly set and no IV was generated
5270Sstevel@tonic-gate 			 * during EVP_BytesToKey. Hence the IV is undefined,
5280Sstevel@tonic-gate 			 * making correct decryption impossible. */
5290Sstevel@tonic-gate 			BIO_printf(bio_err, "iv undefined\n");
5300Sstevel@tonic-gate 			goto end;
5310Sstevel@tonic-gate 			}
5320Sstevel@tonic-gate 		if ((hkey != NULL) && !set_hex(hkey,key,sizeof key))
5330Sstevel@tonic-gate 			{
5340Sstevel@tonic-gate 			BIO_printf(bio_err,"invalid hex key value\n");
5350Sstevel@tonic-gate 			goto end;
5360Sstevel@tonic-gate 			}
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate 		if ((benc=BIO_new(BIO_f_cipher())) == NULL)
5390Sstevel@tonic-gate 			goto end;
540*2139Sjp161948 
541*2139Sjp161948 		/* Since we may be changing parameters work on the encryption
542*2139Sjp161948 		 * context rather than calling BIO_set_cipher().
543*2139Sjp161948 		 */
544*2139Sjp161948 
545*2139Sjp161948 		BIO_get_cipher_ctx(benc, &ctx);
546*2139Sjp161948 		if (!EVP_CipherInit_ex(ctx, cipher, NULL, NULL, NULL, enc))
5470Sstevel@tonic-gate 			{
548*2139Sjp161948 			BIO_printf(bio_err, "Error setting cipher %s\n",
549*2139Sjp161948 				EVP_CIPHER_name(cipher));
550*2139Sjp161948 			ERR_print_errors(bio_err);
551*2139Sjp161948 			goto end;
552*2139Sjp161948 			}
553*2139Sjp161948 
554*2139Sjp161948 		if (nopad)
5550Sstevel@tonic-gate 			EVP_CIPHER_CTX_set_padding(ctx, 0);
556*2139Sjp161948 
557*2139Sjp161948 		if (!EVP_CipherInit_ex(ctx, NULL, NULL, key, iv, enc))
558*2139Sjp161948 			{
559*2139Sjp161948 			BIO_printf(bio_err, "Error setting cipher %s\n",
560*2139Sjp161948 				EVP_CIPHER_name(cipher));
561*2139Sjp161948 			ERR_print_errors(bio_err);
562*2139Sjp161948 			goto end;
5630Sstevel@tonic-gate 			}
564*2139Sjp161948 
5650Sstevel@tonic-gate 		if (debug)
5660Sstevel@tonic-gate 			{
5670Sstevel@tonic-gate 			BIO_set_callback(benc,BIO_debug_callback);
5680Sstevel@tonic-gate 			BIO_set_callback_arg(benc,bio_err);
5690Sstevel@tonic-gate 			}
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 		if (printkey)
5720Sstevel@tonic-gate 			{
5730Sstevel@tonic-gate 			if (!nosalt)
5740Sstevel@tonic-gate 				{
5750Sstevel@tonic-gate 				printf("salt=");
576*2139Sjp161948 				for (i=0; i<(int)sizeof(salt); i++)
5770Sstevel@tonic-gate 					printf("%02X",salt[i]);
5780Sstevel@tonic-gate 				printf("\n");
5790Sstevel@tonic-gate 				}
5800Sstevel@tonic-gate 			if (cipher->key_len > 0)
5810Sstevel@tonic-gate 				{
5820Sstevel@tonic-gate 				printf("key=");
5830Sstevel@tonic-gate 				for (i=0; i<cipher->key_len; i++)
5840Sstevel@tonic-gate 					printf("%02X",key[i]);
5850Sstevel@tonic-gate 				printf("\n");
5860Sstevel@tonic-gate 				}
5870Sstevel@tonic-gate 			if (cipher->iv_len > 0)
5880Sstevel@tonic-gate 				{
5890Sstevel@tonic-gate 				printf("iv =");
5900Sstevel@tonic-gate 				for (i=0; i<cipher->iv_len; i++)
5910Sstevel@tonic-gate 					printf("%02X",iv[i]);
5920Sstevel@tonic-gate 				printf("\n");
5930Sstevel@tonic-gate 				}
5940Sstevel@tonic-gate 			if (printkey == 2)
5950Sstevel@tonic-gate 				{
5960Sstevel@tonic-gate 				ret=0;
5970Sstevel@tonic-gate 				goto end;
5980Sstevel@tonic-gate 				}
5990Sstevel@tonic-gate 			}
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 	/* Only encrypt/decrypt as we write the file */
6030Sstevel@tonic-gate 	if (benc != NULL)
6040Sstevel@tonic-gate 		wbio=BIO_push(benc,wbio);
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	for (;;)
6070Sstevel@tonic-gate 		{
6080Sstevel@tonic-gate 		inl=BIO_read(rbio,(char *)buff,bsize);
6090Sstevel@tonic-gate 		if (inl <= 0) break;
6100Sstevel@tonic-gate 		if (BIO_write(wbio,(char *)buff,inl) != inl)
6110Sstevel@tonic-gate 			{
6120Sstevel@tonic-gate 			BIO_printf(bio_err,"error writing output file\n");
6130Sstevel@tonic-gate 			goto end;
6140Sstevel@tonic-gate 			}
6150Sstevel@tonic-gate 		}
6160Sstevel@tonic-gate 	if (!BIO_flush(wbio))
6170Sstevel@tonic-gate 		{
6180Sstevel@tonic-gate 		BIO_printf(bio_err,"bad decrypt\n");
6190Sstevel@tonic-gate 		goto end;
6200Sstevel@tonic-gate 		}
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate 	ret=0;
6230Sstevel@tonic-gate 	if (verbose)
6240Sstevel@tonic-gate 		{
6250Sstevel@tonic-gate 		BIO_printf(bio_err,"bytes read   :%8ld\n",BIO_number_read(in));
6260Sstevel@tonic-gate 		BIO_printf(bio_err,"bytes written:%8ld\n",BIO_number_written(out));
6270Sstevel@tonic-gate 		}
6280Sstevel@tonic-gate end:
6290Sstevel@tonic-gate 	ERR_print_errors(bio_err);
6300Sstevel@tonic-gate 	if (strbuf != NULL) OPENSSL_free(strbuf);
6310Sstevel@tonic-gate 	if (buff != NULL) OPENSSL_free(buff);
6320Sstevel@tonic-gate 	if (in != NULL) BIO_free(in);
6330Sstevel@tonic-gate 	if (out != NULL) BIO_free_all(out);
6340Sstevel@tonic-gate 	if (benc != NULL) BIO_free(benc);
6350Sstevel@tonic-gate 	if (b64 != NULL) BIO_free(b64);
6360Sstevel@tonic-gate 	if(pass) OPENSSL_free(pass);
6370Sstevel@tonic-gate 	apps_shutdown();
6380Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
6390Sstevel@tonic-gate 	}
6400Sstevel@tonic-gate 
set_hex(char * in,unsigned char * out,int size)6410Sstevel@tonic-gate int set_hex(char *in, unsigned char *out, int size)
6420Sstevel@tonic-gate 	{
6430Sstevel@tonic-gate 	int i,n;
6440Sstevel@tonic-gate 	unsigned char j;
6450Sstevel@tonic-gate 
6460Sstevel@tonic-gate 	n=strlen(in);
6470Sstevel@tonic-gate 	if (n > (size*2))
6480Sstevel@tonic-gate 		{
6490Sstevel@tonic-gate 		BIO_printf(bio_err,"hex string is too long\n");
6500Sstevel@tonic-gate 		return(0);
6510Sstevel@tonic-gate 		}
6520Sstevel@tonic-gate 	memset(out,0,size);
6530Sstevel@tonic-gate 	for (i=0; i<n; i++)
6540Sstevel@tonic-gate 		{
6550Sstevel@tonic-gate 		j=(unsigned char)*in;
6560Sstevel@tonic-gate 		*(in++)='\0';
6570Sstevel@tonic-gate 		if (j == 0) break;
6580Sstevel@tonic-gate 		if ((j >= '0') && (j <= '9'))
6590Sstevel@tonic-gate 			j-='0';
6600Sstevel@tonic-gate 		else if ((j >= 'A') && (j <= 'F'))
6610Sstevel@tonic-gate 			j=j-'A'+10;
6620Sstevel@tonic-gate 		else if ((j >= 'a') && (j <= 'f'))
6630Sstevel@tonic-gate 			j=j-'a'+10;
6640Sstevel@tonic-gate 		else
6650Sstevel@tonic-gate 			{
6660Sstevel@tonic-gate 			BIO_printf(bio_err,"non-hex digit\n");
6670Sstevel@tonic-gate 			return(0);
6680Sstevel@tonic-gate 			}
6690Sstevel@tonic-gate 		if (i&1)
6700Sstevel@tonic-gate 			out[i/2]|=j;
6710Sstevel@tonic-gate 		else
6720Sstevel@tonic-gate 			out[i/2]=(j<<4);
6730Sstevel@tonic-gate 		}
6740Sstevel@tonic-gate 	return(1);
6750Sstevel@tonic-gate 	}
676