2 * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
5 /* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/opensslconf.h>
60 #ifdef OPENSSL_NO_ENGINE
61 NON_EMPTY_TRANSLATION_UNIT
68 # include <openssl/err.h>
69 # include <openssl/engine.h>
70 # include <openssl/ssl.h>
72 typedef enum OPTION_choice {
73 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
74 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
75 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
78 OPTIONS engine_options[] = {
79 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
80 {OPT_HELP_STR, 1, '-',
81 " engine... Engines to load\n"},
82 {"help", OPT_HELP, '-', "Display this summary"},
83 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
84 {"vv", OPT_VV, '-', "Also display each command's description"},
85 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
86 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
87 {"c", OPT_C, '-', "List the capabilities of specified engine"},
88 {"t", OPT_T, '-', "Check that specified engine is available"},
89 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
90 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
91 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
92 {OPT_MORE_STR, OPT_EOF, 1,
93 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
97 static void identity(char *ptr)
101 static int append_buf(char **buf, int *size, const char *s)
105 *buf = app_malloc(*size, "engine buffer");
109 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
112 tmp = OPENSSL_realloc(*buf, *size);
122 OPENSSL_strlcat(*buf, ", ", *size);
123 OPENSSL_strlcat(*buf, s, *size);
128 static int util_flags(BIO *out, unsigned int flags, const char *indent)
130 int started = 0, err = 0;
131 /* Indent before displaying input flags */
132 BIO_printf(out, "%s%s(input flags): ", indent, indent);
134 BIO_printf(out, "<no flags>\n");
138 * If the object is internal, mark it in a way that shows instead of
139 * having it part of all the other flags, even if it really is.
141 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
142 BIO_printf(out, "[Internal] ");
145 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
146 BIO_printf(out, "NUMERIC");
150 * Now we check that no combinations of the mutually exclusive NUMERIC,
151 * STRING, and NO_INPUT flags have been used. Future flags that can be
152 * OR'd together with these would need to added after these to preserve
155 if (flags & ENGINE_CMD_FLAG_STRING) {
157 BIO_printf(out, "|");
160 BIO_printf(out, "STRING");
163 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
165 BIO_printf(out, "|");
168 BIO_printf(out, "NO_INPUT");
171 /* Check for unknown flags */
172 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
173 ~ENGINE_CMD_FLAG_STRING &
174 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
177 BIO_printf(out, "|");
178 BIO_printf(out, "<0x%04X>", flags);
181 BIO_printf(out, " <illegal flags!>");
182 BIO_printf(out, "\n");
186 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
188 static const int line_wrap = 78;
195 STACK_OF(OPENSSL_STRING) *cmds = NULL;
196 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
197 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
198 0, NULL, NULL)) <= 0)) {
202 cmds = sk_OPENSSL_STRING_new_null();
208 /* Get the command input flags */
209 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
212 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
213 /* Get the command name */
214 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
217 name = app_malloc(len + 1, "name buffer");
218 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
221 /* Get the command description */
222 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
226 desc = app_malloc(len + 1, "description buffer");
227 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
231 /* Now decide on the output */
234 xpos = BIO_puts(out, indent);
236 /* Otherwise prepend a ", " */
237 xpos += BIO_printf(out, ", ");
240 * We're just listing names, comma-delimited
242 if ((xpos > (int)strlen(indent)) &&
243 (xpos + (int)strlen(name) > line_wrap)) {
244 BIO_printf(out, "\n");
245 xpos = BIO_puts(out, indent);
247 xpos += BIO_printf(out, "%s", name);
249 /* We're listing names plus descriptions */
250 BIO_printf(out, "%s: %s\n", name,
251 (desc == NULL) ? "<no description>" : desc);
252 /* ... and sometimes input flags */
253 if ((verbose >= 3) && !util_flags(out, flags, indent))
262 /* Move to the next command */
263 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
266 BIO_printf(out, "\n");
269 sk_OPENSSL_STRING_pop_free(cmds, identity);
275 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
276 BIO *out, const char *indent)
278 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
281 BIO_printf(out, "[Error]: internal stack error\n");
284 for (loop = 0; loop < num; loop++) {
286 const char *cmd, *arg;
287 cmd = sk_OPENSSL_STRING_value(cmds, loop);
288 res = 1; /* assume success */
289 /* Check if this command has no ":arg" */
290 if ((arg = strstr(cmd, ":")) == NULL) {
291 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
294 if ((int)(arg - cmd) > 254) {
295 BIO_printf(out, "[Error]: command name too long\n");
298 memcpy(buf, cmd, (int)(arg - cmd));
299 buf[arg - cmd] = '\0';
300 arg++; /* Move past the ":" */
301 /* Call the command with the argument */
302 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
306 BIO_printf(out, "[Success]: %s\n", cmd);
308 BIO_printf(out, "[Failure]: %s\n", cmd);
309 ERR_print_errors(out);
314 int engine_main(int argc, char **argv)
317 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
319 STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
320 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
321 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
323 const char *indent = " ";
328 out = dup_bio_out(FORMAT_TEXT);
329 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
332 /* Remember the original command name, parse/skip any leading engine
333 * names, and then setup to parse the rest of the line as flags. */
335 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
336 sk_OPENSSL_STRING_push(engines, argv1);
341 opt_init(argc, argv, engine_options);
343 while ((o = opt_next()) != OPT_EOF) {
347 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
350 opt_help(engine_options);
357 /* Convert to an integer from one to four. */
358 i = (int)(o - OPT_V) + 1;
371 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
374 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
379 /* Allow any trailing parameters as engine names. */
380 argc = opt_num_rest();
382 for ( ; *argv; argv++) {
384 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
386 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
389 sk_OPENSSL_STRING_push(engines, *argv);
392 if (sk_OPENSSL_STRING_num(engines) == 0) {
393 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
394 sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
398 for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
399 const char *id = sk_OPENSSL_STRING_value(engines, i);
400 if ((e = ENGINE_by_id(id)) != NULL) {
401 const char *name = ENGINE_get_name(e);
403 * Do "id" first, then "name". Easier to auto-parse.
405 BIO_printf(out, "(%s) %s\n", id, name);
406 util_do_cmds(e, pre_cmds, out, indent);
407 if (strcmp(ENGINE_get_id(e), id) != 0) {
408 BIO_printf(out, "Loaded: (%s) %s\n",
409 ENGINE_get_id(e), ENGINE_get_name(e));
413 char *cap_buf = NULL;
416 ENGINE_CIPHERS_PTR fn_c;
417 ENGINE_DIGESTS_PTR fn_d;
418 ENGINE_PKEY_METHS_PTR fn_pk;
420 if (ENGINE_get_RSA(e) != NULL
421 && !append_buf(&cap_buf, &cap_size, "RSA"))
423 if (ENGINE_get_DSA(e) != NULL
424 && !append_buf(&cap_buf, &cap_size, "DSA"))
426 if (ENGINE_get_DH(e) != NULL
427 && !append_buf(&cap_buf, &cap_size, "DH"))
429 if (ENGINE_get_RAND(e) != NULL
430 && !append_buf(&cap_buf, &cap_size, "RAND"))
433 fn_c = ENGINE_get_ciphers(e);
436 n = fn_c(e, NULL, &nids, 0);
437 for (k = 0; k < n; ++k)
438 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
442 fn_d = ENGINE_get_digests(e);
445 n = fn_d(e, NULL, &nids, 0);
446 for (k = 0; k < n; ++k)
447 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
451 fn_pk = ENGINE_get_pkey_meths(e);
454 n = fn_pk(e, NULL, &nids, 0);
455 for (k = 0; k < n; ++k)
456 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
459 if (cap_buf && (*cap_buf != '\0'))
460 BIO_printf(out, " [%s]\n", cap_buf);
462 OPENSSL_free(cap_buf);
465 BIO_printf(out, "%s", indent);
466 if (ENGINE_init(e)) {
467 BIO_printf(out, "[ available ]\n");
468 util_do_cmds(e, post_cmds, out, indent);
471 BIO_printf(out, "[ unavailable ]\n");
472 if (test_avail_noise)
473 ERR_print_errors_fp(stdout);
477 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
481 ERR_print_errors(bio_err);
487 ERR_print_errors(bio_err);
488 sk_OPENSSL_STRING_pop_free(engines, identity);
489 sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
490 sk_OPENSSL_STRING_pop_free(post_cmds, identity);