Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/crypto/provider_conf.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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 <openssl/provider.h>
16
#include "internal/provider.h"
17
#include "internal/cryptlib.h"
18
#include "internal/conf.h"
19
#include "provider_local.h"
20
#include "crypto/context.h"
21
22
DEFINE_STACK_OF(OSSL_PROVIDER)
23
24
/* PROVIDER config module */
25
26
typedef struct {
27
    CRYPTO_RWLOCK *lock;
28
    STACK_OF(OSSL_PROVIDER) *activated_providers;
29
} PROVIDER_CONF_GLOBAL;
30
31
void *ossl_prov_conf_ctx_new(OSSL_LIB_CTX *libctx)
32
429
{
33
429
    PROVIDER_CONF_GLOBAL *pcgbl = OPENSSL_zalloc(sizeof(*pcgbl));
34
35
429
    if (pcgbl == NULL)
36
0
        return NULL;
37
38
429
    pcgbl->lock = CRYPTO_THREAD_lock_new();
39
429
    if (pcgbl->lock == NULL) {
40
0
        OPENSSL_free(pcgbl);
41
0
        return NULL;
42
0
    }
43
44
429
    return pcgbl;
45
429
}
46
47
void ossl_prov_conf_ctx_free(void *vpcgbl)
48
154
{
49
154
    PROVIDER_CONF_GLOBAL *pcgbl = vpcgbl;
50
51
154
    sk_OSSL_PROVIDER_pop_free(pcgbl->activated_providers,
52
154
        ossl_provider_free);
53
54
154
    OSSL_TRACE(CONF, "Cleaned up providers\n");
55
154
    CRYPTO_THREAD_lock_free(pcgbl->lock);
56
154
    OPENSSL_free(pcgbl);
57
154
}
58
59
static const char *skip_dot(const char *name)
60
0
{
61
0
    const char *p = strchr(name, '.');
62
63
0
    if (p != NULL)
64
0
        return p + 1;
65
0
    return name;
66
0
}
67
68
/*
69
 * Parse the provider params section
70
 * Returns:
71
 * 1 for success
72
 * 0 for non-fatal errors
73
 * < 0 for fatal errors
74
 */
75
static int provider_conf_params_internal(OSSL_PROVIDER *prov,
76
    OSSL_PROVIDER_INFO *provinfo,
77
    const char *name, const char *value,
78
    const CONF *cnf,
79
    STACK_OF(OPENSSL_CSTRING) *visited)
