2 * Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 /* We need to use some engine deprecated APIs */
11 #define OPENSSL_SUPPRESS_DEPRECATED
13 #include <openssl/opensslconf.h>
20 #include <openssl/err.h>
21 #include <openssl/engine.h>
22 #include <openssl/ssl.h>
23 #include <openssl/store.h>
25 DEFINE_STACK_OF_STRING()
26 DEFINE_STACK_OF_CSTRING()
28 typedef enum OPTION_choice {
29 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
30 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
31 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
34 const OPTIONS engine_options[] = {
35 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
37 OPT_SECTION("General"),
38 {"help", OPT_HELP, '-', "Display this summary"},
39 {"t", OPT_T, '-', "Check that specified engine is available"},
40 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
41 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
43 OPT_SECTION("Output"),
44 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
45 {"vv", OPT_VV, '-', "Also display each command's description"},
46 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
47 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
48 {"c", OPT_C, '-', "List the capabilities of specified engine"},
49 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
50 {OPT_MORE_STR, OPT_EOF, 1,
51 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
54 {"engine", 0, 0, "ID of engine(s) to load"},
58 static int append_buf(char **buf, int *size, const char *s)
60 const int expand = 256;
61 int len = strlen(s) + 1;
65 *size = ((len + expand - 1) / expand) * expand;
66 p = *buf = app_malloc(*size, "engine buffer");
68 const int blen = strlen(p);
74 *size = ((len + expand - 1) / expand) * expand;
75 p = OPENSSL_realloc(p, *size);
95 static int util_flags(BIO *out, unsigned int flags, const char *indent)
97 int started = 0, err = 0;
98 /* Indent before displaying input flags */
99 BIO_printf(out, "%s%s(input flags): ", indent, indent);
101 BIO_printf(out, "<no flags>\n");
105 * If the object is internal, mark it in a way that shows instead of
106 * having it part of all the other flags, even if it really is.
108 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
109 BIO_printf(out, "[Internal] ");
112 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
113 BIO_printf(out, "NUMERIC");
117 * Now we check that no combinations of the mutually exclusive NUMERIC,
118 * STRING, and NO_INPUT flags have been used. Future flags that can be
119 * OR'd together with these would need to added after these to preserve
122 if (flags & ENGINE_CMD_FLAG_STRING) {
124 BIO_printf(out, "|");
127 BIO_printf(out, "STRING");
130 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
132 BIO_printf(out, "|");
135 BIO_printf(out, "NO_INPUT");
138 /* Check for unknown flags */
139 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
140 ~ENGINE_CMD_FLAG_STRING &
141 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
144 BIO_printf(out, "|");
145 BIO_printf(out, "<0x%04X>", flags);
148 BIO_printf(out, " <illegal flags!>");
149 BIO_printf(out, "\n");
153 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
155 static const int line_wrap = 78;
162 STACK_OF(OPENSSL_STRING) *cmds = NULL;
163 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
164 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
165 0, NULL, NULL)) <= 0)) {
169 cmds = sk_OPENSSL_STRING_new_null();
175 /* Get the command input flags */
176 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
179 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
180 /* Get the command name */
181 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
184 name = app_malloc(len + 1, "name buffer");
185 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
188 /* Get the command description */
189 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
193 desc = app_malloc(len + 1, "description buffer");
194 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
198 /* Now decide on the output */
201 xpos = BIO_puts(out, indent);
203 /* Otherwise prepend a ", " */
204 xpos += BIO_printf(out, ", ");
207 * We're just listing names, comma-delimited
209 if ((xpos > (int)strlen(indent)) &&
210 (xpos + (int)strlen(name) > line_wrap)) {
211 BIO_printf(out, "\n");
212 xpos = BIO_puts(out, indent);
214 xpos += BIO_printf(out, "%s", name);
216 /* We're listing names plus descriptions */
217 BIO_printf(out, "%s: %s\n", name,
218 (desc == NULL) ? "<no description>" : desc);
219 /* ... and sometimes input flags */
220 if ((verbose >= 3) && !util_flags(out, flags, indent))
229 /* Move to the next command */
230 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
233 BIO_printf(out, "\n");
236 sk_OPENSSL_STRING_free(cmds);
242 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
243 BIO *out, const char *indent)
245 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
248 BIO_printf(out, "[Error]: internal stack error\n");
251 for (loop = 0; loop < num; loop++) {
253 const char *cmd, *arg;
254 cmd = sk_OPENSSL_STRING_value(cmds, loop);
255 res = 1; /* assume success */
256 /* Check if this command has no ":arg" */
257 if ((arg = strstr(cmd, ":")) == NULL) {
258 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
261 if ((int)(arg - cmd) > 254) {
262 BIO_printf(out, "[Error]: command name too long\n");
265 memcpy(buf, cmd, (int)(arg - cmd));
266 buf[arg - cmd] = '\0';
267 arg++; /* Move past the ":" */
268 /* Call the command with the argument */
269 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
273 BIO_printf(out, "[Success]: %s\n", cmd);
275 BIO_printf(out, "[Failure]: %s\n", cmd);
276 ERR_print_errors(out);
281 struct util_store_cap_data {
287 static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
289 struct util_store_cap_data *ctx = arg;
291 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
293 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
294 OSSL_STORE_LOADER_get0_scheme(loader));
295 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
300 int engine_main(int argc, char **argv)
303 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
305 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
306 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
307 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
309 const char *indent = " ";
314 out = dup_bio_out(FORMAT_TEXT);
315 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
318 /* Remember the original command name, parse/skip any leading engine
319 * names, and then setup to parse the rest of the line as flags. */
321 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
322 sk_OPENSSL_CSTRING_push(engines, argv1);
327 opt_init(argc, argv, engine_options);
329 while ((o = opt_next()) != OPT_EOF) {
333 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
336 opt_help(engine_options);
343 /* Convert to an integer from one to four. */
344 i = (int)(o - OPT_V) + 1;
358 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
361 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
366 /* Allow any trailing parameters as engine names. */
367 argc = opt_num_rest();
369 for ( ; *argv; argv++) {
371 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
373 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
376 sk_OPENSSL_CSTRING_push(engines, *argv);
379 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
380 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
381 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
386 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
387 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
388 if ((e = ENGINE_by_id(id)) != NULL) {
389 const char *name = ENGINE_get_name(e);
391 * Do "id" first, then "name". Easier to auto-parse.
393 BIO_printf(out, "(%s) %s\n", id, name);
394 util_do_cmds(e, pre_cmds, out, indent);
395 if (strcmp(ENGINE_get_id(e), id) != 0) {
396 BIO_printf(out, "Loaded: (%s) %s\n",
397 ENGINE_get_id(e), ENGINE_get_name(e));
401 char *cap_buf = NULL;
404 ENGINE_CIPHERS_PTR fn_c;
405 ENGINE_DIGESTS_PTR fn_d;
406 ENGINE_PKEY_METHS_PTR fn_pk;
408 if (ENGINE_get_RSA(e) != NULL
409 && !append_buf(&cap_buf, &cap_size, "RSA"))
411 if (ENGINE_get_DSA(e) != NULL
412 && !append_buf(&cap_buf, &cap_size, "DSA"))
414 if (ENGINE_get_DH(e) != NULL
415 && !append_buf(&cap_buf, &cap_size, "DH"))
417 if (ENGINE_get_RAND(e) != NULL
418 && !append_buf(&cap_buf, &cap_size, "RAND"))
421 fn_c = ENGINE_get_ciphers(e);
424 n = fn_c(e, NULL, &nids, 0);
425 for (k = 0; k < n; ++k)
426 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
430 fn_d = ENGINE_get_digests(e);
433 n = fn_d(e, NULL, &nids, 0);
434 for (k = 0; k < n; ++k)
435 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
439 fn_pk = ENGINE_get_pkey_meths(e);
442 n = fn_pk(e, NULL, &nids, 0);
443 for (k = 0; k < n; ++k)
444 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
448 struct util_store_cap_data store_ctx;
450 store_ctx.engine = e;
451 store_ctx.cap_buf = &cap_buf;
452 store_ctx.cap_size = &cap_size;
455 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
459 if (cap_buf != NULL && (*cap_buf != '\0'))
460 BIO_printf(out, " [%s]\n", cap_buf);
462 OPENSSL_free(cap_buf);
465 BIO_printf(out, "%s", indent);
466 if (ENGINE_init(e)) {
467 BIO_printf(out, "[ available ]\n");
468 util_do_cmds(e, post_cmds, out, indent);
471 BIO_printf(out, "[ unavailable ]\n");
472 if (test_avail_noise)
473 ERR_print_errors_fp(stdout);
477 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
481 ERR_print_errors(bio_err);
482 /* because exit codes above 127 have special meaning on Unix */
490 ERR_print_errors(bio_err);
491 sk_OPENSSL_CSTRING_free(engines);
492 sk_OPENSSL_STRING_free(pre_cmds);
493 sk_OPENSSL_STRING_free(post_cmds);