Coverage Report

Created: 2026-09-12 06:55

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
23.6k
{
22
23.6k
    EVP_CIPHER_free(pc->alloc_cipher);
23
23.6k
    pc->alloc_cipher = NULL;
24
23.6k
    pc->cipher = NULL;
25
23.6k
}
26
27
int ossl_prov_cipher_copy(PROV_CIPHER *dst, const PROV_CIPHER *src)
28
276
{
29
276
    if (src->alloc_cipher != NULL && !EVP_CIPHER_up_ref(src->alloc_cipher))
30
0
        return 0;
31
276
    dst->cipher = src->cipher;
32
276
    dst->alloc_cipher = src->alloc_cipher;
33
276
    return 1;
34
276
}
35
36
static int set_propq(const OSSL_PARAM *propq, const char **propquery)
37
760k
{
38
760k
    *propquery = NULL;
39
760k
    if (propq != NULL) {
40
5.16k
        if (propq->data_type != OSSL_PARAM_UTF8_STRING)
41
0
            return 0;
42
5.16k
        *propquery = propq->data;
43
5.16k
    }
44
760k
    return 1;
45
760k
}
46
47
int ossl_prov_cipher_load(PROV_CIPHER *pc, const OSSL_PARAM *cipher,
48
    const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx)
49
479
{
50
479
    const char *propquery;
51
52
479
    if (!set_propq(propq, &propquery))
53
0
        return 0;
54
55
479
    if (cipher == NULL)
56
80
        return 1;
57
399
    if (cipher->data_type != OSSL_PARAM_UTF8_STRING)
58
0
        return 0;
59
60
399
    EVP_CIPHER_free(pc->alloc_cipher);
61
399
    pc->cipher = pc->alloc_cipher = EVP_CIPHER_fetch(ctx, cipher->data,
62
399
        propquery);
63
399
    return pc->cipher != NULL;
64
399
}
65
66
const EVP_CIPHER *ossl_prov_cipher_cipher(const PROV_CIPHER *pc)
67
2.50k
{
68
2.50k
    return pc->cipher;
69
2.50k
}
70
71
void ossl_prov_digest_reset(PROV_DIGEST *pd)
72
3.17M
{
73
3.17M
    EVP_MD_free(pd->alloc_md);
74
3.17M
    pd->alloc_md = NULL;
75
3.17M
    pd->md = NULL;
76
3.17M
}
77
78
int ossl_prov_digest_copy(PROV_DIGEST *dst, const PROV_DIGEST *src)
79
596k
{
80
596k
    if (src->alloc_md != NULL && !EVP_MD_up_ref(src->alloc_md))
81
0
        return 0;
82
596k
    dst->md = src->md;
83
596k
    dst->alloc_md = src->alloc_md;
84
596k
    return 1;
85
596k
}
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.33M
{
90
1.33M
    EVP_MD_free(pd->alloc_md);
91
1.33M
    pd->md = pd->alloc_md = EVP_MD_fetch(libctx, mdname, propquery);
92
93
1.33M
    return pd->md;
94
1.33M
}
95
96
int ossl_prov_digest_load(PROV_DIGEST *pd, const OSSL_PARAM *digest,
97
    const OSSL_PARAM *propq, OSSL_LIB_CTX *ctx)
