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).
63 #include <openssl/err.h>
64 #ifndef OPENSSL_NO_ENGINE
65 # include <openssl/engine.h>
66 # include <openssl/ssl.h>
68 typedef enum OPTION_choice {
69 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
70 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
71 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
74 OPTIONS engine_options[] = {
75 {"help", OPT_HELP, '-', "Display this summary"},
76 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
77 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
78 {"vv", OPT_VV, '-', "Also display each command's description"},
79 {"v", OPT_V, '-', "For each engine, list its 'control commands'"},
80 {"c", OPT_C, '-', "List the capabilities of each engine"},
81 {"t", OPT_T, '-', "Check that each engine is available"},
82 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
83 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
84 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
85 {OPT_MORE_STR, OPT_EOF, 1,
86 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
90 static void identity(char *ptr)
95 static int append_buf(char **buf, const char *s, int *size, int step)
101 *buf = app_malloc(*size, "engine buffer");
108 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
110 *buf = OPENSSL_realloc(*buf, *size);
117 BUF_strlcat(*buf, ", ", *size);
118 BUF_strlcat(*buf, s, *size);
123 static int util_flags(BIO *out, unsigned int flags, const char *indent)
125 int started = 0, err = 0;
126 /* Indent before displaying input flags */
127 BIO_printf(out, "%s%s(input flags): ", indent, indent);
129 BIO_printf(out, "<no flags>\n");
133 * If the object is internal, mark it in a way that shows instead of
134 * having it part of all the other flags, even if it really is.
136 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
137 BIO_printf(out, "[Internal] ");
140 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
141 BIO_printf(out, "NUMERIC");
145 * Now we check that no combinations of the mutually exclusive NUMERIC,
146 * STRING, and NO_INPUT flags have been used. Future flags that can be
147 * OR'd together with these would need to added after these to preserve
150 if (flags & ENGINE_CMD_FLAG_STRING) {
152 BIO_printf(out, "|");
155 BIO_printf(out, "STRING");
158 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
160 BIO_printf(out, "|");
163 BIO_printf(out, "NO_INPUT");
166 /* Check for unknown flags */
167 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
168 ~ENGINE_CMD_FLAG_STRING &
169 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
172 BIO_printf(out, "|");
173 BIO_printf(out, "<0x%04X>", flags);
176 BIO_printf(out, " <illegal flags!>");
177 BIO_printf(out, "\n");
181 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
183 static const int line_wrap = 78;
190 STACK_OF(OPENSSL_STRING) *cmds = NULL;
191 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
192 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
193 0, NULL, NULL)) <= 0)) {
197 cmds = sk_OPENSSL_STRING_new_null();
203 /* Get the command input flags */
204 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
207 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
208 /* Get the command name */
209 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
212 name = app_malloc(len + 1, "name buffer");
213 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
216 /* Get the command description */
217 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
221 desc = app_malloc(len + 1, "description buffer");
222 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
226 /* Now decide on the output */
229 xpos = BIO_puts(out, indent);
231 /* Otherwise prepend a ", " */
232 xpos += BIO_printf(out, ", ");
235 * We're just listing names, comma-delimited
237 if ((xpos > (int)strlen(indent)) &&
238 (xpos + (int)strlen(name) > line_wrap)) {
239 BIO_printf(out, "\n");
240 xpos = BIO_puts(out, indent);
242 xpos += BIO_printf(out, "%s", name);
244 /* We're listing names plus descriptions */
245 BIO_printf(out, "%s: %s\n", name,
246 (desc == NULL) ? "<no description>" : desc);
247 /* ... and sometimes input flags */
248 if ((verbose >= 3) && !util_flags(out, flags, indent))
257 /* Move to the next command */
258 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
261 BIO_printf(out, "\n");
264 sk_OPENSSL_STRING_pop_free(cmds, identity);
270 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
271 BIO *out, const char *indent)
273 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
276 BIO_printf(out, "[Error]: internal stack error\n");
279 for (loop = 0; loop < num; loop++) {
281 const char *cmd, *arg;
282 cmd = sk_OPENSSL_STRING_value(cmds, loop);
283 res = 1; /* assume success */
284 /* Check if this command has no ":arg" */
285 if ((arg = strstr(cmd, ":")) == NULL) {
286 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
289 if ((int)(arg - cmd) > 254) {
290 BIO_printf(out, "[Error]: command name too long\n");
293 memcpy(buf, cmd, (int)(arg - cmd));
294 buf[arg - cmd] = '\0';
295 arg++; /* Move past the ":" */
296 /* Call the command with the argument */
297 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
301 BIO_printf(out, "[Success]: %s\n", cmd);
303 BIO_printf(out, "[Failure]: %s\n", cmd);
304 ERR_print_errors(out);
309 int engine_main(int argc, char **argv)
312 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
314 STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
315 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
316 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
318 const char *indent = " ";
322 out = dup_bio_out(FORMAT_TEXT);
323 prog = opt_init(argc, argv, engine_options);
324 if (!engines || !pre_cmds || !post_cmds)
326 while ((o = opt_next()) != OPT_EOF) {
330 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
333 opt_help(engine_options);
340 /* Convert to an integer from one to four. */
341 i = (int)(o - OPT_V) + 1;
354 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
357 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
361 argc = opt_num_rest();
363 for ( ; *argv; argv++)
364 sk_OPENSSL_STRING_push(engines, *argv);
366 if (sk_OPENSSL_STRING_num(engines) == 0) {
367 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
368 sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
372 for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
373 const char *id = sk_OPENSSL_STRING_value(engines, i);
374 if ((e = ENGINE_by_id(id)) != NULL) {
375 const char *name = ENGINE_get_name(e);
377 * Do "id" first, then "name". Easier to auto-parse.
379 BIO_printf(out, "(%s) %s\n", id, name);
380 util_do_cmds(e, pre_cmds, out, indent);
381 if (strcmp(ENGINE_get_id(e), id) != 0) {
382 BIO_printf(out, "Loaded: (%s) %s\n",
383 ENGINE_get_id(e), ENGINE_get_name(e));
387 char *cap_buf = NULL;
390 ENGINE_CIPHERS_PTR fn_c;
391 ENGINE_DIGESTS_PTR fn_d;
392 ENGINE_PKEY_METHS_PTR fn_pk;
394 if (ENGINE_get_RSA(e) != NULL
395 && !append_buf(&cap_buf, "RSA", &cap_size, 256))
397 if (ENGINE_get_DSA(e) != NULL
398 && !append_buf(&cap_buf, "DSA", &cap_size, 256))
400 if (ENGINE_get_DH(e) != NULL
401 && !append_buf(&cap_buf, "DH", &cap_size, 256))
403 if (ENGINE_get_RAND(e) != NULL
404 && !append_buf(&cap_buf, "RAND", &cap_size, 256))
407 fn_c = ENGINE_get_ciphers(e);
410 n = fn_c(e, NULL, &nids, 0);
411 for (k = 0; k < n; ++k)
412 if (!append_buf(&cap_buf,
413 OBJ_nid2sn(nids[k]), &cap_size, 256))
417 fn_d = ENGINE_get_digests(e);
420 n = fn_d(e, NULL, &nids, 0);
421 for (k = 0; k < n; ++k)
422 if (!append_buf(&cap_buf,
423 OBJ_nid2sn(nids[k]), &cap_size, 256))
427 fn_pk = ENGINE_get_pkey_meths(e);
430 n = fn_pk(e, NULL, &nids, 0);
431 for (k = 0; k < n; ++k)
432 if (!append_buf(&cap_buf,
433 OBJ_nid2sn(nids[k]), &cap_size, 256))
436 if (cap_buf && (*cap_buf != '\0'))
437 BIO_printf(out, " [%s]\n", cap_buf);
439 OPENSSL_free(cap_buf);
442 BIO_printf(out, "%s", indent);
443 if (ENGINE_init(e)) {
444 BIO_printf(out, "[ available ]\n");
445 util_do_cmds(e, post_cmds, out, indent);
448 BIO_printf(out, "[ unavailable ]\n");
449 if (test_avail_noise)
450 ERR_print_errors_fp(stdout);
454 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
458 ERR_print_errors(bio_err);
464 ERR_print_errors(bio_err);
465 sk_OPENSSL_STRING_pop_free(engines, identity);
466 sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
467 sk_OPENSSL_STRING_pop_free(post_cmds, identity);
474 static void *dummy = &dummy;