o_names.c 10 KB

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