tb_digest.c 2.5 KB

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