]> git.ipfire.org Git - thirdparty/openssl.git/blame - apps/engine.c
free NULL cleanup -- coda
[thirdparty/openssl.git] / apps / engine.c
CommitLineData
0f113f3e
MC
1/*
2 * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
3 * 2000.
14c6d27d
RL
4 */
5/* ====================================================================
6 * Copyright (c) 2000 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
14c6d27d
RL
14 *
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
18 * distribution.
19 *
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/)"
24 *
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.
29 *
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.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
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 * ====================================================================
52 *
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).
56 *
57 */
58
59#include <stdio.h>
60#include <stdlib.h>
61#include <string.h>
14c6d27d
RL
62#include "apps.h"
63#include <openssl/err.h>
70531c14 64#ifndef OPENSSL_NO_ENGINE
0f113f3e
MC
65# include <openssl/engine.h>
66# include <openssl/ssl.h>
67
7e1b7485
RS
68typedef 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
72} OPTION_CHOICE;
73
74OPTIONS 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\""},
87 {NULL}
14c6d27d
RL
88};
89
5ce278a7 90static void identity(char *ptr)
0f113f3e
MC
91{
92 return;
93}
14c6d27d 94
354c3ace 95static int append_buf(char **buf, const char *s, int *size, int step)
0f113f3e
MC
96{
97 int l = strlen(s);
69e7805f 98
0f113f3e
MC
99 if (*buf == NULL) {
100 *size = step;
68dc6824 101 *buf = app_malloc(*size, "engine buffer");
0f113f3e
MC
102 **buf = '\0';
103 }
69e7805f 104
0f113f3e
MC
105 if (**buf != '\0')
106 l += 2; /* ", " */
69e7805f 107
0f113f3e
MC
108 if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
109 *size += step;
110 *buf = OPENSSL_realloc(*buf, *size);
111 }
69e7805f 112
0f113f3e
MC
113 if (*buf == NULL)
114 return 0;
69e7805f 115
0f113f3e
MC
116 if (**buf != '\0')
117 BUF_strlcat(*buf, ", ", *size);
118 BUF_strlcat(*buf, s, *size);
69e7805f 119
0f113f3e
MC
120 return 1;
121}
69e7805f 122
7e1b7485 123static int util_flags(BIO *out, unsigned int flags, const char *indent)
0f113f3e
MC
124{
125 int started = 0, err = 0;
126 /* Indent before displaying input flags */
7e1b7485 127 BIO_printf(out, "%s%s(input flags): ", indent, indent);
0f113f3e 128 if (flags == 0) {
7e1b7485 129 BIO_printf(out, "<no flags>\n");
0f113f3e
MC
130 return 1;
131 }
132 /*
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.
135 */
136 if (flags & ENGINE_CMD_FLAG_INTERNAL) {
7e1b7485 137 BIO_printf(out, "[Internal] ");
0f113f3e
MC
138 }
139
140 if (flags & ENGINE_CMD_FLAG_NUMERIC) {
7e1b7485 141 BIO_printf(out, "NUMERIC");
0f113f3e
MC
142 started = 1;
143 }
144 /*
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
148 * the testing logic.
149 */
150 if (flags & ENGINE_CMD_FLAG_STRING) {
151 if (started) {
7e1b7485 152 BIO_printf(out, "|");
0f113f3e
MC
153 err = 1;
154 }
7e1b7485 155 BIO_printf(out, "STRING");
0f113f3e
MC
156 started = 1;
157 }
158 if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
159 if (started) {
7e1b7485 160 BIO_printf(out, "|");
0f113f3e
MC
161 err = 1;
162 }
7e1b7485 163 BIO_printf(out, "NO_INPUT");
0f113f3e
MC
164 started = 1;
165 }
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;
170 if (flags) {
171 if (started)
7e1b7485
RS
172 BIO_printf(out, "|");
173 BIO_printf(out, "<0x%04X>", flags);
0f113f3e
MC
174 }
175 if (err)
7e1b7485
RS
176 BIO_printf(out, " <illegal flags!>");
177 BIO_printf(out, "\n");
0f113f3e
MC
178 return 1;
179}
180
7e1b7485 181static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
0f113f3e
MC
182{
183 static const int line_wrap = 78;
184 int num;
185 int ret = 0;
186 char *name = NULL;
187 char *desc = NULL;
188 int flags;
189 int xpos = 0;
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)) {
0f113f3e
MC
194 return 1;
195 }
196
197 cmds = sk_OPENSSL_STRING_new_null();
0f113f3e
MC
198 if (!cmds)
199 goto err;
7e1b7485 200
0f113f3e
MC
201 do {
202 int len;
203 /* Get the command input flags */
204 if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
205 NULL, NULL)) < 0)
206 goto err;
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,
210 NULL, NULL)) <= 0)
211 goto err;
68dc6824 212 name = app_malloc(len + 1, "name buffer");
0f113f3e
MC
213 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
214 NULL) <= 0)
215 goto err;
216 /* Get the command description */
217 if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
218 NULL, NULL)) < 0)
219 goto err;
220 if (len > 0) {
68dc6824 221 desc = app_malloc(len + 1, "description buffer");
0f113f3e 222 if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
2cd3ad9b 223 NULL) <= 0)
0f113f3e
MC
224 goto err;
225 }
226 /* Now decide on the output */
227 if (xpos == 0)
228 /* Do an indent */
7e1b7485 229 xpos = BIO_puts(out, indent);
0f113f3e
MC
230 else
231 /* Otherwise prepend a ", " */
7e1b7485 232 xpos += BIO_printf(out, ", ");
0f113f3e
MC
233 if (verbose == 1) {
234 /*
235 * We're just listing names, comma-delimited
236 */
237 if ((xpos > (int)strlen(indent)) &&
238 (xpos + (int)strlen(name) > line_wrap)) {
7e1b7485
RS
239 BIO_printf(out, "\n");
240 xpos = BIO_puts(out, indent);
0f113f3e 241 }
7e1b7485 242 xpos += BIO_printf(out, "%s", name);
0f113f3e
MC
243 } else {
244 /* We're listing names plus descriptions */
7e1b7485 245 BIO_printf(out, "%s: %s\n", name,
0f113f3e
MC
246 (desc == NULL) ? "<no description>" : desc);
247 /* ... and sometimes input flags */
7e1b7485 248 if ((verbose >= 3) && !util_flags(out, flags, indent))
0f113f3e
MC
249 goto err;
250 xpos = 0;
251 }
252 }
253 OPENSSL_free(name);
254 name = NULL;
b548a1f1
RS
255 OPENSSL_free(desc);
256 desc = NULL;
0f113f3e
MC
257 /* Move to the next command */
258 num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
259 } while (num > 0);
260 if (xpos > 0)
7e1b7485 261 BIO_printf(out, "\n");
0f113f3e
MC
262 ret = 1;
263 err:
25aaa98a 264 sk_OPENSSL_STRING_pop_free(cmds, identity);
b548a1f1
RS
265 OPENSSL_free(name);
266 OPENSSL_free(desc);
0f113f3e
MC
267 return ret;
268}
f11bc840 269
c869da88 270static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
7e1b7485 271 BIO *out, const char *indent)
0f113f3e
MC
272{
273 int loop, res, num = sk_OPENSSL_STRING_num(cmds);
274
275 if (num < 0) {
7e1b7485 276 BIO_printf(out, "[Error]: internal stack error\n");
0f113f3e
MC
277 return;
278 }
279 for (loop = 0; loop < num; loop++) {
280 char buf[256];
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))
287 res = 0;
288 } else {
289 if ((int)(arg - cmd) > 254) {
7e1b7485 290 BIO_printf(out, "[Error]: command name too long\n");
0f113f3e
MC
291 return;
292 }
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))
298 res = 0;
299 }
300 if (res)
7e1b7485 301 BIO_printf(out, "[Success]: %s\n", cmd);
0f113f3e 302 else {
7e1b7485
RS
303 BIO_printf(out, "[Failure]: %s\n", cmd);
304 ERR_print_errors(out);
0f113f3e
MC
305 }
306 }
307}
f11bc840 308
7e1b7485 309int engine_main(int argc, char **argv)
0f113f3e
MC
310{
311 int ret = 1, i;
0f113f3e
MC
312 int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
313 ENGINE *e;
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();
7e1b7485 317 BIO *out;
0f113f3e 318 const char *indent = " ";
7e1b7485
RS
319 OPTION_CHOICE o;
320 char *prog;
0f113f3e 321
7e1b7485
RS
322 out = dup_bio_out();
323 prog = opt_init(argc, argv, engine_options);
324 if (!engines || !pre_cmds || !post_cmds)
0f113f3e 325 goto end;
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++;
350 case OPT_T:
351 test_avail++;
352 break;
353 case OPT_PRE:
354 sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
355 break;
356 case OPT_POST:
357 sk_OPENSSL_STRING_push(post_cmds, opt_arg());
358 break;
359 }
0f113f3e 360 }
7e1b7485
RS
361 argc = opt_num_rest();
362 argv = opt_rest();
363 for ( ; *argv; argv++)
364 sk_OPENSSL_STRING_push(engines, *argv);
0f113f3e
MC
365
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));
369 }
370 }
371
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);
376 /*
377 * Do "id" first, then "name". Easier to auto-parse.
378 */
7e1b7485
RS
379 BIO_printf(out, "(%s) %s\n", id, name);
380 util_do_cmds(e, pre_cmds, out, indent);
0f113f3e 381 if (strcmp(ENGINE_get_id(e), id) != 0) {
7e1b7485 382 BIO_printf(out, "Loaded: (%s) %s\n",
0f113f3e
MC
383 ENGINE_get_id(e), ENGINE_get_name(e));
384 }
385 if (list_cap) {
386 int cap_size = 256;
387 char *cap_buf = NULL;
388 int k, n;
389 const int *nids;
390 ENGINE_CIPHERS_PTR fn_c;
391 ENGINE_DIGESTS_PTR fn_d;
392 ENGINE_PKEY_METHS_PTR fn_pk;
393
394 if (ENGINE_get_RSA(e) != NULL
395 && !append_buf(&cap_buf, "RSA", &cap_size, 256))
396 goto end;
397 if (ENGINE_get_DSA(e) != NULL
398 && !append_buf(&cap_buf, "DSA", &cap_size, 256))
399 goto end;
400 if (ENGINE_get_DH(e) != NULL
401 && !append_buf(&cap_buf, "DH", &cap_size, 256))
402 goto end;
403 if (ENGINE_get_RAND(e) != NULL
404 && !append_buf(&cap_buf, "RAND", &cap_size, 256))
405 goto end;
406
407 fn_c = ENGINE_get_ciphers(e);
408 if (!fn_c)
409 goto skip_ciphers;
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))
414 goto end;
415
416 skip_ciphers:
417 fn_d = ENGINE_get_digests(e);
418 if (!fn_d)
419 goto skip_digests;
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))
424 goto end;
425
426 skip_digests:
427 fn_pk = ENGINE_get_pkey_meths(e);
428 if (!fn_pk)
429 goto skip_pmeths;
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))
434 goto end;
435 skip_pmeths:
436 if (cap_buf && (*cap_buf != '\0'))
7e1b7485 437 BIO_printf(out, " [%s]\n", cap_buf);
0f113f3e
MC
438
439 OPENSSL_free(cap_buf);
440 }
441 if (test_avail) {
7e1b7485 442 BIO_printf(out, "%s", indent);
0f113f3e 443 if (ENGINE_init(e)) {
7e1b7485
RS
444 BIO_printf(out, "[ available ]\n");
445 util_do_cmds(e, post_cmds, out, indent);
0f113f3e
MC
446 ENGINE_finish(e);
447 } else {
7e1b7485 448 BIO_printf(out, "[ unavailable ]\n");
0f113f3e
MC
449 if (test_avail_noise)
450 ERR_print_errors_fp(stdout);
451 ERR_clear_error();
452 }
453 }
7e1b7485 454 if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
0f113f3e
MC
455 goto end;
456 ENGINE_free(e);
457 } else
458 ERR_print_errors(bio_err);
459 }
460
461 ret = 0;
462 end:
463
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);
7e1b7485
RS
468 BIO_free_all(out);
469 return (ret);
0f113f3e 470}
12d4e7b8
DSH
471#else
472
473# if PEDANTIC
0f113f3e 474static void *dummy = &dummy;
12d4e7b8
DSH
475# endif
476
0b13e9f0 477#endif