Change OSSL_PARAM_UTF8_STRING_PTR to OSSL_PARAM_UTF8_PTR
[openssl.git] / crypto / provider_core.c
1 /*
2  * Copyright 2019 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/core.h>
11 #include <openssl/core_numbers.h>
12 #include <openssl/opensslv.h>
13 #include "internal/cryptlib.h"
14 #include "internal/thread_once.h"
15 #include "internal/provider.h"
16 #include "internal/refcount.h"
17
18 /*-
19  * Provider Object structure
20  * =========================
21  */
22
23 struct provider_store_st;        /* Forward declaration */
24
25 struct ossl_provider_st {
26     /* Flag bits */
27     unsigned int flag_initialized:1;
28
29     /* OpenSSL library side data */
30     CRYPTO_REF_COUNT refcnt;
31 #ifndef HAVE_ATOMICS
32     CRYPTO_RWLOCK refcnt_lock;   /* For the ref counter */
33 #endif
34     char *name;
35     DSO *module;
36     OSSL_provider_init_fn *init_function;
37
38     /* Provider side functions */
39     OSSL_provider_teardown_fn *teardown;
40     OSSL_provider_get_param_types_fn *get_param_types;
41     OSSL_provider_get_params_fn *get_params;
42 };
43 DEFINE_STACK_OF(OSSL_PROVIDER)
44
45 static int ossl_provider_cmp(const OSSL_PROVIDER * const *a,
46                              const OSSL_PROVIDER * const *b)
47 {
48     return strcmp((*a)->name, (*b)->name);
49 }
50
51 /*-
52  * Provider Object store
53  * =====================
54  *
55  * The Provider Object store is a library context object, and therefore needs
56  * an index.
57  */
58
59 struct provider_store_st {
60     STACK_OF(OSSL_PROVIDER) *providers;
61     CRYPTO_RWLOCK *lock;
62 };
63 static int provider_store_index = -1;
64
65 static void provider_store_free(void *vstore)
66 {
67     struct provider_store_st *store = vstore;
68
69     if (store == NULL)
70         return;
71     sk_OSSL_PROVIDER_pop_free(store->providers, ossl_provider_free);
72     CRYPTO_THREAD_lock_free(store->lock);
73     OPENSSL_free(store);
74 }
75
76 static void *provider_store_new(void)
77 {
78     struct provider_store_st *store = OPENSSL_zalloc(sizeof(*store));
79
80     if (store == NULL
81         || (store->providers = sk_OSSL_PROVIDER_new(ossl_provider_cmp)) == NULL
82         || (store->lock = CRYPTO_THREAD_lock_new()) == NULL) {
83         provider_store_free(store);
84         store = NULL;
85     }
86     return store;
87 }
88
89 static const OPENSSL_CTX_METHOD provider_store_method = {
90     provider_store_new,
91     provider_store_free,
92 };
93
94 static CRYPTO_ONCE provider_store_init_flag = CRYPTO_ONCE_STATIC_INIT;
95 DEFINE_RUN_ONCE_STATIC(do_provider_store_init)
96 {
97     return OPENSSL_init_crypto(0, NULL)
98         && (provider_store_index =
99             openssl_ctx_new_index(&provider_store_method)) != -1;
100 }
101
102
103 static struct provider_store_st *get_provider_store(OPENSSL_CTX *libctx)
104 {
105     struct provider_store_st *store = NULL;
106
107     if (!RUN_ONCE(&provider_store_init_flag, do_provider_store_init))
108         return NULL;
109
110     store = openssl_ctx_get_data(libctx, provider_store_index);
111     if (store == NULL)
112         CRYPTOerr(CRYPTO_F_GET_PROVIDER_STORE, ERR_R_INTERNAL_ERROR);
113     return store;
114 }
115
116 /*-
117  * Provider Object methods
118  * =======================
119  */
120
121 int ossl_provider_upref(OSSL_PROVIDER *prov)
122 {
123     int ref = 0;
124
125 #ifndef HAVE_ATOMICS
126     CRYPTO_UP_REF(&prov->refcnt, &ref, prov->refcnt_lock);
127 #else
128     CRYPTO_UP_REF(&prov->refcnt, &ref, NULL);
129 #endif
130     return ref;
131 }
132
133 /* Finder, constructor and destructor */
134 OSSL_PROVIDER *ossl_provider_find(OPENSSL_CTX *libctx, const char *name)
135 {
136     struct provider_store_st *store = NULL;
137     OSSL_PROVIDER *prov = NULL;
138
139     if ((store = get_provider_store(libctx)) != NULL) {
140         OSSL_PROVIDER tmpl = { 0, };
141         int i;
142
143         tmpl.name = (char *)name;
144         CRYPTO_THREAD_write_lock(store->lock);
145         if ((i = sk_OSSL_PROVIDER_find(store->providers, &tmpl)) == -1
146             || (prov = sk_OSSL_PROVIDER_value(store->providers, i)) == NULL
147             || !ossl_provider_upref(prov))
148             prov = NULL;
149         CRYPTO_THREAD_unlock(store->lock);
150     }
151
152     return prov;
153 }
154
155 OSSL_PROVIDER *ossl_provider_new(OPENSSL_CTX *libctx, const char *name,
156                                  OSSL_provider_init_fn *init_function)
157 {
158     struct provider_store_st *store = NULL;
159     OSSL_PROVIDER *prov = NULL;
160
161     if ((store = get_provider_store(libctx)) == NULL)
162         return NULL;
163
164     if ((prov = ossl_provider_find(libctx, name)) != NULL) { /* refcount +1 */
165         ossl_provider_free(prov); /* refcount -1 */
166         CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW,
167                   CRYPTO_R_PROVIDER_ALREADY_EXISTS);
168         ERR_add_error_data(2, "name=", name);
169         return NULL;
170     }
171
172     if ((prov = OPENSSL_zalloc(sizeof(*prov))) == NULL
173         || !ossl_provider_upref(prov) /* +1 One reference to be returned */
174         || (prov->name = OPENSSL_strdup(name)) == NULL) {
175         ossl_provider_free(prov);
176         CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
177         return NULL;
178     }
179
180     prov->init_function = init_function;
181
182     CRYPTO_THREAD_write_lock(store->lock);
183     if (!ossl_provider_upref(prov)) { /* +1 One reference for the store */
184         ossl_provider_free(prov); /* -1 Reference that was to be returned */
185         prov = NULL;
186     } else if (sk_OSSL_PROVIDER_push(store->providers, prov) == 0) {
187         ossl_provider_free(prov); /* -1 Store reference */
188         ossl_provider_free(prov); /* -1 Reference that was to be returned */
189         prov = NULL;
190     }
191     CRYPTO_THREAD_unlock(store->lock);
192
193     if (prov == NULL)
194         CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_NEW, ERR_R_MALLOC_FAILURE);
195
196     /*
197      * At this point, the provider is only partially "loaded".  To be
198      * fully "loaded", ossl_provider_activate() must also be called.
199      */
200
201     return prov;
202 }
203
204 void ossl_provider_free(OSSL_PROVIDER *prov)
205 {
206     if (prov != NULL) {
207         int ref = 0;
208
209 #ifndef HAVE_ATOMICS
210         CRYPTO_DOWN_REF(&prov->refcnt, &ref, provider_lock);
211 #else
212         CRYPTO_DOWN_REF(&prov->refcnt, &ref, NULL);
213 #endif
214
215         /*
216          * When the refcount drops down to one, there is only one reference,
217          * the store.
218          * When that happens, the provider is inactivated.
219          */
220         if (ref == 1 && prov->flag_initialized) {
221             if (prov->teardown != NULL)
222                 prov->teardown();
223             prov->flag_initialized = 0;
224         }
225
226         /*
227          * When the refcount drops to zero, it has been taken out of
228          * the store.  All we have to do here is clean it out.
229          */
230         if (ref == 0) {
231             DSO_free(prov->module);
232             OPENSSL_free(prov->name);
233             OPENSSL_free(prov);
234         }
235     }
236 }
237
238 /*
239  * Provider activation.
240  *
241  * What "activation" means depends on the provider form; for built in
242  * providers (in the library or the application alike), the provider
243  * can already be considered to be loaded, all that's needed is to
244  * initialize it.  However, for dynamically loadable provider modules,
245  * we must first load that module.
246  *
247  * Built in modules are distinguished from dynamically loaded modules
248  * with an already assigned init function.
249  */
250 static const OSSL_DISPATCH *core_dispatch; /* Define further down */
251
252 int ossl_provider_activate(OSSL_PROVIDER *prov)
253 {
254     const OSSL_DISPATCH *provider_dispatch = NULL;
255
256     if (prov->flag_initialized)
257         return 1;
258
259     /*
260      * If the init function isn't set, it indicates that this provider is
261      * a loadable module.
262      */
263     if (prov->init_function == NULL) {
264         if (prov->module == NULL) {
265             char *platform_module_name = NULL;
266             char *module_path = NULL;
267             const char *load_dir = ossl_safe_getenv("OPENSSL_MODULES");
268
269             if ((prov->module = DSO_new()) == NULL) {
270                 /* DSO_new() generates an error already */
271                 return 0;
272             }
273
274             if (load_dir == NULL)
275                 load_dir = MODULESDIR;
276
277             DSO_ctrl(prov->module, DSO_CTRL_SET_FLAGS,
278                      DSO_FLAG_NAME_TRANSLATION_EXT_ONLY, NULL);
279             if ((platform_module_name =
280                  DSO_convert_filename(prov->module, prov->name)) == NULL
281                 || (module_path =
282                     DSO_merge(prov->module, platform_module_name,
283                               load_dir)) == NULL
284                 || DSO_load(prov->module, module_path, NULL,
285                             DSO_FLAG_NAME_TRANSLATION_EXT_ONLY) == NULL) {
286                 DSO_free(prov->module);
287                 prov->module = NULL;
288             }
289
290             OPENSSL_free(platform_module_name);
291             OPENSSL_free(module_path);
292         }
293
294         if (prov->module != NULL)
295             prov->init_function = (OSSL_provider_init_fn *)
296                 DSO_bind_func(prov->module, "OSSL_provider_init");
297     }
298
299     if (prov->init_function == NULL
300         || !prov->init_function(prov, core_dispatch, &provider_dispatch)) {
301         CRYPTOerr(CRYPTO_F_OSSL_PROVIDER_ACTIVATE, ERR_R_INIT_FAIL);
302         ERR_add_error_data(2, "name=", prov->name);
303         DSO_free(prov->module);
304         prov->module = NULL;
305         return 0;
306     }
307
308     for (; provider_dispatch->function_id != 0; provider_dispatch++) {
309         switch (provider_dispatch->function_id) {
310         case OSSL_FUNC_PROVIDER_TEARDOWN:
311             prov->teardown =
312                 OSSL_get_provider_teardown(provider_dispatch);
313             break;
314         case OSSL_FUNC_PROVIDER_GET_PARAM_TYPES:
315             prov->get_param_types =
316                 OSSL_get_provider_get_param_types(provider_dispatch);
317             break;
318         case OSSL_FUNC_PROVIDER_GET_PARAMS:
319             prov->get_params =
320                 OSSL_get_provider_get_params(provider_dispatch);
321             break;
322         }
323     }
324
325     /* With this flag set, this provider has become fully "loaded". */
326     prov->flag_initialized = 1;
327
328     return 1;
329 }
330
331 /* Getters of Provider Object data */
332 const char *ossl_provider_name(OSSL_PROVIDER *prov)
333 {
334     return prov->name;
335 }
336
337 const DSO *ossl_provider_dso(OSSL_PROVIDER *prov)
338 {
339     return prov->module;
340 }
341
342 const char *ossl_provider_module_name(OSSL_PROVIDER *prov)
343 {
344     return DSO_get_filename(prov->module);
345 }
346
347 const char *ossl_provider_module_path(OSSL_PROVIDER *prov)
348 {
349     /* FIXME: Ensure it's a full path */
350     return DSO_get_filename(prov->module);
351 }
352
353 /* Wrappers around calls to the provider */
354 void ossl_provider_teardown(const OSSL_PROVIDER *prov)
355 {
356     if (prov->teardown != NULL)
357         prov->teardown();
358 }
359
360 const OSSL_ITEM *ossl_provider_get_param_types(const OSSL_PROVIDER *prov)
361 {
362     return prov->get_param_types == NULL ? NULL : prov->get_param_types(prov);
363 }
364
365 int ossl_provider_get_params(const OSSL_PROVIDER *prov,
366                              const OSSL_PARAM params[])
367 {
368     return prov->get_params == NULL ? 0 : prov->get_params(prov, params);
369 }
370
371 /*-
372  * Core functions for the provider
373  * ===============================
374  *
375  * This is the set of functions that the core makes available to the provider
376  */
377
378 /*
379  * This returns a list of Provider Object parameters with their types, for
380  * discovery.  We do not expect that many providers will use this, but one
381  * never knows.
382  */
383 static const OSSL_ITEM param_types[] = {
384     { OSSL_PARAM_UTF8_PTR, "openssl-version" },
385     { OSSL_PARAM_UTF8_PTR, "provider-name" },
386     { 0, NULL }
387 };
388
389 static const OSSL_ITEM *core_get_param_types(const OSSL_PROVIDER *prov)
390 {
391     return param_types;
392 }
393
394 static int core_get_params(const OSSL_PROVIDER *prov, const OSSL_PARAM params[])
395 {
396     int i;
397
398     for (i = 0; params[i].key != NULL; i++) {
399         if (strcmp(params[i].key, "openssl-version") == 0) {
400             *(void **)params[i].data = OPENSSL_VERSION_STR;
401             if (params[i].return_size)
402                 *params[i].return_size = sizeof(OPENSSL_VERSION_STR);
403         } else if (strcmp(params[i].key, "provider-name") == 0) {
404             *(void **)params[i].data = prov->name;
405             if (params[i].return_size)
406                 *params[i].return_size = strlen(prov->name) + 1;
407         }
408     }
409
410     return 1;
411 }
412
413 static const OSSL_DISPATCH core_dispatch_[] = {
414     { OSSL_FUNC_CORE_GET_PARAM_TYPES, (void (*)(void))core_get_param_types },
415     { OSSL_FUNC_CORE_GET_PARAMS, (void (*)(void))core_get_params },
416     { 0, NULL }
417 };
418 static const OSSL_DISPATCH *core_dispatch = core_dispatch_;