Coverage Report

Created: 2026-09-12 06:55

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl41/providers/implementations/skeymgmt/generic.c
Line
Count
Source
1
/*
2
 * Copyright 2025-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 <string.h>
11
#include <openssl/core_dispatch.h>
12
#include <openssl/core_names.h>
13
#include <openssl/proverr.h>
14
#include "crypto/types.h"
15
#include "internal/cryptlib.h"
16
#include "internal/skey.h"
17
#include "prov/provider_ctx.h"
18
#include "prov/providercommon.h"
19
#include "prov/implementations.h"
20
#include "prov/skeymgmt_lcl.h"
21
22
#include "providers/implementations/skeymgmt/generic.inc"
23
24
void generic_free(void *keydata)
25
0
{
26
0
    PROV_SKEY *generic = keydata;
27
28
0
    if (generic == NULL)
29
0
        return;
30
31
0
    OPENSSL_clear_free(generic->data, generic->length);
32
0
    OPENSSL_free(generic->alias);
33
0
    OPENSSL_free(generic->local_keyid);
34
0
    OPENSSL_free(generic->algorithm_oid);
35
0
    OPENSSL_free(generic->algorithm_params);
36
0
    OPENSSL_free(generic);
37
0
}
38
39
static int generic_import_metadata(PROV_SKEY *skey,
40
    const struct generic_skey_import_st *p)
41
0
{
42
0
    if (p->alias != NULL
43
0
        && p->alias->data_type == OSSL_PARAM_UTF8_STRING
44
0
        && skey->alias == NULL) {
45
0
        skey->alias = OPENSSL_strndup(p->alias->data, p->alias->data_size);
46
0
        if (skey->alias == NULL)
47
0
            return 0;
48
0
        if (strlen(skey->alias) != p->alias->data_size) {
49
0
            ERR_raise(ERR_R_PROV_LIB, PROV_R_INVALID_DATA);
50
0
            return 0;
51
0
        }
52
0
    }
53
54
0
    if (p->local_keyid != NULL
55
0
        && p->local_keyid->data_type == OSSL_PARAM_OCTET_STRING
56
0
        && skey->local_keyid == NULL) {
57
0
        skey->local_keyid = OPENSSL_memdup(p->local_keyid->data,
58
0
            p->local_keyid->data_size);
59
0
        if (skey->local_keyid == NULL)
60
0
            return 0;
61
0
        skey->local_keyid_len = p->local_keyid->data_size;
62
0
    }
63
64
0
    if (p->algorithm_oid != NULL
65
0
        && p->algorithm_oid->data_type == OSSL_PARAM_OCTET_STRING
66
0
        && skey->algorithm_oid == NULL) {
67
0
        skey->algorithm_oid = OPENSSL_memdup(p->algorithm_oid->data,
68
0
            p->algorithm_oid->data_size);
69
0
        if (skey->algorithm_oid == NULL)
70
0
            return 0;
71
0
        skey->algorithm_oid_len = p->algorithm_oid->data_size;
72
0
    }
73
74
0
    if (p->algorithm_params != NULL
75
0
        && p->algorithm_params->data_type == OSSL_PARAM_OCTET_STRING
76
0
        && skey->algorithm_params == NULL) {
77
0
        skey->algorithm_params = OPENSSL_memdup(p->algorithm_params->data,
78
0
            p->algorithm_params->data_size);
79
0
        if (skey->algorithm_params == NULL)
80
0
            return 0;
81
0
        skey->algorithm_params_len = p->algorithm_params->data_size;
82
0
    }
83
84
0
    return 1;
85
0
}
86
87
void *generic_import(void *provctx, int selection ossl_unused, const OSSL_PARAM params[])
88
0
{
89
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
90
0
    struct generic_skey_import_st p;
91
0
    PROV_SKEY *generic = NULL;
92
0
    int ok = 0;
93
94
0
    if (!ossl_prov_is_running())
95
0
        return NULL;
96
97
0
    if (!generic_skey_import_decoder(params, &p))
98
0
        return NULL;
99
100
0
    if (p.raw_bytes == NULL
101
0
        || p.raw_bytes->data_type != OSSL_PARAM_OCTET_STRING)
102
0
        return NULL;
103
104
0
    generic = OPENSSL_zalloc(sizeof(PROV_SKEY));
105
0
    if (generic == NULL)
106
0
        return NULL;
107
108
0
    generic->libctx = libctx;
109
110
0
    generic->type = SKEY_TYPE_GENERIC;
111
112
0
    if ((generic->data = OPENSSL_memdup(p.raw_bytes->data,
113
0
             p.raw_bytes->data_size))
114
0
        == NULL)
115
0
        goto end;
116
0
    generic->length = p.raw_bytes->data_size;
117
118
0
    if (!generic_import_metadata(generic, &p))
119
0
        goto end;
120
121
0
    ok = 1;
122
123
0
end:
124
0
    if (ok == 0) {
125
0
        generic_free(generic);
126
0
        generic = NULL;
127
0
    }
128
0
    return generic;
129
0
}
130
131
const OSSL_PARAM *generic_imp_settable_params(void *provctx)
132
0
{
133
0
    return generic_skey_import_list;
134
0
}
135
136
int generic_export(void *keydata, int selection,
137
    OSSL_CALLBACK *param_callback, void *cbarg)
138
0
{
139
0
    PROV_SKEY *gen = keydata;
140
0
    OSSL_PARAM params[6];
141
0
    int idx = 0;
142
143
0
    if (!ossl_prov_is_running() || gen == NULL || selection == 0)
144
0
        return 0;
145
146
    /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */
