2 * Copyright 2000-2016 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>
23 typedef enum OPTION_choice {
24 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
25 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
26 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
29 OPTIONS engine_options[] = {
30 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
31 {OPT_HELP_STR, 1, '-',
32 " engine... Engines to load\n"},
33 {"help", OPT_HELP, '-', "Display this summary"},
34 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
35 {"vv", OPT_VV, '-', "Also display each command's description"},
36 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
37 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
38 {"c", OPT_C, '-', "List the capabilities of specified engine"},
39 {"t", OPT_T, '-', "Check that specified engine is available"},
40 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
41 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
42 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
43 {OPT_MORE_STR, OPT_EOF, 1,
44 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
48 static void identity(char *ptr)
52 static int append_buf(char **buf, int *size, const char *s)
56 *buf = app_malloc(*size, "engine buffer");
60 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
63 tmp = OPENSSL_realloc(*buf, *size);
73 OPENSSL_strlcat(*buf, ", ", *size);
74 OPENSSL_strlcat(*buf, s, *size);
79 static int util_flags(BIO *out, unsigned int flags, const char *indent)
81 int started = 0, err = 0;
82 /* Indent before displaying input flags */
83 BIO_printf(out, "%s%s(input flags): ", indent, indent);
85 BIO_printf(out, "<no flags>\n");
89 * If the object is internal, mark it in a way that shows instead of
90 * having it part of all the other flags, even if it really is.
92 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
93 BIO_printf(out, "[Internal] ");
96 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
97 BIO_printf(out, "NUMERIC");
101 * Now we check that no combinations of the mutually exclusive NUMERIC,
102 * STRING, and NO_INPUT flags have been used. Future flags that can be
103 * OR'd together with these would need to added after these to preserve
106 if (flags & ENGINE_CMD_FLAG_STRING) {
108 BIO_printf(out, "|");
111 BIO_printf(out, "STRING");
114 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
116 BIO_printf(out, "|");
119 BIO_printf(out, "NO_INPUT");
122 /* Check for unknown flags */
123 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
124 ~ENGINE_CMD_FLAG_STRING &
125 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
128 BIO_printf(out, "|");
129 BIO_printf(out, "<0x%04X>", flags);
132 BIO_printf(out, " <illegal flags!>");
133 BIO_printf(out, "\n");
137 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
139 static const int line_wrap = 78;
146 STACK_OF(OPENSSL_STRING) *cmds = NULL;
147 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
148 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
149 0, NULL, NULL)) <= 0)) {
153 cmds = sk_OPENSSL_STRING_new_null();
159 /* Get the command input flags */
160 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
163 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
164 /* Get the command name */
165 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
168 name = app_malloc(len + 1, "name buffer");
169 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
172 /* Get the command description */
173 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
177 desc = app_malloc(len + 1, "description buffer");
178 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
182 /* Now decide on the output */
185 xpos = BIO_puts(out, indent);
187 /* Otherwise prepend a ", " */
188 xpos += BIO_printf(out, ", ");
191 * We're just listing names, comma-delimited
193 if ((xpos > (int)strlen(indent)) &&
194 (xpos + (int)strlen(name) > line_wrap)) {
195 BIO_printf(out, "\n");
196 xpos = BIO_puts(out, indent);
198 xpos += BIO_printf(out, "%s", name);
200 /* We're listing names plus descriptions */
201 BIO_printf(out, "%s: %s\n", name,
202 (desc == NULL) ? "<no description>" : desc);
203 /* ... and sometimes input flags */
204 if ((verbose >= 3) && !util_flags(out, flags, indent))
213 /* Move to the next command */
214 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
217 BIO_printf(out, "\n");
220 sk_OPENSSL_STRING_pop_free(cmds, identity);
226 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
227 BIO *out, const char *indent)
229 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
232 BIO_printf(out, "[Error]: internal stack error\n");
235 for (loop = 0; loop < num; loop++) {
237 const char *cmd, *arg;
238 cmd = sk_OPENSSL_STRING_value(cmds, loop);
239 res = 1; /* assume success */
240 /* Check if this command has no ":arg" */
241 if ((arg = strstr(cmd, ":")) == NULL) {
242 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
245 if ((int)(arg - cmd) > 254) {
246 BIO_printf(out, "[Error]: command name too long\n");
249 memcpy(buf, cmd, (int)(arg - cmd));
250 buf[arg - cmd] = '\0';
251 arg++; /* Move past the ":" */
252 /* Call the command with the argument */
253 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
257 BIO_printf(out, "[Success]: %s\n", cmd);
259 BIO_printf(out, "[Failure]: %s\n", cmd);
260 ERR_print_errors(out);
265 int engine_main(int argc, char **argv)
268 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
270 STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
271 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
272 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
274 const char *indent = " ";
279 out = dup_bio_out(FORMAT_TEXT);
280 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
283 /* Remember the original command name, parse/skip any leading engine
284 * names, and then setup to parse the rest of the line as flags. */
286 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
287 sk_OPENSSL_STRING_push(engines, argv1);
292 opt_init(argc, argv, engine_options);
294 while ((o = opt_next()) != OPT_EOF) {
298 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
301 opt_help(engine_options);
308 /* Convert to an integer from one to four. */
309 i = (int)(o - OPT_V) + 1;
322 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
325 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
330 /* Allow any trailing parameters as engine names. */
331 argc = opt_num_rest();
333 for ( ; *argv; argv++) {
335 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
337 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
340 sk_OPENSSL_STRING_push(engines, *argv);
343 if (sk_OPENSSL_STRING_num(engines) == 0) {
344 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
345 sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
349 for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
350 const char *id = sk_OPENSSL_STRING_value(engines, i);
351 if ((e = ENGINE_by_id(id)) != NULL) {
352 const char *name = ENGINE_get_name(e);
354 * Do "id" first, then "name". Easier to auto-parse.
356 BIO_printf(out, "(%s) %s\n", id, name);
357 util_do_cmds(e, pre_cmds, out, indent);
358 if (strcmp(ENGINE_get_id(e), id) != 0) {
359 BIO_printf(out, "Loaded: (%s) %s\n",
360 ENGINE_get_id(e), ENGINE_get_name(e));
364 char *cap_buf = NULL;
367 ENGINE_CIPHERS_PTR fn_c;
368 ENGINE_DIGESTS_PTR fn_d;
369 ENGINE_PKEY_METHS_PTR fn_pk;
371 if (ENGINE_get_RSA(e) != NULL
372 && !append_buf(&cap_buf, &cap_size, "RSA"))
374 if (ENGINE_get_DSA(e) != NULL
375 && !append_buf(&cap_buf, &cap_size, "DSA"))
377 if (ENGINE_get_DH(e) != NULL
378 && !append_buf(&cap_buf, &cap_size, "DH"))
380 if (ENGINE_get_RAND(e) != NULL
381 && !append_buf(&cap_buf, &cap_size, "RAND"))
384 fn_c = ENGINE_get_ciphers(e);
387 n = fn_c(e, NULL, &nids, 0);
388 for (k = 0; k < n; ++k)
389 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
393 fn_d = ENGINE_get_digests(e);
396 n = fn_d(e, NULL, &nids, 0);
397 for (k = 0; k < n; ++k)
398 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
402 fn_pk = ENGINE_get_pkey_meths(e);
405 n = fn_pk(e, NULL, &nids, 0);
406 for (k = 0; k < n; ++k)
407 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
410 if (cap_buf && (*cap_buf != '\0'))
411 BIO_printf(out, " [%s]\n", cap_buf);
413 OPENSSL_free(cap_buf);
416 BIO_printf(out, "%s", indent);
417 if (ENGINE_init(e)) {
418 BIO_printf(out, "[ available ]\n");
419 util_do_cmds(e, post_cmds, out, indent);
422 BIO_printf(out, "[ unavailable ]\n");
423 if (test_avail_noise)
424 ERR_print_errors_fp(stdout);
428 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
432 ERR_print_errors(bio_err);
438 ERR_print_errors(bio_err);
439 sk_OPENSSL_STRING_pop_free(engines, identity);
440 sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
441 sk_OPENSSL_STRING_pop_free(post_cmds, identity);