Coverage Report

Created: 2025-10-10 07:01

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/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
0
{
26
0
    EVP_CIPHER_free(pc->alloc_cipher);
27
0
    pc->alloc_cipher = NULL;
28
0
    pc->cipher = NULL;
29
0
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30
0
    ENGINE_finish(pc->engine);
31
0
#endif
32
0
    pc->engine = NULL;
33
0
}
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 set_propq(const OSSL_PARAM *propq, const char **propquery)
52
0
{
53
0
    *propquery = NULL;
54
0
    if (propq != NULL) {
55
0
        if (propq->data_type != OSSL_PARAM_UTF8_STRING)
56
0
            return 0;
57
0
        *propquery = propq->data;
58
0
    }
59
0
    return 1;
60
0
}
61
62
static int set_engine(const OSSL_PARAM *e, ENGINE **engine)
63
0
{
64
0
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65
0
    ENGINE_finish(*engine);
66
0
#endif
67
0
    *engine = NULL;
68
    /* Inside the FIPS module, we don't support legacy ciphers */
69
0
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70
0
    if (e != NULL) {
71
0
        if (e->data_type != OSSL_PARAM_UTF8_STRING)
72
0
            return 0;
73
        /* Get a structural reference */
74
0
        *engine = ENGINE_by_id(e->data);
75
0
        if (*engine == NULL)
76
0
            return 0;
77
        /* Get a functional reference */
78
0
        if (!ENGINE_init(*engine)) {
79
0
            ENGINE_free(*engine);
80
0
            *engine = NULL;
81
0
            return 0;
82
0
        }
83
        /* Free the structural reference */
84
0
        ENGINE_free(*engine);
85
0
    }
86
0
#endif
87
0
    return 1;
88
0
}
89
90
int ossl_prov_cipher_load(PROV_CIPHER *pc, const OSSL_PARAM *cipher,
91
                          const OSSL_PARAM *propq, const OSSL_PARAM *engine,
92
                          OSSL_LIB_CTX *ctx)
93
0
{
94
0
    const char *propquery;
95
96
0
   if (!set_propq(propq, &propquery) || !set_engine(engine, &pc->engine))
97
0
        return 0;
98
99
0
    if (cipher == NULL)
100
0
        return 1;
101
0
    if (cipher->data_type != OSSL_PARAM_UTF8_STRING)
102
0
        return 0;
103
104
0
    EVP_CIPHER_free(pc->alloc_cipher);
105
0
    ERR_set_mark();
106
0
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, cipher->data,
107
0
                                                     propquery);
108
0
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
109
0
    if (pc->cipher == NULL) {
110
0
        const EVP_CIPHER *evp_cipher;
111
112
0
        evp_cipher = EVP_get_cipherbyname(cipher->data);
113
        /* Do not use global EVP_CIPHERs */
114
0
        if (evp_cipher != NULL && evp_cipher->origin != EVP_ORIG_GLOBAL)
115
0
            pc->cipher = evp_cipher;
116
0
    }
117
0
#endif
118
0
    if (pc->cipher != NULL)
119
0
        ERR_pop_to_mark();
120
0
    else
121
0
        ERR_clear_last_mark();
122
0
    return pc->cipher != NULL;
123
0
}
124
125
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
126
0
{
127
0
    return pc->cipher;
128
0
}
129
130
ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
131
0
{
132
0
    return pc->engine;
133
0
}
134
135
void ossl_prov_digest_reset(PROV_DIGEST *pd)
136
0
{
137
0
    EVP_MD_free(pd->alloc_md);
138
0
    pd->alloc_md = NULL;
139
0
    pd->md = NULL;
140
0
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
141
0
    ENGINE_finish(pd->engine);
142
0
#endif
143
0
    pd->engine = NULL;
144
0
}
145
146
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
147
0
{
148
0
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
149
0
        return 0;
150
0
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
151
0
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
152
0
        EVP_MD_free(src->alloc_md);
153
0
        return 0;
154
0
    }
155
0
#endif
156
0
    dst->engine = src->engine;
157
0
    dst->md = src->md;
158
0
    dst->alloc_md = src->alloc_md;
159
0
    return 1;
160
0
}
161
162
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
163
                                     const char *mdname, const char *propquery)
164
0
{
165
0
    EVP_MD_free(pd->alloc_md);
166
0
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
167
168
0
    return pd->md;
169
0
}
170
171
int ossl_prov_digest_load(PROV_DIGEST *pd, const OSSL_PARAM *digest,
172
                          const OSSL_PARAM *propq, const OSSL_PARAM *engine,
173
                          OSSL_LIB_CTX *ctx)
174
0
{
175
0
    const char *propquery;
176
177
0
    if (!set_propq(propq, &propquery) || !set_engine(engine, &pd->engine))
178
0
        return 0;
179
180
0
    if (digest == NULL)
181
0
        return 1;
182
0
    if (digest->data_type != OSSL_PARAM_UTF8_STRING)
183
0
        return 0;
184
185
0
    ERR_set_mark();
186
0
    ossl_prov_digest_fetch(pd, ctx, digest->data, propquery);
187
0
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
188
0
    if (pd->md == NULL) {
189
0
        const EVP_MD *md;
190
191
0
        md = EVP_get_digestbyname(digest->data);
192
        /* Do not use global EVP_MDs */
193
0
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
194
0
            pd->md = md;
195
0
    }
196
0
#endif
197
0
    if (pd->md != NULL)
198
0
        ERR_pop_to_mark();
199
0
    else
200
0
        ERR_clear_last_mark();
201
0
    return pd->md != NULL;
202
0
}
203
204
void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md)
205
0
{
206
0
    ossl_prov_digest_reset(pd);
207
0
    pd->md = pd->alloc_md = md;
208
0
}
209
210
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
211
0
{
212
0
    return pd->md;
213
0
}
214
215
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
216
0
{
217
0
    return pd->engine;
218
0
}
219
220
int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
221
                         const char *ciphername,