80
0
{
81
0
    STACK_OF(CONF_VALUE) *sect;
82
0
    int ok = 1;
83
0
    int rc = 0;
84
85
0
    sect = NCONF_get_section(cnf, value);
86
0
    if (sect != NULL) {
87
0
        int i;
88
0
        char buffer[512];
89
0
        size_t buffer_len = 0;
90
91
0
        OSSL_TRACE1(CONF, "Provider params: start section %s\n", value);
92
93
        /*
94
         * Check to see if the provided section value has already
95
         * been visited.  If it has, then we have a recursive lookup
96
         * in the configuration which isn't valid.  As such we should error
97
         * out
98
         */
99
0
        for (i = 0; i < sk_OPENSSL_CSTRING_num(visited); i++) {
100
0
            if (sk_OPENSSL_CSTRING_value(visited, i) == value) {
101
0
                ERR_raise(ERR_LIB_CONF, CONF_R_RECURSIVE_SECTION_REFERENCE);
102
0
                return -1;
103
0
            }
104
0
        }
105
106
        /*
107
         * We've not visited this node yet, so record it on the stack
108
         */
109
0
        if (!sk_OPENSSL_CSTRING_push(visited, value))
110
0
            return -1;
111
112
0
        if (name != NULL) {
113
0
            OPENSSL_strlcpy(buffer, name, sizeof(buffer));
114
0
            OPENSSL_strlcat(buffer, ".", sizeof(buffer));
115
0
            buffer_len = strlen(buffer);
116
0
        }
117
118
0
        for (i = 0; i < sk_CONF_VALUE_num(sect); i++) {
119
0
            CONF_VALUE *sectconf = sk_CONF_VALUE_value(sect, i);
120
121
0
            if (buffer_len + strlen(sectconf->name) >= sizeof(buffer)) {
122
0
                sk_OPENSSL_CSTRING_pop(visited);
123
0
                return -1;
124
0
            }
125
0
            buffer[buffer_len] = '\0';
126
0
            OPENSSL_strlcat(buffer, sectconf->name, sizeof(buffer));
127
0
            rc = provider_conf_params_internal(prov, provinfo, buffer,
128
0
                sectconf->value, cnf, visited);
129
0
            if (rc < 0) {
130
0
                sk_OPENSSL_CSTRING_pop(visited);
131
0
                return rc;
132
0
            }
133
0
        }
134
0
        sk_OPENSSL_CSTRING_pop(visited);
135
136
0
        OSSL_TRACE1(CONF, "Provider params: finish section %s\n", value);
137
0
    } else {
138
0
        OSSL_TRACE2(CONF, "Provider params: %s = %s\n", name, value);
139
0
        if (prov != NULL)
140
0
            ok = OSSL_PROVIDER_add_conf_parameter(prov, name, value);
141
0
        else
142
0
            ok = ossl_provider_info_add_parameter(provinfo, name, value);
143
0
    }
144
145
0
    return ok;
146
0
}
147
148
/*
149
 * recursively parse the provider configuration section
150
 * of the config file.
151
 * Returns
152
 * 1 on success
153
 * 0 on non-fatal error
154
 * < 0 on fatal errors
155
 */
156
static int provider_conf_params(OSSL_PROVIDER *prov,
157
    OSSL_PROVIDER_INFO *provinfo,
158
    const char *name, const char *value,
159
    const CONF *cnf)
160
0
{
161
0
    int rc;
162
0
    STACK_OF(OPENSSL_CSTRING) *visited = sk_OPENSSL_CSTRING_new_null();
163
164
0
    if (visited == NULL)
165
0
        return -1;
166
167
0
    rc = provider_conf_params_internal(prov, provinfo, name,
168
0
        value, cnf, visited);
169
170
0
    sk_OPENSSL_CSTRING_free(visited);
171
172
0
    return rc;
173
0
}
174
175
static int prov_already_activated(const char *name,
176
    STACK_OF(OSSL_PROVIDER) *activated)
177
0
{
178
0
    int i, max;
179
180
0
    if (activated == NULL)
181
0
        return 0;
182
183
0
    max = sk_OSSL_PROVIDER_num(activated);
184
0
    for (i = 0; i < max; i++) {
185
0
        OSSL_PROVIDER *tstprov = sk_OSSL_PROVIDER_value(activated, i);
186
187
0
        if (strcmp(OSSL_PROVIDER_get0_name(tstprov), name) == 0) {
188
0
            return 1;
189
0
        }
190
0
    }
191
192
0
    return 0;
193
0
}
194
195
/*
196
 * Attempt to activate a provider
197
 * Returns:
198
 * 1 on successful activation
199
 * 0 on failed activation for non-fatal error
200
 * < 0 on failed activation for fatal errors
201
 */
202
static int provider_conf_activate(OSSL_LIB_CTX *libctx, const char *name,
203
    const char *value, const char *path,
204
    int soft, const CONF *cnf)
205
0
{
206
0
    PROVIDER_CONF_GLOBAL *pcgbl
207
0
        = ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_PROVIDER_CONF_INDEX);
208
0
    OSSL_PROVIDER *prov = NULL, *actual = NULL;
209
0
    int ok = 0;
210
211
0
    if (pcgbl == NULL || !CRYPTO_THREAD_write_lock(pcgbl->lock)) {
212
0
        ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
213
0
        return -1;
214
0
    }
