core_namemap.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /*
  2. * Copyright 2019-2021 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" /* ossl_lh_strcasehash */
  13. #include "internal/tsan_assist.h"
  14. /*-
  15. * The namenum entry
  16. * =================
  17. */
  18. typedef struct {
  19. char *name;
  20. int number;
  21. } NAMENUM_ENTRY;
  22. DEFINE_LHASH_OF(NAMENUM_ENTRY);
  23. /*-
  24. * The namemap itself
  25. * ==================
  26. */
  27. struct ossl_namemap_st {
  28. /* Flags */
  29. unsigned int stored:1; /* If 1, it's stored in a library context */
  30. CRYPTO_RWLOCK *lock;
  31. LHASH_OF(NAMENUM_ENTRY) *namenum; /* Name->number mapping */
  32. #ifdef tsan_ld_acq
  33. TSAN_QUALIFIER int max_number; /* Current max number TSAN version */
  34. #else
  35. int max_number; /* Current max number plain version */
  36. #endif
  37. };
  38. /* LHASH callbacks */
  39. static unsigned long namenum_hash(const NAMENUM_ENTRY *n)
  40. {
  41. return ossl_lh_strcasehash(n->name);
  42. }
  43. static int namenum_cmp(const NAMENUM_ENTRY *a, const NAMENUM_ENTRY *b)
  44. {
  45. return strcasecmp(a->name, b->name);
  46. }
  47. static void namenum_free(NAMENUM_ENTRY *n)
  48. {
  49. if (n != NULL)
  50. OPENSSL_free(n->name);
  51. OPENSSL_free(n);
  52. }
  53. /* OSSL_LIB_CTX_METHOD functions for a namemap stored in a library context */
  54. static void *stored_namemap_new(OSSL_LIB_CTX *libctx)
  55. {
  56. OSSL_NAMEMAP *namemap = ossl_namemap_new();
  57. if (namemap != NULL)
  58. namemap->stored = 1;
  59. return namemap;
  60. }
  61. static void stored_namemap_free(void *vnamemap)
  62. {
  63. OSSL_NAMEMAP *namemap = vnamemap;
  64. if (namemap != NULL) {
  65. /* Pretend it isn't stored, or ossl_namemap_free() will do nothing */
  66. namemap->stored = 0;
  67. ossl_namemap_free(namemap);
  68. }
  69. }
  70. static const OSSL_LIB_CTX_METHOD stored_namemap_method = {
  71. stored_namemap_new,
  72. stored_namemap_free,
  73. };
  74. /*-
  75. * API functions
  76. * =============
  77. */
  78. int ossl_namemap_empty(OSSL_NAMEMAP *namemap)
  79. {
  80. #ifdef tsan_ld_acq
  81. /* Have TSAN support */
  82. return namemap == NULL || tsan_load(&namemap->max_number) == 0;
  83. #else
  84. /* No TSAN support */
  85. int rv;
  86. if (namemap == NULL)
  87. return 1;
  88. if (!CRYPTO_THREAD_read_lock(namemap->lock))
  89. return -1;
  90. rv = namemap->max_number == 0;
  91. CRYPTO_THREAD_unlock(namemap->lock);
  92. return rv;
  93. #endif
  94. }
  95. typedef struct doall_names_data_st {
  96. int number;
  97. const char **names;
  98. int found;
  99. } DOALL_NAMES_DATA;
  100. static void do_name(const NAMENUM_ENTRY *namenum, DOALL_NAMES_DATA *data)
  101. {
  102. if (namenum->number == data->number)
  103. data->names[data->found++] = namenum->name;
  104. }
  105. IMPLEMENT_LHASH_DOALL_ARG_CONST(NAMENUM_ENTRY, DOALL_NAMES_DATA);
  106. /*
  107. * Call the callback for all names in the namemap with the given number.
  108. * A return value 1 means that the callback was called for all names. A
  109. * return value of 0 means that the callback was not called for any names.
  110. */
  111. int ossl_namemap_doall_names(const OSSL_NAMEMAP *namemap, int number,
  112. void (*fn)(const char *name, void *data),
  113. void *data)
  114. {
  115. DOALL_NAMES_DATA cbdata;
  116. size_t num_names;
  117. int i;
  118. cbdata.number = number;
  119. cbdata.found = 0;
  120. /*
  121. * We collect all the names first under a read lock. Subsequently we call
  122. * the user function, so that we're not holding the read lock when in user
  123. * code. This could lead to deadlocks.
  124. */
  125. if (!CRYPTO_THREAD_read_lock(namemap->lock))
  126. return 0;
  127. num_names = lh_NAMENUM_ENTRY_num_items(namemap->namenum);
  128. if (num_names == 0) {
  129. CRYPTO_THREAD_unlock(namemap->lock);
  130. return 0;
  131. }
  132. cbdata.names = OPENSSL_malloc(sizeof(*cbdata.names) * num_names);
  133. if (cbdata.names == NULL) {
  134. CRYPTO_THREAD_unlock(namemap->lock);
  135. return 0;
  136. }
  137. lh_NAMENUM_ENTRY_doall_DOALL_NAMES_DATA(namemap->namenum, do_name,
  138. &cbdata);
  139. CRYPTO_THREAD_unlock(namemap->lock);
  140. for (i = 0; i < cbdata.found; i++)
  141. fn(cbdata.names[i], data);
  142. OPENSSL_free(cbdata.names);
  143. return 1;
  144. }
  145. static int namemap_name2num_n(const OSSL_NAMEMAP *namemap,
  146. const char *name, size_t name_len)
  147. {
  148. NAMENUM_ENTRY *namenum_entry, namenum_tmpl;
  149. if ((namenum_tmpl.name = OPENSSL_strndup(name, name_len)) == NULL)
  150. return 0;
  151. namenum_tmpl.number = 0;
  152. namenum_entry =
  153. lh_NAMENUM_ENTRY_retrieve(namemap->namenum, &namenum_tmpl);
  154. OPENSSL_free(namenum_tmpl.name);
  155. return namenum_entry != NULL ? namenum_entry->number : 0;
  156. }
  157. int ossl_namemap_name2num_n(const OSSL_NAMEMAP *namemap,
  158. const char *name, size_t name_len)
  159. {
  160. int number;
  161. #ifndef FIPS_MODULE
  162. if (namemap == NULL)
  163. namemap = ossl_namemap_stored(NULL);
  164. #endif
  165. if (namemap == NULL)
  166. return 0;
  167. if (!CRYPTO_THREAD_read_lock(namemap->lock))
  168. return 0;
  169. number = namemap_name2num_n(namemap, name, name_len);
  170. CRYPTO_THREAD_unlock(namemap->lock);
  171. return number;
  172. }
  173. int ossl_namemap_name2num(const OSSL_NAMEMAP *namemap, const char *name)
  174. {
  175. if (name == NULL)
  176. return 0;
  177. return ossl_namemap_name2num_n(namemap, name, strlen(name));
  178. }
  179. struct num2name_data_st {
  180. size_t idx; /* Countdown */
  181. const char *name; /* Result */
  182. };
  183. static void do_num2name(const char *name, void *vdata)
  184. {
  185. struct num2name_data_st *data = vdata;
  186. if (data->idx > 0)
  187. data->idx--;
  188. else if (data->name == NULL)
  189. data->name = name;
  190. }
  191. const char *ossl_namemap_num2name(const OSSL_NAMEMAP *namemap, int number,
  192. size_t idx)
  193. {
  194. struct num2name_data_st data;
  195. data.idx = idx;
  196. data.name = NULL;
  197. if (!ossl_namemap_doall_names(namemap, number, do_num2name, &data))
  198. return NULL;
  199. return data.name;
  200. }
  201. static int namemap_add_name_n(OSSL_NAMEMAP *namemap, int number,
  202. const char *name, size_t name_len)
  203. {
  204. NAMENUM_ENTRY *namenum = NULL;
  205. int tmp_number;
  206. /* If it already exists, we don't add it */
  207. if ((tmp_number = namemap_name2num_n(namemap, name, name_len)) != 0)
  208. return tmp_number;
  209. if ((namenum = OPENSSL_zalloc(sizeof(*namenum))) == NULL
  210. || (namenum->name = OPENSSL_strndup(name, name_len)) == NULL)
  211. goto err;
  212. namenum->number =
  213. number != 0 ? number : 1 + tsan_counter(&namemap->max_number);
  214. (void)lh_NAMENUM_ENTRY_insert(namemap->namenum, namenum);
  215. if (lh_NAMENUM_ENTRY_error(namemap->namenum))
  216. goto err;
  217. return namenum->number;
  218. err:
  219. namenum_free(namenum);
  220. return 0;
  221. }
  222. int ossl_namemap_add_name_n(OSSL_NAMEMAP *namemap, int number,
  223. const char *name, size_t name_len)
  224. {
  225. int tmp_number;
  226. #ifndef FIPS_MODULE
  227. if (namemap == NULL)
  228. namemap = ossl_namemap_stored(NULL);
  229. #endif
  230. if (name == NULL || name_len == 0 || namemap == NULL)
  231. return 0;
  232. if (!CRYPTO_THREAD_write_lock(namemap->lock))
  233. return 0;
  234. tmp_number = namemap_add_name_n(namemap, number, name, name_len);
  235. CRYPTO_THREAD_unlock(namemap->lock);
  236. return tmp_number;
  237. }
  238. int ossl_namemap_add_name(OSSL_NAMEMAP *namemap, int number, const char *name)
  239. {
  240. if (name == NULL)
  241. return 0;
  242. return ossl_namemap_add_name_n(namemap, number, name, strlen(name));
  243. }
  244. int ossl_namemap_add_names(OSSL_NAMEMAP *namemap, int number,
  245. const char *names, const char separator)
  246. {
  247. const char *p, *q;
  248. size_t l;
  249. /* Check that we have a namemap */
  250. if (!ossl_assert(namemap != NULL)) {
  251. ERR_raise(ERR_LIB_CRYPTO, ERR_R_PASSED_NULL_PARAMETER);
  252. return 0;
  253. }
  254. if (!CRYPTO_THREAD_write_lock(namemap->lock))
  255. return 0;
  256. /*
  257. * Check that no name is an empty string, and that all names have at
  258. * most one numeric identity together.
  259. */
  260. for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
  261. int this_number;
  262. if ((q = strchr(p, separator)) == NULL)
  263. l = strlen(p); /* offset to \0 */
  264. else
  265. l = q - p; /* offset to the next separator */
  266. this_number = namemap_name2num_n(namemap, p, l);
  267. if (*p == '\0' || *p == separator) {
  268. ERR_raise(ERR_LIB_CRYPTO, CRYPTO_R_BAD_ALGORITHM_NAME);
  269. goto err;
  270. }
  271. if (number == 0) {
  272. number = this_number;
  273. } else if (this_number != 0 && this_number != number) {
  274. ERR_raise_data(ERR_LIB_CRYPTO, CRYPTO_R_CONFLICTING_NAMES,
  275. "\"%.*s\" has an existing different identity %d (from \"%s\")",
  276. l, p, this_number, names);
  277. goto err;
  278. }
  279. }
  280. /* Now that we have checked, register all names */
  281. for (p = names; *p != '\0'; p = (q == NULL ? p + l : q + 1)) {
  282. int this_number;
  283. if ((q = strchr(p, separator)) == NULL)
  284. l = strlen(p); /* offset to \0 */
  285. else
  286. l = q - p; /* offset to the next separator */
  287. this_number = namemap_add_name_n(namemap, number, p, l);
  288. if (number == 0) {
  289. number = this_number;
  290. } else if (this_number != number) {
  291. ERR_raise_data(ERR_LIB_CRYPTO, ERR_R_INTERNAL_ERROR,
  292. "Got number %d when expecting %d",
  293. this_number, number);
  294. goto err;
  295. }
  296. }
  297. CRYPTO_THREAD_unlock(namemap->lock);
  298. return number;
  299. err:
  300. CRYPTO_THREAD_unlock(namemap->lock);
  301. return 0;
  302. }
  303. /*-
  304. * Pre-population
  305. * ==============
  306. */
  307. #ifndef FIPS_MODULE
  308. #include <openssl/evp.h>
  309. /* Creates an initial namemap with names found in the legacy method db */
  310. static void get_legacy_evp_names(const char *main_name, const char *alias,
  311. void *arg)
  312. {
  313. int main_id = ossl_namemap_add_name(arg, 0, main_name);
  314. /*
  315. * We could check that the returned value is the same as main_id,
  316. * but since this is a void function, there's no sane way to report
  317. * the error. The best we can do is trust ourselve to keep the legacy
  318. * method database conflict free.
  319. *
  320. * This registers any alias with the same number as the main name.
  321. * Should it be that the current |on| *has* the main name, this is
  322. * simply a no-op.
  323. */
  324. if (alias != NULL) {
  325. (void)ossl_namemap_add_name(arg, main_id, alias);
  326. }
  327. }
  328. static void get_legacy_cipher_names(const OBJ_NAME *on, void *arg)
  329. {
  330. const EVP_CIPHER *cipher = (void *)OBJ_NAME_get(on->name, on->type);
  331. get_legacy_evp_names(EVP_CIPHER_name(cipher), on->name, arg);
  332. }
  333. static void get_legacy_md_names(const OBJ_NAME *on, void *arg)
  334. {
  335. const EVP_MD *md = (void *)OBJ_NAME_get(on->name, on->type);
  336. /* We don't want the pkey_type names, so we need some extra care */
  337. int snid, lnid;
  338. snid = OBJ_sn2nid(on->name);
  339. lnid = OBJ_ln2nid(on->name);
  340. if (snid != EVP_MD_pkey_type(md) && lnid != EVP_MD_pkey_type(md))
  341. get_legacy_evp_names(EVP_MD_name(md), on->name, arg);
  342. else
  343. get_legacy_evp_names(EVP_MD_name(md), NULL, arg);
  344. }
  345. #endif
  346. /*-
  347. * Constructors / destructors
  348. * ==========================
  349. */
  350. OSSL_NAMEMAP *ossl_namemap_stored(OSSL_LIB_CTX *libctx)
  351. {
  352. #ifndef FIPS_MODULE
  353. int nms;
  354. #endif
  355. OSSL_NAMEMAP *namemap =
  356. ossl_lib_ctx_get_data(libctx, OSSL_LIB_CTX_NAMEMAP_INDEX,
  357. &stored_namemap_method);
  358. if (namemap == NULL)
  359. return NULL;
  360. #ifndef FIPS_MODULE
  361. nms = ossl_namemap_empty(namemap);
  362. if (nms < 0) {
  363. /*
  364. * Could not get lock to make the count, so maybe internal objects
  365. * weren't added. This seems safest.
  366. */
  367. return NULL;
  368. }
  369. if (nms == 1) {
  370. /* Before pilfering, we make sure the legacy database is populated */
  371. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS
  372. | OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  373. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH,
  374. get_legacy_cipher_names, namemap);
  375. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH,
  376. get_legacy_md_names, namemap);
  377. }
  378. #endif
  379. return namemap;
  380. }
  381. OSSL_NAMEMAP *ossl_namemap_new(void)
  382. {
  383. OSSL_NAMEMAP *namemap;
  384. if ((namemap = OPENSSL_zalloc(sizeof(*namemap))) != NULL
  385. && (namemap->lock = CRYPTO_THREAD_lock_new()) != NULL
  386. && (namemap->namenum =
  387. lh_NAMENUM_ENTRY_new(namenum_hash, namenum_cmp)) != NULL)
  388. return namemap;
  389. ossl_namemap_free(namemap);
  390. return NULL;
  391. }
  392. void ossl_namemap_free(OSSL_NAMEMAP *namemap)
  393. {
  394. if (namemap == NULL || namemap->stored)
  395. return;
  396. lh_NAMENUM_ENTRY_doall(namemap->namenum, namenum_free);
  397. lh_NAMENUM_ENTRY_free(namemap->namenum);
  398. CRYPTO_THREAD_lock_free(namemap->lock);
  399. OPENSSL_free(namemap);
  400. }