Coverage Report

Created: 2025-08-11 07:04

/src/openssl30/providers/common/provider_util.c
Line
Count
Source (jump to first uncovered line)
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
/* We need to use some engine deprecated APIs */
11
#define OPENSSL_SUPPRESS_DEPRECATED
12
13
#include <openssl/evp.h>
14
#include <openssl/core_names.h>
15
#include <openssl/err.h>
16
#include <openssl/proverr.h>
17
#ifndef FIPS_MODULE
18
# include <openssl/engine.h>
19
# include "crypto/evp.h"
20
#endif
21
#include "prov/provider_util.h"
22
#include "internal/nelem.h"
23
24
void ossl_prov_cipher_reset(PROV_CIPHER *pc)
25
13.8k
{
26
13.8k
    EVP_CIPHER_free(pc->alloc_cipher);
27
13.8k
    pc->alloc_cipher = NULL;
28
13.8k
    pc->cipher = NULL;
29
13.8k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30
13.8k
    ENGINE_finish(pc->engine);
31
13.8k
#endif
32
13.8k
    pc->engine = NULL;
33
13.8k
}
34
35
int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
36
0
{
37
0
    if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
38
0
        return 0;
39
0
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
40
0
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
41
0
        EVP_CIPHER_free(src->alloc_cipher);
42
0
        return 0;
43
0
    }
44
0
#endif
45
0
    dst->engine = src->engine;
46
0
    dst->cipher = src->cipher;
47
0
    dst->alloc_cipher = src->alloc_cipher;
48
0
    return 1;
49
0
}
50
51
static int load_common(const OSSL_PARAM params[], const char **propquery,
52
                       ENGINE **engine)
53
1.21M
{
54
1.21M
    const OSSL_PARAM *p;
55
56
1.21M
    *propquery = NULL;
57
1.21M
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58
1.21M
    if (p != NULL) {
59
3.41k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
60
0
            return 0;
61
3.41k
        *propquery = p->data;
62
3.41k
    }
63
64
1.21M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65
1.21M
    ENGINE_finish(*engine);
66
1.21M
#endif
67
1.21M
    *engine = NULL;
68
    /* Inside the FIPS module, we don't support legacy ciphers */
69
1.21M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70
1.21M
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71
1.21M
    if (p != NULL) {
72
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
73
0
            return 0;
74
        /* Get a structural reference */
75
0
        *engine = ENGINE_by_id(p->data);
76
0
        if (*engine == NULL)
77
0
            return 0;
78
        /* Get a functional reference */
79
0
        if (!ENGINE_init(*engine)) {
80
0
            ENGINE_free(*engine);
81
0
            *engine = NULL;
82
0
            return 0;
83
0
        }
84
        /* Free the structural reference */
85
0
        ENGINE_free(*engine);
86
0
    }
87
1.21M
#endif
88
1.21M
    return 1;
89
1.21M
}
90
91
int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
92
                                      const OSSL_PARAM params[],
93
                                      OSSL_LIB_CTX *ctx)
94
99
{
95
99
    const OSSL_PARAM *p;
96
99
    const char *propquery;
97
98
99
    if (params == NULL)
99
0
        return 1;
100
101
99
    if (!load_common(params, &propquery, &pc->engine))
102
0
        return 0;
103
104
99
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
105
99
    if (p == NULL)
106
0
        return 1;
107
99
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
108
0
        return 0;
109
110
99
    EVP_CIPHER_free(pc->alloc_cipher);
111
99
    ERR_set_mark();
112
99
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113
99
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114
99
    if (pc->cipher == NULL) {
115
34
        const EVP_CIPHER *cipher;
116
117
34
        cipher = EVP_get_cipherbyname(p->data);
118
        /* Do not use global EVP_CIPHERs */
119
34
        if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
120
0
            pc->cipher = cipher;
121
34
    }
122
99
#endif
123
99
    if (pc->cipher != NULL)
124
65
        ERR_pop_to_mark();
125
34
    else
126
34
        ERR_clear_last_mark();
127
99
    return pc->cipher != NULL;
128
99
}
129
130
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
131
345
{
132
345
    return pc->cipher;
133
345
}
134
135
ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
136
232
{
137
232
    return pc->engine;
138
232
}
139
140
void ossl_prov_digest_reset(PROV_DIGEST *pd)
141
2.46M
{
142
2.46M
    EVP_MD_free(pd->alloc_md);
143
2.46M
    pd->alloc_md = NULL;
144
2.46M
    pd->md = NULL;
145
2.46M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146
2.46M
    ENGINE_finish(pd->engine);
147
2.46M
#endif
148
2.46M
    pd->engine = NULL;
149
2.46M
}
150
151
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152
1.05M
{
153
1.05M
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154
0
        return 0;
155
1.05M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156
1.05M
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
157
0
        EVP_MD_free(src->alloc_md);
158
0
        return 0;
159
0
    }
160
1.05M
#endif
161
1.05M
    dst->engine = src->engine;
162
1.05M
    dst->md = src->md;
163
1.05M
    dst->alloc_md = src->alloc_md;
164
1.05M
    return 1;
165
1.05M
}
166
167
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168
                           const char *mdname, const char *propquery)
169
1.40M
{
170
1.40M
    EVP_MD_free(pd->alloc_md);
171
1.40M
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172
173
1.40M
    return pd->md;
174
1.40M
}
175
176
int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177
                                      const OSSL_PARAM params[],
178
                                      OSSL_LIB_CTX *ctx)
