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