Coverage Report

Created: 2026-04-01 06:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl35/providers/common/provider_util.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2025 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/providercommon.h"
22
#include "prov/provider_util.h"
23
24
void ossl_prov_cipher_reset(PROV_CIPHER *pc)
25
22.5k
{
26
22.5k
    EVP_CIPHER_free(pc->alloc_cipher);
27
22.5k
    pc->alloc_cipher = NULL;
28
22.5k
    pc->cipher = NULL;
29
22.5k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30
22.5k
    ENGINE_finish(pc->engine);
31
22.5k
#endif
32
22.5k
    pc->engine = NULL;
33
22.5k
}
34
35
int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
36
258
{
37
258
    if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
38
0
        return 0;
39
258
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
40
258
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
41
0
        EVP_CIPHER_free(src->alloc_cipher);
42
0
        return 0;
43
0
    }
44
258
#endif
45
258
    dst->engine = src->engine;
46
258
    dst->cipher = src->cipher;
47
258
    dst->alloc_cipher = src->alloc_cipher;
48
258
    return 1;
49
258
}
50
51
static int load_common(const OSSL_PARAM params[], const char **propquery,
52
    ENGINE **engine)
53
1.18M
{
54
1.18M
    const OSSL_PARAM *p;
55
56
1.18M
    *propquery = NULL;
57
1.18M
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58
1.18M
    if (p != NULL) {
59
5.09k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
60
0
            return 0;
61
5.09k
        *propquery = p->data;
62
5.09k
    }
63
64
1.18M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65
1.18M
    ENGINE_finish(*engine);
66
1.18M
#endif
67
1.18M
    *engine = NULL;
68
    /* Inside the FIPS module, we don't support legacy ciphers */
69
1.18M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70
1.18M
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71
1.18M
    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.18M
#endif
88
1.18M
    return 1;
89
1.18M
}
90
91
int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
92
    const OSSL_PARAM params[],
93
    OSSL_LIB_CTX *ctx)
94
132
{
95
132
    const OSSL_PARAM *p;
96
132
    const char *propquery;
97
98
132
    if (ossl_param_is_empty(params))
99
0
        return 1;
100
101
132
    if (!load_common(params, &propquery, &pc->engine))
102
0
        return 0;
103
104
132
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
105
132
    if (p == NULL)
106
0
        return 1;
107
132
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
108
0
        return 0;
109
110
132
    EVP_CIPHER_free(pc->alloc_cipher);
111
132
    ERR_set_mark();
112
132
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113
132
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114
132
    if (pc->cipher == NULL) {
115
43
        const EVP_CIPHER *cipher;
116
117
43
        cipher = EVP_get_cipherbyname(p->data);
118
        /* Do not use global EVP_CIPHERs */
119
43
        if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
120
0
            pc->cipher = cipher;
121
43
    }
122
132
#endif
123
132
    if (pc->cipher != NULL)
124
89
        ERR_pop_to_mark();
125
43
    else
126
43
        ERR_clear_last_mark();
127
132
    return pc->cipher != NULL;
128
132
}
129
130
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
131
1.49k
{
132
1.49k
    return pc->cipher;
133
1.49k
}
134
135
ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
136
634
{
137
634
    return pc->engine;
138
634
}
139
140
void ossl_prov_digest_reset(PROV_DIGEST *pd)
141
3.35M
{
142
3.35M
    EVP_MD_free(pd->alloc_md);
143
3.35M
    pd->alloc_md = NULL;
144
3.35M
    pd->md = NULL;
145
3.35M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146
3.35M
    ENGINE_finish(pd->engine);
147
3.35M
#endif
148
3.35M
    pd->engine = NULL;
149
3.35M
}
150
151
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152
1.38M
{
153
1.38M
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154
0
        return 0;
155
1.38M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156
1.38M
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
157
0
        EVP_MD_free(src->alloc_md);
158
0
        return 0;
159
0
    }
160
1.38M
#endif
161
1.38M
    dst->engine = src->engine;
162
1.38M
    dst->md = src->md;
163
1.38M
    dst->alloc_md = src->alloc_md;
164
1.38M
    return 1;
165
1.38M
}
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.69M
{
170
1.69M
    EVP_MD_free(pd->alloc_md);
171
1.69M
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172
173
1.69M
    return pd->md;
174
1.69M
}
175
176
int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177
    const OSSL_PARAM params[],
178
    OSSL_LIB_CTX *ctx)
179
383k
{
180
383k
    const OSSL_PARAM *p;
181
383k
    const char *propquery;
182
183
383k
    if (ossl_param_is_empty(params))
184
0
        return 1;
185
186
383k
    if (!load_common(params, &propquery, &pd->engine))
187
0
        return 0;
188
189
383k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190
383k
    if (p == NULL)
191
48.5k
        return 1;
192
335k
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
193
0
        return 0;
194
195
335k
    ERR_set_mark();
196
335k
    ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197
335k
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198
335k
    if (pd->md == NULL) {
199
550
        const EVP_MD *md;
200
201
550
        md = EVP_get_digestbyname(p->data);
202
        /* Do not use global EVP_MDs */
203
550
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204
0
            pd->md = md;
205
550
    }
206
335k
#endif
207
335k
    if (pd->md != NULL)
208
334k
        ERR_pop_to_mark();
209
550
    else
210
550
        ERR_clear_last_mark();
211
335k
    return pd->md != NULL;
212
335k
}
213
214
void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md)
215
26
{
216
26
    ossl_prov_digest_reset(pd);
217
26
    pd->md = pd->alloc_md = md;
218
26
}
219
220
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
221
3.07M
{
222
3.07M
    return pd->md;
223
3.07M
}
224
225
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
226
240k
{
227
240k
    return pd->engine;
228
240k
}
229
230
int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
231
    const OSSL_PARAM params[],