179
906k
{
180
906k
    const OSSL_PARAM *p;
181
906k
    const char *propquery;
182
183
906k
    if (params == NULL)
184
0
        return 1;
185
186
906k
    if (!load_common(params, &propquery, &pd->engine))
187
0
        return 0;
188
189
906k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190
906k
    if (p == NULL)
191
59.9k
        return 1;
192
846k
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
193
0
        return 0;
194
195
846k
    ERR_set_mark();
196
846k
    ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197
846k
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198
846k
    if (pd->md == NULL) {
199
406
        const EVP_MD *md;
200
201
406
        md = EVP_get_digestbyname(p->data);
202
        /* Do not use global EVP_MDs */
203
406
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204
0
            pd->md = md;
205
406
    }
206
846k
#endif
207
846k
    if (pd->md != NULL)
208
845k
        ERR_pop_to_mark();
209
406
    else
210
406
        ERR_clear_last_mark();
211
846k
    return pd->md != NULL;
212
846k
}
213
214
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
215
2.22M
{
216
2.22M
    return pd->md;
217
2.22M
}
218
219
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
220
227k
{
221
227k
    return pd->engine;
222
227k
}
223
224
int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
225
                         const OSSL_PARAM params[],
226
                         const char *ciphername,
227
                         const char *mdname,
228
                         const char *engine,
229
                         const char *properties,
230
                         const unsigned char *key,
231
                         size_t keylen)
232
59.7k
{
233
59.7k
    const OSSL_PARAM *p;
234
59.7k
    OSSL_PARAM mac_params[6], *mp = mac_params;
235
236
59.7k
    if (params != NULL) {
237
49.4k
        if (mdname == NULL) {
238
30.5k
            if ((p = OSSL_PARAM_locate_const(params,
239
30.5k
                                            OSSL_ALG_PARAM_DIGEST)) != NULL) {
240
30.5k
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
241
0
                    return 0;
242
30.5k
                mdname = p->data;
243
30.5k
            }
244
30.5k
        }
245
49.4k
        if (ciphername == NULL) {
246
49.4k
            if ((p = OSSL_PARAM_locate_const(params,
247
49.4k
                                            OSSL_ALG_PARAM_CIPHER)) != NULL) {
248
101
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
249
0
                    return 0;
250
101
                ciphername = p->data;
251
101
            }
252
49.4k
        }
253
49.4k
        if (engine == NULL) {
254
49.4k
            if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
255
49.4k
                    != NULL) {
256
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
257
0
                    return 0;
258
0
                engine = p->data;
259
0
            }
260
49.4k
        }
261
49.4k
    }
262
263
59.7k
    if (mdname != NULL)
264
59.7k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
265
59.7k
                                                 (char *)mdname, 0);
266
59.7k
    if (ciphername != NULL)
267
101
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
268
101
                                                 (char *)ciphername, 0);
269
59.7k
    if (properties != NULL)
270
1.65k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
271
1.65k
                                                 (char *)properties, 0);
272
273
59.7k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
274
59.7k
    if (engine != NULL)
275
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
276
0
                                                 (char *) engine, 0);
277
59.7k
#endif
278
279
59.7k
    if (key != NULL)
280
0
        *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
281
0
                                                  (unsigned char *)key,
282
0
                                                  keylen);
283
284
59.7k
    *mp = OSSL_PARAM_construct_end();
285
286
59.7k
    return EVP_MAC_CTX_set_params(macctx, mac_params);
287
288
59.7k
}
289
290
int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
291
                                      const OSSL_PARAM params[],
292
                                      const char *macname,
293
                                      const char *ciphername,
294
                                      const char *mdname,
295
                                      OSSL_LIB_CTX *libctx)
296
49.6k
{
297
49.6k
    const OSSL_PARAM *p;
298
49.6k
    const char *properties = NULL;
299
300
49.6k
    if (macname == NULL
301
49.6k
        && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
302
333
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
303
0
            return 0;
304
333
        macname = p->data;
305
333
    }
306
49.6k
    if ((p = OSSL_PARAM_locate_const(params,
307
49.6k
                                     OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
308
1.78k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
309
0
            return 0;
310
1.78k
        properties = p->data;
311
1.78k
    }
312
313
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
314
49.6k
    if (macname != NULL) {
315
49.6k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
316
317
49.6k
        EVP_MAC_CTX_free(*macctx);
318
49.6k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
319
        /* The context holds on to the MAC */
320
49.6k
        EVP_MAC_free(mac);
321
49.6k
        if (*macctx == NULL)
322
134
            return 0;
323
49.6k
    }
324
325
    /*
326
     * If there is no MAC yet (and therefore, no MAC context), we ignore
327
     * all other parameters.
328
     */
329
49.4k
    if (*macctx == NULL)
330
0
        return 1;
331
332
49.4k
    if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
333
49.4k
                             properties, NULL, 0))
334
48.8k
        return 1;
335
336
584
    EVP_MAC_CTX_free(*macctx);
337
584
    *macctx = NULL;
338
584
    return 0;
339
49.4k
}
340
341
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
342
                                         OSSL_ALGORITHM *out)
343
88
{
344
88
    int i, j;
345
346
88
    if (out[0].algorithm_names == NULL) {
347
11.2k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
348
11.1k
            if (in[i].capable == NULL || in[i].capable())
349
11.0k
                out[j++] = in[i].alg;
350
11.1k
        }
351
85
        out[j++] = in[i].alg;
352
85
    }
353
88
}