• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019-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 <string.h>
11 #include <openssl/trace.h>
12 #include <openssl/err.h>
13 #include <openssl/conf.h>
14 #include <openssl/safestack.h>
15 #include <openssl/provider.h>
16 #include "internal/provider.h"
17 #include "internal/cryptlib.h"
18 #include "provider_local.h"
19 
20 DEFINE_STACK_OF(OSSL_PROVIDER)
21 
22 /* PROVIDER config module */
23 
24 typedef struct {
25     CRYPTO_RWLOCK *lock;
26     STACK_OF(OSSL_PROVIDER) *activated_providers;
27 } PROVIDER_CONF_GLOBAL;
28 
prov_conf_ossl_ctx_new(OSSL_LIB_CTX * libctx)29 static void *prov_conf_ossl_ctx_new(OSSL_LIB_CTX *libctx)
30 {
31     PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl));
32 
33     if (pcgbl == NULL)
34         return NULL;
35 
36     pcgbl->lock = CRYPTO_THREAD_lock_new();
37     if (pcgbl->lock == NULL) {
38         OPENSSL_free(pcgbl);
39         return NULL;
40     }
41 
42     return pcgbl;
43 }
44 
prov_conf_ossl_ctx_free(void * vpcgbl)45 static void prov_conf_ossl_ctx_free(void *vpcgbl)
46 {
47     PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl;
48 
49     sk_OSSL_PROVIDER_pop_free(pcgbl->activated_providers,
50                               ossl_provider_free);
51 
52     OSSL_TRACE(CONF, "Cleaned up providers\n");
53     CRYPTO_THREAD_lock_free(pcgbl->lock);
54     OPENSSL_free(pcgbl);
55 }
56 
57 static const OSSL_LIB_CTX_METHOD provider_conf_ossl_ctx_method = {
58     /* Must be freed before the provider store is freed */
59     OSSL_LIB_CTX_METHOD_PRIORITY_2,
60     prov_conf_ossl_ctx_new,
61     prov_conf_ossl_ctx_free,
62 };
63 
skip_dot(const char * name)64 static const char *skip_dot(const char *name)
65 {
66     const char *p = strchr(name, '.');
67 
68     if (p != NULL)
69         return p + 1;
70     return name;
71 }
72 
provider_conf_params(OSSL_PROVIDER * prov,OSSL_PROVIDER_INFO * provinfo,const char * name,const char * value,const CONF * cnf)73 static int provider_conf_params(OSSL_PROVIDER *prov,
74                                 OSSL_PROVIDER_INFO *provinfo,
75                                 const char *name, const char *value,
76                                 const CONF *cnf)
77 {
78     STACK_OF(CONF_VALUE) *sect;
79     int ok = 1;
80 
81     sect = NCONF_get_section(cnf, value);
82     if (sect != NULL) {
83         int i;
84         char buffer[512];
85         size_t buffer_len = 0;
86 
87         OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
88 
89         if (name != NULL) {
90             OPENSSL_strlcpy(buffer, name, sizeof(buffer));
91             OPENSSL_strlcat(buffer, ".", sizeof(buffer));
92             buffer_len = strlen(buffer);
93         }
94 
95         for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
96             CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
97 
98             if (buffer_len + strlen(sectconf->name) >= sizeof(buffer))
99                 return 0;
100             buffer[buffer_len] = '\0';
101             OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
102             if (!provider_conf_params(prov, provinfo, buffer, sectconf->value,
103                                       cnf))
104                 return 0;
105         }
106 
107         OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
108     } else {
109         OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
110         if (prov != NULL)
111             ok = ossl_provider_add_parameter(prov, name, value);
112         else
113             ok = ossl_provider_info_add_parameter(provinfo, name, value);
114     }
115 
116     return ok;
117 }
118 
prov_already_activated(const char * name,STACK_OF (OSSL_PROVIDER)* activated)119 static int prov_already_activated(const char *name,
120                                   STACK_OF(OSSL_PROVIDER) *activated)
121 {
122     int i, max;
123 
124     if (activated == NULL)
125         return 0;
126 
127     max = sk_OSSL_PROVIDER_num(activated);
128     for (i = 0; i < max; i++) {
129         OSSL_PROVIDER *tstprov = sk_OSSL_PROVIDER_value(activated, i);
130 
131         if (strcmp(OSSL_PROVIDER_get0_name(tstprov), name) == 0) {
132             return 1;
133         }
134     }
135 
136     return 0;
137 }
138 
provider_conf_load(OSSL_LIB_CTX * libctx,const char * name,const char * value,const CONF * cnf)139 static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name,
140                               const char *value, const CONF *cnf)
141 {
142     int i;
143     STACK_OF(CONF_VALUE) *ecmds;
144     int soft = 0;
145     OSSL_PROVIDER *prov = NULL, *actual = NULL;
146     const char *path = NULL;
147     long activate = 0;
148     int ok = 0;
149 
150     name = skip_dot(name);
151     OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
152     /* Value is a section containing PROVIDER commands */
153     ecmds = NCONF_get_section(cnf, value);
154 
155     if (!ecmds) {
156         ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
157                        "section=%s not found", value);
158         return 0;
159     }
160 
161     /* Find the needed data first */
162     for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
163         CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
164         const char *confname = skip_dot(ecmd->name);
165         const char *confvalue = ecmd->value;
166 
167         OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
168                     confname, confvalue);
169 
170         /* First handle some special pseudo confs */
171 
172         /* Override provider name to use */
173         if (strcmp(confname, "identity") == 0)
174             name = confvalue;
175         else if (strcmp(confname, "soft_load") == 0)
176             soft = 1;
177         /* Load a dynamic PROVIDER */
178         else if (strcmp(confname, "module") == 0)
179             path = confvalue;
180         else if (strcmp(confname, "activate") == 0)
181             activate = 1;
182     }
183 
184     if (activate) {
185         PROVIDER_CONF_GLOBAL *pcgbl
186             = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX,
187                                     &provider_conf_ossl_ctx_method);
188 
189         if (pcgbl == NULL || !CRYPTO_THREAD_write_lock(pcgbl->lock)) {
190             ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
191             return 0;
192         }
193         if (!prov_already_activated(name, pcgbl->activated_providers)) {
194             /*
195             * There is an attempt to activate a provider, so we should disable
196             * loading of fallbacks. Otherwise a misconfiguration could mean the
197             * intended provider does not get loaded. Subsequent fetches could
198             * then fallback to the default provider - which may be the wrong
199             * thing.
200             */
201             if (!ossl_provider_disable_fallback_loading(libctx)) {
202                 CRYPTO_THREAD_unlock(pcgbl->lock);
203                 ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
204                 return 0;
205             }
206             prov = ossl_provider_find(libctx, name, 1);
207             if (prov == NULL)
208                 prov = ossl_provider_new(libctx, name, NULL, 1);
209             if (prov == NULL) {
210                 CRYPTO_THREAD_unlock(pcgbl->lock);
211                 if (soft)
212                     ERR_clear_error();
213                 return 0;
214             }
215 
216             if (path != NULL)
217                 ossl_provider_set_module_path(prov, path);
218 
219             ok = provider_conf_params(prov, NULL, NULL, value, cnf);
220 
221             if (ok) {
222                 if (!ossl_provider_activate(prov, 1, 0)) {
223                     ok = 0;
224                 } else if (!ossl_provider_add_to_store(prov, &actual, 0)) {
225                     ossl_provider_deactivate(prov, 1);
226                     ok = 0;
227                 } else if (actual != prov
228                            && !ossl_provider_activate(actual, 1, 0)) {
229                     ossl_provider_free(actual);
230                     ok = 0;
231                 } else {
232                     if (pcgbl->activated_providers == NULL)
233                         pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null();
234                     if (pcgbl->activated_providers == NULL
235                         || !sk_OSSL_PROVIDER_push(pcgbl->activated_providers,
236                                                   actual)) {
237                         ossl_provider_deactivate(actual, 1);
238                         ossl_provider_free(actual);
239                         ok = 0;
240                     } else {
241                         ok = 1;
242                     }
243                 }
244             }
245             if (!ok)
246                 ossl_provider_free(prov);
247         }
248         CRYPTO_THREAD_unlock(pcgbl->lock);
249     } else {
250         OSSL_PROVIDER_INFO entry;
251 
252         memset(&entry, 0, sizeof(entry));
253         ok = 1;
254         if (name != NULL) {
255             entry.name = OPENSSL_strdup(name);
256             if (entry.name == NULL) {
257                 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
258                 ok = 0;
259             }
260         }
261         if (ok && path != NULL) {
262             entry.path = OPENSSL_strdup(path);
263             if (entry.path == NULL) {
264                 ERR_raise(ERR_LIB_CRYPTO, ERR_R_MALLOC_FAILURE);
265                 ok = 0;
266             }
267         }
268         if (ok)
269             ok = provider_conf_params(NULL, &entry, NULL, value, cnf);
270         if (ok && (entry.path != NULL || entry.parameters != NULL))
271             ok = ossl_provider_info_add_to_store(libctx, &entry);
272         if (!ok || (entry.path == NULL && entry.parameters == NULL)) {
273             ossl_provider_info_clear(&entry);
274         }
275 
276     }
277 
278     /*
279      * Even if ok is 0, we still return success. Failure to load a provider is
280      * not fatal. We want to continue to load the rest of the config file.
281      */
282     return 1;
283 }
284 
provider_conf_init(CONF_IMODULE * md,const CONF * cnf)285 static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
286 {
287     STACK_OF(CONF_VALUE) *elist;
288     CONF_VALUE *cval;
289     int i;
290 
291     OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
292                 CONF_imodule_get_value(md));
293 
294     /* Value is a section containing PROVIDERs to configure */
295     elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
296 
297     if (!elist) {
298         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
299         return 0;
300     }
301 
302     for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
303         cval = sk_CONF_VALUE_value(elist, i);
304         if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf),
305                     cval->name, cval->value, cnf))
306             return 0;
307     }
308 
309     return 1;
310 }
311 
ossl_provider_add_conf_module(void)312 void ossl_provider_add_conf_module(void)
313 {
314     OSSL_TRACE(CONF, "Adding config module 'providers'\n");
315     CONF_module_add("providers", provider_conf_init, NULL);
316 }
317