215
0
    if (!prov_already_activated(name, pcgbl->activated_providers)) {
216
        /*
217
         * There is an attempt to activate a provider, so we should disable
218
         * loading of fallbacks. Otherwise a misconfiguration could mean the
219
         * intended provider does not get loaded. Subsequent fetches could
220
         * then fallback to the default provider - which may be the wrong
221
         * thing.
222
         */
223
0
        if (!ossl_provider_disable_fallback_loading(libctx)) {
224
0
            CRYPTO_THREAD_unlock(pcgbl->lock);
225
0
            ERR_raise(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR);
226
0
            return -1;
227
0
        }
228
0
        prov = ossl_provider_find(libctx, name, 1);
229
0
        if (prov == NULL)
230
0
            prov = ossl_provider_new(libctx, name, NULL, NULL, 1);
231
0
        if (prov == NULL) {
232
0
            CRYPTO_THREAD_unlock(pcgbl->lock);
233
0
            if (soft)
234
0
                ERR_clear_error();
235
0
            return (soft == 0) ? -1 : 0;
236
0
        }
237
238
0
        if (path != NULL)
239
0
            ossl_provider_set_module_path(prov, path);
240
241
0
        ok = provider_conf_params(prov, NULL, NULL, value, cnf);
242
243
0
        if (ok == 1) {
244
0
            if (!ossl_provider_activate(prov, 1, 0)) {
245
0
                ok = 0;
246
0
            } else if (!ossl_provider_add_to_store(prov, &actual, 0)) {
247
0
                ossl_provider_deactivate(prov, 1);
248
0
                ok = 0;
249
0
            } else if (actual != prov
250
0
                && !ossl_provider_activate(actual, 1, 0)) {
251
0
                ossl_provider_free(actual);
252
0
                ok = 0;
253
0
            } else {
254
0
                if (pcgbl->activated_providers == NULL)
255
0
                    pcgbl->activated_providers = sk_OSSL_PROVIDER_new_null();
256
0
                if (pcgbl->activated_providers == NULL
257
0
                    || !sk_OSSL_PROVIDER_push(pcgbl->activated_providers,
258
0
                        actual)) {
259
0
                    ossl_provider_deactivate(actual, 1);
260
0
                    ossl_provider_free(actual);
261
0
                    ok = 0;
262
0
                } else {
263
0
                    ok = 1;
264
0
                }
265
0
            }
266
0
        }
267
268
0
        if (ok <= 0)
269
0
            ossl_provider_free(prov);
270
0
    }
271
0
    CRYPTO_THREAD_unlock(pcgbl->lock);
272
273
0
    return ok;
274
0
}
275
276
static int provider_conf_parse_bool_setting(const char *confname,
277
    const char *confvalue, int *val)
278
0
{
279
0
    if (!ossl_conf_parse_bool(confvalue, val)) {
280
0
        ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
281
0
            "directive %s set to unrecognized value",
282
0
            confname);
283
0
        return 0;
284
0
    }
285
286
0
    return 1;
287
0
}
288
289
static int provider_conf_load(OSSL_LIB_CTX *libctx, const char *name,
290
    const char *value, const CONF *cnf)
291
0
{
292
0
    int i;
293
0
    STACK_OF(CONF_VALUE) *ecmds;
294
0
    int soft = 0;
295
0
    const char *path = NULL;
296
0
    int activate = 0;
297
0
    int ok = 0;
298
0
    int added = 0;
299
300
0
    name = skip_dot(name);
301
0
    OSSL_TRACE1(CONF, "Configuring provider %s\n", name);
302
    /* Value is a section containing PROVIDER commands */
303
0
    ecmds = NCONF_get_section(cnf, value);
304
305
0
    if (!ecmds) {
306
0
        ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR,
307
0
            "section=%s not found", value);
308
0
        return 0;
309
0
    }
310
311
    /* Find the needed data first */
