]>
Commit | Line | Data |
---|---|---|
3cd69b74 MC |
1 | /* |
2 | * Copyright 2020 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 | /* | |
11 | * A filtering provider for test purposes. We pass all calls through to the | |
12 | * default provider except where we want other behaviour for a test. | |
13 | */ | |
14 | ||
15 | #include <string.h> | |
16 | #include <openssl/core.h> | |
23c48d94 | 17 | #include <openssl/core_dispatch.h> |
3cd69b74 MC |
18 | #include <openssl/provider.h> |
19 | #include <openssl/crypto.h> | |
20 | ||
21 | OSSL_provider_init_fn filter_provider_init; | |
22 | ||
23 | int filter_provider_set_filter(int operation, const char *name); | |
24 | ||
25 | #define MAX_FILTERS 10 | |
26 | #define MAX_ALG_FILTERS 5 | |
27 | ||
28 | struct filter_prov_globals_st { | |
b4250010 | 29 | OSSL_LIB_CTX *libctx; |
3cd69b74 MC |
30 | OSSL_PROVIDER *deflt; |
31 | struct { | |
32 | int operation; | |
33 | OSSL_ALGORITHM alg[MAX_ALG_FILTERS + 1]; | |
34 | } dispatch[MAX_FILTERS]; | |
35 | int num_dispatch; | |
ca2c778c | 36 | int no_cache; |
3cd69b74 MC |
37 | }; |
38 | ||
39 | static struct filter_prov_globals_st ourglobals; | |
40 | ||
41 | static struct filter_prov_globals_st *get_globals(void) | |
42 | { | |
43 | /* | |
b4250010 | 44 | * Ideally we'd like to store this in the OSSL_LIB_CTX so that we can have |
3cd69b74 MC |
45 | * more than one instance of the filter provider at a time. But for now we |
46 | * just make it simple. | |
47 | */ | |
48 | return &ourglobals; | |
49 | } | |
50 | ||
363b1e5d DMSP |
51 | static OSSL_FUNC_provider_gettable_params_fn filter_gettable_params; |
52 | static OSSL_FUNC_provider_get_params_fn filter_get_params; | |
53 | static OSSL_FUNC_provider_query_operation_fn filter_query; | |
54 | static OSSL_FUNC_provider_teardown_fn filter_teardown; | |
3cd69b74 MC |
55 | |
56 | static const OSSL_PARAM *filter_gettable_params(void *provctx) | |
57 | { | |
58 | struct filter_prov_globals_st *globs = get_globals(); | |
59 | ||
60 | return OSSL_PROVIDER_gettable_params(globs->deflt); | |
61 | } | |
62 | ||
63 | static int filter_get_params(void *provctx, OSSL_PARAM params[]) | |
64 | { | |
65 | struct filter_prov_globals_st *globs = get_globals(); | |
66 | ||
67 | return OSSL_PROVIDER_get_params(globs->deflt, params); | |
68 | } | |
69 | ||
9d2d857f MC |
70 | static int filter_get_capabilities(void *provctx, const char *capability, |
71 | OSSL_CALLBACK *cb, void *arg) | |
72 | { | |
73 | struct filter_prov_globals_st *globs = get_globals(); | |
74 | ||
75 | return OSSL_PROVIDER_get_capabilities(globs->deflt, capability, cb, arg); | |
76 | } | |
77 | ||
3cd69b74 MC |
78 | static const OSSL_ALGORITHM *filter_query(void *provctx, |
79 | int operation_id, | |
80 | int *no_cache) | |
81 | { | |
82 | struct filter_prov_globals_st *globs = get_globals(); | |
83 | int i; | |
84 | ||
85 | for (i = 0; i < globs->num_dispatch; i++) { | |
86 | if (globs->dispatch[i].operation == operation_id) { | |
ca2c778c | 87 | *no_cache = globs->no_cache; |
3cd69b74 MC |
88 | return globs->dispatch[i].alg; |
89 | } | |
90 | } | |
91 | ||
92 | /* No filter set, so pass it down to the chained provider */ | |
93 | return OSSL_PROVIDER_query_operation(globs->deflt, operation_id, no_cache); | |
94 | } | |
95 | ||
96 | static void filter_teardown(void *provctx) | |
97 | { | |
98 | struct filter_prov_globals_st *globs = get_globals(); | |
99 | ||
100 | OSSL_PROVIDER_unload(globs->deflt); | |
b4250010 | 101 | OSSL_LIB_CTX_free(globs->libctx); |
3cd69b74 MC |
102 | } |
103 | ||
104 | /* Functions we provide to the core */ | |
105 | static const OSSL_DISPATCH filter_dispatch_table[] = { | |
106 | { OSSL_FUNC_PROVIDER_GETTABLE_PARAMS, (void (*)(void))filter_gettable_params }, | |
107 | { OSSL_FUNC_PROVIDER_GET_PARAMS, (void (*)(void))filter_get_params }, | |
108 | { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))filter_query }, | |
9d2d857f | 109 | { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))filter_get_capabilities }, |
3cd69b74 MC |
110 | { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))filter_teardown }, |
111 | { 0, NULL } | |
112 | }; | |
113 | ||
114 | int filter_provider_init(const OSSL_CORE_HANDLE *handle, | |
115 | const OSSL_DISPATCH *in, | |
116 | const OSSL_DISPATCH **out, | |
117 | void **provctx) | |
118 | { | |
119 | memset(&ourglobals, 0, sizeof(ourglobals)); | |
b4250010 | 120 | ourglobals.libctx = OSSL_LIB_CTX_new(); |
3cd69b74 MC |
121 | if (ourglobals.libctx == NULL) |
122 | goto err; | |
123 | ||
124 | ourglobals.deflt = OSSL_PROVIDER_load(ourglobals.libctx, "default"); | |
125 | if (ourglobals.deflt == NULL) | |
126 | goto err; | |
127 | ||
128 | *provctx = OSSL_PROVIDER_get0_provider_ctx(ourglobals.deflt); | |
129 | *out = filter_dispatch_table; | |
130 | return 1; | |
131 | ||
132 | err: | |
133 | OSSL_PROVIDER_unload(ourglobals.deflt); | |
b4250010 | 134 | OSSL_LIB_CTX_free(ourglobals.libctx); |
3cd69b74 MC |
135 | return 0; |
136 | } | |
137 | ||
138 | /* | |
139 | * Set a filter for the given operation id. The filter string is a colon | |
140 | * separated list of algorithms that will be made available by this provider. | |
141 | * Anything not in the filter will be suppressed. If a filter is not set for | |
142 | * a given operation id then all algorithms are made available. | |
143 | */ | |
144 | int filter_provider_set_filter(int operation, const char *filterstr) | |
145 | { | |
146 | int no_cache = 0; | |
147 | int algnum = 0, last = 0, ret = 0; | |
148 | struct filter_prov_globals_st *globs = get_globals(); | |
149 | size_t namelen; | |
150 | char *filterstrtmp = OPENSSL_strdup(filterstr); | |
151 | char *name, *sep; | |
152 | const OSSL_ALGORITHM *provalgs = OSSL_PROVIDER_query_operation(globs->deflt, | |
153 | operation, | |
154 | &no_cache); | |
155 | const OSSL_ALGORITHM *algs; | |
156 | ||
157 | if (filterstrtmp == NULL) | |
158 | goto err; | |
159 | ||
3cd69b74 MC |
160 | /* Nothing to filter */ |
161 | if (provalgs == NULL) | |
162 | goto err; | |
163 | ||
164 | if (globs->num_dispatch >= MAX_FILTERS) | |
165 | goto err; | |
166 | ||
a07dc816 | 167 | for (name = filterstrtmp; !last; name = (sep == NULL ? NULL : sep + 1)) { |
3cd69b74 MC |
168 | sep = strstr(name, ":"); |
169 | if (sep != NULL) | |
170 | *sep = '\0'; | |
171 | else | |
172 | last = 1; | |
173 | namelen = strlen(name); | |
174 | ||
175 | for (algs = provalgs; algs->algorithm_names != NULL; algs++) { | |
176 | const char *found = strstr(algs->algorithm_names, name); | |
177 | ||
178 | if (found == NULL) | |
179 | continue; | |
180 | if (found[namelen] != '\0' && found[namelen] != ':') | |
181 | continue; | |
182 | if (found != algs->algorithm_names && found[-1] != ':') | |
183 | continue; | |
184 | ||
185 | /* We found a match */ | |
186 | if (algnum >= MAX_ALG_FILTERS) | |
187 | goto err; | |
188 | ||
189 | globs->dispatch[globs->num_dispatch].alg[algnum++] = *algs; | |
190 | break; | |
191 | } | |
192 | if (algs->algorithm_names == NULL) { | |
193 | /* No match found */ | |
194 | goto err; | |
195 | } | |
196 | } | |
197 | ||
198 | globs->dispatch[globs->num_dispatch].operation = operation; | |
ca2c778c | 199 | globs->no_cache = no_cache; |
3cd69b74 MC |
200 | globs->num_dispatch++; |
201 | ||
202 | ret = 1; | |
203 | err: | |
204 | OPENSSL_free(filterstrtmp); | |
205 | return ret; | |
206 | } |