Coverage Report

Created: 2025-06-13 06:58

/src/openssl32/providers/implementations/keymgmt/kdf_legacy_kmgmt.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2019-2023 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
 * This implemments a dummy key manager for legacy KDFs that still support the
12
 * old way of performing a KDF via EVP_PKEY_derive(). New KDFs should not be
13
 * implemented this way. In reality there is no key data for such KDFs, so this
14
 * key manager does very little.
15
 */
16
17
#include <openssl/core_dispatch.h>
18
#include <openssl/core_names.h>
19
#include <openssl/err.h>
20
#include "prov/implementations.h"
21
#include "prov/providercommon.h"
22
#include "prov/provider_ctx.h"
23
#include "prov/kdfexchange.h"
24
25
static OSSL_FUNC_keymgmt_new_fn kdf_newdata;
26
static OSSL_FUNC_keymgmt_free_fn kdf_freedata;
27
static OSSL_FUNC_keymgmt_has_fn kdf_has;
28
29
KDF_DATA *ossl_kdf_data_new(void *provctx)
30
0
{
31
0
    KDF_DATA *kdfdata;
32
33
0
    if (!ossl_prov_is_running())
34
0
        return NULL;
35
36
0
    kdfdata = OPENSSL_zalloc(sizeof(*kdfdata));
37
0
    if (kdfdata == NULL)
38
0
        return NULL;
39
40
0
    if (!CRYPTO_NEW_REF(&kdfdata->refcnt, 1)) {
41
0
        OPENSSL_free(kdfdata);
42
0
        return NULL;
43
0
    }
44
0
    kdfdata->libctx = PROV_LIBCTX_OF(provctx);
45
46
0
    return kdfdata;
47
0
}
48
49
void ossl_kdf_data_free(KDF_DATA *kdfdata)
50
0
{
51
0
    int ref = 0;
52
53
0
    if (kdfdata == NULL)
54
0
        return;
55
56
0
    CRYPTO_DOWN_REF(&kdfdata->refcnt, &ref);
57
0
    if (ref > 0)
58
0
        return;
59
60
0
    CRYPTO_FREE_REF(&kdfdata->refcnt);
61
0
    OPENSSL_free(kdfdata);
62
0
}
63
64
int ossl_kdf_data_up_ref(KDF_DATA *kdfdata)
65
0
{
66
0
    int ref = 0;
67
68
    /* This is effectively doing a new operation on the KDF_DATA and should be
69
     * adequately guarded again modules' error states.  However, both current
70
     * calls here are guarded properly in exchange/kdf_exch.c.  Thus, it
71
     * could be removed here.  The concern is that something in the future
72
     * might call this function without adequate guards.  It's a cheap call,
73
     * it seems best to leave it even though it is currently redundant.
74
     */
75
0
    if (!ossl_prov_is_running())
76
0
        return 0;
77
78
0
    CRYPTO_UP_REF(&kdfdata->refcnt, &ref);
79
0
    return 1;
80
0
}
81
82
static void *kdf_newdata(void *provctx)
83
0
{
84
0
    return ossl_kdf_data_new(provctx);
85
0
}
86
87
static void kdf_freedata(void *kdfdata)
88
0
{
89
0
    ossl_kdf_data_free(kdfdata);
90
0
}
91
92
static int kdf_has(const void *keydata, int selection)
93
0
{
94
0
    return 1; /* nothing is missing */
95
0
}
96
97
const OSSL_DISPATCH ossl_kdf_keymgmt_functions[] = {
98
    { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))kdf_newdata },
99
    { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))kdf_freedata },
100
    { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))kdf_has },
101
    OSSL_DISPATCH_END
102
};