2
0

core_namemap.c 14 KB

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