Coverage Report

Created: 2025-12-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/crypto/property/defn_cache.c
Line
Count
Source
1
/*
2
 * Copyright 2019-2022 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2019, Oracle and/or its affiliates.  All rights reserved.
4
 *
5
 * Licensed under the Apache License 2.0 (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 */
10
11
#include <string.h>
12
#include <openssl/err.h>
13
#include <openssl/lhash.h>
14
#include "internal/propertyerr.h"
15
#include "internal/property.h"
16
#include "internal/core.h"
17
#include "property_local.h"
18
#include "crypto/context.h"
19
20
/*
21
 * Implement a property definition cache.
22
 * These functions assume that they are called under a write lock.
23
 * No attempt is made to clean out the cache, except when it is shut down.
24
 */
25
26
typedef struct {
27
    const char *prop;
28
    OSSL_PROPERTY_LIST *defn;
29
    char body[1];
30
} PROPERTY_DEFN_ELEM;
31
32
DEFINE_LHASH_OF_EX(PROPERTY_DEFN_ELEM);
33
34
static unsigned long property_defn_hash(const PROPERTY_DEFN_ELEM *a)
35
292
{
36
292
    return OPENSSL_LH_strhash(a->prop);
37
292
}
38
39
static int property_defn_cmp(const PROPERTY_DEFN_ELEM *a,
40
    const PROPERTY_DEFN_ELEM *b)
41
271
{
42
271
    return strcmp(a->prop, b->prop);
43
271
}
44
45
static void property_defn_free(PROPERTY_DEFN_ELEM *elem)
46
1
{
47
1
    ossl_property_free(elem->defn);
48
1
    OPENSSL_free(elem);
49
1
}
50
51
void ossl_property_defns_free(void *vproperty_defns)
52
3
{
53
3
    LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns = vproperty_defns;
54
55
3
    if (property_defns != NULL) {
56
3
        lh_PROPERTY_DEFN_ELEM_doall(property_defns,
57
3
            &property_defn_free);
58
3
        lh_PROPERTY_DEFN_ELEM_free(property_defns);
59
3
    }
60
3
}
61
62
void *ossl_property_defns_new(OSSL_LIB_CTX *ctx)
63
9
{
64
9
    return lh_PROPERTY_DEFN_ELEM_new(&property_defn_hash, &property_defn_cmp);
65
9
}
66
67
OSSL_PROPERTY_LIST *ossl_prop_defn_get(OSSL_LIB_CTX *ctx, const char *prop)
68
278
{
69
278
    PROPERTY_DEFN_ELEM elem, *r;
70
278
    LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
71
72
278
    property_defns = ossl_lib_ctx_get_data(ctx,
73
278
        OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
74
278
    if (!ossl_assert(property_defns != NULL) || !ossl_lib_ctx_read_lock(ctx))
75
0
        return NULL;
76
77
278
    elem.prop = prop;
78
278
    r = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem);
79
278
    ossl_lib_ctx_unlock(ctx);
80
278
    if (r == NULL || !ossl_assert(r->defn != NULL))
81
7
        return NULL;
82
271
    return r->defn;
83
278
}
84
85
/*
86
 * Cache the property list for a given property string *pl.
87
 * If an entry already exists in the cache *pl is freed and
88
 * overwritten with the existing entry from the cache.
89
 */
90
int ossl_prop_defn_set(OSSL_LIB_CTX *ctx, const char *prop,
91
    OSSL_PROPERTY_LIST **pl)
92
7
{
93
7
    PROPERTY_DEFN_ELEM elem, *old, *p = NULL;
94
7
    size_t len;
95
7
    LHASH_OF(PROPERTY_DEFN_ELEM) *property_defns;
96
7
    int res = 1;
97
98
7
    property_defns = ossl_lib_ctx_get_data(ctx,
99
7
        OSSL_LIB_CTX_PROPERTY_DEFN_INDEX);
100
7
    if (property_defns == NULL)
101
0
        return 0;
102
103
7
    if (prop == NULL)
104
0
        return 1;
105
106
7
    if (!ossl_lib_ctx_write_lock(ctx))
107
0
        return 0;
108
7
    elem.prop = prop;
109
7
    if (pl == NULL) {
110
0
        lh_PROPERTY_DEFN_ELEM_delete(property_defns, &elem);
111
0
        goto end;
112
0
    }
113
    /* check if property definition is in the cache already */
114
7
    if ((p = lh_PROPERTY_DEFN_ELEM_retrieve(property_defns, &elem)) != NULL) {
115
0
        ossl_property_free(*pl);
116
0
        *pl = p->defn;
117
0
        goto end;
118
0
    }
119
7
    len = strlen(prop);
120
7
    p = OPENSSL_malloc(sizeof(*p) + len);
121
7
    if (p != NULL) {
122
7
        p->prop = p->body;
123
7
        p->defn = *pl;
124
7
        memcpy(p->body, prop, len + 1);
125
7
        old = lh_PROPERTY_DEFN_ELEM_insert(property_defns, p);
126
7
        if (!ossl_assert(old == NULL))
127
            /* This should not happen. An existing entry is handled above. */
128
0
            goto end;
129
7
        if (!lh_PROPERTY_DEFN_ELEM_error(property_defns))
130
7
            goto end;
131
7
    }
132
0
    OPENSSL_free(p);
133
0
    res = 0;
134
7
end:
135
7
    ossl_lib_ctx_unlock(ctx);
136
7
    return res;
137
0
}