o_names.c 11 KB

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