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