Coverage Report

Created: 2026-03-09 06:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/openssl/include/internal/hashtable.h
Line
Count
Source
1
/*
2
 * Copyright 2024-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
#ifndef OPENSSL_HASHTABLE_H
11
#define OPENSSL_HASHTABLE_H
12
#pragma once
13
14
#include <stddef.h>
15
#include <stdint.h>
16
#include <openssl/e_os2.h>
17
#include <internal/rcu.h>
18
#include "crypto/context.h"
19
20
typedef struct ht_internal_st HT;
21
22
/*
23
 * Represents a key to a hashtable
24
 */
25
typedef struct ht_key_header_st {
26
    size_t keysize;
27
    size_t bufsize;
28
    uint8_t *keybuf;
29
} HT_KEY;
30
31
/*
32
 * Represents a value in the hash table
33
 */
34
typedef struct ht_value_st {
35
    void *value;
36
    uintptr_t *type_id;
37
    HT_KEY key;
38
} HT_VALUE;
39
40
/*
41
 * Represents a list of values filtered from a hash table
42
 */
43
typedef struct ht_value_list_st {
44
    size_t list_len;
45
    HT_VALUE **list;
46
} HT_VALUE_LIST;
47
48
/*
49
 * Hashtable configuration
50
 */
51
typedef struct ht_config_st {
52
    OSSL_LIB_CTX *ctx;
53
    void (*ht_free_fn)(HT_VALUE *obj);
54
    uint64_t (*ht_hash_fn)(HT_KEY *key);
55
    size_t init_neighborhoods;
56
    uint32_t collision_check;
57
    uint32_t lockless_reads;
58
    uint32_t no_rcu;
59
} HT_CONFIG;
60
61
/*
62
 * Hashtable key rules
63
 * Any struct can be used to formulate a hash table key, as long as the
64
 * following rules
65
 * 1) The first element of the struct defining the key must be an HT_KEY
66
 * 2) All struct elements must have a compile time defined length
67
 * 3) Pointers can be used, but the value of the pointer, rather than
68
 *    the contents of the address it points to will be used to compute
69
 *    the hash
70
 * The key definition macros will assist with enforcing these rules
71
 */
72
73
/*
74
 * Starts the definition of a hash table key
75
 */
76
#define HT_START_KEY_DEFN(keyname) \
77
    typedef struct keyname##_st {  \
78
        HT_KEY key_header;         \
79
        struct {
80
81
/*
82
 * Ends a hash table key definitions
83
 */
84
#define HT_END_KEY_DEFN(keyname) \
85
    }                            \
86
    keyfields;                   \
87
    }                            \
88
    keyname;
89
90
/*
91
 * Defines a field in a hash table key
92
 */
93
#define HT_DEF_KEY_FIELD(name, type) type name;
94
95
/*
96
 * convenience macro to define a static char
97
 * array field in a hash table key
98
 */
99
#define HT_DEF_KEY_FIELD_CHAR_ARRAY(name, size) \
100
    HT_DEF_KEY_FIELD(name[size], char)
101
102
/*
103
 * Defines a uint8_t (blob) field in a hash table key
104
 */
105
#define HT_DEF_KEY_FIELD_UINT8T_ARRAY(name, size) \
106
    HT_DEF_KEY_FIELD(name[size], uint8_t)
107
108
/*
109
 * Initializes a key
110
 */
111
#define HT_INIT_KEY(key)                                                                           \
112
720k
    do {                                                                                           \
113
720k
        memset((key), 0, sizeof(*(key)));                                                          \
114
720k
        (key)->key_header.keysize = (key)->key_header.bufsize = (sizeof(*(key)) - sizeof(HT_KEY)); \
115
720k
        (key)->key_header.keybuf = (((uint8_t *)key) + sizeof(HT_KEY));                            \
116
720k
    } while (0)
