Coverage Report

Created: 2024-11-21 07:03

/src/openssl/providers/common/provider_util.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2022 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
8.86k
{
26
8.86k
    EVP_CIPHER_free(pc->alloc_cipher);
27
8.86k
    pc->alloc_cipher = NULL;
28
8.86k
    pc->cipher = NULL;
29
8.86k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30
8.86k
    ENGINE_finish(pc->engine);
31
8.86k
#endif
32
8.86k
    pc->engine = NULL;
33
8.86k
}
34
35
int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
36
2.91k
{
37
2.91k
    if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
38
0
        return 0;
39
2.91k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
40
2.91k
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
41
0
        EVP_CIPHER_free(src->alloc_cipher);
42
0
        return 0;
43
0
    }
44
2.91k
#endif
45
2.91k
    dst->engine = src->engine;
46
2.91k
    dst->cipher = src->cipher;
47
2.91k
    dst->alloc_cipher = src->alloc_cipher;
48
2.91k
    return 1;
49
2.91k
}
50
51
static int load_common(const OSSL_PARAM params[], const char **propquery,
52
                       ENGINE **engine)
53
5.19k
{
54
5.19k
    const OSSL_PARAM *p;
55
56
5.19k
    *propquery = NULL;
57
5.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58
5.19k
    if (p != NULL) {
59
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
60
0
            return 0;
61
0
        *propquery = p->data;
62
0
    }
63
64
5.19k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65
5.19k
    ENGINE_finish(*engine);
66
5.19k
#endif
67
5.19k
    *engine = NULL;
68
    /* Inside the FIPS module, we don't support legacy ciphers */
69
5.19k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70
5.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71
5.19k
    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
5.19k
#endif
88
5.19k
    return 1;
89
5.19k
}
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 (ossl_param_is_empty(params))
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
0
{
132
0
    return pc->cipher;
133
0
}
134
135
ENGINE *ossl_prov_cipher_engine(const PROV_CIPHER *pc)
136
0
{
137
0
    return pc->engine;
138
0
}
139
140
void ossl_prov_digest_reset(PROV_DIGEST *pd)
141
54.7k
{
142
54.7k
    EVP_MD_free(pd->alloc_md);
143
54.7k
    pd->alloc_md = NULL;
144
54.7k
    pd->md = NULL;
145
54.7k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146
54.7k
    ENGINE_finish(pd->engine);
147
54.7k
#endif
148
54.7k
    pd->engine = NULL;
149
54.7k
}
150
151
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152
50.0k
{
153
50.0k
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154
0
        return 0;
155
50.0k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156
50.0k
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
157
0
        EVP_MD_free(src->alloc_md);
158
0
        return 0;
159
0
    }
160
50.0k
#endif
161
50.0k
    dst->engine = src->engine;
162
50.0k
    dst->md = src->md;
163
50.0k
    dst->alloc_md = src->alloc_md;
164
50.0k
    return 1;
165
50.0k
}
166
167
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168
                                     const char *mdname, const char *propquery)
169
5.19k
{
170
5.19k
    EVP_MD_free(pd->alloc_md);
171
5.19k
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172
173
5.19k
    return pd->md;
174
5.19k
}
175
176
int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177
                                      const OSSL_PARAM params[],
178
                                      OSSL_LIB_CTX *ctx)
