looks like a cut&paste error
[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 " -l          - load all built-in engines\n",
81 NULL
82 };
83
84 static void identity(void *ptr)
85         {
86         return;
87         }
88
89 static int append_buf(char **buf, char *s, int *size, int step)
90         {
91         int l = strlen(s);
92
93         if (*buf == NULL)
94                 {
95                 *size = step;
96                 *buf = OPENSSL_malloc(*size);
97                 if (*buf == NULL)
98                         return 0;
99                 **buf = '\0';
100                 }
101
102         if (**buf != '\0')
103                 l += 2;         /* ", " */
104
105         if (strlen(*buf) + strlen(s) >= *size)
106                 {
107                 *size += step;
108                 *buf = OPENSSL_realloc(*buf, *size);
109                 }
110
111         if (*buf == NULL)
112                 return 0;
113
114         if (**buf != '\0')
115                 strcat(*buf, ", ");
116         strcat(*buf, s);
117
118         return 1;
119         }
120
121 int MAIN(int, char **);
122
123 int MAIN(int argc, char **argv)
124         {
125         int ret=1,i;
126         char **pp;
127         int verbose=0, list_cap=0, test_avail=0;
128         ENGINE *e;
129         STACK *engines = sk_new_null();
130         int badops=0;
131         BIO *bio_out=NULL;
132
133         apps_startup();
134         SSL_load_error_strings();
135
136         if (bio_err == NULL)
137                 bio_err=BIO_new_fp(stderr,BIO_NOCLOSE);
138         bio_out=BIO_new_fp(stdout,BIO_NOCLOSE);
139 #ifdef VMS
140         {
141         BIO *tmpbio = BIO_new(BIO_f_linebuffer());
142         bio_out = BIO_push(tmpbio, bio_out);
143         }
144 #endif
145
146         argc--;
147         argv++;
148         while (argc >= 1)
149                 {
150                 if (strcmp(*argv,"-v") == 0)
151                         verbose=1;
152                 else if (strcmp(*argv,"-c") == 0)
153                         list_cap=1;
154                 else if (strcmp(*argv,"-t") == 0)
155                         test_avail=1;
156                 else if (strcmp(*argv,"-l") == 0)
157                         ENGINE_load_builtin_engines();
158                 else if ((strncmp(*argv,"-h",2) == 0) ||
159                          (strcmp(*argv,"-?") == 0))
160                         {
161                         badops=1;
162                         break;
163                         }
164                 else
165                         {
166                         sk_push(engines,*argv);
167                         }
168                 argc--;
169                 argv++;
170                 }
171
172         if (badops)
173                 {
174                 for (pp=engine_usage; (*pp != NULL); pp++)
175                         BIO_printf(bio_err,"%s",*pp);
176                 goto end;
177                 }
178
179         if (sk_num(engines) == 0)
180                 {
181                 for(e = ENGINE_get_first(); e != NULL; e = ENGINE_get_next(e))
182                         {
183                         sk_push(engines,(char *)ENGINE_get_id(e));
184                         }
185                 }
186
187         for (i=0; i<sk_num(engines); i++)
188                 {
189                 const char *id = sk_value(engines,i);
190                 if ((e = ENGINE_by_id(id)) != NULL)
191                         {
192                         const char *name = ENGINE_get_name(e);
193                         BIO_printf(bio_out, "%s (%s)", name, id);
194                         if (list_cap || test_avail)
195                                 BIO_printf(bio_out, ":");
196                         if (test_avail)
197                                 {
198                                 if (ENGINE_init(e))
199                                         {
200                                         BIO_printf(bio_out, " available");
201                                         ENGINE_finish(e);
202                                         }
203                                 else
204                                         {
205                                         BIO_printf(bio_out, " unavailable");
206                                         ERR_clear_error();
207                                         }
208                                 }
209                         if (list_cap)
210                                 {
211                                 int cap_size = 256;
212                                 char *cap_buf = NULL;
213
214                                 if (ENGINE_get_RSA(e) != NULL
215                                         && !append_buf(&cap_buf, "RSA",
216                                                 &cap_size, 256))
217                                         goto end;
218                                 if (ENGINE_get_DSA(e) != NULL
219                                         && !append_buf(&cap_buf, "DSA",
220                                                 &cap_size, 256))
221                                         goto end;
222                                 if (ENGINE_get_DH(e) != NULL
223                                         && !append_buf(&cap_buf, "DH",
224                                                 &cap_size, 256))
225                                         goto end;
226                                 if (ENGINE_get_RAND(e) != NULL
227                                         && !append_buf(&cap_buf, "RAND",
228                                                 &cap_size, 256))
229                                         goto end;
230
231                                 if (*cap_buf != '\0')
232                                         BIO_printf(bio_out, " [%s]", cap_buf);
233
234                                 OPENSSL_free(cap_buf);
235                                 }
236                         BIO_printf(bio_out, "\n");
237                         }
238                 else
239                         ERR_print_errors(bio_err);
240                 }
241
242         ret=0;
243 end:
244         ERR_print_errors(bio_err);
245         sk_pop_free(engines, identity);
246         if (bio_out != NULL) BIO_free_all(bio_out);
247         EXIT(ret);
248         }