117
118
/*
119
 * Initializes a key as a raw buffer
120
 * This operates identically to HT_INIT_KEY
121
 * but it treats the provided key as a raw buffer
122
 * and iteratively accounts the running amount of
123
 * data copied into the key from the caller.
124
 *
125
 * This MUST be used with the RAW macros below:
126
 * HT_COPY_RAW_KEY
127
 * HT_COPY_RAW_KEY_CASE
128
 */
129
#define HT_INIT_RAW_KEY(key)           \
130
720k
    do {                               \
131
720k
        HT_INIT_KEY((key));            \
132
720k
        (key)->key_header.keysize = 0; \
133
720k
    } while (0)
134
135
/*
136
 * Helper function to copy raw data into a key
137
 * This should not be called independently
138
 * use the HT_COPY_RAW_KEY macro instead
139
 */
140
static ossl_inline ossl_unused int ossl_key_raw_copy(HT_KEY *key, const uint8_t *buf, size_t len)
141
0
{
142
0
    if (key->keysize + len > key->bufsize)
143
0
        return 0;
144
0
    memcpy(&key->keybuf[key->keysize], buf, len);
145
0
    key->keysize += len;
146
0
    return 1;
147
0
}
Unexecuted instantiation: core_namemap.c:ossl_key_raw_copy
Unexecuted instantiation: x509_att.c:ossl_key_raw_copy
Unexecuted instantiation: x_attrib.c:ossl_key_raw_copy
Unexecuted instantiation: x_name.c:ossl_key_raw_copy
Unexecuted instantiation: hashtable.c:ossl_key_raw_copy
Unexecuted instantiation: v3_purp.c:ossl_key_raw_copy
Unexecuted instantiation: v3_utl.c:ossl_key_raw_copy
Unexecuted instantiation: x509_lu.c:ossl_key_raw_copy
Unexecuted instantiation: x509_set.c:ossl_key_raw_copy
Unexecuted instantiation: x509_v3.c:ossl_key_raw_copy
Unexecuted instantiation: x509_vfy.c:ossl_key_raw_copy
Unexecuted instantiation: x509_vpm.c:ossl_key_raw_copy
Unexecuted instantiation: x_all.c:ossl_key_raw_copy
Unexecuted instantiation: x_crl.c:ossl_key_raw_copy
Unexecuted instantiation: x_exten.c:ossl_key_raw_copy
Unexecuted instantiation: v3_addr.c:ossl_key_raw_copy
Unexecuted instantiation: v3_asid.c:ossl_key_raw_copy
Unexecuted instantiation: v3_bcons.c:ossl_key_raw_copy
Unexecuted instantiation: v3_cpols.c:ossl_key_raw_copy
Unexecuted instantiation: v3_crld.c:ossl_key_raw_copy
Unexecuted instantiation: v3_lib.c:ossl_key_raw_copy
Unexecuted instantiation: v3_tlsf.c:ossl_key_raw_copy
Unexecuted instantiation: v3_ac_tgt.c:ossl_key_raw_copy
Unexecuted instantiation: v3_battcons.c:ossl_key_raw_copy
148
149
/*
150
 * Copy data directly into a key
151
 * When initialized with HT_INIT_RAW_KEY, this macro
152
 * can be used to copy packed data into a key for hashtable usage
153
 * It is advantageous as it limits the amount of data that needs to
154
 * be hashed when doing inserts/lookups/deletes, as it tracks how much
155
 * key data is actually valid
156
 */
157
#define HT_COPY_RAW_KEY(key, buf, len) ossl_key_raw_copy(key, buf, len)
158
159
/*
160
 * Similar to HT_COPY_RAW_KEY but accepts a character buffer, and copies
161
 * data while converting case for case insensitive matches
162
 */
