tb_digest.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright 2001-2021 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 ossl_engine_table_select(&digest_table, nid,
  59. OPENSSL_FILE, OPENSSL_LINE);
  60. }
  61. /* Obtains a digest implementation from an ENGINE functional reference */
  62. const EVP_MD *ENGINE_get_digest(ENGINE *e, int nid)
  63. {
  64. const EVP_MD *ret;
  65. ENGINE_DIGESTS_PTR fn = ENGINE_get_digests(e);
  66. if (!fn || !fn(e, &ret, NULL, nid)) {
  67. ERR_raise(ERR_LIB_ENGINE, ENGINE_R_UNIMPLEMENTED_DIGEST);
  68. return NULL;
  69. }
  70. return ret;
  71. }
  72. /* Gets the digest callback from an ENGINE structure */
  73. ENGINE_DIGESTS_PTR ENGINE_get_digests(const ENGINE *e)
  74. {
  75. return e->digests;
  76. }
  77. /* Sets the digest callback in an ENGINE structure */
  78. int ENGINE_set_digests(ENGINE *e, ENGINE_DIGESTS_PTR f)
  79. {
  80. e->digests = f;
  81. return 1;
  82. }