]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/objects/o_names.c
Add checks on sk_TYPE_push() returned value
[thirdparty/openssl.git] / crypto / objects / o_names.c
1 /*
2 * Copyright 1998-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (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 "obj_lcl.h"
20
21 /*
22 * We define this wrapper for two reasons. Firstly, later versions of
23 * DEC C add linkage information to certain functions, which makes it
24 * tricky to use them as values to regular function pointers.
25 * Secondly, in the EDK2 build environment, the strcmp function is
26 * actually an external function (AsciiStrCmp) with the Microsoft ABI,
27 * so we can't transparently assign function pointers to it.
28 * Arguably the latter is a stupidity of the UEFI environment, but
29 * since the wrapper solves the DEC C issue too, let's just use the
30 * same solution.
31 */
32 #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI)
33 static int obj_strcmp(const char *a, const char *b)
34 {
35 return strcmp(a, b);
36 }
37 #else
38 #define obj_strcmp strcmp
39 #endif
40
41 /*
42 * I use the ex_data stuff to manage the identifiers for the obj_name_types
43 * that applications may define. I only really use the free function field.
44 */
45 static LHASH_OF(OBJ_NAME) *names_lh = NULL;
46 static int names_type_num = OBJ_NAME_TYPE_NUM;
47
48 struct name_funcs_st {
49 unsigned long (*hash_func) (const char *name);
50 int (*cmp_func) (const char *a, const char *b);
51 void (*free_func) (const char *, int, const char *);
52 };
53
54 static STACK_OF(NAME_FUNCS) *name_funcs_stack;
55
56 /*
57 * The LHASH callbacks now use the raw "void *" prototypes and do
58 * per-variable casting in the functions. This prevents function pointer
59 * casting without the need for macro-generated wrapper functions.
60 */
61
62 static unsigned long obj_name_hash(const OBJ_NAME *a);
63 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b);
64
65 int OBJ_NAME_init(void)
66 {
67 if (names_lh != NULL)
68 return (1);
69 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
70 names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
71 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
72 return (names_lh != NULL);
73 }
74
75 int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
76 int (*cmp_func) (const char *, const char *),
77 void (*free_func) (const char *, int, const char *))
78 {
79 int ret, i, push;
80 NAME_FUNCS *name_funcs;
81
82 if (name_funcs_stack == NULL) {
83 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
84 name_funcs_stack = sk_NAME_FUNCS_new_null();
85 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
86 }
87 if (name_funcs_stack == NULL) {
88 /* ERROR */
89 return (0);
90 }
91 ret = names_type_num;
92 names_type_num++;
93 for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
94 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
95 name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
96 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
97 if (name_funcs == NULL) {
98 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
99 return (0);
100 }
101 name_funcs->hash_func = OPENSSL_LH_strhash;
102 name_funcs->cmp_func = obj_strcmp;
103 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
104
105 push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
106 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
107
108 if (!push) {
109 OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
110 OPENSSL_free(name_funcs);
111 return 0;
112 }
113 }
114 name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
115 if (hash_func != NULL)
116 name_funcs->hash_func = hash_func;
117 if (cmp_func != NULL)
118 name_funcs->cmp_func = cmp_func;
119 if (free_func != NULL)
120 name_funcs->free_func = free_func;
121 return (ret);
122 }
123
124 static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
125 {
126 int ret;
127
128 ret = a->type - b->type;
129 if (ret == 0) {
130 if ((name_funcs_stack != NULL)
131 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
132 ret = sk_NAME_FUNCS_value(name_funcs_stack,
133 a->type)->cmp_func(a->name, b->name);
134 } else
135 ret = strcmp(a->name, b->name);
136 }
137 return (ret);
138 }
139
140 static unsigned long obj_name_hash(const OBJ_NAME *a)
141 {
142 unsigned long ret;
143
144 if ((name_funcs_stack != NULL)
145 && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
146 ret =
147 sk_NAME_FUNCS_value(name_funcs_stack,
148 a->type)->hash_func(a->name);
149 } else {
150 ret = OPENSSL_LH_strhash(a->name);
151 }
152 ret ^= a->type;
153 return (ret);
154 }
155
156 const char *OBJ_NAME_get(const char *name, int type)
157 {
158 OBJ_NAME on, *ret;
159 int num = 0, alias;
160
161 if (name == NULL)
162 return (NULL);
163 if ((names_lh == NULL) && !OBJ_NAME_init())
164 return (NULL);
165
166 alias = type & OBJ_NAME_ALIAS;
167 type &= ~OBJ_NAME_ALIAS;
168
169 on.name = name;
170 on.type = type;
171
172 for (;;) {
173 ret = lh_OBJ_NAME_retrieve(names_lh, &on);
174 if (ret == NULL)
175 return (NULL);
176 if ((ret->alias) && !alias) {
177 if (++num > 10)
178 return (NULL);
179 on.name = ret->data;
180 } else {
181 return (ret->data);
182 }
183 }
184 }
185
186 int OBJ_NAME_add(const char *name, int type, const char *data)
187 {
188 OBJ_NAME *onp, *ret;
189 int alias;
190
191 if ((names_lh == NULL) && !OBJ_NAME_init())
192 return (0);
193
194 alias = type & OBJ_NAME_ALIAS;
195 type &= ~OBJ_NAME_ALIAS;
196
197 onp = OPENSSL_malloc(sizeof(*onp));
198 if (onp == NULL) {
199 /* ERROR */
200 return 0;
201 }
202
203 onp->name = name;
204 onp->alias = alias;
205 onp->type = type;
206 onp->data = data;
207
208 ret = lh_OBJ_NAME_insert(names_lh, onp);
209 if (ret != NULL) {
210 /* free things */
211 if ((name_funcs_stack != NULL)
212 && (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 sk_NAME_FUNCS_value(name_funcs_stack,
218 ret->type)->free_func(ret->name, ret->type,
219 ret->data);
220 }
221 OPENSSL_free(ret);
222 } else {
223 if (lh_OBJ_NAME_error(names_lh)) {
224 /* ERROR */
225 OPENSSL_free(onp);
226 return 0;
227 }
228 }
229 return 1;
230 }
231
232 int OBJ_NAME_remove(const char *name, int type)
233 {
234 OBJ_NAME on, *ret;
235
236 if (names_lh == NULL)
237 return (0);
238
239 type &= ~OBJ_NAME_ALIAS;
240 on.name = name;
241 on.type = type;
242 ret = lh_OBJ_NAME_delete(names_lh, &on);
243 if (ret != NULL) {
244 /* free things */
245 if ((name_funcs_stack != NULL)
246 && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
247 /*
248 * XXX: I'm not sure I understand why the free function should
249 * get three arguments... -- Richard Levitte
250 */
251 sk_NAME_FUNCS_value(name_funcs_stack,
252 ret->type)->free_func(ret->name, ret->type,
253 ret->data);
254 }
255 OPENSSL_free(ret);
256 return (1);
257 } else
258 return (0);
259 }
260
261 typedef struct {
262 int type;
263 void (*fn) (const OBJ_NAME *, void *arg);
264 void *arg;
265 } OBJ_DOALL;
266
267 static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
268 {
269 if (name->type == d->type)
270 d->fn(name, d->arg);
271 }
272
273 IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
274
275 void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
276 void *arg)
277 {
278 OBJ_DOALL d;
279
280 d.type = type;
281 d.fn = fn;
282 d.arg = arg;
283
284 lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
285 }
286
287 struct doall_sorted {
288 int type;
289 int n;
290 const OBJ_NAME **names;
291 };
292
293 static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
294 {
295 struct doall_sorted *d = d_;
296
297 if (name->type != d->type)
298 return;
299
300 d->names[d->n++] = name;
301 }
302
303 static int do_all_sorted_cmp(const void *n1_, const void *n2_)
304 {
305 const OBJ_NAME *const *n1 = n1_;
306 const OBJ_NAME *const *n2 = n2_;
307
308 return strcmp((*n1)->name, (*n2)->name);
309 }
310
311 void OBJ_NAME_do_all_sorted(int type,
312 void (*fn) (const OBJ_NAME *, void *arg),
313 void *arg)
314 {
315 struct doall_sorted d;
316 int n;
317
318 d.type = type;
319 d.names =
320 OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
321 /* Really should return an error if !d.names...but its a void function! */
322 if (d.names != NULL) {
323 d.n = 0;
324 OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
325
326 qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
327
328 for (n = 0; n < d.n; ++n)
329 fn(d.names[n], arg);
330
331 OPENSSL_free((void *)d.names);
332 }
333 }
334
335 static int free_type;
336
337 static void names_lh_free_doall(OBJ_NAME *onp)
338 {
339 if (onp == NULL)
340 return;
341
342 if (free_type < 0 || free_type == onp->type)
343 OBJ_NAME_remove(onp->name, onp->type);
344 }
345
346 static void name_funcs_free(NAME_FUNCS *ptr)
347 {
348 OPENSSL_free(ptr);
349 }
350
351 void OBJ_NAME_cleanup(int type)
352 {
353 unsigned long down_load;
354
355 if (names_lh == NULL)
356 return;
357
358 free_type = type;
359 down_load = lh_OBJ_NAME_get_down_load(names_lh);
360 lh_OBJ_NAME_set_down_load(names_lh, 0);
361
362 lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
363 if (type < 0) {
364 lh_OBJ_NAME_free(names_lh);
365 sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
366 names_lh = NULL;
367 name_funcs_stack = NULL;
368 } else
369 lh_OBJ_NAME_set_down_load(names_lh, down_load);
370 }