163
#define HT_COPY_RAW_KEY_CASE(key, buf, len)                                            \
164
720k
    do {                                                                               \
165
720k
        size_t tmplen = (size_t)(len);                                                 \
166
720k
        if (tmplen > (key)->bufsize - (key)->keysize)                                  \
167
720k
            tmplen = (key)->bufsize - (key)->keysize;                                  \
168
720k
        ossl_ht_strcase((key), (char *)&((key)->keybuf[(key)->keysize]), buf, tmplen); \
169
720k
        (key)->keysize += tmplen;                                                      \
170
720k
    } while (0)
171
172
/*
173
 * Resets a hash table key to a known state
174
 */
175
#define HT_KEY_RESET(key) memset((key)->key_header.keybuf, 0, (key)->key_header.keysize)
176
177
/*
178
 * Sets a scalar field in a hash table key
179
 */
180
0
#define HT_SET_KEY_FIELD(key, member, value) (key)->keyfields.member = value;
181
182
/*
183
 * Sets a string field in a hash table key, preserving
184
 * null terminator
185
 */
186
#define HT_SET_KEY_STRING(key, member, value)                                             \
187
    do {                                                                                  \
188
        if ((value) != NULL)                                                              \
189
            strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \
190
    } while (0)
191
192
/*
193
 * This is the same as HT_SET_KEY_STRING, except that it uses
194
 * ossl_ht_strcase to make the value being passed case insensitive
195
 * This is useful for instances in which we want upper and lower case
196
 * key value to hash to the same entry
197
 */
198
#define HT_SET_KEY_STRING_CASE(key, member, value)                                                  \
199
    do {                                                                                            \
200
        ossl_ht_strcase(NULL, (key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \
201
    } while (0)
202
203
/*
204
 * Same as HT_SET_KEY_STRING but also takes length of the string.
205
 */
206
#define HT_SET_KEY_STRING_N(key, member, value, len)                                          \
207
    do {                                                                                      \
208
        if ((value) != NULL) {                                                                \
209
            if (len < sizeof((key)->keyfields.member))                                        \
210
                strncpy((key)->keyfields.member, value, len);                                 \
211
            else                                                                              \
212
                strncpy((key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \
213
        }                                                                                     \
214
    } while (0)
215
216
/* Same as HT_SET_KEY_STRING_CASE but also takes length of the string. */
217
#define HT_SET_KEY_STRING_CASE_N(key, member, value, len)                                               \
218
    do {                                                                                                \
219
        if ((size_t)len < sizeof((key)->keyfields.member))                                              \
220
            ossl_ht_strcase(NULL, (key)->keyfields.member, value, len);                                 \
221
        else                                                                                            \
222
            ossl_ht_strcase(NULL, (key)->keyfields.member, value, sizeof((key)->keyfields.member) - 1); \
223
    } while (0)
224
225
/*
226
 * Sets a uint8_t (blob) field in a hash table key
227
 */
228
#define HT_SET_KEY_BLOB(key, member, value, len)         \
229
    do {                                                 \
230
        if (value != NULL)                               \
231
            memcpy((key)->keyfields.member, value, len); \
232
    } while (0)
233
234
/*
235
 * Converts a defined key type to an HT_KEY
236
 */
237
720k
#define TO_HT_KEY(key) &(key)->key_header
238
239
/*
240
 * Converts an HT_KEY back to its defined
241
 * type
242
 */
243
#define FROM_HT_KEY(key, type) (type)(key)
244
245
/*
246
 * Implements the following type safe operations for a hash table
247
 * ossl_ht_NAME_TYPE_insert - insert a value to a hash table of type TYPE
248
 * ossl_ht_NAME_TYPE_get - gets a value of a specific type from the hash table
249
 * ossl_ht_NAME_TYPE_from_value - converts an HT_VALUE to its type
250
 * ossl_ht_NAME_TYPE_to_value - converts a TYPE to an HT_VALUE
251
 * ossl_ht_NAME_TYPE_type - boolean to detect if a value is of TYPE
252
 */
253
#define IMPLEMENT_HT_VALUE_TYPE_FNS(vtype, name, pfx)                          \
254
    static uintptr_t name##_##vtype##_id = 0;                                  \
255
    pfx ossl_unused int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key,  \
256
        vtype *data,                                                           \
257
        vtype **olddata)                                                       \
258
    {                                                                          \
259
        HT_VALUE inval;                                                        \
260
        HT_VALUE *oval = NULL;                                                 \
261
        int rc;                                                                \
262
                                                                               \
263
        inval.value = data;                                                    \
264
        inval.type_id = &name##_##vtype##_id;                                  \
265
        rc = ossl_ht_insert(h, key, &inval, olddata == NULL ? NULL : &oval);   \
266
        if (oval != NULL)                                                      \
267
            *olddata = (vtype *)ossl_ht_inner_value(h, oval);                  \
268
        return rc;                                                             \
269
    }                                                                          \
270
                                                                               \
271
    pfx ossl_unused vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v)  \
272
    {                                                                          \
273
        uintptr_t *expect_type = &name##_##vtype##_id;                         \
274
        if (v == NULL)                                                         \
275
            return NULL;                                                       \
276
        if (v->type_id != expect_type)                                         \
277
            return NULL;                                                       \
278
        return (vtype *)v->value;                                              \
279
    }                                                                          \
280
                                                                               \
281
    pfx ossl_unused vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h,   \
282
        HT_KEY *key,                                                           \
283
        HT_VALUE **v)                                                          \
284
    {                                                                          \
285
        HT_VALUE *vv;                                                          \
286
        vv = ossl_ht_get(h, key);                                              \
287
        if (vv == NULL)                                                        \
288
            return NULL;                                                       \
289
        *v = ossl_ht_deref_value(h, &vv);                                      \
290
        return ossl_ht_##name##_##vtype##_from_value(*v);                      \
291
    }                                                                          \
292
                                                                               \
293
    pfx ossl_unused HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, \
294
        HT_VALUE *v)                                                           \
