2 * Copyright 2019-2020 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"
17 DEFINE_STACK_OF(OSSL_PROVIDER)
18 DEFINE_STACK_OF(CONF_VALUE)
20 /* PROVIDER config module */
22 static STACK_OF(OSSL_PROVIDER) *activated_providers = NULL;
24 static const char *skip_dot(const char *name)
26 const char *p = strchr(name, '.');
33 static int provider_conf_params(OSSL_PROVIDER *prov,
34 const char *name, const char *value,
37 STACK_OF(CONF_VALUE) *sect;
40 sect = NCONF_get_section(cnf, value);
44 size_t buffer_len = 0;
46 OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
49 OPENSSL_strlcpy(buffer, name, sizeof(buffer));
50 OPENSSL_strlcat(buffer, ".", sizeof(buffer));
51 buffer_len = strlen(buffer);
54 for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
55 CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
57 if (buffer_len + strlen(sectconf->name) >= sizeof(buffer))
59 buffer[buffer_len] = '\0';
60 OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
61 if (!provider_conf_params(prov, buffer, sectconf->value, cnf))
65 OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
67 OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
68 ok = ossl_provider_add_parameter(prov, name, value);
74 static int provider_conf_load(OPENSSL_CTX *libctx, const char *name,
75 const char *value, const CONF *cnf)
78 STACK_OF(CONF_VALUE) *ecmds;
80 OSSL_PROVIDER *prov = NULL;
81 const char *path = NULL;
85 name = skip_dot(name);
86 OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
87 /* Value is a section containing PROVIDER commands */
88 ecmds = NCONF_get_section(cnf, value);
91 CRYPTOerr(CRYPTO_F_PROVIDER_CONF_LOAD, CRYPTO_R_PROVIDER_SECTION_ERROR);
95 /* Find the needed data first */
96 for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
97 CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
98 const char *confname = skip_dot(ecmd->name);
99 const char *confvalue = ecmd->value;
101 OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
102 confname, confvalue);
104 /* First handle some special pseudo confs */
106 /* Override provider name to use */
107 if (strcmp(confname, "identity") == 0)
109 else if (strcmp(confname, "soft_load") == 0)
111 /* Load a dynamic PROVIDER */
112 else if (strcmp(confname, "module") == 0)
114 else if (strcmp(confname, "activate") == 0)
118 prov = ossl_provider_find(libctx, name, 1);
120 prov = ossl_provider_new(libctx, name, NULL, 1);
128 ossl_provider_set_module_path(prov, path);
130 ok = provider_conf_params(prov, NULL, value, cnf);
132 if (ok && activate) {
133 if (!ossl_provider_activate(prov)) {
136 if (activated_providers == NULL)
137 activated_providers = sk_OSSL_PROVIDER_new_null();
138 sk_OSSL_PROVIDER_push(activated_providers, prov);
143 if (!(activate && ok))
144 ossl_provider_free(prov);
149 static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
151 STACK_OF(CONF_VALUE) *elist;
155 OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
156 CONF_imodule_get_value(md));
158 /* Value is a section containing PROVIDERs to configure */
159 elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
162 CRYPTOerr(CRYPTO_F_PROVIDER_CONF_INIT,
163 CRYPTO_R_PROVIDER_SECTION_ERROR);
167 for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
168 cval = sk_CONF_VALUE_value(elist, i);
169 if (!provider_conf_load(cnf->libctx, cval->name, cval->value, cnf))
177 static void provider_conf_deinit(CONF_IMODULE *md)
179 sk_OSSL_PROVIDER_pop_free(activated_providers, ossl_provider_free);
180 activated_providers = NULL;
181 OSSL_TRACE(CONF, "Cleaned up providers\n");
184 void ossl_provider_add_conf_module(void)
186 OSSL_TRACE(CONF, "Adding config module 'providers'\n");
187 CONF_module_add("providers", provider_conf_init, provider_conf_deinit);