2 * Copyright 2000-2018 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>
11 #ifdef OPENSSL_NO_ENGINE
12 NON_EMPTY_TRANSLATION_UNIT
20 # include <openssl/err.h>
21 # include <openssl/engine.h>
22 # include <openssl/ssl.h>
23 # include <openssl/store.h>
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"},
33 {OPT_HELP_STR, 1, '-',
34 " engine... Engines to load\n"},
36 OPT_SECTION("General"),
37 {"help", OPT_HELP, '-', "Display this summary"},
38 {"t", OPT_T, '-', "Check that specified engine is available"},
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"},
42 OPT_SECTION("Output"),
43 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
44 {"vv", OPT_VV, '-', "Also display each command's description"},
45 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
46 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
47 {"c", OPT_C, '-', "List the capabilities of specified engine"},
48 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
49 {OPT_MORE_STR, OPT_EOF, 1,
50 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
54 static int append_buf(char **buf, int *size, const char *s)
56 const int expand = 256;
57 int len = strlen(s) + 1;
61 *size = ((len + expand - 1) / expand) * expand;
62 p = *buf = app_malloc(*size, "engine buffer");
64 const int blen = strlen(p);
70 *size = ((len + expand - 1) / expand) * expand;
71 p = OPENSSL_realloc(p, *size);
91 static int util_flags(BIO *out, unsigned int flags, const char *indent)
93 int started = 0, err = 0;
94 /* Indent before displaying input flags */
95 BIO_printf(out, "%s%s(input flags): ", indent, indent);
97 BIO_printf(out, "<no flags>\n");
101 * If the object is internal, mark it in a way that shows instead of
102 * having it part of all the other flags, even if it really is.
104 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
105 BIO_printf(out, "[Internal] ");
108 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
109 BIO_printf(out, "NUMERIC");
113 * Now we check that no combinations of the mutually exclusive NUMERIC,
114 * STRING, and NO_INPUT flags have been used. Future flags that can be
115 * OR'd together with these would need to added after these to preserve
118 if (flags & ENGINE_CMD_FLAG_STRING) {
120 BIO_printf(out, "|");
123 BIO_printf(out, "STRING");
126 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
128 BIO_printf(out, "|");
131 BIO_printf(out, "NO_INPUT");
134 /* Check for unknown flags */
135 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
136 ~ENGINE_CMD_FLAG_STRING &
137 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
140 BIO_printf(out, "|");
141 BIO_printf(out, "<0x%04X>", flags);
144 BIO_printf(out, " <illegal flags!>");
145 BIO_printf(out, "\n");
149 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
151 static const int line_wrap = 78;
158 STACK_OF(OPENSSL_STRING) *cmds = NULL;
159 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
160 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
161 0, NULL, NULL)) <= 0)) {
165 cmds = sk_OPENSSL_STRING_new_null();
171 /* Get the command input flags */
172 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
175 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
176 /* Get the command name */
177 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
180 name = app_malloc(len + 1, "name buffer");
181 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
184 /* Get the command description */
185 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
189 desc = app_malloc(len + 1, "description buffer");
190 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
194 /* Now decide on the output */
197 xpos = BIO_puts(out, indent);
199 /* Otherwise prepend a ", " */
200 xpos += BIO_printf(out, ", ");
203 * We're just listing names, comma-delimited
205 if ((xpos > (int)strlen(indent)) &&
206 (xpos + (int)strlen(name) > line_wrap)) {
207 BIO_printf(out, "\n");
208 xpos = BIO_puts(out, indent);
210 xpos += BIO_printf(out, "%s", name);
212 /* We're listing names plus descriptions */
213 BIO_printf(out, "%s: %s\n", name,
214 (desc == NULL) ? "<no description>" : desc);
215 /* ... and sometimes input flags */
216 if ((verbose >= 3) && !util_flags(out, flags, indent))
225 /* Move to the next command */
226 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
229 BIO_printf(out, "\n");
232 sk_OPENSSL_STRING_free(cmds);
238 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
239 BIO *out, const char *indent)
241 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
244 BIO_printf(out, "[Error]: internal stack error\n");
247 for (loop = 0; loop < num; loop++) {
249 const char *cmd, *arg;
250 cmd = sk_OPENSSL_STRING_value(cmds, loop);
251 res = 1; /* assume success */
252 /* Check if this command has no ":arg" */
253 if ((arg = strstr(cmd, ":")) == NULL) {
254 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
257 if ((int)(arg - cmd) > 254) {
258 BIO_printf(out, "[Error]: command name too long\n");
261 memcpy(buf, cmd, (int)(arg - cmd));
262 buf[arg - cmd] = '\0';
263 arg++; /* Move past the ":" */
264 /* Call the command with the argument */
265 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
269 BIO_printf(out, "[Success]: %s\n", cmd);
271 BIO_printf(out, "[Failure]: %s\n", cmd);
272 ERR_print_errors(out);
277 struct util_store_cap_data {
283 static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
285 struct util_store_cap_data *ctx = arg;
287 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
289 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
290 OSSL_STORE_LOADER_get0_scheme(loader));
291 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
296 int engine_main(int argc, char **argv)
299 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
301 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
302 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
303 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
305 const char *indent = " ";
310 out = dup_bio_out(FORMAT_TEXT);
311 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
314 /* Remember the original command name, parse/skip any leading engine
315 * names, and then setup to parse the rest of the line as flags. */
317 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
318 sk_OPENSSL_CSTRING_push(engines, argv1);
323 opt_init(argc, argv, engine_options);
325 while ((o = opt_next()) != OPT_EOF) {
329 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
332 opt_help(engine_options);
339 /* Convert to an integer from one to four. */
340 i = (int)(o - OPT_V) + 1;
354 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
357 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
362 /* Allow any trailing parameters as engine names. */
363 argc = opt_num_rest();
365 for ( ; *argv; argv++) {
367 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
369 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
372 sk_OPENSSL_CSTRING_push(engines, *argv);
375 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
376 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
377 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
382 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
383 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
384 if ((e = ENGINE_by_id(id)) != NULL) {
385 const char *name = ENGINE_get_name(e);
387 * Do "id" first, then "name". Easier to auto-parse.
389 BIO_printf(out, "(%s) %s\n", id, name);
390 util_do_cmds(e, pre_cmds, out, indent);
391 if (strcmp(ENGINE_get_id(e), id) != 0) {
392 BIO_printf(out, "Loaded: (%s) %s\n",
393 ENGINE_get_id(e), ENGINE_get_name(e));
397 char *cap_buf = NULL;
400 ENGINE_CIPHERS_PTR fn_c;
401 ENGINE_DIGESTS_PTR fn_d;
402 ENGINE_PKEY_METHS_PTR fn_pk;
404 if (ENGINE_get_RSA(e) != NULL
405 && !append_buf(&cap_buf, &cap_size, "RSA"))
407 if (ENGINE_get_DSA(e) != NULL
408 && !append_buf(&cap_buf, &cap_size, "DSA"))
410 if (ENGINE_get_DH(e) != NULL
411 && !append_buf(&cap_buf, &cap_size, "DH"))
413 if (ENGINE_get_RAND(e) != NULL
414 && !append_buf(&cap_buf, &cap_size, "RAND"))
417 fn_c = ENGINE_get_ciphers(e);
420 n = fn_c(e, NULL, &nids, 0);
421 for (k = 0; k < n; ++k)
422 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
426 fn_d = ENGINE_get_digests(e);
429 n = fn_d(e, NULL, &nids, 0);
430 for (k = 0; k < n; ++k)
431 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
435 fn_pk = ENGINE_get_pkey_meths(e);
438 n = fn_pk(e, NULL, &nids, 0);
439 for (k = 0; k < n; ++k)
440 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
444 struct util_store_cap_data store_ctx;
446 store_ctx.engine = e;
447 store_ctx.cap_buf = &cap_buf;
448 store_ctx.cap_size = &cap_size;
451 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
455 if (cap_buf != NULL && (*cap_buf != '\0'))
456 BIO_printf(out, " [%s]\n", cap_buf);
458 OPENSSL_free(cap_buf);
461 BIO_printf(out, "%s", indent);
462 if (ENGINE_init(e)) {
463 BIO_printf(out, "[ available ]\n");
464 util_do_cmds(e, post_cmds, out, indent);
467 BIO_printf(out, "[ unavailable ]\n");
468 if (test_avail_noise)
469 ERR_print_errors_fp(stdout);
473 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
477 ERR_print_errors(bio_err);
478 /* because exit codes above 127 have special meaning on Unix */
486 ERR_print_errors(bio_err);
487 sk_OPENSSL_CSTRING_free(engines);
488 sk_OPENSSL_STRING_free(pre_cmds);
489 sk_OPENSSL_STRING_free(post_cmds);