Coverage Report

Created: 2023-06-08 06:41

/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
154
{
26
154
    EVP_CIPHER_free(pc->alloc_cipher);
27
154
    pc->alloc_cipher = NULL;
28
154
    pc->cipher = NULL;
29
154
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
30
154
    ENGINE_finish(pc->engine);
31
154
#endif
32
154
    pc->engine = NULL;
33
154
}
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
11.5k
{
54
11.5k
    const OSSL_PARAM *p;
55
56
11.5k
    *propquery = NULL;
57
11.5k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_PROPERTIES);
58
11.5k
    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
11.5k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
65
11.5k
    ENGINE_finish(*engine);
66
11.5k
#endif
67
11.5k
    *engine = NULL;
68
    /* Inside the FIPS module, we don't support legacy ciphers */
69
11.5k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
70
11.5k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE);
71
11.5k
    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
11.5k
#endif
88
11.5k
    return 1;
89
11.5k
}
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
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
17.9k
{
142
17.9k
    EVP_MD_free(pd->alloc_md);
143
17.9k
    pd->alloc_md = NULL;
144
17.9k
    pd->md = NULL;
145
17.9k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
146
17.9k
    ENGINE_finish(pd->engine);
147
17.9k
#endif
148
17.9k
    pd->engine = NULL;
149
17.9k
}
150
151
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
152
7.45k
{
153
7.45k
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
154
0
        return 0;
155
7.45k
#if !defined(FIPS_MODULE) && !defined(OPENSSL_NO_ENGINE)
156
7.45k
    if (src->engine != NULL && !ENGINE_init(src->engine)) {
157
0
        EVP_MD_free(src->alloc_md);
158
0
        return 0;
159
0
    }
160
7.45k
#endif
161
7.45k
    dst->engine = src->engine;
162
7.45k
    dst->md = src->md;
163
7.45k
    dst->alloc_md = src->alloc_md;
164
7.45k
    return 1;
165
7.45k
}
166
167
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
168
                           const char *mdname, const char *propquery)
169
10.5k
{
170
10.5k
    EVP_MD_free(pd->alloc_md);
171
10.5k
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
172
173
10.5k
    return pd->md;
174
10.5k
}
175
176
int ossl_prov_digest_load_from_params(PROV_DIGEST *pd,
177
                                      const OSSL_PARAM params[],
178
                                      OSSL_LIB_CTX *ctx)
179
11.5k
{
180
11.5k
    const OSSL_PARAM *p;
181
11.5k
    const char *propquery;
182
183
11.5k
    if (params == NULL)
184
0
        return 1;
185
186
11.5k
    if (!load_common(params, &propquery, &pd->engine))
187
0
        return 0;
188
189
11.5k
    p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_DIGEST);
190
11.5k
    if (p == NULL)
191
996
        return 1;
192
10.5k
    if (p->data_type != OSSL_PARAM_UTF8_STRING)
193
0
        return 0;
194
195
10.5k
    ERR_set_mark();
196
10.5k
    ossl_prov_digest_fetch(pd, ctx, p->data, propquery);
197
10.5k
#ifndef FIPS_MODULE /* Inside the FIPS module, we don't support legacy digests */
198
10.5k
    if (pd->md == NULL) {
199
0
        const EVP_MD *md;
200
201
0
        md = EVP_get_digestbyname(p->data);
202
        /* Do not use global EVP_MDs */
203
0
        if (md != NULL && md->origin != EVP_ORIG_GLOBAL)
204
0
            pd->md = md;
205
0
    }
206
10.5k
#endif
207
10.5k
    if (pd->md != NULL)
208
10.5k
        ERR_pop_to_mark();
209
0
    else
210
0
        ERR_clear_last_mark();
211
10.5k
    return pd->md != NULL;
