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