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