summaryrefslogtreecommitdiffstats
path: root/apps/provider.c
blob: ab91e68f431b745f36161267bfafe10fb1992e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
 *
 * Licensed under the Apache License 2.0 (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
 */

#include <openssl/opensslconf.h>

#include "apps.h"
#include "app_params.h"
#include "progs.h"
#include <openssl/err.h>
#include <openssl/evp.h>
#include <openssl/safestack.h>
#include <openssl/provider.h>
#include <openssl/core.h>
#include <openssl/core_numbers.h>

typedef enum OPTION_choice {
    OPT_ERR = -1, OPT_EOF = 0, OPT_HELP,
    OPT_V = 100, OPT_VV, OPT_VVV
} OPTION_CHOICE;

const OPTIONS provider_options[] = {
    {OPT_HELP_STR, 1, '-', "Usage: %s [options] provider...\n"},
    {OPT_HELP_STR, 1, '-', "  provider... Providers to load\n"},
    {"help", OPT_HELP, '-', "Display this summary"},
    {"v", OPT_V, '-', "List the algorithm names of specified provider"},
    {"vv", OPT_VV, '-', "List the algorithm names of specified providers,"},
    {OPT_MORE_STR, 0, '-', "categorised by operation type"},
    {"vvv", OPT_VVV, '-', "List the algorithm names of specified provider"},
    {OPT_MORE_STR, 0, '-', "one at a time, and list all known parameters"},
    {NULL}
};

typedef struct info_st INFO;
typedef struct meta_st META;

struct info_st {
    const char *name;
    void *method;
    const OSSL_PARAM *gettable_params;
    const OSSL_PARAM *gettable_ctx_params;
    const OSSL_PARAM *settable_ctx_params;
};

struct meta_st {
    int first;                   /* For prints */
    int total;
    int indent;
    int subindent;
    int verbose;
    const char *label;
    OSSL_PROVIDER *prov;
    void (*fn)(META *meta, INFO *info);
};

static void print_caps(META *meta, INFO *info)
{
    switch (meta->verbose) {
    case 1:
        BIO_printf(bio_out, meta->first ? "%s" : " %s", info->name);
        break;
    case 2:
        if (meta->first) {
            if (meta->total > 0)
                BIO_printf(bio_out, "\n");
            BIO_printf(bio_out, "%*s%ss:", meta->indent, "", meta->label);
        }
        BIO_printf(bio_out, " %s", info->name);
        break;
    case 3:
    default:
        BIO_printf(bio_out, "%*s%s %s\n", meta->indent, "", meta->label,
                   info->name);
        print_param_types("retrievable algorithm parameters",
                          info->gettable_params, meta->subindent);
        print_param_types("retrievable operation parameters",
                          info->gettable_ctx_params, meta->subindent);
        print_param_types("settable operation parameters",
                          info->settable_ctx_params, meta->subindent);
        break;
    }
    meta->first = 0;
}

static void do_method(void *method, const char *name,
                      const OSSL_PARAM *gettable_params,
                      const OSSL_PARAM *gettable_ctx_params,
                      const OSSL_PARAM *settable_ctx_params,
                      META *meta)
{
    INFO info;

    info.name = name;
    info.method = method;
    info.gettable_params = gettable_params;
    info.gettable_ctx_params = gettable_ctx_params;
    info.settable_ctx_params = settable_ctx_params;
    meta->fn(meta, &info);
    meta->total++;
}

static void do_cipher(EVP_CIPHER *cipher, void *meta)
{
    do_method(cipher, EVP_CIPHER_name(cipher),
              EVP_CIPHER_gettable_params(cipher),
              EVP_CIPHER_gettable_ctx_params(cipher),
              EVP_CIPHER_settable_ctx_params(cipher),
              meta);
}

static void do_digest(EVP_MD *digest, void *meta)
{
    do_method(digest, EVP_MD_name(digest),
              EVP_MD_gettable_params(digest),
              EVP_MD_gettable_ctx_params(digest),
              EVP_MD_settable_ctx_params(digest),
              meta);
}

static void do_mac(EVP_MAC *mac, void *meta)
{
    do_method(mac, EVP_MAC_name(mac),
              EVP_MAC_gettable_params(mac),
              EVP_MAC_gettable_ctx_params(mac),
              EVP_MAC_settable_ctx_params(mac),
              meta);
}

/*
 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have gettables and settables
 */
#if 0
static void do_keymgmt(EVP_KEYMGMT *keymgmt, void *meta)
{
    do_method(keymgmt, EVP_KEYMGMT_name(keymgmt),
              EVP_KEYMGMT_gettable_params(keymgmt),
              EVP_KEYMGMT_gettable_ctx_params(keymgmt),
              EVP_KEYMGMT_settable_ctx_params(keymgmt),
              meta);
}

static void do_keyexch(EVP_KEYEXCH *keyexch, void *meta)
{
    do_method(keyexch, EVP_KEYEXCH_name(keyexch),
              EVP_KEYEXCH_gettable_params(keyexch),
              EVP_KEYEXCH_gettable_ctx_params(keyexch),
              EVP_KEYEXCH_settable_ctx_params(keyexch),
              meta);
}
#endif

int provider_main(int argc, char **argv)
{
    int ret = 1, i;
    int verbose = 0;
    STACK_OF(OPENSSL_CSTRING) *providers = sk_OPENSSL_CSTRING_new_null();
    OPTION_CHOICE o;
    char *prog;

    prog = opt_init(argc, argv, provider_options);
    while ((o = opt_next()) != OPT_EOF) {
        switch (o) {
        default: /* Catching OPT_ERR & covering OPT_EOF which isn't possible */
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
            goto end;
        case OPT_HELP:
            opt_help(provider_options);
            ret = 0;
            goto end;
        case OPT_VVV:
        case OPT_VV:
        case OPT_V:
            /* Convert to an integer from one to four. */
            i = (int)(o - OPT_V) + 1;
            if (verbose < i)
                verbose = i;
            break;
        }
    }

    /* Allow any trailing parameters as provider names. */
    argc = opt_num_rest();
    argv = opt_rest();
    for ( ; *argv; argv++) {
        if (**argv == '-') {
            BIO_printf(bio_err, "%s: Cannot mix flags and provider names.\n",
                       prog);
            BIO_printf(bio_err, "%s: Use -help for summary.\n", prog);
            goto end;
        }
        sk_OPENSSL_CSTRING_push(providers, *argv);
    }

    ret = 0;
    for (i = 0; i < sk_OPENSSL_CSTRING_num(providers); i++) {
        const char *name = sk_OPENSSL_CSTRING_value(providers, i);
        OSSL_PROVIDER *prov = OSSL_PROVIDER_load(NULL, name);

        if (prov != NULL) {
            BIO_printf(bio_out, verbose == 0 ? "%s\n" :  "[ %s ]\n", name);

            if (verbose > 0) {
                META data;

                data.total = 0;
                data.first = 1;
                data.verbose = verbose;
                data.prov = prov;
                data.fn = print_caps;

                switch (verbose) {
                case 1:
                    BIO_printf(bio_out, "    ");
                    break;
                case 2:
                    data.indent = 4;
                    break;
                case 3:
                default:
                    data.indent = 4;
                    data.subindent = 10;
                    break;
                }

                if (verbose > 1) {
                    data.first = 1;
                    data.label = "Cipher";
                }
                EVP_CIPHER_do_all_ex(NULL, do_cipher, &data);
                if (verbose > 1) {
                    data.first = 1;
                    data.label = "Digest";
                }
                EVP_MD_do_all_ex(NULL, do_digest, &data);
                if (verbose > 1) {
                    data.first = 1;
                    data.label = "MAC";
                }
                EVP_MAC_do_all_ex(NULL, do_mac, &data);

/*
 * TODO(3.0) Enable when KEYMGMT and KEYEXCH have do_all_ex functions
 */
#if 0
                if (verbose > 1) {
                    data.first = 1;
                    data.label = "Key manager";
                }
                EVP_KEYMGMT_do_all_ex(NULL, do_keymgmt, &data);
                if (verbose > 1) {
                    data.first = 1;
                    data.label = "Key exchange";
                }
                EVP_KEYEXCH_do_all_ex(NULL, do_keyexch, &data);
#endif

                switch (verbose) {
                default:
                    break;
                case 2:
                case 1:
                    BIO_printf(bio_out, "\n");
                    break;
                }
            }
            OSSL_PROVIDER_unload(prov);
        } else {
            ERR_print_errors(bio_err);
            ret = 1;
            /*
             * Just because one provider module failed, there's no reason to
             * stop, if there are more to try.