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