Rename the field 'provctx and data' to 'algctx' inside some objects containing
[openssl.git] / crypto / provider_conf.c
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 "internal/provider.h"
16 #include "internal/cryptlib.h"
17
18 DEFINE_STACK_OF(OSSL_PROVIDER)
19
20 /* PROVIDER config module */
21
22 typedef struct {
23     STACK_OF(OSSL_PROVIDER) *activated_providers;
24 } PROVIDER_CONF_GLOBAL;
25
26 static void *prov_conf_ossl_ctx_new(OSSL_LIB_CTX *libctx)
27 {
28     PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl));
29
30     if (pcgbl == NULL)
31         return NULL;
32
33     return pcgbl;
34 }
35
36 static void prov_conf_ossl_ctx_free(void *vpcgbl)
37 {
38     PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl;
39
40     sk_OSSL_PROVIDER_pop_free(pcgbl->activated_providers,
41                               ossl_provider_free);
42
43     OSSL_TRACE(CONF, "Cleaned up providers\n");
44     OPENSSL_free(pcgbl);
45 }
46
47 static const OSSL_LIB_CTX_METHOD provider_conf_ossl_ctx_method = {
48     /* Must be freed before the provider store is freed */
49     OSSL_LIB_CTX_METHOD_PRIORITY_2,
50     prov_conf_ossl_ctx_new,
51     prov_conf_ossl_ctx_free,
52 };
53
54 static const char *skip_dot(const char *name)
55 {
56     const char *p = strchr(name, '.');
57
58     if (p != NULL)
59         return p + 1;
60     return name;
61 }
62
63 static int provider_conf_params(OSSL_PROVIDER *prov,
64                                 const char *name, const char *value,
65                                 const CONF *cnf)
66 {
67     STACK_OF(CONF_VALUE) *sect;
68     int ok = 1;
69
70     sect = NCONF_get_section(cnf, value);
71     if (sect != NULL) {
72         int i;
73         char buffer[512];
74         size_t buffer_len = 0;
75
76         OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
77
78         if (name != NULL) {
79             OPENSSL_strlcpy(buffer, name, sizeof(buffer));
80             OPENSSL_strlcat(buffer, ".", sizeof(buffer));
81             buffer_len = strlen(buffer);
82         }
83
84         for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
85             CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
86
87             if (buffer_len + strlen(sectconf->name) >= sizeof(buffer))
88                 return 0;
89             buffer[buffer_len] = '\0';
90             OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
91             if (!provider_conf_params(prov, buffer, sectconf->value, cnf))
92                 return 0;
93         }
94
95         OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
96     } else {
97         OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
98         ok = ossl_provider_add_parameter(prov, name, value);
99     }
100
101     return ok;
102 }
103
104 static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name,
105                               const char *value, const CONF *cnf)
106 {
107     int i;
108     STACK_OF(CONF_VALUE) *ecmds;
109     int soft = 0;
110     OSSL_PROVIDER *prov = NULL;
111     const char *path = NULL;
112     long activate = 0;
113     int ok = 0;
114     PROVIDER_CONF_GLOBAL *pcgbl
115         = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX,
116                                 &provider_conf_ossl_ctx_method);
117
118     name = skip_dot(name);
119     OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
120     /* Value is a section containing PROVIDER commands */
121     ecmds = NCONF_get_section(cnf, value);
122
123     if (!ecmds) {
124         ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
125                        "section=%s not found", value);
126         return 0;
127     }
128
129     /* Find the needed data first */
130     for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
131         CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
132         const char *confname = skip_dot(ecmd->name);
133         const char *confvalue = ecmd->value;
134
135         OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
136                     confname, confvalue);
137
138         /* First handle some special pseudo confs */
139
140         /* Override provider name to use */
141         if (strcmp(confname, "identity") == 0)
142             name = confvalue;
143         else if (strcmp(confname, "soft_load") == 0)
144             soft = 1;
145         /* Load a dynamic PROVIDER */
146         else if (strcmp(confname, "module") == 0)
147             path = confvalue;
148         else if (strcmp(confname, "activate") == 0)
149             activate = 1;
150     }
151
152     prov = ossl_provider_find(libctx, name, 1);
153     if (prov == NULL)
154         prov = ossl_provider_new(libctx, name, NULL, 1);
155     if (prov == NULL) {
156         if (soft)
157             ERR_clear_error();
158         return 0;
159     }
160
161     if (path != NULL)
162         ossl_provider_set_module_path(prov, path);
163
164     ok = provider_conf_params(prov, NULL, value, cnf);
165
166     if (ok && activate) {
167         if (!ossl_provider_activate(prov, 0, 1)) {
168             ok = 0;
169         } else {
170             if (pcgbl->activated_providers == NULL)
171                 pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null();
172             sk_OSSL_PROVIDER_push(pcgbl->activated_providers, prov);
173             ok = 1;
174         }
175     }
176
177     if (!(activate && ok))
178         ossl_provider_free(prov);
179
180     return ok;
181 }
182
183 static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
184 {
185     STACK_OF(CONF_VALUE) *elist;
186     CONF_VALUE *cval;
187     int i;
188
189     OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
190                 CONF_imodule_get_value(md));
191
192     /* Value is a section containing PROVIDERs to configure */
193     elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
194
195     if (!elist) {
196         ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
197         return 0;
198     }
199
200     for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
201         cval = sk_CONF_VALUE_value(elist, i);
202         if (!provider_conf_load(cnf->libctx, cval->name, cval->value, cnf))
203             return 0;
204     }
205
206     return 1;
207 }
208
209 void ossl_provider_add_conf_module(void)
210 {
211     OSSL_TRACE(CONF, "Adding config module 'providers'\n");
212     CONF_module_add("providers", provider_conf_init, NULL);
213 }