tb_digest.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright 2001-2018 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 "eng_local.h"
  10. static ENGINE_TABLE *digest_table = NULL;
  11. void ENGINE_unregister_digests(ENGINE *e)
  12. {
  13. engine_table_unregister(&digest_table, e);
  14. }
  15. static void engine_unregister_all_digests(void)
  16. {
  17. engine_table_cleanup(&digest_table);
  18. }
  19. int ENGINE_register_digests(ENGINE *e)
  20. {
  21. if (e->digests) {
  22. const int *nids;
  23. int num_nids = e->digests(e, NULL, &nids, 0);
  24. if (num_nids > 0)
  25. return engine_table_register(&digest_table,
  26. engine_unregister_all_digests, e,
  27. nids, num_nids, 0);
  28. }
  29. return 1;
  30. }
  31. void ENGINE_register_all_digests(void)
  32. {
  33. ENGINE *e;
  34. for (e = ENGINE_get_first(); e; e = ENGINE_get_next(e))
  35. ENGINE_register_digests(e);
  36. }
  37. int ENGINE_set_default_digests(ENGINE *e)
  38. {
  39. if (e->digests) {
  40. const int *nids;
  41. int num_nids = e->digests(e, NULL, &nids, 0);
  42. if (num_nids > 0)
  43. return engine_table_register(&digest_table,
  44. engine_unregister_all_digests, e,
  45. nids, num_nids, 1);
  46. }
  47. return 1;
  48. }
  49. /*
  50. * Exposed API function to get a functional reference from the implementation
  51. * table (ie. try to get a functional reference from the tabled structural
  52. * references) for a given digest 'nid'
  53. */
  54. ENGINE *ENGINE_get_digest_engine(int nid)
  55. {
  56. return engine_table_select(&digest_table, nid);
  57. }
  58. /* Obtains a digest implementation from an ENGINE functional reference */
  59. const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid)
  60. {
  61. const EVP_MD *ret;
  62. ENGINE_DIGESTS_PTR fn = ENGINE_get_digests(e);
  63. if (!fn || !fn(e, &ret, NULL, nid)) {
  64. ENGINEerr(ENGINE_F_ENGINE_GET_DIGEST, ENGINE_R_UNIMPLEMENTED_DIGEST);
  65. return NULL;
  66. }
  67. return ret;
  68. }
  69. /* Gets the digest callback from an ENGINE structure */
  70. ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e)
  71. {
  72. return e->digests;
  73. }
  74. /* Sets the digest callback in an ENGINE structure */
  75. int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f)
  76. {
  77. e->digests = f;
  78. return 1;
  79. }