o_names.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /*
  2. * Copyright 1998-2021 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. #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. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  61. names_lh = NULL;
  62. obj_lock = CRYPTO_THREAD_lock_new();
  63. if (obj_lock != NULL)
  64. names_lh = lh_OBJ_NAME_new(obj_name_hash, obj_name_cmp);
  65. if (names_lh == NULL) {
  66. CRYPTO_THREAD_lock_free(obj_lock);
  67. obj_lock = NULL;
  68. }
  69. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  70. return names_lh != NULL && obj_lock != NULL;
  71. }
  72. int OBJ_NAME_init(void)
  73. {
  74. return RUN_ONCE(&init, o_names_init);
  75. }
  76. int OBJ_NAME_new_index(unsigned long (*hash_func) (const char *),
  77. int (*cmp_func) (const char *, const char *),
  78. void (*free_func) (const char *, int, const char *))
  79. {
  80. int ret = 0, i, push;
  81. NAME_FUNCS *name_funcs;
  82. if (!OBJ_NAME_init())
  83. return 0;
  84. CRYPTO_THREAD_write_lock(obj_lock);
  85. if (name_funcs_stack == NULL) {
  86. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  87. name_funcs_stack = sk_NAME_FUNCS_new_null();
  88. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  89. }
  90. if (name_funcs_stack == NULL) {
  91. /* ERROR */
  92. goto out;
  93. }
  94. ret = names_type_num;
  95. names_type_num++;
  96. for (i = sk_NAME_FUNCS_num(name_funcs_stack); i < names_type_num; i++) {
  97. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  98. name_funcs = OPENSSL_zalloc(sizeof(*name_funcs));
  99. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  100. if (name_funcs == NULL) {
  101. OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  102. ret = 0;
  103. goto out;
  104. }
  105. name_funcs->hash_func = openssl_lh_strcasehash;
  106. name_funcs->cmp_func = obj_strcasecmp;
  107. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_DISABLE);
  108. push = sk_NAME_FUNCS_push(name_funcs_stack, name_funcs);
  109. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ENABLE);
  110. if (!push) {
  111. OBJerr(OBJ_F_OBJ_NAME_NEW_INDEX, ERR_R_MALLOC_FAILURE);
  112. OPENSSL_free(name_funcs);
  113. ret = 0;
  114. goto out;
  115. }
  116. }
  117. name_funcs = sk_NAME_FUNCS_value(name_funcs_stack, ret);
  118. if (hash_func != NULL)
  119. name_funcs->hash_func = hash_func;
  120. if (cmp_func != NULL)
  121. name_funcs->cmp_func = cmp_func;
  122. if (free_func != NULL)
  123. name_funcs->free_func = free_func;
  124. out:
  125. CRYPTO_THREAD_unlock(obj_lock);
  126. return ret;
  127. }
  128. static int obj_name_cmp(const OBJ_NAME *a, const OBJ_NAME *b)
  129. {
  130. int ret;
  131. ret = a->type - b->type;
  132. if (ret == 0) {
  133. if ((name_funcs_stack != NULL)
  134. && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
  135. ret = sk_NAME_FUNCS_value(name_funcs_stack,
  136. a->type)->cmp_func(a->name, b->name);
  137. } else
  138. ret = strcasecmp(a->name, b->name);
  139. }
  140. return ret;
  141. }
  142. static unsigned long obj_name_hash(const OBJ_NAME *a)
  143. {
  144. unsigned long ret;
  145. if ((name_funcs_stack != NULL)
  146. && (sk_NAME_FUNCS_num(name_funcs_stack) > a->type)) {
  147. ret =
  148. sk_NAME_FUNCS_value(name_funcs_stack,
  149. a->type)->hash_func(a->name);
  150. } else {
  151. ret = openssl_lh_strcasehash(a->name);
  152. }
  153. ret ^= a->type;
  154. return ret;
  155. }
  156. const char *OBJ_NAME_get(const char *name, int type)
  157. {
  158. OBJ_NAME on, *ret;
  159. int num = 0, alias;
  160. const char *value = NULL;
  161. if (name == NULL)
  162. return NULL;
  163. if (!OBJ_NAME_init())
  164. return NULL;
  165. CRYPTO_THREAD_read_lock(obj_lock);
  166. alias = type & OBJ_NAME_ALIAS;
  167. type &= ~OBJ_NAME_ALIAS;
  168. on.name = name;
  169. on.type = type;
  170. for (;;) {
  171. ret = lh_OBJ_NAME_retrieve(names_lh, &on);
  172. if (ret == NULL)
  173. break;
  174. if ((ret->alias) && !alias) {
  175. if (++num > 10)
  176. break;
  177. on.name = ret->data;
  178. } else {
  179. value = ret->data;
  180. break;
  181. }
  182. }
  183. CRYPTO_THREAD_unlock(obj_lock);
  184. return value;
  185. }
  186. int OBJ_NAME_add(const char *name, int type, const char *data)
  187. {
  188. OBJ_NAME *onp, *ret;
  189. int alias, ok = 0;
  190. if (!OBJ_NAME_init())
  191. return 0;
  192. alias = type & OBJ_NAME_ALIAS;
  193. type &= ~OBJ_NAME_ALIAS;
  194. onp = OPENSSL_malloc(sizeof(*onp));
  195. if (onp == NULL)
  196. return 0;
  197. onp->name = name;
  198. onp->alias = alias;
  199. onp->type = type;
  200. onp->data = data;
  201. CRYPTO_THREAD_write_lock(obj_lock);
  202. ret = lh_OBJ_NAME_insert(names_lh, onp);
  203. if (ret != NULL) {
  204. /* free things */
  205. if ((name_funcs_stack != NULL)
  206. && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
  207. /*
  208. * XXX: I'm not sure I understand why the free function should
  209. * get three arguments... -- Richard Levitte
  210. */
  211. sk_NAME_FUNCS_value(name_funcs_stack,
  212. ret->type)->free_func(ret->name, ret->type,
  213. ret->data);
  214. }
  215. OPENSSL_free(ret);
  216. } else {
  217. if (lh_OBJ_NAME_error(names_lh)) {
  218. /* ERROR */
  219. OPENSSL_free(onp);
  220. goto unlock;
  221. }
  222. }
  223. ok = 1;
  224. unlock:
  225. CRYPTO_THREAD_unlock(obj_lock);
  226. return ok;
  227. }
  228. int OBJ_NAME_remove(const char *name, int type)
  229. {
  230. OBJ_NAME on, *ret;
  231. int ok = 0;
  232. if (!OBJ_NAME_init())
  233. return 0;
  234. CRYPTO_THREAD_write_lock(obj_lock);
  235. type &= ~OBJ_NAME_ALIAS;
  236. on.name = name;
  237. on.type = type;
  238. ret = lh_OBJ_NAME_delete(names_lh, &on);
  239. if (ret != NULL) {
  240. /* free things */
  241. if ((name_funcs_stack != NULL)
  242. && (sk_NAME_FUNCS_num(name_funcs_stack) > ret->type)) {
  243. /*
  244. * XXX: I'm not sure I understand why the free function should
  245. * get three arguments... -- Richard Levitte
  246. */
  247. sk_NAME_FUNCS_value(name_funcs_stack,
  248. ret->type)->free_func(ret->name, ret->type,
  249. ret->data);
  250. }
  251. OPENSSL_free(ret);
  252. ok = 1;
  253. }
  254. CRYPTO_THREAD_unlock(obj_lock);
  255. return ok;
  256. }
  257. typedef struct {
  258. int type;
  259. void (*fn) (const OBJ_NAME *, void *arg);
  260. void *arg;
  261. } OBJ_DOALL;
  262. static void do_all_fn(const OBJ_NAME *name, OBJ_DOALL *d)
  263. {
  264. if (name->type == d->type)
  265. d->fn(name, d->arg);
  266. }
  267. IMPLEMENT_LHASH_DOALL_ARG_CONST(OBJ_NAME, OBJ_DOALL);
  268. void OBJ_NAME_do_all(int type, void (*fn) (const OBJ_NAME *, void *arg),
  269. void *arg)
  270. {
  271. OBJ_DOALL d;
  272. d.type = type;
  273. d.fn = fn;
  274. d.arg = arg;
  275. lh_OBJ_NAME_doall_OBJ_DOALL(names_lh, do_all_fn, &d);
  276. }
  277. struct doall_sorted {
  278. int type;
  279. int n;
  280. const OBJ_NAME **names;
  281. };
  282. static void do_all_sorted_fn(const OBJ_NAME *name, void *d_)
  283. {
  284. struct doall_sorted *d = d_;
  285. if (name->type != d->type)
  286. return;
  287. d->names[d->n++] = name;
  288. }
  289. static int do_all_sorted_cmp(const void *n1_, const void *n2_)
  290. {
  291. const OBJ_NAME *const *n1 = n1_;
  292. const OBJ_NAME *const *n2 = n2_;
  293. return strcmp((*n1)->name, (*n2)->name);
  294. }
  295. void OBJ_NAME_do_all_sorted(int type,
  296. void (*fn) (const OBJ_NAME *, void *arg),
  297. void *arg)
  298. {
  299. struct doall_sorted d;
  300. int n;
  301. d.type = type;
  302. d.names =
  303. OPENSSL_malloc(sizeof(*d.names) * lh_OBJ_NAME_num_items(names_lh));
  304. /* Really should return an error if !d.names...but its a void function! */
  305. if (d.names != NULL) {
  306. d.n = 0;
  307. OBJ_NAME_do_all(type, do_all_sorted_fn, &d);
  308. qsort((void *)d.names, d.n, sizeof(*d.names), do_all_sorted_cmp);
  309. for (n = 0; n < d.n; ++n)
  310. fn(d.names[n], arg);
  311. OPENSSL_free((void *)d.names);
  312. }
  313. }
  314. static int free_type;
  315. static void names_lh_free_doall(OBJ_NAME *onp)
  316. {
  317. if (onp == NULL)
  318. return;
  319. if (free_type < 0 || free_type == onp->type)
  320. OBJ_NAME_remove(onp->name, onp->type);
  321. }
  322. static void name_funcs_free(NAME_FUNCS *ptr)
  323. {
  324. OPENSSL_free(ptr);
  325. }
  326. void OBJ_NAME_cleanup(int type)
  327. {
  328. unsigned long down_load;
  329. if (names_lh == NULL)
  330. return;
  331. free_type = type;
  332. down_load = lh_OBJ_NAME_get_down_load(names_lh);
  333. lh_OBJ_NAME_set_down_load(names_lh, 0);
  334. lh_OBJ_NAME_doall(names_lh, names_lh_free_doall);
  335. if (type < 0) {
  336. lh_OBJ_NAME_free(names_lh);
  337. sk_NAME_FUNCS_pop_free(name_funcs_stack, name_funcs_free);
  338. CRYPTO_THREAD_lock_free(obj_lock);
  339. names_lh = NULL;
  340. name_funcs_stack = NULL;
  341. obj_lock = NULL;
  342. } else
  343. lh_OBJ_NAME_set_down_load(names_lh, down_load);
  344. }