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