295
    {                                                                          \
296
        v->type_id = &name##_##vtype##_id;                                     \
297
        v->value = data;                                                       \
298
        return v;                                                              \
299
    }                                                                          \
300
                                                                               \
301
    pfx ossl_unused int ossl_ht_##name##_##vtype##_type(HT_VALUE *h)           \
302
    {                                                                          \
303
        return h->type_id == &name##_##vtype##_id;                             \
304
    }
305
306
#define DECLARE_HT_VALUE_TYPE_FNS(vtype, name)                               \
307
    int ossl_ht_##name##_##vtype##_insert(HT *h, HT_KEY *key, vtype *data,   \
308
        vtype **olddata);                                                    \
309
    vtype *ossl_ht_##name##_##vtype##_from_value(HT_VALUE *v);               \
310
    vtype *ossl_unused ossl_ht_##name##_##vtype##_get(HT *h,                 \
311
        HT_KEY *key,                                                         \
312
        HT_VALUE **v);                                                       \
313
    HT_VALUE *ossl_ht_##name##_##vtype##_to_value(vtype *data, HT_VALUE *v); \
314
    int ossl_ht_##name##_##vtype##_type(HT_VALUE *h);
315
316
/*
317
 * Helper function to construct case insensitive keys
318
 */
319
static ossl_inline ossl_unused void ossl_ht_strcase(HT_KEY *key, char *tgt, const char *src, size_t len)
320
720k
{
321
720k
    size_t i;
322
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
323
    const long int case_adjust = ~0x40;
324
#else
325
720k
    const long int case_adjust = ~0x20;
326
720k
#endif
327
328
720k
    if (src == NULL)
329
0
        return;
330
331
    /*
332
     * If we're passed a key, we're doing raw key copies
333
     * so check that we don't overflow here, and truncate if
334
     * we copy more space than we have available
335
     */
336
720k
    if (key != NULL && key->keysize + len > key->bufsize)
337
0
        len = (size_t)(key->bufsize - key->keysize);
338
339
5.10M
    for (i = 0; src[i] != '\0' && i < len; i++)
340
4.38M
        tgt[i] = case_adjust & src[i];
341
720k
}
core_namemap.c:ossl_ht_strcase
Line
Count
Source
320
720k
{
321
720k
    size_t i;
322
#if defined(CHARSET_EBCDIC) && !defined(CHARSET_EBCDIC_TEST)
323
    const long int case_adjust = ~0x40;
324
#else
325
720k
    const long int case_adjust = ~0x20;
326
720k
#endif
327
328
720k
    if (src == NULL)
329
0
        return;
330
331
    /*
332
     * If we're passed a key, we're doing raw key copies
333
     * so check that we don't overflow here, and truncate if
334
     * we copy more space than we have available
335
     */
336
720k
    if (key != NULL && key->keysize + len > key->bufsize)
337
0
        len = (size_t)(key->bufsize - key->keysize);
338
339
5.10M
    for (i = 0; src[i] != '\0' && i < len; i++)
340
4.38M
        tgt[i] = case_adjust & src[i];
341
720k
}
Unexecuted instantiation: x509_att.c:ossl_ht_strcase
Unexecuted instantiation: x_attrib.c:ossl_ht_strcase
Unexecuted instantiation: x_name.c:ossl_ht_strcase
Unexecuted instantiation: hashtable.c:ossl_ht_strcase
Unexecuted instantiation: v3_purp.c:ossl_ht_strcase
Unexecuted instantiation: v3_utl.c:ossl_ht_strcase
Unexecuted instantiation: x509_lu.c:ossl_ht_strcase
Unexecuted instantiation: x509_set.c:ossl_ht_strcase
Unexecuted instantiation: x509_v3.c:ossl_ht_strcase
Unexecuted instantiation: x509_vfy.c:ossl_ht_strcase
Unexecuted instantiation: x509_vpm.c:ossl_ht_strcase
Unexecuted instantiation: x_all.c:ossl_ht_strcase
Unexecuted instantiation: x_crl.c:ossl_ht_strcase
Unexecuted instantiation: x_exten.c:ossl_ht_strcase
Unexecuted instantiation: v3_addr.c:ossl_ht_strcase
Unexecuted instantiation: v3_asid.c:ossl_ht_strcase
Unexecuted instantiation: v3_bcons.c:ossl_ht_strcase
Unexecuted instantiation: v3_cpols.c:ossl_ht_strcase
Unexecuted instantiation: v3_crld.c:ossl_ht_strcase
Unexecuted instantiation: v3_lib.c:ossl_ht_strcase
Unexecuted instantiation: v3_tlsf.c:ossl_ht_strcase
Unexecuted instantiation: v3_ac_tgt.c:ossl_ht_strcase
Unexecuted instantiation: v3_battcons.c:ossl_ht_strcase
342
343
/*
344
 * Create a new hashtable
345
 */