179
5.19k
{
180
5.19k
    const OSSL_PARAM *p;
181
5.19k
    const char *propquery;
182
183
5.19k
    if (ossl_param_is_empty(params))
184
0
        return 1;
185
186
5.19k
    if (!load_common(params, &propquery, &pd->engine))
187
0
        return 0;
188
189
5.19k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190
5.19k
    if (p == NULL)
191
0
        return 1;
192
5.19k
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
193
0
        return 0;
194
195
5.19k
    ERR_set_mark();
196
5.19k
    ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197
5.19k
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198
5.19k
    if (pd->md == NULL) {
199
738
        const EVP_MD *md;
200
201
738
        md = EVP_get_digestbyname(p->data);
202
        /* Do not use global EVP_MDs */
203
738
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204
0
            pd->md = md;
205
738
    }
206
5.19k
#endif
207
5.19k
    if (pd->md != NULL)
208
4.45k
        ERR_pop_to_mark();
209
738
    else
210
738
        ERR_clear_last_mark();
211
5.19k
    return pd->md != NULL;
212
5.19k
}
213
214
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
215
4.70k
{
216
4.70k
    return pd->md;
217
4.70k
}
218
219
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
220
2.67k
{
221
2.67k
    return pd->engine;
222
2.67k
}
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
3.03k
{
233
3.03k
    const OSSL_PARAM *p;
234
3.03k
    OSSL_PARAM mac_params[6], *mp = mac_params;
235
236
3.03k
    if (params != NULL) {
237
124
        if (mdname == NULL) {
238
104
            if ((p = OSSL_PARAM_locate_const(params,
239
104
                                            OSSL_ALG_PARAM_DIGEST)) != NULL) {
240
104
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
241
0
                    return 0;
242
104
                mdname = p->data;
243
104
            }
244
104
        }
245
124
        if (ciphername == NULL) {
246
124
            if ((p = OSSL_PARAM_locate_const(params,
247
124
                                            OSSL_ALG_PARAM_CIPHER)) != NULL) {
248
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
249
0
                    return 0;
250
0
                ciphername = p->data;
251
0
            }
252
124
        }
253
124
        if (engine == NULL) {
254
124
            if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
255
124
                    != NULL) {
256
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
257
0
                    return 0;
258
0
                engine = p->data;
259
0
            }
260
124
        }
261
124
    }
262
263
3.03k
    if (mdname != NULL)
264
3.03k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
265
3.03k
                                                 (char *)mdname, 0);
266
3.03k
    if (ciphername != NULL)
267
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
268
0
                                                 (char *)ciphername, 0);
269
3.03k
    if (properties != NULL)
270
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
271
0
                                                 (char *)properties, 0);
272
273
3.03k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
274
3.03k
    if (engine != NULL)
275
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
276
0
                                                 (char *) engine, 0);
277
3.03k
#endif
278
279
3.03k
    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
3.03k
    *mp = OSSL_PARAM_construct_end();
285
286
3.03k
    return EVP_MAC_CTX_set_params(macctx, mac_params);
287
288
3.03k
}
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
350
{
297
350
    const OSSL_PARAM *p;
298
350
    const char *properties = NULL;
299
300
350
    if (macname == NULL
301
350
        && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
302
49
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
303
0
            return 0;
304
49
        macname = p->data;
305
49
    }
306
350
    if ((p = OSSL_PARAM_locate_const(params,
307
350
                                     OSSL_ALG_PARAM_PROPERTIES)) != NULL) {
308
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
309
0
            return 0;
310
0
        properties = p->data;
311
0
    }
312
313
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
314
350
    if (macname != NULL) {
315
124
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
316
317
124
        EVP_MAC_CTX_free(*macctx);
318
124
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
319
        /* The context holds on to the MAC */
320
124
        EVP_MAC_free(mac);
321
124
        if (*macctx == NULL)
322
0
            return 0;
323
124
    }
324
325
    /*
326
     * If there is no MAC yet (and therefore, no MAC context), we ignore
327
     * all other parameters.
328
     */
329
350
    if (*macctx == NULL)
330
226
        return 1;
331
332
124
    if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
333
124
                             properties, NULL, 0))
334
112
        return 1;
335
336
12
    EVP_MAC_CTX_free(*macctx);
337
12
    *macctx = NULL;
338
12
    return 0;
339
124
}
340
341
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
342
                                         OSSL_ALGORITHM *out)
343
2
{
344
2
    int i, j;
345
346
2
    if (out[0].algorithm_names == NULL) {
347
262
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
348
260
            if (in[i].capable == NULL || in[i].capable())
349
256
                out[j++] = in[i].alg;
350
260
        }
351
2
        out[j++] = in[i].alg;
352
2
    }
353
2
}
354
355
/* Duplicate a lump of memory safely */
356
int ossl_prov_memdup(const void *src, size_t src_len,
357
                     unsigned char **dest, size_t *dest_len)
358
0
{
359
0
    if (src != NULL) {
360
0
        if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
361
0
            return 0;
362
0
        *dest_len = src_len;
363
0
    } else {
364
0
        *dest = NULL;
365
0
        *dest_len = 0;
366
0
    }
367
0
    return 1;
368
0
}