98
506k
{
99
506k
    const char *propquery;
100
101
506k
    if (!set_propq(propq, &propquery))
102
0
        return 0;
103
104
506k
    if (digest == NULL)
105
167
        return 1;
106
506k
    if (digest->data_type != OSSL_PARAM_UTF8_STRING)
107
0
        return 0;
108
109
506k
    ossl_prov_digest_fetch(pd, ctx, digest->data, propquery);
110
506k
    return pd->md != NULL;
111
506k
}
112
113
void ossl_prov_digest_set_md(PROV_DIGEST *pd, EVP_MD *md)
114
608
{
115
608
    ossl_prov_digest_reset(pd);
116
608
    pd->md = pd->alloc_md = md;
117
608
}
118
119
const EVP_MD *ossl_prov_digest_md(const PROV_DIGEST *pd)
120
2.66M
{
121
2.66M
    return pd->md;
122
2.66M
}
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
35.6k
{
130
35.6k
    OSSL_PARAM mac_params[5], *mp = mac_params, *mergep;
131
35.6k
    int free_merge = 0;
132
35.6k
    int ret;
133
134
35.6k
    if (mdname != NULL)
135
35.5k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
136
35.5k
            (char *)mdname, 0);
137
35.6k
    if (ciphername != NULL)
138
195
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
139
195
            (char *)ciphername, 0);
140
35.6k
    if (properties != NULL)
141
1.11k
        *mp++ = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_PROPERTIES,
142
1.11k
            (char *)properties, 0);
143
144
35.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
35.6k
    if (param == NULL) {
156
29.3k
        mergep = mac_params;
157
29.3k
    } else {
158
6.28k
        free_merge = 1;
159
6.28k
        mergep = OSSL_PARAM_merge(mac_params, param);
160
6.28k
        if (mergep == NULL)
161
0
            return 0;
162
6.28k
    }
163
164
35.6k
    ret = EVP_MAC_CTX_set_params(macctx, mergep);
165
166
35.6k
    if (free_merge == 1)
167
6.28k
        OSSL_PARAM_free(mergep);
168
35.6k
    return ret;
169
35.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
29.4k
{
177
29.4k
    const char *properties = NULL;
178
179
29.4k
    if (macname == NULL && pmac != NULL)
180
203
        if (!OSSL_PARAM_get_utf8_string_ptr(pmac, &macname))
181
0
            return 0;
182
29.4k
    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
29.4k
    if (macname != NULL) {
187
29.2k
        EVP_MAC *mac = EVP_MAC_fetch(libctx, macname, properties);
188
189
29.2k
        EVP_MAC_CTX_free(*macctx);
190
29.2k
        *macctx = mac == NULL ? NULL : EVP_MAC_CTX_new(mac);
191
        /* The context holds on to the MAC */
192
29.2k
        EVP_MAC_free(mac);
193
29.2k
        if (*macctx == NULL)
194
57
            return 0;
195
29.2k
    }
196
197
    /*
198
     * If there is no MAC yet (and therefore, no MAC context), we ignore
199
     * all other parameters.
200
     */
201
29.3k
    if (*macctx == NULL)
202
0
        return 1;
203
204
29.3k
    if (ciphername == NULL && pcipher != NULL)
205
195
        if (!OSSL_PARAM_get_utf8_string_ptr(pcipher, &ciphername))
206
0
            return 0;
207
29.3k
    if (mdname == NULL && pdigest != NULL)
208
15.5k
        if (!OSSL_PARAM_get_utf8_string_ptr(pdigest, &mdname))
209
0
            return 0;
210
211
29.3k
    if (ossl_prov_set_macctx(*macctx, ciphername, mdname, properties, NULL))
212
29.0k
        return 1;
213
214
305
    EVP_MAC_CTX_free(*macctx);
215
305
    *macctx = NULL;
216
305
    return 0;
217
29.3k
}
218
219
void ossl_prov_cache_exported_algorithms(const OSSL_ALGORITHM_CAPABLE *in,
220
    OSSL_ALGORITHM *out)
221
102
{
222
102
    int i, j;
223
224
102
    if (out[0].algorithm_names == NULL) {
225
13.5k
        for (i = j = 0; in[i].alg.algorithm_names != NULL; ++i) {
226
13.4k
            if (in[i].capable == NULL || in[i].capable())
227
12.8k
                out[j++] = in[i].alg;
228
13.4k
        }
229
99
        out[j++] = in[i].alg;
230
99
    }
231
102
}
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
}