346
HT *ossl_ht_new(const HT_CONFIG *conf);
347
348
/*
349
 * Frees a hash table, potentially freeing all elements
350
 */
351
void ossl_ht_free(HT *htable);
352
353
/*
354
 * Lock the table for reading
355
 */
356
int ossl_ht_read_lock(HT *htable);
357
358
/*
359
 * Lock the table for writing
360
 */
361
void ossl_ht_write_lock(HT *htable);
362
363
/*
364
 * Read unlock
365
 */
366
void ossl_ht_read_unlock(HT *htable);
367
368
/*
369
 * Write unlock
370
 */
371
void ossl_ht_write_unlock(HT *htable);
372
373
/*
374
 * Empties a hash table, potentially freeing all elements
375
 */
376
int ossl_ht_flush(HT *htable);
377
378
/*
379
 * Inserts an element to a hash table, optionally returning
380
 * replaced data to caller
381
 * Returns 1 if the insert was successful, 0 on error
382
 */
383
int ossl_ht_insert(HT *htable, HT_KEY *key, HT_VALUE *data,
384
    HT_VALUE **olddata);
385
386
/*
387
 * Deletes a value from a hash table, based on key
388
 * Returns 1 if the key was removed, 0 if they key was not found
389
 */
390
int ossl_ht_delete(HT *htable, HT_KEY *key);
391
392
/*
393
 * Returns number of elements in the hash table
394
 */
