]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/engine.c
Copyright year updates
[thirdparty/openssl.git] / apps / engine.c
CommitLineData
0f113f3e 1/*
b6461792 2 * Copyright 2000-2024 The OpenSSL Project Authors. All Rights Reserved.
14c6d27d 3 *
dffa7520 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
846e33c7
RS
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
14c6d27d
RL
8 */
9
ad8fc6f6
P
10/* We need to use some engine deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
effaf4de 13#include <openssl/opensslconf.h>
1ae56f2f
RS
14
15#include "apps.h"
16#include "progs.h"
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <openssl/err.h>
21#include <openssl/engine.h>
22#include <openssl/ssl.h>
23#include <openssl/store.h>
0f113f3e 24
7e1b7485 25typedef enum OPTION_choice {
b0f96018 26 OPT_COMMON,
7e1b7485
RS
27 OPT_C, OPT_T, OPT_TT, OPT_PRE, OPT_POST,
28 OPT_V = 100, OPT_VV, OPT_VVV, OPT_VVVV
29} OPTION_CHOICE;
30
44c83ebd 31const OPTIONS engine_options[] = {
0d1e003f 32 {OPT_HELP_STR, 1, '-', "Usage: %s [options] engine...\n"},
5388f986
RS
33
34 OPT_SECTION("General"),
7e1b7485 35 {"help", OPT_HELP, '-', "Display this summary"},
5388f986
RS
36 {"t", OPT_T, '-', "Check that specified engine is available"},
37 {"pre", OPT_PRE, 's', "Run command against the ENGINE before loading it"},
38 {"post", OPT_POST, 's', "Run command against the ENGINE after loading it"},
39
40 OPT_SECTION("Output"),
0d1e003f 41 {"v", OPT_V, '-', "List 'control commands' For each specified engine"},
7e1b7485 42 {"vv", OPT_VV, '-', "Also display each command's description"},
0d1e003f
RS
43 {"vvv", OPT_VVV, '-', "Also add the input flags for each command"},
44 {"vvvv", OPT_VVVV, '-', "Also show internal input flags"},
45 {"c", OPT_C, '-', "List the capabilities of specified engine"},
7e1b7485 46 {"tt", OPT_TT, '-', "Display error trace for unavailable engines"},
7e1b7485
RS
47 {OPT_MORE_STR, OPT_EOF, 1,
48 "Commands are like \"SO_PATH:/lib/libdriver.so\""},
92de469f
RS
49
50 OPT_PARAMETERS(),
51 {"engine", 0, 0, "ID of engine(s) to load"},
7e1b7485 52 {NULL}
14c6d27d
RL
53};
54
0d1e003f 55static int append_buf(char **buf, int *size, const char *s)
0f113f3e 56{
b2ac85ad
P
57 const int expand = 256;
58 int len = strlen(s) + 1;
59 char *p = *buf;
60
61 if (p == NULL) {
62 *size = ((len + expand - 1) / expand) * expand;
63 p = *buf = app_malloc(*size, "engine buffer");
64 } else {
65 const int blen = strlen(p);
66
67 if (blen > 0)
68 len += 2 + blen;
69e7805f 69
b2ac85ad
P
70 if (len > *size) {
71 *size = ((len + expand - 1) / expand) * expand;
72 p = OPENSSL_realloc(p, *size);
73 if (p == NULL) {
74 OPENSSL_free(*buf);
75 *buf = NULL;
76 return 0;
77 }
78 *buf = p;
7c0ef843 79 }
69e7805f 80
b2ac85ad
P
81 if (blen > 0) {
82 p += blen;
83 *p++ = ',';
84 *p++ = ' ';
85 }
86 }
69e7805f 87
b2ac85ad 88 strcpy(p, s);
0f113f3e
MC
89 return 1;
90}
69e7805f 91
7e1b7485 92static int util_flags(BIO *out, unsigned int flags, const char *indent)
0f113f3e
MC
93{
94 int started = 0, err = 0;
95 /* Indent before displaying input flags */
7e1b7485 96 BIO_printf(out, "%s%s(input flags): ", indent, indent);
0f113f3e 97 if (flags == 0) {
7e1b7485 98 BIO_printf(out, "<no flags>\n");
0f113f3e
MC
99 return 1;
100 }
101 /*
102 * If the object is internal, mark it in a way that shows instead of
103 * having it part of all the other flags, even if it really is.
104 */
105 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
7e1b7485 106 BIO_printf(out, "[Internal] ");
0f113f3e
MC
107 }
108
109 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
7e1b7485 110 BIO_printf(out, "NUMERIC");
0f113f3e
MC
111 started = 1;
112 }
113 /*
114 * Now we check that no combinations of the mutually exclusive NUMERIC,
115 * STRING, and NO_INPUT flags have been used. Future flags that can be
116 * OR'd together with these would need to added after these to preserve
117 * the testing logic.
118 */
119 if (flags & ENGINE_CMD_FLAG_STRING) {
120 if (started) {
7e1b7485 121 BIO_printf(out, "|");
0f113f3e
MC
122 err = 1;
123 }
7e1b7485 124 BIO_printf(out, "STRING");
0f113f3e
MC
125 started = 1;
126 }
127 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
128 if (started) {
7e1b7485 129 BIO_printf(out, "|");
0f113f3e
MC
130 err = 1;
131 }
7e1b7485 132 BIO_printf(out, "NO_INPUT");
0f113f3e
MC
133 started = 1;
134 }
135 /* Check for unknown flags */
136 flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
137 ~ENGINE_CMD_FLAG_STRING &
138 ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
139 if (flags) {
140 if (started)
7e1b7485
RS
141 BIO_printf(out, "|");
142 BIO_printf(out, "<0x%04X>", flags);
0f113f3e
MC
143 }
144 if (err)
7e1b7485
RS
145 BIO_printf(out, " <illegal flags!>");
146 BIO_printf(out, "\n");
0f113f3e
MC
147 return 1;
148}
149
7e1b7485 150static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
0f113f3e
MC
151{
152 static const int line_wrap = 78;
153 int num;
154 int ret = 0;
155 char *name = NULL;
156 char *desc = NULL;
157 int flags;
158 int xpos = 0;
159 STACK_OF(OPENSSL_STRING) *cmds = NULL;
160 if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
161 ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
162 0, NULL, NULL)) <= 0)) {
0f113f3e
MC
163 return 1;
164 }
165
166 cmds = sk_OPENSSL_STRING_new_null();
2234212c 167 if (cmds == NULL)
0f113f3e 168 goto err;
7e1b7485 169
0f113f3e
MC
170 do {
171 int len;
172 /* Get the command input flags */
173 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
174 NULL, NULL)) < 0)
175 goto err;
176 if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
177 /* Get the command name */
178 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
179 NULL, NULL)) <= 0)
180 goto err;
68dc6824 181 name = app_malloc(len + 1, "name buffer");
0f113f3e
MC
182 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
183 NULL) <= 0)
184 goto err;
185 /* Get the command description */
186 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
187 NULL, NULL)) < 0)
188 goto err;
189 if (len > 0) {
68dc6824 190 desc = app_malloc(len + 1, "description buffer");
0f113f3e 191 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
2cd3ad9b 192 NULL) <= 0)
0f113f3e
MC
193 goto err;
194 }
195 /* Now decide on the output */
196 if (xpos == 0)
197 /* Do an indent */
7e1b7485 198 xpos = BIO_puts(out, indent);
0f113f3e
MC
199 else
200 /* Otherwise prepend a ", " */
7e1b7485 201 xpos += BIO_printf(out, ", ");
0f113f3e
MC
202 if (verbose == 1) {
203 /*
204 * We're just listing names, comma-delimited
205 */
206 if ((xpos > (int)strlen(indent)) &&
207 (xpos + (int)strlen(name) > line_wrap)) {
7e1b7485
RS
208 BIO_printf(out, "\n");
209 xpos = BIO_puts(out, indent);
0f113f3e 210 }
7e1b7485 211 xpos += BIO_printf(out, "%s", name);
0f113f3e
MC
212 } else {
213 /* We're listing names plus descriptions */
7e1b7485 214 BIO_printf(out, "%s: %s\n", name,
0f113f3e
MC
215 (desc == NULL) ? "<no description>" : desc);
216 /* ... and sometimes input flags */
7e1b7485 217 if ((verbose >= 3) && !util_flags(out, flags, indent))
0f113f3e
MC
218 goto err;
219 xpos = 0;
220 }
221 }
222 OPENSSL_free(name);
223 name = NULL;
b548a1f1
RS
224 OPENSSL_free(desc);
225 desc = NULL;
0f113f3e
MC
226 /* Move to the next command */
227 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
228 } while (num > 0);
229 if (xpos > 0)
7e1b7485 230 BIO_printf(out, "\n");
0f113f3e
MC
231 ret = 1;
232 err:
1dcb8ca2 233 sk_OPENSSL_STRING_free(cmds);
b548a1f1
RS
234 OPENSSL_free(name);
235 OPENSSL_free(desc);
0f113f3e
MC
236 return ret;
237}
f11bc840 238
c869da88 239static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
7e1b7485 240 BIO *out, const char *indent)
0f113f3e
MC
241{
242 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
243
244 if (num < 0) {
7e1b7485 245 BIO_printf(out, "[Error]: internal stack error\n");
0f113f3e
MC
246 return;
247 }
248 for (loop = 0; loop < num; loop++) {
249 char buf[256];
250 const char *cmd, *arg;
251 cmd = sk_OPENSSL_STRING_value(cmds, loop);
252 res = 1; /* assume success */
253 /* Check if this command has no ":arg" */
0f644b96 254 if ((arg = strchr(cmd, ':')) == NULL) {
0f113f3e
MC
255 if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
256 res = 0;
257 } else {
258 if ((int)(arg - cmd) > 254) {
7e1b7485 259 BIO_printf(out, "[Error]: command name too long\n");
0f113f3e
MC
260 return;
261 }
262 memcpy(buf, cmd, (int)(arg - cmd));
263 buf[arg - cmd] = '\0';
264 arg++; /* Move past the ":" */
265 /* Call the command with the argument */
266 if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
267 res = 0;
268 }
2234212c 269 if (res) {
7e1b7485 270 BIO_printf(out, "[Success]: %s\n", cmd);
2234212c 271 } else {
7e1b7485
RS
272 BIO_printf(out, "[Failure]: %s\n", cmd);
273 ERR_print_errors(out);
0f113f3e
MC
274 }
275 }
276}
f11bc840 277
fa66949c
RL
278struct util_store_cap_data {
279 ENGINE *engine;
280 char **cap_buf;
281 int *cap_size;
282 int ok;
283};
284static void util_store_cap(const OSSL_STORE_LOADER *loader, void *arg)
285{
286 struct util_store_cap_data *ctx = arg;
287
288 if (OSSL_STORE_LOADER_get0_engine(loader) == ctx->engine) {
289 char buf[256];
290 BIO_snprintf(buf, sizeof(buf), "STORE(%s)",
291 OSSL_STORE_LOADER_get0_scheme(loader));
292 if (!append_buf(ctx->cap_buf, ctx->cap_size, buf))
293 ctx->ok = 0;
294 }
295}
296
7e1b7485 297int engine_main(int argc, char **argv)
0f113f3e
MC
298{
299 int ret = 1, i;
0f113f3e
MC
300 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
301 ENGINE *e;
1dcb8ca2 302 STACK_OF(OPENSSL_CSTRING) *engines = sk_OPENSSL_CSTRING_new_null();
0f113f3e
MC
303 STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
304 STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
7e1b7485 305 BIO *out;
0f113f3e 306 const char *indent = " ";
7e1b7485
RS
307 OPTION_CHOICE o;
308 char *prog;
0d1e003f 309 char *argv1;
0f113f3e 310
a60994df 311 out = dup_bio_out(FORMAT_TEXT);
0d1e003f 312 if (engines == NULL || pre_cmds == NULL || post_cmds == NULL)
0f113f3e 313 goto end;
0d1e003f
RS
314
315 /* Remember the original command name, parse/skip any leading engine
316 * names, and then setup to parse the rest of the line as flags. */
317 prog = argv[0];
318 while ((argv1 = argv[1]) != NULL && *argv1 != '-') {
1dcb8ca2 319 sk_OPENSSL_CSTRING_push(engines, argv1);
0d1e003f
RS
320 argc--;
321 argv++;
322 }
323 argv[0] = prog;
324 opt_init(argc, argv, engine_options);
325
7e1b7485
RS
326 while ((o = opt_next()) != OPT_EOF) {
327 switch (o) {
328 case OPT_EOF:
329 case OPT_ERR:
330 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
331 goto end;
332 case OPT_HELP:
333 opt_help(engine_options);
334 ret = 0;
335 goto end;
336 case OPT_VVVV:
337 case OPT_VVV:
338 case OPT_VV:
339 case OPT_V:
340 /* Convert to an integer from one to four. */
341 i = (int)(o - OPT_V) + 1;
342 if (verbose < i)
343 verbose = i;
344 break;
345 case OPT_C:
0f113f3e 346 list_cap = 1;
7e1b7485
RS
347 break;
348 case OPT_TT:
349 test_avail_noise++;
9929c817 350 /* fall through */
7e1b7485
RS
351 case OPT_T:
352 test_avail++;
353 break;
354 case OPT_PRE:
355 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
356 break;
357 case OPT_POST:
358 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
359 break;
360 }
0f113f3e 361 }
0d1e003f 362
021410ea 363 /* Any remaining arguments are engine names. */
7e1b7485
RS
364 argc = opt_num_rest();
365 argv = opt_rest();
0d1e003f
RS
366 for ( ; *argv; argv++) {
367 if (**argv == '-') {
368 BIO_printf(bio_err, "%s: Cannot mix flags and engine names.\n",
369 prog);
370 BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
371 goto end;
372 }
1dcb8ca2 373 sk_OPENSSL_CSTRING_push(engines, *argv);
0d1e003f 374 }
0f113f3e 375
1dcb8ca2 376 if (sk_OPENSSL_CSTRING_num(engines) == 0) {
0f113f3e 377 for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
1dcb8ca2 378 sk_OPENSSL_CSTRING_push(engines, ENGINE_get_id(e));
0f113f3e
MC
379 }
380 }
381
9e64457d 382 ret = 0;
1dcb8ca2
MC
383 for (i = 0; i < sk_OPENSSL_CSTRING_num(engines); i++) {
384 const char *id = sk_OPENSSL_CSTRING_value(engines, i);
0f113f3e
MC
385 if ((e = ENGINE_by_id(id)) != NULL) {
386 const char *name = ENGINE_get_name(e);
387 /*
388 * Do "id" first, then "name". Easier to auto-parse.
389 */
7e1b7485
RS
390 BIO_printf(out, "(%s) %s\n", id, name);
391 util_do_cmds(e, pre_cmds, out, indent);
0f113f3e 392 if (strcmp(ENGINE_get_id(e), id) != 0) {
7e1b7485 393 BIO_printf(out, "Loaded: (%s) %s\n",
0f113f3e
MC
394 ENGINE_get_id(e), ENGINE_get_name(e));
395 }
396 if (list_cap) {
397 int cap_size = 256;
398 char *cap_buf = NULL;
399 int k, n;
400 const int *nids;
401 ENGINE_CIPHERS_PTR fn_c;
402 ENGINE_DIGESTS_PTR fn_d;
403 ENGINE_PKEY_METHS_PTR fn_pk;
404
405 if (ENGINE_get_RSA(e) != NULL
0d1e003f 406 && !append_buf(&cap_buf, &cap_size, "RSA"))
0f113f3e 407 goto end;
5d70f118
MO
408 if (ENGINE_get_EC(e) != NULL
409 && !append_buf(&cap_buf, &cap_size, "EC"))
410 goto end;
0f113f3e 411 if (ENGINE_get_DSA(e) != NULL
0d1e003f 412 && !append_buf(&cap_buf, &cap_size, "DSA"))
0f113f3e
MC
413 goto end;
414 if (ENGINE_get_DH(e) != NULL
0d1e003f 415 && !append_buf(&cap_buf, &cap_size, "DH"))
0f113f3e
MC
416 goto end;
417 if (ENGINE_get_RAND(e) != NULL
0d1e003f 418 && !append_buf(&cap_buf, &cap_size, "RAND"))
0f113f3e
MC
419 goto end;
420
421 fn_c = ENGINE_get_ciphers(e);
2234212c 422 if (fn_c == NULL)
0f113f3e
MC
423 goto skip_ciphers;
424 n = fn_c(e, NULL, &nids, 0);
425 for (k = 0; k < n; ++k)
0d1e003f 426 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
0f113f3e
MC
427 goto end;
428
429 skip_ciphers:
430 fn_d = ENGINE_get_digests(e);
2234212c 431 if (fn_d == NULL)
0f113f3e
MC
432 goto skip_digests;
433 n = fn_d(e, NULL, &nids, 0);
434 for (k = 0; k < n; ++k)
0d1e003f 435 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
0f113f3e
MC
436 goto end;
437
438 skip_digests:
439 fn_pk = ENGINE_get_pkey_meths(e);
2234212c 440 if (fn_pk == NULL)
0f113f3e
MC
441 goto skip_pmeths;
442 n = fn_pk(e, NULL, &nids, 0);
443 for (k = 0; k < n; ++k)
0d1e003f 444 if (!append_buf(&cap_buf, &cap_size, OBJ_nid2sn(nids[k])))
0f113f3e
MC
445 goto end;
446 skip_pmeths:
fa66949c
RL
447 {
448 struct util_store_cap_data store_ctx;
449
450 store_ctx.engine = e;
451 store_ctx.cap_buf = &cap_buf;
452 store_ctx.cap_size = &cap_size;
453 store_ctx.ok = 1;
454
455 OSSL_STORE_do_all_loaders(util_store_cap, &store_ctx);
456 if (!store_ctx.ok)
457 goto end;
458 }
2234212c 459 if (cap_buf != NULL && (*cap_buf != '\0'))
7e1b7485 460 BIO_printf(out, " [%s]\n", cap_buf);
0f113f3e
MC
461
462 OPENSSL_free(cap_buf);
463 }
464 if (test_avail) {
7e1b7485 465 BIO_printf(out, "%s", indent);
0f113f3e 466 if (ENGINE_init(e)) {
7e1b7485
RS
467 BIO_printf(out, "[ available ]\n");
468 util_do_cmds(e, post_cmds, out, indent);
0f113f3e
MC
469 ENGINE_finish(e);
470 } else {
7e1b7485 471 BIO_printf(out, "[ unavailable ]\n");
0f113f3e
MC
472 if (test_avail_noise)
473 ERR_print_errors_fp(stdout);
474 ERR_clear_error();
475 }
476 }
7e1b7485 477 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
0f113f3e
MC
478 goto end;
479 ENGINE_free(e);
9e64457d 480 } else {
0f113f3e 481 ERR_print_errors(bio_err);
9e64457d
RL
482 /* because exit codes above 127 have special meaning on Unix */
483 if (++ret > 127)
484 ret = 127;
485 }
0f113f3e
MC
486 }
487
0f113f3e
MC
488 end:
489
490 ERR_print_errors(bio_err);
1dcb8ca2
MC
491 sk_OPENSSL_CSTRING_free(engines);
492 sk_OPENSSL_STRING_free(pre_cmds);
493 sk_OPENSSL_STRING_free(post_cmds);
7e1b7485 494 BIO_free_all(out);
26a7d938 495 return ret;
0f113f3e 496}