2 * Copyright 2019-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
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"
18 DEFINE_STACK_OF(OSSL_PROVIDER
)
20 /* PROVIDER config module */
23 STACK_OF(OSSL_PROVIDER
) *activated_providers
;
24 } PROVIDER_CONF_GLOBAL
;
26 static void *prov_conf_ossl_ctx_new(OSSL_LIB_CTX
*libctx
)
28 PROVIDER_CONF_GLOBAL
*pcgbl
= OPENSSL_zalloc(sizeof(*pcgbl
));
36 static void prov_conf_ossl_ctx_free(void *vpcgbl
)
38 PROVIDER_CONF_GLOBAL
*pcgbl
= vpcgbl
;
40 sk_OSSL_PROVIDER_pop_free(pcgbl
->activated_providers
,
43 OSSL_TRACE(CONF
, "Cleaned up providers\n");
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
,
54 static const char *skip_dot(const char *name
)
56 const char *p
= strchr(name
, '.');
63 static int provider_conf_params(OSSL_PROVIDER
*prov
,
64 const char *name
, const char *value
,
67 STACK_OF(CONF_VALUE
) *sect
;
70 sect
= NCONF_get_section(cnf
, value
);
74 size_t buffer_len
= 0;
76 OSSL_TRACE1(CONF
, "Provider params: start section %s\n", value
);
79 OPENSSL_strlcpy(buffer
, name
, sizeof(buffer
));
80 OPENSSL_strlcat(buffer
, ".", sizeof(buffer
));
81 buffer_len
= strlen(buffer
);
84 for (i
= 0; i
< sk_CONF_VALUE_num(sect
); i
++) {
85 CONF_VALUE
*sectconf
= sk_CONF_VALUE_value(sect
, i
);
87 if (buffer_len
+ strlen(sectconf
->name
) >= sizeof(buffer
))
89 buffer
[buffer_len
] = '\0';
90 OPENSSL_strlcat(buffer
, sectconf
->name
, sizeof(buffer
));
91 if (!provider_conf_params(prov
, buffer
, sectconf
->value
, cnf
))
95 OSSL_TRACE1(CONF
, "Provider params: finish section %s\n", value
);
97 OSSL_TRACE2(CONF
, "Provider params: %s = %s\n", name
, value
);
98 ok
= ossl_provider_add_parameter(prov
, name
, value
);
104 static int provider_conf_load(OSSL_LIB_CTX
*libctx
, const char *name
,
105 const char *value
, const CONF
*cnf
)
108 STACK_OF(CONF_VALUE
) *ecmds
;
110 OSSL_PROVIDER
*prov
= NULL
;
111 const char *path
= NULL
;
114 PROVIDER_CONF_GLOBAL
*pcgbl
115 = ossl_lib_ctx_get_data(libctx
, OSSL_LIB_CTX_PROVIDER_CONF_INDEX
,
116 &provider_conf_ossl_ctx_method
);
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
);
124 ERR_raise_data(ERR_LIB_CRYPTO
, CRYPTO_R_PROVIDER_SECTION_ERROR
,
125 "section=%s not found", value
);
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
;
135 OSSL_TRACE2(CONF
, "Provider command: %s = %s\n",
136 confname
, confvalue
);
138 /* First handle some special pseudo confs */
140 /* Override provider name to use */
141 if (strcmp(confname
, "identity") == 0)
143 else if (strcmp(confname
, "soft_load") == 0)
145 /* Load a dynamic PROVIDER */
146 else if (strcmp(confname
, "module") == 0)
148 else if (strcmp(confname
, "activate") == 0)
152 prov
= ossl_provider_find(libctx
, name
, 1);
154 prov
= ossl_provider_new(libctx
, name
, NULL
, 1);
162 ossl_provider_set_module_path(prov
, path
);
164 ok
= provider_conf_params(prov
, NULL
, value
, cnf
);
166 if (ok
&& activate
) {
167 if (!ossl_provider_activate(prov
, 0, 1)) {
170 if (pcgbl
->activated_providers
== NULL
)
171 pcgbl
->activated_providers
= sk_OSSL_PROVIDER_new_null();
172 sk_OSSL_PROVIDER_push(pcgbl
->activated_providers
, prov
);
177 if (!(activate
&& ok
))
178 ossl_provider_free(prov
);
183 static int provider_conf_init(CONF_IMODULE
*md
, const CONF
*cnf
)
185 STACK_OF(CONF_VALUE
) *elist
;
189 OSSL_TRACE1(CONF
, "Loading providers module: section %s\n",
190 CONF_imodule_get_value(md
));
192 /* Value is a section containing PROVIDERs to configure */
193 elist
= NCONF_get_section(cnf
, CONF_imodule_get_value(md
));
196 ERR_raise(ERR_LIB_CRYPTO
, CRYPTO_R_PROVIDER_SECTION_ERROR
);
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
))
209 void ossl_provider_add_conf_module(void)
211 OSSL_TRACE(CONF
, "Adding config module 'providers'\n");
212 CONF_module_add("providers", provider_conf_init
, NULL
);