1 /*
2  * Copyright 2021 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 #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>
17 #include "testutil.h"
18 
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 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 },
28     { 0, NULL }
29 };
30 
31 static const OSSL_ALGORITHM dummy_decoders[] = {
32     { "DUMMY", "provider=dummy,input=pem", dummy_decoder_functions },
33     { NULL, NULL, NULL }
34 };
35 
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 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 },
46     { 0, NULL }
47 };
48 
49 static const OSSL_ALGORITHM dummy_encoders[] = {
50     { "DUMMY", "provider=dummy,output=pem", dummy_encoder_functions },
51     { NULL, NULL, NULL }
52 };
53 
dummy_store_open(void * provctx,const char * uri)54 static void *dummy_store_open(void *provctx, const char *uri)
55 {
56     return NULL;
57 }
58 
dummy_store_load(void * loaderctx,OSSL_CALLBACK * object_cb,void * object_cbarg,OSSL_PASSPHRASE_CALLBACK * pw_cb,void * pw_cbarg)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 
dumm_store_eof(void * loaderctx)66 static int dumm_store_eof(void *loaderctx)
67 {
68     return 0;
69 }
70 
dummy_store_close(void * loaderctx)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 },
81     { 0, NULL }
82 };
83 
84 static const OSSL_ALGORITHM dummy_store[] = {
85     { "DUMMY", "provider=dummy", dummy_store_functions },
86     { NULL, NULL, NULL }
87 };
88 
dummy_rand_newctx(void * provctx,void * parent,const OSSL_DISPATCH * parent_calls)89 static void *dummy_rand_newctx(void *provctx, void *parent,
90                                const OSSL_DISPATCH *parent_calls)
91 {
92     return provctx;
93 }
94 
dummy_rand_freectx(void * vctx)95 static void dummy_rand_freectx(void *vctx)
96 {
97 }
98 
dummy_rand_instantiate(void * vdrbg,unsigned int strength,int prediction_resistance,const unsigned char * pstr,size_t pstr_len,const OSSL_PARAM params[])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 
dummy_rand_uninstantiate(void * vdrbg)107 static int dummy_rand_uninstantiate(void *vdrbg)
108 {
109     return 1;
110 }
111 
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 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 
dummy_rand_gettable_ctx_params(void * vctx,void * provctx)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 
dummy_rand_get_ctx_params(void * vctx,OSSL_PARAM params[])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 
dummy_rand_enable_locking(void * vtest)144 static int dummy_rand_enable_locking(void *vtest)
145 {
146     return 1;
147 }
148 
dummy_rand_lock(void * vtest)149 static int dummy_rand_lock(void *vtest)
150 {
151     return 1;
152 }
153 
dummy_rand_unlock(void * vtest)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 },
170     { 0, NULL }
171 };
172 
173 static const OSSL_ALGORITHM dummy_rand[] = {
174     { "DUMMY", "provider=dummy", dummy_rand_functions },
175     { NULL, NULL, NULL }
176 };
177 
dummy_query(void * provctx,int operation_id,int * no_cache)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;
189     case OSSL_OP_RAND:
190         return dummy_rand;
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 },
198     { 0, NULL }
199 };
200 
dummy_provider_init(const OSSL_CORE_HANDLE * handle,const OSSL_DISPATCH * in,const OSSL_DISPATCH ** out,void ** provctx)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);
207     unsigned char buf[32];
208 
209     *provctx = (void *)libctx;
210     *out = dummy_dispatch_table;
211 
212     /*
213      * Do some work using the child libctx, to make sure this is possible from
214      * inside the init function.
215      */
216     if (!RAND_bytes_ex(libctx, buf, sizeof(buf), 0))
217         return 0;
218 
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
227  * Test 3: EVP_RAND
228  */
fetch_test(int tst)229 static int fetch_test(int tst)
230 {
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;
237     int testresult = 0;
238     unsigned char buf[32];
239 
240     if (!TEST_ptr(libctx))
241         goto err;
242 
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")))
247         goto err;
248 
249     switch(tst) {
250     case 0:
251         decoder = OSSL_DECODER_fetch(libctx, "DUMMY", NULL);
252         if (!TEST_ptr(decoder))
253             goto err;
254         break;
255     case 1:
256         encoder = OSSL_ENCODER_fetch(libctx, "DUMMY", NULL);
257         if (!TEST_ptr(encoder))
258             goto err;
259         break;
260     case 2:
261         loader = OSSL_STORE_LOADER_fetch(libctx, "DUMMY", NULL);
262         if (!TEST_ptr(loader))
263             goto err;
264         break;
265     case 3:
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))
268             goto err;
269         break;
270     default:
271         goto err;
272     }
273 
274     testresult = 1;
275  err:
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);
282     return testresult;
283 }
284 
setup_tests(void)285 int setup_tests(void)
286 {
287     ADD_ALL_TESTS(fetch_test, 4);
288 
289     return 1;
290 }
291