312
0
    for (i = 0; i < sk_CONF_VALUE_num(ecmds); i++) {
313
0
        CONF_VALUE *ecmd = sk_CONF_VALUE_value(ecmds, i);
314
0
        const char *confname = skip_dot(ecmd->name);
315
0
        const char *confvalue = ecmd->value;
316
317
0
        OSSL_TRACE2(CONF, "Provider command: %s = %s\n",
318
0
            confname, confvalue);
319
320
        /* First handle some special pseudo confs */
321
322
        /* Override provider name to use */
323
0
        if (strcmp(confname, "identity") == 0) {
324
0
            name = confvalue;
325
0
        } else if (strcmp(confname, "soft_load") == 0) {
326
0
            if (!provider_conf_parse_bool_setting(confname,
327
0
                    confvalue, &soft))
328
0
                return 0;
329
            /* Load a dynamic PROVIDER */
330
0
        } else if (strcmp(confname, "module") == 0) {
331
0
            path = confvalue;
332
0
        } else if (strcmp(confname, "activate") == 0) {
333
0
            if (!provider_conf_parse_bool_setting(confname,
334
0
                    confvalue, &activate))
335
0
                return 0;
336
0
        }
337
0
    }
338
339
0
    if (activate) {
340
0
        ok = provider_conf_activate(libctx, name, value, path, soft, cnf);
341
0
    } else {
342
0
        OSSL_PROVIDER_INFO entry;
343
344
0
        memset(&entry, 0, sizeof(entry));
345
0
        ok = 1;
346
0
        if (name != NULL) {
347
0
            entry.name = OPENSSL_strdup(name);
348
0
            if (entry.name == NULL)
349
0
                ok = 0;
350
0
        }
351
0
        if (ok && path != NULL) {
352
0
            entry.path = OPENSSL_strdup(path);
353
0
            if (entry.path == NULL)
354
0
                ok = 0;
355
0
        }
356
0
        if (ok)
357
0
            ok = provider_conf_params(NULL, &entry, NULL, value, cnf);
358
0
        if (ok >= 1 && (entry.path != NULL || entry.parameters != NULL)) {
359
0
            ok = ossl_provider_info_add_to_store(libctx, &entry);
360
0
            added = ok;
361
0
        }
362
0
        if (added == 0)
363
0
            ossl_provider_info_clear(&entry);
364
0
    }
365
366
    /*
367
     * Provider activation returns a tristate:
368
     * 1 for successful activation
369
     * 0 for non-fatal activation failure
370
     * < 0 for fatal activation failure
371
     * We return success (1) for activation, (1) for non-fatal activation
372
     * failure, and (0) for fatal activation failure
373
     */
374
0
    return ok >= 0;
375
0
}
376
377
static int provider_conf_init(CONF_IMODULE *md, const CONF *cnf)
378
0
{
379
0
    STACK_OF(CONF_VALUE) *elist;
380
0
    CONF_VALUE *cval;
381
0
    int i;
382
383
0
    OSSL_TRACE1(CONF, "Loading providers module: section %s\n",
384
0
        CONF_imodule_get_value(md));
385
386
    /* Value is a section containing PROVIDERs to configure */
387
0
    elist = NCONF_get_section(cnf, CONF_imodule_get_value(md));
388
389
0
    if (!elist) {
390
0
        ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_PROVIDER_SECTION_ERROR);
391
0
        return 0;
392
0
    }
393
394
0
    for (i = 0; i < sk_CONF_VALUE_num(elist); i++) {
395
0
        cval = sk_CONF_VALUE_value(elist, i);
396
0
        if (!provider_conf_load(NCONF_get0_libctx((CONF *)cnf),
397
0
                cval->name, cval->value, cnf))
398
0
            return 0;
399
0
    }
400
401
0
    return 1;
402
0
}
403
404
void ossl_provider_add_conf_module(void)
405
0
{
406
0
    OSSL_TRACE(CONF, "Adding config module 'providers'\n");
407
    CONF_module_add("providers", provider_conf_init, NULL);
408
0
}