a66696152fef64994737dd2a857fdc883729a513
[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 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          - verbose mode, a textual listing of the engines in OpenSSL\n",
76 #if 0
77 " -c          - for each engine, also list the capabilities\n",
78 #endif
79 " -t          - for each engine, check that they are really available\n",
80 NULL
81 };
82
83 static void identity(void *ptr)
84         {
85         return;
86         }
87
88 int MAIN(int, char **);
89
90 int MAIN(int argc, char **argv)
91         {
92         int ret=1,i;
93         char **pp;
94         int verbose=0, list_cap=0, test_avail=0;
95         ENGINE *e;
96         STACK *engines = sk_new_null();
97         int badops=0;
98         BIO *bio_out=NULL;
99
100         apps_startup();
101         SSL_load_error_strings();
102
103         if (bio_err == NULL)
104                 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
105         bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
106 #ifdef VMS
107         {
108         BIO *tmpbio = BIO_new(BIO_f_linebuffer());
109         bio_out = BIO_push(tmpbio, bio_out);
110         }
111 #endif
112
113         argc--;
114         argv++;
115         while (argc >= 1)
116                 {
117                 if (strcmp(*argv,"-v") == 0)
118                         verbose=1;
119                 else if (strcmp(*argv,"-c") == 0)
120                         {
121                         list_cap=1;
122
123                         /* When list_cap is implemented. remove the following
124                          * 3 lines.
125                          */
126                         BIO_printf(bio_err, "-c not yet supported\n");
127                         badops=1;
128                         break;
129                         }
130                 else if (strcmp(*argv,"-t") == 0)
131                         test_avail=1;
132                 else if ((strncmp(*argv,"-h",2) == 0) ||
133                          (strcmp(*argv,"-?") == 0))
134                         {
135                         badops=1;
136                         break;
137                         }
138                 else
139                         {
140                         sk_push(engines,*argv);
141                         }
142                 argc--;
143                 argv++;
144                 }
145
146         if (badops)
147                 {
148                 for (pp=engine_usage; (*pp != NULL); pp++)
149                         BIO_printf(bio_err,"%s",*pp);
150                 goto end;
151                 }
152
153         if (sk_num(engines) == 0)
154                 {
155                 for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
156                         {
157                         sk_push(engines,(char *)ENGINE_get_id(e));
158                         }
159                 }
160
161         for (i=0; i<sk_num(engines); i++)
162                 {
163                 const char *id = sk_value(engines,i);
164                 if ((e = ENGINE_by_id(id)) != NULL)
165                         {
166                         const char *name = ENGINE_get_name(e);
167                         BIO_printf(bio_out, "%s (%s)", name, id);
168                         if (list_cap || test_avail)
169                                 BIO_printf(bio_out, ": ");
170                         if (test_avail)
171                                 {
172                                 if (ENGINE_init(e))
173                                         {
174                                         BIO_printf(bio_out, "available");
175                                         ENGINE_finish(e);
176                                         }
177                                 else
178                                         {
179                                         BIO_printf(bio_out, "unavailable");
180                                         ERR_clear_error();
181                                         }
182                                 }
183                         BIO_printf(bio_out, "\n");
184                         }
185                 else
186                         ERR_print_errors(bio_err);
187                 }
188
189         ret=0;
190         ERR_print_errors(bio_err);
191 end:
192         sk_pop_free(engines, identity);
193         if (bio_out != NULL) BIO_free_all(bio_out);
194         EXIT(ret);
195         }
196