core_namemap.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. if (namemap != NULL) {
  60. /* Pretend it isn't stored, or ossl_namemap_free() will do nothing */
  61. namemap->stored = 0;
  62. ossl_namemap_free(namemap);
  63. }
  64. }
  65. static const OPENSSL_CTX_METHOD stored_namemap_method = {
  66. stored_namemap_new,
  67. stored_namemap_free,
  68. };
  69. /*-
  70. * API functions
  71. * =============
  72. */
  73. int ossl_namemap_empty(OSSL_NAMEMAP *namemap)
  74. {
  75. int rv = 0;
  76. CRYPTO_THREAD_read_lock(namemap->lock);
  77. if (namemap->max_number == 0)
  78. rv = 1;
  79. CRYPTO_THREAD_unlock(namemap->lock);
  80. return rv;
  81. }
  82. typedef struct doall_names_data_st {
  83. int number;
  84. void (*fn)(const char *name, void *data);
  85. void *data;
  86. } DOALL_NAMES_DATA;
  87. static void do_name(const NAMENUM_ENTRY *namenum, DOALL_NAMES_DATA *data)
  88. {
  89. if (namenum->number == data->number)
  90. data->fn(namenum->name, data->data);
  91. }
  92. IMPLEMENT_LHASH_DOALL_ARG_CONST(NAMENUM_ENTRY, DOALL_NAMES_DATA);
  93. void ossl_namemap_doall_names(const OSSL_NAMEMAP *namemap, int number,
  94. void (*fn)(const char *name, void *data),
  95. void *data)
  96. {
  97. DOALL_NAMES_DATA cbdata;
  98. cbdata.number = number;
  99. cbdata.fn = fn;
  100. cbdata.data = data;
  101. CRYPTO_THREAD_read_lock(namemap->lock);
  102. lh_NAMENUM_ENTRY_doall_DOALL_NAMES_DATA(namemap->namenum, do_name,
  103. &cbdata);
  104. CRYPTO_THREAD_unlock(namemap->lock);
  105. }
  106. int ossl_namemap_name2num_n(const OSSL_NAMEMAP *namemap,
  107. const char *name, size_t name_len)
  108. {
  109. NAMENUM_ENTRY *namenum_entry, namenum_tmpl;
  110. int number = 0;
  111. #ifndef FIPS_MODE
  112. if (namemap == NULL)
  113. namemap = ossl_namemap_stored(NULL);
  114. #endif
  115. if (namemap == NULL)
  116. return 0;
  117. if ((namenum_tmpl.name = OPENSSL_strndup(name, name_len)) == NULL)
  118. return 0;
  119. namenum_tmpl.number = 0;
  120. CRYPTO_THREAD_read_lock(namemap->lock);
  121. namenum_entry =
  122. lh_NAMENUM_ENTRY_retrieve(namemap->namenum, &namenum_tmpl);
  123. if (namenum_entry != NULL)
  124. number = namenum_entry->number;
  125. CRYPTO_THREAD_unlock(namemap->lock);
  126. OPENSSL_free(namenum_tmpl.name);
  127. return number;
  128. }
  129. int ossl_namemap_name2num(const OSSL_NAMEMAP *namemap, const char *name)
  130. {
  131. if (name == NULL)
  132. return 0;
  133. return ossl_namemap_name2num_n(namemap, name, strlen(name));
  134. }
  135. struct num2name_data_st {
  136. size_t idx; /* Countdown */
  137. const char *name; /* Result */
  138. };
  139. static void do_num2name(const char *name, void *vdata)
  140. {
  141. struct num2name_data_st *data = vdata;
  142. if (data->idx > 0)
  143. data->idx--;
  144. else if (data->name == NULL)
  145. data->name = name;
  146. }
  147. const char *ossl_namemap_num2name(const OSSL_NAMEMAP *namemap, int number,
  148. size_t idx)
  149. {
  150. struct num2name_data_st data;
  151. data.idx = idx;
  152. data.name = NULL;
  153. ossl_namemap_doall_names(namemap, number, do_num2name, &data);
  154. return data.name;
  155. }
  156. int ossl_namemap_add_name_n(OSSL_NAMEMAP *namemap, int number,
  157. const char *name, size_t name_len)
  158. {
  159. NAMENUM_ENTRY *namenum = NULL;
  160. int tmp_number;
  161. #ifndef FIPS_MODE
  162. if (namemap == NULL)
  163. namemap = ossl_namemap_stored(NULL);
  164. #endif
  165. if (name == NULL || name_len == 0 || namemap == NULL)
  166. return 0;
  167. if ((tmp_number = ossl_namemap_name2num_n(namemap, name, name_len)) != 0)
  168. return tmp_number; /* Pretend success */
  169. CRYPTO_THREAD_write_lock(namemap->lock);
  170. if ((namenum = OPENSSL_zalloc(sizeof(*namenum))) == NULL
  171. || (namenum->name = OPENSSL_strndup(name, name_len)) == NULL)
  172. goto err;
  173. namenum->number = tmp_number =
  174. number != 0 ? number : ++namemap->max_number;
  175. (void)lh_NAMENUM_ENTRY_insert(namemap->namenum, namenum);
  176. if (lh_NAMENUM_ENTRY_error(namemap->namenum))
  177. goto err;
  178. CRYPTO_THREAD_unlock(namemap->lock);
  179. return tmp_number;
  180. err:
  181. namenum_free(namenum);
  182. CRYPTO_THREAD_unlock(namemap->lock);
  183. return 0;
  184. }
  185. int ossl_namemap_add_name(OSSL_NAMEMAP *namemap, int number, const char *name)
  186. {
  187. if (name == NULL)
  188. return 0;
  189. return ossl_namemap_add_name_n(namemap, number, name, strlen(name));
  190. }
  191. int ossl_namemap_add_names(OSSL_NAMEMAP *namemap, int number,
  192. const char *names, const char separator)
  193. {
  194. const char *p, *q;
  195. size_t l;
  196. /* Check that we have a namemap */
  197. if (!ossl_assert(namemap != NULL)) {
  198. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  199. return 0;
  200. }
  201. /*
  202. * Check that no name is an empty string, and that all names have at
  203. * most one numeric identity together.
  204. */
  205. for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
  206. int this_number;
  207. if ((q = strchr(p, separator)) == NULL)
  208. l = strlen(p); /* offset to \0 */
  209. else
  210. l = q - p; /* offset to the next separator */
  211. this_number = ossl_namemap_name2num_n(namemap, p, l);
  212. if (*p == '\0' || *p == separator) {
  213. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_BAD_ALGORITHM_NAME);
  214. return 0;
  215. }
  216. if (number == 0) {
  217. number = this_number;
  218. } else if (this_number != 0 && this_number != number) {
  219. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_CONFLICTING_NAMES,
  220. "\"%.*s\" has an existing different identity %d (from \"%s\")",
  221. l, p, this_number, names);
  222. return 0;
  223. }
  224. }
  225. /* Now that we have checked, register all names */
  226. for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
  227. int this_number;
  228. if ((q = strchr(p, separator)) == NULL)
  229. l = strlen(p); /* offset to \0 */
  230. else
  231. l = q - p; /* offset to the next separator */
  232. this_number = ossl_namemap_add_name_n(namemap, number, p, l);
  233. if (number == 0) {
  234. number = this_number;
  235. } else if (this_number != number) {
  236. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
  237. "Got number %d when expecting %d",
  238. this_number, number);
  239. return 0;
  240. }
  241. }
  242. return number;
  243. }
  244. /*-
  245. * Pre-population
  246. * ==============
  247. */
  248. #ifndef FIPS_MODE
  249. #include <openssl/evp.h>
  250. /* Creates an initial namemap with names found in the legacy method db */
  251. static void get_legacy_evp_names(const char *main_name, const char *alias,
  252. void *arg)
  253. {
  254. int main_id = ossl_namemap_add_name(arg, 0, main_name);
  255. /*
  256. * We could check that the returned value is the same as main_id,
  257. * but since this is a void function, there's no sane way to report
  258. * the error. The best we can do is trust ourselve to keep the legacy
  259. * method database conflict free.
  260. *
  261. * This registers any alias with the same number as the main name.
  262. * Should it be that the current |on| *has* the main name, this is
  263. * simply a no-op.
  264. */
  265. if (alias != NULL) {
  266. (void)ossl_namemap_add_name(arg, main_id, alias);
  267. }
  268. }
  269. static void get_legacy_cipher_names(const OBJ_NAME *on, void *arg)
  270. {
  271. const EVP_CIPHER *cipher = (void *)OBJ_NAME_get(on->name, on->type);
  272. get_legacy_evp_names(EVP_CIPHER_name(cipher), on->name, arg);
  273. }
  274. static void get_legacy_md_names(const OBJ_NAME *on, void *arg)
  275. {
  276. const EVP_MD *md = (void *)OBJ_NAME_get(on->name, on->type);
  277. /* We don't want the pkey_type names, so we need some extra care */
  278. int snid, lnid;
  279. snid = OBJ_sn2nid(on->name);
  280. lnid = OBJ_ln2nid(on->name);
  281. if (snid != EVP_MD_pkey_type(md) && lnid != EVP_MD_pkey_type(md))
  282. get_legacy_evp_names(EVP_MD_name(md), on->name, arg);
  283. else
  284. get_legacy_evp_names(EVP_MD_name(md), NULL, arg);
  285. }
  286. #endif
  287. /*-
  288. * Constructors / destructors
  289. * ==========================
  290. */
  291. OSSL_NAMEMAP *ossl_namemap_stored(OPENSSL_CTX *libctx)
  292. {
  293. OSSL_NAMEMAP *namemap =
  294. openssl_ctx_get_data(libctx, OPENSSL_CTX_NAMEMAP_INDEX,
  295. &stored_namemap_method);
  296. #ifndef FIPS_MODE
  297. if (namemap != NULL && ossl_namemap_empty(namemap)) {
  298. /* Before pilfering, we make sure the legacy database is populated */
  299. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
  300. | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  301. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH,
  302. get_legacy_cipher_names, namemap);
  303. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH,
  304. get_legacy_md_names, namemap);
  305. }
  306. #endif
  307. return namemap;
  308. }
  309. OSSL_NAMEMAP *ossl_namemap_new(void)
  310. {
  311. OSSL_NAMEMAP *namemap;
  312. if ((namemap = OPENSSL_zalloc(sizeof(*namemap))) != NULL
  313. && (namemap->lock = CRYPTO_THREAD_lock_new()) != NULL
  314. && (namemap->namenum =
  315. lh_NAMENUM_ENTRY_new(namenum_hash, namenum_cmp)) != NULL)
  316. return namemap;
  317. ossl_namemap_free(namemap);
  318. return NULL;
  319. }
  320. void ossl_namemap_free(OSSL_NAMEMAP *namemap)
  321. {
  322. if (namemap == NULL || namemap->stored)
  323. return;
  324. lh_NAMENUM_ENTRY_doall(namemap->namenum, namenum_free);
  325. lh_NAMENUM_ENTRY_free(namemap->namenum);
  326. CRYPTO_THREAD_lock_free(namemap->lock);
  327. OPENSSL_free(namemap);
  328. }