xref: /onnv-gate/usr/src/common/openssl/apps/engine.c (revision 2139:6243c3338933)
10Sstevel@tonic-gate /* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */
20Sstevel@tonic-gate /* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
30Sstevel@tonic-gate  * project 2000.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate /* ====================================================================
60Sstevel@tonic-gate  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
90Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
100Sstevel@tonic-gate  * are met:
110Sstevel@tonic-gate  *
120Sstevel@tonic-gate  * 1. Redistributions of source code must retain the above copyright
130Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
140Sstevel@tonic-gate  *
150Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
160Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in
170Sstevel@tonic-gate  *    the documentation and/or other materials provided with the
180Sstevel@tonic-gate  *    distribution.
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this
210Sstevel@tonic-gate  *    software must display the following acknowledgment:
220Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
230Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
260Sstevel@tonic-gate  *    endorse or promote products derived from this software without
270Sstevel@tonic-gate  *    prior written permission. For written permission, please contact
280Sstevel@tonic-gate  *    licensing@OpenSSL.org.
290Sstevel@tonic-gate  *
300Sstevel@tonic-gate  * 5. Products derived from this software may not be called "OpenSSL"
310Sstevel@tonic-gate  *    nor may "OpenSSL" appear in their names without prior written
320Sstevel@tonic-gate  *    permission of the OpenSSL Project.
330Sstevel@tonic-gate  *
340Sstevel@tonic-gate  * 6. Redistributions of any form whatsoever must retain the following
350Sstevel@tonic-gate  *    acknowledgment:
360Sstevel@tonic-gate  *    "This product includes software developed by the OpenSSL Project
370Sstevel@tonic-gate  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
400Sstevel@tonic-gate  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
410Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
420Sstevel@tonic-gate  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
430Sstevel@tonic-gate  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
440Sstevel@tonic-gate  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
450Sstevel@tonic-gate  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
460Sstevel@tonic-gate  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
470Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
480Sstevel@tonic-gate  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
490Sstevel@tonic-gate  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
500Sstevel@tonic-gate  * OF THE POSSIBILITY OF SUCH DAMAGE.
510Sstevel@tonic-gate  * ====================================================================
520Sstevel@tonic-gate  *
530Sstevel@tonic-gate  * This product includes cryptographic software written by Eric Young
540Sstevel@tonic-gate  * (eay@cryptsoft.com).  This product includes software written by Tim
550Sstevel@tonic-gate  * Hudson (tjh@cryptsoft.com).
560Sstevel@tonic-gate  *
570Sstevel@tonic-gate  */
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #ifndef OPENSSL_NO_ENGINE
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <stdio.h>
620Sstevel@tonic-gate #include <stdlib.h>
630Sstevel@tonic-gate #include <string.h>
640Sstevel@tonic-gate #ifdef OPENSSL_NO_STDIO
650Sstevel@tonic-gate #define APPS_WIN16
660Sstevel@tonic-gate #endif
670Sstevel@tonic-gate #include "apps.h"
680Sstevel@tonic-gate #include <openssl/err.h>
690Sstevel@tonic-gate #include <openssl/engine.h>
700Sstevel@tonic-gate #include <openssl/ssl.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #undef PROG
730Sstevel@tonic-gate #define PROG	engine_main
740Sstevel@tonic-gate 
75*2139Sjp161948 static const char *engine_usage[]={
760Sstevel@tonic-gate "usage: engine opts [engine ...]\n",
770Sstevel@tonic-gate " -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n",
780Sstevel@tonic-gate "               -vv will additionally display each command's description\n",
790Sstevel@tonic-gate "               -vvv will also add the input flags for each command\n",
800Sstevel@tonic-gate "               -vvvv will also show internal input flags\n",
810Sstevel@tonic-gate " -c          - for each engine, also list the capabilities\n",
82*2139Sjp161948 " -t[t]       - for each engine, check that they are really available\n",
83*2139Sjp161948 "               -tt will display error trace for unavailable engines\n",
840Sstevel@tonic-gate " -pre <cmd>  - runs command 'cmd' against the ENGINE before any attempts\n",
850Sstevel@tonic-gate "               to load it (if -t is used)\n",
860Sstevel@tonic-gate " -post <cmd> - runs command 'cmd' against the ENGINE after loading it\n",
870Sstevel@tonic-gate "               (only used if -t is also provided)\n",
880Sstevel@tonic-gate " NB: -pre and -post will be applied to all ENGINEs supplied on the command\n",
890Sstevel@tonic-gate " line, or all supported ENGINEs if none are specified.\n",
900Sstevel@tonic-gate " Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n",
910Sstevel@tonic-gate " argument \"/lib/libdriver.so\".\n",
920Sstevel@tonic-gate NULL
930Sstevel@tonic-gate };
940Sstevel@tonic-gate 
identity(void * ptr)950Sstevel@tonic-gate static void identity(void *ptr)
960Sstevel@tonic-gate 	{
970Sstevel@tonic-gate 	return;
980Sstevel@tonic-gate 	}
990Sstevel@tonic-gate 
append_buf(char ** buf,const char * s,int * size,int step)1000Sstevel@tonic-gate static int append_buf(char **buf, const char *s, int *size, int step)
1010Sstevel@tonic-gate 	{
1020Sstevel@tonic-gate 	int l = strlen(s);
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if (*buf == NULL)
1050Sstevel@tonic-gate 		{
1060Sstevel@tonic-gate 		*size = step;
1070Sstevel@tonic-gate 		*buf = OPENSSL_malloc(*size);
1080Sstevel@tonic-gate 		if (*buf == NULL)
1090Sstevel@tonic-gate 			return 0;
1100Sstevel@tonic-gate 		**buf = '\0';
1110Sstevel@tonic-gate 		}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	if (**buf != '\0')
1140Sstevel@tonic-gate 		l += 2;		/* ", " */
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	if (strlen(*buf) + strlen(s) >= (unsigned int)*size)
1170Sstevel@tonic-gate 		{
1180Sstevel@tonic-gate 		*size += step;
1190Sstevel@tonic-gate 		*buf = OPENSSL_realloc(*buf, *size);
1200Sstevel@tonic-gate 		}
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate 	if (*buf == NULL)
1230Sstevel@tonic-gate 		return 0;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	if (**buf != '\0')
1260Sstevel@tonic-gate 		BUF_strlcat(*buf, ", ", *size);
1270Sstevel@tonic-gate 	BUF_strlcat(*buf, s, *size);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	return 1;
1300Sstevel@tonic-gate 	}
1310Sstevel@tonic-gate 
util_flags(BIO * bio_out,unsigned int flags,const char * indent)1320Sstevel@tonic-gate static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
1330Sstevel@tonic-gate 	{
1340Sstevel@tonic-gate 	int started = 0, err = 0;
1350Sstevel@tonic-gate 	/* Indent before displaying input flags */
1360Sstevel@tonic-gate 	BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
1370Sstevel@tonic-gate 	if(flags == 0)
1380Sstevel@tonic-gate 		{
1390Sstevel@tonic-gate 		BIO_printf(bio_out, "<no flags>\n");
1400Sstevel@tonic-gate 		return 1;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate         /* If the object is internal, mark it in a way that shows instead of
1430Sstevel@tonic-gate          * having it part of all the other flags, even if it really is. */
1440Sstevel@tonic-gate 	if(flags & ENGINE_CMD_FLAG_INTERNAL)
1450Sstevel@tonic-gate 		{
1460Sstevel@tonic-gate 		BIO_printf(bio_out, "[Internal] ");
1470Sstevel@tonic-gate 		}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	if(flags & ENGINE_CMD_FLAG_NUMERIC)
1500Sstevel@tonic-gate 		{
1510Sstevel@tonic-gate 		if(started)
1520Sstevel@tonic-gate 			{
1530Sstevel@tonic-gate 			BIO_printf(bio_out, "|");
1540Sstevel@tonic-gate 			err = 1;
1550Sstevel@tonic-gate 			}
1560Sstevel@tonic-gate 		BIO_printf(bio_out, "NUMERIC");
1570Sstevel@tonic-gate 		started = 1;
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 	/* Now we check that no combinations of the mutually exclusive NUMERIC,
1600Sstevel@tonic-gate 	 * STRING, and NO_INPUT flags have been used. Future flags that can be
1610Sstevel@tonic-gate 	 * OR'd together with these would need to added after these to preserve
1620Sstevel@tonic-gate 	 * the testing logic. */
1630Sstevel@tonic-gate 	if(flags & ENGINE_CMD_FLAG_STRING)
1640Sstevel@tonic-gate 		{
1650Sstevel@tonic-gate 		if(started)
1660Sstevel@tonic-gate 			{
1670Sstevel@tonic-gate 			BIO_printf(bio_out, "|");
1680Sstevel@tonic-gate 			err = 1;
1690Sstevel@tonic-gate 			}
1700Sstevel@tonic-gate 		BIO_printf(bio_out, "STRING");
1710Sstevel@tonic-gate 		started = 1;
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	if(flags & ENGINE_CMD_FLAG_NO_INPUT)
1740Sstevel@tonic-gate 		{
1750Sstevel@tonic-gate 		if(started)
1760Sstevel@tonic-gate 			{
1770Sstevel@tonic-gate 			BIO_printf(bio_out, "|");
1780Sstevel@tonic-gate 			err = 1;
1790Sstevel@tonic-gate 			}
1800Sstevel@tonic-gate 		BIO_printf(bio_out, "NO_INPUT");
1810Sstevel@tonic-gate 		started = 1;
1820Sstevel@tonic-gate 		}
1830Sstevel@tonic-gate 	/* Check for unknown flags */
1840Sstevel@tonic-gate 	flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
1850Sstevel@tonic-gate 			~ENGINE_CMD_FLAG_STRING &
1860Sstevel@tonic-gate 			~ENGINE_CMD_FLAG_NO_INPUT &
1870Sstevel@tonic-gate 			~ENGINE_CMD_FLAG_INTERNAL;
1880Sstevel@tonic-gate 	if(flags)
1890Sstevel@tonic-gate 		{
1900Sstevel@tonic-gate 		if(started) BIO_printf(bio_out, "|");
1910Sstevel@tonic-gate 		BIO_printf(bio_out, "<0x%04X>", flags);
1920Sstevel@tonic-gate 		}
1930Sstevel@tonic-gate 	if(err)
1940Sstevel@tonic-gate 		BIO_printf(bio_out, "  <illegal flags!>");
1950Sstevel@tonic-gate 	BIO_printf(bio_out, "\n");
1960Sstevel@tonic-gate 	return 1;
1970Sstevel@tonic-gate 	}
1980Sstevel@tonic-gate 
util_verbose(ENGINE * e,int verbose,BIO * bio_out,const char * indent)1990Sstevel@tonic-gate static int util_verbose(ENGINE *e, int verbose, BIO *bio_out, const char *indent)
2000Sstevel@tonic-gate 	{
2010Sstevel@tonic-gate 	static const int line_wrap = 78;
2020Sstevel@tonic-gate 	int num;
2030Sstevel@tonic-gate 	int ret = 0;
2040Sstevel@tonic-gate 	char *name = NULL;
2050Sstevel@tonic-gate 	char *desc = NULL;
2060Sstevel@tonic-gate 	int flags;
2070Sstevel@tonic-gate 	int xpos = 0;
2080Sstevel@tonic-gate 	STACK *cmds = NULL;
2090Sstevel@tonic-gate 	if(!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
2100Sstevel@tonic-gate 			((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
2110Sstevel@tonic-gate 					0, NULL, NULL)) <= 0))
2120Sstevel@tonic-gate 		{
2130Sstevel@tonic-gate #if 0
2140Sstevel@tonic-gate 		BIO_printf(bio_out, "%s<no control commands>\n", indent);
2150Sstevel@tonic-gate #endif
2160Sstevel@tonic-gate 		return 1;
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	cmds = sk_new_null();
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if(!cmds)
2220Sstevel@tonic-gate 		goto err;
2230Sstevel@tonic-gate 	do {
2240Sstevel@tonic-gate 		int len;
2250Sstevel@tonic-gate 		/* Get the command input flags */
2260Sstevel@tonic-gate 		if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
2270Sstevel@tonic-gate 					NULL, NULL)) < 0)
2280Sstevel@tonic-gate 			goto err;
2290Sstevel@tonic-gate                 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4)
2300Sstevel@tonic-gate                         {
2310Sstevel@tonic-gate                         /* Get the command name */
2320Sstevel@tonic-gate                         if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
2330Sstevel@tonic-gate                                 NULL, NULL)) <= 0)
2340Sstevel@tonic-gate                                 goto err;
2350Sstevel@tonic-gate                         if((name = OPENSSL_malloc(len + 1)) == NULL)
2360Sstevel@tonic-gate                                 goto err;
2370Sstevel@tonic-gate                         if(ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
2380Sstevel@tonic-gate                                 NULL) <= 0)
2390Sstevel@tonic-gate                                 goto err;
2400Sstevel@tonic-gate                         /* Get the command description */
2410Sstevel@tonic-gate                         if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
2420Sstevel@tonic-gate                                 NULL, NULL)) < 0)
2430Sstevel@tonic-gate                                 goto err;
2440Sstevel@tonic-gate                         if(len > 0)
2450Sstevel@tonic-gate                                 {
2460Sstevel@tonic-gate                                 if((desc = OPENSSL_malloc(len + 1)) == NULL)
2470Sstevel@tonic-gate                                         goto err;
2480Sstevel@tonic-gate                                 if(ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
2490Sstevel@tonic-gate                                         NULL) <= 0)
2500Sstevel@tonic-gate                                         goto err;
2510Sstevel@tonic-gate                                 }
2520Sstevel@tonic-gate                         /* Now decide on the output */
2530Sstevel@tonic-gate                         if(xpos == 0)
2540Sstevel@tonic-gate                                 /* Do an indent */
2550Sstevel@tonic-gate                                 xpos = BIO_printf(bio_out, indent);
2560Sstevel@tonic-gate                         else
2570Sstevel@tonic-gate                                 /* Otherwise prepend a ", " */
2580Sstevel@tonic-gate                                 xpos += BIO_printf(bio_out, ", ");
2590Sstevel@tonic-gate                         if(verbose == 1)
2600Sstevel@tonic-gate                                 {
2610Sstevel@tonic-gate                                 /* We're just listing names, comma-delimited */
2620Sstevel@tonic-gate                                 if((xpos > (int)strlen(indent)) &&
2630Sstevel@tonic-gate 					(xpos + (int)strlen(name) > line_wrap))
2640Sstevel@tonic-gate                                         {
2650Sstevel@tonic-gate                                         BIO_printf(bio_out, "\n");
2660Sstevel@tonic-gate                                         xpos = BIO_printf(bio_out, indent);
2670Sstevel@tonic-gate                                         }
2680Sstevel@tonic-gate                                 xpos += BIO_printf(bio_out, "%s", name);
2690Sstevel@tonic-gate                                 }
2700Sstevel@tonic-gate                         else
2710Sstevel@tonic-gate                                 {
2720Sstevel@tonic-gate                                 /* We're listing names plus descriptions */
2730Sstevel@tonic-gate                                 BIO_printf(bio_out, "%s: %s\n", name,
2740Sstevel@tonic-gate                                         (desc == NULL) ? "<no description>" : desc);
2750Sstevel@tonic-gate                                 /* ... and sometimes input flags */
2760Sstevel@tonic-gate                                 if((verbose >= 3) && !util_flags(bio_out, flags,
2770Sstevel@tonic-gate                                         indent))
2780Sstevel@tonic-gate                                         goto err;
2790Sstevel@tonic-gate                                 xpos = 0;
2800Sstevel@tonic-gate                                 }
2810Sstevel@tonic-gate                         }
2820Sstevel@tonic-gate 		OPENSSL_free(name); name = NULL;
2830Sstevel@tonic-gate 		if(desc) { OPENSSL_free(desc); desc = NULL; }
2840Sstevel@tonic-gate 		/* Move to the next command */
2850Sstevel@tonic-gate 		num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE,
2860Sstevel@tonic-gate 					num, NULL, NULL);
2870Sstevel@tonic-gate 		} while(num > 0);
2880Sstevel@tonic-gate 	if(xpos > 0)
2890Sstevel@tonic-gate 		BIO_printf(bio_out, "\n");
2900Sstevel@tonic-gate 	ret = 1;
2910Sstevel@tonic-gate err:
2920Sstevel@tonic-gate 	if(cmds) sk_pop_free(cmds, identity);
2930Sstevel@tonic-gate 	if(name) OPENSSL_free(name);
2940Sstevel@tonic-gate 	if(desc) OPENSSL_free(desc);
2950Sstevel@tonic-gate 	return ret;
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
util_do_cmds(ENGINE * e,STACK * cmds,BIO * bio_out,const char * indent)2980Sstevel@tonic-gate static void util_do_cmds(ENGINE *e, STACK *cmds, BIO *bio_out, const char *indent)
2990Sstevel@tonic-gate 	{
3000Sstevel@tonic-gate 	int loop, res, num = sk_num(cmds);
3010Sstevel@tonic-gate 	if(num < 0)
3020Sstevel@tonic-gate 		{
3030Sstevel@tonic-gate 		BIO_printf(bio_out, "[Error]: internal stack error\n");
3040Sstevel@tonic-gate 		return;
3050Sstevel@tonic-gate 		}
3060Sstevel@tonic-gate 	for(loop = 0; loop < num; loop++)
3070Sstevel@tonic-gate 		{
3080Sstevel@tonic-gate 		char buf[256];
3090Sstevel@tonic-gate 		const char *cmd, *arg;
3100Sstevel@tonic-gate 		cmd = sk_value(cmds, loop);
3110Sstevel@tonic-gate 		res = 1; /* assume success */
3120Sstevel@tonic-gate 		/* Check if this command has no ":arg" */
3130Sstevel@tonic-gate 		if((arg = strstr(cmd, ":")) == NULL)
3140Sstevel@tonic-gate 			{
3150Sstevel@tonic-gate 			if(!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
3160Sstevel@tonic-gate 				res = 0;
3170Sstevel@tonic-gate 			}
3180Sstevel@tonic-gate 		else
3190Sstevel@tonic-gate 			{
3200Sstevel@tonic-gate 			if((int)(arg - cmd) > 254)
3210Sstevel@tonic-gate 				{
3220Sstevel@tonic-gate 				BIO_printf(bio_out,"[Error]: command name too long\n");
3230Sstevel@tonic-gate 				return;
3240Sstevel@tonic-gate 				}
3250Sstevel@tonic-gate 			memcpy(buf, cmd, (int)(arg - cmd));
3260Sstevel@tonic-gate 			buf[arg-cmd] = '\0';
3270Sstevel@tonic-gate 			arg++; /* Move past the ":" */
3280Sstevel@tonic-gate 			/* Call the command with the argument */
3290Sstevel@tonic-gate 			if(!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
3300Sstevel@tonic-gate 				res = 0;
3310Sstevel@tonic-gate 			}
3320Sstevel@tonic-gate 		if(res)
3330Sstevel@tonic-gate 			BIO_printf(bio_out, "[Success]: %s\n", cmd);
3340Sstevel@tonic-gate 		else
3350Sstevel@tonic-gate 			{
3360Sstevel@tonic-gate 			BIO_printf(bio_out, "[Failure]: %s\n", cmd);
3370Sstevel@tonic-gate 			ERR_print_errors(bio_out);
3380Sstevel@tonic-gate 			}
3390Sstevel@tonic-gate 		}
3400Sstevel@tonic-gate 	}
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate int MAIN(int, char **);
3430Sstevel@tonic-gate 
MAIN(int argc,char ** argv)3440Sstevel@tonic-gate int MAIN(int argc, char **argv)
3450Sstevel@tonic-gate 	{
3460Sstevel@tonic-gate 	int ret=1,i;
347*2139Sjp161948 	const char **pp;
348*2139Sjp161948 	int verbose=0, list_cap=0, test_avail=0, test_avail_noise = 0;
3490Sstevel@tonic-gate 	ENGINE *e;
3500Sstevel@tonic-gate 	STACK *engines = sk_new_null();
3510Sstevel@tonic-gate 	STACK *pre_cmds = sk_new_null();
3520Sstevel@tonic-gate 	STACK *post_cmds = sk_new_null();
3530Sstevel@tonic-gate 	int badops=1;
3540Sstevel@tonic-gate 	BIO *bio_out=NULL;
3550Sstevel@tonic-gate 	const char *indent = "     ";
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	apps_startup();
3580Sstevel@tonic-gate 	SSL_load_error_strings();
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate 	if (bio_err == NULL)
3610Sstevel@tonic-gate 		bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (!load_config(bio_err, NULL))
3640Sstevel@tonic-gate 		goto end;
3650Sstevel@tonic-gate 	bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
3660Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
3670Sstevel@tonic-gate 	{
3680Sstevel@tonic-gate 	BIO *tmpbio = BIO_new(BIO_f_linebuffer());
3690Sstevel@tonic-gate 	bio_out = BIO_push(tmpbio, bio_out);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate #endif
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	argc--;
3740Sstevel@tonic-gate 	argv++;
3750Sstevel@tonic-gate 	while (argc >= 1)
3760Sstevel@tonic-gate 		{
3770Sstevel@tonic-gate 		if (strncmp(*argv,"-v",2) == 0)
3780Sstevel@tonic-gate 			{
3790Sstevel@tonic-gate 			if(strspn(*argv + 1, "v") < strlen(*argv + 1))
3800Sstevel@tonic-gate 				goto skip_arg_loop;
3810Sstevel@tonic-gate 			if((verbose=strlen(*argv + 1)) > 4)
3820Sstevel@tonic-gate 				goto skip_arg_loop;
3830Sstevel@tonic-gate 			}
3840Sstevel@tonic-gate 		else if (strcmp(*argv,"-c") == 0)
3850Sstevel@tonic-gate 			list_cap=1;
386*2139Sjp161948 		else if (strncmp(*argv,"-t",2) == 0)
387*2139Sjp161948 			{
3880Sstevel@tonic-gate 			test_avail=1;
389*2139Sjp161948 			if(strspn(*argv + 1, "t") < strlen(*argv + 1))
390*2139Sjp161948 				goto skip_arg_loop;
391*2139Sjp161948 			if((test_avail_noise = strlen(*argv + 1) - 1) > 1)
392*2139Sjp161948 				goto skip_arg_loop;
393*2139Sjp161948 			}
3940Sstevel@tonic-gate 		else if (strcmp(*argv,"-pre") == 0)
3950Sstevel@tonic-gate 			{
3960Sstevel@tonic-gate 			argc--; argv++;
397*2139Sjp161948 			if (argc == 0)
398*2139Sjp161948 				goto skip_arg_loop;
3990Sstevel@tonic-gate 			sk_push(pre_cmds,*argv);
4000Sstevel@tonic-gate 			}
4010Sstevel@tonic-gate 		else if (strcmp(*argv,"-post") == 0)
4020Sstevel@tonic-gate 			{
4030Sstevel@tonic-gate 			argc--; argv++;
404*2139Sjp161948 			if (argc == 0)
405*2139Sjp161948 				goto skip_arg_loop;
4060Sstevel@tonic-gate 			sk_push(post_cmds,*argv);
4070Sstevel@tonic-gate 			}
4080Sstevel@tonic-gate 		else if ((strncmp(*argv,"-h",2) == 0) ||
4090Sstevel@tonic-gate 				(strcmp(*argv,"-?") == 0))
4100Sstevel@tonic-gate 			goto skip_arg_loop;
4110Sstevel@tonic-gate 		else
4120Sstevel@tonic-gate 			sk_push(engines,*argv);
4130Sstevel@tonic-gate 		argc--;
4140Sstevel@tonic-gate 		argv++;
4150Sstevel@tonic-gate 		}
4160Sstevel@tonic-gate 	/* Looks like everything went OK */
4170Sstevel@tonic-gate 	badops = 0;
4180Sstevel@tonic-gate skip_arg_loop:
4190Sstevel@tonic-gate 
4200Sstevel@tonic-gate 	if (badops)
4210Sstevel@tonic-gate 		{
4220Sstevel@tonic-gate 		for (pp=engine_usage; (*pp != NULL); pp++)
4230Sstevel@tonic-gate 			BIO_printf(bio_err,"%s",*pp);
4240Sstevel@tonic-gate 		goto end;
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 	if (sk_num(engines) == 0)
4280Sstevel@tonic-gate 		{
4290Sstevel@tonic-gate 		for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
4300Sstevel@tonic-gate 			{
4310Sstevel@tonic-gate 			sk_push(engines,(char *)ENGINE_get_id(e));
4320Sstevel@tonic-gate 			}
4330Sstevel@tonic-gate 		}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	for (i=0; i<sk_num(engines); i++)
4360Sstevel@tonic-gate 		{
4370Sstevel@tonic-gate 		const char *id = sk_value(engines,i);
4380Sstevel@tonic-gate 		if ((e = ENGINE_by_id(id)) != NULL)
4390Sstevel@tonic-gate 			{
4400Sstevel@tonic-gate 			const char *name = ENGINE_get_name(e);
4410Sstevel@tonic-gate 			/* Do "id" first, then "name". Easier to auto-parse. */
4420Sstevel@tonic-gate 			BIO_printf(bio_out, "(%s) %s\n", id, name);
4430Sstevel@tonic-gate 			util_do_cmds(e, pre_cmds, bio_out, indent);
4440Sstevel@tonic-gate 			if (strcmp(ENGINE_get_id(e), id) != 0)
4450Sstevel@tonic-gate 				{
4460Sstevel@tonic-gate 				BIO_printf(bio_out, "Loaded: (%s) %s\n",
4470Sstevel@tonic-gate 					ENGINE_get_id(e), ENGINE_get_name(e));
4480Sstevel@tonic-gate 				}
4490Sstevel@tonic-gate 			if (list_cap)
4500Sstevel@tonic-gate 				{
4510Sstevel@tonic-gate 				int cap_size = 256;
4520Sstevel@tonic-gate 				char *cap_buf = NULL;
4530Sstevel@tonic-gate 				int k,n;
4540Sstevel@tonic-gate 				const int *nids;
4550Sstevel@tonic-gate 				ENGINE_CIPHERS_PTR fn_c;
4560Sstevel@tonic-gate 				ENGINE_DIGESTS_PTR fn_d;
4570Sstevel@tonic-gate 
4580Sstevel@tonic-gate 				if (ENGINE_get_RSA(e) != NULL
4590Sstevel@tonic-gate 					&& !append_buf(&cap_buf, "RSA",
4600Sstevel@tonic-gate 						&cap_size, 256))
4610Sstevel@tonic-gate 					goto end;
4620Sstevel@tonic-gate 				if (ENGINE_get_DSA(e) != NULL
4630Sstevel@tonic-gate 					&& !append_buf(&cap_buf, "DSA",
4640Sstevel@tonic-gate 						&cap_size, 256))
4650Sstevel@tonic-gate 					goto end;
4660Sstevel@tonic-gate 				if (ENGINE_get_DH(e) != NULL
4670Sstevel@tonic-gate 					&& !append_buf(&cap_buf, "DH",
4680Sstevel@tonic-gate 						&cap_size, 256))
4690Sstevel@tonic-gate 					goto end;
4700Sstevel@tonic-gate 				if (ENGINE_get_RAND(e) != NULL
4710Sstevel@tonic-gate 					&& !append_buf(&cap_buf, "RAND",
4720Sstevel@tonic-gate 						&cap_size, 256))
4730Sstevel@tonic-gate 					goto end;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 				fn_c = ENGINE_get_ciphers(e);
4760Sstevel@tonic-gate 				if(!fn_c) goto skip_ciphers;
4770Sstevel@tonic-gate 				n = fn_c(e, NULL, &nids, 0);
4780Sstevel@tonic-gate 				for(k=0 ; k < n ; ++k)
4790Sstevel@tonic-gate 					if(!append_buf(&cap_buf,
4800Sstevel@tonic-gate 						       OBJ_nid2sn(nids[k]),
4810Sstevel@tonic-gate 						       &cap_size, 256))
4820Sstevel@tonic-gate 						goto end;
4830Sstevel@tonic-gate 
4840Sstevel@tonic-gate skip_ciphers:
4850Sstevel@tonic-gate 				fn_d = ENGINE_get_digests(e);
4860Sstevel@tonic-gate 				if(!fn_d) goto skip_digests;
4870Sstevel@tonic-gate 				n = fn_d(e, NULL, &nids, 0);
4880Sstevel@tonic-gate 				for(k=0 ; k < n ; ++k)
4890Sstevel@tonic-gate 					if(!append_buf(&cap_buf,
4900Sstevel@tonic-gate 						       OBJ_nid2sn(nids[k]),
4910Sstevel@tonic-gate 						       &cap_size, 256))
4920Sstevel@tonic-gate 						goto end;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate skip_digests:
4950Sstevel@tonic-gate 				if (cap_buf && (*cap_buf != '\0'))
4960Sstevel@tonic-gate 					BIO_printf(bio_out, " [%s]\n", cap_buf);
4970Sstevel@tonic-gate 
4980Sstevel@tonic-gate 				OPENSSL_free(cap_buf);
4990Sstevel@tonic-gate 				}
5000Sstevel@tonic-gate 			if(test_avail)
5010Sstevel@tonic-gate 				{
5020Sstevel@tonic-gate 				BIO_printf(bio_out, "%s", indent);
5030Sstevel@tonic-gate 				if (ENGINE_init(e))
5040Sstevel@tonic-gate 					{
5050Sstevel@tonic-gate 					BIO_printf(bio_out, "[ available ]\n");
5060Sstevel@tonic-gate 					util_do_cmds(e, post_cmds, bio_out, indent);
5070Sstevel@tonic-gate 					ENGINE_finish(e);
5080Sstevel@tonic-gate 					}
5090Sstevel@tonic-gate 				else
5100Sstevel@tonic-gate 					{
5110Sstevel@tonic-gate 					BIO_printf(bio_out, "[ unavailable ]\n");
512*2139Sjp161948 					if(test_avail_noise)
513*2139Sjp161948 						ERR_print_errors_fp(stdout);
5140Sstevel@tonic-gate 					ERR_clear_error();
5150Sstevel@tonic-gate 					}
5160Sstevel@tonic-gate 				}
5170Sstevel@tonic-gate 			if((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
5180Sstevel@tonic-gate 				goto end;
5190Sstevel@tonic-gate 			ENGINE_free(e);
5200Sstevel@tonic-gate 			}
5210Sstevel@tonic-gate 		else
5220Sstevel@tonic-gate 			ERR_print_errors(bio_err);
5230Sstevel@tonic-gate 		}
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	ret=0;
5260Sstevel@tonic-gate end:
527*2139Sjp161948 
5280Sstevel@tonic-gate 	ERR_print_errors(bio_err);
5290Sstevel@tonic-gate 	sk_pop_free(engines, identity);
5300Sstevel@tonic-gate 	sk_pop_free(pre_cmds, identity);
5310Sstevel@tonic-gate 	sk_pop_free(post_cmds, identity);
5320Sstevel@tonic-gate 	if (bio_out != NULL) BIO_free_all(bio_out);
5330Sstevel@tonic-gate 	apps_shutdown();
5340Sstevel@tonic-gate 	OPENSSL_EXIT(ret);
5350Sstevel@tonic-gate 	}
5360Sstevel@tonic-gate #else
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate # if PEDANTIC
5390Sstevel@tonic-gate static void *dummy=&dummy;
5400Sstevel@tonic-gate # endif
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate #endif
543