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 #include <openssl/opensslconf.h>
17 #include <openssl/err.h>
18 #include <openssl/engine.h>
19 #include <openssl/ssl.h>
20 #include <openssl/store.h>
22 DEFINE_STACK_OF_STRING()
23 DEFINE_STACK_OF_CSTRING()
25 typedef enum OPTION_choice {
26 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
27 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
28 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
31 const OPTIONS engine_options[] = {
32 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
34 OPT_SECTION("General"),
35 {"help", OPT_HELP, '-', "Display this summary"},
36 {"t", OPT_T, '-', "Check that specified engine is available"},
37 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
38 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
40 OPT_SECTION("Output"),
41 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
42 {"vv", OPT_VV, '-', "Also display each command's description"},
43 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
44 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
45 {"c", OPT_C, '-', "List the capabilities of specified engine"},
46 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
47 {OPT_MORE_STR, OPT_EOF, 1,
48 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
51 {"engine", 0, 0, "ID of engine(s) to load"},
55 static int append_buf(char **buf, int *size, const char *s)
57 const int expand = 256;
58 int len = strlen(s) + 1;
62 *size = ((len + expand - 1) / expand) * expand;
63 p = *buf = app_malloc(*size, "engine buffer");
65 const int blen = strlen(p);
71 *size = ((len + expand - 1) / expand) * expand;
72 p = OPENSSL_realloc(p, *size);
92 static int util_flags(BIO *out, unsigned int flags, const char *indent)
94 int started = 0, err = 0;
95 /* Indent before displaying input flags */
96 BIO_printf(out, "%s%s(input flags): ", indent, indent);
98 BIO_printf(out, "<no flags>\n");
102 * If the object is internal, mark it in a way that shows instead of
103 * having it part of all the other flags, even if it really is.
105 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
106 BIO_printf(out, "[Internal] ");
109 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
110 BIO_printf(out, "NUMERIC");
114 * Now we check that no combinations of the mutually exclusive NUMERIC,
115 * STRING, and NO_INPUT flags have been used. Future flags that can be
116 * OR'd together with these would need to added after these to preserve
119 if (flags & ENGINE_CMD_FLAG_STRING) {
121 BIO_printf(out, "|");
124 BIO_printf(out, "STRING");
127 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
129 BIO_printf(out, "|");
132 BIO_printf(out, "NO_INPUT");
135 /* Check for unknown flags */
136 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
137 ~ENGINE_CMD_FLAG_STRING &
138 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
141 BIO_printf(out, "|");
142 BIO_printf(out, "<0x%04X>", flags);
145 BIO_printf(out, " <illegal flags!>");
146 BIO_printf(out, "\n");
150 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
152 static const int line_wrap = 78;
159 STACK_OF(OPENSSL_STRING) *cmds = NULL;
160 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
161 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
162 0, NULL, NULL)) <= 0)) {
166 cmds = sk_OPENSSL_STRING_new_null();
172 /* Get the command input flags */
173 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
176 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
177 /* Get the command name */
178 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
181 name = app_malloc(len + 1, "name buffer");
182 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
185 /* Get the command description */
186 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
190 desc = app_malloc(len + 1, "description buffer");
191 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
195 /* Now decide on the output */
198 xpos = BIO_puts(out, indent);
200 /* Otherwise prepend a ", " */
201 xpos += BIO_printf(out, ", ");
204 * We're just listing names, comma-delimited
206 if ((xpos > (int)strlen(indent)) &&
207 (xpos + (int)strlen(name) > line_wrap)) {
208 BIO_printf(out, "\n");
209 xpos = BIO_puts(out, indent);
211 xpos += BIO_printf(out, "%s", name);
213 /* We're listing names plus descriptions */
214 BIO_printf(out, "%s: %s\n", name,
215 (desc == NULL) ? "<no description>" : desc);
216 /* ... and sometimes input flags */
217 if ((verbose >= 3) && !util_flags(out, flags, indent))
226 /* Move to the next command */
227 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
230 BIO_printf(out, "\n");
233 sk_OPENSSL_STRING_free(cmds);
239 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
240 BIO *out, const char *indent)
242 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
245 BIO_printf(out, "[Error]: internal stack error\n");
248 for (loop = 0; loop < num; loop++) {
250 const char *cmd, *arg;
251 cmd = sk_OPENSSL_STRING_value(cmds, loop);
252 res = 1; /* assume success */
253 /* Check if this command has no ":arg" */
254 if ((arg = strstr(cmd, ":")) == NULL) {
255 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
258 if ((int)(arg - cmd) > 254) {
259 BIO_printf(out, "[Error]: command name too long\n");
262 memcpy(buf, cmd, (int)(arg - cmd));
263 buf[arg - cmd] = '\0';
264 arg++; /* Move past the ":" */
265 /* Call the command with the argument */
266 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
270 BIO_printf(out, "[Success]: %s\n", cmd);
272 BIO_printf(out, "[Failure]: %s\n", cmd);
273 ERR_print_errors(out);
278 struct util_store_cap_data {
284 static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
286 struct util_store_cap_data *ctx = arg;
288 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
290 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
291 OSSL_STORE_LOADER_get0_scheme(loader));
292 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
297 int engine_main(int argc, char **argv)
300 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
302 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
303 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
304 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
306 const char *indent = " ";
311 out = dup_bio_out(FORMAT_TEXT);
312 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
315 /* Remember the original command name, parse/skip any leading engine
316 * names, and then setup to parse the rest of the line as flags. */
318 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
319 sk_OPENSSL_CSTRING_push(engines, argv1);
324 opt_init(argc, argv, engine_options);
326 while ((o = opt_next()) != OPT_EOF) {
330 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
333 opt_help(engine_options);
340 /* Convert to an integer from one to four. */
341 i = (int)(o - OPT_V) + 1;
355 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
358 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
363 /* Allow any trailing parameters as engine names. */
364 argc = opt_num_rest();
366 for ( ; *argv; argv++) {
368 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
370 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
373 sk_OPENSSL_CSTRING_push(engines, *argv);
376 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
377 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
378 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
383 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
384 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
385 if ((e = ENGINE_by_id(id)) != NULL) {
386 const char *name = ENGINE_get_name(e);
388 * Do "id" first, then "name". Easier to auto-parse.
390 BIO_printf(out, "(%s) %s\n", id, name);
391 util_do_cmds(e, pre_cmds, out, indent);
392 if (strcmp(ENGINE_get_id(e), id) != 0) {
393 BIO_printf(out, "Loaded: (%s) %s\n",
394 ENGINE_get_id(e), ENGINE_get_name(e));
398 char *cap_buf = NULL;
401 ENGINE_CIPHERS_PTR fn_c;
402 ENGINE_DIGESTS_PTR fn_d;
403 ENGINE_PKEY_METHS_PTR fn_pk;
405 if (ENGINE_get_RSA(e) != NULL
406 && !append_buf(&cap_buf, &cap_size, "RSA"))
408 if (ENGINE_get_DSA(e) != NULL
409 && !append_buf(&cap_buf, &cap_size, "DSA"))
411 if (ENGINE_get_DH(e) != NULL
412 && !append_buf(&cap_buf, &cap_size, "DH"))
414 if (ENGINE_get_RAND(e) != NULL
415 && !append_buf(&cap_buf, &cap_size, "RAND"))
418 fn_c = ENGINE_get_ciphers(e);
421 n = fn_c(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_d = ENGINE_get_digests(e);
430 n = fn_d(e, NULL, &nids, 0);
431 for (k = 0; k < n; ++k)
432 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
436 fn_pk = ENGINE_get_pkey_meths(e);
439 n = fn_pk(e, NULL, &nids, 0);
440 for (k = 0; k < n; ++k)
441 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
445 struct util_store_cap_data store_ctx;
447 store_ctx.engine = e;
448 store_ctx.cap_buf = &cap_buf;
449 store_ctx.cap_size = &cap_size;
452 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
456 if (cap_buf != NULL && (*cap_buf != '\0'))
457 BIO_printf(out, " [%s]\n", cap_buf);
459 OPENSSL_free(cap_buf);
462 BIO_printf(out, "%s", indent);
463 if (ENGINE_init(e)) {
464 BIO_printf(out, "[ available ]\n");
465 util_do_cmds(e, post_cmds, out, indent);
468 BIO_printf(out, "[ unavailable ]\n");
469 if (test_avail_noise)
470 ERR_print_errors_fp(stdout);
474 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
478 ERR_print_errors(bio_err);
479 /* because exit codes above 127 have special meaning on Unix */
487 ERR_print_errors(bio_err);
488 sk_OPENSSL_CSTRING_free(engines);
489 sk_OPENSSL_STRING_free(pre_cmds);
490 sk_OPENSSL_STRING_free(post_cmds);