Coverage Report

Created: 2026-10-28 06:28

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/providers/common/provider_util.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2026 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
#include <openssl/evp.h>
11
#include <openssl/core_names.h>
12
#include <openssl/err.h>
13
#include <openssl/proverr.h>
14
#ifndef FIPS_MODULE
15
#include "crypto/evp.h"
16
#endif
17
#include "prov/providercommon.h"
18
#include "prov/provider_util.h"
19
20
void ossl_prov_cipher_reset(PROV_CIPHER *pc)
21
30.6k
{
22
30.6k
    EVP_CIPHER_free(pc->alloc_cipher);
23
30.6k
    pc->alloc_cipher = NULL;
24
30.6k
    pc->cipher = NULL;
25
30.6k
}
26
27
int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
28
398
{
29
398
    if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
30
0
        return 0;
31
398
    dst->cipher = src->cipher;
32
398
    dst->alloc_cipher = src->alloc_cipher;
33
398
    return 1;
34
398
}
35
36
static int set_propq(const OSSL_PARAM *propq, const char **propquery)
37
1.06M
{
38
1.06M
    *propquery = NULL;
39
1.06M
    if (propq != NULL) {
40
6.90k
        if (propq->data_type != OSSL_PARAM_UTF8_STRING)
41
0
            return 0;
42
6.90k
        *propquery = propq->data;
43
6.90k
    }
44
1.06M
    return 1;
45
1.06M
}
46
47
int ossl_prov_cipher_load(PROV_CIPHER *pc, const OSSL_PARAM *cipher,
48
    const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx)
49
597
{
50
597
    const char *propquery;
51
52
597
    if (!set_propq(propq, &propquery))
53
0
        return 0;
54
55
597
    if (cipher == NULL)
56
82
        return 1;
57
515
    if (cipher->data_type != OSSL_PARAM_UTF8_STRING)
58
0
        return 0;
59
60
515
    EVP_CIPHER_free(pc->alloc_cipher);
61
515
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, cipher->data,
62
515
        propquery);
63
515
    return pc->cipher != NULL;
64
515
}
65
66
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
67
2.91k
{
68
2.91k
    return pc->cipher;
69
2.91k
}
70
71
void ossl_prov_digest_reset(PROV_DIGEST *pd)
72
3.97M
{
73
3.97M
    EVP_MD_free(pd->alloc_md);
74
3.97M
    pd->alloc_md = NULL;
75
3.97M
    pd->md = NULL;
76
3.97M
}
77
78
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
79
961k
{
80
961k
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
81
0
        return 0;
82
961k
    dst->md = src->md;
83
961k
    dst->alloc_md = src->alloc_md;
84
961k
    return 1;
85
961k
}
86
87
const EVP_MD *ossl_prov_digest_fetch(PROV_DIGEST *pd, OSSL_LIB_CTX *libctx,
88
    const char *mdname, const char *propquery)
89
1.72M
{
90
1.72M
    EVP_MD_free(pd->alloc_md);
91
1.72M
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
92
93
1.72M
    return pd->md;
94
1.72M
}
95
96
int ossl_prov_digest_load(PROV_DIGEST *pd, const OSSL_PARAM *digest,
97
    const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx)
98
786k
{
99
786k
    const char *propquery;
100
101
786k
    if (!set_propq(propq, &propquery))
102
0
        return 0;
103
104
786k
    if (digest == NULL)
105
251
        return 1;
106
785k
    if (digest->data_type != OSSL_PARAM_UTF8_STRING)
107
0
        return 0;
108
109
785k
    ossl_prov_digest_fetch(pd, ctx, digest->data, propquery);
110
785k
    return pd->md != NULL;
111
785k
}
112
113
void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md)
114
708
{
115
708
    ossl_prov_digest_reset(pd);
116
708
    pd->md = pd->alloc_md = md;
117
708
}
118
119
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
120
3.40M
{
121
3.40M
    return pd->md;
122
3.40M
}
123
124
int ossl_prov_set_macctx(EVP_MAC_CTX *macctx,
125
    const char *ciphername,
126
    const char *mdname,
127
    const char *properties,
128
    const OSSL_PARAM param[])
129
65.6k
{
130
65.6k
    OSSL_PARAM mac_params[5], *mp = mac_params, *mergep;
131
65.6k
    int free_merge = 0;
132
65.6k
    int ret;
133
134
65.6k
    if (mdname != NULL)
135
65.3k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
136
65.3k
            (char *)mdname, 0);