395
size_t ossl_ht_count(HT *htable);
396
397
/*
398
 * Iterates over each element in the table.
399
 * aborts the loop when cb returns 0
400
 * Contents of elements in the list may be modified during
401
 * this traversal, assuming proper thread safety is observed while doing
402
 * so (holding the table write lock is sufficient).  However, elements of the
403
 * table may not be inserted or removed while iterating.
404
 */
405
void ossl_ht_foreach_until(HT *htable, int (*cb)(HT_VALUE *obj, void *arg),
406
    void *arg);
407
/*
408
 * Returns a list of elements in a hash table based on
409
 * filter function return value.  Returns NULL on error,
410
 * or an HT_VALUE_LIST object on success.  Note it is possible
411
 * That a list will be returned with 0 entries, if none were found.
412
 * The zero length list must still be freed via ossl_ht_value_list_free
413
 */
414
HT_VALUE_LIST *ossl_ht_filter(HT *htable, size_t max_len,
415
    int (*filter)(HT_VALUE *obj, void *arg),
416
    void *arg);
417
/*
418
 * Frees the list returned from ossl_ht_filter
419
 */
420
void ossl_ht_value_list_free(HT_VALUE_LIST *list);
421
422
/*
423
 * Fetches a value from the hash table, based
424
 * on key.  Returns NULL if the element was not found.
425
 */
426
HT_VALUE *ossl_ht_get(HT *htable, HT_KEY *key);
427
428
/**
429
 * ossl_ht_deref_value - Dereference a value stored in a hash table entry
430
 * @h:   The hash table handle
431
 * @val: Pointer to the value pointer inside the hash table
432
 *
433
 * This helper returns the actual value stored in a hash table entry,
434
 * with awareness of whether the table is configured for RCU (Read-Copy-Update)
435
 * safe lookups.
436
 *
437
 * If the hash table is configured to use RCU lookups, the function
438
 * calls ossl_rcu_deref() to safely read the value under RCU protection.
439
 * This ensures that the caller sees a consistent pointer in concurrent environments.
440
 *
441
 * If RCU is not enabled (i.e. `h->config.no_rcu` is true), the function
442
 * simply dereferences @val directly.
443
 *
444
 * Return:
445
 * A pointer to the dereferenced hash table value (`HT_VALUE *`), or NULL if
446
 * the underlying pointer is NULL.
447
 */
448
HT_VALUE *ossl_ht_deref_value(HT *p, HT_VALUE **val);
449
450
/**
451
 * ossl_ht_inner_value - Extract the user payload from a hash table value
452
 * @h: The hash table handle
453
 * @v: The hash table value wrapper (HT_VALUE)
454
 *
455
 * This helper returns the user-provided payload stored inside a
456
 * hash table value container. The behavior differs depending on
457
 * whether the hash table is configured to use RCU (Read-Copy-Update)
458
 * for concurrency control.
459
 *
460
 * - If RCU is enabled, the function simply returns `v->value` without
461
 *   modifying or freeing the container.
462
 *
463
 * - If RCU is disabled the container structure `v` is no longer needed once
464
 *   the inner pointer has been extracted. In this case, the function frees
465
 *   `v` and returns the inner `value` pointer directly.
466
 *
467
 * Return:
468
 * A pointer to the user payload (`void *`) contained in the hash table
469
 * value wrapper.
470
 */
471
void *ossl_ht_inner_value(HT *h, HT_VALUE *v);
472
473
#endif