o_names.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. * Copyright 1998-2021 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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <openssl/err.h>
  13. #include <openssl/lhash.h>
  14. #include <openssl/objects.h>
  15. #include <openssl/safestack.h>
  16. #include <openssl/e_os2.h>
  17. #include "internal/thread_once.h"
  18. #include "crypto/lhash.h"
  19. #include "obj_local.h"
  20. #include "e_os.h"
  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 strcasecmp function is
  26. * actually an external function with the Microsoft ABI, so we can't
  27. * transparently assign function pointers to it.
  28. */
  29. #if defined(OPENSSL_SYS_VMS_DECC) || defined(OPENSSL_SYS_UEFI)
  30. static int obj_strcasecmp(const char *a, const char *b)
  31. {
  32. return strcasecmp(a, b);
  33. }
  34. #else
  35. #define obj_strcasecmp strcasecmp
  36. #endif
  37. /*
  38. * I use the ex_data stuff to manage the identifiers for the obj_name_types
  39. * that applications may define. I only really use the free function field.
  40. */
  41. static LHASH_OF(OBJ_NAME) *names_lh = NULL;
  42. static int names_type_num = OBJ_NAME_TYPE_NUM;
  43. static CRYPTO_RWLOCK *obj_lock = NULL;
  44. struct name_funcs_st {
  45. unsigned long (*hash_func) (const char *name);
  46. int (*cmp_func) (const char *a, const char *b);
  47. void (*free_func) (const char *, int, const char *);
  48. };
  49. static STACK_OF(NAME_FUNCS) *name_funcs_stack;
  50. /*
  51. * The LHASH callbacks now use the raw "void *" prototypes and do
  52. * per-variable casting in the functions. This prevents function pointer
  53. * casting without the need for macro-generated wrapper functions.
  54. */
  55. static unsigned long obj_name_hash(const OBJ_NAME *a);
  56. static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b);
  57. static CRYPTO_ONCE init = CRYPTO_ONCE_STATIC_INIT;
  58. DEFINE_RUN_ONCE_STATIC(o_names_init)
  59. {
  60. names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
  61. obj_lock = CRYPTO_THREAD_lock_new();
  62. return names_lh != NULL && obj_lock != NULL;
  63. }
  64. int OBJ_NAME_init(void)
  65. {
  66. return RUN_ONCE(&init, o_names_init);
  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. {
  72. int ret = 0, i, push;
  73. NAME_FUNCS *name_funcs;
  74. if (!OBJ_NAME_init())
  75. return 0;
  76. if (!CRYPTO_THREAD_write_lock(obj_lock))
  77. return 0;
  78. if (name_funcs_stack == NULL)
  79. name_funcs_stack = sk_NAME_FUNCS_new_null();
  80. if (name_funcs_stack == NULL) {
  81. /* ERROR */
  82. goto out;
  83. }
  84. ret = names_type_num;
  85. names_type_num++;
  86. for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
  87. name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
  88. if (name_funcs == NULL) {
  89. ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
  90. ret = 0;
  91. goto out;
  92. }
  93. name_funcs->hash_func = ossl_lh_strcasehash;
  94. name_funcs->cmp_func = obj_strcasecmp;
  95. push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
  96. if (!push) {
  97. ERR_raise(ERR_LIB_OBJ, ERR_R_MALLOC_FAILURE);
  98. OPENSSL_free(name_funcs);
  99. ret = 0;
  100. goto out;
  101. }
  102. }
  103. name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
  104. if (hash_func != NULL)
  105. name_funcs->hash_func = hash_func;
  106. if (cmp_func != NULL)
  107. name_funcs->cmp_func = cmp_func;
  108. if (free_func != NULL)
  109. name_funcs->free_func = free_func;
  110. out:
  111. CRYPTO_THREAD_unlock(obj_lock);
  112. return ret;
  113. }
  114. static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
  115. {
  116. int ret;
  117. ret = a->type - b->type;
  118. if (ret == 0) {
  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)->cmp_func(a->name, b->name);
  123. } else
  124. ret = strcasecmp(a->name, b->name);
  125. }
  126. return ret;
  127. }
  128. static unsigned long obj_name_hash(const OBJ_NAME *a)
  129. {
  130. unsigned long ret;
  131. if ((name_funcs_stack != NULL)
  132. && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
  133. ret =
  134. sk_NAME_FUNCS_value(name_funcs_stack,
  135. a->type)->hash_func(a->name);
  136. } else {
  137. ret = ossl_lh_strcasehash(a->name);
  138. }
  139. ret ^= a->type;
  140. return ret;
  141. }
  142. const char *OBJ_NAME_get(const char *name, int type)
  143. {
  144. OBJ_NAME on, *ret;
  145. int num = 0, alias;
  146. const char *value = NULL;
  147. if (name == NULL)
  148. return NULL;
  149. if (!OBJ_NAME_init())
  150. return NULL;
  151. if (!CRYPTO_THREAD_read_lock(obj_lock))
  152. return NULL;
  153. alias = type & OBJ_NAME_ALIAS;
  154. type &= ~OBJ_NAME_ALIAS;
  155. on.name = name;
  156. on.type = type;
  157. for (;;) {
  158. ret = lh_OBJ_NAME_retrieve(names_lh, &on);
  159. if (ret == NULL)
  160. break;
  161. if ((ret->alias) && !alias) {
  162. if (++num > 10)
  163. break;
  164. on.name = ret->data;
  165. } else {
  166. value = ret->data;
  167. break;
  168. }
  169. }
  170. CRYPTO_THREAD_unlock(obj_lock);
  171. return value;
  172. }
  173. int OBJ_NAME_add(const char *name, int type, const char *data)
  174. {
  175. OBJ_NAME *onp, *ret;
  176. int alias, ok = 0;
  177. if (!OBJ_NAME_init())
  178. return 0;
  179. alias = type & OBJ_NAME_ALIAS;
  180. type &= ~OBJ_NAME_ALIAS;
  181. onp = OPENSSL_malloc(sizeof(*onp));
  182. if (onp == NULL)
  183. return 0;
  184. onp->name = name;
  185. onp->alias = alias;
  186. onp->type = type;
  187. onp->data = data;
  188. if (!CRYPTO_THREAD_write_lock(obj_lock)) {
  189. OPENSSL_free(onp);
  190. return 0;
  191. }
  192. ret = lh_OBJ_NAME_insert(names_lh, onp);
  193. if (ret != NULL) {
  194. /* free things */
  195. if ((name_funcs_stack != NULL)
  196. && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
  197. /*
  198. * XXX: I'm not sure I understand why the free function should
  199. * get three arguments... -- Richard Levitte
  200. */
  201. sk_NAME_FUNCS_value(name_funcs_stack,
  202. ret->type)->free_func(ret->name, ret->type,
  203. ret->data);
  204. }
  205. OPENSSL_free(ret);
  206. } else {
  207. if (lh_OBJ_NAME_error(names_lh)) {
  208. /* ERROR */
  209. OPENSSL_free(onp);
  210. goto unlock;
  211. }
  212. }
  213. ok = 1;
  214. unlock:
  215. CRYPTO_THREAD_unlock(obj_lock);
  216. return ok;
  217. }
  218. int OBJ_NAME_remove(const char *name, int type)
  219. {
  220. OBJ_NAME on, *ret;
  221. int ok = 0;
  222. if (!OBJ_NAME_init())
  223. return 0;
  224. if (!CRYPTO_THREAD_write_lock(obj_lock))
  225. return 0;
  226. type &= ~OBJ_NAME_ALIAS;
  227. on.name = name;
  228. on.type = type;
  229. ret = lh_OBJ_NAME_delete(names_lh, &on);
  230. if (ret != NULL) {
  231. /* free things */
  232. if ((name_funcs_stack != NULL)
  233. && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
  234. /*
  235. * XXX: I'm not sure I understand why the free function should
  236. * get three arguments... -- Richard Levitte
  237. */
  238. sk_NAME_FUNCS_value(name_funcs_stack,
  239. ret->type)->free_func(ret->name, ret->type,
  240. ret->data);
  241. }
  242. OPENSSL_free(ret);
  243. ok = 1;
  244. }
  245. CRYPTO_THREAD_unlock(obj_lock);
  246. return ok;
  247. }
  248. typedef struct {
  249. int type;
  250. void (*fn) (const OBJ_NAME *, void *arg);
  251. void *arg;
  252. } OBJ_DOALL;
  253. static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
  254. {
  255. if (name->type == d->type)
  256. d->fn(name, d->arg);
  257. }
  258. IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
  259. void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
  260. void *arg)
  261. {
  262. OBJ_DOALL d;
  263. d.type = type;
  264. d.fn = fn;
  265. d.arg = arg;
  266. lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
  267. }
  268. struct doall_sorted {
  269. int type;
  270. int n;
  271. const OBJ_NAME **names;
  272. };
  273. static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
  274. {
  275. struct doall_sorted *d = d_;
  276. if (name->type != d->type)
  277. return;
  278. d->names[d->n++] = name;
  279. }
  280. static int do_all_sorted_cmp(const void *n1_, const void *n2_)
  281. {
  282. const OBJ_NAME *const *n1 = n1_;
  283. const OBJ_NAME *const *n2 = n2_;
  284. return strcmp((*n1)->name, (*n2)->name);
  285. }
  286. void OBJ_NAME_do_all_sorted(int type,
  287. void (*fn) (const OBJ_NAME *, void *arg),
  288. void *arg)
  289. {
  290. struct doall_sorted d;
  291. int n;
  292. d.type = type;
  293. d.names =
  294. OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
  295. /* Really should return an error if !d.names...but its a void function! */
  296. if (d.names != NULL) {
  297. d.n = 0;
  298. OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
  299. qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
  300. for (n = 0; n < d.n; ++n)
  301. fn(d.names[n], arg);
  302. OPENSSL_free((void *)d.names);
  303. }
  304. }
  305. static int free_type;
  306. static void names_lh_free_doall(OBJ_NAME *onp)
  307. {
  308. if (onp == NULL)
  309. return;
  310. if (free_type < 0 || free_type == onp->type)
  311. OBJ_NAME_remove(onp->name, onp->type);
  312. }
  313. static void name_funcs_free(NAME_FUNCS *ptr)
  314. {
  315. OPENSSL_free(ptr);
  316. }
  317. void OBJ_NAME_cleanup(int type)
  318. {
  319. unsigned long down_load;
  320. if (names_lh == NULL)
  321. return;
  322. free_type = type;
  323. down_load = lh_OBJ_NAME_get_down_load(names_lh);
  324. lh_OBJ_NAME_set_down_load(names_lh, 0);
  325. lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
  326. if (type < 0) {
  327. lh_OBJ_NAME_free(names_lh);
  328. sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
  329. CRYPTO_THREAD_lock_free(obj_lock);
  330. names_lh = NULL;
  331. name_funcs_stack = NULL;
  332. obj_lock = NULL;
  333. } else
  334. lh_OBJ_NAME_set_down_load(names_lh, down_load);
  335. }