2 * Copyright 2021 The OpenSSL Project Authors. All Rights Reserved.
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
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>
15 #include <openssl/rand.h>
16 #include <openssl/core_names.h>
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)
26 static const OSSL_DISPATCH dummy_decoder_functions[] = {
27 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_decoder_decode },
31 static const OSSL_ALGORITHM dummy_decoders[] = {
32 { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions },
36 static int dummy_encoder_encode(void *ctx, OSSL_CORE_BIO *out,
38 const OSSL_PARAM obj_abstract[], int selection,
39 OSSL_PASSPHRASE_CALLBACK *cb, void *cbarg)
44 static const OSSL_DISPATCH dummy_encoder_functions[] = {
45 { OSSL_FUNC_DECODER_DECODE, (void (*)(void))dummy_encoder_encode },
49 static const OSSL_ALGORITHM dummy_encoders[] = {
50 { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions },
54 static void *dummy_store_open(void *provctx, const char *uri)
59 static int dummy_store_load(void *loaderctx, OSSL_CALLBACK *object_cb,
60 void *object_cbarg, OSSL_PASSPHRASE_CALLBACK *pw_cb,
66 static int dumm_store_eof(void *loaderctx)
71 static int dummy_store_close(void *loaderctx)
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 },
84 static const OSSL_ALGORITHM dummy_store[] = {
85 { "DUMMY", "provider=dummy", dummy_store_functions },
89 static void *dummy_rand_newctx(void *provctx, void *parent,
90 const OSSL_DISPATCH *parent_calls)
95 static void dummy_rand_freectx(void *vctx)
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[])
107 static int dummy_rand_uninstantiate(void *vdrbg)
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)
118 for (i = 0; i <outlen; i++)
119 out[i] = (unsigned char)(i & 0xff);
124 static const OSSL_PARAM *dummy_rand_gettable_ctx_params(void *vctx, void *provctx)
126 static const OSSL_PARAM known_gettable_ctx_params[] = {
127 OSSL_PARAM_size_t(OSSL_RAND_PARAM_MAX_REQUEST, NULL),
130 return known_gettable_ctx_params;
133 static int dummy_rand_get_ctx_params(void *vctx, OSSL_PARAM params[])
137 p = OSSL_PARAM_locate(params, OSSL_RAND_PARAM_MAX_REQUEST);
138 if (p != NULL && !OSSL_PARAM_set_size_t(p, INT_MAX))
144 static int dummy_rand_enable_locking(void *vtest)
149 static int dummy_rand_lock(void *vtest)
154 static void dummy_rand_unlock(void *vtest)
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 },
173 static const OSSL_ALGORITHM dummy_rand[] = {
174 { "DUMMY", "provider=dummy", dummy_rand_functions },
178 static const OSSL_ALGORITHM *dummy_query(void *provctx, int operation_id,
182 switch (operation_id) {
183 case OSSL_OP_DECODER:
184 return dummy_decoders;
185 case OSSL_OP_ENCODER:
186 return dummy_encoders;
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 },
201 static int dummy_provider_init(const OSSL_CORE_HANDLE *handle,
202 const OSSL_DISPATCH *in,
203 const OSSL_DISPATCH **out,
206 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_child(handle, in);
207 unsigned char buf[32];
209 *provctx = (void *)libctx;
210 *out = dummy_dispatch_table;
213 * Do some work using the child libctx, to make sure this is possible from
214 * inside the init function.
216 if (!RAND_bytes_ex(libctx, buf, sizeof(buf), 0))
223 * Try fetching and freeing various things.
226 * Test 2: Store loader
229 static int fetch_test(int tst)
231 OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new();
232 OSSL_PROVIDER *dummyprov = NULL;
233 OSSL_PROVIDER *nullprov = NULL;
234 OSSL_DECODER *decoder = NULL;
235 OSSL_ENCODER *encoder = NULL;
236 OSSL_STORE_LOADER *loader = NULL;
238 unsigned char buf[32];
240 if (!TEST_ptr(libctx))
243 if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "dummy-prov",
244 dummy_provider_init))
245 || !TEST_ptr(nullprov = OSSL_PROVIDER_load(libctx, "default"))
246 || !TEST_ptr(dummyprov = OSSL_PROVIDER_load(libctx, "dummy-prov")))
251 decoder = OSSL_DECODER_fetch(libctx, "DUMMY", NULL);
252 if (!TEST_ptr(decoder))
256 encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", NULL);
257 if (!TEST_ptr(encoder))
261 loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", NULL);
262 if (!TEST_ptr(loader))
266 if (!TEST_true(RAND_set_DRBG_type(libctx, "DUMMY", NULL, NULL, NULL))
267 || !TEST_int_ge(RAND_bytes_ex(libctx, buf, sizeof(buf), 0), 1))
276 OSSL_DECODER_free(decoder);
277 OSSL_ENCODER_free(encoder);
278 OSSL_STORE_LOADER_free(loader);
279 OSSL_PROVIDER_unload(dummyprov);
280 OSSL_PROVIDER_unload(nullprov);
281 OSSL_LIB_CTX_free(libctx);
285 int setup_tests(void)
287 ADD_ALL_TESTS(fetch_test, 4);