names.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /*
  2. * Copyright 1995-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 <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. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
  70. return NULL;
  71. cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  72. if (cp != NULL)
  73. return cp;
  74. /*
  75. * It's not in the method database, but it might be there under a different
  76. * name. So we check for aliases in the EVP namemap and try all of those
  77. * in turn.
  78. */
  79. namemap = ossl_namemap_stored(libctx);
  80. id = ossl_namemap_name2num(namemap, name);
  81. if (id == 0)
  82. return NULL;
  83. if (!ossl_namemap_doall_names(namemap, id, cipher_from_name, &cp))
  84. return NULL;
  85. return cp;
  86. }
  87. static void digest_from_name(const char *name, void *data)
  88. {
  89. const EVP_MD **md = data;
  90. if (*md != NULL)
  91. return;
  92. *md = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  93. }
  94. const EVP_MD *EVP_get_digestbyname(const char *name)
  95. {
  96. return evp_get_digestbyname_ex(NULL, name);
  97. }
  98. const EVP_MD *evp_get_digestbyname_ex(OSSL_LIB_CTX *libctx, const char *name)
  99. {
  100. const EVP_MD *dp;
  101. OSSL_NAMEMAP *namemap;
  102. int id;
  103. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
  104. return NULL;
  105. dp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  106. if (dp != NULL)
  107. return dp;
  108. /*
  109. * It's not in the method database, but it might be there under a different
  110. * name. So we check for aliases in the EVP namemap and try all of those
  111. * in turn.
  112. */
  113. namemap = ossl_namemap_stored(libctx);
  114. id = ossl_namemap_name2num(namemap, name);
  115. if (id == 0)
  116. return NULL;
  117. if (!ossl_namemap_doall_names(namemap, id, digest_from_name, &dp))
  118. return NULL;
  119. return dp;
  120. }
  121. void evp_cleanup_int(void)
  122. {
  123. OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);
  124. OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
  125. OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
  126. /*
  127. * The above calls will only clean out the contents of the name hash
  128. * table, but not the hash table itself. The following line does that
  129. * part. -- Richard Levitte
  130. */
  131. OBJ_NAME_cleanup(-1);
  132. EVP_PBE_cleanup();
  133. OBJ_sigid_free();
  134. evp_app_cleanup_int();
  135. }
  136. struct doall_cipher {
  137. void *arg;
  138. void (*fn) (const EVP_CIPHER *ciph,
  139. const char *from, const char *to, void *arg);
  140. };
  141. static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
  142. {
  143. struct doall_cipher *dc = arg;
  144. if (nm->alias)
  145. dc->fn(NULL, nm->name, nm->data, dc->arg);
  146. else
  147. dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
  148. }
  149. void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
  150. const char *from, const char *to, void *x),
  151. void *arg)
  152. {
  153. struct doall_cipher dc;
  154. /* Ignore errors */
  155. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  156. dc.fn = fn;
  157. dc.arg = arg;
  158. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  159. }
  160. void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
  161. const char *from, const char *to,
  162. void *x), void *arg)
  163. {
  164. struct doall_cipher dc;
  165. /* Ignore errors */
  166. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  167. dc.fn = fn;
  168. dc.arg = arg;
  169. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  170. }
  171. struct doall_md {
  172. void *arg;
  173. void (*fn) (const EVP_MD *ciph,
  174. const char *from, const char *to, void *arg);
  175. };
  176. static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
  177. {
  178. struct doall_md *dc = arg;
  179. if (nm->alias)
  180. dc->fn(NULL, nm->name, nm->data, dc->arg);
  181. else
  182. dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
  183. }
  184. void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
  185. const char *from, const char *to, void *x),
  186. void *arg)
  187. {
  188. struct doall_md dc;
  189. /* Ignore errors */
  190. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  191. dc.fn = fn;
  192. dc.arg = arg;
  193. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  194. }
  195. void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
  196. const char *from, const char *to,
  197. void *x), void *arg)
  198. {
  199. struct doall_md dc;
  200. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  201. dc.fn = fn;
  202. dc.arg = arg;
  203. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  204. }