Coverage Report

Created: 2025-08-25 06:30

/src/openssl/providers/implementations/skeymgmt/generic.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2025 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
11
#include <string.h>
12
#include <openssl/core_dispatch.h>
13
#include <openssl/core_names.h>
14
#include <openssl/proverr.h>
15
#include "crypto/types.h"
16
#include "internal/cryptlib.h"
17
#include "internal/skey.h"
18
#include "prov/provider_ctx.h"
19
#include "prov/providercommon.h"
20
#include "prov/implementations.h"
21
#include "prov/skeymgmt_lcl.h"
22
23
void generic_free(void *keydata)
24
0
{
25
0
    PROV_SKEY *generic = keydata;
26
27
0
    if (generic == NULL)
28
0
        return;
29
30
0
    OPENSSL_clear_free(generic->data, generic->length);
31
0
    OPENSSL_free(generic);
32
0
}
33
34
/* Machine generated by util/perl/OpenSSL/paramnames.pm */
35
#ifndef generic_skey_import_list
36
static const OSSL_PARAM generic_skey_import_list[] = {
37
    OSSL_PARAM_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, NULL, 0),
38
    OSSL_PARAM_END
39
};
40
#endif
41
42
#ifndef generic_skey_import_st
43
struct generic_skey_import_st {
44
    OSSL_PARAM *raw_bytes;
45
};
46
#endif
47
48
#ifndef generic_skey_import_decoder
49
static int generic_skey_import_decoder
50
    (const OSSL_PARAM *p, struct generic_skey_import_st *r)
51
0
{
52
0
    const char *s;
53
54
0
    memset(r, 0, sizeof(*r));
55
0
    if (p != NULL)
56
0
        for (; (s = p->key) != NULL; p++)
57
0
            if (ossl_likely(strcmp("raw-bytes", s + 0) == 0)) {
58
                /* SKEY_PARAM_RAW_BYTES */
59
0
                if (ossl_unlikely(r->raw_bytes != NULL)) {
60
0
                    ERR_raise_data(ERR_LIB_PROV, PROV_R_REPEATED_PARAMETER,
61
0
                                   "param %s is repeated", s);
62
0
                    return 0;
63
0
                }
64
0
                r->raw_bytes = (OSSL_PARAM *)p;
65
0
            }
66
0
    return 1;
67
0
}
68
#endif
69
/* End of machine generated */
70
71
void *generic_import(void *provctx, int selection, const OSSL_PARAM params[])
72
0
{
73
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
74
0
    struct generic_skey_import_st p;
75
0
    PROV_SKEY *generic = NULL;
76
0
    int ok = 0;
77
78
0
    if (!ossl_prov_is_running())
79
0
        return NULL;
80
81
0
    if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
82
0
        return NULL;
83
84
0
    if (!generic_skey_import_decoder(params, &p))
85
0
        return NULL;
86
87
0
    if (p.raw_bytes == NULL
88
0
            || p.raw_bytes->data_type != OSSL_PARAM_OCTET_STRING)
89
0
        return NULL;
90
91
0
    generic = OPENSSL_zalloc(sizeof(PROV_SKEY));
92
0
    if (generic == NULL)
93
0
        return NULL;
94
95
0
    generic->libctx = libctx;
96
97
0
    generic->type = SKEY_TYPE_GENERIC;
98
99
0
    if ((generic->data = OPENSSL_memdup(p.raw_bytes->data,
100
0
                                        p.raw_bytes->data_size)) == NULL)
101
0
        goto end;
102
0
    generic->length = p.raw_bytes->data_size;
103
0
    ok = 1;
104
105
0
end:
106
0
    if (ok == 0) {
107
0
        generic_free(generic);
108
0
        generic = NULL;
109
0
    }
110
0
    return generic;
111
0
}
112
113
const OSSL_PARAM *generic_imp_settable_params(void *provctx)
114
0
{
115
0
    return generic_skey_import_list;
116
0
}
117
118
int generic_export(void *keydata, int selection,
119
                   OSSL_CALLBACK *param_callback, void *cbarg)
120
0
{
121
0
    PROV_SKEY *gen = keydata;
122
0
    OSSL_PARAM params[2];
123
124
0
    if (!ossl_prov_is_running() || gen == NULL)
125
0
        return 0;
126
127
    /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */
128
0
    if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
129
0
        return 0;
130
131
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
132
0
                                                  gen->data, gen->length);
133
0
    params[1] = OSSL_PARAM_construct_end();
134
135
0
    return param_callback(params, cbarg);
136
0
}
137
138
const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {
139
    { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },
140
    { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },
141
    { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },
142
    { OSSL_FUNC_SKEYMGMT_IMP_SETTABLE_PARAMS,
143
      (void (*)(void))generic_imp_settable_params },
144
    OSSL_DISPATCH_END
145
};