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