147
0
    if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) != 0)
148
0
        params[idx++] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
149
0
            gen->data, gen->length);
150
151
0
    if ((selection & OSSL_SKEYMGMT_SELECT_PARAMETERS) != 0) {
152
0
        if (gen->alias != NULL)
153
0
            params[idx++] = OSSL_PARAM_construct_utf8_string(
154
0
                OSSL_SKEY_PARAM_ALIAS, gen->alias, 0);
155
156
0
        if (gen->local_keyid != NULL)
157
0
            params[idx++] = OSSL_PARAM_construct_octet_string(
158
0
                OSSL_SKEY_PARAM_LOCAL_KEYID,
159
0
                gen->local_keyid, gen->local_keyid_len);
160
161
0
        if (gen->algorithm_oid != NULL)
162
0
            params[idx++] = OSSL_PARAM_construct_octet_string(
163
0
                OSSL_SKEY_PARAM_ALGORITHM_OID,
164
0
                gen->algorithm_oid, gen->algorithm_oid_len);
165
166
0
        if (gen->algorithm_params != NULL)
167
0
            params[idx++] = OSSL_PARAM_construct_octet_string(
168
0
                OSSL_SKEY_PARAM_ALGORITHM_PARAMS,
169
0
                gen->algorithm_params, gen->algorithm_params_len);
170
0
    }
171
172
0
    params[idx] = OSSL_PARAM_construct_end();
173
174
0
    return param_callback(params, cbarg);
175
0
}
176
177
const char *generic_get_key_id(void *keydata)
178
0
{
179
0
    PROV_SKEY *gen = keydata;
180
181
0
    if (gen == NULL)
182
0
        return NULL;
183
184
0
    return gen->alias;
185
0
}
186
187
int generic_get_local_keyid(void *keydata,
188
    const unsigned char **id, size_t *len)
189
0
{
190
0
    PROV_SKEY *gen = keydata;
191
192
0
    if (gen == NULL)
193
0
        return 0;
194
0
    if (id == NULL || len == NULL)
195
0
        return 0;
196
197
0
    *id = gen->local_keyid;
198
0
    *len = gen->local_keyid_len;
199
0
    return 1;
200
0
}
201
202
int generic_get_algorithm_id(void *keydata,
203
    const unsigned char **oid, size_t *oid_len,
204
    const unsigned char **params, size_t *params_len)
205
0
{
206
0
    PROV_SKEY *gen = keydata;
207
0
    int ret_oid = 0, ret_params = 0;
208
209
0
    if (gen == NULL)
210
0
        return 0;
211
212
0
    if (oid != NULL && oid_len != NULL)
213
0
        ret_oid = 1;
214
0
    if (params != NULL && params_len != NULL)
215
0
        ret_params = 1;
216
217
0
    if (ret_oid == 0 && ret_params == 0)
218
0
        return 0;
219
220
0
    if (ret_oid == 1) {
221
0
        *oid = gen->algorithm_oid;
222
0
        *oid_len = gen->algorithm_oid_len;
223
0
    }
224
0
    if (ret_params == 1) {
225
0
        *params = gen->algorithm_params;
226
0
        *params_len = gen->algorithm_params_len;
227
0
    }
228
0
    return 1;
229
0
}
230
231
const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {
232
    { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },
233
    { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },
234
    { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },
235
    { OSSL_FUNC_SKEYMGMT_IMP_SETTABLE_PARAMS,
236
        (void (*)(void))generic_imp_settable_params },
237
    { OSSL_FUNC_SKEYMGMT_GET_KEY_ID, (void (*)(void))generic_get_key_id },
238
    { OSSL_FUNC_SKEYMGMT_GET_LOCAL_KEYID,
239
        (void (*)(void))generic_get_local_keyid },
240
    { OSSL_FUNC_SKEYMGMT_GET_ALGORITHM_ID,
241
        (void (*)(void))generic_get_algorithm_id },
242
    OSSL_DISPATCH_END
243
};