Annotation of src/usr.bin/openssl/engine.c, Revision 1.3
1.3 ! doug 1: /* $OpenBSD: engine.c,v 1.2 2014/12/07 15:08:32 jsing Exp $ */
1.1 jsing 2: /* Written by Richard Levitte <richard@levitte.org> for the OpenSSL
3: * project 2000.
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
13: * notice, this list of conditions and the following disclaimer.
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>
62:
63: #include "apps.h"
64:
65: #ifndef OPENSSL_NO_ENGINE
66: #include <openssl/engine.h>
67: #include <openssl/err.h>
68: #include <openssl/ssl.h>
69:
70: static const char *engine_usage[] = {
71: "usage: engine opts [engine ...]\n",
72: " -v[v[v[v]]] - verbose mode, for each engine, list its 'control commands'\n",
73: " -vv will additionally display each command's description\n",
74: " -vvv will also add the input flags for each command\n",
75: " -vvvv will also show internal input flags\n",
76: " -c - for each engine, also list the capabilities\n",
77: " -t[t] - for each engine, check that they are really available\n",
78: " -tt will display error trace for unavailable engines\n",
79: " -pre <cmd> - runs command 'cmd' against the ENGINE before any attempts\n",
80: " to load it (if -t is used)\n",
81: " -post <cmd> - runs command 'cmd' against the ENGINE after loading it\n",
82: " (only used if -t is also provided)\n",
83: " NB: -pre and -post will be applied to all ENGINEs supplied on the command\n",
84: " line, or all supported ENGINEs if none are specified.\n",
85: " Eg. '-pre \"SO_PATH:/lib/libdriver.so\"' calls command \"SO_PATH\" with\n",
86: " argument \"/lib/libdriver.so\".\n",
87: NULL
88: };
89:
90: static void
91: identity(char *ptr)
92: {
93: return;
94: }
95:
96: static int
97: append_buf(char **buf, const char *s, int *size, int step)
98: {
99: if (*buf == NULL) {
100: *size = step;
101: *buf = malloc(*size);
102: if (*buf == NULL)
103: return 0;
104: **buf = '\0';
105: }
106:
107: if (strlen(*buf) + strlen(s) >= (unsigned int) *size) {
108: *size += step;
109: *buf = realloc(*buf, *size);
110: }
111: if (*buf == NULL)
112: return 0;
113:
114: if (**buf != '\0')
115: strlcat(*buf, ", ", *size);
116: strlcat(*buf, s, *size);
117:
118: return 1;
119: }
120:
121: static int
122: util_flags(BIO * bio_out, unsigned int flags, const char *indent)
123: {
124: int started = 0, err = 0;
125: /* Indent before displaying input flags */
126: BIO_printf(bio_out, "%s%s(input flags): ", indent, indent);
127: if (flags == 0) {
128: BIO_printf(bio_out, "<no flags>\n");
129: return 1;
130: }
131: /*
132: * If the object is internal, mark it in a way that shows instead of
133: * having it part of all the other flags, even if it really is.
134: */
135: if (flags & ENGINE_CMD_FLAG_INTERNAL) {
136: BIO_printf(bio_out, "[Internal] ");
137: }
138: if (flags & ENGINE_CMD_FLAG_NUMERIC) {
139: BIO_printf(bio_out, "NUMERIC");
140: started = 1;
141: }
142: /*
143: * Now we check that no combinations of the mutually exclusive
144: * NUMERIC, STRING, and NO_INPUT flags have been used. Future flags
145: * that can be OR'd together with these would need to added after
146: * these to preserve the testing logic.
147: */
148: if (flags & ENGINE_CMD_FLAG_STRING) {
149: if (started) {
150: BIO_printf(bio_out, "|");
151: err = 1;
152: }
153: BIO_printf(bio_out, "STRING");
154: started = 1;
155: }
156: if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
157: if (started) {
158: BIO_printf(bio_out, "|");
159: err = 1;
160: }
161: BIO_printf(bio_out, "NO_INPUT");
162: started = 1;
163: }
164: /* Check for unknown flags */
165: flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
166: ~ENGINE_CMD_FLAG_STRING &
167: ~ENGINE_CMD_FLAG_NO_INPUT &
168: ~ENGINE_CMD_FLAG_INTERNAL;
169: if (flags) {
170: if (started)
171: BIO_printf(bio_out, "|");
172: BIO_printf(bio_out, "<0x%04X>", flags);
173: }
174: if (err)
175: BIO_printf(bio_out, " <illegal flags!>");
176: BIO_printf(bio_out, "\n");
177: return 1;
178: }
179:
180: static int
181: util_verbose(ENGINE * e, int verbose, BIO * bio_out, const char *indent)
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)) {
194: return 1;
195: }
196: cmds = sk_OPENSSL_STRING_new_null();
197:
198: if (!cmds)
199: goto err;
200: do {
201: int len;
202: /* Get the command input flags */
203: if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
204: NULL, NULL)) < 0)
205: goto err;
206: if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
207: /* Get the command name */
208: if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
209: NULL, NULL)) <= 0)
210: goto err;
211: if ((name = malloc(len + 1)) == NULL)
212: goto err;
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) {
221: if ((desc = malloc(len + 1)) == NULL)
222: goto err;
223: if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
224: NULL) <= 0)
225: goto err;
226: }
227: /* Now decide on the output */
228: if (xpos == 0)
229: /* Do an indent */
230: xpos = BIO_puts(bio_out, indent);
231: else
232: /* Otherwise prepend a ", " */
233: xpos += BIO_printf(bio_out, ", ");
234: if (verbose == 1) {
235: /* We're just listing names, comma-delimited */
236: if ((xpos > (int) strlen(indent)) &&
237: (xpos + (int) strlen(name) > line_wrap)) {
238: BIO_printf(bio_out, "\n");
239: xpos = BIO_puts(bio_out, indent);
240: }
241: xpos += BIO_printf(bio_out, "%s", name);
242: } else {
243: /* We're listing names plus descriptions */
244: BIO_printf(bio_out, "%s: %s\n", name,
245: (desc == NULL) ? "<no description>" : desc);
246: /* ... and sometimes input flags */
247: if ((verbose >= 3) && !util_flags(bio_out, flags,
248: indent))
249: goto err;
250: xpos = 0;
251: }
252: }
253: free(name);
254: name = NULL;
255: free(desc);
256: desc = NULL;
257:
258: /* Move to the next command */
259: num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE,
260: num, NULL, NULL);
261: } while (num > 0);
262: if (xpos > 0)
263: BIO_printf(bio_out, "\n");
264: ret = 1;
265: err:
266: if (cmds)
267: sk_OPENSSL_STRING_pop_free(cmds, identity);
268: free(name);
269: free(desc);
270: return ret;
271: }
272:
273: static void
274: util_do_cmds(ENGINE * e, STACK_OF(OPENSSL_STRING) * cmds,
275: BIO * bio_out, const char *indent)
276: {
277: int loop, res, num = sk_OPENSSL_STRING_num(cmds);
278:
279: if (num < 0) {
280: BIO_printf(bio_out, "[Error]: internal stack error\n");
281: return;
282: }
283: for (loop = 0; loop < num; loop++) {
284: char buf[256];
285: const char *cmd, *arg;
286: cmd = sk_OPENSSL_STRING_value(cmds, loop);
287: res = 1; /* assume success */
288: /* Check if this command has no ":arg" */
289: if ((arg = strstr(cmd, ":")) == NULL) {
290: if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
291: res = 0;
292: } else {
293: if ((int) (arg - cmd) > 254) {
294: BIO_printf(bio_out, "[Error]: command name too long\n");
295: return;
296: }
297: memcpy(buf, cmd, (int) (arg - cmd));
298: buf[arg - cmd] = '\0';
299: arg++; /* Move past the ":" */
300: /* Call the command with the argument */
301: if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
302: res = 0;
303: }
304: if (res)
305: BIO_printf(bio_out, "[Success]: %s\n", cmd);
306: else {
307: BIO_printf(bio_out, "[Failure]: %s\n", cmd);
308: ERR_print_errors(bio_out);
309: }
310: }
311: }
312:
313: int engine_main(int, char **);
314:
315: int
316: engine_main(int argc, char **argv)
317: {
318: int ret = 1, i;
319: const char **pp;
320: int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
321: ENGINE *e;
322: STACK_OF(OPENSSL_STRING) * engines = sk_OPENSSL_STRING_new_null();
323: STACK_OF(OPENSSL_STRING) * pre_cmds = sk_OPENSSL_STRING_new_null();
324: STACK_OF(OPENSSL_STRING) * post_cmds = sk_OPENSSL_STRING_new_null();
325: int badops = 1;
326: BIO *bio_out = NULL;
327: const char *indent = " ";
328:
329: bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
330:
331: argc--;
332: argv++;
333: while (argc >= 1) {
334: if (strncmp(*argv, "-v", 2) == 0) {
335: if (strspn(*argv + 1, "v") < strlen(*argv + 1))
336: goto skip_arg_loop;
337: if ((verbose = strlen(*argv + 1)) > 4)
338: goto skip_arg_loop;
339: } else if (strcmp(*argv, "-c") == 0)
340: list_cap = 1;
341: else if (strncmp(*argv, "-t", 2) == 0) {
342: test_avail = 1;
343: if (strspn(*argv + 1, "t") < strlen(*argv + 1))
344: goto skip_arg_loop;
345: if ((test_avail_noise = strlen(*argv + 1) - 1) > 1)
346: goto skip_arg_loop;
347: } else if (strcmp(*argv, "-pre") == 0) {
348: argc--;
349: argv++;
350: if (argc == 0)
351: goto skip_arg_loop;
352: sk_OPENSSL_STRING_push(pre_cmds, *argv);
353: } else if (strcmp(*argv, "-post") == 0) {
354: argc--;
355: argv++;
356: if (argc == 0)
357: goto skip_arg_loop;
358: sk_OPENSSL_STRING_push(post_cmds, *argv);
359: } else if ((strncmp(*argv, "-h", 2) == 0) ||
360: (strcmp(*argv, "-?") == 0))
361: goto skip_arg_loop;
362: else
363: sk_OPENSSL_STRING_push(engines, *argv);
364: argc--;
365: argv++;
366: }
367: /* Looks like everything went OK */
368: badops = 0;
369: skip_arg_loop:
370:
371: if (badops) {
372: for (pp = engine_usage; (*pp != NULL); pp++)
373: BIO_printf(bio_err, "%s", *pp);
374: goto end;
375: }
376: if (sk_OPENSSL_STRING_num(engines) == 0) {
377: for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
378: sk_OPENSSL_STRING_push(engines, (char *) ENGINE_get_id(e));
379: }
380: }
381: for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
382: const char *id = sk_OPENSSL_STRING_value(engines, i);
383: if ((e = ENGINE_by_id(id)) != NULL) {
384: const char *name = ENGINE_get_name(e);
385: /* Do "id" first, then "name". Easier to auto-parse. */
386: BIO_printf(bio_out, "(%s) %s\n", id, name);
387: util_do_cmds(e, pre_cmds, bio_out, indent);
388: if (strcmp(ENGINE_get_id(e), id) != 0) {
389: BIO_printf(bio_out, "Loaded: (%s) %s\n",
390: ENGINE_get_id(e), ENGINE_get_name(e));
391: }
392: if (list_cap) {
393: int cap_size = 256;
394: char *cap_buf = NULL;
395: int k, n;
396: const int *nids;
397: ENGINE_CIPHERS_PTR fn_c;
398: ENGINE_DIGESTS_PTR fn_d;
399: ENGINE_PKEY_METHS_PTR fn_pk;
400:
401: if (ENGINE_get_RSA(e) != NULL
402: && !append_buf(&cap_buf, "RSA",
403: &cap_size, 256))
404: goto end;
405: if (ENGINE_get_DSA(e) != NULL
406: && !append_buf(&cap_buf, "DSA",
407: &cap_size, 256))
408: goto end;
409: if (ENGINE_get_DH(e) != NULL
410: && !append_buf(&cap_buf, "DH",
411: &cap_size, 256))
412: goto end;
413: if (ENGINE_get_RAND(e) != NULL
414: && !append_buf(&cap_buf, "RAND",
415: &cap_size, 256))
416: goto end;
417:
418: fn_c = ENGINE_get_ciphers(e);
419: if (!fn_c)
420: goto skip_ciphers;
421: n = fn_c(e, NULL, &nids, 0);
422: for (k = 0; k < n; ++k)
423: if (!append_buf(&cap_buf,
424: OBJ_nid2sn(nids[k]),
425: &cap_size, 256))
426: goto end;
427:
428: skip_ciphers:
429: fn_d = ENGINE_get_digests(e);
430: if (!fn_d)
431: goto skip_digests;
432: n = fn_d(e, NULL, &nids, 0);
433: for (k = 0; k < n; ++k)
434: if (!append_buf(&cap_buf,
435: OBJ_nid2sn(nids[k]),
436: &cap_size, 256))
437: goto end;
438:
439: skip_digests:
440: fn_pk = ENGINE_get_pkey_meths(e);
441: if (!fn_pk)
442: goto skip_pmeths;
443: n = fn_pk(e, NULL, &nids, 0);
444: for (k = 0; k < n; ++k)
445: if (!append_buf(&cap_buf,
446: OBJ_nid2sn(nids[k]),
447: &cap_size, 256))
448: goto end;
449: skip_pmeths:
450: if (cap_buf && (*cap_buf != '\0'))
451: BIO_printf(bio_out, " [%s]\n", cap_buf);
452:
453: free(cap_buf);
454: }
455: if (test_avail) {
456: BIO_printf(bio_out, "%s", indent);
457: if (ENGINE_init(e)) {
458: BIO_printf(bio_out, "[ available ]\n");
459: util_do_cmds(e, post_cmds, bio_out, indent);
460: ENGINE_finish(e);
461: } else {
462: BIO_printf(bio_out, "[ unavailable ]\n");
463: if (test_avail_noise)
464: ERR_print_errors_fp(stdout);
465: ERR_clear_error();
466: }
467: }
468: if ((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
469: goto end;
470: ENGINE_free(e);
471: } else
472: ERR_print_errors(bio_err);
473: }
474:
475: ret = 0;
476: end:
477:
478: ERR_print_errors(bio_err);
479: sk_OPENSSL_STRING_pop_free(engines, identity);
480: sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
481: sk_OPENSSL_STRING_pop_free(post_cmds, identity);
482: if (bio_out != NULL)
483: BIO_free_all(bio_out);
484:
485: return (ret);
486: }
487: #endif