232
    const char *ciphername,
233
    const char *mdname,
234
    const char *engine,
235
    const char *properties,
236
    const unsigned char *key,
237
    size_t keylen)
238
82.8k
{
239
82.8k
    const OSSL_PARAM *p;
240
82.8k
    OSSL_PARAM mac_params[6], *mp = mac_params;
241
242
82.8k
    if (params != NULL) {
243
68.4k
        if (mdname == NULL) {
244
36.2k
            if ((p = OSSL_PARAM_locate_const(params,
245
36.2k
                     OSSL_ALG_PARAM_DIGEST))
246
36.2k
                != NULL) {
247
36.2k
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
248
0
                    return 0;
249
36.2k
                mdname = p->data;
250
36.2k
            }
251
36.2k
        }
252
68.4k
        if (ciphername == NULL) {
253
68.4k
            if ((p = OSSL_PARAM_locate_const(params,
254
68.4k
                     OSSL_ALG_PARAM_CIPHER))
255
68.4k
                != NULL) {
256
236
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
257
0
                    return 0;
258
236
                ciphername = p->data;
259
236
            }
260
68.4k
        }
261
68.4k
        if (engine == NULL) {
262
68.4k
            if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
263
68.4k
                != NULL) {
264
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
265
0
                    return 0;
266
0
                engine = p->data;
267
0
            }
268
68.4k
        }
269
68.4k
    }
270
271
82.8k
    if (mdname != NULL)
272
82.8k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
273
82.8k
            (char *)mdname, 0);
274
82.8k
    if (ciphername != NULL)
275
236
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
276
236
            (char *)ciphername, 0);
277
82.8k
    if (properties != NULL)
278
2.56k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
279
2.56k
            (char *)properties, 0);
280
281
82.8k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
282
82.8k
    if (engine != NULL)
283
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
284
0
            (char *)engine, 0);
285
82.8k
#endif
286
287
82.8k
    if (key != NULL)
288
0
        *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
289
0
            (unsigned char *)key,
290
0
            keylen);
291
292
82.8k
    *mp = OSSL_PARAM_construct_end();
293
294
82.8k
    return EVP_MAC_CTX_set_params(macctx, mac_params);
295
82.8k
}
296
297
int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
298
    const OSSL_PARAM params[],
299
    const char *macname,
300
    const char *ciphername,
301
    const char *mdname,
302
    OSSL_LIB_CTX *libctx)
303
68.6k
{
304
68.6k
    const OSSL_PARAM *p;
305
68.6k
    const char *properties = NULL;
306
307
68.6k
    if (macname == NULL
308
515
        && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
309
515
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
310
0
            return 0;
311
515
        macname = p->data;
312
515
    }
313
68.6k
    if ((p = OSSL_PARAM_locate_const(params,
314
68.6k
             OSSL_ALG_PARAM_PROPERTIES))
315
68.6k
        != NULL) {
316
2.77k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
317
0
            return 0;
318
2.77k
        properties = p->data;
319
2.77k
    }
320
321
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
322
68.6k
    if (macname != NULL) {
323
68.6k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
324
325
68.6k
        EVP_MAC_CTX_free(*macctx);
326
68.6k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
327
        /* The context holds on to the MAC */
328
68.6k
        EVP_MAC_free(mac);
329
68.6k
        if (*macctx == NULL)
330
210
            return 0;
331
68.6k
    }
332
333
    /*
334
     * If there is no MAC yet (and therefore, no MAC context), we ignore
335
     * all other parameters.
336
     */
337
68.4k
    if (*macctx == NULL)
338
0
        return 1;
339
340
68.4k
    if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
341
68.4k
            properties, NULL, 0))
342
67.6k
        return 1;
343
344
865
    EVP_MAC_CTX_free(*macctx);
345
865
    *macctx = NULL;
346
865
    return 0;
347
68.4k
}
348
349
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
350
    OSSL_ALGORITHM *out)
351
99
{
352
99
    int i, j;
353
354
99
    if (out[0].algorithm_names == NULL) {
355
12.9k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
356
12.8k
            if (in[i].capable == NULL || in[i].capable())
357
12.4k
                out[j++] = in[i].alg;
358
12.8k
        }
359
96
        out[j++] = in[i].alg;
360
96
    }
361
99
}
362
363
/* Duplicate a lump of memory safely */
364
int ossl_prov_memdup(const void *src, size_t src_len,
365
    unsigned char **dest, size_t *dest_len)
366
0
{
367
0
    if (src != NULL) {
368
0
        if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
369
0
            return 0;
370
0
        *dest_len = src_len;
371
0
    } else {
372
0
        *dest = NULL;
373
0
        *dest_len = 0;
374
0
    }
375
0
    return 1;
376
0
}