137
65.6k
    if (ciphername != NULL)
138
273
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
139
273
            (char *)ciphername, 0);
140
65.6k
    if (properties != NULL)
141
1.77k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
142
1.77k
            (char *)properties, 0);
143
144
65.6k
    *mp = OSSL_PARAM_construct_end();
145
146
    /*
147
     * OSSL_PARAM_merge returns NULL and sets an error if either
148
     * list passed to it is NULL, and we aren't guaranteed that the
149
     * passed in value of param is not NULL here.
150
     * Given that we just want the union of the two lists, even if one
151
     * is empty, we have to check for that case, and if param is NULL,
152
     * just use the mac_params list.  In turn we only free the merge
153
     * result if we actually did the merge
154
     */
155
65.6k
    if (param == NULL) {
156
53.6k
        mergep = mac_params;
157
53.6k
    } else {
158
11.9k
        free_merge = 1;
159
11.9k
        mergep = OSSL_PARAM_merge(mac_params, param);
160
11.9k
        if (mergep == NULL)
161
0
            return 0;
162
11.9k
    }
163
164
65.6k
    ret = EVP_MAC_CTX_set_params(macctx, mergep);
165
166
65.6k
    if (free_merge == 1)
167
11.9k
        OSSL_PARAM_free(mergep);
168
65.6k
    return ret;
169
65.6k
}
170
171
int ossl_prov_macctx_load(EVP_MAC_CTX **macctx,
172
    const OSSL_PARAM *pmac, const OSSL_PARAM *pcipher,
173
    const OSSL_PARAM *pdigest, const OSSL_PARAM *propq,
174
    const char *macname, const char *ciphername,
175
    const char *mdname, OSSL_LIB_CTX *libctx)
176
53.7k
{
177
53.7k
    const char *properties = NULL;
178
179
53.7k
    if (macname == NULL && pmac != NULL)
180
292
        if (!OSSL_PARAM_get_utf8_string_ptr(pmac, &macname))
181
0
            return 0;
182
53.7k
    if (propq != NULL && !OSSL_PARAM_get_utf8_string_ptr(propq, &properties))
183
0
        return 0;
184
185
    /* If we got a new mac name, we make a new EVP_MAC_CTX */
186
53.7k
    if (macname != NULL) {
187
53.5k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
188
189
53.5k
        EVP_MAC_CTX_free(*macctx);
190
53.5k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
191
        /* The context holds on to the MAC */
192
53.5k
        EVP_MAC_free(mac);
193
53.5k
        if (*macctx == NULL)
194
115
            return 0;
195
53.5k
    }
196
197
    /*
198
     * If there is no MAC yet (and therefore, no MAC context), we ignore
199
     * all other parameters.
200
     */
201
53.6k
    if (*macctx == NULL)
202
0
        return 1;
203
204
53.6k
    if (ciphername == NULL && pcipher != NULL)
205
273
        if (!OSSL_PARAM_get_utf8_string_ptr(pcipher, &ciphername))
206
0
            return 0;
207
53.6k
    if (mdname == NULL && pdigest != NULL)
208
26.9k
        if (!OSSL_PARAM_get_utf8_string_ptr(pdigest, &mdname))
209
0
            return 0;
210
211
53.6k
    if (ossl_prov_set_macctx(*macctx, ciphername, mdname, properties, NULL))
212
52.9k
        return 1;
213
214
644
    EVP_MAC_CTX_free(*macctx);
215
644
    *macctx = NULL;
216
644
    return 0;
217
53.6k
}
218
219
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
220
    OSSL_ALGORITHM *out)
221
125
{
222
125
    int i, j;
223
224
125
    if (out[0].algorithm_names == NULL) {
225
16.7k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
226
16.6k
            if (in[i].capable == NULL || in[i].capable())
227
15.8k
                out[j++] = in[i].alg;
228
16.6k
        }
229
122
        out[j++] = in[i].alg;
230
122
    }
231
125
}
232
233
/* Duplicate a lump of memory safely */
234
int ossl_prov_memdup(const void *src, size_t src_len,
235
    unsigned char **dest, size_t *dest_len)
236
0
{
237
0
    if (src != NULL) {
238
0
        if ((*dest = OPENSSL_memdup(src, src_len)) == NULL)
239
0
            return 0;
240
0
        *dest_len = src_len;
241
0
    } else {
242
0
        *dest = NULL;
243
0
        *dest_len = 0;
244
0
    }
245
0
    return 1;
246
0
}