Coverage Report

Created: 2025-08-28 07:07

/src/openssl35/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
int generic_export(void *keydata, int selection,
69
                   OSSL_CALLBACK *param_callback, void *cbarg)
70
0
{
71
0
    PROV_SKEY *gen = keydata;
72
0
    OSSL_PARAM params[2];
73
74
0
    if (!ossl_prov_is_running() || gen == NULL)
75
0
        return 0;
76
77
    /* If we use generic SKEYMGMT as a "base class", we shouldn't check the type */
78
0
    if ((selection & OSSL_SKEYMGMT_SELECT_SECRET_KEY) == 0)
79
0
        return 0;
80
81
0
    params[0] = OSSL_PARAM_construct_octet_string(OSSL_SKEY_PARAM_RAW_BYTES,
82
0
                                                  gen->data, gen->length);
83
0
    params[1] = OSSL_PARAM_construct_end();
84
85
0
    return param_callback(params, cbarg);
86
0
}
87
88
const OSSL_DISPATCH ossl_generic_skeymgmt_functions[] = {
89
    { OSSL_FUNC_SKEYMGMT_FREE, (void (*)(void))generic_free },
90
    { OSSL_FUNC_SKEYMGMT_IMPORT, (void (*)(void))generic_import },
91
    { OSSL_FUNC_SKEYMGMT_EXPORT, (void (*)(void))generic_export },
92
    OSSL_DISPATCH_END
93
};