o_names.c 9.4 KB

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