Fix Coverity 1453452: Control flow issues (DEADCODE)
[openssl.git] / apps / provider.c
1 /*
2  * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11
12 #include "apps.h"
13 #include "app_params.h"
14 #include "progs.h"
15 #include <openssl/err.h>
16 #include <openssl/evp.h>
17 #include <openssl/safestack.h>
18 #include <openssl/provider.h>
19 #include <openssl/core.h>
20 #include <openssl/core_numbers.h>
21
22 typedef enum OPTION_choice {
23     OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
24     OPT_V = 100, OPT_VV, OPT_VVV
25 } OPTION_CHOICE;
26
27 const OPTIONS provider_options[] = {
28     {OPT_HELP_STR, 1, '-', "Usage: %s [options] provider...\n"},
29     {OPT_HELP_STR, 1, '-', "  provider... Providers to load\n"},
30     {"help", OPT_HELP, '-', "Display this summary"},
31     {"v", OPT_V, '-', "List the algorithm names of specified provider"},
32     {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
33     {OPT_MORE_STR, 0, '-', "categorised by operation type"},
34     {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
35     {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
36     {NULL}
37 };
38
39 typedef struct info_st INFO;
40 typedef struct meta_st META;
41
42 struct info_st {
43     const char *name;
44     void *method;
45     const OSSL_PARAM *gettable_params;
46     const OSSL_PARAM *gettable_ctx_params;
47     const OSSL_PARAM *settable_ctx_params;
48 };
49
50 struct meta_st {
51     int first;                   /* For prints */
52     int total;
53     int indent;
54     int subindent;
55     int verbose;
56     const char *label;
57     OSSL_PROVIDER *prov;
58     void (*fn)(META *meta, INFO *info);
59 };
60
61 static void print_caps(META *meta, INFO *info)
62 {
63     switch (meta->verbose) {
64     case 1:
65         BIO_printf(bio_out, meta->first ? "%s" : " %s", info->name);
66         break;
67     case 2:
68         if (meta->first) {
69             if (meta->total > 0)
70                 BIO_printf(bio_out, "\n");
71             BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
72         }
73         BIO_printf(bio_out, " %s", info->name);
74         break;
75     case 3:
76     default:
77         BIO_printf(bio_out, "%*s%s %s\n", meta->indent, "", meta->label,
78                    info->name);
79         print_param_types("retrievable algorithm parameters",
80                           info->gettable_params, meta->subindent);
81         print_param_types("retrievable operation parameters",
82                           info->gettable_ctx_params, meta->subindent);
83         print_param_types("settable operation parameters",
84                           info->settable_ctx_params, meta->subindent);
85         break;
86     }
87     meta->first = 0;
88 }
89
90 static void do_method(void *method, const char *name,
91                       const OSSL_PARAM *gettable_params,
92                       const OSSL_PARAM *gettable_ctx_params,
93                       const OSSL_PARAM *settable_ctx_params,
94                       META *meta)
95 {
96     INFO info;
97
98     info.name = name;
99     info.method = method;
100     info.gettable_params = gettable_params;
101     info.gettable_ctx_params = gettable_ctx_params;
102     info.settable_ctx_params = settable_ctx_params;
103     meta->fn(meta, &info);
104     meta->total++;
105 }
106
107 static void do_cipher(EVP_CIPHER *cipher, void *meta)
108 {
109     do_method(cipher, EVP_CIPHER_name(cipher),
110               EVP_CIPHER_gettable_params(cipher),
111               EVP_CIPHER_CTX_gettable_params(cipher),
112               EVP_CIPHER_CTX_settable_params(cipher),
113               meta);
114 }
115
116 static void do_digest(EVP_MD *digest, void *meta)
117 {
118     do_method(digest, EVP_MD_name(digest),
119               EVP_MD_gettable_params(digest),
120               EVP_MD_CTX_gettable_params(digest),
121               EVP_MD_CTX_settable_params(digest),
122               meta);
123 }
124
125 static void do_mac(EVP_MAC *mac, void *meta)
126 {
127     do_method(mac, EVP_MAC_name(mac),
128               EVP_MAC_gettable_params(mac),
129               EVP_MAC_CTX_gettable_params(mac),
130               EVP_MAC_CTX_settable_params(mac),
131               meta);
132 }
133
134 /*
135  * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
136  */
137 #if 0
138 static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
139 {
140     do_method(keymgmt, EVP_KEYMGMT_name(keymgmt),
141               EVP_KEYMGMT_gettable_params(keymgmt),
142               EVP_KEYMGMT_gettable_ctx_params(keymgmt),
143               EVP_KEYMGMT_settable_ctx_params(keymgmt),
144               meta);
145 }
146
147 static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
148 {
149     do_method(keyexch, EVP_KEYEXCH_name(keyexch),
150               EVP_KEYEXCH_gettable_params(keyexch),
151               EVP_KEYEXCH_gettable_ctx_params(keyexch),
152               EVP_KEYEXCH_settable_ctx_params(keyexch),
153               meta);
154 }
155 #endif
156
157 int provider_main(int argc, char **argv)
158 {
159     int ret = 1, i;
160     int verbose = 0;
161     STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
162     OPTION_CHOICE o;
163     char *prog;
164
165     prog = opt_init(argc, argv, provider_options);
166     while ((o = opt_next()) != OPT_EOF) {
167         switch (o) {
168         default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
169             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
170             goto end;
171         case OPT_HELP:
172             opt_help(provider_options);
173             ret = 0;
174             goto end;
175         case OPT_VVV:
176         case OPT_VV:
177         case OPT_V:
178             /* Convert to an integer from one to four. */
179             i = (int)(o - OPT_V) + 1;
180             if (verbose < i)
181                 verbose = i;
182             break;
183         }
184     }
185
186     /* Allow any trailing parameters as provider names. */
187     argc = opt_num_rest();
188     argv = opt_rest();
189     for ( ; *argv; argv++) {
190         if (**argv == '-') {
191             BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
192                        prog);
193             BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
194             goto end;
195         }
196         sk_OPENSSL_CSTRING_push(providers, *argv);
197     }
198
199     ret = 0;
200     for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
201         const char *name = sk_OPENSSL_CSTRING_value(providers, i);
202         OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);
203
204         if (prov != NULL) {
205             BIO_printf(bio_out, verbose == 0 ? "%s\n" :  "[ %s ]\n", name);
206
207             if (verbose > 0) {
208                 META data;
209
210                 data.total = 0;
211                 data.first = 1;
212                 data.verbose = verbose;
213                 data.prov = prov;
214                 data.fn = print_caps;
215
216                 switch (verbose) {
217                 case 1:
218                     BIO_printf(bio_out, "    ");
219                     break;
220                 case 2:
221                     data.indent = 4;
222                     break;
223                 case 3:
224                 default:
225                     data.indent = 4;
226                     data.subindent = 10;
227                     break;
228                 }
229
230                 if (verbose > 1) {
231                     data.first = 1;
232                     data.label = "Cipher";
233                 }
234                 EVP_CIPHER_do_all_ex(NULL, do_cipher, &data);
235                 if (verbose > 1) {
236                     data.first = 1;
237                     data.label = "Digest";
238                 }
239                 EVP_MD_do_all_ex(NULL, do_digest, &data);
240                 if (verbose > 1) {
241                     data.first = 1;
242                     data.label = "MAC";
243                 }
244                 EVP_MAC_do_all_ex(NULL, do_mac, &data);
245
246 /*
247  * TODO(3.0) Enable when KEYMGMT and KEYEXCH have do_all_ex functions
248  */
249 #if 0
250                 if (verbose > 1) {
251                     data.first = 1;
252                     data.label = "Key manager";
253                 }
254                 EVP_KEYMGMT_do_all_ex(NULL, do_keymgmt, &data);
255                 if (verbose > 1) {
256                     data.first = 1;
257                     data.label = "Key exchange";
258                 }
259                 EVP_KEYEXCH_do_all_ex(NULL, do_keyexch, &data);
260 #endif
261
262                 switch (verbose) {
263                 default:
264                     break;
265                 case 2:
266                 case 1:
267                     BIO_printf(bio_out, "\n");
268                     break;
269                 }
270             }
271             OSSL_PROVIDER_unload(prov);
272         } else {
273             ERR_print_errors(bio_err);
274             ret = 1;
275             /*
276              * Just because one provider module failed, there's no reason to
277              * stop, if there are more to try.
278              */
279         }
280     }
281
282  end:
283
284     ERR_print_errors(bio_err);
285     sk_OPENSSL_CSTRING_free(providers);
286     return ret;
287 }