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