Coverage Report

Created: 2025-08-11 07:04

/src/openssl35/providers/common/provider_util.c
Line
Count
Source (jump to first uncovered line)
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
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
59
{
95
59
    const OSSL_PARAM *p;
96
59
    const char *propquery;
97
98
59
    if (ossl_param_is_empty(params))
99
0
        return 1;
100
101
59
    if (!load_common(params, &propquery, &pc->engine))
102
0
        return 0;
103
104
59
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
105
59
    if (p == NULL)
106
0
        return 1;
107
59
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
108
0
        return 0;
109
110
59
    EVP_CIPHER_free(pc->alloc_cipher);
111
59
    ERR_set_mark();
112
59
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113
59
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114
59
    if (pc->cipher == NULL) {
115
33
        const EVP_CIPHER *cipher;
116
117
33
        cipher = EVP_get_cipherbyname(p->data);
118
        /* Do not use global EVP_CIPHERs */
119
33
        if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
120
0
            pc->cipher = cipher;
121
33
    }
122
59
#endif
123
59
    if (pc->cipher != NULL)
124
26
        ERR_pop_to_mark();
125
33
    else
126
33
        ERR_clear_last_mark();
127
59
    return pc->cipher != NULL;
128
59
}
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
304k
{
180
304k
    const OSSL_PARAM *p;
181
304k
    const char *propquery;
182
183
304k
    if (ossl_param_is_empty(params))
184
0
        return 1;
185
186
304k
    if (!load_common(params, &propquery, &pd->engine))
187
0
        return 0;
188
189
304k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190
304k
    if (p == NULL)
191
51.3k
        return 1;
192
253k
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
193
0
        return 0;
194
195
253k
    ERR_set_mark();
196
253k
    ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197
253k
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198
253k
    if (pd->md == NULL) {
199
391
        const EVP_MD *md;
200
201
391
        md = EVP_get_digestbyname(p->data);
202
        /* Do not use global EVP_MDs */
203
391
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204
0
            pd->md = md;
205
391
    }
206
253k
#endif
207
253k
    if (pd->md != NULL)
208
252k
        ERR_pop_to_mark();
209
391
    else
210
391
        ERR_clear_last_mark();
211
253k
    return pd->md != NULL;
212
253k
}
213
214
void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md)
215
0
{
216
0
    ossl_prov_digest_reset(pd);
217
0
    pd->md = pd->alloc_md = md;
218
0
}
219
220
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
221
2.22M
{
222
2.22M
    return pd->md;
223
2.22M
}
224
225
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
226
227k
{
227
227k
    return pd->engine;
228
227k
}
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
59.7k
{
239
59.7k
    const OSSL_PARAM *p;
240
59.7k
    OSSL_PARAM mac_params[6], *mp = mac_params;
241
242
59.7k
    if (params != NULL) {
243
49.4k
        if (mdname == NULL) {
244
30.5k
            if ((p = OSSL_PARAM_locate_const(params,
245
30.5k
                                            OSSL_ALG_PARAM_DIGEST)) != NULL) {
246
30.5k
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
247
0
                    return 0;
248
30.5k
                mdname = p->data;
249
30.5k
            }
250
30.5k
        }
251
49.4k
        if (ciphername == NULL) {
252
49.4k
            if ((p = OSSL_PARAM_locate_const(params,
253
49.4k
                                            OSSL_ALG_PARAM_CIPHER)) != NULL) {
254
101
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
255
0
                    return 0;
256
101
                ciphername = p->data;
257
101
            }
258
49.4k
        }
259
49.4k
        if (engine == NULL) {
260
49.4k
            if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
261
49.4k
                    != NULL) {
262
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
263
0
                    return 0;
264
0
                engine = p->data;
265
0
            }
266
49.4k
        }
267
49.4k
    }
268
269
59.7k
    if (mdname != NULL)
270
59.7k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
271
59.7k
                                                 (char *)mdname, 0);
272
59.7k
    if (ciphername != NULL)
273
101
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
274
101
                                                 (char *)ciphername, 0);
275
59.7k
    if (properties != NULL)
276
1.65k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
277
1.65k
                                                 (char *)properties, 0);
278
279
59.7k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
280
59.7k
    if (engine != NULL)
281
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
282
0
                                                 (char *) engine, 0);
283
59.7k
#endif
284
285
59.7k
    if (key != NULL)
286
0
        *mp++ = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
287
0
                                                  (unsigned char *)key,
288
0
                                                  keylen);
289
290
59.7k
    *mp = OSSL_PARAM_construct_end();
291
292
59.7k
    return EVP_MAC_CTX_set_params(macctx, mac_params);
293
294
59.7k
}
295
296
int ossl_prov_macctx_load_from_params(EVP_MAC_CTX **macctx,
297
                                      const OSSL_PARAM params[],
298
                                      const char *macname,
299
                                      const char *ciphername,
300
                                      const char *mdname,
301
                                      OSSL_LIB_CTX *libctx)
302
49.6k
{
303
49.6k
    const OSSL_PARAM *p;
304
49.6k
    const char *properties = NULL;
305
306
49.6k
    if (macname == NULL
307
49.6k
        && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
308
333
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
309
0
            return 0;
310
333
        macname = p->data;
311
333
    }
312
49.6k
    if ((p = OSSL_PARAM_locate_const(params,
313
49.6k
                                     OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
314
1.78k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
315
0
            return 0;
316
1.78k
        properties = p->data;
317
1.78k
    }
318
319
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
320
49.6k
    if (macname != NULL) {
321
49.6k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
322
323
49.6k
        EVP_MAC_CTX_free(*macctx);
324
49.6k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
325
        /* The context holds on to the MAC */
326
49.6k
        EVP_MAC_free(mac);
327
49.6k
        if (*macctx == NULL)
328
134
            return 0;
329
49.6k
    }
330
331
    /*
332
     * If there is no MAC yet (and therefore, no MAC context), we ignore
333
     * all other parameters.
334
     */
335
49.4k
    if (*macctx == NULL)
336
0
        return 1;
337
338
49.4k
    if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
339
49.4k
                             properties, NULL, 0))
340
48.8k
        return 1;
341
342
584
    EVP_MAC_CTX_free(*macctx);
343
584
    *macctx = NULL;
344
584
    return 0;
345
49.4k
}
346
347
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
348
                                         OSSL_ALGORITHM *out)
349
88
{
350
88
    int i, j;
351
352
88
    if (out[0].algorithm_names == NULL) {
353
11.2k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
354
11.1k
            if (in[i].capable == NULL || in[i].capable())
355
11.0k
                out[j++] = in[i].alg;
356
11.1k
        }
357
85
        out[j++] = in[i].alg;
358
85
    }
359
88
}
360
361
/* Duplicate a lump of memory safely */
362
int ossl_prov_memdup(const void *src, size_t src_len,
363
                     unsigned char **dest, size_t *dest_len)
364
0
{
365
0
    if (src != NULL) {
366
0
        if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
367
0
            return 0;
368
0
        *dest_len = src_len;
369
0
    } else {
370
0
        *dest = NULL;
371
0
        *dest_len = 0;
372
0
    }
373
0
    return 1;
374
0
}