names.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. }
  79. struct doall_cipher {
  80. void *arg;
  81. void (*fn) (const EVP_CIPHER *ciph,
  82. const char *from, const char *to, void *arg);
  83. };
  84. static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
  85. {
  86. struct doall_cipher *dc = arg;
  87. if (nm->alias)
  88. dc->fn(NULL, nm->name, nm->data, dc->arg);
  89. else
  90. dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
  91. }
  92. void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
  93. const char *from, const char *to, void *x),
  94. void *arg)
  95. {
  96. struct doall_cipher dc;
  97. /* Ignore errors */
  98. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  99. dc.fn = fn;
  100. dc.arg = arg;
  101. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  102. }
  103. void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
  104. const char *from, const char *to,
  105. void *x), void *arg)
  106. {
  107. struct doall_cipher dc;
  108. /* Ignore errors */
  109. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  110. dc.fn = fn;
  111. dc.arg = arg;
  112. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  113. }
  114. struct doall_md {
  115. void *arg;
  116. void (*fn) (const EVP_MD *ciph,
  117. const char *from, const char *to, void *arg);
  118. };
  119. static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
  120. {
  121. struct doall_md *dc = arg;
  122. if (nm->alias)
  123. dc->fn(NULL, nm->name, nm->data, dc->arg);
  124. else
  125. dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
  126. }
  127. void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
  128. const char *from, const char *to, void *x),
  129. void *arg)
  130. {
  131. struct doall_md dc;
  132. /* Ignore errors */
  133. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  134. dc.fn = fn;
  135. dc.arg = arg;
  136. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  137. }
  138. void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
  139. const char *from, const char *to,
  140. void *x), void *arg)
  141. {
  142. struct doall_md dc;
  143. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  144. dc.fn = fn;
  145. dc.arg = arg;
  146. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  147. }