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>
17 #include <openssl/err.h>
18 #include <openssl/engine.h>
19 #include <openssl/ssl.h>
20 #include <openssl/store.h>
22 typedef enum OPTION_choice {
23 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
25 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
28 const OPTIONS engine_options[] = {
29 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
31 OPT_SECTION("General"),
32 {"help", OPT_HELP, '-', "Display this summary"},
33 {"t", OPT_T, '-', "Check that specified engine is available"},
34 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
35 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
37 OPT_SECTION("Output"),
38 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
39 {"vv", OPT_VV, '-', "Also display each command's description"},
40 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
41 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
42 {"c", OPT_C, '-', "List the capabilities of specified engine"},
43 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
44 {OPT_MORE_STR, OPT_EOF, 1,
45 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
48 {"engine", 0, 0, "ID of engine(s) to load"},
52 static int append_buf(char **buf, int *size, const char *s)
54 const int expand = 256;
55 int len = strlen(s) + 1;
59 *size = ((len + expand - 1) / expand) * expand;
60 p = *buf = app_malloc(*size, "engine buffer");
62 const int blen = strlen(p);
68 *size = ((len + expand - 1) / expand) * expand;
69 p = OPENSSL_realloc(p, *size);
89 static int util_flags(BIO *out, unsigned int flags, const char *indent)
91 int started = 0, err = 0;
92 /* Indent before displaying input flags */
93 BIO_printf(out, "%s%s(input flags): ", indent, indent);
95 BIO_printf(out, "<no flags>\n");
99 * If the object is internal, mark it in a way that shows instead of
100 * having it part of all the other flags, even if it really is.
102 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
103 BIO_printf(out, "[Internal] ");
106 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
107 BIO_printf(out, "NUMERIC");
111 * Now we check that no combinations of the mutually exclusive NUMERIC,
112 * STRING, and NO_INPUT flags have been used. Future flags that can be
113 * OR'd together with these would need to added after these to preserve
116 if (flags & ENGINE_CMD_FLAG_STRING) {
118 BIO_printf(out, "|");
121 BIO_printf(out, "STRING");
124 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
126 BIO_printf(out, "|");
129 BIO_printf(out, "NO_INPUT");
132 /* Check for unknown flags */
133 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
134 ~ENGINE_CMD_FLAG_STRING &
135 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
138 BIO_printf(out, "|");
139 BIO_printf(out, "<0x%04X>", flags);
142 BIO_printf(out, " <illegal flags!>");
143 BIO_printf(out, "\n");
147 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
149 static const int line_wrap = 78;
156 STACK_OF(OPENSSL_STRING) *cmds = NULL;
157 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
158 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
159 0, NULL, NULL)) <= 0)) {
163 cmds = sk_OPENSSL_STRING_new_null();
169 /* Get the command input flags */
170 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
173 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
174 /* Get the command name */
175 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
178 name = app_malloc(len + 1, "name buffer");
179 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
182 /* Get the command description */
183 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
187 desc = app_malloc(len + 1, "description buffer");
188 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
192 /* Now decide on the output */
195 xpos = BIO_puts(out, indent);
197 /* Otherwise prepend a ", " */
198 xpos += BIO_printf(out, ", ");
201 * We're just listing names, comma-delimited
203 if ((xpos > (int)strlen(indent)) &&
204 (xpos + (int)strlen(name) > line_wrap)) {
205 BIO_printf(out, "\n");
206 xpos = BIO_puts(out, indent);
208 xpos += BIO_printf(out, "%s", name);
210 /* We're listing names plus descriptions */
211 BIO_printf(out, "%s: %s\n", name,
212 (desc == NULL) ? "<no description>" : desc);
213 /* ... and sometimes input flags */
214 if ((verbose >= 3) && !util_flags(out, flags, indent))
223 /* Move to the next command */
224 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
227 BIO_printf(out, "\n");
230 sk_OPENSSL_STRING_free(cmds);
236 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
237 BIO *out, const char *indent)
239 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
242 BIO_printf(out, "[Error]: internal stack error\n");
245 for (loop = 0; loop < num; loop++) {
247 const char *cmd, *arg;
248 cmd = sk_OPENSSL_STRING_value(cmds, loop);
249 res = 1; /* assume success */
250 /* Check if this command has no ":arg" */
251 if ((arg = strstr(cmd, ":")) == NULL) {
252 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
255 if ((int)(arg - cmd) > 254) {
256 BIO_printf(out, "[Error]: command name too long\n");
259 memcpy(buf, cmd, (int)(arg - cmd));
260 buf[arg - cmd] = '\0';
261 arg++; /* Move past the ":" */
262 /* Call the command with the argument */
263 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
267 BIO_printf(out, "[Success]: %s\n", cmd);
269 BIO_printf(out, "[Failure]: %s\n", cmd);
270 ERR_print_errors(out);
275 struct util_store_cap_data {
281 static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
283 struct util_store_cap_data *ctx = arg;
285 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
287 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
288 OSSL_STORE_LOADER_get0_scheme(loader));
289 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
294 int engine_main(int argc, char **argv)
297 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
299 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
300 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
301 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
303 const char *indent = " ";
308 out = dup_bio_out(FORMAT_TEXT);
309 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
312 /* Remember the original command name, parse/skip any leading engine
313 * names, and then setup to parse the rest of the line as flags. */
315 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
316 sk_OPENSSL_CSTRING_push(engines, argv1);
321 opt_init(argc, argv, engine_options);
323 while ((o = opt_next()) != OPT_EOF) {
327 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
330 opt_help(engine_options);
337 /* Convert to an integer from one to four. */
338 i = (int)(o - OPT_V) + 1;
352 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
355 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
360 /* Allow any trailing parameters as engine names. */
361 argc = opt_num_rest();
363 for ( ; *argv; argv++) {
365 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
367 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
370 sk_OPENSSL_CSTRING_push(engines, *argv);
373 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
374 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
375 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
380 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
381 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
382 if ((e = ENGINE_by_id(id)) != NULL) {
383 const char *name = ENGINE_get_name(e);
385 * Do "id" first, then "name". Easier to auto-parse.
387 BIO_printf(out, "(%s) %s\n", id, name);
388 util_do_cmds(e, pre_cmds, out, indent);
389 if (strcmp(ENGINE_get_id(e), id) != 0) {
390 BIO_printf(out, "Loaded: (%s) %s\n",
391 ENGINE_get_id(e), ENGINE_get_name(e));
395 char *cap_buf = NULL;
398 ENGINE_CIPHERS_PTR fn_c;
399 ENGINE_DIGESTS_PTR fn_d;
400 ENGINE_PKEY_METHS_PTR fn_pk;
402 if (ENGINE_get_RSA(e) != NULL
403 && !append_buf(&cap_buf, &cap_size, "RSA"))
405 if (ENGINE_get_DSA(e) != NULL
406 && !append_buf(&cap_buf, &cap_size, "DSA"))
408 if (ENGINE_get_DH(e) != NULL
409 && !append_buf(&cap_buf, &cap_size, "DH"))
411 if (ENGINE_get_RAND(e) != NULL
412 && !append_buf(&cap_buf, &cap_size, "RAND"))
415 fn_c = ENGINE_get_ciphers(e);
418 n = fn_c(e, NULL, &nids, 0);
419 for (k = 0; k < n; ++k)
420 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
424 fn_d = ENGINE_get_digests(e);
427 n = fn_d(e, NULL, &nids, 0);
428 for (k = 0; k < n; ++k)
429 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
433 fn_pk = ENGINE_get_pkey_meths(e);
436 n = fn_pk(e, NULL, &nids, 0);
437 for (k = 0; k < n; ++k)
438 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
442 struct util_store_cap_data store_ctx;
444 store_ctx.engine = e;
445 store_ctx.cap_buf = &cap_buf;
446 store_ctx.cap_size = &cap_size;
449 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
453 if (cap_buf != NULL && (*cap_buf != '\0'))
454 BIO_printf(out, " [%s]\n", cap_buf);
456 OPENSSL_free(cap_buf);
459 BIO_printf(out, "%s", indent);
460 if (ENGINE_init(e)) {
461 BIO_printf(out, "[ available ]\n");
462 util_do_cmds(e, post_cmds, out, indent);
465 BIO_printf(out, "[ unavailable ]\n");
466 if (test_avail_noise)
467 ERR_print_errors_fp(stdout);
471 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
475 ERR_print_errors(bio_err);
476 /* because exit codes above 127 have special meaning on Unix */
484 ERR_print_errors(bio_err);
485 sk_OPENSSL_CSTRING_free(engines);
486 sk_OPENSSL_STRING_free(pre_cmds);
487 sk_OPENSSL_STRING_free(post_cmds);