names.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
  51. {
  52. const EVP_CIPHER *cp;
  53. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
  54. return NULL;
  55. cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  56. return cp;
  57. }
  58. const EVP_MD *EVP_get_digestbyname(const char *name)
  59. {
  60. const EVP_MD *cp;
  61. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
  62. return NULL;
  63. cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  64. return cp;
  65. }
  66. void evp_cleanup_int(void)
  67. {
  68. OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
  69. OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
  70. /*
  71. * The above calls will only clean out the contents of the name hash
  72. * table, but not the hash table itself. The following line does that
  73. * part. -- Richard Levitte
  74. */
  75. OBJ_NAME_cleanup(-1);
  76. EVP_PBE_cleanup();
  77. OBJ_sigid_free();
  78. evp_app_cleanup_int();
  79. }
  80. struct doall_cipher {
  81. void *arg;
  82. void (*fn) (const EVP_CIPHER *ciph,
  83. const char *from, const char *to, void *arg);
  84. };
  85. static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
  86. {
  87. struct doall_cipher *dc = arg;
  88. if (nm->alias)
  89. dc->fn(NULL, nm->name, nm->data, dc->arg);
  90. else
  91. dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
  92. }
  93. void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
  94. const char *from, const char *to, void *x),
  95. void *arg)
  96. {
  97. struct doall_cipher dc;
  98. /* Ignore errors */
  99. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  100. dc.fn = fn;
  101. dc.arg = arg;
  102. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  103. }
  104. void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
  105. const char *from, const char *to,
  106. void *x), void *arg)
  107. {
  108. struct doall_cipher dc;
  109. /* Ignore errors */
  110. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  111. dc.fn = fn;
  112. dc.arg = arg;
  113. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  114. }
  115. struct doall_md {
  116. void *arg;
  117. void (*fn) (const EVP_MD *ciph,
  118. const char *from, const char *to, void *arg);
  119. };
  120. static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
  121. {
  122. struct doall_md *dc = arg;
  123. if (nm->alias)
  124. dc->fn(NULL, nm->name, nm->data, dc->arg);
  125. else
  126. dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
  127. }
  128. void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
  129. const char *from, const char *to, void *x),
  130. void *arg)
  131. {
  132. struct doall_md dc;
  133. /* Ignore errors */
  134. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  135. dc.fn = fn;
  136. dc.arg = arg;
  137. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  138. }
  139. void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
  140. const char *from, const char *to,
  141. void *x), void *arg)
  142. {
  143. struct doall_md dc;
  144. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  145. dc.fn = fn;
  146. dc.arg = arg;
  147. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  148. }