o_names.c 9.6 KB

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