xref: /onnv-gate/usr/src/common/openssl/apps/openssl.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/openssl.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  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
600Sstevel@tonic-gate  *
610Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
620Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
630Sstevel@tonic-gate  * are met:
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
660Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
670Sstevel@tonic-gate  *
680Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
690Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
700Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
710Sstevel@tonic-gate  *    distribution.
720Sstevel@tonic-gate  *
730Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
740Sstevel@tonic-gate  *    software must display the following acknowledgment:
750Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
760Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
790Sstevel@tonic-gate  *    endorse or promote products derived from this software without
800Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
810Sstevel@tonic-gate  *    openssl-core@openssl.org.
820Sstevel@tonic-gate  *
830Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
840Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
850Sstevel@tonic-gate  *    permission of the OpenSSL Project.
860Sstevel@tonic-gate  *
870Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
880Sstevel@tonic-gate  *    acknowledgment:
890Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
900Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
930Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
940Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
950Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
960Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
970Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
980Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
990Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1000Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1010Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1020Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1030Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
1040Sstevel@tonic-gate  * ====================================================================
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
1070Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
1080Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
1090Sstevel@tonic-gate  *
1100Sstevel@tonic-gate  */
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #include <stdio.h>
1140Sstevel@tonic-gate #include <string.h>
1150Sstevel@tonic-gate #include <stdlib.h>
1160Sstevel@tonic-gate #define OPENSSL_C /* tells apps.h to use complete apps_startup() */
1170Sstevel@tonic-gate #include "apps.h"
1180Sstevel@tonic-gate #include <openssl/bio.h>
1190Sstevel@tonic-gate #include <openssl/crypto.h>
1200Sstevel@tonic-gate #include <openssl/lhash.h>
1210Sstevel@tonic-gate #include <openssl/conf.h>
1220Sstevel@tonic-gate #include <openssl/x509.h>
1230Sstevel@tonic-gate #include <openssl/pem.h>
1240Sstevel@tonic-gate #include <openssl/ssl.h>
1250Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
1260Sstevel@tonic-gate #include <openssl/engine.h>
1270Sstevel@tonic-gate #endif
1280Sstevel@tonic-gate #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
1290Sstevel@tonic-gate #include "progs.h"
1300Sstevel@tonic-gate #include "s_apps.h"
1310Sstevel@tonic-gate #include <openssl/err.h>
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
1340Sstevel@tonic-gate  * base prototypes (we cast each variable inside the function to the required
1350Sstevel@tonic-gate  * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
1360Sstevel@tonic-gate  * functions. */
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
1390Sstevel@tonic-gate static unsigned long MS_CALLBACK hash(const void *a_void);
1400Sstevel@tonic-gate /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
1410Sstevel@tonic-gate static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
1420Sstevel@tonic-gate static LHASH *prog_init(void );
1430Sstevel@tonic-gate static int do_cmd(LHASH *prog,int argc,char *argv[]);
1440Sstevel@tonic-gate char *default_config_file=NULL;
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate /* Make sure there is only one when MONOLITH is defined */
1470Sstevel@tonic-gate #ifdef MONOLITH
1480Sstevel@tonic-gate CONF *config=NULL;
1490Sstevel@tonic-gate BIO *bio_err=NULL;
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 
lock_dbg_cb(int mode,int type,const char * file,int line)1530Sstevel@tonic-gate static void lock_dbg_cb(int mode, int type, const char *file, int line)
1540Sstevel@tonic-gate 	{
1550Sstevel@tonic-gate 	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
1560Sstevel@tonic-gate 	const char *errstr = NULL;
1570Sstevel@tonic-gate 	int rw;
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
1600Sstevel@tonic-gate 	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
1610Sstevel@tonic-gate 		{
1620Sstevel@tonic-gate 		errstr = "invalid mode";
1630Sstevel@tonic-gate 		goto err;
1640Sstevel@tonic-gate 		}
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate 	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
1670Sstevel@tonic-gate 		{
1680Sstevel@tonic-gate 		errstr = "type out of bounds";
1690Sstevel@tonic-gate 		goto err;
1700Sstevel@tonic-gate 		}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	if (mode & CRYPTO_LOCK)
1730Sstevel@tonic-gate 		{
1740Sstevel@tonic-gate 		if (modes[type])
1750Sstevel@tonic-gate 			{
1760Sstevel@tonic-gate 			errstr = "already locked";
1770Sstevel@tonic-gate 			/* must not happen in a single-threaded program
1780Sstevel@tonic-gate 			 * (would deadlock) */
1790Sstevel@tonic-gate 			goto err;
1800Sstevel@tonic-gate 			}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 		modes[type] = rw;
1830Sstevel@tonic-gate 		}
1840Sstevel@tonic-gate 	else if (mode & CRYPTO_UNLOCK)
1850Sstevel@tonic-gate 		{
1860Sstevel@tonic-gate 		if (!modes[type])
1870Sstevel@tonic-gate 			{
1880Sstevel@tonic-gate 			errstr = "not locked";
1890Sstevel@tonic-gate 			goto err;
1900Sstevel@tonic-gate 			}
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate 		if (modes[type] != rw)
1930Sstevel@tonic-gate 			{
1940Sstevel@tonic-gate 			errstr = (rw == CRYPTO_READ) ?
1950Sstevel@tonic-gate 				"CRYPTO_r_unlock on write lock" :
1960Sstevel@tonic-gate 				"CRYPTO_w_unlock on read lock";
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 		modes[type] = 0;
2000Sstevel@tonic-gate 		}
2010Sstevel@tonic-gate 	else
2020Sstevel@tonic-gate 		{
2030Sstevel@tonic-gate 		errstr = "invalid mode";
2040Sstevel@tonic-gate 		goto err;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate  err:
2080Sstevel@tonic-gate 	if (errstr)
2090Sstevel@tonic-gate 		{
2100Sstevel@tonic-gate 		/* we cannot use bio_err here */
2110Sstevel@tonic-gate 		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
2120Sstevel@tonic-gate 			errstr, mode, type, file, line);
2130Sstevel@tonic-gate 		}
2140Sstevel@tonic-gate 	}
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 
main(int Argc,char * Argv[])2170Sstevel@tonic-gate int main(int Argc, char *Argv[])
2180Sstevel@tonic-gate 	{
2190Sstevel@tonic-gate 	ARGS arg;
2200Sstevel@tonic-gate #define PROG_NAME_SIZE	39
2210Sstevel@tonic-gate 	char pname[PROG_NAME_SIZE+1];
2220Sstevel@tonic-gate 	FUNCTION f,*fp;
223*2139Sjp161948 	MS_STATIC const char *prompt;
224*2139Sjp161948 	MS_STATIC char buf[1024];
2250Sstevel@tonic-gate 	char *to_free=NULL;
2260Sstevel@tonic-gate 	int n,i,ret=0;
2270Sstevel@tonic-gate 	int argc;
2280Sstevel@tonic-gate 	char **argv,*p;
2290Sstevel@tonic-gate 	LHASH *prog=NULL;
2300Sstevel@tonic-gate 	long errline;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	arg.data=NULL;
2330Sstevel@tonic-gate 	arg.count=0;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (bio_err == NULL)
2360Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
2370Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
2400Sstevel@tonic-gate 		{
2410Sstevel@tonic-gate 		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
2420Sstevel@tonic-gate 			{
2430Sstevel@tonic-gate 			CRYPTO_malloc_debug_init();
2440Sstevel@tonic-gate 			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
2450Sstevel@tonic-gate 			}
2460Sstevel@tonic-gate 		else
2470Sstevel@tonic-gate 			{
2480Sstevel@tonic-gate 			/* OPENSSL_DEBUG_MEMORY=off */
2490Sstevel@tonic-gate 			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
2500Sstevel@tonic-gate 			}
2510Sstevel@tonic-gate 		}
2520Sstevel@tonic-gate 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate #if 0
2550Sstevel@tonic-gate 	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
2560Sstevel@tonic-gate #endif
2570Sstevel@tonic-gate 		{
2580Sstevel@tonic-gate 		CRYPTO_set_locking_callback(lock_dbg_cb);
2590Sstevel@tonic-gate 		}
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	apps_startup();
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/* Lets load up our environment a little */
2640Sstevel@tonic-gate 	p=getenv("OPENSSL_CONF");
2650Sstevel@tonic-gate 	if (p == NULL)
2660Sstevel@tonic-gate 		p=getenv("SSLEAY_CONF");
2670Sstevel@tonic-gate 	if (p == NULL)
2680Sstevel@tonic-gate 		p=to_free=make_config_name();
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 	default_config_file=p;
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	config=NCONF_new(NULL);
2730Sstevel@tonic-gate 	i=NCONF_load(config,p,&errline);
2740Sstevel@tonic-gate 	if (i == 0)
2750Sstevel@tonic-gate 		{
2760Sstevel@tonic-gate 		NCONF_free(config);
2770Sstevel@tonic-gate 		config = NULL;
2780Sstevel@tonic-gate 		ERR_clear_error();
2790Sstevel@tonic-gate 		}
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	prog=prog_init();
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/* first check the program name */
2840Sstevel@tonic-gate 	program_name(Argv[0],pname,sizeof pname);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	f.name=pname;
2870Sstevel@tonic-gate 	fp=(FUNCTION *)lh_retrieve(prog,&f);
2880Sstevel@tonic-gate 	if (fp != NULL)
2890Sstevel@tonic-gate 		{
2900Sstevel@tonic-gate 		Argv[0]=pname;
2910Sstevel@tonic-gate 		ret=fp->func(Argc,Argv);
2920Sstevel@tonic-gate 		goto end;
2930Sstevel@tonic-gate 		}
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	/* ok, now check that there are not arguments, if there are,
2960Sstevel@tonic-gate 	 * run with them, shifting the ssleay off the front */
2970Sstevel@tonic-gate 	if (Argc != 1)
2980Sstevel@tonic-gate 		{
2990Sstevel@tonic-gate 		Argc--;
3000Sstevel@tonic-gate 		Argv++;
3010Sstevel@tonic-gate 		ret=do_cmd(prog,Argc,Argv);
3020Sstevel@tonic-gate 		if (ret < 0) ret=0;
3030Sstevel@tonic-gate 		goto end;
3040Sstevel@tonic-gate 		}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	/* ok, lets enter the old 'OpenSSL>' mode */
3070Sstevel@tonic-gate 
3080Sstevel@tonic-gate 	for (;;)
3090Sstevel@tonic-gate 		{
3100Sstevel@tonic-gate 		ret=0;
3110Sstevel@tonic-gate 		p=buf;
3120Sstevel@tonic-gate 		n=sizeof buf;
3130Sstevel@tonic-gate 		i=0;
3140Sstevel@tonic-gate 		for (;;)
3150Sstevel@tonic-gate 			{
3160Sstevel@tonic-gate 			p[0]='\0';
3170Sstevel@tonic-gate 			if (i++)
3180Sstevel@tonic-gate 				prompt=">";
3190Sstevel@tonic-gate 			else	prompt="OpenSSL> ";
3200Sstevel@tonic-gate 			fputs(prompt,stdout);
3210Sstevel@tonic-gate 			fflush(stdout);
3220Sstevel@tonic-gate 			fgets(p,n,stdin);
3230Sstevel@tonic-gate 			if (p[0] == '\0') goto end;
3240Sstevel@tonic-gate 			i=strlen(p);
3250Sstevel@tonic-gate 			if (i <= 1) break;
3260Sstevel@tonic-gate 			if (p[i-2] != '\\') break;
3270Sstevel@tonic-gate 			i-=2;
3280Sstevel@tonic-gate 			p+=i;
3290Sstevel@tonic-gate 			n-=i;
3300Sstevel@tonic-gate 			}
3310Sstevel@tonic-gate 		if (!chopup_args(&arg,buf,&argc,&argv)) break;
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		ret=do_cmd(prog,argc,argv);
3340Sstevel@tonic-gate 		if (ret < 0)
3350Sstevel@tonic-gate 			{
3360Sstevel@tonic-gate 			ret=0;
3370Sstevel@tonic-gate 			goto end;
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 		if (ret != 0)
3400Sstevel@tonic-gate 			BIO_printf(bio_err,"error in %s\n",argv[0]);
3410Sstevel@tonic-gate 		(void)BIO_flush(bio_err);
3420Sstevel@tonic-gate 		}
3430Sstevel@tonic-gate 	BIO_printf(bio_err,"bad exit\n");
3440Sstevel@tonic-gate 	ret=1;
3450Sstevel@tonic-gate end:
3460Sstevel@tonic-gate 	if (to_free)
3470Sstevel@tonic-gate 		OPENSSL_free(to_free);
3480Sstevel@tonic-gate 	if (config != NULL)
3490Sstevel@tonic-gate 		{
3500Sstevel@tonic-gate 		NCONF_free(config);
3510Sstevel@tonic-gate 		config=NULL;
3520Sstevel@tonic-gate 		}
3530Sstevel@tonic-gate 	if (prog != NULL) lh_free(prog);
3540Sstevel@tonic-gate 	if (arg.data != NULL) OPENSSL_free(arg.data);
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate 	apps_shutdown();
3570Sstevel@tonic-gate 
3580Sstevel@tonic-gate 	CRYPTO_mem_leaks(bio_err);
3590Sstevel@tonic-gate 	if (bio_err != NULL)
3600Sstevel@tonic-gate 		{
3610Sstevel@tonic-gate 		BIO_free(bio_err);
3620Sstevel@tonic-gate 		bio_err=NULL;
3630Sstevel@tonic-gate 		}
3640Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 
3670Sstevel@tonic-gate #define LIST_STANDARD_COMMANDS "list-standard-commands"
3680Sstevel@tonic-gate #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
3690Sstevel@tonic-gate #define LIST_CIPHER_COMMANDS "list-cipher-commands"
3700Sstevel@tonic-gate 
do_cmd(LHASH * prog,int argc,char * argv[])3710Sstevel@tonic-gate static int do_cmd(LHASH *prog, int argc, char *argv[])
3720Sstevel@tonic-gate 	{
3730Sstevel@tonic-gate 	FUNCTION f,*fp;
3740Sstevel@tonic-gate 	int i,ret=1,tp,nl;
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	if ((argc <= 0) || (argv[0] == NULL))
3770Sstevel@tonic-gate 		{ ret=0; goto end; }
3780Sstevel@tonic-gate 	f.name=argv[0];
3790Sstevel@tonic-gate 	fp=(FUNCTION *)lh_retrieve(prog,&f);
3800Sstevel@tonic-gate 	if (fp != NULL)
3810Sstevel@tonic-gate 		{
3820Sstevel@tonic-gate 		ret=fp->func(argc,argv);
3830Sstevel@tonic-gate 		}
3840Sstevel@tonic-gate 	else if ((strncmp(argv[0],"no-",3)) == 0)
3850Sstevel@tonic-gate 		{
3860Sstevel@tonic-gate 		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
3870Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
3880Sstevel@tonic-gate 		{
3890Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
3900Sstevel@tonic-gate 		bio_stdout = BIO_push(tmpbio, bio_stdout);
3910Sstevel@tonic-gate 		}
3920Sstevel@tonic-gate #endif
3930Sstevel@tonic-gate 		f.name=argv[0]+3;
3940Sstevel@tonic-gate 		ret = (lh_retrieve(prog,&f) != NULL);
3950Sstevel@tonic-gate 		if (!ret)
3960Sstevel@tonic-gate 			BIO_printf(bio_stdout, "%s\n", argv[0]);
3970Sstevel@tonic-gate 		else
3980Sstevel@tonic-gate 			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
3990Sstevel@tonic-gate 		BIO_free_all(bio_stdout);
4000Sstevel@tonic-gate 		goto end;
4010Sstevel@tonic-gate 		}
4020Sstevel@tonic-gate 	else if ((strcmp(argv[0],"quit") == 0) ||
4030Sstevel@tonic-gate 		(strcmp(argv[0],"q") == 0) ||
4040Sstevel@tonic-gate 		(strcmp(argv[0],"exit") == 0) ||
4050Sstevel@tonic-gate 		(strcmp(argv[0],"bye") == 0))
4060Sstevel@tonic-gate 		{
4070Sstevel@tonic-gate 		ret= -1;
4080Sstevel@tonic-gate 		goto end;
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
4110Sstevel@tonic-gate 		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
4120Sstevel@tonic-gate 		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
4130Sstevel@tonic-gate 		{
4140Sstevel@tonic-gate 		int list_type;
4150Sstevel@tonic-gate 		BIO *bio_stdout;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
4180Sstevel@tonic-gate 			list_type = FUNC_TYPE_GENERAL;
4190Sstevel@tonic-gate 		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
4200Sstevel@tonic-gate 			list_type = FUNC_TYPE_MD;
4210Sstevel@tonic-gate 		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
4220Sstevel@tonic-gate 			list_type = FUNC_TYPE_CIPHER;
4230Sstevel@tonic-gate 		bio_stdout = BIO_new_fp(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 		bio_stdout = BIO_push(tmpbio, bio_stdout);
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate #endif
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		for (fp=functions; fp->name != NULL; fp++)
4320Sstevel@tonic-gate 			if (fp->type == list_type)
4330Sstevel@tonic-gate 				BIO_printf(bio_stdout, "%s\n", fp->name);
4340Sstevel@tonic-gate 		BIO_free_all(bio_stdout);
4350Sstevel@tonic-gate 		ret=0;
4360Sstevel@tonic-gate 		goto end;
4370Sstevel@tonic-gate 		}
4380Sstevel@tonic-gate 	else
4390Sstevel@tonic-gate 		{
4400Sstevel@tonic-gate 		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
4410Sstevel@tonic-gate 			argv[0]);
4420Sstevel@tonic-gate 		BIO_printf(bio_err, "\nStandard commands");
4430Sstevel@tonic-gate 		i=0;
4440Sstevel@tonic-gate 		tp=0;
4450Sstevel@tonic-gate 		for (fp=functions; fp->name != NULL; fp++)
4460Sstevel@tonic-gate 			{
4470Sstevel@tonic-gate 			nl=0;
4480Sstevel@tonic-gate 			if (((i++) % 5) == 0)
4490Sstevel@tonic-gate 				{
4500Sstevel@tonic-gate 				BIO_printf(bio_err,"\n");
4510Sstevel@tonic-gate 				nl=1;
4520Sstevel@tonic-gate 				}
4530Sstevel@tonic-gate 			if (fp->type != tp)
4540Sstevel@tonic-gate 				{
4550Sstevel@tonic-gate 				tp=fp->type;
4560Sstevel@tonic-gate 				if (!nl) BIO_printf(bio_err,"\n");
4570Sstevel@tonic-gate 				if (tp == FUNC_TYPE_MD)
4580Sstevel@tonic-gate 					{
4590Sstevel@tonic-gate 					i=1;
4600Sstevel@tonic-gate 					BIO_printf(bio_err,
4610Sstevel@tonic-gate 						"\nMessage Digest commands (see the `dgst' command for more details)\n");
4620Sstevel@tonic-gate 					}
4630Sstevel@tonic-gate 				else if (tp == FUNC_TYPE_CIPHER)
4640Sstevel@tonic-gate 					{
4650Sstevel@tonic-gate 					i=1;
4660Sstevel@tonic-gate 					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
4670Sstevel@tonic-gate 					}
4680Sstevel@tonic-gate 				}
4690Sstevel@tonic-gate 			BIO_printf(bio_err,"%-15s",fp->name);
4700Sstevel@tonic-gate 			}
4710Sstevel@tonic-gate 		BIO_printf(bio_err,"\n\n");
4720Sstevel@tonic-gate 		ret=0;
4730Sstevel@tonic-gate 		}
4740Sstevel@tonic-gate end:
4750Sstevel@tonic-gate 	return(ret);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 
SortFnByName(const void * _f1,const void * _f2)4780Sstevel@tonic-gate static int SortFnByName(const void *_f1,const void *_f2)
4790Sstevel@tonic-gate     {
4800Sstevel@tonic-gate     const FUNCTION *f1=_f1;
4810Sstevel@tonic-gate     const FUNCTION *f2=_f2;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate     if(f1->type != f2->type)
4840Sstevel@tonic-gate 	return f1->type-f2->type;
4850Sstevel@tonic-gate     return strcmp(f1->name,f2->name);
4860Sstevel@tonic-gate     }
4870Sstevel@tonic-gate 
prog_init(void)4880Sstevel@tonic-gate static LHASH *prog_init(void)
4890Sstevel@tonic-gate 	{
4900Sstevel@tonic-gate 	LHASH *ret;
4910Sstevel@tonic-gate 	FUNCTION *f;
492*2139Sjp161948 	size_t i;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 	/* Purely so it looks nice when the user hits ? */
4950Sstevel@tonic-gate 	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
4960Sstevel@tonic-gate 	    ;
4970Sstevel@tonic-gate 	qsort(functions,i,sizeof *functions,SortFnByName);
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	if ((ret=lh_new(hash, cmp)) == NULL)
5000Sstevel@tonic-gate 		return(NULL);
5010Sstevel@tonic-gate 
5020Sstevel@tonic-gate 	for (f=functions; f->name != NULL; f++)
5030Sstevel@tonic-gate 		lh_insert(ret,f);
5040Sstevel@tonic-gate 	return(ret);
5050Sstevel@tonic-gate 	}
5060Sstevel@tonic-gate 
5070Sstevel@tonic-gate /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
cmp(const void * a_void,const void * b_void)5080Sstevel@tonic-gate static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
5090Sstevel@tonic-gate 	{
510*2139Sjp161948 	return(strncmp(((const FUNCTION *)a_void)->name,
511*2139Sjp161948 			((const FUNCTION *)b_void)->name,8));
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
hash(const void * a_void)5150Sstevel@tonic-gate static unsigned long MS_CALLBACK hash(const void *a_void)
5160Sstevel@tonic-gate 	{
517*2139Sjp161948 	return(lh_strhash(((const FUNCTION *)a_void)->name));
5180Sstevel@tonic-gate 	}
519