212
10.5k
}
213
214
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
215
10.5k
{
216
10.5k
    return pd->md;
217
10.5k
}
218
219
ENGINE *ossl_prov_digest_engine(const PROV_DIGEST *pd)
220
3.38k
{
221
3.38k
    return pd->engine;
222
3.38k
}
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
1.30k
{
233
1.30k
    const OSSL_PARAM *p;
234
1.30k
    OSSL_PARAM mac_params[6], *mp = mac_params;
235
236
1.30k
    if (params != NULL) {
237
1.15k
        if (mdname == NULL) {
238
720
            if ((p = OSSL_PARAM_locate_const(params,
239
720
                                            OSSL_ALG_PARAM_DIGEST)) != NULL) {
240
720
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
241
0
                    return 0;
242
720
                mdname = p->data;
243
720
            }
244
720
        }
245
1.15k
        if (ciphername == NULL) {
246
1.15k
            if ((p = OSSL_PARAM_locate_const(params,
247
1.15k
                                            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
1.15k
        }
253
1.15k
        if (engine == NULL) {
254
1.15k
            if ((p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_ENGINE))
255
1.15k
                    != NULL) {
256
0
                if (p->data_type != OSSL_PARAM_UTF8_STRING)
257
0
                    return 0;
258
0
                engine = p->data;
259
0
            }
260
1.15k
        }
261
1.15k
    }
262
263
1.30k
    if (mdname != NULL)
264
1.30k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
265
1.30k
                                                 (char *)mdname, 0);
266
1.30k
    if (ciphername != NULL)
267
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
268
0
                                                 (char *)ciphername, 0);
269
1.30k
    if (properties != NULL)
270
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
271
0
                                                 (char *)properties, 0);
272
273
1.30k
#if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
274
1.30k
    if (engine != NULL)
275
0
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_ALG_PARAM_ENGINE,
276
0
                                                 (char *) engine, 0);
277
1.30k
#endif
278
279
1.30k
    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
1.30k
    *mp = OSSL_PARAM_construct_end();
285
286
1.30k
    return EVP_MAC_CTX_set_params(macctx, mac_params);
287
288
1.30k
}
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
1.15k
{
297
1.15k
    const OSSL_PARAM *p;
298
1.15k
    const char *properties = NULL;
299
300
1.15k
    if (macname == NULL
301
1.15k
        && (p = OSSL_PARAM_locate_const(params, OSSL_ALG_PARAM_MAC)) != NULL) {
302
0
        if (p->data_type != OSSL_PARAM_UTF8_STRING)
303
0
            return 0;
304
0
        macname = p->data;
305
0
    }
306
1.15k
    if ((p = OSSL_PARAM_locate_const(params,
307
1.15k
                                     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
1.15k
    if (macname != NULL) {
315
1.15k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
316
317
1.15k
        EVP_MAC_CTX_free(*macctx);
318
1.15k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
319
        /* The context holds on to the MAC */
320
1.15k
        EVP_MAC_free(mac);
321
1.15k
        if (*macctx == NULL)
322
0
            return 0;
323
1.15k
    }
324
325
    /*
326
     * If there is no MAC yet (and therefore, no MAC context), we ignore
327
     * all other parameters.
328
     */
329
1.15k
    if (*macctx == NULL)
330
0
        return 1;
331
332
1.15k
    if (ossl_prov_set_macctx(*macctx, params, ciphername, mdname, NULL,
333
1.15k
                             properties, NULL, 0))
334
1.15k
        return 1;
335
336
0
    EVP_MAC_CTX_free(*macctx);
337
0
    *macctx = NULL;
338
0
    return 0;
339
1.15k
}
340
341
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
342
                                         OSSL_ALGORITHM *out)
343
1
{
344
1
    int i, j;
345
346
1
    if (out[0].algorithm_names == NULL) {
347
125
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
348
124
            if (in[i].capable == NULL || in[i].capable())
349
124
                out[j++] = in[i].alg;
350
124
        }
351
1
        out[j++] = in[i].alg;
352
1
    }
353
1
}