blob: 83f9588a0ab19fca3c45269e8f5e9af766ea20a1 [file] [log] [blame]
Matt Caswell0f113f32015-01-22 03:40:55 +00001/*
Matt Caswell6738bf12018-02-13 12:51:29 +00002 * Copyright 2000-2018 The OpenSSL Project Authors. All Rights Reserved.
Richard Levitte14c6d272000-11-01 02:57:35 +00003 *
Rich Salz846e33c2016-05-17 14:18:30 -04004 * 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
Richard Levitte14c6d272000-11-01 02:57:35 +00008 */
9
Rich Salzeffaf4d2016-01-31 13:08:23 -050010#include <openssl/opensslconf.h>
11#ifdef OPENSSL_NO_ENGINE
12NON_EMPTY_TRANSLATION_UNIT
13#else
14
15# include "apps.h"
Richard Levittedab2cd62018-01-31 11:13:10 +010016# include "progs.h"
Rich Salzeffaf4d2016-01-31 13:08:23 -050017# include <stdio.h>
18# include <stdlib.h>
19# include <string.h>
20# include <openssl/err.h>
Matt Caswell0f113f32015-01-22 03:40:55 +000021# include <openssl/engine.h>
22# include <openssl/ssl.h>
Richard Levittefa669492016-12-11 07:06:13 +010023# include <openssl/store.h>
Richard Levitte14c6d272000-11-01 02:57:35 +000024
Rich Salz7e1b7482015-04-24 15:26:15 -040025typedef enum OPTION_choice {
26 OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
27 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
28 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
29} OPTION_CHOICE;
Richard Levitte14c6d272000-11-01 02:57:35 +000030
FdaSilvaYY44c83eb2016-03-13 14:07:50 +010031const OPTIONS engine_options[] = {
Rich Salz0d1e0032015-12-22 08:22:33 -050032 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
33 {OPT_HELP_STR, 1, '-',
34 " engine... Engines to load\n"},
Rich Salz7e1b7482015-04-24 15:26:15 -040035 {"help", OPT_HELP, '-', "Display this summary"},
Rich Salz0d1e0032015-12-22 08:22:33 -050036 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
Rich Salz7e1b7482015-04-24 15:26:15 -040037 {"vv", OPT_VV, '-', "Also display each command's description"},
Rich Salz0d1e0032015-12-22 08:22:33 -050038 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
39 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
40 {"c", OPT_C, '-', "List the capabilities of specified engine"},
41 {"t", OPT_T, '-', "Check that specified engine is available"},
Rich Salz7e1b7482015-04-24 15:26:15 -040042 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
43 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
44 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
45 {OPT_MORE_STR, OPT_EOF, 1,
46 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
47 {NULL}
Richard Levitte14c6d272000-11-01 02:57:35 +000048};
49
Rich Salz0d1e0032015-12-22 08:22:33 -050050static int append_buf(char **buf, int *size, const char *s)
Matt Caswell0f113f32015-01-22 03:40:55 +000051{
Paulib2ac85a2017-07-06 08:03:58 +100052 const int expand = 256;
53 int len = strlen(s) + 1;
54 char *p = *buf;
Richard Levitte69e78052000-11-02 19:24:48 +000055
Paulib2ac85a2017-07-06 08:03:58 +100056 if (p == NULL) {
57 *size = ((len + expand - 1) / expand) * expand;
58 p = *buf = app_malloc(*size, "engine buffer");
59 } else {
60 const int blen = strlen(p);
61
62 if (blen > 0)
63 len += 2 + blen;
64
65 if (len > *size) {
66 *size = ((len + expand - 1) / expand) * expand;
67 p = OPENSSL_realloc(p, *size);
68 if (p == NULL) {
69 OPENSSL_free(*buf);
70 *buf = NULL;
71 return 0;
72 }
73 *buf = p;
Dr. Stephen Henson7c0ef842016-05-11 21:14:57 +010074 }
Paulib2ac85a2017-07-06 08:03:58 +100075
76 if (blen > 0) {
77 p += blen;
78 *p++ = ',';
79 *p++ = ' ';
80 }
Matt Caswell0f113f32015-01-22 03:40:55 +000081 }
Richard Levitte69e78052000-11-02 19:24:48 +000082
Paulib2ac85a2017-07-06 08:03:58 +100083 strcpy(p, s);
Matt Caswell0f113f32015-01-22 03:40:55 +000084 return 1;
85}
Richard Levitte69e78052000-11-02 19:24:48 +000086
Rich Salz7e1b7482015-04-24 15:26:15 -040087static int util_flags(BIO *out, unsigned int flags, const char *indent)
Matt Caswell0f113f32015-01-22 03:40:55 +000088{
89 int started = 0, err = 0;
90 /* Indent before displaying input flags */
Rich Salz7e1b7482015-04-24 15:26:15 -040091 BIO_printf(out, "%s%s(input flags): ", indent, indent);
Matt Caswell0f113f32015-01-22 03:40:55 +000092 if (flags == 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -040093 BIO_printf(out, "<no flags>\n");
Matt Caswell0f113f32015-01-22 03:40:55 +000094 return 1;
95 }
96 /*
97 * If the object is internal, mark it in a way that shows instead of
98 * having it part of all the other flags, even if it really is.
99 */
100 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400101 BIO_printf(out, "[Internal] ");
Matt Caswell0f113f32015-01-22 03:40:55 +0000102 }
Richard Levitte2cd3ad92001-06-20 06:35:46 +0000103
Matt Caswell0f113f32015-01-22 03:40:55 +0000104 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400105 BIO_printf(out, "NUMERIC");
Matt Caswell0f113f32015-01-22 03:40:55 +0000106 started = 1;
107 }
108 /*
109 * Now we check that no combinations of the mutually exclusive NUMERIC,
110 * STRING, and NO_INPUT flags have been used. Future flags that can be
111 * OR'd together with these would need to added after these to preserve
112 * the testing logic.
113 */
114 if (flags & ENGINE_CMD_FLAG_STRING) {
115 if (started) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400116 BIO_printf(out, "|");
Matt Caswell0f113f32015-01-22 03:40:55 +0000117 err = 1;
118 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400119 BIO_printf(out, "STRING");
Matt Caswell0f113f32015-01-22 03:40:55 +0000120 started = 1;
121 }
122 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
123 if (started) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400124 BIO_printf(out, "|");
Matt Caswell0f113f32015-01-22 03:40:55 +0000125 err = 1;
126 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400127 BIO_printf(out, "NO_INPUT");
Matt Caswell0f113f32015-01-22 03:40:55 +0000128 started = 1;
129 }
130 /* Check for unknown flags */
131 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
132 ~ENGINE_CMD_FLAG_STRING &
133 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
134 if (flags) {
135 if (started)
Rich Salz7e1b7482015-04-24 15:26:15 -0400136 BIO_printf(out, "|");
137 BIO_printf(out, "<0x%04X>", flags);
Matt Caswell0f113f32015-01-22 03:40:55 +0000138 }
139 if (err)
Rich Salz7e1b7482015-04-24 15:26:15 -0400140 BIO_printf(out, " <illegal flags!>");
141 BIO_printf(out, "\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000142 return 1;
143}
Geoff Thorpef11bc842001-04-19 02:08:26 +0000144
Rich Salz7e1b7482015-04-24 15:26:15 -0400145static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
Matt Caswell0f113f32015-01-22 03:40:55 +0000146{
147 static const int line_wrap = 78;
148 int num;
149 int ret = 0;
150 char *name = NULL;
151 char *desc = NULL;
152 int flags;
153 int xpos = 0;
154 STACK_OF(OPENSSL_STRING) *cmds = NULL;
155 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
156 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
157 0, NULL, NULL)) <= 0)) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000158 return 1;
159 }
Dr. Stephen Hensona45e4a52001-06-19 17:13:48 +0000160
Matt Caswell0f113f32015-01-22 03:40:55 +0000161 cmds = sk_OPENSSL_STRING_new_null();
Paul Yang22342122017-06-13 01:24:02 +0800162 if (cmds == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000163 goto err;
Rich Salz7e1b7482015-04-24 15:26:15 -0400164
Matt Caswell0f113f32015-01-22 03:40:55 +0000165 do {
166 int len;
167 /* Get the command input flags */
168 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
169 NULL, NULL)) < 0)
170 goto err;
171 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
172 /* Get the command name */
173 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
174 NULL, NULL)) <= 0)
175 goto err;
Rich Salz68dc6822015-04-30 17:48:31 -0400176 name = app_malloc(len + 1, "name buffer");
Matt Caswell0f113f32015-01-22 03:40:55 +0000177 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
178 NULL) <= 0)
179 goto err;
180 /* Get the command description */
181 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
182 NULL, NULL)) < 0)
183 goto err;
184 if (len > 0) {
Rich Salz68dc6822015-04-30 17:48:31 -0400185 desc = app_malloc(len + 1, "description buffer");
Matt Caswell0f113f32015-01-22 03:40:55 +0000186 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
Richard Levitte2cd3ad92001-06-20 06:35:46 +0000187 NULL) <= 0)
Matt Caswell0f113f32015-01-22 03:40:55 +0000188 goto err;
189 }
190 /* Now decide on the output */
191 if (xpos == 0)
192 /* Do an indent */
Rich Salz7e1b7482015-04-24 15:26:15 -0400193 xpos = BIO_puts(out, indent);
Matt Caswell0f113f32015-01-22 03:40:55 +0000194 else
195 /* Otherwise prepend a ", " */
Rich Salz7e1b7482015-04-24 15:26:15 -0400196 xpos += BIO_printf(out, ", ");
Matt Caswell0f113f32015-01-22 03:40:55 +0000197 if (verbose == 1) {
198 /*
199 * We're just listing names, comma-delimited
200 */
201 if ((xpos > (int)strlen(indent)) &&
202 (xpos + (int)strlen(name) > line_wrap)) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400203 BIO_printf(out, "\n");
204 xpos = BIO_puts(out, indent);
Matt Caswell0f113f32015-01-22 03:40:55 +0000205 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400206 xpos += BIO_printf(out, "%s", name);
Matt Caswell0f113f32015-01-22 03:40:55 +0000207 } else {
208 /* We're listing names plus descriptions */
Rich Salz7e1b7482015-04-24 15:26:15 -0400209 BIO_printf(out, "%s: %s\n", name,
Matt Caswell0f113f32015-01-22 03:40:55 +0000210 (desc == NULL) ? "<no description>" : desc);
211 /* ... and sometimes input flags */
Rich Salz7e1b7482015-04-24 15:26:15 -0400212 if ((verbose >= 3) && !util_flags(out, flags, indent))
Matt Caswell0f113f32015-01-22 03:40:55 +0000213 goto err;
214 xpos = 0;
215 }
216 }
217 OPENSSL_free(name);
218 name = NULL;
Rich Salzb548a1f2015-05-01 10:02:07 -0400219 OPENSSL_free(desc);
220 desc = NULL;
Matt Caswell0f113f32015-01-22 03:40:55 +0000221 /* Move to the next command */
222 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
223 } while (num > 0);
224 if (xpos > 0)
Rich Salz7e1b7482015-04-24 15:26:15 -0400225 BIO_printf(out, "\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000226 ret = 1;
227 err:
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100228 sk_OPENSSL_STRING_free(cmds);
Rich Salzb548a1f2015-05-01 10:02:07 -0400229 OPENSSL_free(name);
230 OPENSSL_free(desc);
Matt Caswell0f113f32015-01-22 03:40:55 +0000231 return ret;
232}
Geoff Thorpef11bc842001-04-19 02:08:26 +0000233
Dr. Stephen Hensonc869da82009-07-27 21:10:00 +0000234static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
Rich Salz7e1b7482015-04-24 15:26:15 -0400235 BIO *out, const char *indent)
Matt Caswell0f113f32015-01-22 03:40:55 +0000236{
237 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
Ben Laurie5ce278a2008-06-04 11:01:43 +0000238
Matt Caswell0f113f32015-01-22 03:40:55 +0000239 if (num < 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400240 BIO_printf(out, "[Error]: internal stack error\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000241 return;
242 }
243 for (loop = 0; loop < num; loop++) {
244 char buf[256];
245 const char *cmd, *arg;
246 cmd = sk_OPENSSL_STRING_value(cmds, loop);
247 res = 1; /* assume success */
248 /* Check if this command has no ":arg" */
249 if ((arg = strstr(cmd, ":")) == NULL) {
250 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
251 res = 0;
252 } else {
253 if ((int)(arg - cmd) > 254) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400254 BIO_printf(out, "[Error]: command name too long\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000255 return;
256 }
257 memcpy(buf, cmd, (int)(arg - cmd));
258 buf[arg - cmd] = '\0';
259 arg++; /* Move past the ":" */
260 /* Call the command with the argument */
261 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
262 res = 0;
263 }
Paul Yang22342122017-06-13 01:24:02 +0800264 if (res) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400265 BIO_printf(out, "[Success]: %s\n", cmd);
Paul Yang22342122017-06-13 01:24:02 +0800266 } else {
Rich Salz7e1b7482015-04-24 15:26:15 -0400267 BIO_printf(out, "[Failure]: %s\n", cmd);
268 ERR_print_errors(out);
Matt Caswell0f113f32015-01-22 03:40:55 +0000269 }
270 }
271}
Geoff Thorpef11bc842001-04-19 02:08:26 +0000272
Richard Levittefa669492016-12-11 07:06:13 +0100273struct util_store_cap_data {
274 ENGINE *engine;
275 char **cap_buf;
276 int *cap_size;
277 int ok;
278};
279static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
280{
281 struct util_store_cap_data *ctx = arg;
282
283 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
284 char buf[256];
285 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
286 OSSL_STORE_LOADER_get0_scheme(loader));
287 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
288 ctx->ok = 0;
289 }
290}
291
Rich Salz7e1b7482015-04-24 15:26:15 -0400292int engine_main(int argc, char **argv)
Matt Caswell0f113f32015-01-22 03:40:55 +0000293{
294 int ret = 1, i;
Matt Caswell0f113f32015-01-22 03:40:55 +0000295 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
296 ENGINE *e;
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100297 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
Matt Caswell0f113f32015-01-22 03:40:55 +0000298 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
299 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
Rich Salz7e1b7482015-04-24 15:26:15 -0400300 BIO *out;
Matt Caswell0f113f32015-01-22 03:40:55 +0000301 const char *indent = " ";
Rich Salz7e1b7482015-04-24 15:26:15 -0400302 OPTION_CHOICE o;
303 char *prog;
Rich Salz0d1e0032015-12-22 08:22:33 -0500304 char *argv1;
Richard Levitte14c6d272000-11-01 02:57:35 +0000305
Richard Levittea60994d2015-09-06 12:20:12 +0200306 out = dup_bio_out(FORMAT_TEXT);
Rich Salz0d1e0032015-12-22 08:22:33 -0500307 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000308 goto end;
Rich Salz0d1e0032015-12-22 08:22:33 -0500309
310 /* Remember the original command name, parse/skip any leading engine
311 * names, and then setup to parse the rest of the line as flags. */
312 prog = argv[0];
313 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100314 sk_OPENSSL_CSTRING_push(engines, argv1);
Rich Salz0d1e0032015-12-22 08:22:33 -0500315 argc--;
316 argv++;
317 }
318 argv[0] = prog;
319 opt_init(argc, argv, engine_options);
320
Rich Salz7e1b7482015-04-24 15:26:15 -0400321 while ((o = opt_next()) != OPT_EOF) {
322 switch (o) {
323 case OPT_EOF:
324 case OPT_ERR:
325 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
326 goto end;
327 case OPT_HELP:
328 opt_help(engine_options);
329 ret = 0;
330 goto end;
331 case OPT_VVVV:
332 case OPT_VVV:
333 case OPT_VV:
334 case OPT_V:
335 /* Convert to an integer from one to four. */
336 i = (int)(o - OPT_V) + 1;
337 if (verbose < i)
338 verbose = i;
339 break;
340 case OPT_C:
Matt Caswell0f113f32015-01-22 03:40:55 +0000341 list_cap = 1;
Rich Salz7e1b7482015-04-24 15:26:15 -0400342 break;
343 case OPT_TT:
344 test_avail_noise++;
Bernd Edlinger018fcbe2017-05-11 16:21:37 +0200345 /* fall thru */
Rich Salz7e1b7482015-04-24 15:26:15 -0400346 case OPT_T:
347 test_avail++;
348 break;
349 case OPT_PRE:
350 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
351 break;
352 case OPT_POST:
353 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
354 break;
355 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000356 }
Rich Salz0d1e0032015-12-22 08:22:33 -0500357
358 /* Allow any trailing parameters as engine names. */
Rich Salz7e1b7482015-04-24 15:26:15 -0400359 argc = opt_num_rest();
360 argv = opt_rest();
Rich Salz0d1e0032015-12-22 08:22:33 -0500361 for ( ; *argv; argv++) {
362 if (**argv == '-') {
363 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
364 prog);
365 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
366 goto end;
367 }
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100368 sk_OPENSSL_CSTRING_push(engines, *argv);
Rich Salz0d1e0032015-12-22 08:22:33 -0500369 }
Richard Levitte14c6d272000-11-01 02:57:35 +0000370
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100371 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
Matt Caswell0f113f32015-01-22 03:40:55 +0000372 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100373 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
Matt Caswell0f113f32015-01-22 03:40:55 +0000374 }
375 }
Richard Levitte14c6d272000-11-01 02:57:35 +0000376
Richard Levitte9e644572016-07-19 08:57:01 +0200377 ret = 0;
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100378 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
379 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
Matt Caswell0f113f32015-01-22 03:40:55 +0000380 if ((e = ENGINE_by_id(id)) != NULL) {
381 const char *name = ENGINE_get_name(e);
382 /*
383 * Do "id" first, then "name". Easier to auto-parse.
384 */
Rich Salz7e1b7482015-04-24 15:26:15 -0400385 BIO_printf(out, "(%s) %s\n", id, name);
386 util_do_cmds(e, pre_cmds, out, indent);
Matt Caswell0f113f32015-01-22 03:40:55 +0000387 if (strcmp(ENGINE_get_id(e), id) != 0) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400388 BIO_printf(out, "Loaded: (%s) %s\n",
Matt Caswell0f113f32015-01-22 03:40:55 +0000389 ENGINE_get_id(e), ENGINE_get_name(e));
390 }
391 if (list_cap) {
392 int cap_size = 256;
393 char *cap_buf = NULL;
394 int k, n;
395 const int *nids;
396 ENGINE_CIPHERS_PTR fn_c;
397 ENGINE_DIGESTS_PTR fn_d;
398 ENGINE_PKEY_METHS_PTR fn_pk;
Richard Levitte69e78052000-11-02 19:24:48 +0000399
Matt Caswell0f113f32015-01-22 03:40:55 +0000400 if (ENGINE_get_RSA(e) != NULL
Rich Salz0d1e0032015-12-22 08:22:33 -0500401 && !append_buf(&cap_buf, &cap_size, "RSA"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000402 goto end;
403 if (ENGINE_get_DSA(e) != NULL
Rich Salz0d1e0032015-12-22 08:22:33 -0500404 && !append_buf(&cap_buf, &cap_size, "DSA"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000405 goto end;
406 if (ENGINE_get_DH(e) != NULL
Rich Salz0d1e0032015-12-22 08:22:33 -0500407 && !append_buf(&cap_buf, &cap_size, "DH"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000408 goto end;
409 if (ENGINE_get_RAND(e) != NULL
Rich Salz0d1e0032015-12-22 08:22:33 -0500410 && !append_buf(&cap_buf, &cap_size, "RAND"))
Matt Caswell0f113f32015-01-22 03:40:55 +0000411 goto end;
Richard Levitte69e78052000-11-02 19:24:48 +0000412
Matt Caswell0f113f32015-01-22 03:40:55 +0000413 fn_c = ENGINE_get_ciphers(e);
Paul Yang22342122017-06-13 01:24:02 +0800414 if (fn_c == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000415 goto skip_ciphers;
416 n = fn_c(e, NULL, &nids, 0);
417 for (k = 0; k < n; ++k)
Rich Salz0d1e0032015-12-22 08:22:33 -0500418 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
Matt Caswell0f113f32015-01-22 03:40:55 +0000419 goto end;
Ben Laurie354c3ac2001-08-18 10:22:54 +0000420
Matt Caswell0f113f32015-01-22 03:40:55 +0000421 skip_ciphers:
422 fn_d = ENGINE_get_digests(e);
Paul Yang22342122017-06-13 01:24:02 +0800423 if (fn_d == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000424 goto skip_digests;
425 n = fn_d(e, NULL, &nids, 0);
426 for (k = 0; k < n; ++k)
Rich Salz0d1e0032015-12-22 08:22:33 -0500427 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
Matt Caswell0f113f32015-01-22 03:40:55 +0000428 goto end;
Geoff Thorpe4ba163c2001-10-01 15:41:31 +0000429
Matt Caswell0f113f32015-01-22 03:40:55 +0000430 skip_digests:
431 fn_pk = ENGINE_get_pkey_meths(e);
Paul Yang22342122017-06-13 01:24:02 +0800432 if (fn_pk == NULL)
Matt Caswell0f113f32015-01-22 03:40:55 +0000433 goto skip_pmeths;
434 n = fn_pk(e, NULL, &nids, 0);
435 for (k = 0; k < n; ++k)
Rich Salz0d1e0032015-12-22 08:22:33 -0500436 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
Matt Caswell0f113f32015-01-22 03:40:55 +0000437 goto end;
438 skip_pmeths:
Richard Levittefa669492016-12-11 07:06:13 +0100439 {
440 struct util_store_cap_data store_ctx;
441
442 store_ctx.engine = e;
443 store_ctx.cap_buf = &cap_buf;
444 store_ctx.cap_size = &cap_size;
445 store_ctx.ok = 1;
446
447 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
448 if (!store_ctx.ok)
449 goto end;
450 }
Paul Yang22342122017-06-13 01:24:02 +0800451 if (cap_buf != NULL && (*cap_buf != '\0'))
Rich Salz7e1b7482015-04-24 15:26:15 -0400452 BIO_printf(out, " [%s]\n", cap_buf);
Richard Levitte69e78052000-11-02 19:24:48 +0000453
Matt Caswell0f113f32015-01-22 03:40:55 +0000454 OPENSSL_free(cap_buf);
455 }
456 if (test_avail) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400457 BIO_printf(out, "%s", indent);
Matt Caswell0f113f32015-01-22 03:40:55 +0000458 if (ENGINE_init(e)) {
Rich Salz7e1b7482015-04-24 15:26:15 -0400459 BIO_printf(out, "[ available ]\n");
460 util_do_cmds(e, post_cmds, out, indent);
Matt Caswell0f113f32015-01-22 03:40:55 +0000461 ENGINE_finish(e);
462 } else {
Rich Salz7e1b7482015-04-24 15:26:15 -0400463 BIO_printf(out, "[ unavailable ]\n");
Matt Caswell0f113f32015-01-22 03:40:55 +0000464 if (test_avail_noise)
465 ERR_print_errors_fp(stdout);
466 ERR_clear_error();
467 }
468 }
Rich Salz7e1b7482015-04-24 15:26:15 -0400469 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
Matt Caswell0f113f32015-01-22 03:40:55 +0000470 goto end;
471 ENGINE_free(e);
Richard Levitte9e644572016-07-19 08:57:01 +0200472 } else {
Matt Caswell0f113f32015-01-22 03:40:55 +0000473 ERR_print_errors(bio_err);
Richard Levitte9e644572016-07-19 08:57:01 +0200474 /* because exit codes above 127 have special meaning on Unix */
475 if (++ret > 127)
476 ret = 127;
477 }
Matt Caswell0f113f32015-01-22 03:40:55 +0000478 }
Richard Levitte14c6d272000-11-01 02:57:35 +0000479
Matt Caswell0f113f32015-01-22 03:40:55 +0000480 end:
Dr. Stephen Henson767712f2003-03-12 02:38:57 +0000481
Matt Caswell0f113f32015-01-22 03:40:55 +0000482 ERR_print_errors(bio_err);
Matt Caswell1dcb8ca2016-06-15 16:25:21 +0100483 sk_OPENSSL_CSTRING_free(engines);
484 sk_OPENSSL_STRING_free(pre_cmds);
485 sk_OPENSSL_STRING_free(post_cmds);
Rich Salz7e1b7482015-04-24 15:26:15 -0400486 BIO_free_all(out);
KaoruToda26a7d932017-10-17 23:04:09 +0900487 return ret;
Matt Caswell0f113f32015-01-22 03:40:55 +0000488}
Richard Levitte0b13e9f2003-01-30 17:39:26 +0000489#endif