names.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 "internal/cryptlib.h"
  11. #include <openssl/evp.h>
  12. #include "internal/objects.h"
  13. #include <openssl/x509.h>
  14. #include "internal/evp_int.h"
  15. int EVP_add_cipher(const EVP_CIPHER *c)
  16. {
  17. int r;
  18. if (c == NULL)
  19. return 0;
  20. r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
  21. (const char *)c);
  22. if (r == 0)
  23. return 0;
  24. r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
  25. (const char *)c);
  26. return r;
  27. }
  28. int EVP_add_digest(const EVP_MD *md)
  29. {
  30. int r;
  31. const char *name;
  32. name = OBJ_nid2sn(md->type);
  33. r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
  34. if (r == 0)
  35. return 0;
  36. r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
  37. (const char *)md);
  38. if (r == 0)
  39. return 0;
  40. if (md->pkey_type && md->type != md->pkey_type) {
  41. r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
  42. OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
  43. if (r == 0)
  44. return 0;
  45. r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
  46. OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
  47. }
  48. return r;
  49. }
  50. int EVP_add_mac(const EVP_MAC *m)
  51. {
  52. int r;
  53. if (m == NULL)
  54. return 0;
  55. r = OBJ_NAME_add(OBJ_nid2sn(m->type), OBJ_NAME_TYPE_MAC_METH,
  56. (const char *)m);
  57. if (r == 0)
  58. return 0;
  59. r = OBJ_NAME_add(OBJ_nid2ln(m->type), OBJ_NAME_TYPE_MAC_METH,
  60. (const char *)m);
  61. return r;
  62. }
  63. const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
  64. {
  65. const EVP_CIPHER *cp;
  66. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
  67. return NULL;
  68. cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  69. return cp;
  70. }
  71. const EVP_MD *EVP_get_digestbyname(const char *name)
  72. {
  73. const EVP_MD *cp;
  74. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
  75. return NULL;
  76. cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  77. return cp;
  78. }
  79. const EVP_MAC *EVP_get_macbyname(const char *name)
  80. {
  81. const EVP_MAC *mp;
  82. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_MACS, NULL))
  83. return NULL;
  84. mp = (const EVP_MAC *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MAC_METH);
  85. return mp;
  86. }
  87. void evp_cleanup_int(void)
  88. {
  89. OBJ_NAME_cleanup(OBJ_NAME_TYPE_MAC_METH);
  90. OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
  91. OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
  92. /*
  93. * The above calls will only clean out the contents of the name hash
  94. * table, but not the hash table itself. The following line does that
  95. * part. -- Richard Levitte
  96. */
  97. OBJ_NAME_cleanup(-1);
  98. EVP_PBE_cleanup();
  99. OBJ_sigid_free();
  100. evp_app_cleanup_int();
  101. }
  102. struct doall_cipher {
  103. void *arg;
  104. void (*fn) (const EVP_CIPHER *ciph,
  105. const char *from, const char *to, void *arg);
  106. };
  107. static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
  108. {
  109. struct doall_cipher *dc = arg;
  110. if (nm->alias)
  111. dc->fn(NULL, nm->name, nm->data, dc->arg);
  112. else
  113. dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
  114. }
  115. void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
  116. const char *from, const char *to, void *x),
  117. void *arg)
  118. {
  119. struct doall_cipher dc;
  120. /* Ignore errors */
  121. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  122. dc.fn = fn;
  123. dc.arg = arg;
  124. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  125. }
  126. void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
  127. const char *from, const char *to,
  128. void *x), void *arg)
  129. {
  130. struct doall_cipher dc;
  131. /* Ignore errors */
  132. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  133. dc.fn = fn;
  134. dc.arg = arg;
  135. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  136. }
  137. struct doall_md {
  138. void *arg;
  139. void (*fn) (const EVP_MD *ciph,
  140. const char *from, const char *to, void *arg);
  141. };
  142. static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
  143. {
  144. struct doall_md *dc = arg;
  145. if (nm->alias)
  146. dc->fn(NULL, nm->name, nm->data, dc->arg);
  147. else
  148. dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
  149. }
  150. void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
  151. const char *from, const char *to, void *x),
  152. void *arg)
  153. {
  154. struct doall_md dc;
  155. /* Ignore errors */
  156. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  157. dc.fn = fn;
  158. dc.arg = arg;
  159. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  160. }
  161. void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
  162. const char *from, const char *to,
  163. void *x), void *arg)
  164. {
  165. struct doall_md dc;
  166. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  167. dc.fn = fn;
  168. dc.arg = arg;
  169. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  170. }
  171. struct doall_mac {
  172. void *arg;
  173. void (*fn) (const EVP_MAC *ciph,
  174. const char *from, const char *to, void *arg);
  175. };
  176. static void do_all_mac_fn(const OBJ_NAME *nm, void *arg)
  177. {
  178. struct doall_mac *dc = arg;
  179. if (nm->alias)
  180. dc->fn(NULL, nm->name, nm->data, dc->arg);
  181. else
  182. dc->fn((const EVP_MAC *)nm->data, nm->name, NULL, dc->arg);
  183. }
  184. void EVP_MAC_do_all(void (*fn)
  185. (const EVP_MAC *ciph, const char *from, const char *to,
  186. void *x), void *arg)
  187. {
  188. struct doall_mac dc;
  189. /* Ignore errors */
  190. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_MACS, NULL);
  191. dc.fn = fn;
  192. dc.arg = arg;
  193. OBJ_NAME_do_all(OBJ_NAME_TYPE_MAC_METH, do_all_mac_fn, &dc);
  194. }
  195. void EVP_MAC_do_all_sorted(void (*fn)
  196. (const EVP_MAC *ciph, const char *from,
  197. const char *to, void *x), void *arg)
  198. {
  199. struct doall_mac dc;
  200. /* Ignore errors */
  201. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_MACS, NULL);
  202. dc.fn = fn;
  203. dc.arg = arg;
  204. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MAC_METH, do_all_mac_fn, &dc);
  205. }