o_names.c 10 KB

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