5 #include <openssl/err.h>
6 #include <openssl/lhash.h>
7 #include <openssl/objects.h>
8 #include <openssl/safestack.h>
9 #include <openssl/e_os2.h>
13 * We define this wrapper for two reasons. Firstly, later versions of
14 * DEC C add linkage information to certain functions, which makes it
15 * tricky to use them as values to regular function pointers.
16 * Secondly, in the EDK2 build environment, the strcmp function is
17 * actually an external function (AsciiStrCmp) with the Microsoft ABI,
18 * so we can't transparently assign function pointers to it.
19 * Arguably the latter is a stupidity of the UEFI environment, but
20 * since the wrapper solves the DEC C issue too, let's just use the
23 #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI)
24 static int obj_strcmp(const char *a, const char *b)
29 #define obj_strcmp strcmp
33 * I use the ex_data stuff to manage the identifiers for the obj_name_types
34 * that applications may define. I only really use the free function field.
36 static LHASH_OF(OBJ_NAME) *names_lh = NULL;
37 static int names_type_num = OBJ_NAME_TYPE_NUM;
39 struct name_funcs_st {
40 unsigned long (*hash_func) (const char *name);
41 int (*cmp_func) (const char *a, const char *b);
42 void (*free_func) (const char *, int, const char *);
45 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
48 * The LHASH callbacks now use the raw "void *" prototypes and do
49 * per-variable casting in the functions. This prevents function pointer
50 * casting without the need for macro-generated wrapper functions.
53 static unsigned long obj_name_hash(const OBJ_NAME *a);
54 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b);
56 int OBJ_NAME_init(void)
60 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
61 names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
62 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
63 return (names_lh != NULL);
66 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
67 int (*cmp_func) (const char *, const char *),
68 void (*free_func) (const char *, int, const char *))
72 NAME_FUNCS *name_funcs;
74 if (name_funcs_stack == NULL) {
75 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
76 name_funcs_stack = sk_NAME_FUNCS_new_null();
77 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
79 if (name_funcs_stack == NULL) {
85 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
86 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
87 name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
88 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
89 if (name_funcs == NULL) {
90 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
93 name_funcs->hash_func = lh_strhash;
94 name_funcs->cmp_func = obj_strcmp;
95 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
96 sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
97 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
99 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
100 if (hash_func != NULL)
101 name_funcs->hash_func = hash_func;
102 if (cmp_func != NULL)
103 name_funcs->cmp_func = cmp_func;
104 if (free_func != NULL)
105 name_funcs->free_func = free_func;
109 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
113 ret = a->type - b->type;
115 if ((name_funcs_stack != NULL)
116 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
117 ret = sk_NAME_FUNCS_value(name_funcs_stack,
118 a->type)->cmp_func(a->name, b->name);
120 ret = strcmp(a->name, b->name);
125 static unsigned long obj_name_hash(const OBJ_NAME *a)
129 if ((name_funcs_stack != NULL)
130 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
132 sk_NAME_FUNCS_value(name_funcs_stack,
133 a->type)->hash_func(a->name);
135 ret = lh_strhash(a->name);
141 const char *OBJ_NAME_get(const char *name, int type)
148 if ((names_lh == NULL) && !OBJ_NAME_init())
151 alias = type & OBJ_NAME_ALIAS;
152 type &= ~OBJ_NAME_ALIAS;
158 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
161 if ((ret->alias) && !alias) {
171 int OBJ_NAME_add(const char *name, int type, const char *data)
176 if ((names_lh == NULL) && !OBJ_NAME_init())
179 alias = type & OBJ_NAME_ALIAS;
180 type &= ~OBJ_NAME_ALIAS;
182 onp = OPENSSL_malloc(sizeof(*onp));
193 ret = lh_OBJ_NAME_insert(names_lh, onp);
196 if ((name_funcs_stack != NULL)
197 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
199 * XXX: I'm not sure I understand why the free function should
200 * get three arguments... -- Richard Levitte
202 sk_NAME_FUNCS_value(name_funcs_stack,
203 ret->type)->free_func(ret->name, ret->type,
208 if (lh_OBJ_NAME_error(names_lh)) {
216 int OBJ_NAME_remove(const char *name, int type)
220 if (names_lh == NULL)
223 type &= ~OBJ_NAME_ALIAS;
226 ret = lh_OBJ_NAME_delete(names_lh, &on);
229 if ((name_funcs_stack != NULL)
230 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
232 * XXX: I'm not sure I understand why the free function should
233 * get three arguments... -- Richard Levitte
235 sk_NAME_FUNCS_value(name_funcs_stack,
236 ret->type)->free_func(ret->name, ret->type,
247 void (*fn) (const OBJ_NAME *, void *arg);
251 static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
253 if (name->type == d->type)
257 IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
259 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
268 lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
271 struct doall_sorted {
274 const OBJ_NAME **names;
277 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
279 struct doall_sorted *d = d_;
281 if (name->type != d->type)
284 d->names[d->n++] = name;
287 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
289 const OBJ_NAME *const *n1 = n1_;
290 const OBJ_NAME *const *n2 = n2_;
292 return strcmp((*n1)->name, (*n2)->name);
295 void OBJ_NAME_do_all_sorted(int type,
296 void (*fn) (const OBJ_NAME *, void *arg),
299 struct doall_sorted d;
304 OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
305 /* Really should return an error if !d.names...but its a void function! */
306 if (d.names != NULL) {
308 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
310 qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
312 for (n = 0; n < d.n; ++n)
315 OPENSSL_free((void *)d.names);
319 static int free_type;
321 static void names_lh_free_doall(OBJ_NAME *onp)
326 if (free_type < 0 || free_type == onp->type)
327 OBJ_NAME_remove(onp->name, onp->type);
330 static void name_funcs_free(NAME_FUNCS *ptr)
335 void OBJ_NAME_cleanup(int type)
337 unsigned long down_load;
339 if (names_lh == NULL)
343 down_load = lh_OBJ_NAME_get_down_load(names_lh);
344 lh_OBJ_NAME_set_down_load(names_lh, 0);
346 lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
348 lh_OBJ_NAME_free(names_lh);
349 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
351 name_funcs_stack = NULL;
353 lh_OBJ_NAME_set_down_load(names_lh, down_load);