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 int append_buf(char **buf, int *size, const char *s)
52 *buf = app_malloc(*size, "engine buffer");
56 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
59 tmp = OPENSSL_realloc(*buf, *size);
69 OPENSSL_strlcat(*buf, ", ", *size);
70 OPENSSL_strlcat(*buf, s, *size);
75 static int util_flags(BIO *out, unsigned int flags, const char *indent)
77 int started = 0, err = 0;
78 /* Indent before displaying input flags */
79 BIO_printf(out, "%s%s(input flags): ", indent, indent);
81 BIO_printf(out, "<no flags>\n");
85 * If the object is internal, mark it in a way that shows instead of
86 * having it part of all the other flags, even if it really is.
88 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
89 BIO_printf(out, "[Internal] ");
92 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
93 BIO_printf(out, "NUMERIC");
97 * Now we check that no combinations of the mutually exclusive NUMERIC,
98 * STRING, and NO_INPUT flags have been used. Future flags that can be
99 * OR'd together with these would need to added after these to preserve
102 if (flags & ENGINE_CMD_FLAG_STRING) {
104 BIO_printf(out, "|");
107 BIO_printf(out, "STRING");
110 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
112 BIO_printf(out, "|");
115 BIO_printf(out, "NO_INPUT");
118 /* Check for unknown flags */
119 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
120 ~ENGINE_CMD_FLAG_STRING &
121 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
124 BIO_printf(out, "|");
125 BIO_printf(out, "<0x%04X>", flags);
128 BIO_printf(out, " <illegal flags!>");
129 BIO_printf(out, "\n");
133 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
135 static const int line_wrap = 78;
142 STACK_OF(OPENSSL_STRING) *cmds = NULL;
143 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
144 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
145 0, NULL, NULL)) <= 0)) {
149 cmds = sk_OPENSSL_STRING_new_null();
155 /* Get the command input flags */
156 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
159 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
160 /* Get the command name */
161 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
164 name = app_malloc(len + 1, "name buffer");
165 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
168 /* Get the command description */
169 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
173 desc = app_malloc(len + 1, "description buffer");
174 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
178 /* Now decide on the output */
181 xpos = BIO_puts(out, indent);
183 /* Otherwise prepend a ", " */
184 xpos += BIO_printf(out, ", ");
187 * We're just listing names, comma-delimited
189 if ((xpos > (int)strlen(indent)) &&
190 (xpos + (int)strlen(name) > line_wrap)) {
191 BIO_printf(out, "\n");
192 xpos = BIO_puts(out, indent);
194 xpos += BIO_printf(out, "%s", name);
196 /* We're listing names plus descriptions */
197 BIO_printf(out, "%s: %s\n", name,
198 (desc == NULL) ? "<no description>" : desc);
199 /* ... and sometimes input flags */
200 if ((verbose >= 3) && !util_flags(out, flags, indent))
209 /* Move to the next command */
210 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
213 BIO_printf(out, "\n");
216 sk_OPENSSL_STRING_free(cmds);
222 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
223 BIO *out, const char *indent)
225 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
228 BIO_printf(out, "[Error]: internal stack error\n");
231 for (loop = 0; loop < num; loop++) {
233 const char *cmd, *arg;
234 cmd = sk_OPENSSL_STRING_value(cmds, loop);
235 res = 1; /* assume success */
236 /* Check if this command has no ":arg" */
237 if ((arg = strstr(cmd, ":")) == NULL) {
238 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
241 if ((int)(arg - cmd) > 254) {
242 BIO_printf(out, "[Error]: command name too long\n");
245 memcpy(buf, cmd, (int)(arg - cmd));
246 buf[arg - cmd] = '\0';
247 arg++; /* Move past the ":" */
248 /* Call the command with the argument */
249 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
253 BIO_printf(out, "[Success]: %s\n", cmd);
255 BIO_printf(out, "[Failure]: %s\n", cmd);
256 ERR_print_errors(out);
261 int engine_main(int argc, char **argv)
264 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
266 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
267 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
268 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
270 const char *indent = " ";
275 out = dup_bio_out(FORMAT_TEXT);
276 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
279 /* Remember the original command name, parse/skip any leading engine
280 * names, and then setup to parse the rest of the line as flags. */
282 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
283 sk_OPENSSL_CSTRING_push(engines, argv1);
288 opt_init(argc, argv, engine_options);
290 while ((o = opt_next()) != OPT_EOF) {
294 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
297 opt_help(engine_options);
304 /* Convert to an integer from one to four. */
305 i = (int)(o - OPT_V) + 1;
319 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
322 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
327 /* Allow any trailing parameters as engine names. */
328 argc = opt_num_rest();
330 for ( ; *argv; argv++) {
332 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
334 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
337 sk_OPENSSL_CSTRING_push(engines, *argv);
340 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
341 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
342 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
347 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
348 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
349 if ((e = ENGINE_by_id(id)) != NULL) {
350 const char *name = ENGINE_get_name(e);
352 * Do "id" first, then "name". Easier to auto-parse.
354 BIO_printf(out, "(%s) %s\n", id, name);
355 util_do_cmds(e, pre_cmds, out, indent);
356 if (strcmp(ENGINE_get_id(e), id) != 0) {
357 BIO_printf(out, "Loaded: (%s) %s\n",
358 ENGINE_get_id(e), ENGINE_get_name(e));
362 char *cap_buf = NULL;
365 ENGINE_CIPHERS_PTR fn_c;
366 ENGINE_DIGESTS_PTR fn_d;
367 ENGINE_PKEY_METHS_PTR fn_pk;
369 if (ENGINE_get_RSA(e) != NULL
370 && !append_buf(&cap_buf, &cap_size, "RSA"))
372 if (ENGINE_get_DSA(e) != NULL
373 && !append_buf(&cap_buf, &cap_size, "DSA"))
375 if (ENGINE_get_DH(e) != NULL
376 && !append_buf(&cap_buf, &cap_size, "DH"))
378 if (ENGINE_get_RAND(e) != NULL
379 && !append_buf(&cap_buf, &cap_size, "RAND"))
382 fn_c = ENGINE_get_ciphers(e);
385 n = fn_c(e, NULL, &nids, 0);
386 for (k = 0; k < n; ++k)
387 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
391 fn_d = ENGINE_get_digests(e);
394 n = fn_d(e, NULL, &nids, 0);
395 for (k = 0; k < n; ++k)
396 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
400 fn_pk = ENGINE_get_pkey_meths(e);
403 n = fn_pk(e, NULL, &nids, 0);
404 for (k = 0; k < n; ++k)
405 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
408 if (cap_buf && (*cap_buf != '\0'))
409 BIO_printf(out, " [%s]\n", cap_buf);
411 OPENSSL_free(cap_buf);
414 BIO_printf(out, "%s", indent);
415 if (ENGINE_init(e)) {
416 BIO_printf(out, "[ available ]\n");
417 util_do_cmds(e, post_cmds, out, indent);
420 BIO_printf(out, "[ unavailable ]\n");
421 if (test_avail_noise)
422 ERR_print_errors_fp(stdout);
426 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
430 ERR_print_errors(bio_err);
431 /* because exit codes above 127 have special meaning on Unix */
439 ERR_print_errors(bio_err);
440 sk_OPENSSL_CSTRING_free(engines);
441 sk_OPENSSL_STRING_free(pre_cmds);
442 sk_OPENSSL_STRING_free(post_cmds);