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) {
114 *buf = OPENSSL_realloc(*buf, *size);
122 BUF_strlcat(*buf, ", ", *size);
123 BUF_strlcat(*buf, s, *size);
128 static int util_flags(BIO *bio_out, unsigned int flags, const char *indent)
130 int started = 0, err = 0;
131 /* Indent before displaying input flags */
132 BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
134 BIO_printf(bio_out, "<no flags>\n");
138 * If the object is internal, mark it in a way that shows instead of
139 * having it part of all the other flags, even if it really is.
141 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
142 BIO_printf(bio_out, "[Internal] ");
145 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
146 BIO_printf(bio_out, "NUMERIC");
150 * Now we check that no combinations of the mutually exclusive NUMERIC,
151 * STRING, and NO_INPUT flags have been used. Future flags that can be
152 * OR'd together with these would need to added after these to preserve
155 if (flags & ENGINE_CMD_FLAG_STRING) {
157 BIO_printf(bio_out, "|");
160 BIO_printf(bio_out, "STRING");
163 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
165 BIO_printf(bio_out, "|");
168 BIO_printf(bio_out, "NO_INPUT");
171 /* Check for unknown flags */
172 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
173 ~ENGINE_CMD_FLAG_STRING &
174 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
177 BIO_printf(bio_out, "|");
178 BIO_printf(bio_out, "<0x%04X>", flags);
181 BIO_printf(bio_out, " <illegal flags!>");
182 BIO_printf(bio_out, "\n");
186 static int util_verbose(ENGINE *e, int verbose, BIO *bio_out,
189 static const int line_wrap = 78;
196 STACK_OF(OPENSSL_STRING) *cmds = NULL;
197 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
198 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
199 0, NULL, NULL)) <= 0)) {
201 BIO_printf(bio_out, "%s<no control commands>\n", indent);
206 cmds = sk_OPENSSL_STRING_new_null();
212 /* Get the command input flags */
213 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
216 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
217 /* Get the command name */
218 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
221 if ((name = OPENSSL_malloc(len + 1)) == NULL)
223 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
226 /* Get the command description */
227 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
231 if ((desc = OPENSSL_malloc(len + 1)) == NULL)
233 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
237 /* Now decide on the output */
240 xpos = BIO_puts(bio_out, indent);
242 /* Otherwise prepend a ", " */
243 xpos += BIO_printf(bio_out, ", ");
246 * We're just listing names, comma-delimited
248 if ((xpos > (int)strlen(indent)) &&
249 (xpos + (int)strlen(name) > line_wrap)) {
250 BIO_printf(bio_out, "\n");
251 xpos = BIO_puts(bio_out, indent);
253 xpos += BIO_printf(bio_out, "%s", name);
255 /* We're listing names plus descriptions */
256 BIO_printf(bio_out, "%s: %s\n", name,
257 (desc == NULL) ? "<no description>" : desc);
258 /* ... and sometimes input flags */
259 if ((verbose >= 3) && !util_flags(bio_out, flags, indent))
270 /* Move to the next command */
271 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
274 BIO_printf(bio_out, "\n");
278 sk_OPENSSL_STRING_pop_free(cmds, identity);
286 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
287 BIO *bio_out, const char *indent)
289 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
292 BIO_printf(bio_out, "[Error]: internal stack error\n");
295 for (loop = 0; loop < num; loop++) {
297 const char *cmd, *arg;
298 cmd = sk_OPENSSL_STRING_value(cmds, loop);
299 res = 1; /* assume success */
300 /* Check if this command has no ":arg" */
301 if ((arg = strstr(cmd, ":")) == NULL) {
302 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
305 if ((int)(arg - cmd) > 254) {
306 BIO_printf(bio_out, "[Error]: command name too long\n");
309 memcpy(buf, cmd, (int)(arg - cmd));
310 buf[arg - cmd] = '\0';
311 arg++; /* Move past the ":" */
312 /* Call the command with the argument */
313 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
317 BIO_printf(bio_out, "[Success]: %s\n", cmd);
319 BIO_printf(bio_out, "[Failure]: %s\n", cmd);
320 ERR_print_errors(bio_out);
325 int MAIN(int, char **);
327 int MAIN(int argc, char **argv)
331 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
333 STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
334 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
335 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
338 const char *indent = " ";
341 SSL_load_error_strings();
344 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
346 if (!load_config(bio_err, NULL))
348 bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
349 # ifdef OPENSSL_SYS_VMS
351 BIO *tmpbio = BIO_new(BIO_f_linebuffer());
352 bio_out = BIO_push(tmpbio, bio_out);
359 if (strncmp(*argv, "-v", 2) == 0) {
360 if (strspn(*argv + 1, "v") < strlen(*argv + 1))
362 if ((verbose = strlen(*argv + 1)) > 4)
364 } else if (strcmp(*argv, "-c") == 0)
366 else if (strncmp(*argv, "-t", 2) == 0) {
368 if (strspn(*argv + 1, "t") < strlen(*argv + 1))
370 if ((test_avail_noise = strlen(*argv + 1) - 1) > 1)
372 } else if (strcmp(*argv, "-pre") == 0) {
377 sk_OPENSSL_STRING_push(pre_cmds, *argv);
378 } else if (strcmp(*argv, "-post") == 0) {
383 sk_OPENSSL_STRING_push(post_cmds, *argv);
384 } else if ((strncmp(*argv, "-h", 2) == 0) ||
385 (strcmp(*argv, "-?") == 0))
388 sk_OPENSSL_STRING_push(engines, *argv);
392 /* Looks like everything went OK */
397 for (pp = engine_usage; (*pp != NULL); pp++)
398 BIO_printf(bio_err, "%s", *pp);
402 if (sk_OPENSSL_STRING_num(engines) == 0) {
403 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
404 sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
408 for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
409 const char *id = sk_OPENSSL_STRING_value(engines, i);
410 if ((e = ENGINE_by_id(id)) != NULL) {
411 const char *name = ENGINE_get_name(e);
413 * Do "id" first, then "name". Easier to auto-parse.
415 BIO_printf(bio_out, "(%s) %s\n", id, name);
416 util_do_cmds(e, pre_cmds, bio_out, indent);
417 if (strcmp(ENGINE_get_id(e), id) != 0) {
418 BIO_printf(bio_out, "Loaded: (%s) %s\n",
419 ENGINE_get_id(e), ENGINE_get_name(e));
423 char *cap_buf = NULL;
426 ENGINE_CIPHERS_PTR fn_c;
427 ENGINE_DIGESTS_PTR fn_d;
428 ENGINE_PKEY_METHS_PTR fn_pk;
430 if (ENGINE_get_RSA(e) != NULL
431 && !append_buf(&cap_buf, "RSA", &cap_size, 256))
433 if (ENGINE_get_DSA(e) != NULL
434 && !append_buf(&cap_buf, "DSA", &cap_size, 256))
436 if (ENGINE_get_DH(e) != NULL
437 && !append_buf(&cap_buf, "DH", &cap_size, 256))
439 if (ENGINE_get_RAND(e) != NULL
440 && !append_buf(&cap_buf, "RAND", &cap_size, 256))
443 fn_c = ENGINE_get_ciphers(e);
446 n = fn_c(e, NULL, &nids, 0);
447 for (k = 0; k < n; ++k)
448 if (!append_buf(&cap_buf,
449 OBJ_nid2sn(nids[k]), &cap_size, 256))
453 fn_d = ENGINE_get_digests(e);
456 n = fn_d(e, NULL, &nids, 0);
457 for (k = 0; k < n; ++k)
458 if (!append_buf(&cap_buf,
459 OBJ_nid2sn(nids[k]), &cap_size, 256))
463 fn_pk = ENGINE_get_pkey_meths(e);
466 n = fn_pk(e, NULL, &nids, 0);
467 for (k = 0; k < n; ++k)
468 if (!append_buf(&cap_buf,
469 OBJ_nid2sn(nids[k]), &cap_size, 256))
472 if (cap_buf && (*cap_buf != '\0'))
473 BIO_printf(bio_out, " [%s]\n", cap_buf);
475 OPENSSL_free(cap_buf);
478 BIO_printf(bio_out, "%s", indent);
479 if (ENGINE_init(e)) {
480 BIO_printf(bio_out, "[ available ]\n");
481 util_do_cmds(e, post_cmds, bio_out, indent);
484 BIO_printf(bio_out, "[ unavailable ]\n");
485 if (test_avail_noise)
486 ERR_print_errors_fp(stdout);
490 if ((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
494 ERR_print_errors(bio_err);
500 ERR_print_errors(bio_err);
501 sk_OPENSSL_STRING_pop_free(engines, identity);
502 sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
503 sk_OPENSSL_STRING_pop_free(post_cmds, identity);
505 BIO_free_all(bio_out);
512 static void *dummy = &dummy;