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