2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #include <openssl/opensslconf.h>
16 #include <openssl/err.h>
17 #include <openssl/engine.h>
18 #include <openssl/ssl.h>
19 #include <openssl/store.h>
21 typedef enum OPTION_choice {
22 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
23 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
24 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
27 const OPTIONS engine_options[] = {
28 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
29 {OPT_HELP_STR, 1, '-',
30 " engine... Engines to load\n"},
31 {"help", OPT_HELP, '-', "Display this summary"},
32 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
33 {"vv", OPT_VV, '-', "Also display each command's description"},
34 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
35 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
36 {"c", OPT_C, '-', "List the capabilities of specified engine"},
37 {"t", OPT_T, '-', "Check that specified engine is available"},
38 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
39 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
40 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
41 {OPT_MORE_STR, OPT_EOF, 1,
42 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
46 static int append_buf(char **buf, int *size, const char *s)
48 const int expand = 256;
49 int len = strlen(s) + 1;
53 *size = ((len + expand - 1) / expand) * expand;
54 p = *buf = app_malloc(*size, "engine buffer");
56 const int blen = strlen(p);
62 *size = ((len + expand - 1) / expand) * expand;
63 p = OPENSSL_realloc(p, *size);
83 static int util_flags(BIO *out, unsigned int flags, const char *indent)
85 int started = 0, err = 0;
86 /* Indent before displaying input flags */
87 BIO_printf(out, "%s%s(input flags): ", indent, indent);
89 BIO_printf(out, "<no flags>\n");
93 * If the object is internal, mark it in a way that shows instead of
94 * having it part of all the other flags, even if it really is.
96 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
97 BIO_printf(out, "[Internal] ");
100 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
101 BIO_printf(out, "NUMERIC");
105 * Now we check that no combinations of the mutually exclusive NUMERIC,
106 * STRING, and NO_INPUT flags have been used. Future flags that can be
107 * OR'd together with these would need to added after these to preserve
110 if (flags & ENGINE_CMD_FLAG_STRING) {
112 BIO_printf(out, "|");
115 BIO_printf(out, "STRING");
118 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
120 BIO_printf(out, "|");
123 BIO_printf(out, "NO_INPUT");
126 /* Check for unknown flags */
127 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
128 ~ENGINE_CMD_FLAG_STRING &
129 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
132 BIO_printf(out, "|");
133 BIO_printf(out, "<0x%04X>", flags);
136 BIO_printf(out, " <illegal flags!>");
137 BIO_printf(out, "\n");
141 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
143 static const int line_wrap = 78;
150 STACK_OF(OPENSSL_STRING) *cmds = NULL;
151 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
152 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
153 0, NULL, NULL)) <= 0)) {
157 cmds = sk_OPENSSL_STRING_new_null();
163 /* Get the command input flags */
164 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
167 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
168 /* Get the command name */
169 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
172 name = app_malloc(len + 1, "name buffer");
173 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
176 /* Get the command description */
177 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
181 desc = app_malloc(len + 1, "description buffer");
182 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
186 /* Now decide on the output */
189 xpos = BIO_puts(out, indent);
191 /* Otherwise prepend a ", " */
192 xpos += BIO_printf(out, ", ");
195 * We're just listing names, comma-delimited
197 if ((xpos > (int)strlen(indent)) &&
198 (xpos + (int)strlen(name) > line_wrap)) {
199 BIO_printf(out, "\n");
200 xpos = BIO_puts(out, indent);
202 xpos += BIO_printf(out, "%s", name);
204 /* We're listing names plus descriptions */
205 BIO_printf(out, "%s: %s\n", name,
206 (desc == NULL) ? "<no description>" : desc);
207 /* ... and sometimes input flags */
208 if ((verbose >= 3) && !util_flags(out, flags, indent))
217 /* Move to the next command */
218 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
221 BIO_printf(out, "\n");
224 sk_OPENSSL_STRING_free(cmds);
230 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
231 BIO *out, const char *indent)
233 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
236 BIO_printf(out, "[Error]: internal stack error\n");
239 for (loop = 0; loop < num; loop++) {
241 const char *cmd, *arg;
242 cmd = sk_OPENSSL_STRING_value(cmds, loop);
243 res = 1; /* assume success */
244 /* Check if this command has no ":arg" */
245 if ((arg = strstr(cmd, ":")) == NULL) {
246 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
249 if ((int)(arg - cmd) > 254) {
250 BIO_printf(out, "[Error]: command name too long\n");
253 memcpy(buf, cmd, (int)(arg - cmd));
254 buf[arg - cmd] = '\0';
255 arg++; /* Move past the ":" */
256 /* Call the command with the argument */
257 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
261 BIO_printf(out, "[Success]: %s\n", cmd);
263 BIO_printf(out, "[Failure]: %s\n", cmd);
264 ERR_print_errors(out);
269 struct util_store_cap_data {
275 static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
277 struct util_store_cap_data *ctx = arg;
279 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
281 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
282 OSSL_STORE_LOADER_get0_scheme(loader));
283 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
288 int engine_main(int argc, char **argv)
291 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
293 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
294 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
295 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
297 const char *indent = " ";
302 out = dup_bio_out(FORMAT_TEXT);
303 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
306 /* Remember the original command name, parse/skip any leading engine
307 * names, and then setup to parse the rest of the line as flags. */
309 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
310 sk_OPENSSL_CSTRING_push(engines, argv1);
315 opt_init(argc, argv, engine_options);
317 while ((o = opt_next()) != OPT_EOF) {
321 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
324 opt_help(engine_options);
331 /* Convert to an integer from one to four. */
332 i = (int)(o - OPT_V) + 1;
346 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
349 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
354 /* Allow any trailing parameters as engine names. */
355 argc = opt_num_rest();
357 for ( ; *argv; argv++) {
359 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
361 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
364 sk_OPENSSL_CSTRING_push(engines, *argv);
367 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
368 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
369 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
374 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
375 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
376 if ((e = ENGINE_by_id(id)) != NULL) {
377 const char *name = ENGINE_get_name(e);
379 * Do "id" first, then "name". Easier to auto-parse.
381 BIO_printf(out, "(%s) %s\n", id, name);
382 util_do_cmds(e, pre_cmds, out, indent);
383 if (strcmp(ENGINE_get_id(e), id) != 0) {
384 BIO_printf(out, "Loaded: (%s) %s\n",
385 ENGINE_get_id(e), ENGINE_get_name(e));
389 char *cap_buf = NULL;
392 ENGINE_CIPHERS_PTR fn_c;
393 ENGINE_DIGESTS_PTR fn_d;
394 ENGINE_PKEY_METHS_PTR fn_pk;
396 if (ENGINE_get_RSA(e) != NULL
397 && !append_buf(&cap_buf, &cap_size, "RSA"))
399 if (ENGINE_get_DSA(e) != NULL
400 && !append_buf(&cap_buf, &cap_size, "DSA"))
402 if (ENGINE_get_DH(e) != NULL
403 && !append_buf(&cap_buf, &cap_size, "DH"))
405 if (ENGINE_get_RAND(e) != NULL
406 && !append_buf(&cap_buf, &cap_size, "RAND"))
409 fn_c = ENGINE_get_ciphers(e);
412 n = fn_c(e, NULL, &nids, 0);
413 for (k = 0; k < n; ++k)
414 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
418 fn_d = ENGINE_get_digests(e);
421 n = fn_d(e, NULL, &nids, 0);
422 for (k = 0; k < n; ++k)
423 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
427 fn_pk = ENGINE_get_pkey_meths(e);
430 n = fn_pk(e, NULL, &nids, 0);
431 for (k = 0; k < n; ++k)
432 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
436 struct util_store_cap_data store_ctx;
438 store_ctx.engine = e;
439 store_ctx.cap_buf = &cap_buf;
440 store_ctx.cap_size = &cap_size;
443 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
447 if (cap_buf != NULL && (*cap_buf != '\0'))
448 BIO_printf(out, " [%s]\n", cap_buf);
450 OPENSSL_free(cap_buf);
453 BIO_printf(out, "%s", indent);
454 if (ENGINE_init(e)) {
455 BIO_printf(out, "[ available ]\n");
456 util_do_cmds(e, post_cmds, out, indent);
459 BIO_printf(out, "[ unavailable ]\n");
460 if (test_avail_noise)
461 ERR_print_errors_fp(stdout);
465 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
469 ERR_print_errors(bio_err);
470 /* because exit codes above 127 have special meaning on Unix */
478 ERR_print_errors(bio_err);
479 sk_OPENSSL_CSTRING_free(engines);
480 sk_OPENSSL_STRING_free(pre_cmds);
481 sk_OPENSSL_STRING_free(post_cmds);