names.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * Copyright 1995-2024 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 <openssl/evp.h>
  11. #include <openssl/kdf.h>
  12. #include <openssl/x509.h>
  13. #include "internal/cryptlib.h"
  14. #include "internal/namemap.h"
  15. #include "crypto/objects.h"
  16. #include "crypto/evp.h"
  17. int EVP_add_cipher(const EVP_CIPHER *c)
  18. {
  19. int r;
  20. if (c == NULL)
  21. return 0;
  22. r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
  23. (const char *)c);
  24. if (r == 0)
  25. return 0;
  26. r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
  27. (const char *)c);
  28. return r;
  29. }
  30. int EVP_add_digest(const EVP_MD *md)
  31. {
  32. int r;
  33. const char *name;
  34. name = OBJ_nid2sn(md->type);
  35. r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
  36. if (r == 0)
  37. return 0;
  38. r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
  39. (const char *)md);
  40. if (r == 0)
  41. return 0;
  42. if (md->pkey_type && md->type != md->pkey_type) {
  43. r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
  44. OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
  45. if (r == 0)
  46. return 0;
  47. r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
  48. OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
  49. }
  50. return r;
  51. }
  52. static void cipher_from_name(const char *name, void *data)
  53. {
  54. const EVP_CIPHER **cipher = data;
  55. if (*cipher != NULL)
  56. return;
  57. *cipher = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  58. }
  59. const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
  60. {
  61. return evp_get_cipherbyname_ex(NULL, name);
  62. }
  63. const EVP_CIPHER *evp_get_cipherbyname_ex(OSSL_LIB_CTX *libctx,
  64. const char *name)
  65. {
  66. const EVP_CIPHER *cp;
  67. OSSL_NAMEMAP *namemap;
  68. int id;
  69. int do_retry = 1;
  70. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
  71. return NULL;
  72. cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  73. if (cp != NULL)
  74. return cp;
  75. /*
  76. * It's not in the method database, but it might be there under a different
  77. * name. So we check for aliases in the EVP namemap and try all of those
  78. * in turn.
  79. */
  80. namemap = ossl_namemap_stored(libctx);
  81. retry:
  82. id = ossl_namemap_name2num(namemap, name);
  83. if (id == 0) {
  84. EVP_CIPHER *fetched_cipher;
  85. /* Try to fetch it because the name might not be known yet. */
  86. if (!do_retry)
  87. return NULL;
  88. do_retry = 0;
  89. ERR_set_mark();
  90. fetched_cipher = EVP_CIPHER_fetch(libctx, name, NULL);
  91. EVP_CIPHER_free(fetched_cipher);
  92. ERR_pop_to_mark();
  93. goto retry;
  94. }
  95. if (!ossl_namemap_doall_names(namemap, id, cipher_from_name, &cp))
  96. return NULL;
  97. return cp;
  98. }
  99. static void digest_from_name(const char *name, void *data)
  100. {
  101. const EVP_MD **md = data;
  102. if (*md != NULL)
  103. return;
  104. *md = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  105. }
  106. const EVP_MD *EVP_get_digestbyname(const char *name)
  107. {
  108. return evp_get_digestbyname_ex(NULL, name);
  109. }
  110. const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, const char *name)
  111. {
  112. const EVP_MD *dp;
  113. OSSL_NAMEMAP *namemap;
  114. int id;
  115. int do_retry = 1;
  116. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
  117. return NULL;
  118. dp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  119. if (dp != NULL)
  120. return dp;
  121. /*
  122. * It's not in the method database, but it might be there under a different
  123. * name. So we check for aliases in the EVP namemap and try all of those
  124. * in turn.
  125. */
  126. namemap = ossl_namemap_stored(libctx);
  127. retry:
  128. id = ossl_namemap_name2num(namemap, name);
  129. if (id == 0) {
  130. EVP_MD *fetched_md;
  131. /* Try to fetch it because the name might not be known yet. */
  132. if (!do_retry)
  133. return NULL;
  134. do_retry = 0;
  135. ERR_set_mark();
  136. fetched_md = EVP_MD_fetch(libctx, name, NULL);
  137. EVP_MD_free(fetched_md);
  138. ERR_pop_to_mark();
  139. goto retry;
  140. }
  141. if (!ossl_namemap_doall_names(namemap, id, digest_from_name, &dp))
  142. return NULL;
  143. return dp;
  144. }
  145. void evp_cleanup_int(void)
  146. {
  147. OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);
  148. OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
  149. OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
  150. /*
  151. * The above calls will only clean out the contents of the name hash
  152. * table, but not the hash table itself. The following line does that
  153. * part. -- Richard Levitte
  154. */
  155. OBJ_NAME_cleanup(-1);
  156. EVP_PBE_cleanup();
  157. OBJ_sigid_free();
  158. evp_app_cleanup_int();
  159. }
  160. struct doall_cipher {
  161. void *arg;
  162. void (*fn) (const EVP_CIPHER *ciph,
  163. const char *from, const char *to, void *arg);
  164. };
  165. static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
  166. {
  167. struct doall_cipher *dc = arg;
  168. if (nm->alias)
  169. dc->fn(NULL, nm->name, nm->data, dc->arg);
  170. else
  171. dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
  172. }
  173. void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
  174. const char *from, const char *to, void *x),
  175. void *arg)
  176. {
  177. struct doall_cipher dc;
  178. /* Ignore errors */
  179. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  180. dc.fn = fn;
  181. dc.arg = arg;
  182. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  183. }
  184. void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
  185. const char *from, const char *to,
  186. void *x), void *arg)
  187. {
  188. struct doall_cipher dc;
  189. /* Ignore errors */
  190. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  191. dc.fn = fn;
  192. dc.arg = arg;
  193. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  194. }
  195. struct doall_md {
  196. void *arg;
  197. void (*fn) (const EVP_MD *ciph,
  198. const char *from, const char *to, void *arg);
  199. };
  200. static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
  201. {
  202. struct doall_md *dc = arg;
  203. if (nm->alias)
  204. dc->fn(NULL, nm->name, nm->data, dc->arg);
  205. else
  206. dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
  207. }
  208. void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
  209. const char *from, const char *to, void *x),
  210. void *arg)
  211. {
  212. struct doall_md dc;
  213. /* Ignore errors */
  214. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  215. dc.fn = fn;
  216. dc.arg = arg;
  217. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  218. }
  219. void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
  220. const char *from, const char *to,
  221. void *x), void *arg)
  222. {
  223. struct doall_md dc;
  224. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  225. dc.fn = fn;
  226. dc.arg = arg;
  227. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  228. }