xref: /onnv-gate/usr/src/common/openssl/apps/openssl.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* apps/openssl.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate /* ====================================================================
59*0Sstevel@tonic-gate  * Copyright (c) 1998-2001 The OpenSSL Project.  All rights reserved.
60*0Sstevel@tonic-gate  *
61*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
62*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
63*0Sstevel@tonic-gate  * are met:
64*0Sstevel@tonic-gate  *
65*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
66*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
67*0Sstevel@tonic-gate  *
68*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
69*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
70*0Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
71*0Sstevel@tonic-gate  *    distribution.
72*0Sstevel@tonic-gate  *
73*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
74*0Sstevel@tonic-gate  *    software must display the following acknowledgment:
75*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
76*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
77*0Sstevel@tonic-gate  *
78*0Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
79*0Sstevel@tonic-gate  *    endorse or promote products derived from this software without
80*0Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
81*0Sstevel@tonic-gate  *    openssl-core@openssl.org.
82*0Sstevel@tonic-gate  *
83*0Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
84*0Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
85*0Sstevel@tonic-gate  *    permission of the OpenSSL Project.
86*0Sstevel@tonic-gate  *
87*0Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
88*0Sstevel@tonic-gate  *    acknowledgment:
89*0Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
90*0Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
91*0Sstevel@tonic-gate  *
92*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
93*0Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
94*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
95*0Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
96*0Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
97*0Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
98*0Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
99*0Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
100*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
101*0Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
102*0Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
103*0Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
104*0Sstevel@tonic-gate  * ====================================================================
105*0Sstevel@tonic-gate  *
106*0Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
107*0Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
108*0Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
109*0Sstevel@tonic-gate  *
110*0Sstevel@tonic-gate  */
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate #include <stdio.h>
114*0Sstevel@tonic-gate #include <string.h>
115*0Sstevel@tonic-gate #include <stdlib.h>
116*0Sstevel@tonic-gate #define OPENSSL_C /* tells apps.h to use complete apps_startup() */
117*0Sstevel@tonic-gate #include "apps.h"
118*0Sstevel@tonic-gate #include <openssl/bio.h>
119*0Sstevel@tonic-gate #include <openssl/crypto.h>
120*0Sstevel@tonic-gate #include <openssl/lhash.h>
121*0Sstevel@tonic-gate #include <openssl/conf.h>
122*0Sstevel@tonic-gate #include <openssl/x509.h>
123*0Sstevel@tonic-gate #include <openssl/pem.h>
124*0Sstevel@tonic-gate #include <openssl/ssl.h>
125*0Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
126*0Sstevel@tonic-gate #include <openssl/engine.h>
127*0Sstevel@tonic-gate #endif
128*0Sstevel@tonic-gate #define USE_SOCKETS /* needed for the _O_BINARY defs in the MS world */
129*0Sstevel@tonic-gate #include "progs.h"
130*0Sstevel@tonic-gate #include "s_apps.h"
131*0Sstevel@tonic-gate #include <openssl/err.h>
132*0Sstevel@tonic-gate 
133*0Sstevel@tonic-gate /* The LHASH callbacks ("hash" & "cmp") have been replaced by functions with the
134*0Sstevel@tonic-gate  * base prototypes (we cast each variable inside the function to the required
135*0Sstevel@tonic-gate  * type of "FUNCTION*"). This removes the necessity for macro-generated wrapper
136*0Sstevel@tonic-gate  * functions. */
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate /* static unsigned long MS_CALLBACK hash(FUNCTION *a); */
139*0Sstevel@tonic-gate static unsigned long MS_CALLBACK hash(const void *a_void);
140*0Sstevel@tonic-gate /* static int MS_CALLBACK cmp(FUNCTION *a,FUNCTION *b); */
141*0Sstevel@tonic-gate static int MS_CALLBACK cmp(const void *a_void,const void *b_void);
142*0Sstevel@tonic-gate static LHASH *prog_init(void );
143*0Sstevel@tonic-gate static int do_cmd(LHASH *prog,int argc,char *argv[]);
144*0Sstevel@tonic-gate char *default_config_file=NULL;
145*0Sstevel@tonic-gate 
146*0Sstevel@tonic-gate /* Make sure there is only one when MONOLITH is defined */
147*0Sstevel@tonic-gate #ifdef MONOLITH
148*0Sstevel@tonic-gate CONF *config=NULL;
149*0Sstevel@tonic-gate BIO *bio_err=NULL;
150*0Sstevel@tonic-gate #endif
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate static void lock_dbg_cb(int mode, int type, const char *file, int line)
154*0Sstevel@tonic-gate 	{
155*0Sstevel@tonic-gate 	static int modes[CRYPTO_NUM_LOCKS]; /* = {0, 0, ... } */
156*0Sstevel@tonic-gate 	const char *errstr = NULL;
157*0Sstevel@tonic-gate 	int rw;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 	rw = mode & (CRYPTO_READ|CRYPTO_WRITE);
160*0Sstevel@tonic-gate 	if (!((rw == CRYPTO_READ) || (rw == CRYPTO_WRITE)))
161*0Sstevel@tonic-gate 		{
162*0Sstevel@tonic-gate 		errstr = "invalid mode";
163*0Sstevel@tonic-gate 		goto err;
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 
166*0Sstevel@tonic-gate 	if (type < 0 || type >= CRYPTO_NUM_LOCKS)
167*0Sstevel@tonic-gate 		{
168*0Sstevel@tonic-gate 		errstr = "type out of bounds";
169*0Sstevel@tonic-gate 		goto err;
170*0Sstevel@tonic-gate 		}
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	if (mode & CRYPTO_LOCK)
173*0Sstevel@tonic-gate 		{
174*0Sstevel@tonic-gate 		if (modes[type])
175*0Sstevel@tonic-gate 			{
176*0Sstevel@tonic-gate 			errstr = "already locked";
177*0Sstevel@tonic-gate 			/* must not happen in a single-threaded program
178*0Sstevel@tonic-gate 			 * (would deadlock) */
179*0Sstevel@tonic-gate 			goto err;
180*0Sstevel@tonic-gate 			}
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate 		modes[type] = rw;
183*0Sstevel@tonic-gate 		}
184*0Sstevel@tonic-gate 	else if (mode & CRYPTO_UNLOCK)
185*0Sstevel@tonic-gate 		{
186*0Sstevel@tonic-gate 		if (!modes[type])
187*0Sstevel@tonic-gate 			{
188*0Sstevel@tonic-gate 			errstr = "not locked";
189*0Sstevel@tonic-gate 			goto err;
190*0Sstevel@tonic-gate 			}
191*0Sstevel@tonic-gate 
192*0Sstevel@tonic-gate 		if (modes[type] != rw)
193*0Sstevel@tonic-gate 			{
194*0Sstevel@tonic-gate 			errstr = (rw == CRYPTO_READ) ?
195*0Sstevel@tonic-gate 				"CRYPTO_r_unlock on write lock" :
196*0Sstevel@tonic-gate 				"CRYPTO_w_unlock on read lock";
197*0Sstevel@tonic-gate 			}
198*0Sstevel@tonic-gate 
199*0Sstevel@tonic-gate 		modes[type] = 0;
200*0Sstevel@tonic-gate 		}
201*0Sstevel@tonic-gate 	else
202*0Sstevel@tonic-gate 		{
203*0Sstevel@tonic-gate 		errstr = "invalid mode";
204*0Sstevel@tonic-gate 		goto err;
205*0Sstevel@tonic-gate 		}
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate  err:
208*0Sstevel@tonic-gate 	if (errstr)
209*0Sstevel@tonic-gate 		{
210*0Sstevel@tonic-gate 		/* we cannot use bio_err here */
211*0Sstevel@tonic-gate 		fprintf(stderr, "openssl (lock_dbg_cb): %s (mode=%d, type=%d) at %s:%d\n",
212*0Sstevel@tonic-gate 			errstr, mode, type, file, line);
213*0Sstevel@tonic-gate 		}
214*0Sstevel@tonic-gate 	}
215*0Sstevel@tonic-gate 
216*0Sstevel@tonic-gate 
217*0Sstevel@tonic-gate int main(int Argc, char *Argv[])
218*0Sstevel@tonic-gate 	{
219*0Sstevel@tonic-gate 	ARGS arg;
220*0Sstevel@tonic-gate #define PROG_NAME_SIZE	39
221*0Sstevel@tonic-gate 	char pname[PROG_NAME_SIZE+1];
222*0Sstevel@tonic-gate 	FUNCTION f,*fp;
223*0Sstevel@tonic-gate 	MS_STATIC char *prompt,buf[1024];
224*0Sstevel@tonic-gate 	char *to_free=NULL;
225*0Sstevel@tonic-gate 	int n,i,ret=0;
226*0Sstevel@tonic-gate 	int argc;
227*0Sstevel@tonic-gate 	char **argv,*p;
228*0Sstevel@tonic-gate 	LHASH *prog=NULL;
229*0Sstevel@tonic-gate 	long errline;
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate 	arg.data=NULL;
232*0Sstevel@tonic-gate 	arg.count=0;
233*0Sstevel@tonic-gate 
234*0Sstevel@tonic-gate 	if (bio_err == NULL)
235*0Sstevel@tonic-gate 		if ((bio_err=BIO_new(BIO_s_file())) != NULL)
236*0Sstevel@tonic-gate 			BIO_set_fp(bio_err,stderr,BIO_NOCLOSE|BIO_FP_TEXT);
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate 	if (getenv("OPENSSL_DEBUG_MEMORY") != NULL) /* if not defined, use compiled-in library defaults */
239*0Sstevel@tonic-gate 		{
240*0Sstevel@tonic-gate 		if (!(0 == strcmp(getenv("OPENSSL_DEBUG_MEMORY"), "off")))
241*0Sstevel@tonic-gate 			{
242*0Sstevel@tonic-gate 			CRYPTO_malloc_debug_init();
243*0Sstevel@tonic-gate 			CRYPTO_set_mem_debug_options(V_CRYPTO_MDEBUG_ALL);
244*0Sstevel@tonic-gate 			}
245*0Sstevel@tonic-gate 		else
246*0Sstevel@tonic-gate 			{
247*0Sstevel@tonic-gate 			/* OPENSSL_DEBUG_MEMORY=off */
248*0Sstevel@tonic-gate 			CRYPTO_set_mem_debug_functions(0, 0, 0, 0, 0);
249*0Sstevel@tonic-gate 			}
250*0Sstevel@tonic-gate 		}
251*0Sstevel@tonic-gate 	CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
252*0Sstevel@tonic-gate 
253*0Sstevel@tonic-gate #if 0
254*0Sstevel@tonic-gate 	if (getenv("OPENSSL_DEBUG_LOCKING") != NULL)
255*0Sstevel@tonic-gate #endif
256*0Sstevel@tonic-gate 		{
257*0Sstevel@tonic-gate 		CRYPTO_set_locking_callback(lock_dbg_cb);
258*0Sstevel@tonic-gate 		}
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 	apps_startup();
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate 	/* Lets load up our environment a little */
263*0Sstevel@tonic-gate 	p=getenv("OPENSSL_CONF");
264*0Sstevel@tonic-gate 	if (p == NULL)
265*0Sstevel@tonic-gate 		p=getenv("SSLEAY_CONF");
266*0Sstevel@tonic-gate 	if (p == NULL)
267*0Sstevel@tonic-gate 		p=to_free=make_config_name();
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 	default_config_file=p;
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	config=NCONF_new(NULL);
272*0Sstevel@tonic-gate 	i=NCONF_load(config,p,&errline);
273*0Sstevel@tonic-gate 	if (i == 0)
274*0Sstevel@tonic-gate 		{
275*0Sstevel@tonic-gate 		NCONF_free(config);
276*0Sstevel@tonic-gate 		config = NULL;
277*0Sstevel@tonic-gate 		ERR_clear_error();
278*0Sstevel@tonic-gate 		}
279*0Sstevel@tonic-gate 
280*0Sstevel@tonic-gate 	prog=prog_init();
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	/* first check the program name */
283*0Sstevel@tonic-gate 	program_name(Argv[0],pname,sizeof pname);
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate 	f.name=pname;
286*0Sstevel@tonic-gate 	fp=(FUNCTION *)lh_retrieve(prog,&f);
287*0Sstevel@tonic-gate 	if (fp != NULL)
288*0Sstevel@tonic-gate 		{
289*0Sstevel@tonic-gate 		Argv[0]=pname;
290*0Sstevel@tonic-gate 		ret=fp->func(Argc,Argv);
291*0Sstevel@tonic-gate 		goto end;
292*0Sstevel@tonic-gate 		}
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 	/* ok, now check that there are not arguments, if there are,
295*0Sstevel@tonic-gate 	 * run with them, shifting the ssleay off the front */
296*0Sstevel@tonic-gate 	if (Argc != 1)
297*0Sstevel@tonic-gate 		{
298*0Sstevel@tonic-gate 		Argc--;
299*0Sstevel@tonic-gate 		Argv++;
300*0Sstevel@tonic-gate 		ret=do_cmd(prog,Argc,Argv);
301*0Sstevel@tonic-gate 		if (ret < 0) ret=0;
302*0Sstevel@tonic-gate 		goto end;
303*0Sstevel@tonic-gate 		}
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 	/* ok, lets enter the old 'OpenSSL>' mode */
306*0Sstevel@tonic-gate 
307*0Sstevel@tonic-gate 	for (;;)
308*0Sstevel@tonic-gate 		{
309*0Sstevel@tonic-gate 		ret=0;
310*0Sstevel@tonic-gate 		p=buf;
311*0Sstevel@tonic-gate 		n=sizeof buf;
312*0Sstevel@tonic-gate 		i=0;
313*0Sstevel@tonic-gate 		for (;;)
314*0Sstevel@tonic-gate 			{
315*0Sstevel@tonic-gate 			p[0]='\0';
316*0Sstevel@tonic-gate 			if (i++)
317*0Sstevel@tonic-gate 				prompt=">";
318*0Sstevel@tonic-gate 			else	prompt="OpenSSL> ";
319*0Sstevel@tonic-gate 			fputs(prompt,stdout);
320*0Sstevel@tonic-gate 			fflush(stdout);
321*0Sstevel@tonic-gate 			fgets(p,n,stdin);
322*0Sstevel@tonic-gate 			if (p[0] == '\0') goto end;
323*0Sstevel@tonic-gate 			i=strlen(p);
324*0Sstevel@tonic-gate 			if (i <= 1) break;
325*0Sstevel@tonic-gate 			if (p[i-2] != '\\') break;
326*0Sstevel@tonic-gate 			i-=2;
327*0Sstevel@tonic-gate 			p+=i;
328*0Sstevel@tonic-gate 			n-=i;
329*0Sstevel@tonic-gate 			}
330*0Sstevel@tonic-gate 		if (!chopup_args(&arg,buf,&argc,&argv)) break;
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate 		ret=do_cmd(prog,argc,argv);
333*0Sstevel@tonic-gate 		if (ret < 0)
334*0Sstevel@tonic-gate 			{
335*0Sstevel@tonic-gate 			ret=0;
336*0Sstevel@tonic-gate 			goto end;
337*0Sstevel@tonic-gate 			}
338*0Sstevel@tonic-gate 		if (ret != 0)
339*0Sstevel@tonic-gate 			BIO_printf(bio_err,"error in %s\n",argv[0]);
340*0Sstevel@tonic-gate 		(void)BIO_flush(bio_err);
341*0Sstevel@tonic-gate 		}
342*0Sstevel@tonic-gate 	BIO_printf(bio_err,"bad exit\n");
343*0Sstevel@tonic-gate 	ret=1;
344*0Sstevel@tonic-gate end:
345*0Sstevel@tonic-gate 	if (to_free)
346*0Sstevel@tonic-gate 		OPENSSL_free(to_free);
347*0Sstevel@tonic-gate 	if (config != NULL)
348*0Sstevel@tonic-gate 		{
349*0Sstevel@tonic-gate 		NCONF_free(config);
350*0Sstevel@tonic-gate 		config=NULL;
351*0Sstevel@tonic-gate 		}
352*0Sstevel@tonic-gate 	if (prog != NULL) lh_free(prog);
353*0Sstevel@tonic-gate 	if (arg.data != NULL) OPENSSL_free(arg.data);
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	apps_shutdown();
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate 	CRYPTO_mem_leaks(bio_err);
358*0Sstevel@tonic-gate 	if (bio_err != NULL)
359*0Sstevel@tonic-gate 		{
360*0Sstevel@tonic-gate 		BIO_free(bio_err);
361*0Sstevel@tonic-gate 		bio_err=NULL;
362*0Sstevel@tonic-gate 		}
363*0Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
364*0Sstevel@tonic-gate 	}
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate #define LIST_STANDARD_COMMANDS "list-standard-commands"
367*0Sstevel@tonic-gate #define LIST_MESSAGE_DIGEST_COMMANDS "list-message-digest-commands"
368*0Sstevel@tonic-gate #define LIST_CIPHER_COMMANDS "list-cipher-commands"
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate static int do_cmd(LHASH *prog, int argc, char *argv[])
371*0Sstevel@tonic-gate 	{
372*0Sstevel@tonic-gate 	FUNCTION f,*fp;
373*0Sstevel@tonic-gate 	int i,ret=1,tp,nl;
374*0Sstevel@tonic-gate 
375*0Sstevel@tonic-gate 	if ((argc <= 0) || (argv[0] == NULL))
376*0Sstevel@tonic-gate 		{ ret=0; goto end; }
377*0Sstevel@tonic-gate 	f.name=argv[0];
378*0Sstevel@tonic-gate 	fp=(FUNCTION *)lh_retrieve(prog,&f);
379*0Sstevel@tonic-gate 	if (fp != NULL)
380*0Sstevel@tonic-gate 		{
381*0Sstevel@tonic-gate 		ret=fp->func(argc,argv);
382*0Sstevel@tonic-gate 		}
383*0Sstevel@tonic-gate 	else if ((strncmp(argv[0],"no-",3)) == 0)
384*0Sstevel@tonic-gate 		{
385*0Sstevel@tonic-gate 		BIO *bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
386*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
387*0Sstevel@tonic-gate 		{
388*0Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
389*0Sstevel@tonic-gate 		bio_stdout = BIO_push(tmpbio, bio_stdout);
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate #endif
392*0Sstevel@tonic-gate 		f.name=argv[0]+3;
393*0Sstevel@tonic-gate 		ret = (lh_retrieve(prog,&f) != NULL);
394*0Sstevel@tonic-gate 		if (!ret)
395*0Sstevel@tonic-gate 			BIO_printf(bio_stdout, "%s\n", argv[0]);
396*0Sstevel@tonic-gate 		else
397*0Sstevel@tonic-gate 			BIO_printf(bio_stdout, "%s\n", argv[0]+3);
398*0Sstevel@tonic-gate 		BIO_free_all(bio_stdout);
399*0Sstevel@tonic-gate 		goto end;
400*0Sstevel@tonic-gate 		}
401*0Sstevel@tonic-gate 	else if ((strcmp(argv[0],"quit") == 0) ||
402*0Sstevel@tonic-gate 		(strcmp(argv[0],"q") == 0) ||
403*0Sstevel@tonic-gate 		(strcmp(argv[0],"exit") == 0) ||
404*0Sstevel@tonic-gate 		(strcmp(argv[0],"bye") == 0))
405*0Sstevel@tonic-gate 		{
406*0Sstevel@tonic-gate 		ret= -1;
407*0Sstevel@tonic-gate 		goto end;
408*0Sstevel@tonic-gate 		}
409*0Sstevel@tonic-gate 	else if ((strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0) ||
410*0Sstevel@tonic-gate 		(strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0) ||
411*0Sstevel@tonic-gate 		(strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0))
412*0Sstevel@tonic-gate 		{
413*0Sstevel@tonic-gate 		int list_type;
414*0Sstevel@tonic-gate 		BIO *bio_stdout;
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 		if (strcmp(argv[0],LIST_STANDARD_COMMANDS) == 0)
417*0Sstevel@tonic-gate 			list_type = FUNC_TYPE_GENERAL;
418*0Sstevel@tonic-gate 		else if (strcmp(argv[0],LIST_MESSAGE_DIGEST_COMMANDS) == 0)
419*0Sstevel@tonic-gate 			list_type = FUNC_TYPE_MD;
420*0Sstevel@tonic-gate 		else /* strcmp(argv[0],LIST_CIPHER_COMMANDS) == 0 */
421*0Sstevel@tonic-gate 			list_type = FUNC_TYPE_CIPHER;
422*0Sstevel@tonic-gate 		bio_stdout = BIO_new_fp(stdout,BIO_NOCLOSE);
423*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
424*0Sstevel@tonic-gate 		{
425*0Sstevel@tonic-gate 		BIO *tmpbio = BIO_new(BIO_f_linebuffer());
426*0Sstevel@tonic-gate 		bio_stdout = BIO_push(tmpbio, bio_stdout);
427*0Sstevel@tonic-gate 		}
428*0Sstevel@tonic-gate #endif
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 		for (fp=functions; fp->name != NULL; fp++)
431*0Sstevel@tonic-gate 			if (fp->type == list_type)
432*0Sstevel@tonic-gate 				BIO_printf(bio_stdout, "%s\n", fp->name);
433*0Sstevel@tonic-gate 		BIO_free_all(bio_stdout);
434*0Sstevel@tonic-gate 		ret=0;
435*0Sstevel@tonic-gate 		goto end;
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 	else
438*0Sstevel@tonic-gate 		{
439*0Sstevel@tonic-gate 		BIO_printf(bio_err,"openssl:Error: '%s' is an invalid command.\n",
440*0Sstevel@tonic-gate 			argv[0]);
441*0Sstevel@tonic-gate 		BIO_printf(bio_err, "\nStandard commands");
442*0Sstevel@tonic-gate 		i=0;
443*0Sstevel@tonic-gate 		tp=0;
444*0Sstevel@tonic-gate 		for (fp=functions; fp->name != NULL; fp++)
445*0Sstevel@tonic-gate 			{
446*0Sstevel@tonic-gate 			nl=0;
447*0Sstevel@tonic-gate 			if (((i++) % 5) == 0)
448*0Sstevel@tonic-gate 				{
449*0Sstevel@tonic-gate 				BIO_printf(bio_err,"\n");
450*0Sstevel@tonic-gate 				nl=1;
451*0Sstevel@tonic-gate 				}
452*0Sstevel@tonic-gate 			if (fp->type != tp)
453*0Sstevel@tonic-gate 				{
454*0Sstevel@tonic-gate 				tp=fp->type;
455*0Sstevel@tonic-gate 				if (!nl) BIO_printf(bio_err,"\n");
456*0Sstevel@tonic-gate 				if (tp == FUNC_TYPE_MD)
457*0Sstevel@tonic-gate 					{
458*0Sstevel@tonic-gate 					i=1;
459*0Sstevel@tonic-gate 					BIO_printf(bio_err,
460*0Sstevel@tonic-gate 						"\nMessage Digest commands (see the `dgst' command for more details)\n");
461*0Sstevel@tonic-gate 					}
462*0Sstevel@tonic-gate 				else if (tp == FUNC_TYPE_CIPHER)
463*0Sstevel@tonic-gate 					{
464*0Sstevel@tonic-gate 					i=1;
465*0Sstevel@tonic-gate 					BIO_printf(bio_err,"\nCipher commands (see the `enc' command for more details)\n");
466*0Sstevel@tonic-gate 					}
467*0Sstevel@tonic-gate 				}
468*0Sstevel@tonic-gate 			BIO_printf(bio_err,"%-15s",fp->name);
469*0Sstevel@tonic-gate 			}
470*0Sstevel@tonic-gate 		BIO_printf(bio_err,"\n\n");
471*0Sstevel@tonic-gate 		ret=0;
472*0Sstevel@tonic-gate 		}
473*0Sstevel@tonic-gate end:
474*0Sstevel@tonic-gate 	return(ret);
475*0Sstevel@tonic-gate 	}
476*0Sstevel@tonic-gate 
477*0Sstevel@tonic-gate static int SortFnByName(const void *_f1,const void *_f2)
478*0Sstevel@tonic-gate     {
479*0Sstevel@tonic-gate     const FUNCTION *f1=_f1;
480*0Sstevel@tonic-gate     const FUNCTION *f2=_f2;
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate     if(f1->type != f2->type)
483*0Sstevel@tonic-gate 	return f1->type-f2->type;
484*0Sstevel@tonic-gate     return strcmp(f1->name,f2->name);
485*0Sstevel@tonic-gate     }
486*0Sstevel@tonic-gate 
487*0Sstevel@tonic-gate static LHASH *prog_init(void)
488*0Sstevel@tonic-gate 	{
489*0Sstevel@tonic-gate 	LHASH *ret;
490*0Sstevel@tonic-gate 	FUNCTION *f;
491*0Sstevel@tonic-gate 	int i;
492*0Sstevel@tonic-gate 
493*0Sstevel@tonic-gate 	/* Purely so it looks nice when the user hits ? */
494*0Sstevel@tonic-gate 	for(i=0,f=functions ; f->name != NULL ; ++f,++i)
495*0Sstevel@tonic-gate 	    ;
496*0Sstevel@tonic-gate 	qsort(functions,i,sizeof *functions,SortFnByName);
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	if ((ret=lh_new(hash, cmp)) == NULL)
499*0Sstevel@tonic-gate 		return(NULL);
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	for (f=functions; f->name != NULL; f++)
502*0Sstevel@tonic-gate 		lh_insert(ret,f);
503*0Sstevel@tonic-gate 	return(ret);
504*0Sstevel@tonic-gate 	}
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate /* static int MS_CALLBACK cmp(FUNCTION *a, FUNCTION *b) */
507*0Sstevel@tonic-gate static int MS_CALLBACK cmp(const void *a_void, const void *b_void)
508*0Sstevel@tonic-gate 	{
509*0Sstevel@tonic-gate 	return(strncmp(((FUNCTION *)a_void)->name,
510*0Sstevel@tonic-gate 			((FUNCTION *)b_void)->name,8));
511*0Sstevel@tonic-gate 	}
512*0Sstevel@tonic-gate 
513*0Sstevel@tonic-gate /* static unsigned long MS_CALLBACK hash(FUNCTION *a) */
514*0Sstevel@tonic-gate static unsigned long MS_CALLBACK hash(const void *a_void)
515*0Sstevel@tonic-gate 	{
516*0Sstevel@tonic-gate 	return(lh_strhash(((FUNCTION *)a_void)->name));
517*0Sstevel@tonic-gate 	}
518