/src/openssl/crypto/objects/o_names.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 1998-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 | | #include <stdio.h> |
11 | | #include <stdlib.h> |
12 | | #include <string.h> |
13 | | |
14 | | #include <openssl/err.h> |
15 | | #include <openssl/lhash.h> |
16 | | #include <openssl/objects.h> |
17 | | #include <openssl/safestack.h> |
18 | | #include <openssl/e_os2.h> |
19 | | #include "internal/thread_once.h" |
20 | | #include "crypto/lhash.h" |
21 | | #include "obj_local.h" |
22 | | #include "internal/e_os.h" |
23 | | |
24 | | /* |
25 | | * I use the ex_data stuff to manage the identifiers for the obj_name_types |
26 | | * that applications may define. I only really use the free function field. |
27 | | */ |
28 | | static LHASH_OF(OBJ_NAME) *names_lh = NULL; |
29 | | static int names_type_num = OBJ_NAME_TYPE_NUM; |
30 | | static CRYPTO_RWLOCK *obj_lock = NULL; |
31 | | |
32 | | static STACK_OF(NAME_FUNCS) *name_funcs_stack; |
33 | | |
34 | | /* |
35 | | * The LHASH callbacks now use the raw "void *" prototypes and do |
36 | | * per-variable casting in the functions. This prevents function pointer |
37 | | * casting without the need for macro-generated wrapper functions. |
38 | | */ |
39 | | |
40 | | static unsigned long obj_name_hash(const OBJ_NAME *a); |
41 | | static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b); |
42 | | |
43 | | static CRYPTO_ONCE init = CRYPTO_ONCE_STATIC_INIT; |
44 | | DEFINE_RUN_ONCE_STATIC(o_names_init) |
45 | 16 | { |
46 | 16 | names_lh = NULL; |
47 | 16 | obj_lock = CRYPTO_THREAD_lock_new(); |
48 | 16 | if (obj_lock != NULL) |
49 | 16 | names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp); |
50 | 16 | if (names_lh == NULL) { |
51 | 0 | CRYPTO_THREAD_lock_free(obj_lock); |
52 | 0 | obj_lock = NULL; |
53 | 0 | } |
54 | 16 | return names_lh != NULL && obj_lock != NULL; |
55 | 16 | } |
56 | | |
57 | | int OBJ_NAME_init(void) |
58 | 15.7k | { |
59 | 15.7k | return RUN_ONCE(&init, o_names_init); |
60 | 15.7k | } |
61 | | |
62 | | int OBJ_NAME_new_index(unsigned long (*hash_func)(const char *), |
63 | | int (*cmp_func)(const char *, const char *), |
64 | | void (*free_func)(const char *, int, const char *)) |
65 | 0 | { |
66 | 0 | int ret = 0, i, push; |
67 | 0 | NAME_FUNCS *name_funcs; |
68 | |
|
69 | 0 | if (!OBJ_NAME_init()) |
70 | 0 | return 0; |
71 | | |
72 | 0 | if (!CRYPTO_THREAD_write_lock(obj_lock)) |
73 | 0 | return 0; |
74 | | |
75 | 0 | if (name_funcs_stack == NULL) |
76 | 0 | name_funcs_stack = sk_NAME_FUNCS_new_null(); |
77 | 0 | if (name_funcs_stack == NULL) { |
78 | | /* ERROR */ |
79 | 0 | goto out; |
80 | 0 | } |
81 | 0 | ret = names_type_num; |
82 | 0 | names_type_num++; |
83 | 0 | for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) { |
84 | 0 | name_funcs = OPENSSL_zalloc(sizeof(*name_funcs)); |
85 | 0 | if (name_funcs == NULL) { |
86 | 0 | ret = 0; |
87 | 0 | goto out; |
88 | 0 | } |
89 | 0 | name_funcs->hash_func = ossl_lh_strcasehash; |
90 | 0 | name_funcs->cmp_func = OPENSSL_strcasecmp; |
91 | 0 | push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs); |
92 | |
|
93 | 0 | if (!push) { |
94 | 0 | ERR_raise(ERR_LIB_OBJ, ERR_R_CRYPTO_LIB); |
95 | 0 | OPENSSL_free(name_funcs); |
96 | 0 | ret = 0; |
97 | 0 | goto out; |
98 | 0 | } |
99 | 0 | } |
100 | 0 | name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret); |
101 | 0 | if (hash_func != NULL) |
102 | 0 | name_funcs->hash_func = hash_func; |
103 | 0 | if (cmp_func != NULL) |
104 | 0 | name_funcs->cmp_func = cmp_func; |
105 | 0 | if (free_func != NULL) |
106 | 0 | name_funcs->free_func = free_func; |
107 | |
|
108 | 0 | out: |
109 | 0 | CRYPTO_THREAD_unlock(obj_lock); |
110 | 0 | return ret; |
111 | 0 | } |
112 | | |
113 | | static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b) |
114 | 10.8k | { |
115 | 10.8k | int ret; |
116 | | |
117 | 10.8k | ret = a->type - b->type; |
118 | 10.8k | if (ret == 0) { |
119 | 10.8k | if ((name_funcs_stack != NULL) |
120 | 0 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) { |
121 | 0 | ret = sk_NAME_FUNCS_value(name_funcs_stack, |
122 | 0 | a->type) |
123 | 0 | ->cmp_func(a->name, b->name); |
124 | 0 | } else |
125 | 10.8k | ret = OPENSSL_strcasecmp(a->name, b->name); |
126 | 10.8k | } |
127 | 10.8k | return ret; |
128 | 10.8k | } |
129 | | |
130 | | static unsigned long obj_name_hash(const OBJ_NAME *a) |
131 | 17.3k | { |
132 | 17.3k | unsigned long ret; |
133 | | |
134 | 17.3k | if ((name_funcs_stack != NULL) |
135 | 0 | && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) { |
136 | 0 | ret = sk_NAME_FUNCS_value(name_funcs_stack, |
137 | 0 | a->type) |
138 | 0 | ->hash_func(a->name); |
139 | 17.3k | } else { |
140 | 17.3k | ret = ossl_lh_strcasehash(a->name); |
141 | 17.3k | } |
142 | 17.3k | ret ^= a->type; |
143 | 17.3k | return ret; |
144 | 17.3k | } |
145 | | |
146 | | const char *OBJ_NAME_get(const char *name, int type) |
147 | 9.00k | { |
148 | 9.00k | OBJ_NAME on, *ret; |
149 | 9.00k | int num = 0, alias; |
150 | 9.00k | const char *value = NULL; |
151 | | |
152 | 9.00k | if (name == NULL) |
153 | 0 | return NULL; |
154 | 9.00k | if (!OBJ_NAME_init()) |
155 | 0 | return NULL; |
156 | 9.00k | if (!CRYPTO_THREAD_read_lock(obj_lock)) |
157 | 0 | return NULL; |
158 | | |
159 | 9.00k | alias = type & OBJ_NAME_ALIAS; |
160 | 9.00k | type &= ~OBJ_NAME_ALIAS; |
161 | | |
162 | 9.00k | on.name = name; |
163 | 9.00k | on.type = type; |
164 | | |
165 | 10.5k | for (;;) { |
166 | 10.5k | ret = lh_OBJ_NAME_retrieve(names_lh, &on); |
167 | 10.5k | if (ret == NULL) |
168 | 2.62k | break; |
169 | 7.90k | if ((ret->alias) && !alias) { |
170 | 1.52k | if (++num > 10) |
171 | 0 | break; |
172 | 1.52k | on.name = ret->data; |
173 | 6.38k | } else { |
174 | 6.38k | value = ret->data; |
175 | 6.38k | break; |
176 | 6.38k | } |
177 | 7.90k | } |
178 | | |
179 | 9.00k | CRYPTO_THREAD_unlock(obj_lock); |
180 | 9.00k | return value; |
181 | 9.00k | } |
182 | | |
183 | | int OBJ_NAME_add(const char *name, int type, const char *data) |
184 | 6.78k | { |
185 | 6.78k | OBJ_NAME *onp, *ret; |
186 | 6.78k | int alias, ok = 0; |
187 | | |
188 | 6.78k | if (!OBJ_NAME_init()) |
189 | 0 | return 0; |
190 | | |
191 | 6.78k | alias = type & OBJ_NAME_ALIAS; |
192 | 6.78k | type &= ~OBJ_NAME_ALIAS; |
193 | | |
194 | 6.78k | onp = OPENSSL_malloc(sizeof(*onp)); |
195 | 6.78k | if (onp == NULL) |
196 | 0 | return 0; |
197 | | |
198 | 6.78k | onp->name = name; |
199 | 6.78k | onp->alias = alias; |
200 | 6.78k | onp->type = type; |
201 | 6.78k | onp->data = data; |
202 | | |
203 | 6.78k | if (!CRYPTO_THREAD_write_lock(obj_lock)) { |
204 | 0 | OPENSSL_free(onp); |
205 | 0 | return 0; |
206 | 0 | } |
207 | | |
208 | 6.78k | ret = lh_OBJ_NAME_insert(names_lh, onp); |
209 | 6.78k | if (ret != NULL) { |
210 | | /* free things */ |
211 | 2.91k | if ((name_funcs_stack != NULL) |
212 | 0 | && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) { |
213 | | /* |
214 | | * XXX: I'm not sure I understand why the free function should |
215 | | * get three arguments... -- Richard Levitte |
216 | | */ |
217 | 0 | sk_NAME_FUNCS_value(name_funcs_stack, |
218 | 0 | ret->type) |
219 | 0 | ->free_func(ret->name, ret->type, |
220 | 0 | ret->data); |
221 | 0 | } |
222 | 2.91k | OPENSSL_free(ret); |
223 | 3.87k | } else { |
224 | 3.87k | if (lh_OBJ_NAME_error(names_lh)) { |
225 | | /* ERROR */ |
226 | 0 | OPENSSL_free(onp); |
227 | 0 | goto unlock; |
228 | 0 | } |
229 | 3.87k | } |
230 | | |
231 | 6.78k | ok = 1; |
232 | | |
233 | 6.78k | unlock: |
234 | 6.78k | CRYPTO_THREAD_unlock(obj_lock); |
235 | 6.78k | return ok; |
236 | 6.78k | } |
237 | | |
238 | | int OBJ_NAME_remove(const char *name, int type) |
239 | 0 | { |
240 | 0 | OBJ_NAME on, *ret; |
241 | 0 | int ok = 0; |
242 | |
|
243 | 0 | if (!OBJ_NAME_init()) |
244 | 0 | return 0; |
245 | | |
246 | 0 | if (!CRYPTO_THREAD_write_lock(obj_lock)) |
247 | 0 | return 0; |
248 | | |
249 | 0 | type &= ~OBJ_NAME_ALIAS; |
250 | 0 | on.name = name; |
251 | 0 | on.type = type; |
252 | 0 | ret = lh_OBJ_NAME_delete(names_lh, &on); |
253 | 0 | if (ret != NULL) { |
254 | | /* free things */ |
255 | 0 | if ((name_funcs_stack != NULL) |
256 | 0 | && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) { |
257 | | /* |
258 | | * XXX: I'm not sure I understand why the free function should |
259 | | * get three arguments... -- Richard Levitte |
260 | | */ |
261 | 0 | sk_NAME_FUNCS_value(name_funcs_stack, |
262 | 0 | ret->type) |
263 | 0 | ->free_func(ret->name, ret->type, |
264 | 0 | ret->data); |
265 | 0 | } |
266 | 0 | OPENSSL_free(ret); |
267 | 0 | ok = 1; |
268 | 0 | } |
269 | |
|
270 | 0 | CRYPTO_THREAD_unlock(obj_lock); |
271 | 0 | return ok; |
272 | 0 | } |
273 | | |
274 | | typedef struct { |
275 | | int type; |
276 | | void (*fn)(const OBJ_NAME *, void *arg); |
277 | | void *arg; |
278 | | } OBJ_DOALL; |
279 | | |
280 | | static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d) |
281 | 7.74k | { |
282 | 7.74k | if (name->type == d->type) |
283 | 3.87k | d->fn(name, d->arg); |
284 | 7.74k | } |
285 | | |
286 | | IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL); |
287 | | |
288 | | void OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *arg), |
289 | | void *arg) |
290 | 32 | { |
291 | 32 | OBJ_DOALL d; |
292 | | |
293 | 32 | d.type = type; |
294 | 32 | d.fn = fn; |
295 | 32 | d.arg = arg; |
296 | | |
297 | 32 | lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d); |
298 | 32 | } |
299 | | |
300 | | struct doall_sorted { |
301 | | int type; |
302 | | int n; |
303 | | const OBJ_NAME **names; |
304 | | }; |
305 | | |
306 | | static void do_all_sorted_fn(const OBJ_NAME *name, void *d_) |
307 | 0 | { |
308 | 0 | struct doall_sorted *d = d_; |
309 | |
|
310 | 0 | if (name->type != d->type) |
311 | 0 | return; |
312 | | |
313 | 0 | d->names[d->n++] = name; |
314 | 0 | } |
315 | | |
316 | | static int do_all_sorted_cmp(const void *n1_, const void *n2_) |
317 | 0 | { |
318 | 0 | const OBJ_NAME *const *n1 = n1_; |
319 | 0 | const OBJ_NAME *const *n2 = n2_; |
320 | |
|
321 | 0 | return strcmp((*n1)->name, (*n2)->name); |
322 | 0 | } |
323 | | |
324 | | void OBJ_NAME_do_all_sorted(int type, |
325 | | void (*fn)(const OBJ_NAME *, void *arg), |
326 | | void *arg) |
327 | 0 | { |
328 | 0 | struct doall_sorted d; |
329 | 0 | int n; |
330 | |
|
331 | 0 | d.type = type; |
332 | 0 | d.names = OPENSSL_malloc_array(lh_OBJ_NAME_num_items(names_lh), sizeof(*d.names)); |
333 | | /* Really should return an error if !d.names...but its a void function! */ |
334 | 0 | if (d.names != NULL) { |
335 | 0 | d.n = 0; |
336 | 0 | OBJ_NAME_do_all(type, do_all_sorted_fn, &d); |
337 | |
|
338 | 0 | qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp); |
339 | |
|
340 | 0 | for (n = 0; n < d.n; ++n) |
341 | 0 | fn(d.names[n], arg); |
342 | |
|
343 | 0 | OPENSSL_free((void *)d.names); |
344 | 0 | } |
345 | 0 | } |
346 | | |
347 | | static int free_type; |
348 | | |
349 | | static void names_lh_free_doall(OBJ_NAME *onp) |
350 | 0 | { |
351 | 0 | if (onp == NULL) |
352 | 0 | return; |
353 | | |
354 | 0 | if (free_type < 0 || free_type == onp->type) |
355 | 0 | OBJ_NAME_remove(onp->name, onp->type); |
356 | 0 | } |
357 | | |
358 | | static void name_funcs_free(NAME_FUNCS *ptr) |
359 | 0 | { |
360 | 0 | OPENSSL_free(ptr); |
361 | 0 | } |
362 | | |
363 | | void OBJ_NAME_cleanup(int type) |
364 | 0 | { |
365 | 0 | unsigned long down_load; |
366 | |
|
367 | 0 | if (names_lh == NULL) |
368 | 0 | return; |
369 | | |
370 | 0 | free_type = type; |
371 | 0 | down_load = lh_OBJ_NAME_get_down_load(names_lh); |
372 | 0 | lh_OBJ_NAME_set_down_load(names_lh, 0); |
373 | |
|
374 | 0 | lh_OBJ_NAME_doall(names_lh, names_lh_free_doall); |
375 | 0 | if (type < 0) { |
376 | 0 | lh_OBJ_NAME_free(names_lh); |
377 | 0 | sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free); |
378 | 0 | CRYPTO_THREAD_lock_free(obj_lock); |
379 | 0 | names_lh = NULL; |
380 | 0 | name_funcs_stack = NULL; |
381 | 0 | obj_lock = NULL; |
382 | 0 | } else |
383 | 0 | lh_OBJ_NAME_set_down_load(names_lh, down_load); |
384 | 0 | } |