xref: /minix3/crypto/external/bsd/openssl/dist/apps/engine.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
4*0a6a1f1dSLionel Sambuc  * 2000.
5ebfedea0SLionel Sambuc  */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc  * Copyright (c) 2000 The OpenSSL Project.  All rights reserved.
8ebfedea0SLionel Sambuc  *
9ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc  * are met:
12ebfedea0SLionel Sambuc  *
13ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc  *
16ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc  *    distribution.
20ebfedea0SLionel Sambuc  *
21ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
23ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25ebfedea0SLionel Sambuc  *
26ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
30ebfedea0SLionel Sambuc  *
31ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc  *
35ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc  *    acknowledgment:
37ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39ebfedea0SLionel Sambuc  *
40ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc  * ====================================================================
53ebfedea0SLionel Sambuc  *
54ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
56ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc  *
58ebfedea0SLionel Sambuc  */
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc #include <stdio.h>
61ebfedea0SLionel Sambuc #include <stdlib.h>
62ebfedea0SLionel Sambuc #include <string.h>
63ebfedea0SLionel Sambuc #ifdef OPENSSL_NO_STDIO
64ebfedea0SLionel Sambuc # define APPS_WIN16
65ebfedea0SLionel Sambuc #endif
66ebfedea0SLionel Sambuc #include "apps.h"
67ebfedea0SLionel Sambuc #include <openssl/err.h>
68ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_ENGINE
69ebfedea0SLionel Sambuc # include <openssl/engine.h>
70ebfedea0SLionel Sambuc # include <openssl/ssl.h>
71ebfedea0SLionel Sambuc 
72ebfedea0SLionel Sambuc # undef PROG
73ebfedea0SLionel Sambuc # define PROG    engine_main
74ebfedea0SLionel Sambuc 
75ebfedea0SLionel Sambuc static const char *engine_usage[] = {
76ebfedea0SLionel Sambuc     "usage: engine opts [engine ...]\n",
77ebfedea0SLionel Sambuc     " -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n",
78ebfedea0SLionel Sambuc     "               -vv will additionally display each command's description\n",
79ebfedea0SLionel Sambuc     "               -vvv will also add the input flags for each command\n",
80ebfedea0SLionel Sambuc     "               -vvvv will also show internal input flags\n",
81ebfedea0SLionel Sambuc     " -c          - for each engine, also list the capabilities\n",
82ebfedea0SLionel Sambuc     " -t[t]       - for each engine, check that they are really available\n",
83ebfedea0SLionel Sambuc     "               -tt will display error trace for unavailable engines\n",
84ebfedea0SLionel Sambuc     " -pre <cmd>  - runs command 'cmd' against the ENGINE before any attempts\n",
85ebfedea0SLionel Sambuc     "               to load it (if -t is used)\n",
86ebfedea0SLionel Sambuc     " -post <cmd> - runs command 'cmd' against the ENGINE after loading it\n",
87ebfedea0SLionel Sambuc     "               (only used if -t is also provided)\n",
88ebfedea0SLionel Sambuc     " NB: -pre and -post will be applied to all ENGINEs supplied on the command\n",
89ebfedea0SLionel Sambuc     " line, or all supported ENGINEs if none are specified.\n",
90ebfedea0SLionel Sambuc     " Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n",
91ebfedea0SLionel Sambuc     " argument \"/lib/libdriver.so\".\n",
92ebfedea0SLionel Sambuc     NULL
93ebfedea0SLionel Sambuc };
94ebfedea0SLionel Sambuc 
identity(char * ptr)95ebfedea0SLionel Sambuc static void identity(char *ptr)
96ebfedea0SLionel Sambuc {
97ebfedea0SLionel Sambuc     return;
98ebfedea0SLionel Sambuc }
99ebfedea0SLionel Sambuc 
append_buf(char ** buf,const char * s,int * size,int step)100ebfedea0SLionel Sambuc static int append_buf(char **buf, const char *s, int *size, int step)
101ebfedea0SLionel Sambuc {
102ebfedea0SLionel Sambuc     int l = strlen(s);
103ebfedea0SLionel Sambuc 
104*0a6a1f1dSLionel Sambuc     if (*buf == NULL) {
105ebfedea0SLionel Sambuc         *size = step;
106ebfedea0SLionel Sambuc         *buf = OPENSSL_malloc(*size);
107ebfedea0SLionel Sambuc         if (*buf == NULL)
108ebfedea0SLionel Sambuc             return 0;
109ebfedea0SLionel Sambuc         **buf = '\0';
110ebfedea0SLionel Sambuc     }
111ebfedea0SLionel Sambuc 
112ebfedea0SLionel Sambuc     if (**buf != '\0')
113ebfedea0SLionel Sambuc         l += 2;                 /* ", " */
114ebfedea0SLionel Sambuc 
115*0a6a1f1dSLionel Sambuc     if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
116ebfedea0SLionel Sambuc         *size += step;
117ebfedea0SLionel Sambuc         *buf = OPENSSL_realloc(*buf, *size);
118ebfedea0SLionel Sambuc     }
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc     if (*buf == NULL)
121ebfedea0SLionel Sambuc         return 0;
122ebfedea0SLionel Sambuc 
123ebfedea0SLionel Sambuc     if (**buf != '\0')
124ebfedea0SLionel Sambuc         BUF_strlcat(*buf, ", ", *size);
125ebfedea0SLionel Sambuc     BUF_strlcat(*buf, s, *size);
126ebfedea0SLionel Sambuc 
127ebfedea0SLionel Sambuc     return 1;
128ebfedea0SLionel Sambuc }
129ebfedea0SLionel Sambuc 
util_flags(BIO * bio_out,unsigned int flags,const char * indent)130ebfedea0SLionel Sambuc static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
131ebfedea0SLionel Sambuc {
132ebfedea0SLionel Sambuc     int started = 0, err = 0;
133ebfedea0SLionel Sambuc     /* Indent before displaying input flags */
134ebfedea0SLionel Sambuc     BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
135*0a6a1f1dSLionel Sambuc     if (flags == 0) {
136ebfedea0SLionel Sambuc         BIO_printf(bio_out, "<no flags>\n");
137ebfedea0SLionel Sambuc         return 1;
138ebfedea0SLionel Sambuc     }
139*0a6a1f1dSLionel Sambuc     /*
140*0a6a1f1dSLionel Sambuc      * If the object is internal, mark it in a way that shows instead of
141*0a6a1f1dSLionel Sambuc      * having it part of all the other flags, even if it really is.
142*0a6a1f1dSLionel Sambuc      */
143*0a6a1f1dSLionel Sambuc     if (flags & ENGINE_CMD_FLAG_INTERNAL) {
144ebfedea0SLionel Sambuc         BIO_printf(bio_out, "[Internal] ");
145ebfedea0SLionel Sambuc     }
146ebfedea0SLionel Sambuc 
147*0a6a1f1dSLionel Sambuc     if (flags & ENGINE_CMD_FLAG_NUMERIC) {
148ebfedea0SLionel Sambuc         BIO_printf(bio_out, "NUMERIC");
149ebfedea0SLionel Sambuc         started = 1;
150ebfedea0SLionel Sambuc     }
151*0a6a1f1dSLionel Sambuc     /*
152*0a6a1f1dSLionel Sambuc      * Now we check that no combinations of the mutually exclusive NUMERIC,
153ebfedea0SLionel Sambuc      * STRING, and NO_INPUT flags have been used. Future flags that can be
154ebfedea0SLionel Sambuc      * OR'd together with these would need to added after these to preserve
155*0a6a1f1dSLionel Sambuc      * the testing logic.
156*0a6a1f1dSLionel Sambuc      */
157*0a6a1f1dSLionel Sambuc     if (flags & ENGINE_CMD_FLAG_STRING) {
158*0a6a1f1dSLionel Sambuc         if (started) {
159ebfedea0SLionel Sambuc             BIO_printf(bio_out, "|");
160ebfedea0SLionel Sambuc             err = 1;
161ebfedea0SLionel Sambuc         }
162ebfedea0SLionel Sambuc         BIO_printf(bio_out, "STRING");
163ebfedea0SLionel Sambuc         started = 1;
164ebfedea0SLionel Sambuc     }
165*0a6a1f1dSLionel Sambuc     if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
166*0a6a1f1dSLionel Sambuc         if (started) {
167ebfedea0SLionel Sambuc             BIO_printf(bio_out, "|");
168ebfedea0SLionel Sambuc             err = 1;
169ebfedea0SLionel Sambuc         }
170ebfedea0SLionel Sambuc         BIO_printf(bio_out, "NO_INPUT");
171ebfedea0SLionel Sambuc         started = 1;
172ebfedea0SLionel Sambuc     }
173ebfedea0SLionel Sambuc     /* Check for unknown flags */
174ebfedea0SLionel Sambuc     flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
175ebfedea0SLionel Sambuc         ~ENGINE_CMD_FLAG_STRING &
176*0a6a1f1dSLionel Sambuc         ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
177*0a6a1f1dSLionel Sambuc     if (flags) {
178*0a6a1f1dSLionel Sambuc         if (started)
179*0a6a1f1dSLionel Sambuc             BIO_printf(bio_out, "|");
180ebfedea0SLionel Sambuc         BIO_printf(bio_out, "<0x%04X>", flags);
181ebfedea0SLionel Sambuc     }
182ebfedea0SLionel Sambuc     if (err)
183ebfedea0SLionel Sambuc         BIO_printf(bio_out, "  <illegal flags!>");
184ebfedea0SLionel Sambuc     BIO_printf(bio_out, "\n");
185ebfedea0SLionel Sambuc     return 1;
186ebfedea0SLionel Sambuc }
187ebfedea0SLionel Sambuc 
util_verbose(ENGINE * e,int verbose,BIO * bio_out,const char * indent)188*0a6a1f1dSLionel Sambuc static int util_verbose(ENGINE *e, int verbose, BIO *bio_out,
189*0a6a1f1dSLionel Sambuc                         const char *indent)
190ebfedea0SLionel Sambuc {
191ebfedea0SLionel Sambuc     static const int line_wrap = 78;
192ebfedea0SLionel Sambuc     int num;
193ebfedea0SLionel Sambuc     int ret = 0;
194ebfedea0SLionel Sambuc     char *name = NULL;
195ebfedea0SLionel Sambuc     char *desc = NULL;
196ebfedea0SLionel Sambuc     int flags;
197ebfedea0SLionel Sambuc     int xpos = 0;
198ebfedea0SLionel Sambuc     STACK_OF(OPENSSL_STRING) *cmds = NULL;
199ebfedea0SLionel Sambuc     if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
200ebfedea0SLionel Sambuc         ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
201*0a6a1f1dSLionel Sambuc                             0, NULL, NULL)) <= 0)) {
202ebfedea0SLionel Sambuc # if 0
203ebfedea0SLionel Sambuc         BIO_printf(bio_out, "%s<no control commands>\n", indent);
204ebfedea0SLionel Sambuc # endif
205ebfedea0SLionel Sambuc         return 1;
206ebfedea0SLionel Sambuc     }
207ebfedea0SLionel Sambuc 
208ebfedea0SLionel Sambuc     cmds = sk_OPENSSL_STRING_new_null();
209ebfedea0SLionel Sambuc 
210ebfedea0SLionel Sambuc     if (!cmds)
211ebfedea0SLionel Sambuc         goto err;
212ebfedea0SLionel Sambuc     do {
213ebfedea0SLionel Sambuc         int len;
214ebfedea0SLionel Sambuc         /* Get the command input flags */
215ebfedea0SLionel Sambuc         if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
216ebfedea0SLionel Sambuc                                  NULL, NULL)) < 0)
217ebfedea0SLionel Sambuc             goto err;
218*0a6a1f1dSLionel Sambuc         if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
219ebfedea0SLionel Sambuc             /* Get the command name */
220ebfedea0SLionel Sambuc             if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
221ebfedea0SLionel Sambuc                                    NULL, NULL)) <= 0)
222ebfedea0SLionel Sambuc                 goto err;
223ebfedea0SLionel Sambuc             if ((name = OPENSSL_malloc(len + 1)) == NULL)
224ebfedea0SLionel Sambuc                 goto err;
225ebfedea0SLionel Sambuc             if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
226ebfedea0SLionel Sambuc                             NULL) <= 0)
227ebfedea0SLionel Sambuc                 goto err;
228ebfedea0SLionel Sambuc             /* Get the command description */
229ebfedea0SLionel Sambuc             if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
230ebfedea0SLionel Sambuc                                    NULL, NULL)) < 0)
231ebfedea0SLionel Sambuc                 goto err;
232*0a6a1f1dSLionel Sambuc             if (len > 0) {
233ebfedea0SLionel Sambuc                 if ((desc = OPENSSL_malloc(len + 1)) == NULL)
234ebfedea0SLionel Sambuc                     goto err;
235ebfedea0SLionel Sambuc                 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
236ebfedea0SLionel Sambuc                                 NULL) <= 0)
237ebfedea0SLionel Sambuc                     goto err;
238ebfedea0SLionel Sambuc             }
239ebfedea0SLionel Sambuc             /* Now decide on the output */
240ebfedea0SLionel Sambuc             if (xpos == 0)
241ebfedea0SLionel Sambuc                 /* Do an indent */
242ebfedea0SLionel Sambuc                 xpos = BIO_puts(bio_out, indent);
243ebfedea0SLionel Sambuc             else
244ebfedea0SLionel Sambuc                 /* Otherwise prepend a ", " */
245ebfedea0SLionel Sambuc                 xpos += BIO_printf(bio_out, ", ");
246*0a6a1f1dSLionel Sambuc             if (verbose == 1) {
247*0a6a1f1dSLionel Sambuc                 /*
248*0a6a1f1dSLionel Sambuc                  * We're just listing names, comma-delimited
249*0a6a1f1dSLionel Sambuc                  */
250ebfedea0SLionel Sambuc                 if ((xpos > (int)strlen(indent)) &&
251*0a6a1f1dSLionel Sambuc                     (xpos + (int)strlen(name) > line_wrap)) {
252ebfedea0SLionel Sambuc                     BIO_printf(bio_out, "\n");
253ebfedea0SLionel Sambuc                     xpos = BIO_puts(bio_out, indent);
254ebfedea0SLionel Sambuc                 }
255ebfedea0SLionel Sambuc                 xpos += BIO_printf(bio_out, "%s", name);
256*0a6a1f1dSLionel Sambuc             } else {
257ebfedea0SLionel Sambuc                 /* We're listing names plus descriptions */
258ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "%s: %s\n", name,
259ebfedea0SLionel Sambuc                            (desc == NULL) ? "<no description>" : desc);
260ebfedea0SLionel Sambuc                 /* ... and sometimes input flags */
261*0a6a1f1dSLionel Sambuc                 if ((verbose >= 3) && !util_flags(bio_out, flags, indent))
262ebfedea0SLionel Sambuc                     goto err;
263ebfedea0SLionel Sambuc                 xpos = 0;
264ebfedea0SLionel Sambuc             }
265ebfedea0SLionel Sambuc         }
266*0a6a1f1dSLionel Sambuc         OPENSSL_free(name);
267*0a6a1f1dSLionel Sambuc         name = NULL;
268*0a6a1f1dSLionel Sambuc         if (desc) {
269*0a6a1f1dSLionel Sambuc             OPENSSL_free(desc);
270*0a6a1f1dSLionel Sambuc             desc = NULL;
271*0a6a1f1dSLionel Sambuc         }
272ebfedea0SLionel Sambuc         /* Move to the next command */
273*0a6a1f1dSLionel Sambuc         num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
274ebfedea0SLionel Sambuc     } while (num > 0);
275ebfedea0SLionel Sambuc     if (xpos > 0)
276ebfedea0SLionel Sambuc         BIO_printf(bio_out, "\n");
277ebfedea0SLionel Sambuc     ret = 1;
278ebfedea0SLionel Sambuc  err:
279*0a6a1f1dSLionel Sambuc     if (cmds)
280*0a6a1f1dSLionel Sambuc         sk_OPENSSL_STRING_pop_free(cmds, identity);
281*0a6a1f1dSLionel Sambuc     if (name)
282*0a6a1f1dSLionel Sambuc         OPENSSL_free(name);
283*0a6a1f1dSLionel Sambuc     if (desc)
284*0a6a1f1dSLionel Sambuc         OPENSSL_free(desc);
285ebfedea0SLionel Sambuc     return ret;
286ebfedea0SLionel Sambuc }
287ebfedea0SLionel Sambuc 
util_do_cmds(ENGINE * e,STACK_OF (OPENSSL_STRING)* cmds,BIO * bio_out,const char * indent)288ebfedea0SLionel Sambuc static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
289ebfedea0SLionel Sambuc                          BIO *bio_out, const char *indent)
290ebfedea0SLionel Sambuc {
291ebfedea0SLionel Sambuc     int loop, res, num = sk_OPENSSL_STRING_num(cmds);
292ebfedea0SLionel Sambuc 
293*0a6a1f1dSLionel Sambuc     if (num < 0) {
294ebfedea0SLionel Sambuc         BIO_printf(bio_out, "[Error]: internal stack error\n");
295ebfedea0SLionel Sambuc         return;
296ebfedea0SLionel Sambuc     }
297*0a6a1f1dSLionel Sambuc     for (loop = 0; loop < num; loop++) {
298ebfedea0SLionel Sambuc         char buf[256];
299ebfedea0SLionel Sambuc         const char *cmd, *arg;
300ebfedea0SLionel Sambuc         cmd = sk_OPENSSL_STRING_value(cmds, loop);
301ebfedea0SLionel Sambuc         res = 1;                /* assume success */
302ebfedea0SLionel Sambuc         /* Check if this command has no ":arg" */
303*0a6a1f1dSLionel Sambuc         if ((arg = strstr(cmd, ":")) == NULL) {
304ebfedea0SLionel Sambuc             if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
305ebfedea0SLionel Sambuc                 res = 0;
306*0a6a1f1dSLionel Sambuc         } else {
307*0a6a1f1dSLionel Sambuc             if ((int)(arg - cmd) > 254) {
308ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "[Error]: command name too long\n");
309ebfedea0SLionel Sambuc                 return;
310ebfedea0SLionel Sambuc             }
311ebfedea0SLionel Sambuc             memcpy(buf, cmd, (int)(arg - cmd));
312ebfedea0SLionel Sambuc             buf[arg - cmd] = '\0';
313ebfedea0SLionel Sambuc             arg++;              /* Move past the ":" */
314ebfedea0SLionel Sambuc             /* Call the command with the argument */
315ebfedea0SLionel Sambuc             if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
316ebfedea0SLionel Sambuc                 res = 0;
317ebfedea0SLionel Sambuc         }
318ebfedea0SLionel Sambuc         if (res)
319ebfedea0SLionel Sambuc             BIO_printf(bio_out, "[Success]: %s\n", cmd);
320*0a6a1f1dSLionel Sambuc         else {
321ebfedea0SLionel Sambuc             BIO_printf(bio_out, "[Failure]: %s\n", cmd);
322ebfedea0SLionel Sambuc             ERR_print_errors(bio_out);
323ebfedea0SLionel Sambuc         }
324ebfedea0SLionel Sambuc     }
325ebfedea0SLionel Sambuc }
326ebfedea0SLionel Sambuc 
327ebfedea0SLionel Sambuc int MAIN(int, char **);
328ebfedea0SLionel Sambuc 
MAIN(int argc,char ** argv)329ebfedea0SLionel Sambuc int MAIN(int argc, char **argv)
330ebfedea0SLionel Sambuc {
331ebfedea0SLionel Sambuc     int ret = 1, i;
332ebfedea0SLionel Sambuc     const char **pp;
333ebfedea0SLionel Sambuc     int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
334ebfedea0SLionel Sambuc     ENGINE *e;
335ebfedea0SLionel Sambuc     STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
336ebfedea0SLionel Sambuc     STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
337ebfedea0SLionel Sambuc     STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
338ebfedea0SLionel Sambuc     int badops = 1;
339ebfedea0SLionel Sambuc     BIO *bio_out = NULL;
340ebfedea0SLionel Sambuc     const char *indent = "     ";
341ebfedea0SLionel Sambuc 
342ebfedea0SLionel Sambuc     apps_startup();
343ebfedea0SLionel Sambuc     SSL_load_error_strings();
344ebfedea0SLionel Sambuc 
345ebfedea0SLionel Sambuc     if (bio_err == NULL)
346ebfedea0SLionel Sambuc         bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
347ebfedea0SLionel Sambuc 
348ebfedea0SLionel Sambuc     if (!load_config(bio_err, NULL))
349ebfedea0SLionel Sambuc         goto end;
350ebfedea0SLionel Sambuc     bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
351ebfedea0SLionel Sambuc # ifdef OPENSSL_SYS_VMS
352ebfedea0SLionel Sambuc     {
353ebfedea0SLionel Sambuc         BIO *tmpbio = BIO_new(BIO_f_linebuffer());
354ebfedea0SLionel Sambuc         bio_out = BIO_push(tmpbio, bio_out);
355ebfedea0SLionel Sambuc     }
356ebfedea0SLionel Sambuc # endif
357ebfedea0SLionel Sambuc 
358ebfedea0SLionel Sambuc     argc--;
359ebfedea0SLionel Sambuc     argv++;
360*0a6a1f1dSLionel Sambuc     while (argc >= 1) {
361*0a6a1f1dSLionel Sambuc         if (strncmp(*argv, "-v", 2) == 0) {
362ebfedea0SLionel Sambuc             if (strspn(*argv + 1, "v") < strlen(*argv + 1))
363ebfedea0SLionel Sambuc                 goto skip_arg_loop;
364ebfedea0SLionel Sambuc             if ((verbose = strlen(*argv + 1)) > 4)
365ebfedea0SLionel Sambuc                 goto skip_arg_loop;
366*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-c") == 0)
367ebfedea0SLionel Sambuc             list_cap = 1;
368*0a6a1f1dSLionel Sambuc         else if (strncmp(*argv, "-t", 2) == 0) {
369ebfedea0SLionel Sambuc             test_avail = 1;
370ebfedea0SLionel Sambuc             if (strspn(*argv + 1, "t") < strlen(*argv + 1))
371ebfedea0SLionel Sambuc                 goto skip_arg_loop;
372ebfedea0SLionel Sambuc             if ((test_avail_noise = strlen(*argv + 1) - 1) > 1)
373ebfedea0SLionel Sambuc                 goto skip_arg_loop;
374*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-pre") == 0) {
375*0a6a1f1dSLionel Sambuc             argc--;
376*0a6a1f1dSLionel Sambuc             argv++;
377ebfedea0SLionel Sambuc             if (argc == 0)
378ebfedea0SLionel Sambuc                 goto skip_arg_loop;
379ebfedea0SLionel Sambuc             sk_OPENSSL_STRING_push(pre_cmds, *argv);
380*0a6a1f1dSLionel Sambuc         } else if (strcmp(*argv, "-post") == 0) {
381*0a6a1f1dSLionel Sambuc             argc--;
382*0a6a1f1dSLionel Sambuc             argv++;
383ebfedea0SLionel Sambuc             if (argc == 0)
384ebfedea0SLionel Sambuc                 goto skip_arg_loop;
385ebfedea0SLionel Sambuc             sk_OPENSSL_STRING_push(post_cmds, *argv);
386*0a6a1f1dSLionel Sambuc         } else if ((strncmp(*argv, "-h", 2) == 0) ||
387ebfedea0SLionel Sambuc                    (strcmp(*argv, "-?") == 0))
388ebfedea0SLionel Sambuc             goto skip_arg_loop;
389ebfedea0SLionel Sambuc         else
390ebfedea0SLionel Sambuc             sk_OPENSSL_STRING_push(engines, *argv);
391ebfedea0SLionel Sambuc         argc--;
392ebfedea0SLionel Sambuc         argv++;
393ebfedea0SLionel Sambuc     }
394ebfedea0SLionel Sambuc     /* Looks like everything went OK */
395ebfedea0SLionel Sambuc     badops = 0;
396ebfedea0SLionel Sambuc  skip_arg_loop:
397ebfedea0SLionel Sambuc 
398*0a6a1f1dSLionel Sambuc     if (badops) {
399ebfedea0SLionel Sambuc         for (pp = engine_usage; (*pp != NULL); pp++)
400ebfedea0SLionel Sambuc             BIO_printf(bio_err, "%s", *pp);
401ebfedea0SLionel Sambuc         goto end;
402ebfedea0SLionel Sambuc     }
403ebfedea0SLionel Sambuc 
404*0a6a1f1dSLionel Sambuc     if (sk_OPENSSL_STRING_num(engines) == 0) {
405*0a6a1f1dSLionel Sambuc         for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
406ebfedea0SLionel Sambuc             sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
407ebfedea0SLionel Sambuc         }
408ebfedea0SLionel Sambuc     }
409ebfedea0SLionel Sambuc 
410*0a6a1f1dSLionel Sambuc     for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
411ebfedea0SLionel Sambuc         const char *id = sk_OPENSSL_STRING_value(engines, i);
412*0a6a1f1dSLionel Sambuc         if ((e = ENGINE_by_id(id)) != NULL) {
413ebfedea0SLionel Sambuc             const char *name = ENGINE_get_name(e);
414*0a6a1f1dSLionel Sambuc             /*
415*0a6a1f1dSLionel Sambuc              * Do "id" first, then "name". Easier to auto-parse.
416*0a6a1f1dSLionel Sambuc              */
417ebfedea0SLionel Sambuc             BIO_printf(bio_out, "(%s) %s\n", id, name);
418ebfedea0SLionel Sambuc             util_do_cmds(e, pre_cmds, bio_out, indent);
419*0a6a1f1dSLionel Sambuc             if (strcmp(ENGINE_get_id(e), id) != 0) {
420ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "Loaded: (%s) %s\n",
421ebfedea0SLionel Sambuc                            ENGINE_get_id(e), ENGINE_get_name(e));
422ebfedea0SLionel Sambuc             }
423*0a6a1f1dSLionel Sambuc             if (list_cap) {
424ebfedea0SLionel Sambuc                 int cap_size = 256;
425ebfedea0SLionel Sambuc                 char *cap_buf = NULL;
426ebfedea0SLionel Sambuc                 int k, n;
427ebfedea0SLionel Sambuc                 const int *nids;
428ebfedea0SLionel Sambuc                 ENGINE_CIPHERS_PTR fn_c;
429ebfedea0SLionel Sambuc                 ENGINE_DIGESTS_PTR fn_d;
430ebfedea0SLionel Sambuc                 ENGINE_PKEY_METHS_PTR fn_pk;
431ebfedea0SLionel Sambuc 
432ebfedea0SLionel Sambuc                 if (ENGINE_get_RSA(e) != NULL
433*0a6a1f1dSLionel Sambuc                     && !append_buf(&cap_buf, "RSA", &cap_size, 256))
434ebfedea0SLionel Sambuc                     goto end;
435ebfedea0SLionel Sambuc                 if (ENGINE_get_DSA(e) != NULL
436*0a6a1f1dSLionel Sambuc                     && !append_buf(&cap_buf, "DSA", &cap_size, 256))
437ebfedea0SLionel Sambuc                     goto end;
438ebfedea0SLionel Sambuc                 if (ENGINE_get_DH(e) != NULL
439*0a6a1f1dSLionel Sambuc                     && !append_buf(&cap_buf, "DH", &cap_size, 256))
440ebfedea0SLionel Sambuc                     goto end;
441ebfedea0SLionel Sambuc                 if (ENGINE_get_RAND(e) != NULL
442*0a6a1f1dSLionel Sambuc                     && !append_buf(&cap_buf, "RAND", &cap_size, 256))
443ebfedea0SLionel Sambuc                     goto end;
444ebfedea0SLionel Sambuc 
445ebfedea0SLionel Sambuc                 fn_c = ENGINE_get_ciphers(e);
446*0a6a1f1dSLionel Sambuc                 if (!fn_c)
447*0a6a1f1dSLionel Sambuc                     goto skip_ciphers;
448ebfedea0SLionel Sambuc                 n = fn_c(e, NULL, &nids, 0);
449ebfedea0SLionel Sambuc                 for (k = 0; k < n; ++k)
450ebfedea0SLionel Sambuc                     if (!append_buf(&cap_buf,
451*0a6a1f1dSLionel Sambuc                                     OBJ_nid2sn(nids[k]), &cap_size, 256))
452ebfedea0SLionel Sambuc                         goto end;
453ebfedea0SLionel Sambuc 
454ebfedea0SLionel Sambuc  skip_ciphers:
455ebfedea0SLionel Sambuc                 fn_d = ENGINE_get_digests(e);
456*0a6a1f1dSLionel Sambuc                 if (!fn_d)
457*0a6a1f1dSLionel Sambuc                     goto skip_digests;
458ebfedea0SLionel Sambuc                 n = fn_d(e, NULL, &nids, 0);
459ebfedea0SLionel Sambuc                 for (k = 0; k < n; ++k)
460ebfedea0SLionel Sambuc                     if (!append_buf(&cap_buf,
461*0a6a1f1dSLionel Sambuc                                     OBJ_nid2sn(nids[k]), &cap_size, 256))
462ebfedea0SLionel Sambuc                         goto end;
463ebfedea0SLionel Sambuc 
464ebfedea0SLionel Sambuc  skip_digests:
465ebfedea0SLionel Sambuc                 fn_pk = ENGINE_get_pkey_meths(e);
466*0a6a1f1dSLionel Sambuc                 if (!fn_pk)
467*0a6a1f1dSLionel Sambuc                     goto skip_pmeths;
468ebfedea0SLionel Sambuc                 n = fn_pk(e, NULL, &nids, 0);
469ebfedea0SLionel Sambuc                 for (k = 0; k < n; ++k)
470ebfedea0SLionel Sambuc                     if (!append_buf(&cap_buf,
471*0a6a1f1dSLionel Sambuc                                     OBJ_nid2sn(nids[k]), &cap_size, 256))
472ebfedea0SLionel Sambuc                         goto end;
473ebfedea0SLionel Sambuc  skip_pmeths:
474ebfedea0SLionel Sambuc                 if (cap_buf && (*cap_buf != '\0'))
475ebfedea0SLionel Sambuc                     BIO_printf(bio_out, " [%s]\n", cap_buf);
476ebfedea0SLionel Sambuc 
477ebfedea0SLionel Sambuc                 OPENSSL_free(cap_buf);
478ebfedea0SLionel Sambuc             }
479*0a6a1f1dSLionel Sambuc             if (test_avail) {
480ebfedea0SLionel Sambuc                 BIO_printf(bio_out, "%s", indent);
481*0a6a1f1dSLionel Sambuc                 if (ENGINE_init(e)) {
482ebfedea0SLionel Sambuc                     BIO_printf(bio_out, "[ available ]\n");
483ebfedea0SLionel Sambuc                     util_do_cmds(e, post_cmds, bio_out, indent);
484ebfedea0SLionel Sambuc                     ENGINE_finish(e);
485*0a6a1f1dSLionel Sambuc                 } else {
486ebfedea0SLionel Sambuc                     BIO_printf(bio_out, "[ unavailable ]\n");
487ebfedea0SLionel Sambuc                     if (test_avail_noise)
488ebfedea0SLionel Sambuc                         ERR_print_errors_fp(stdout);
489ebfedea0SLionel Sambuc                     ERR_clear_error();
490ebfedea0SLionel Sambuc                 }
491ebfedea0SLionel Sambuc             }
492ebfedea0SLionel Sambuc             if ((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
493ebfedea0SLionel Sambuc                 goto end;
494ebfedea0SLionel Sambuc             ENGINE_free(e);
495*0a6a1f1dSLionel Sambuc         } else
496ebfedea0SLionel Sambuc             ERR_print_errors(bio_err);
497ebfedea0SLionel Sambuc     }
498ebfedea0SLionel Sambuc 
499ebfedea0SLionel Sambuc     ret = 0;
500ebfedea0SLionel Sambuc  end:
501ebfedea0SLionel Sambuc 
502ebfedea0SLionel Sambuc     ERR_print_errors(bio_err);
503ebfedea0SLionel Sambuc     sk_OPENSSL_STRING_pop_free(engines, identity);
504ebfedea0SLionel Sambuc     sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
505ebfedea0SLionel Sambuc     sk_OPENSSL_STRING_pop_free(post_cmds, identity);
506*0a6a1f1dSLionel Sambuc     if (bio_out != NULL)
507*0a6a1f1dSLionel Sambuc         BIO_free_all(bio_out);
508ebfedea0SLionel Sambuc     apps_shutdown();
509ebfedea0SLionel Sambuc     OPENSSL_EXIT(ret);
510ebfedea0SLionel Sambuc }
511ebfedea0SLionel Sambuc #else
512ebfedea0SLionel Sambuc 
513ebfedea0SLionel Sambuc # if PEDANTIC
514ebfedea0SLionel Sambuc static void *dummy = &dummy;
515ebfedea0SLionel Sambuc # endif
516ebfedea0SLionel Sambuc 
517ebfedea0SLionel Sambuc #endif
518