2 * Copyright 1998-2022 The OpenSSL Project Authors. All Rights Reserved.
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
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"
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.
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;
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 *);
38 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
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.
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);
49 static CRYPTO_ONCE init = CRYPTO_ONCE_STATIC_INIT;
50 DEFINE_RUN_ONCE_STATIC(o_names_init)
53 obj_lock = CRYPTO_THREAD_lock_new();
55 names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
56 if (names_lh == NULL) {
57 CRYPTO_THREAD_lock_free(obj_lock);
60 return names_lh != NULL && obj_lock != NULL;
63 int OBJ_NAME_init(void)
65 return RUN_ONCE(&init, o_names_init);
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 *))
73 NAME_FUNCS *name_funcs;
78 if (!CRYPTO_THREAD_write_lock(obj_lock))
81 if (name_funcs_stack == NULL)
82 name_funcs_stack = sk_NAME_FUNCS_new_null();
83 if (name_funcs_stack == NULL) {
89 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
90 name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
91 if (name_funcs == NULL) {
92 ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
96 name_funcs->hash_func = ossl_lh_strcasehash;
97 name_funcs->cmp_func = OPENSSL_strcasecmp;
98 push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
101 ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
102 OPENSSL_free(name_funcs);
107 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
108 if (hash_func != NULL)
109 name_funcs->hash_func = hash_func;
110 if (cmp_func != NULL)
111 name_funcs->cmp_func = cmp_func;
112 if (free_func != NULL)
113 name_funcs->free_func = free_func;
116 CRYPTO_THREAD_unlock(obj_lock);
120 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
124 ret = a->type - b->type;
126 if ((name_funcs_stack != NULL)
127 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
128 ret = sk_NAME_FUNCS_value(name_funcs_stack,
129 a->type)->cmp_func(a->name, b->name);
131 ret = OPENSSL_strcasecmp(a->name, b->name);
136 static unsigned long obj_name_hash(const OBJ_NAME *a)
140 if ((name_funcs_stack != NULL)
141 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
143 sk_NAME_FUNCS_value(name_funcs_stack,
144 a->type)->hash_func(a->name);
146 ret = ossl_lh_strcasehash(a->name);
152 const char *OBJ_NAME_get(const char *name, int type)
156 const char *value = NULL;
160 if (!OBJ_NAME_init())
162 if (!CRYPTO_THREAD_read_lock(obj_lock))
165 alias = type & OBJ_NAME_ALIAS;
166 type &= ~OBJ_NAME_ALIAS;
172 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
175 if ((ret->alias) && !alias) {
185 CRYPTO_THREAD_unlock(obj_lock);
189 int OBJ_NAME_add(const char *name, int type, const char *data)
194 if (!OBJ_NAME_init())
197 alias = type & OBJ_NAME_ALIAS;
198 type &= ~OBJ_NAME_ALIAS;
200 onp = OPENSSL_malloc(sizeof(*onp));
209 if (!CRYPTO_THREAD_write_lock(obj_lock)) {
214 ret = lh_OBJ_NAME_insert(names_lh, onp);
217 if ((name_funcs_stack != NULL)
218 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
220 * XXX: I'm not sure I understand why the free function should
221 * get three arguments... -- Richard Levitte
223 sk_NAME_FUNCS_value(name_funcs_stack,
224 ret->type)->free_func(ret->name, ret->type,
229 if (lh_OBJ_NAME_error(names_lh)) {
239 CRYPTO_THREAD_unlock(obj_lock);
243 int OBJ_NAME_remove(const char *name, int type)
248 if (!OBJ_NAME_init())
251 if (!CRYPTO_THREAD_write_lock(obj_lock))
254 type &= ~OBJ_NAME_ALIAS;
257 ret = lh_OBJ_NAME_delete(names_lh, &on);
260 if ((name_funcs_stack != NULL)
261 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
263 * XXX: I'm not sure I understand why the free function should
264 * get three arguments... -- Richard Levitte
266 sk_NAME_FUNCS_value(name_funcs_stack,
267 ret->type)->free_func(ret->name, ret->type,
274 CRYPTO_THREAD_unlock(obj_lock);
280 void (*fn) (const OBJ_NAME *, void *arg);
284 static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
286 if (name->type == d->type)
290 IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
292 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
301 lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
304 struct doall_sorted {
307 const OBJ_NAME **names;
310 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
312 struct doall_sorted *d = d_;
314 if (name->type != d->type)
317 d->names[d->n++] = name;
320 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
322 const OBJ_NAME *const *n1 = n1_;
323 const OBJ_NAME *const *n2 = n2_;
325 return strcmp((*n1)->name, (*n2)->name);
328 void OBJ_NAME_do_all_sorted(int type,
329 void (*fn) (const OBJ_NAME *, void *arg),
332 struct doall_sorted d;
337 OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
338 /* Really should return an error if !d.names...but its a void function! */
339 if (d.names != NULL) {
341 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
343 qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
345 for (n = 0; n < d.n; ++n)
348 OPENSSL_free((void *)d.names);
352 static int free_type;
354 static void names_lh_free_doall(OBJ_NAME *onp)
359 if (free_type < 0 || free_type == onp->type)
360 OBJ_NAME_remove(onp->name, onp->type);
363 static void name_funcs_free(NAME_FUNCS *ptr)
368 void OBJ_NAME_cleanup(int type)
370 unsigned long down_load;
372 if (names_lh == NULL)
376 down_load = lh_OBJ_NAME_get_down_load(names_lh);
377 lh_OBJ_NAME_set_down_load(names_lh, 0);
379 lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
381 lh_OBJ_NAME_free(names_lh);
382 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
383 CRYPTO_THREAD_lock_free(obj_lock);
385 name_funcs_stack = NULL;
388 lh_OBJ_NAME_set_down_load(names_lh, down_load);