names.c 6.4 KB

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