summaryrefslogtreecommitdiff
path: root/src/lib/libcrypto/objects/o_names.c
diff options
context:
space:
mode:
authorcvs2svn <admin@example.com>2023-04-23 13:43:47 +0000
committercvs2svn <admin@example.com>2023-04-23 13:43:47 +0000
commite2496982472bdf233be95c5ea72d1c4dc6c91db3 (patch)
tree316488e5daf86864d8466bce0e5b000d85768378 /src/lib/libcrypto/objects/o_names.c
parent097d0cf840b9007212bcd2516ed5e18939e6da3d (diff)
downloadopenbsd-tb_20230422.tar.gz
openbsd-tb_20230422.tar.bz2
openbsd-tb_20230422.zip
This commit was manufactured by cvs2git to create tag 'tb_20230422'.tb_20230422
Diffstat (limited to '')
-rw-r--r--src/lib/libcrypto/objects/o_names.c355
1 files changed, 0 insertions, 355 deletions
diff --git a/src/lib/libcrypto/objects/o_names.c b/src/lib/libcrypto/objects/o_names.c
deleted file mode 100644
index 2cdd2f3aa6..0000000000
--- a/src/lib/libcrypto/objects/o_names.c
+++ /dev/null
@@ -1,355 +0,0 @@
1/* $OpenBSD: o_names.c,v 1.23 2022/11/08 23:19:09 mbuhl Exp $ */
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5
6#include <openssl/opensslconf.h>
7
8#include <openssl/err.h>
9#include <openssl/lhash.h>
10#include <openssl/objects.h>
11#include <openssl/safestack.h>
12
13/* I use the ex_data stuff to manage the identifiers for the obj_name_types
14 * that applications may define. I only really use the free function field.
15 */
16DECLARE_LHASH_OF(OBJ_NAME);
17static LHASH_OF(OBJ_NAME) *names_lh = NULL;
18static int names_type_num = OBJ_NAME_TYPE_NUM;
19
20typedef struct name_funcs_st {
21 unsigned long (*hash_func)(const char *name);
22 int (*cmp_func)(const char *a, const char *b);
23 void (*free_func)(const char *, int, const char *);
24} NAME_FUNCS;
25
26DECLARE_STACK_OF(NAME_FUNCS)
27
28static STACK_OF(NAME_FUNCS) *name_funcs_stack;
29
30/* The LHASH callbacks now use the raw "void *" prototypes and do per-variable
31 * casting in the functions. This prevents function pointer casting without the
32 * need for macro-generated wrapper functions. */
33
34/* static unsigned long obj_name_hash(OBJ_NAME *a); */
35static unsigned long obj_name_hash(const void *a_void);
36/* static int obj_name_cmp(OBJ_NAME *a,OBJ_NAME *b); */
37static int obj_name_cmp(const void *a_void, const void *b_void);
38
39static IMPLEMENT_LHASH_HASH_FN(obj_name, OBJ_NAME)
40static IMPLEMENT_LHASH_COMP_FN(obj_name, OBJ_NAME)
41
42int
43OBJ_NAME_init(void)
44{
45 if (names_lh != NULL)
46 return (1);
47 names_lh = lh_OBJ_NAME_new();
48 return (names_lh != NULL);
49}
50
51int
52OBJ_NAME_new_index(unsigned long (*hash_func)(const char *),
53 int (*cmp_func)(const char *, const char *),
54 void (*free_func)(const char *, int, const char *))
55{
56 int ret;
57 int i;
58 NAME_FUNCS *name_funcs;
59
60 if (name_funcs_stack == NULL)
61 name_funcs_stack = sk_NAME_FUNCS_new_null();
62 if (name_funcs_stack == NULL)
63 return (0);
64
65 ret = names_type_num;
66 names_type_num++;
67 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
68 name_funcs = malloc(sizeof(NAME_FUNCS));
69 if (!name_funcs) {
70 OBJerror(ERR_R_MALLOC_FAILURE);
71 return (0);
72 }
73 name_funcs->hash_func = lh_strhash;
74 name_funcs->cmp_func = strcmp;
75 name_funcs->free_func = NULL;
76 if (sk_NAME_FUNCS_push(name_funcs_stack, name_funcs) == 0) {
77 free(name_funcs);
78 OBJerror(ERR_R_MALLOC_FAILURE);
79 return (0);
80 }
81 }
82 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
83 if (hash_func != NULL)
84 name_funcs->hash_func = hash_func;
85 if (cmp_func != NULL)
86 name_funcs->cmp_func = cmp_func;
87 if (free_func != NULL)
88 name_funcs->free_func = free_func;
89 return (ret);
90}
91
92/* static int obj_name_cmp(OBJ_NAME *a, OBJ_NAME *b) */
93static int
94obj_name_cmp(const void *a_void, const void *b_void)
95{
96 int ret;
97 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
98 const OBJ_NAME *b = (const OBJ_NAME *)b_void;
99
100 ret = a->type - b->type;
101 if (ret == 0) {
102 if ((name_funcs_stack != NULL) &&
103 (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
104 ret = sk_NAME_FUNCS_value(name_funcs_stack,
105 a->type)->cmp_func(a->name, b->name);
106 } else
107 ret = strcmp(a->name, b->name);
108 }
109 return (ret);
110}
111
112/* static unsigned long obj_name_hash(OBJ_NAME *a) */
113static unsigned long
114obj_name_hash(const void *a_void)
115{
116 unsigned long ret;
117 const OBJ_NAME *a = (const OBJ_NAME *)a_void;
118
119 if ((name_funcs_stack != NULL) &&
120 (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
121 ret = sk_NAME_FUNCS_value(name_funcs_stack,
122 a->type)->hash_func(a->name);
123 } else {
124 ret = lh_strhash(a->name);
125 }
126 ret ^= a->type;
127 return (ret);
128}
129
130const char *
131OBJ_NAME_get(const char *name, int type)
132{
133 OBJ_NAME on, *ret;
134 int num = 0, alias;
135
136 if (name == NULL)
137 return (NULL);
138 if ((names_lh == NULL) && !OBJ_NAME_init())
139 return (NULL);
140
141 alias = type&OBJ_NAME_ALIAS;
142 type&= ~OBJ_NAME_ALIAS;
143
144 on.name = name;
145 on.type = type;
146
147 for (;;) {
148 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
149 if (ret == NULL)
150 return (NULL);
151 if ((ret->alias) && !alias) {
152 if (++num > 10)
153 return (NULL);
154 on.name = ret->data;
155 } else {
156 return (ret->data);
157 }
158 }
159}
160
161int
162OBJ_NAME_add(const char *name, int type, const char *data)
163{
164 OBJ_NAME *onp, *ret;
165 int alias;
166
167 if ((names_lh == NULL) && !OBJ_NAME_init())
168 return (0);
169
170 alias = type & OBJ_NAME_ALIAS;
171 type &= ~OBJ_NAME_ALIAS;
172
173 onp = malloc(sizeof(OBJ_NAME));
174 if (onp == NULL) {
175 /* ERROR */
176 return (0);
177 }
178
179 onp->name = name;
180 onp->alias = alias;
181 onp->type = type;
182 onp->data = data;
183
184 ret = lh_OBJ_NAME_insert(names_lh, onp);
185 if (ret != NULL) {
186 /* free things */
187 if ((name_funcs_stack != NULL) &&
188 (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
189 /* XXX: I'm not sure I understand why the free
190 * function should get three arguments...
191 * -- Richard Levitte
192 */
193 sk_NAME_FUNCS_value(
194 name_funcs_stack, ret->type)->free_func(
195 ret->name, ret->type, ret->data);
196 }
197 free(ret);
198 } else {
199 if (lh_OBJ_NAME_error(names_lh)) {
200 free(onp);
201 /* ERROR */
202 return (0);
203 }
204 }
205 return (1);
206}
207
208int
209OBJ_NAME_remove(const char *name, int type)
210{
211 OBJ_NAME on, *ret;
212
213 if (names_lh == NULL)
214 return (0);
215
216 type &= ~OBJ_NAME_ALIAS;
217 on.name = name;
218 on.type = type;
219 ret = lh_OBJ_NAME_delete(names_lh, &on);
220 if (ret != NULL) {
221 /* free things */
222 if ((name_funcs_stack != NULL) &&
223 (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
224 /* XXX: I'm not sure I understand why the free
225 * function should get three arguments...
226 * -- Richard Levitte
227 */
228 sk_NAME_FUNCS_value(
229 name_funcs_stack, ret->type)->free_func(
230 ret->name, ret->type, ret->data);
231 }
232 free(ret);
233 return (1);
234 } else
235 return (0);
236}
237
238struct doall {
239 int type;
240 void (*fn)(const OBJ_NAME *, void *arg);
241 void *arg;
242};
243
244static void
245do_all_fn_doall_arg(const OBJ_NAME *name, struct doall *d)
246{
247 if (name->type == d->type)
248 d->fn(name, d->arg);
249}
250
251static IMPLEMENT_LHASH_DOALL_ARG_FN(do_all_fn, const OBJ_NAME, struct doall)
252
253void
254OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *arg), void *arg)
255{
256 struct doall d;
257
258 d.type = type;
259 d.fn = fn;
260 d.arg = arg;
261
262 lh_OBJ_NAME_doall_arg(names_lh, LHASH_DOALL_ARG_FN(do_all_fn),
263 struct doall, &d);
264}
265
266struct doall_sorted {
267 int type;
268 int n;
269 const OBJ_NAME **names;
270};
271
272static void
273do_all_sorted_fn(const OBJ_NAME *name, void *d_)
274{
275 struct doall_sorted *d = d_;
276
277 if (name->type != d->type)
278 return;
279
280 d->names[d->n++] = name;
281}
282
283static int
284do_all_sorted_cmp(const void *n1_, const void *n2_)
285{
286 const OBJ_NAME * const *n1 = n1_;
287 const OBJ_NAME * const *n2 = n2_;
288
289 return strcmp((*n1)->name, (*n2)->name);
290}
291
292void
293OBJ_NAME_do_all_sorted(int type, void (*fn)(const OBJ_NAME *, void *arg),
294 void *arg)
295{
296 struct doall_sorted d;
297 int n;
298
299 d.type = type;
300 d.names = reallocarray(NULL, lh_OBJ_NAME_num_items(names_lh),
301 sizeof *d.names);
302 d.n = 0;
303 if (d.names != NULL) {
304 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
305
306 qsort((void *)d.names, d.n, sizeof *d.names, do_all_sorted_cmp);
307
308 for (n = 0; n < d.n; ++n)
309 fn(d.names[n], arg);
310
311 free(d.names);
312 }
313}
314
315static int free_type;
316
317static void
318names_lh_free_doall(OBJ_NAME *onp)
319{
320 if (onp == NULL)
321 return;
322
323 if (free_type < 0 || free_type == onp->type)
324 OBJ_NAME_remove(onp->name, onp->type);
325}
326
327static IMPLEMENT_LHASH_DOALL_FN(names_lh_free, OBJ_NAME)
328
329static void
330name_funcs_free(NAME_FUNCS *ptr)
331{
332 free(ptr);
333}
334
335void
336OBJ_NAME_cleanup(int type)
337{
338 unsigned long down_load;
339
340 if (names_lh == NULL)
341 return;
342
343 free_type = type;
344 down_load = lh_OBJ_NAME_down_load(names_lh);
345 lh_OBJ_NAME_down_load(names_lh) = 0;
346
347 lh_OBJ_NAME_doall(names_lh, LHASH_DOALL_FN(names_lh_free));
348 if (type < 0) {
349 lh_OBJ_NAME_free(names_lh);
350 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
351 names_lh = NULL;
352 name_funcs_stack = NULL;
353 } else
354 lh_OBJ_NAME_down_load(names_lh) = down_load;
355}