222
                         const char *mdname,
223
                         const char *engine,
224
                         const char *properties,
225
                         const OSSL_PARAM param[])
226
0
{
227
0
    OSSL_PARAM mac_params[5], *mp = mac_params, *mergep;
228
0
    int free_merge = 0;
229
0
    int ret;
230
231
0
    if (mdname != NULL)
232
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
233
0
                                                 (char *)mdname, 0);
234
0
    if (ciphername != NULL)
235
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
236
0
                                                 (char *)ciphername, 0);
237
0
    if (properties != NULL)
238
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
239
0
                                                 (char *)properties, 0);
240
241
0
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
242
0
    if (engine != NULL)
243
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
244
0
                                                 (char *) engine, 0);
245
0
#endif
246
247
0
    *mp = OSSL_PARAM_construct_end();
248
249
    /*
250
     * OSSL_PARAM_merge returns NULL and sets an error if either
251
     * list passed to it is NULL, and we aren't guaranteed that the
252
     * passed in value of param is not NULL here.
253
     * Given that we just want the union of the two lists, even if one
254
     * is empty, we have to check for that case, and if param is NULL,
255
     * just use the mac_params list.  In turn we only free the merge
256
     * result if we actually did the merge
257
     */
258
0
    if (param == NULL) {
259
0
        mergep = mac_params;
260
0
    } else {
261
0
        free_merge = 1;
262
0
        mergep = OSSL_PARAM_merge(mac_params, param);
263
0
        if (mergep == NULL)
264
0
            return 0;
265
0
    }
266
267
0
    ret = EVP_MAC_CTX_set_params(macctx, mergep);
268
269
0
    if (free_merge == 1)
270
0
        OSSL_PARAM_free(mergep);
271
0
    return ret;
272
0
}
273
274
int ossl_prov_macctx_load(EVP_MAC_CTX **macctx,
275
                          const OSSL_PARAM *pmac, const OSSL_PARAM *pcipher,
276
                          const OSSL_PARAM *pdigest, const OSSL_PARAM *propq,
277
                          const OSSL_PARAM *pengine,
278
                          const char *macname, const char *ciphername,
279
                          const char *mdname, OSSL_LIB_CTX *libctx)
280
0
{
281
0
    const char *properties = NULL;
282
0
    const char *engine = NULL;
283
284
0
    if (macname == NULL && pmac != NULL)
285
0
        if (!OSSL_PARAM_get_utf8_string_ptr(pmac, &macname))
286
0
            return 0;
287
0
    if (propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(propq, &properties))
288
0
        return 0;
289
290
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
291
0
    if (macname != NULL) {
292
0
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
293
294
0
        EVP_MAC_CTX_free(*macctx);
295
0
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
296
        /* The context holds on to the MAC */
297
0
        EVP_MAC_free(mac);
298
0
        if (*macctx == NULL)
299
0
            return 0;
300
0
    }
301
302
    /*
303
     * If there is no MAC yet (and therefore, no MAC context), we ignore
304
     * all other parameters.
305
     */
306
0
    if (*macctx == NULL)
307
0
        return 1;
308
309
0
    if (ciphername == NULL && pcipher != NULL)
310
0
        if (!OSSL_PARAM_get_utf8_string_ptr(pcipher, &ciphername))
311
0
            return 0;
312
0
    if (mdname == NULL && pdigest != NULL)
313
0
        if (!OSSL_PARAM_get_utf8_string_ptr(pdigest, &mdname))
314
0
            return 0;
315
0
    if (pengine != NULL && !OSSL_PARAM_get_utf8_string_ptr(pengine, &engine))
316
0
        return 0;
317
318
0
    if (ossl_prov_set_macctx(*macctx, ciphername, mdname, engine, properties, NULL))
319
0
        return 1;
320
321
0
    EVP_MAC_CTX_free(*macctx);
322
0
    *macctx = NULL;
323
0
    return 0;
324
0
}
325
326
int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
327
                                      const OSSL_PARAM params[],
328
                                      const char *macname,
329
                                      const char *ciphername,
330
                                      const char *mdname,
331
                                      OSSL_LIB_CTX *libctx)
332
0
{
333
0
    return ossl_prov_macctx_load
334
0
            (macctx, OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC),
335
0
             OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER),
336
0
             OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST),
337
0
             OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES),
338
0
             OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE),
339
0
             macname, ciphername, mdname, libctx);
340
0
}
341
342
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
343
                                         OSSL_ALGORITHM *out)
344
16
{
345
16
    int i, j;
346
347
16
    if (out[0].algorithm_names == NULL) {
348
2.24k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
349
2.22k
            if (in[i].capable == NULL || in[i].capable())
350
2.08k
                out[j++] = in[i].alg;
351
2.22k
        }
352
16
        out[j++] = in[i].alg;
353
16
    }
354
16
}
355
356
/* Duplicate a lump of memory safely */
357
int ossl_prov_memdup(const void *src, size_t src_len,
358
                     unsigned char **dest, size_t *dest_len)
359
0
{
360
0
    if (src != NULL) {
361
0
        if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
362
0
            return 0;
363
0
        *dest_len = src_len;
364
0
    } else {
365
0
        *dest = NULL;
366
0
        *dest_len = 0;
367
0
    }
368
0
    return 1;
369
0
}