Coverage Report

Created: 2023-09-25 06:41

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