Coverage Report

Created: 2025-06-13 06:57

/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
#include <openssl/core_dispatch.h>
11
#include <openssl/core_names.h>
12
#include "crypto/types.h"
13
#include "internal/skey.h"
14
#include "prov/provider_ctx.h"
15
#include "prov/providercommon.h"
16
#include "prov/implementations.h"
17
#include "skeymgmt_lcl.h"
18
19
void generic_free(void *keydata)
20
0
{
21
0
    PROV_SKEY *generic = keydata;
22
23
0
    if (generic == NULL)
24
0
        return;
25
26
0
    OPENSSL_free(generic->data);
27
0
    OPENSSL_free(generic);
28
0
}
29
30
void *generic_import(void *provctx, int selection, const OSSL_PARAM params[])
31
0
{
32
0
    OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(provctx);
33
0
    const OSSL_PARAM *raw_bytes;
34
0
    PROV_SKEY *generic = NULL;
35
0
    int ok = 0;
36
37
0
    if (!ossl_prov_is_running())
38
0
        return NULL;
39
40
0
    if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
41
0
        return NULL;
42
43
0
    raw_bytes = OSSL_PARAM_locate_const(params, OSSL_SKEY_PARAM_RAW_BYTES);
44
0
    if (raw_bytes == NULL)
45
0
        return NULL;
46
47
0
    generic = OPENSSL_zalloc(sizeof(PROV_SKEY));
48
0
    if (generic == NULL)
49
0
        return NULL;
50
51
0
    generic->libctx = libctx;
52
53
0
    generic->type = SKEY_TYPE_GENERIC;
54
55
0
    if ((generic->data = OPENSSL_memdup(raw_bytes->data, raw_bytes->data_size)) == NULL)
56
0
        goto end;
57
0
    generic->length = raw_bytes->data_size;
58
0
    ok = 1;
59
60
0
end:
61
0
    if (ok == 0) {
62
0
        generic_free(generic);
63
0
        generic = NULL;
64
0
    }
65
0
    return generic;
66
0
}
67
68
static const OSSL_PARAM generic_import_params[] = {
69
    OSSL_PARAM_octet_string(OSSL_SKEY_PARAM_RAW_BYTES, NULL, 0),
70
    OSSL_PARAM_END
71
};
72
73
const OSSL_PARAM *generic_imp_settable_params(void *provctx)
74
0
{
75
0
    return generic_import_params;
76
0
}
77
78
int generic_export(void *keydata, int selection,
79
                   OSSL_CALLBACK *param_callback, void *cbarg)
80
0
{
81
0
    PROV_SKEY *gen = keydata;
82
0
    OSSL_PARAM params[2];
83
84
0
    if (!ossl_prov_is_running() || gen == NULL)
85
0
        return 0;
86
87
    /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */
88
0
    if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
89
0
        return 0;
90
91
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
92
0
                                                  gen->data, gen->length);
93
0
    params[1] = OSSL_PARAM_construct_end();
94
95
0
    return param_callback(params, cbarg);
96
0
}
97
98
const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {
99
    { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },
100
    { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },
101
    { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },
102
    { OSSL_FUNC_SKEYMGMT_IMP_SETTABLE_PARAMS,
103
      (void (*)(void))generic_imp_settable_params },
104
    OSSL_DISPATCH_END
105
};