1 /* apps/engine.c -*- mode: C; c-file-style: "eay" -*- */
2 /* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #ifndef OPENSSL_NO_ENGINE
64 #ifdef OPENSSL_NO_STDIO
68 #include <openssl/err.h>
69 #include <openssl/engine.h>
70 #include <openssl/ssl.h>
73 #define PROG engine_main
75 static const char *engine_usage[]={
76 "usage: engine opts [engine ...]\n",
77 " -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n",
78 " -vv will additionally display each command's description\n",
79 " -vvv will also add the input flags for each command\n",
80 " -vvvv will also show internal input flags\n",
81 " -c - for each engine, also list the capabilities\n",
82 " -t[t] - for each engine, check that they are really available\n",
83 " -tt will display error trace for unavailable engines\n",
84 " -pre <cmd> - runs command 'cmd' against the ENGINE before any attempts\n",
85 " to load it (if -t is used)\n",
86 " -post <cmd> - runs command 'cmd' against the ENGINE after loading it\n",
87 " (only used if -t is also provided)\n",
88 " NB: -pre and -post will be applied to all ENGINEs supplied on the command\n",
89 " line, or all supported ENGINEs if none are specified.\n",
90 " Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n",
91 " argument \"/lib/libdriver.so\".\n",
95 static void identity(void *ptr)
100 static int append_buf(char **buf, const char *s, int *size, int step)
107 *buf = OPENSSL_malloc(*size);
116 if (strlen(*buf) + strlen(s) >= (unsigned int)*size)
119 *buf = OPENSSL_realloc(*buf, *size);
126 BUF_strlcat(*buf, ", ", *size);
127 BUF_strlcat(*buf, s, *size);
132 static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
134 int started = 0, err = 0;
135 /* Indent before displaying input flags */
136 BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
139 BIO_printf(bio_out, "<no flags>\n");
142 /* If the object is internal, mark it in a way that shows instead of
143 * having it part of all the other flags, even if it really is. */
144 if(flags & ENGINE_CMD_FLAG_INTERNAL)
146 BIO_printf(bio_out, "[Internal] ");
149 if(flags & ENGINE_CMD_FLAG_NUMERIC)
151 BIO_printf(bio_out, "NUMERIC");
154 /* Now we check that no combinations of the mutually exclusive NUMERIC,
155 * STRING, and NO_INPUT flags have been used. Future flags that can be
156 * OR'd together with these would need to added after these to preserve
157 * the testing logic. */
158 if(flags & ENGINE_CMD_FLAG_STRING)
162 BIO_printf(bio_out, "|");
165 BIO_printf(bio_out, "STRING");
168 if(flags & ENGINE_CMD_FLAG_NO_INPUT)
172 BIO_printf(bio_out, "|");
175 BIO_printf(bio_out, "NO_INPUT");
178 /* Check for unknown flags */
179 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
180 ~ENGINE_CMD_FLAG_STRING &
181 ~ENGINE_CMD_FLAG_NO_INPUT &
182 ~ENGINE_CMD_FLAG_INTERNAL;
185 if(started) BIO_printf(bio_out, "|");
186 BIO_printf(bio_out, "<0x%04X>", flags);
189 BIO_printf(bio_out, " <illegal flags!>");
190 BIO_printf(bio_out, "\n");
194 static int util_verbose(ENGINE *e, int verbose, BIO *bio_out, const char *indent)
196 static const int line_wrap = 78;
204 if(!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
205 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
206 0, NULL, NULL)) <= 0))
209 BIO_printf(bio_out, "%s<no control commands>\n", indent);
214 cmds = sk_new_null();
220 /* Get the command input flags */
221 if((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
224 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4)
226 /* Get the command name */
227 if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
230 if((name = OPENSSL_malloc(len + 1)) == NULL)
232 if(ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
235 /* Get the command description */
236 if((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
241 if((desc = OPENSSL_malloc(len + 1)) == NULL)
243 if(ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
247 /* Now decide on the output */
250 xpos = BIO_printf(bio_out, indent);
252 /* Otherwise prepend a ", " */
253 xpos += BIO_printf(bio_out, ", ");
256 /* We're just listing names, comma-delimited */
257 if((xpos > (int)strlen(indent)) &&
258 (xpos + (int)strlen(name) > line_wrap))
260 BIO_printf(bio_out, "\n");
261 xpos = BIO_printf(bio_out, indent);
263 xpos += BIO_printf(bio_out, "%s", name);
267 /* We're listing names plus descriptions */
268 BIO_printf(bio_out, "%s: %s\n", name,
269 (desc == NULL) ? "<no description>" : desc);
270 /* ... and sometimes input flags */
271 if((verbose >= 3) && !util_flags(bio_out, flags,
277 OPENSSL_free(name); name = NULL;
278 if(desc) { OPENSSL_free(desc); desc = NULL; }
279 /* Move to the next command */
280 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE,
284 BIO_printf(bio_out, "\n");
287 if(cmds) sk_pop_free(cmds, identity);
288 if(name) OPENSSL_free(name);
289 if(desc) OPENSSL_free(desc);
293 static void util_do_cmds(ENGINE *e, STACK *cmds, BIO *bio_out, const char *indent)
295 int loop, res, num = sk_num(cmds);
298 BIO_printf(bio_out, "[Error]: internal stack error\n");
301 for(loop = 0; loop < num; loop++)
304 const char *cmd, *arg;
305 cmd = sk_value(cmds, loop);
306 res = 1; /* assume success */
307 /* Check if this command has no ":arg" */
308 if((arg = strstr(cmd, ":")) == NULL)
310 if(!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
315 if((int)(arg - cmd) > 254)
317 BIO_printf(bio_out,"[Error]: command name too long\n");
320 memcpy(buf, cmd, (int)(arg - cmd));
322 arg++; /* Move past the ":" */
323 /* Call the command with the argument */
324 if(!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
328 BIO_printf(bio_out, "[Success]: %s\n", cmd);
331 BIO_printf(bio_out, "[Failure]: %s\n", cmd);
332 ERR_print_errors(bio_out);
337 int MAIN(int, char **);
339 int MAIN(int argc, char **argv)
343 int verbose=0, list_cap=0, test_avail=0, test_avail_noise = 0;
345 STACK *engines = sk_new_null();
346 STACK *pre_cmds = sk_new_null();
347 STACK *post_cmds = sk_new_null();
350 const char *indent = " ";
353 SSL_load_error_strings();
356 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
358 if (!load_config(bio_err, NULL))
360 bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
361 #ifdef OPENSSL_SYS_VMS
363 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
364 bio_out = BIO_push(tmpbio, bio_out);
372 if (strncmp(*argv,"-v",2) == 0)
374 if(strspn(*argv + 1, "v") < strlen(*argv + 1))
376 if((verbose=strlen(*argv + 1)) > 4)
379 else if (strcmp(*argv,"-c") == 0)
381 else if (strncmp(*argv,"-t",2) == 0)
384 if(strspn(*argv + 1, "t") < strlen(*argv + 1))
386 if((test_avail_noise = strlen(*argv + 1) - 1) > 1)
389 else if (strcmp(*argv,"-pre") == 0)
394 sk_push(pre_cmds,*argv);
396 else if (strcmp(*argv,"-post") == 0)
401 sk_push(post_cmds,*argv);
403 else if ((strncmp(*argv,"-h",2) == 0) ||
404 (strcmp(*argv,"-?") == 0))
407 sk_push(engines,*argv);
411 /* Looks like everything went OK */
417 for (pp=engine_usage; (*pp != NULL); pp++)
418 BIO_printf(bio_err,"%s",*pp);
422 if (sk_num(engines) == 0)
424 for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
426 sk_push(engines,(char *)ENGINE_get_id(e));
430 for (i=0; i<sk_num(engines); i++)
432 const char *id = sk_value(engines,i);
433 if ((e = ENGINE_by_id(id)) != NULL)
435 const char *name = ENGINE_get_name(e);
436 /* Do "id" first, then "name". Easier to auto-parse. */
437 BIO_printf(bio_out, "(%s) %s\n", id, name);
438 util_do_cmds(e, pre_cmds, bio_out, indent);
439 if (strcmp(ENGINE_get_id(e), id) != 0)
441 BIO_printf(bio_out, "Loaded: (%s) %s\n",
442 ENGINE_get_id(e), ENGINE_get_name(e));
447 char *cap_buf = NULL;
450 ENGINE_CIPHERS_PTR fn_c;
451 ENGINE_DIGESTS_PTR fn_d;
452 ENGINE_PKEY_METHS_PTR fn_pk;
454 if (ENGINE_get_RSA(e) != NULL
455 && !append_buf(&cap_buf, "RSA",
458 if (ENGINE_get_DSA(e) != NULL
459 && !append_buf(&cap_buf, "DSA",
462 if (ENGINE_get_DH(e) != NULL
463 && !append_buf(&cap_buf, "DH",
466 if (ENGINE_get_RAND(e) != NULL
467 && !append_buf(&cap_buf, "RAND",
471 fn_c = ENGINE_get_ciphers(e);
472 if(!fn_c) goto skip_ciphers;
473 n = fn_c(e, NULL, &nids, 0);
474 for(k=0 ; k < n ; ++k)
475 if(!append_buf(&cap_buf,
481 fn_d = ENGINE_get_digests(e);
482 if(!fn_d) goto skip_digests;
483 n = fn_d(e, NULL, &nids, 0);
484 for(k=0 ; k < n ; ++k)
485 if(!append_buf(&cap_buf,
491 fn_pk = ENGINE_get_pkey_meths(e);
492 if(!fn_pk) goto skip_pmeths;
493 n = fn_pk(e, NULL, &nids, 0);
494 for(k=0 ; k < n ; ++k)
495 if(!append_buf(&cap_buf,
500 if (cap_buf && (*cap_buf != '\0'))
501 BIO_printf(bio_out, " [%s]\n", cap_buf);
503 OPENSSL_free(cap_buf);
507 BIO_printf(bio_out, "%s", indent);
510 BIO_printf(bio_out, "[ available ]\n");
511 util_do_cmds(e, post_cmds, bio_out, indent);
516 BIO_printf(bio_out, "[ unavailable ]\n");
518 ERR_print_errors_fp(stdout);
522 if((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
527 ERR_print_errors(bio_err);
533 ERR_print_errors(bio_err);
534 sk_pop_free(engines, identity);
535 sk_pop_free(pre_cmds, identity);
536 sk_pop_free(post_cmds, identity);
537 if (bio_out != NULL) BIO_free_all(bio_out);
544 static void *dummy=&dummy;