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
)
51 const int expand
= 256;
52 int len
= strlen(s
) + 1;
56 *size
= ((len
+ expand
- 1) / expand
) * expand
;
57 p
= *buf
= app_malloc(*size
, "engine buffer");
59 const int blen
= strlen(p
);
65 *size
= ((len
+ expand
- 1) / expand
) * expand
;
66 p
= OPENSSL_realloc(p
, *size
);
86 static int util_flags(BIO
*out
, unsigned int flags
, const char *indent
)
88 int started
= 0, err
= 0;
89 /* Indent before displaying input flags */
90 BIO_printf(out
, "%s%s(input flags): ", indent
, indent
);
92 BIO_printf(out
, "<no flags>\n");
96 * If the object is internal, mark it in a way that shows instead of
97 * having it part of all the other flags, even if it really is.
99 if (flags
& ENGINE_CMD_FLAG_INTERNAL
) {
100 BIO_printf(out
, "[Internal] ");
103 if (flags
& ENGINE_CMD_FLAG_NUMERIC
) {
104 BIO_printf(out
, "NUMERIC");
108 * Now we check that no combinations of the mutually exclusive NUMERIC,
109 * STRING, and NO_INPUT flags have been used. Future flags that can be
110 * OR'd together with these would need to added after these to preserve
113 if (flags
& ENGINE_CMD_FLAG_STRING
) {
115 BIO_printf(out
, "|");
118 BIO_printf(out
, "STRING");
121 if (flags
& ENGINE_CMD_FLAG_NO_INPUT
) {
123 BIO_printf(out
, "|");
126 BIO_printf(out
, "NO_INPUT");
129 /* Check for unknown flags */
130 flags
= flags
& ~ENGINE_CMD_FLAG_NUMERIC
&
131 ~ENGINE_CMD_FLAG_STRING
&
132 ~ENGINE_CMD_FLAG_NO_INPUT
& ~ENGINE_CMD_FLAG_INTERNAL
;
135 BIO_printf(out
, "|");
136 BIO_printf(out
, "<0x%04X>", flags
);
139 BIO_printf(out
, " <illegal flags!>");
140 BIO_printf(out
, "\n");
144 static int util_verbose(ENGINE
*e
, int verbose
, BIO
*out
, const char *indent
)
146 static const int line_wrap
= 78;
153 STACK_OF(OPENSSL_STRING
) *cmds
= NULL
;
154 if (!ENGINE_ctrl(e
, ENGINE_CTRL_HAS_CTRL_FUNCTION
, 0, NULL
, NULL
) ||
155 ((num
= ENGINE_ctrl(e
, ENGINE_CTRL_GET_FIRST_CMD_TYPE
,
156 0, NULL
, NULL
)) <= 0)) {
160 cmds
= sk_OPENSSL_STRING_new_null();
166 /* Get the command input flags */
167 if ((flags
= ENGINE_ctrl(e
, ENGINE_CTRL_GET_CMD_FLAGS
, num
,
170 if (!(flags
& ENGINE_CMD_FLAG_INTERNAL
) || verbose
>= 4) {
171 /* Get the command name */
172 if ((len
= ENGINE_ctrl(e
, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD
, num
,
175 name
= app_malloc(len
+ 1, "name buffer");
176 if (ENGINE_ctrl(e
, ENGINE_CTRL_GET_NAME_FROM_CMD
, num
, name
,
179 /* Get the command description */
180 if ((len
= ENGINE_ctrl(e
, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD
, num
,
184 desc
= app_malloc(len
+ 1, "description buffer");
185 if (ENGINE_ctrl(e
, ENGINE_CTRL_GET_DESC_FROM_CMD
, num
, desc
,
189 /* Now decide on the output */
192 xpos
= BIO_puts(out
, indent
);
194 /* Otherwise prepend a ", " */
195 xpos
+= BIO_printf(out
, ", ");
198 * We're just listing names, comma-delimited
200 if ((xpos
> (int)strlen(indent
)) &&
201 (xpos
+ (int)strlen(name
) > line_wrap
)) {
202 BIO_printf(out
, "\n");
203 xpos
= BIO_puts(out
, indent
);
205 xpos
+= BIO_printf(out
, "%s", name
);
207 /* We're listing names plus descriptions */
208 BIO_printf(out
, "%s: %s\n", name
,
209 (desc
== NULL
) ? "<no description>" : desc
);
210 /* ... and sometimes input flags */
211 if ((verbose
>= 3) && !util_flags(out
, flags
, indent
))
220 /* Move to the next command */
221 num
= ENGINE_ctrl(e
, ENGINE_CTRL_GET_NEXT_CMD_TYPE
, num
, NULL
, NULL
);
224 BIO_printf(out
, "\n");
227 sk_OPENSSL_STRING_free(cmds
);
233 static void util_do_cmds(ENGINE
*e
, STACK_OF(OPENSSL_STRING
) *cmds
,
234 BIO
*out
, const char *indent
)
236 int loop
, res
, num
= sk_OPENSSL_STRING_num(cmds
);
239 BIO_printf(out
, "[Error]: internal stack error\n");
242 for (loop
= 0; loop
< num
; loop
++) {
244 const char *cmd
, *arg
;
245 cmd
= sk_OPENSSL_STRING_value(cmds
, loop
);
246 res
= 1; /* assume success */
247 /* Check if this command has no ":arg" */
248 if ((arg
= strstr(cmd
, ":")) == NULL
) {
249 if (!ENGINE_ctrl_cmd_string(e
, cmd
, NULL
, 0))
252 if ((int)(arg
- cmd
) > 254) {
253 BIO_printf(out
, "[Error]: command name too long\n");
256 memcpy(buf
, cmd
, (int)(arg
- cmd
));
257 buf
[arg
- cmd
] = '\0';
258 arg
++; /* Move past the ":" */
259 /* Call the command with the argument */
260 if (!ENGINE_ctrl_cmd_string(e
, buf
, arg
, 0))
264 BIO_printf(out
, "[Success]: %s\n", cmd
);
266 BIO_printf(out
, "[Failure]: %s\n", cmd
);
267 ERR_print_errors(out
);
272 struct util_store_cap_data
{
278 static void util_store_cap(const OSSL_STORE_LOADER
*loader
, void *arg
)
280 struct util_store_cap_data
*ctx
= arg
;
282 if (OSSL_STORE_LOADER_get0_engine(loader
) == ctx
->engine
) {
284 BIO_snprintf(buf
, sizeof(buf
), "STORE(%s)",
285 OSSL_STORE_LOADER_get0_scheme(loader
));
286 if (!append_buf(ctx
->cap_buf
, ctx
->cap_size
, buf
))
291 int engine_main(int argc
, char **argv
)
294 int verbose
= 0, list_cap
= 0, test_avail
= 0, test_avail_noise
= 0;
296 STACK_OF(OPENSSL_CSTRING
) *engines
= sk_OPENSSL_CSTRING_new_null();
297 STACK_OF(OPENSSL_STRING
) *pre_cmds
= sk_OPENSSL_STRING_new_null();
298 STACK_OF(OPENSSL_STRING
) *post_cmds
= sk_OPENSSL_STRING_new_null();
300 const char *indent
= " ";
305 out
= dup_bio_out(FORMAT_TEXT
);
306 if (engines
== NULL
|| pre_cmds
== NULL
|| post_cmds
== NULL
)
309 /* Remember the original command name, parse/skip any leading engine
310 * names, and then setup to parse the rest of the line as flags. */
312 while ((argv1
= argv
[1]) != NULL
&& *argv1
!= '-') {
313 sk_OPENSSL_CSTRING_push(engines
, argv1
);
318 opt_init(argc
, argv
, engine_options
);
320 while ((o
= opt_next()) != OPT_EOF
) {
324 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
327 opt_help(engine_options
);
334 /* Convert to an integer from one to four. */
335 i
= (int)(o
- OPT_V
) + 1;
349 sk_OPENSSL_STRING_push(pre_cmds
, opt_arg());
352 sk_OPENSSL_STRING_push(post_cmds
, opt_arg());
357 /* Allow any trailing parameters as engine names. */
358 argc
= opt_num_rest();
360 for ( ; *argv
; argv
++) {
362 BIO_printf(bio_err
, "%s: Cannot mix flags and engine names.\n",
364 BIO_printf(bio_err
, "%s: Use -help for summary.\n", prog
);
367 sk_OPENSSL_CSTRING_push(engines
, *argv
);
370 if (sk_OPENSSL_CSTRING_num(engines
) == 0) {
371 for (e
= ENGINE_get_first(); e
!= NULL
; e
= ENGINE_get_next(e
)) {
372 sk_OPENSSL_CSTRING_push(engines
, ENGINE_get_id(e
));
377 for (i
= 0; i
< sk_OPENSSL_CSTRING_num(engines
); i
++) {
378 const char *id
= sk_OPENSSL_CSTRING_value(engines
, i
);
379 if ((e
= ENGINE_by_id(id
)) != NULL
) {
380 const char *name
= ENGINE_get_name(e
);
382 * Do "id" first, then "name". Easier to auto-parse.
384 BIO_printf(out
, "(%s) %s\n", id
, name
);
385 util_do_cmds(e
, pre_cmds
, out
, indent
);
386 if (strcmp(ENGINE_get_id(e
), id
) != 0) {
387 BIO_printf(out
, "Loaded: (%s) %s\n",
388 ENGINE_get_id(e
), ENGINE_get_name(e
));
392 char *cap_buf
= NULL
;
395 ENGINE_CIPHERS_PTR fn_c
;
396 ENGINE_DIGESTS_PTR fn_d
;
397 ENGINE_PKEY_METHS_PTR fn_pk
;
399 if (ENGINE_get_RSA(e
) != NULL
400 && !append_buf(&cap_buf
, &cap_size
, "RSA"))
402 if (ENGINE_get_DSA(e
) != NULL
403 && !append_buf(&cap_buf
, &cap_size
, "DSA"))
405 if (ENGINE_get_DH(e
) != NULL
406 && !append_buf(&cap_buf
, &cap_size
, "DH"))
408 if (ENGINE_get_RAND(e
) != NULL
409 && !append_buf(&cap_buf
, &cap_size
, "RAND"))
412 fn_c
= ENGINE_get_ciphers(e
);
415 n
= fn_c(e
, NULL
, &nids
, 0);
416 for (k
= 0; k
< n
; ++k
)
417 if (!append_buf(&cap_buf
, &cap_size
, OBJ_nid2sn(nids
[k
])))
421 fn_d
= ENGINE_get_digests(e
);
424 n
= fn_d(e
, NULL
, &nids
, 0);
425 for (k
= 0; k
< n
; ++k
)
426 if (!append_buf(&cap_buf
, &cap_size
, OBJ_nid2sn(nids
[k
])))
430 fn_pk
= ENGINE_get_pkey_meths(e
);
433 n
= fn_pk(e
, NULL
, &nids
, 0);
434 for (k
= 0; k
< n
; ++k
)
435 if (!append_buf(&cap_buf
, &cap_size
, OBJ_nid2sn(nids
[k
])))
439 struct util_store_cap_data store_ctx
;
441 store_ctx
.engine
= e
;
442 store_ctx
.cap_buf
= &cap_buf
;
443 store_ctx
.cap_size
= &cap_size
;
446 OSSL_STORE_do_all_loaders(util_store_cap
, &store_ctx
);
450 if (cap_buf
!= NULL
&& (*cap_buf
!= '\0'))
451 BIO_printf(out
, " [%s]\n", cap_buf
);
453 OPENSSL_free(cap_buf
);
456 BIO_printf(out
, "%s", indent
);
457 if (ENGINE_init(e
)) {
458 BIO_printf(out
, "[ available ]\n");
459 util_do_cmds(e
, post_cmds
, out
, indent
);
462 BIO_printf(out
, "[ unavailable ]\n");
463 if (test_avail_noise
)
464 ERR_print_errors_fp(stdout
);
468 if ((verbose
> 0) && !util_verbose(e
, verbose
, out
, indent
))
472 ERR_print_errors(bio_err
);
473 /* because exit codes above 127 have special meaning on Unix */
481 ERR_print_errors(bio_err
);
482 sk_OPENSSL_CSTRING_free(engines
);
483 sk_OPENSSL_STRING_free(pre_cmds
);
484 sk_OPENSSL_STRING_free(post_cmds
);