Annotation of src/usr.bin/openssl/engine.c, Revision 1.2
1.2 ! jsing 1: /* $OpenBSD: engine.c,v 1.1 2014/08/26 17:47:24 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: #if 0
195: BIO_printf(bio_out, "%s<no control commands>\n", indent);
196: #endif
197: return 1;
198: }
199: cmds = sk_OPENSSL_STRING_new_null();
200:
201: if (!cmds)
202: goto err;
203: do {
204: int len;
205: /* Get the command input flags */
206: if ((flags = ENGINE_ctrl(e, ENGINE_CTRL_GET_CMD_FLAGS, num,
207: NULL, NULL)) < 0)
208: goto err;
209: if (!(flags & ENGINE_CMD_FLAG_INTERNAL) || verbose >= 4) {
210: /* Get the command name */
211: if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_LEN_FROM_CMD, num,
212: NULL, NULL)) <= 0)
213: goto err;
214: if ((name = malloc(len + 1)) == NULL)
215: goto err;
216: if (ENGINE_ctrl(e, ENGINE_CTRL_GET_NAME_FROM_CMD, num, name,
217: NULL) <= 0)
218: goto err;
219: /* Get the command description */
220: if ((len = ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_LEN_FROM_CMD, num,
221: NULL, NULL)) < 0)
222: goto err;
223: if (len > 0) {
224: if ((desc = malloc(len + 1)) == NULL)
225: goto err;
226: if (ENGINE_ctrl(e, ENGINE_CTRL_GET_DESC_FROM_CMD, num, desc,
227: NULL) <= 0)
228: goto err;
229: }
230: /* Now decide on the output */
231: if (xpos == 0)
232: /* Do an indent */
233: xpos = BIO_puts(bio_out, indent);
234: else
235: /* Otherwise prepend a ", " */
236: xpos += BIO_printf(bio_out, ", ");
237: if (verbose == 1) {
238: /* We're just listing names, comma-delimited */
239: if ((xpos > (int) strlen(indent)) &&
240: (xpos + (int) strlen(name) > line_wrap)) {
241: BIO_printf(bio_out, "\n");
242: xpos = BIO_puts(bio_out, indent);
243: }
244: xpos += BIO_printf(bio_out, "%s", name);
245: } else {
246: /* We're listing names plus descriptions */
247: BIO_printf(bio_out, "%s: %s\n", name,
248: (desc == NULL) ? "<no description>" : desc);
249: /* ... and sometimes input flags */
250: if ((verbose >= 3) && !util_flags(bio_out, flags,
251: indent))
252: goto err;
253: xpos = 0;
254: }
255: }
256: free(name);
257: name = NULL;
258: free(desc);
259: desc = NULL;
260:
261: /* Move to the next command */
262: num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE,
263: num, NULL, NULL);
264: } while (num > 0);
265: if (xpos > 0)
266: BIO_printf(bio_out, "\n");
267: ret = 1;
268: err:
269: if (cmds)
270: sk_OPENSSL_STRING_pop_free(cmds, identity);
271: free(name);
272: free(desc);
273: return ret;
274: }
275:
276: static void
277: util_do_cmds(ENGINE * e, STACK_OF(OPENSSL_STRING) * cmds,
278: BIO * bio_out, const char *indent)
279: {
280: int loop, res, num = sk_OPENSSL_STRING_num(cmds);
281:
282: if (num < 0) {
283: BIO_printf(bio_out, "[Error]: internal stack error\n");
284: return;
285: }
286: for (loop = 0; loop < num; loop++) {
287: char buf[256];
288: const char *cmd, *arg;
289: cmd = sk_OPENSSL_STRING_value(cmds, loop);
290: res = 1; /* assume success */
291: /* Check if this command has no ":arg" */
292: if ((arg = strstr(cmd, ":")) == NULL) {
293: if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
294: res = 0;
295: } else {
296: if ((int) (arg - cmd) > 254) {
297: BIO_printf(bio_out, "[Error]: command name too long\n");
298: return;
299: }
300: memcpy(buf, cmd, (int) (arg - cmd));
301: buf[arg - cmd] = '\0';
302: arg++; /* Move past the ":" */
303: /* Call the command with the argument */
304: if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
305: res = 0;
306: }
307: if (res)
308: BIO_printf(bio_out, "[Success]: %s\n", cmd);
309: else {
310: BIO_printf(bio_out, "[Failure]: %s\n", cmd);
311: ERR_print_errors(bio_out);
312: }
313: }
314: }
315:
316: int engine_main(int, char **);
317:
318: int
319: engine_main(int argc, char **argv)
320: {
321: int ret = 1, i;
322: const char **pp;
323: int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
324: ENGINE *e;
325: STACK_OF(OPENSSL_STRING) * engines = sk_OPENSSL_STRING_new_null();
326: STACK_OF(OPENSSL_STRING) * pre_cmds = sk_OPENSSL_STRING_new_null();
327: STACK_OF(OPENSSL_STRING) * post_cmds = sk_OPENSSL_STRING_new_null();
328: int badops = 1;
329: BIO *bio_out = NULL;
330: const char *indent = " ";
331:
332: bio_out = BIO_new_fp(stdout, BIO_NOCLOSE);
333:
334: argc--;
335: argv++;
336: while (argc >= 1) {
337: if (strncmp(*argv, "-v", 2) == 0) {
338: if (strspn(*argv + 1, "v") < strlen(*argv + 1))
339: goto skip_arg_loop;
340: if ((verbose = strlen(*argv + 1)) > 4)
341: goto skip_arg_loop;
342: } else if (strcmp(*argv, "-c") == 0)
343: list_cap = 1;
344: else if (strncmp(*argv, "-t", 2) == 0) {
345: test_avail = 1;
346: if (strspn(*argv + 1, "t") < strlen(*argv + 1))
347: goto skip_arg_loop;
348: if ((test_avail_noise = strlen(*argv + 1) - 1) > 1)
349: goto skip_arg_loop;
350: } else if (strcmp(*argv, "-pre") == 0) {
351: argc--;
352: argv++;
353: if (argc == 0)
354: goto skip_arg_loop;
355: sk_OPENSSL_STRING_push(pre_cmds, *argv);
356: } else if (strcmp(*argv, "-post") == 0) {
357: argc--;
358: argv++;
359: if (argc == 0)
360: goto skip_arg_loop;
361: sk_OPENSSL_STRING_push(post_cmds, *argv);
362: } else if ((strncmp(*argv, "-h", 2) == 0) ||
363: (strcmp(*argv, "-?") == 0))
364: goto skip_arg_loop;
365: else
366: sk_OPENSSL_STRING_push(engines, *argv);
367: argc--;
368: argv++;
369: }
370: /* Looks like everything went OK */
371: badops = 0;
372: skip_arg_loop:
373:
374: if (badops) {
375: for (pp = engine_usage; (*pp != NULL); pp++)
376: BIO_printf(bio_err, "%s", *pp);
377: goto end;
378: }
379: if (sk_OPENSSL_STRING_num(engines) == 0) {
380: for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
381: sk_OPENSSL_STRING_push(engines, (char *) ENGINE_get_id(e));
382: }
383: }
384: for (i = 0; i < sk_OPENSSL_STRING_num(engines); i++) {
385: const char *id = sk_OPENSSL_STRING_value(engines, i);
386: if ((e = ENGINE_by_id(id)) != NULL) {
387: const char *name = ENGINE_get_name(e);
388: /* Do "id" first, then "name". Easier to auto-parse. */
389: BIO_printf(bio_out, "(%s) %s\n", id, name);
390: util_do_cmds(e, pre_cmds, bio_out, indent);
391: if (strcmp(ENGINE_get_id(e), id) != 0) {
392: BIO_printf(bio_out, "Loaded: (%s) %s\n",
393: ENGINE_get_id(e), ENGINE_get_name(e));
394: }
395: if (list_cap) {
396: int cap_size = 256;
397: char *cap_buf = NULL;
398: int k, n;
399: const int *nids;
400: ENGINE_CIPHERS_PTR fn_c;
401: ENGINE_DIGESTS_PTR fn_d;
402: ENGINE_PKEY_METHS_PTR fn_pk;
403:
404: if (ENGINE_get_RSA(e) != NULL
405: && !append_buf(&cap_buf, "RSA",
406: &cap_size, 256))
407: goto end;
408: if (ENGINE_get_DSA(e) != NULL
409: && !append_buf(&cap_buf, "DSA",
410: &cap_size, 256))
411: goto end;
412: if (ENGINE_get_DH(e) != NULL
413: && !append_buf(&cap_buf, "DH",
414: &cap_size, 256))
415: goto end;
416: if (ENGINE_get_RAND(e) != NULL
417: && !append_buf(&cap_buf, "RAND",
418: &cap_size, 256))
419: goto end;
420:
421: fn_c = ENGINE_get_ciphers(e);
422: if (!fn_c)
423: goto skip_ciphers;
424: n = fn_c(e, NULL, &nids, 0);
425: for (k = 0; k < n; ++k)
426: if (!append_buf(&cap_buf,
427: OBJ_nid2sn(nids[k]),
428: &cap_size, 256))
429: goto end;
430:
431: skip_ciphers:
432: fn_d = ENGINE_get_digests(e);
433: if (!fn_d)
434: goto skip_digests;
435: n = fn_d(e, NULL, &nids, 0);
436: for (k = 0; k < n; ++k)
437: if (!append_buf(&cap_buf,
438: OBJ_nid2sn(nids[k]),
439: &cap_size, 256))
440: goto end;
441:
442: skip_digests:
443: fn_pk = ENGINE_get_pkey_meths(e);
444: if (!fn_pk)
445: goto skip_pmeths;
446: n = fn_pk(e, NULL, &nids, 0);
447: for (k = 0; k < n; ++k)
448: if (!append_buf(&cap_buf,
449: OBJ_nid2sn(nids[k]),
450: &cap_size, 256))
451: goto end;
452: skip_pmeths:
453: if (cap_buf && (*cap_buf != '\0'))
454: BIO_printf(bio_out, " [%s]\n", cap_buf);
455:
456: free(cap_buf);
457: }
458: if (test_avail) {
459: BIO_printf(bio_out, "%s", indent);
460: if (ENGINE_init(e)) {
461: BIO_printf(bio_out, "[ available ]\n");
462: util_do_cmds(e, post_cmds, bio_out, indent);
463: ENGINE_finish(e);
464: } else {
465: BIO_printf(bio_out, "[ unavailable ]\n");
466: if (test_avail_noise)
467: ERR_print_errors_fp(stdout);
468: ERR_clear_error();
469: }
470: }
471: if ((verbose > 0) && !util_verbose(e, verbose, bio_out, indent))
472: goto end;
473: ENGINE_free(e);
474: } else
475: ERR_print_errors(bio_err);
476: }
477:
478: ret = 0;
479: end:
480:
481: ERR_print_errors(bio_err);
482: sk_OPENSSL_STRING_pop_free(engines, identity);
483: sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
484: sk_OPENSSL_STRING_pop_free(post_cmds, identity);
485: if (bio_out != NULL)
486: BIO_free_all(bio_out);
487:
488: return (ret);
489: }
490: #endif