2 * Copyright 2000-2017 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>
11 #ifdef OPENSSL_NO_ENGINE
12 NON_EMPTY_TRANSLATION_UNIT
19 # include <openssl/err.h>
20 # include <openssl/engine.h>
21 # include <openssl/ssl.h>
22 # include <openssl/store.h>
24 typedef enum OPTION_choice {
25 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
26 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
27 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
30 const OPTIONS engine_options[] = {
31 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
32 {OPT_HELP_STR, 1, '-',
33 " engine... Engines to load\n"},
34 {"help", OPT_HELP, '-', "Display this summary"},
35 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
36 {"vv", OPT_VV, '-', "Also display each command's description"},
37 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
38 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
39 {"c", OPT_C, '-', "List the capabilities of specified engine"},
40 {"t", OPT_T, '-', "Check that specified engine is available"},
41 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
42 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
43 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
44 {OPT_MORE_STR, OPT_EOF, 1,
45 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
49 static int append_buf(char **buf, int *size, const char *s)
53 *buf = app_malloc(*size, "engine buffer");
57 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
60 tmp = OPENSSL_realloc(*buf, *size);
76 static int util_flags(BIO *out, unsigned int flags, const char *indent)
78 int started = 0, err = 0;
79 /* Indent before displaying input flags */
80 BIO_printf(out, "%s%s(input flags): ", indent, indent);
82 BIO_printf(out, "<no flags>\n");
86 * If the object is internal, mark it in a way that shows instead of
87 * having it part of all the other flags, even if it really is.
89 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
90 BIO_printf(out, "[Internal] ");
93 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
94 BIO_printf(out, "NUMERIC");
98 * Now we check that no combinations of the mutually exclusive NUMERIC,
99 * STRING, and NO_INPUT flags have been used. Future flags that can be
100 * OR'd together with these would need to added after these to preserve
103 if (flags & ENGINE_CMD_FLAG_STRING) {
105 BIO_printf(out, "|");
108 BIO_printf(out, "STRING");
111 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
113 BIO_printf(out, "|");
116 BIO_printf(out, "NO_INPUT");
119 /* Check for unknown flags */
120 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
121 ~ENGINE_CMD_FLAG_STRING &
122 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
125 BIO_printf(out, "|");
126 BIO_printf(out, "<0x%04X>", flags);
129 BIO_printf(out, " <illegal flags!>");
130 BIO_printf(out, "\n");
134 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
136 static const int line_wrap = 78;
143 STACK_OF(OPENSSL_STRING) *cmds = NULL;
144 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
145 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
146 0, NULL, NULL)) <= 0)) {
150 cmds = sk_OPENSSL_STRING_new_null();
156 /* Get the command input flags */
157 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
160 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
161 /* Get the command name */
162 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
165 name = app_malloc(len + 1, "name buffer");
166 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
169 /* Get the command description */
170 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
174 desc = app_malloc(len + 1, "description buffer");
175 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
179 /* Now decide on the output */
182 xpos = BIO_puts(out, indent);
184 /* Otherwise prepend a ", " */
185 xpos += BIO_printf(out, ", ");
188 * We're just listing names, comma-delimited
190 if ((xpos > (int)strlen(indent)) &&
191 (xpos + (int)strlen(name) > line_wrap)) {
192 BIO_printf(out, "\n");
193 xpos = BIO_puts(out, indent);
195 xpos += BIO_printf(out, "%s", name);
197 /* We're listing names plus descriptions */
198 BIO_printf(out, "%s: %s\n", name,
199 (desc == NULL) ? "<no description>" : desc);
200 /* ... and sometimes input flags */
201 if ((verbose >= 3) && !util_flags(out, flags, indent))
210 /* Move to the next command */
211 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
214 BIO_printf(out, "\n");
217 sk_OPENSSL_STRING_free(cmds);
223 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
224 BIO *out, const char *indent)
226 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
229 BIO_printf(out, "[Error]: internal stack error\n");
232 for (loop = 0; loop < num; loop++) {
234 const char *cmd, *arg;
235 cmd = sk_OPENSSL_STRING_value(cmds, loop);
236 res = 1; /* assume success */
237 /* Check if this command has no ":arg" */
238 if ((arg = strstr(cmd, ":")) == NULL) {
239 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
242 if ((int)(arg - cmd) > 254) {
243 BIO_printf(out, "[Error]: command name too long\n");
246 memcpy(buf, cmd, (int)(arg - cmd));
247 buf[arg - cmd] = '\0';
248 arg++; /* Move past the ":" */
249 /* Call the command with the argument */
250 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
254 BIO_printf(out, "[Success]: %s\n", cmd);
256 BIO_printf(out, "[Failure]: %s\n", cmd);
257 ERR_print_errors(out);
262 struct util_store_cap_data {
268 static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
270 struct util_store_cap_data *ctx = arg;
272 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
274 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
275 OSSL_STORE_LOADER_get0_scheme(loader));
276 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
281 int engine_main(int argc, char **argv)
284 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
286 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
287 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
288 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
290 const char *indent = " ";
295 out = dup_bio_out(FORMAT_TEXT);
296 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
299 /* Remember the original command name, parse/skip any leading engine
300 * names, and then setup to parse the rest of the line as flags. */
302 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
303 sk_OPENSSL_CSTRING_push(engines, argv1);
308 opt_init(argc, argv, engine_options);
310 while ((o = opt_next()) != OPT_EOF) {
314 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
317 opt_help(engine_options);
324 /* Convert to an integer from one to four. */
325 i = (int)(o - OPT_V) + 1;
339 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
342 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
347 /* Allow any trailing parameters as engine names. */
348 argc = opt_num_rest();
350 for ( ; *argv; argv++) {
352 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
354 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
357 sk_OPENSSL_CSTRING_push(engines, *argv);
360 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
361 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
362 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
367 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
368 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
369 if ((e = ENGINE_by_id(id)) != NULL) {
370 const char *name = ENGINE_get_name(e);
372 * Do "id" first, then "name". Easier to auto-parse.
374 BIO_printf(out, "(%s) %s\n", id, name);
375 util_do_cmds(e, pre_cmds, out, indent);
376 if (strcmp(ENGINE_get_id(e), id) != 0) {
377 BIO_printf(out, "Loaded: (%s) %s\n",
378 ENGINE_get_id(e), ENGINE_get_name(e));
382 char *cap_buf = NULL;
385 ENGINE_CIPHERS_PTR fn_c;
386 ENGINE_DIGESTS_PTR fn_d;
387 ENGINE_PKEY_METHS_PTR fn_pk;
389 if (ENGINE_get_RSA(e) != NULL
390 && !append_buf(&cap_buf, &cap_size, "RSA"))
392 if (ENGINE_get_DSA(e) != NULL
393 && !append_buf(&cap_buf, &cap_size, "DSA"))
395 if (ENGINE_get_DH(e) != NULL
396 && !append_buf(&cap_buf, &cap_size, "DH"))
398 if (ENGINE_get_RAND(e) != NULL
399 && !append_buf(&cap_buf, &cap_size, "RAND"))
402 fn_c = ENGINE_get_ciphers(e);
405 n = fn_c(e, NULL, &nids, 0);
406 for (k = 0; k < n; ++k)
407 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
411 fn_d = ENGINE_get_digests(e);
414 n = fn_d(e, NULL, &nids, 0);
415 for (k = 0; k < n; ++k)
416 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
420 fn_pk = ENGINE_get_pkey_meths(e);
423 n = fn_pk(e, NULL, &nids, 0);
424 for (k = 0; k < n; ++k)
425 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
429 struct util_store_cap_data store_ctx;
431 store_ctx.engine = e;
432 store_ctx.cap_buf = &cap_buf;
433 store_ctx.cap_size = &cap_size;
436 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
440 if (cap_buf != NULL && (*cap_buf != '\0'))
441 BIO_printf(out, " [%s]\n", cap_buf);
443 OPENSSL_free(cap_buf);
446 BIO_printf(out, "%s", indent);
447 if (ENGINE_init(e)) {
448 BIO_printf(out, "[ available ]\n");
449 util_do_cmds(e, post_cmds, out, indent);
452 BIO_printf(out, "[ unavailable ]\n");
453 if (test_avail_noise)
454 ERR_print_errors_fp(stdout);
458 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
462 ERR_print_errors(bio_err);
463 /* because exit codes above 127 have special meaning on Unix */
471 ERR_print_errors(bio_err);
472 sk_OPENSSL_CSTRING_free(engines);
473 sk_OPENSSL_STRING_free(pre_cmds);
474 sk_OPENSSL_STRING_free(post_cmds);