3 * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
63 #ifdef OPENSSL_NO_STDIO
67 #include <openssl/err.h>
68 #ifndef OPENSSL_NO_ENGINE
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(char *ptr)
100 static int append_buf(char **buf, const char *s, int *size, int step)
104 *buf = OPENSSL_malloc(*size);
110 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
112 *buf = OPENSSL_realloc(*buf, *size);
119 BUF_strlcat(*buf, ", ", *size);
120 BUF_strlcat(*buf, s, *size);
125 static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
127 int started = 0, err = 0;
128 /* Indent before displaying input flags */
129 BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
131 BIO_printf(bio_out, "<no flags>\n");
135 * If the object is internal, mark it in a way that shows instead of
136 * having it part of all the other flags, even if it really is.
138 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
139 BIO_printf(bio_out, "[Internal] ");
142 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
143 BIO_printf(bio_out, "NUMERIC");
147 * Now we check that no combinations of the mutually exclusive NUMERIC,
148 * STRING, and NO_INPUT flags have been used. Future flags that can be
149 * OR'd together with these would need to added after these to preserve
152 if (flags & ENGINE_CMD_FLAG_STRING) {
154 BIO_printf(bio_out, "|");
157 BIO_printf(bio_out, "STRING");
160 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
162 BIO_printf(bio_out, "|");
165 BIO_printf(bio_out, "NO_INPUT");
168 /* Check for unknown flags */
169 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
170 ~ENGINE_CMD_FLAG_STRING &
171 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
174 BIO_printf(bio_out, "|");
175 BIO_printf(bio_out, "<0x%04X>", flags);
178 BIO_printf(bio_out, " <illegal flags!>");
179 BIO_printf(bio_out, "\n");
183 static int util_verbose(ENGINE *e, int verbose, BIO *bio_out,
186 static const int line_wrap = 78;
193 STACK_OF(OPENSSL_STRING) *cmds = NULL;
194 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
195 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
196 0, NULL, NULL)) <= 0)) {
198 BIO_printf(bio_out, "%s<no control commands>\n", indent);
203 cmds = sk_OPENSSL_STRING_new_null();
209 /* Get the command input flags */
210 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
213 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
214 /* Get the command name */
215 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
218 if ((name = OPENSSL_malloc(len + 1)) == NULL)
220 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
223 /* Get the command description */
224 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
228 if ((desc = OPENSSL_malloc(len + 1)) == NULL)
230 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
234 /* Now decide on the output */
237 xpos = BIO_puts(bio_out, indent);
239 /* Otherwise prepend a ", " */
240 xpos += BIO_printf(bio_out, ", ");
243 * We're just listing names, comma-delimited
245 if ((xpos > (int)strlen(indent)) &&
246 (xpos + (int)strlen(name) > line_wrap)) {
247 BIO_printf(bio_out, "\n");
248 xpos = BIO_puts(bio_out, indent);
250 xpos += BIO_printf(bio_out, "%s", name);
252 /* We're listing names plus descriptions */
253 BIO_printf(bio_out, "%s: %s\n", name,
254 (desc == NULL) ? "<no description>" : desc);
255 /* ... and sometimes input flags */
256 if ((verbose >= 3) && !util_flags(bio_out, flags, indent))
267 /* Move to the next command */
268 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
271 BIO_printf(bio_out, "\n");
275 sk_OPENSSL_STRING_pop_free(cmds, identity);
283 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
284 BIO *bio_out, const char *indent)
286 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
289 BIO_printf(bio_out, "[Error]: internal stack error\n");
292 for (loop = 0; loop < num; loop++) {
294 const char *cmd, *arg;
295 cmd = sk_OPENSSL_STRING_value(cmds, loop);
296 res = 1; /* assume success */
297 /* Check if this command has no ":arg" */
298 if ((arg = strstr(cmd, ":")) == NULL) {
299 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
302 if ((int)(arg - cmd) > 254) {
303 BIO_printf(bio_out, "[Error]: command name too long\n");
306 memcpy(buf, cmd, (int)(arg - cmd));
307 buf[arg - cmd] = '\0';
308 arg++; /* Move past the ":" */
309 /* Call the command with the argument */
310 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
314 BIO_printf(bio_out, "[Success]: %s\n", cmd);
316 BIO_printf(bio_out, "[Failure]: %s\n", cmd);
317 ERR_print_errors(bio_out);
322 int MAIN(int, char **);
324 int MAIN(int argc, char **argv)
328 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
330 STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
331 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
332 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
335 const char *indent = " ";
338 SSL_load_error_strings();
341 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
343 if (!load_config(bio_err, NULL))
345 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
346 # ifdef OPENSSL_SYS_VMS
348 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
349 bio_out = BIO_push(tmpbio, bio_out);
356 if (strncmp(*argv, "-v", 2) == 0) {
357 if (strspn(*argv + 1, "v") < strlen(*argv + 1))
359 if ((verbose = strlen(*argv + 1)) > 4)
361 } else if (strcmp(*argv, "-c") == 0)
363 else if (strncmp(*argv, "-t", 2) == 0) {
365 if (strspn(*argv + 1, "t") < strlen(*argv + 1))
367 if ((test_avail_noise = strlen(*argv + 1) - 1) > 1)
369 } else if (strcmp(*argv, "-pre") == 0) {
374 sk_OPENSSL_STRING_push(pre_cmds, *argv);
375 } else if (strcmp(*argv, "-post") == 0) {
380 sk_OPENSSL_STRING_push(post_cmds, *argv);
381 } else if ((strncmp(*argv, "-h", 2) == 0) ||
382 (strcmp(*argv, "-?") == 0))
385 sk_OPENSSL_STRING_push(engines, *argv);
389 /* Looks like everything went OK */
394 for (pp = engine_usage; (*pp != NULL); pp++)
395 BIO_printf(bio_err, "%s", *pp);
399 if (sk_OPENSSL_STRING_num(engines) == 0) {
400 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
401 sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
405 for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
406 const char *id = sk_OPENSSL_STRING_value(engines, i);
407 if ((e = ENGINE_by_id(id)) != NULL) {
408 const char *name = ENGINE_get_name(e);
410 * Do "id" first, then "name". Easier to auto-parse.
412 BIO_printf(bio_out, "(%s) %s\n", id, name);
413 util_do_cmds(e, pre_cmds, bio_out, indent);
414 if (strcmp(ENGINE_get_id(e), id) != 0) {
415 BIO_printf(bio_out, "Loaded: (%s) %s\n",
416 ENGINE_get_id(e), ENGINE_get_name(e));
420 char *cap_buf = NULL;
423 ENGINE_CIPHERS_PTR fn_c;
424 ENGINE_DIGESTS_PTR fn_d;
425 ENGINE_PKEY_METHS_PTR fn_pk;
427 if (ENGINE_get_RSA(e) != NULL
428 && !append_buf(&cap_buf, "RSA", &cap_size, 256))
430 if (ENGINE_get_DSA(e) != NULL
431 && !append_buf(&cap_buf, "DSA", &cap_size, 256))
433 if (ENGINE_get_DH(e) != NULL
434 && !append_buf(&cap_buf, "DH", &cap_size, 256))
436 if (ENGINE_get_RAND(e) != NULL
437 && !append_buf(&cap_buf, "RAND", &cap_size, 256))
440 fn_c = ENGINE_get_ciphers(e);
443 n = fn_c(e, NULL, &nids, 0);
444 for (k = 0; k < n; ++k)
445 if (!append_buf(&cap_buf,
446 OBJ_nid2sn(nids[k]), &cap_size, 256))
450 fn_d = ENGINE_get_digests(e);
453 n = fn_d(e, NULL, &nids, 0);
454 for (k = 0; k < n; ++k)
455 if (!append_buf(&cap_buf,
456 OBJ_nid2sn(nids[k]), &cap_size, 256))
460 fn_pk = ENGINE_get_pkey_meths(e);
463 n = fn_pk(e, NULL, &nids, 0);
464 for (k = 0; k < n; ++k)
465 if (!append_buf(&cap_buf,
466 OBJ_nid2sn(nids[k]), &cap_size, 256))
469 if (cap_buf && (*cap_buf != '\0'))
470 BIO_printf(bio_out, " [%s]\n", cap_buf);
472 OPENSSL_free(cap_buf);
475 BIO_printf(bio_out, "%s", indent);
476 if (ENGINE_init(e)) {
477 BIO_printf(bio_out, "[ available ]\n");
478 util_do_cmds(e, post_cmds, bio_out, indent);
481 BIO_printf(bio_out, "[ unavailable ]\n");
482 if (test_avail_noise)
483 ERR_print_errors_fp(stdout);
487 if ((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
491 ERR_print_errors(bio_err);
497 ERR_print_errors(bio_err);
498 sk_OPENSSL_STRING_pop_free(engines, identity);
499 sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
500 sk_OPENSSL_STRING_pop_free(post_cmds, identity);
502 BIO_free_all(bio_out);
509 static void *dummy = &dummy;