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