Fix typo in help & comment formatting
[openssl.git] / apps / engine.c
1 /*
2  * Written by Richard Levitte <richard@levitte.org> for the OpenSSL project
3  * 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 #include "apps.h"
63 #include <openssl/err.h>
64 #ifndef OPENSSL_NO_ENGINE
65 # include <openssl/engine.h>
66 # include <openssl/ssl.h>
67
68 typedef 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
74 OPTIONS 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}
88 };
89
90 static void identity(char *ptr)
91 {
92     return;
93 }
94
95 static int append_buf(char **buf, const char *s, int *size, int step)
96 {
97     int l = strlen(s);
98
99     if (*buf == NULL) {
100         *size = step;
101         *buf = OPENSSL_malloc(*size);
102         if (*buf == NULL)
103             return 0;
104         **buf = '\0';
105     }
106
107     if (**buf != '\0')
108         l += 2;                 /* ", " */
109
110     if (strlen(*buf) + strlen(s) >= (unsigned int)*size) {
111         *size += step;
112         *buf = OPENSSL_realloc(*buf, *size);
113     }
114
115     if (*buf == NULL)
116         return 0;
117
118     if (**buf != '\0')
119         BUF_strlcat(*buf, ", ", *size);
120     BUF_strlcat(*buf, s, *size);
121
122     return 1;
123 }
124
125 static int util_flags(BIO *out, unsigned int flags, const char *indent)
126 {
127     int started = 0, err = 0;
128     /* Indent before displaying input flags */
129     BIO_printf(out, "%s%s(input flags): ", indent, indent);
130     if (flags == 0) {
131         BIO_printf(out, "<no flags>\n");
132         return 1;
133     }
134     /*
135      * If the object is internal, mark it in a way that shows instead of
136      * having it part of all the other flags, even if it really is.
137      */
138     if (flags & ENGINE_CMD_FLAG_INTERNAL) {
139         BIO_printf(out, "[Internal] ");
140     }
141
142     if (flags & ENGINE_CMD_FLAG_NUMERIC) {
143         BIO_printf(out, "NUMERIC");
144         started = 1;
145     }
146     /*
147      * Now we check that no combinations of the mutually exclusive NUMERIC,
148      * STRING, and NO_INPUT flags have been used. Future flags that can be
149      * OR'd together with these would need to added after these to preserve
150      * the testing logic.
151      */
152     if (flags & ENGINE_CMD_FLAG_STRING) {
153         if (started) {
154             BIO_printf(out, "|");
155             err = 1;
156         }
157         BIO_printf(out, "STRING");
158         started = 1;
159     }
160     if (flags & ENGINE_CMD_FLAG_NO_INPUT) {
161         if (started) {
162             BIO_printf(out, "|");
163             err = 1;
164         }
165         BIO_printf(out, "NO_INPUT");
166         started = 1;
167     }
168     /* Check for unknown flags */
169     flags = flags & ~ENGINE_CMD_FLAG_NUMERIC &
170         ~ENGINE_CMD_FLAG_STRING &
171         ~ENGINE_CMD_FLAG_NO_INPUT & ~ENGINE_CMD_FLAG_INTERNAL;
172     if (flags) {
173         if (started)
174             BIO_printf(out, "|");
175         BIO_printf(out, "<0x%04X>", flags);
176     }
177     if (err)
178         BIO_printf(out, "  <illegal flags!>");
179     BIO_printf(out, "\n");
180     return 1;
181 }
182
183 static int util_verbose(ENGINE *e, int verbose, BIO *out, const char *indent)
184 {
185     static const int line_wrap = 78;
186     int num;
187     int ret = 0;
188     char *name = NULL;
189     char *desc = NULL;
190     int flags;
191     int xpos = 0;
192     STACK_OF(OPENSSL_STRING) *cmds = NULL;
193     if (!ENGINE_ctrl(e, ENGINE_CTRL_HAS_CTRL_FUNCTION, 0, NULL, NULL) ||
194         ((num = ENGINE_ctrl(e, ENGINE_CTRL_GET_FIRST_CMD_TYPE,
195                             0, NULL, NULL)) <= 0)) {
196         return 1;
197     }
198
199     cmds = sk_OPENSSL_STRING_new_null();
200     if (!cmds)
201         goto err;
202
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 = OPENSSL_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 = OPENSSL_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(out, indent);
234             else
235                 /* Otherwise prepend a ", " */
236                 xpos += BIO_printf(out, ", ");
237             if (verbose == 1) {
238                 /*
239                  * We're just listing names, comma-delimited
240                  */
241                 if ((xpos > (int)strlen(indent)) &&
242                     (xpos + (int)strlen(name) > line_wrap)) {
243                     BIO_printf(out, "\n");
244                     xpos = BIO_puts(out, indent);
245                 }
246                 xpos += BIO_printf(out, "%s", name);
247             } else {
248                 /* We're listing names plus descriptions */
249                 BIO_printf(out, "%s: %s\n", name,
250                            (desc == NULL) ? "<no description>" : desc);
251                 /* ... and sometimes input flags */
252                 if ((verbose >= 3) && !util_flags(out, flags, indent))
253                     goto err;
254                 xpos = 0;
255             }
256         }
257         OPENSSL_free(name);
258         name = NULL;
259         if (desc) {
260             OPENSSL_free(desc);
261             desc = NULL;
262         }
263         /* Move to the next command */
264         num = ENGINE_ctrl(e, ENGINE_CTRL_GET_NEXT_CMD_TYPE, num, NULL, NULL);
265     } while (num > 0);
266     if (xpos > 0)
267         BIO_printf(out, "\n");
268     ret = 1;
269  err:
270     if (cmds)
271         sk_OPENSSL_STRING_pop_free(cmds, identity);
272     if (name)
273         OPENSSL_free(name);
274     if (desc)
275         OPENSSL_free(desc);
276     return ret;
277 }
278
279 static void util_do_cmds(ENGINE *e, STACK_OF(OPENSSL_STRING) *cmds,
280                          BIO *out, const char *indent)
281 {
282     int loop, res, num = sk_OPENSSL_STRING_num(cmds);
283
284     if (num < 0) {
285         BIO_printf(out, "[Error]: internal stack error\n");
286         return;
287     }
288     for (loop = 0; loop < num; loop++) {
289         char buf[256];
290         const char *cmd, *arg;
291         cmd = sk_OPENSSL_STRING_value(cmds, loop);
292         res = 1;                /* assume success */
293         /* Check if this command has no ":arg" */
294         if ((arg = strstr(cmd, ":")) == NULL) {
295             if (!ENGINE_ctrl_cmd_string(e, cmd, NULL, 0))
296                 res = 0;
297         } else {
298             if ((int)(arg - cmd) > 254) {
299                 BIO_printf(out, "[Error]: command name too long\n");
300                 return;
301             }
302             memcpy(buf, cmd, (int)(arg - cmd));
303             buf[arg - cmd] = '\0';
304             arg++;              /* Move past the ":" */
305             /* Call the command with the argument */
306             if (!ENGINE_ctrl_cmd_string(e, buf, arg, 0))
307                 res = 0;
308         }
309         if (res)
310             BIO_printf(out, "[Success]: %s\n", cmd);
311         else {
312             BIO_printf(out, "[Failure]: %s\n", cmd);
313             ERR_print_errors(out);
314         }
315     }
316 }
317
318 int engine_main(int argc, char **argv)
319 {
320     int ret = 1, i;
321     int verbose = 0, list_cap = 0, test_avail = 0, test_avail_noise = 0;
322     ENGINE *e;
323     STACK_OF(OPENSSL_STRING) *engines = sk_OPENSSL_STRING_new_null();
324     STACK_OF(OPENSSL_STRING) *pre_cmds = sk_OPENSSL_STRING_new_null();
325     STACK_OF(OPENSSL_STRING) *post_cmds = sk_OPENSSL_STRING_new_null();
326     BIO *out;
327     const char *indent = "     ";
328     OPTION_CHOICE o;
329     char *prog;
330
331     out = dup_bio_out();
332     prog = opt_init(argc, argv, engine_options);
333     if (!engines || !pre_cmds || !post_cmds)
334         goto end;
335     while ((o = opt_next()) != OPT_EOF) {
336         switch (o) {
337         case OPT_EOF:
338         case OPT_ERR:
339             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
340             goto end;
341         case OPT_HELP:
342             opt_help(engine_options);
343             ret = 0;
344             goto end;
345         case OPT_VVVV:
346         case OPT_VVV:
347         case OPT_VV:
348         case OPT_V:
349             /* Convert to an integer from one to four. */
350             i = (int)(o - OPT_V) + 1;
351             if (verbose < i)
352                 verbose = i;
353             break;
354         case OPT_C:
355             list_cap = 1;
356             break;
357         case OPT_TT:
358             test_avail_noise++;
359         case OPT_T:
360             test_avail++;
361             break;
362         case OPT_PRE:
363             sk_OPENSSL_STRING_push(pre_cmds, opt_arg());
364             break;
365         case OPT_POST:
366             sk_OPENSSL_STRING_push(post_cmds, opt_arg());
367             break;
368         }
369     }
370     argc = opt_num_rest();
371     argv = opt_rest();
372     for ( ; *argv; argv++)
373         sk_OPENSSL_STRING_push(engines, *argv);
374
375     if (sk_OPENSSL_STRING_num(engines) == 0) {
376         for (e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e)) {
377             sk_OPENSSL_STRING_push(engines, (char *)ENGINE_get_id(e));
378         }
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             /*
386              * Do "id" first, then "name". Easier to auto-parse.
387              */
388             BIO_printf(out, "(%s) %s\n", id, name);
389             util_do_cmds(e, pre_cmds, out, indent);
390             if (strcmp(ENGINE_get_id(e), id) != 0) {
391                 BIO_printf(out, "Loaded: (%s) %s\n",
392                            ENGINE_get_id(e), ENGINE_get_name(e));
393             }
394             if (list_cap) {
395                 int cap_size = 256;
396                 char *cap_buf = NULL;
397                 int k, n;
398                 const int *nids;
399                 ENGINE_CIPHERS_PTR fn_c;
400                 ENGINE_DIGESTS_PTR fn_d;
401                 ENGINE_PKEY_METHS_PTR fn_pk;
402
403                 if (ENGINE_get_RSA(e) != NULL
404                     && !append_buf(&cap_buf, "RSA", &cap_size, 256))
405                     goto end;
406                 if (ENGINE_get_DSA(e) != NULL
407                     && !append_buf(&cap_buf, "DSA", &cap_size, 256))
408                     goto end;
409                 if (ENGINE_get_DH(e) != NULL
410                     && !append_buf(&cap_buf, "DH", &cap_size, 256))
411                     goto end;
412                 if (ENGINE_get_RAND(e) != NULL
413                     && !append_buf(&cap_buf, "RAND", &cap_size, 256))
414                     goto end;
415
416                 fn_c = ENGINE_get_ciphers(e);
417                 if (!fn_c)
418                     goto skip_ciphers;
419                 n = fn_c(e, NULL, &nids, 0);
420                 for (k = 0; k < n; ++k)
421                     if (!append_buf(&cap_buf,
422                                     OBJ_nid2sn(nids[k]), &cap_size, 256))
423                         goto end;
424
425  skip_ciphers:
426                 fn_d = ENGINE_get_digests(e);
427                 if (!fn_d)
428                     goto skip_digests;
429                 n = fn_d(e, NULL, &nids, 0);
430                 for (k = 0; k < n; ++k)
431                     if (!append_buf(&cap_buf,
432                                     OBJ_nid2sn(nids[k]), &cap_size, 256))
433                         goto end;
434
435  skip_digests:
436                 fn_pk = ENGINE_get_pkey_meths(e);
437                 if (!fn_pk)
438                     goto skip_pmeths;
439                 n = fn_pk(e, NULL, &nids, 0);
440                 for (k = 0; k < n; ++k)
441                     if (!append_buf(&cap_buf,
442                                     OBJ_nid2sn(nids[k]), &cap_size, 256))
443                         goto end;
444  skip_pmeths:
445                 if (cap_buf && (*cap_buf != '\0'))
446                     BIO_printf(out, " [%s]\n", cap_buf);
447
448                 OPENSSL_free(cap_buf);
449             }
450             if (test_avail) {
451                 BIO_printf(out, "%s", indent);
452                 if (ENGINE_init(e)) {
453                     BIO_printf(out, "[ available ]\n");
454                     util_do_cmds(e, post_cmds, out, indent);
455                     ENGINE_finish(e);
456                 } else {
457                     BIO_printf(out, "[ unavailable ]\n");
458                     if (test_avail_noise)
459                         ERR_print_errors_fp(stdout);
460                     ERR_clear_error();
461                 }
462             }
463             if ((verbose > 0) && !util_verbose(e, verbose, out, indent))
464                 goto end;
465             ENGINE_free(e);
466         } else
467             ERR_print_errors(bio_err);
468     }
469
470     ret = 0;
471  end:
472
473     ERR_print_errors(bio_err);
474     sk_OPENSSL_STRING_pop_free(engines, identity);
475     sk_OPENSSL_STRING_pop_free(pre_cmds, identity);
476     sk_OPENSSL_STRING_pop_free(post_cmds, identity);
477     BIO_free_all(out);
478     return (ret);
479 }
480 #else
481
482 # if PEDANTIC
483 static void *dummy = &dummy;
484 # endif
485
486 #endif