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