Coverage Report

Created: 2025-06-13 06:58

/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
7.21k
{
26
7.21k
    EVP_CIPHER_free(pc->alloc_cipher);
27
7.21k
    pc->alloc_cipher = NULL;
28
7.21k
    pc->cipher = NULL;
29
7.21k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30
7.21k
    ENGINE_finish(pc->engine);
31
7.21k
#endif
32
7.21k
    pc->engine = NULL;
33
7.21k
}
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
778k
{
54
778k
    const OSSL_PARAM *p;
55
56
778k
    *propquery = NULL;
57
778k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58
778k
    if (p != NULL) {
59
2.93k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
60
0
            return 0;
61
2.93k
        *propquery = p->data;
62
2.93k
    }
63
64
778k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65
778k
    ENGINE_finish(*engine);
66
778k
#endif
67
778k
    *engine = NULL;
68
    /* Inside the FIPS module, we don't support legacy ciphers */
69
778k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70
778k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71
778k
    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
778k
#endif
88
778k
    return 1;
89
778k
}
90
91
int ossl_prov_cipher_load_from_params(PROV_CIPHER *pc,
92
                                      const OSSL_PARAM params[],
93
                                      OSSL_LIB_CTX *ctx)
94
0
{
95
0
    const OSSL_PARAM *p;
96
0
    const char *propquery;
97
98
0
    if (params == NULL)
99
0
        return 1;
100
101
0
    if (!load_common(params, &propquery, &pc->engine))
102
0
        return 0;
103
104
0
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_CIPHER);
105
0
    if (p == NULL)
106
0
        return 1;
107
0
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
108
0
        return 0;
109
110
0
    EVP_CIPHER_free(pc->alloc_cipher);
111
0
    ERR_set_mark();
112
0
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, p->data, propquery);
113
0
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy ciphers */
114
0
    if (pc->cipher == NULL) {
115
0
        const EVP_CIPHER *cipher;
116
117
0
        cipher = EVP_get_cipherbyname(p->data);
118
        /* Do not use global EVP_CIPHERs */
119
0
        if (cipher != NULL && cipher->origin != EVP_ORIG_GLOBAL)
120
0
            pc->cipher = cipher;
121
0
    }
122
0
#endif
123
0
    if (pc->cipher != NULL)
124
0
        ERR_pop_to_mark();
125
0
    else
126
0
        ERR_clear_last_mark();
127
0
    return pc->cipher != NULL;
128
0
}
129
130
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
131
110
{
132
110
    return pc->cipher;
133
110
}
134
135
ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
136
86
{
137
86
    return pc->engine;
138
86
}
139
140
void ossl_prov_digest_reset(PROV_DIGEST *pd)
141
1.25M
{
142
1.25M
    EVP_MD_free(pd->alloc_md);
143
1.25M
    pd->alloc_md = NULL;
144
1.25M
    pd->md = NULL;
145
1.25M
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146
1.25M
    ENGINE_finish(pd->engine);
147
1.25M
#endif
148
1.25M
    pd->engine = NULL;
149
1.25M
}
150
151
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152
523k
{
153
523k
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154
0
        return 0;
155
523k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156
523k
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
157
0
        EVP_MD_free(src->alloc_md);
158
0
        return 0;
159
0
    }
160
523k
#endif
161
523k
    dst->engine = src->engine;
162
523k
    dst->md = src->md;
163
523k
    dst->alloc_md = src->alloc_md;
164
523k
    return 1;
165
523k
}
166
167
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168
                           const char *mdname, const char *propquery)
169
726k
{
170
726k
    EVP_MD_free(pd->alloc_md);
171
726k
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172
173
726k
    return pd->md;
174
726k
}
175
176
int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177
                                      const OSSL_PARAM params[],
178
                                      OSSL_LIB_CTX *ctx)
