core_namemap.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2019 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 "e_os.h" /* strcasecmp */
  10. #include "internal/namemap.h"
  11. #include <openssl/lhash.h>
  12. #include "crypto/lhash.h" /* openssl_lh_strcasehash */
  13. /*-
  14. * The namenum entry
  15. * =================
  16. */
  17. typedef struct {
  18. char *name;
  19. int number;
  20. } NAMENUM_ENTRY;
  21. DEFINE_LHASH_OF(NAMENUM_ENTRY);
  22. /*-
  23. * The namemap itself
  24. * ==================
  25. */
  26. struct ossl_namemap_st {
  27. /* Flags */
  28. unsigned int stored:1; /* If 1, it's stored in a library context */
  29. CRYPTO_RWLOCK *lock;
  30. LHASH_OF(NAMENUM_ENTRY) *namenum; /* Name->number mapping */
  31. int max_number; /* Current max number */
  32. };
  33. /* LHASH callbacks */
  34. static unsigned long namenum_hash(const NAMENUM_ENTRY *n)
  35. {
  36. return openssl_lh_strcasehash(n->name);
  37. }
  38. static int namenum_cmp(const NAMENUM_ENTRY *a, const NAMENUM_ENTRY *b)
  39. {
  40. return strcasecmp(a->name, b->name);
  41. }
  42. static void namenum_free(NAMENUM_ENTRY *n)
  43. {
  44. if (n != NULL)
  45. OPENSSL_free(n->name);
  46. OPENSSL_free(n);
  47. }
  48. /* OPENSSL_CTX_METHOD functions for a namemap stored in a library context */
  49. static void *stored_namemap_new(OPENSSL_CTX *libctx)
  50. {
  51. OSSL_NAMEMAP *namemap = ossl_namemap_new();
  52. if (namemap != NULL)
  53. namemap->stored = 1;
  54. return namemap;
  55. }
  56. static void stored_namemap_free(void *vnamemap)
  57. {
  58. OSSL_NAMEMAP *namemap = vnamemap;
  59. /* Pretend it isn't stored, or ossl_namemap_free() will do nothing */
  60. namemap->stored = 0;
  61. ossl_namemap_free(namemap);
  62. }
  63. static const OPENSSL_CTX_METHOD stored_namemap_method = {
  64. stored_namemap_new,
  65. stored_namemap_free,
  66. };
  67. /*-
  68. * API functions
  69. * =============
  70. */
  71. OSSL_NAMEMAP *ossl_namemap_stored(OPENSSL_CTX *libctx)
  72. {
  73. return openssl_ctx_get_data(libctx, OPENSSL_CTX_NAMEMAP_INDEX,
  74. &stored_namemap_method);
  75. }
  76. OSSL_NAMEMAP *ossl_namemap_new(void)
  77. {
  78. OSSL_NAMEMAP *namemap;
  79. if ((namemap = OPENSSL_zalloc(sizeof(*namemap))) != NULL
  80. && (namemap->lock = CRYPTO_THREAD_lock_new()) != NULL
  81. && (namemap->namenum =
  82. lh_NAMENUM_ENTRY_new(namenum_hash, namenum_cmp)) != NULL)
  83. return namemap;
  84. ossl_namemap_free(namemap);
  85. return NULL;
  86. }
  87. void ossl_namemap_free(OSSL_NAMEMAP *namemap)
  88. {
  89. if (namemap == NULL || namemap->stored)
  90. return;
  91. lh_NAMENUM_ENTRY_doall(namemap->namenum, namenum_free);
  92. lh_NAMENUM_ENTRY_free(namemap->namenum);
  93. CRYPTO_THREAD_lock_free(namemap->lock);
  94. OPENSSL_free(namemap);
  95. }
  96. typedef struct doall_names_data_st {
  97. int number;
  98. void (*fn)(const char *name, void *data);
  99. void *data;
  100. } DOALL_NAMES_DATA;
  101. static void do_name(const NAMENUM_ENTRY *namenum, DOALL_NAMES_DATA *data)
  102. {
  103. if (namenum->number == data->number)
  104. data->fn(namenum->name, data->data);
  105. }
  106. IMPLEMENT_LHASH_DOALL_ARG_CONST(NAMENUM_ENTRY, DOALL_NAMES_DATA);
  107. void ossl_namemap_doall_names(const OSSL_NAMEMAP *namemap, int number,
  108. void (*fn)(const char *name, void *data),
  109. void *data)
  110. {
  111. DOALL_NAMES_DATA cbdata;
  112. cbdata.number = number;
  113. cbdata.fn = fn;
  114. cbdata.data = data;
  115. CRYPTO_THREAD_read_lock(namemap->lock);
  116. lh_NAMENUM_ENTRY_doall_DOALL_NAMES_DATA(namemap->namenum, do_name,
  117. &cbdata);
  118. CRYPTO_THREAD_unlock(namemap->lock);
  119. }
  120. int ossl_namemap_name2num_n(const OSSL_NAMEMAP *namemap,
  121. const char *name, size_t name_len)
  122. {
  123. NAMENUM_ENTRY *namenum_entry, namenum_tmpl;
  124. int number = 0;
  125. #ifndef FIPS_MODE
  126. if (namemap == NULL)
  127. namemap = ossl_namemap_stored(NULL);
  128. #endif
  129. if (namemap == NULL)
  130. return 0;
  131. if ((namenum_tmpl.name = OPENSSL_strndup(name, name_len)) == NULL)
  132. return 0;
  133. namenum_tmpl.number = 0;
  134. CRYPTO_THREAD_read_lock(namemap->lock);
  135. namenum_entry =
  136. lh_NAMENUM_ENTRY_retrieve(namemap->namenum, &namenum_tmpl);
  137. if (namenum_entry != NULL)
  138. number = namenum_entry->number;
  139. CRYPTO_THREAD_unlock(namemap->lock);
  140. OPENSSL_free(namenum_tmpl.name);
  141. return number;
  142. }
  143. int ossl_namemap_name2num(const OSSL_NAMEMAP *namemap, const char *name)
  144. {
  145. if (name == NULL)
  146. return 0;
  147. return ossl_namemap_name2num_n(namemap, name, strlen(name));
  148. }
  149. struct num2name_data_st {
  150. size_t idx; /* Countdown */
  151. const char *name; /* Result */
  152. };
  153. static void do_num2name(const char *name, void *vdata)
  154. {
  155. struct num2name_data_st *data = vdata;
  156. if (data->idx > 0)
  157. data->idx--;
  158. else if (data->name == NULL)
  159. data->name = name;
  160. }
  161. const char *ossl_namemap_num2name(const OSSL_NAMEMAP *namemap, int number,
  162. size_t idx)
  163. {
  164. struct num2name_data_st data;
  165. data.idx = idx;
  166. data.name = NULL;
  167. ossl_namemap_doall_names(namemap, number, do_num2name, &data);
  168. return data.name;
  169. }
  170. int ossl_namemap_add_n(OSSL_NAMEMAP *namemap, int number,
  171. const char *name, size_t name_len)
  172. {
  173. NAMENUM_ENTRY *namenum = NULL;
  174. int tmp_number;
  175. #ifndef FIPS_MODE
  176. if (namemap == NULL)
  177. namemap = ossl_namemap_stored(NULL);
  178. #endif
  179. if (name == NULL || name_len == 0 || namemap == NULL)
  180. return 0;
  181. if ((tmp_number = ossl_namemap_name2num_n(namemap, name, name_len)) != 0)
  182. return tmp_number; /* Pretend success */
  183. CRYPTO_THREAD_write_lock(namemap->lock);
  184. if ((namenum = OPENSSL_zalloc(sizeof(*namenum))) == NULL
  185. || (namenum->name = OPENSSL_strndup(name, name_len)) == NULL)
  186. goto err;
  187. namenum->number = tmp_number =
  188. number != 0 ? number : ++namemap->max_number;
  189. (void)lh_NAMENUM_ENTRY_insert(namemap->namenum, namenum);
  190. if (lh_NAMENUM_ENTRY_error(namemap->namenum))
  191. goto err;
  192. CRYPTO_THREAD_unlock(namemap->lock);
  193. return tmp_number;
  194. err:
  195. namenum_free(namenum);
  196. CRYPTO_THREAD_unlock(namemap->lock);
  197. return 0;
  198. }
  199. int ossl_namemap_add(OSSL_NAMEMAP *namemap, int number, const char *name)
  200. {
  201. if (name == NULL)
  202. return 0;
  203. return ossl_namemap_add_n(namemap, number, name, strlen(name));
  204. }