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