]>
Commit | Line | Data |
---|---|---|
d66ff761 | 1 | /* |
da1c088f | 2 | * Copyright 2021-2023 The OpenSSL Project Authors. All Rights Reserved. |
d66ff761 MC |
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/crypto.h> | |
11 | #include <openssl/provider.h> | |
12 | #include <openssl/decoder.h> | |
13 | #include <openssl/encoder.h> | |
14 | #include <openssl/store.h> | |
86f4e260 MC |
15 | #include <openssl/rand.h> |
16 | #include <openssl/core_names.h> | |
d66ff761 MC |
17 | #include "testutil.h" |
18 | ||
19 | static int dummy_decoder_decode(void *ctx, OSSL_CORE_BIO *cin, int selection, | |
20 | OSSL_CALLBACK *object_cb, void *object_cbarg, | |
21 | OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg) | |
22 | { | |
23 | return 0; | |
24 | } | |
25 | ||
26 | static const OSSL_DISPATCH dummy_decoder_functions[] = { | |
27 | { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode }, | |
1e6bd31e | 28 | OSSL_DISPATCH_END |
d66ff761 MC |
29 | }; |
30 | ||
31 | static const OSSL_ALGORITHM dummy_decoders[] = { | |
32 | { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions }, | |
33 | { NULL, NULL, NULL } | |
34 | }; | |
35 | ||
36 | static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out, | |
37 | const void *obj_raw, | |
38 | const OSSL_PARAM obj_abstract[], int selection, | |
39 | OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg) | |
40 | { | |
41 | return 0; | |
42 | } | |
43 | ||
44 | static const OSSL_DISPATCH dummy_encoder_functions[] = { | |
45 | { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode }, | |
1e6bd31e | 46 | OSSL_DISPATCH_END |
d66ff761 MC |
47 | }; |
48 | ||
49 | static const OSSL_ALGORITHM dummy_encoders[] = { | |
50 | { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions }, | |
51 | { NULL, NULL, NULL } | |
52 | }; | |
53 | ||
54 | static void *dummy_store_open(void *provctx, const char *uri) | |
55 | { | |
56 | return NULL; | |
57 | } | |
58 | ||
59 | static int dummy_store_load(void *loaderctx, OSSL_CALLBACK *object_cb, | |
60 | void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb, | |
61 | void *pw_cbarg) | |
62 | { | |
63 | return 0; | |
64 | } | |
65 | ||
66 | static int dumm_store_eof(void *loaderctx) | |
67 | { | |
68 | return 0; | |
69 | } | |
70 | ||
71 | static int dummy_store_close(void *loaderctx) | |
72 | { | |
73 | return 0; | |
74 | } | |
75 | ||
76 | static const OSSL_DISPATCH dummy_store_functions[] = { | |
77 | { OSSL_FUNC_STORE_OPEN, (void (*)(void))dummy_store_open }, | |
78 | { OSSL_FUNC_STORE_LOAD, (void (*)(void))dummy_store_load }, | |
79 | { OSSL_FUNC_STORE_EOF, (void (*)(void))dumm_store_eof }, | |
80 | { OSSL_FUNC_STORE_CLOSE, (void (*)(void))dummy_store_close }, | |
1e6bd31e | 81 | OSSL_DISPATCH_END |
d66ff761 MC |
82 | }; |
83 | ||
84 | static const OSSL_ALGORITHM dummy_store[] = { | |
85 | { "DUMMY", "provider=dummy", dummy_store_functions }, | |
86 | { NULL, NULL, NULL } | |
87 | }; | |
88 | ||
86f4e260 MC |
89 | static void *dummy_rand_newctx(void *provctx, void *parent, |
90 | const OSSL_DISPATCH *parent_calls) | |
91 | { | |
92 | return provctx; | |
93 | } | |
94 | ||
95 | static void dummy_rand_freectx(void *vctx) | |
96 | { | |
97 | } | |
98 | ||
99 | static int dummy_rand_instantiate(void *vdrbg, unsigned int strength, | |
100 | int prediction_resistance, | |
101 | const unsigned char *pstr, size_t pstr_len, | |
102 | const OSSL_PARAM params[]) | |
103 | { | |
104 | return 1; | |
105 | } | |
106 | ||
107 | static int dummy_rand_uninstantiate(void *vdrbg) | |
108 | { | |
109 | return 1; | |
110 | } | |
111 | ||
112 | static int dummy_rand_generate(void *vctx, unsigned char *out, size_t outlen, | |
113 | unsigned int strength, int prediction_resistance, | |
114 | const unsigned char *addin, size_t addin_len) | |
115 | { | |
116 | size_t i; | |
117 | ||
118 | for (i = 0; i <outlen; i++) | |
119 | out[i] = (unsigned char)(i & 0xff); | |
120 | ||
121 | return 1; | |
122 | } | |
123 | ||
124 | static const OSSL_PARAM *dummy_rand_gettable_ctx_params(void *vctx, void *provctx) | |
125 | { | |
126 | static const OSSL_PARAM known_gettable_ctx_params[] = { | |
127 | OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL), | |
128 | OSSL_PARAM_END | |
129 | }; | |
130 | return known_gettable_ctx_params; | |
131 | } | |
132 | ||
133 | static int dummy_rand_get_ctx_params(void *vctx, OSSL_PARAM params[]) | |
134 | { | |
135 | OSSL_PARAM *p; | |
136 | ||
137 | p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST); | |
138 | if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX)) | |
139 | return 0; | |
140 | ||
141 | return 1; | |
142 | } | |
143 | ||
144 | static int dummy_rand_enable_locking(void *vtest) | |
145 | { | |
146 | return 1; | |
147 | } | |
148 | ||
149 | static int dummy_rand_lock(void *vtest) | |
150 | { | |
151 | return 1; | |
152 | } | |
153 | ||
154 | static void dummy_rand_unlock(void *vtest) | |
155 | { | |
156 | } | |
157 | ||
158 | static const OSSL_DISPATCH dummy_rand_functions[] = { | |
159 | { OSSL_FUNC_RAND_NEWCTX, (void (*)(void))dummy_rand_newctx }, | |
160 | { OSSL_FUNC_RAND_FREECTX, (void (*)(void))dummy_rand_freectx }, | |
161 | { OSSL_FUNC_RAND_INSTANTIATE, (void (*)(void))dummy_rand_instantiate }, | |
162 | { OSSL_FUNC_RAND_UNINSTANTIATE, (void (*)(void))dummy_rand_uninstantiate }, | |
163 | { OSSL_FUNC_RAND_GENERATE, (void (*)(void))dummy_rand_generate }, | |
164 | { OSSL_FUNC_RAND_GETTABLE_CTX_PARAMS, | |
165 | (void(*)(void))dummy_rand_gettable_ctx_params }, | |
166 | { OSSL_FUNC_RAND_GET_CTX_PARAMS, (void(*)(void))dummy_rand_get_ctx_params }, | |
167 | { OSSL_FUNC_RAND_ENABLE_LOCKING, (void(*)(void))dummy_rand_enable_locking }, | |
168 | { OSSL_FUNC_RAND_LOCK, (void(*)(void))dummy_rand_lock }, | |
169 | { OSSL_FUNC_RAND_UNLOCK, (void(*)(void))dummy_rand_unlock }, | |
1e6bd31e | 170 | OSSL_DISPATCH_END |
86f4e260 MC |
171 | }; |
172 | ||
173 | static const OSSL_ALGORITHM dummy_rand[] = { | |
174 | { "DUMMY", "provider=dummy", dummy_rand_functions }, | |
175 | { NULL, NULL, NULL } | |
176 | }; | |
177 | ||
d66ff761 MC |
178 | static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id, |
179 | int *no_cache) | |
180 | { | |
181 | *no_cache = 0; | |
182 | switch (operation_id) { | |
183 | case OSSL_OP_DECODER: | |
184 | return dummy_decoders; | |
185 | case OSSL_OP_ENCODER: | |
186 | return dummy_encoders; | |
187 | case OSSL_OP_STORE: | |
188 | return dummy_store; | |
86f4e260 MC |
189 | case OSSL_OP_RAND: |
190 | return dummy_rand; | |
d66ff761 MC |
191 | } |
192 | return NULL; | |
193 | } | |
194 | ||
195 | static const OSSL_DISPATCH dummy_dispatch_table[] = { | |
196 | { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))dummy_query }, | |
197 | { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))OSSL_LIB_CTX_free }, | |
1e6bd31e | 198 | OSSL_DISPATCH_END |
d66ff761 MC |
199 | }; |
200 | ||
201 | static int dummy_provider_init(const OSSL_CORE_HANDLE *handle, | |
202 | const OSSL_DISPATCH *in, | |
203 | const OSSL_DISPATCH **out, | |
204 | void **provctx) | |
205 | { | |
206 | OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in); | |
dcbb2be7 | 207 | unsigned char buf[32]; |
d66ff761 MC |
208 | |
209 | *provctx = (void *)libctx; | |
210 | *out = dummy_dispatch_table; | |
211 | ||
dcbb2be7 MC |
212 | /* |
213 | * Do some work using the child libctx, to make sure this is possible from | |
214 | * inside the init function. | |
215 | */ | |
c9007bda | 216 | if (RAND_bytes_ex(libctx, buf, sizeof(buf), 0) <= 0) |
dcbb2be7 MC |
217 | return 0; |
218 | ||
d66ff761 MC |
219 | return 1; |
220 | } | |
221 | ||
222 | /* | |
223 | * Try fetching and freeing various things. | |
224 | * Test 0: Decoder | |
225 | * Test 1: Encoder | |
226 | * Test 2: Store loader | |
86f4e260 | 227 | * Test 3: EVP_RAND |
f5e97b37 | 228 | * Test 4-7: As above, but additionally with a query string |
d66ff761 MC |
229 | */ |
230 | static int fetch_test(int tst) | |
231 | { | |
232 | OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new(); | |
233 | OSSL_PROVIDER *dummyprov = NULL; | |
dcbb2be7 | 234 | OSSL_PROVIDER *nullprov = NULL; |
d66ff761 MC |
235 | OSSL_DECODER *decoder = NULL; |
236 | OSSL_ENCODER *encoder = NULL; | |
237 | OSSL_STORE_LOADER *loader = NULL; | |
238 | int testresult = 0; | |
86f4e260 | 239 | unsigned char buf[32]; |
f5e97b37 | 240 | int query = tst > 3; |
d66ff761 MC |
241 | |
242 | if (!TEST_ptr(libctx)) | |
243 | goto err; | |
244 | ||
245 | if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov", | |
246 | dummy_provider_init)) | |
dcbb2be7 | 247 | || !TEST_ptr(nullprov = OSSL_PROVIDER_load(libctx, "default")) |
d66ff761 MC |
248 | || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov"))) |
249 | goto err; | |
250 | ||
f5e97b37 | 251 | switch (tst % 4) { |
d66ff761 | 252 | case 0: |
f5e97b37 MC |
253 | decoder = OSSL_DECODER_fetch(libctx, "DUMMY", |
254 | query ? "provider=dummy" : NULL); | |
d66ff761 MC |
255 | if (!TEST_ptr(decoder)) |
256 | goto err; | |
257 | break; | |
258 | case 1: | |
f5e97b37 MC |
259 | encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", |
260 | query ? "provider=dummy" : NULL); | |
d66ff761 MC |
261 | if (!TEST_ptr(encoder)) |
262 | goto err; | |
263 | break; | |
264 | case 2: | |
f5e97b37 MC |
265 | loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", |
266 | query ? "provider=dummy" : NULL); | |
d66ff761 MC |
267 | if (!TEST_ptr(loader)) |
268 | goto err; | |
269 | break; | |
86f4e260 | 270 | case 3: |
f5e97b37 MC |
271 | if (!TEST_true(RAND_set_DRBG_type(libctx, "DUMMY", |
272 | query ? "provider=dummy" : NULL, | |
273 | NULL, NULL)) | |
86f4e260 MC |
274 | || !TEST_int_ge(RAND_bytes_ex(libctx, buf, sizeof(buf), 0), 1)) |
275 | goto err; | |
276 | break; | |
d66ff761 MC |
277 | default: |
278 | goto err; | |
279 | } | |
280 | ||
281 | testresult = 1; | |
282 | err: | |
283 | OSSL_DECODER_free(decoder); | |
284 | OSSL_ENCODER_free(encoder); | |
285 | OSSL_STORE_LOADER_free(loader); | |
286 | OSSL_PROVIDER_unload(dummyprov); | |
dcbb2be7 | 287 | OSSL_PROVIDER_unload(nullprov); |
d66ff761 MC |
288 | OSSL_LIB_CTX_free(libctx); |
289 | return testresult; | |
290 | } | |
291 | ||
292 | int setup_tests(void) | |
293 | { | |
f5e97b37 | 294 | ADD_ALL_TESTS(fetch_test, 8); |
d66ff761 MC |
295 | |
296 | return 1; | |
297 | } |