OpenSSLInitializer.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // OpenSSLInitializer.h
  3. //
  4. // Library: Crypto
  5. // Package: CryptoCore
  6. // Module: OpenSSLInitializer
  7. //
  8. // Definition of the OpenSSLInitializer class.
  9. //
  10. // Copyright (c) 2006-2009, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Crypto_OpenSSLInitializer_INCLUDED
  16. #define Crypto_OpenSSLInitializer_INCLUDED
  17. #include "Poco/Crypto/Crypto.h"
  18. #include "Poco/Mutex.h"
  19. #include "Poco/AtomicCounter.h"
  20. #include <openssl/crypto.h>
  21. #if defined(OPENSSL_FIPS) && OPENSSL_VERSION_NUMBER < 0x010001000L
  22. #include <openssl/fips.h>
  23. #endif
  24. extern "C"
  25. {
  26. struct CRYPTO_dynlock_value
  27. {
  28. Poco::FastMutex _mutex;
  29. };
  30. }
  31. namespace Poco {
  32. namespace Crypto {
  33. class Crypto_API OpenSSLInitializer
  34. /// Initalizes the OpenSSL library.
  35. ///
  36. /// The class ensures the earliest initialization and the
  37. /// latest shutdown of the OpenSSL library.
  38. {
  39. public:
  40. OpenSSLInitializer();
  41. /// Automatically initialize OpenSSL on startup.
  42. ~OpenSSLInitializer();
  43. /// Automatically shut down OpenSSL on exit.
  44. static void initialize();
  45. /// Initializes the OpenSSL machinery.
  46. static void uninitialize();
  47. /// Shuts down the OpenSSL machinery.
  48. static bool isFIPSEnabled();
  49. // Returns true if FIPS mode is enabled, false otherwise.
  50. static void enableFIPSMode(bool enabled);
  51. // Enable or disable FIPS mode. If FIPS is not available, this method doesn't do anything.
  52. protected:
  53. enum
  54. {
  55. SEEDSIZE = 256
  56. };
  57. // OpenSSL multithreading support
  58. static void lock(int mode, int n, const char* file, int line);
  59. static unsigned long id();
  60. static struct CRYPTO_dynlock_value* dynlockCreate(const char* file, int line);
  61. static void dynlock(int mode, struct CRYPTO_dynlock_value* lock, const char* file, int line);
  62. static void dynlockDestroy(struct CRYPTO_dynlock_value* lock, const char* file, int line);
  63. private:
  64. static Poco::FastMutex* _mutexes;
  65. static Poco::AtomicCounter _rc;
  66. };
  67. //
  68. // inlines
  69. //
  70. inline bool OpenSSLInitializer::isFIPSEnabled()
  71. {
  72. #ifdef OPENSSL_FIPS
  73. return FIPS_mode() ? true : false;
  74. #else
  75. return false;
  76. #endif
  77. }
  78. #ifdef OPENSSL_FIPS
  79. inline void OpenSSLInitializer::enableFIPSMode(bool enabled)
  80. {
  81. FIPS_mode_set(enabled);
  82. }
  83. #else
  84. inline void OpenSSLInitializer::enableFIPSMode(bool /*enabled*/)
  85. {
  86. }
  87. #endif
  88. } } // namespace Poco::Crypto
  89. #endif // Crypto_OpenSSLInitializer_INCLUDED