names.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 <openssl/kdf.h>
  13. #include "crypto/objects.h"
  14. #include <openssl/x509.h>
  15. #include "crypto/evp.h"
  16. int EVP_add_cipher(const EVP_CIPHER *c)
  17. {
  18. int r;
  19. if (c == NULL)
  20. return 0;
  21. r = OBJ_NAME_add(OBJ_nid2sn(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
  22. (const char *)c);
  23. if (r == 0)
  24. return 0;
  25. r = OBJ_NAME_add(OBJ_nid2ln(c->nid), OBJ_NAME_TYPE_CIPHER_METH,
  26. (const char *)c);
  27. return r;
  28. }
  29. int EVP_add_digest(const EVP_MD *md)
  30. {
  31. int r;
  32. const char *name;
  33. name = OBJ_nid2sn(md->type);
  34. r = OBJ_NAME_add(name, OBJ_NAME_TYPE_MD_METH, (const char *)md);
  35. if (r == 0)
  36. return 0;
  37. r = OBJ_NAME_add(OBJ_nid2ln(md->type), OBJ_NAME_TYPE_MD_METH,
  38. (const char *)md);
  39. if (r == 0)
  40. return 0;
  41. if (md->pkey_type && md->type != md->pkey_type) {
  42. r = OBJ_NAME_add(OBJ_nid2sn(md->pkey_type),
  43. OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
  44. if (r == 0)
  45. return 0;
  46. r = OBJ_NAME_add(OBJ_nid2ln(md->pkey_type),
  47. OBJ_NAME_TYPE_MD_METH | OBJ_NAME_ALIAS, name);
  48. }
  49. return r;
  50. }
  51. const EVP_CIPHER *EVP_get_cipherbyname(const char *name)
  52. {
  53. const EVP_CIPHER *cp;
  54. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL))
  55. return NULL;
  56. cp = (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH);
  57. return cp;
  58. }
  59. const EVP_MD *EVP_get_digestbyname(const char *name)
  60. {
  61. const EVP_MD *cp;
  62. if (!OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL))
  63. return NULL;
  64. cp = (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH);
  65. return cp;
  66. }
  67. void evp_cleanup_int(void)
  68. {
  69. OBJ_NAME_cleanup(OBJ_NAME_TYPE_KDF_METH);
  70. OBJ_NAME_cleanup(OBJ_NAME_TYPE_CIPHER_METH);
  71. OBJ_NAME_cleanup(OBJ_NAME_TYPE_MD_METH);
  72. /*
  73. * The above calls will only clean out the contents of the name hash
  74. * table, but not the hash table itself. The following line does that
  75. * part. -- Richard Levitte
  76. */
  77. OBJ_NAME_cleanup(-1);
  78. EVP_PBE_cleanup();
  79. OBJ_sigid_free();
  80. evp_app_cleanup_int();
  81. }
  82. struct doall_cipher {
  83. void *arg;
  84. void (*fn) (const EVP_CIPHER *ciph,
  85. const char *from, const char *to, void *arg);
  86. };
  87. static void do_all_cipher_fn(const OBJ_NAME *nm, void *arg)
  88. {
  89. struct doall_cipher *dc = arg;
  90. if (nm->alias)
  91. dc->fn(NULL, nm->name, nm->data, dc->arg);
  92. else
  93. dc->fn((const EVP_CIPHER *)nm->data, nm->name, NULL, dc->arg);
  94. }
  95. void EVP_CIPHER_do_all(void (*fn) (const EVP_CIPHER *ciph,
  96. const char *from, const char *to, void *x),
  97. void *arg)
  98. {
  99. struct doall_cipher dc;
  100. /* Ignore errors */
  101. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  102. dc.fn = fn;
  103. dc.arg = arg;
  104. OBJ_NAME_do_all(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  105. }
  106. void EVP_CIPHER_do_all_sorted(void (*fn) (const EVP_CIPHER *ciph,
  107. const char *from, const char *to,
  108. void *x), void *arg)
  109. {
  110. struct doall_cipher dc;
  111. /* Ignore errors */
  112. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_CIPHERS, NULL);
  113. dc.fn = fn;
  114. dc.arg = arg;
  115. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_CIPHER_METH, do_all_cipher_fn, &dc);
  116. }
  117. struct doall_md {
  118. void *arg;
  119. void (*fn) (const EVP_MD *ciph,
  120. const char *from, const char *to, void *arg);
  121. };
  122. static void do_all_md_fn(const OBJ_NAME *nm, void *arg)
  123. {
  124. struct doall_md *dc = arg;
  125. if (nm->alias)
  126. dc->fn(NULL, nm->name, nm->data, dc->arg);
  127. else
  128. dc->fn((const EVP_MD *)nm->data, nm->name, NULL, dc->arg);
  129. }
  130. void EVP_MD_do_all(void (*fn) (const EVP_MD *md,
  131. const char *from, const char *to, void *x),
  132. void *arg)
  133. {
  134. struct doall_md dc;
  135. /* Ignore errors */
  136. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  137. dc.fn = fn;
  138. dc.arg = arg;
  139. OBJ_NAME_do_all(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  140. }
  141. void EVP_MD_do_all_sorted(void (*fn) (const EVP_MD *md,
  142. const char *from, const char *to,
  143. void *x), void *arg)
  144. {
  145. struct doall_md dc;
  146. OPENSSL_init_crypto(OPENSSL_INIT_ADD_ALL_DIGESTS, NULL);
  147. dc.fn = fn;
  148. dc.arg = arg;
  149. OBJ_NAME_do_all_sorted(OBJ_NAME_TYPE_MD_METH, do_all_md_fn, &dc);
  150. }