179
381k
{
180
381k
    const OSSL_PARAM *p;
181
381k
    const char *propquery;
182
183
381k
    if (params == NULL)
184
0
        return 1;
185
186
381k
    if (!load_common(params, &propquery, &pd->engine))
187
0
        return 0;
188
189
381k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190
381k
    if (p == NULL)
191
15.1k
        return 1;
192
366k
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
193
0
        return 0;
194
195
366k
    ERR_set_mark();
196
366k
    ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197
366k
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198
366k
    if (pd->md == NULL) {
199
3
        const EVP_MD *md;
200
201
3
        md = EVP_get_digestbyname(p->data);
202
        /* Do not use global EVP_MDs */
203
3
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204
0
            pd->md = md;
205
3
    }
206
366k
#endif
207
366k
    if (pd->md != NULL)
208
366k
        ERR_pop_to_mark();
209
3
    else
210
3
        ERR_clear_last_mark();
211
366k
    return pd->md != NULL;
212
366k
}
213
214
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
215
1.07M
{
216
1.07M
    return pd->md;
217
1.07M
}
218
219
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
220
118k
{
221
118k
    return pd->engine;
222
118k
}
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
43.2k
{
233
43.2k
    const OSSL_PARAM *p;
234
43.2k
    OSSL_PARAM mac_params[6], *mp = mac_params;
235
236
43.2k
    if (params != NULL) {
237
36.1k
        if (mdname == NULL) {
238
21.4k
            if ((p = OSSL_PARAM_locate_const(params,
239
21.4k
                                            OSSL_ALG_PARAM_DIGEST)) != NULL) {
240
21.4k
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
241
0
                    return 0;
242
21.4k
                mdname = p->data;
243
21.4k
            }
244
21.4k
        }
245
36.1k
        if (ciphername == NULL) {
246
36.1k
            if ((p = OSSL_PARAM_locate_const(params,
247
36.1k
                                            OSSL_ALG_PARAM_CIPHER)) != NULL) {
248
83
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
249
0
                    return 0;
250
83
                ciphername = p->data;
251
83
            }
252
36.1k
        }
253
36.1k
        if (engine == NULL) {
254
36.1k
            if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
255
36.1k
                    != NULL) {
256
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
257
0
                    return 0;
258
0
                engine = p->data;
259
0
            }
260
36.1k
        }
261
36.1k
    }
262
263
43.2k
    if (mdname != NULL)
264
43.2k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
265
43.2k
                                                 (char *)mdname, 0);
266
43.2k
    if (ciphername != NULL)
267
83
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
268
83
                                                 (char *)ciphername, 0);
269
43.2k
    if (properties != NULL)
270
1.57k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
271
1.57k
                                                 (char *)properties, 0);
272
273
43.2k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
274
43.2k
    if (engine != NULL)
275
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
276
0
                                                 (char *) engine, 0);
277
43.2k
#endif
278
279
43.2k
    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
43.2k
    *mp = OSSL_PARAM_construct_end();
285
286
43.2k
    return EVP_MAC_CTX_set_params(macctx, mac_params);
287
288
43.2k
}
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
36.3k
{
297
36.3k
    const OSSL_PARAM *p;
298
36.3k
    const char *properties = NULL;
299
300
36.3k
    if (macname == NULL
301
36.3k
        && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
302
192
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
303
0
            return 0;
304
192
        macname = p->data;
305
192
    }
306
36.3k
    if ((p = OSSL_PARAM_locate_const(params,
307
36.3k
                                     OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
308
1.77k
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
309
0
            return 0;
310
1.77k
        properties = p->data;
311
1.77k
    }
312
313
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
314
36.3k
    if (macname != NULL) {
315
36.3k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
316
317
36.3k
        EVP_MAC_CTX_free(*macctx);
318
36.3k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
319
        /* The context holds on to the MAC */
320
36.3k
        EVP_MAC_free(mac);
321
36.3k
        if (*macctx == NULL)
322
206
            return 0;
323
36.3k
    }
324
325
    /*
326
     * If there is no MAC yet (and therefore, no MAC context), we ignore
327
     * all other parameters.
328
     */
329
36.1k
    if (*macctx == NULL)
330
0
        return 1;
331
332
36.1k
    if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
333
36.1k
                             properties, NULL, 0))
334
35.6k
        return 1;
335
336
493
    EVP_MAC_CTX_free(*macctx);
337
493
    *macctx = NULL;
338
493
    return 0;
339
36.1k
}
340
341
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
342
                                         OSSL_ALGORITHM *out)
343
45
{
344
45
    int i, j;
345
346
45
    if (out[0].algorithm_names == NULL) {
347
5.87k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
348
5.83k
            if (in[i].capable == NULL || in[i].capable())
349
5.65k
                out[j++] = in[i].alg;
350
5.83k
        }
351
44
        out[j++] = in[i].alg;
352
44
    }
353
45
}