Crypto.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. //
  2. // Crypto.h
  3. //
  4. // Library: Crypto
  5. // Package: CryptoCore
  6. // Module: Crypto
  7. //
  8. // Basic definitions for the Poco Crypto library.
  9. // This file must be the first file included by every other Crypto
  10. // header file.
  11. //
  12. // Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Crypto_Crypto_INCLUDED
  18. #define Crypto_Crypto_INCLUDED
  19. #define POCO_EXTERNAL_OPENSSL_DEFAULT 1
  20. #define POCO_EXTERNAL_OPENSSL_SLPRO 2
  21. #include "Poco/Foundation.h"
  22. #include <openssl/opensslv.h>
  23. #ifndef OPENSSL_VERSION_PREREQ
  24. #if defined(OPENSSL_VERSION_MAJOR) && defined(OPENSSL_VERSION_MINOR)
  25. #define OPENSSL_VERSION_PREREQ(maj, min) \
  26. ((OPENSSL_VERSION_MAJOR << 16) + OPENSSL_VERSION_MINOR >= ((maj) << 16) + (min))
  27. #else
  28. #define OPENSSL_VERSION_PREREQ(maj, min) \
  29. (OPENSSL_VERSION_NUMBER >= (((maj) << 28) | ((min) << 20)))
  30. #endif
  31. #endif
  32. enum RSAPaddingMode
  33. /// The padding mode used for RSA public key encryption.
  34. {
  35. RSA_PADDING_PKCS1,
  36. /// PKCS #1 v1.5 padding. This currently is the most widely used mode.
  37. RSA_PADDING_PKCS1_OAEP,
  38. /// EME-OAEP as defined in PKCS #1 v2.0 with SHA-1, MGF1 and an empty
  39. /// encoding parameter. This mode is recommended for all new applications.
  40. RSA_PADDING_SSLV23,
  41. /// PKCS #1 v1.5 padding with an SSL-specific modification that denotes
  42. /// that the server is SSL3 capable.
  43. RSA_PADDING_NONE
  44. /// Raw RSA encryption. This mode should only be used to implement cryptographically
  45. /// sound padding modes in the application code. Encrypting user data directly with RSA
  46. /// is insecure.
  47. };
  48. //
  49. // The following block is the standard way of creating macros which make exporting
  50. // from a DLL simpler. All files within this DLL are compiled with the Crypto_EXPORTS
  51. // symbol defined on the command line. this symbol should not be defined on any project
  52. // that uses this DLL. This way any other project whose source files include this file see
  53. // Crypto_API functions as being imported from a DLL, whereas this DLL sees symbols
  54. // defined with this macro as being exported.
  55. //
  56. #if defined(_WIN32)
  57. #if defined(POCO_DLL)
  58. #if defined(Crypto_EXPORTS)
  59. #define Crypto_API __declspec(dllexport)
  60. #else
  61. #define Crypto_API __declspec(dllimport)
  62. #endif
  63. #endif
  64. #endif
  65. #if !defined(Crypto_API)
  66. #if !defined(POCO_NO_GCC_API_ATTRIBUTE) && defined (__GNUC__) && (__GNUC__ >= 4)
  67. #define Crypto_API __attribute__ ((visibility ("default")))
  68. #else
  69. #define Crypto_API
  70. #endif
  71. #endif
  72. //
  73. // Automatically link Crypto and OpenSSL libraries.
  74. //
  75. #if defined(_MSC_VER)
  76. #if !defined(POCO_NO_AUTOMATIC_LIBS)
  77. #if defined(POCO_INTERNAL_OPENSSL_MSVC_VER)
  78. #if defined(POCO_EXTERNAL_OPENSSL)
  79. #pragma message("External OpenSSL defined but internal headers used - possible mismatch!")
  80. #endif // POCO_EXTERNAL_OPENSSL
  81. #if !defined(_DEBUG)
  82. #define POCO_DEBUG_SUFFIX ""
  83. #if !defined (_DLL)
  84. #define POCO_STATIC_SUFFIX "mt"
  85. #else // _DLL
  86. #define POCO_STATIC_SUFFIX ""
  87. #endif
  88. #else // _DEBUG
  89. #define POCO_DEBUG_SUFFIX "d"
  90. #if !defined (_DLL)
  91. #define POCO_STATIC_SUFFIX "mt"
  92. #else // _DLL
  93. #define POCO_STATIC_SUFFIX ""
  94. #endif
  95. #endif
  96. #pragma comment(lib, "libcrypto" POCO_STATIC_SUFFIX POCO_DEBUG_SUFFIX ".lib")
  97. #pragma comment(lib, "libssl" POCO_STATIC_SUFFIX POCO_DEBUG_SUFFIX ".lib")
  98. #if !defined(_WIN64) && !defined (_DLL) && \
  99. (POCO_INTERNAL_OPENSSL_MSVC_VER == 120) && \
  100. (POCO_MSVC_VERSION < POCO_INTERNAL_OPENSSL_MSVC_VER)
  101. #pragma comment(lib, "libPreVS2013CRT" POCO_STATIC_SUFFIX POCO_DEBUG_SUFFIX ".lib")
  102. #endif
  103. #if !defined (_DLL) && (POCO_MSVS_VERSION >= 2015)
  104. #pragma comment(lib, "legacy_stdio_definitions.lib")
  105. #pragma comment(lib, "legacy_stdio_wide_specifiers.lib")
  106. #endif
  107. #elif defined(POCO_EXTERNAL_OPENSSL)
  108. #if POCO_EXTERNAL_OPENSSL == POCO_EXTERNAL_OPENSSL_SLPRO
  109. #if defined(POCO_DLL)
  110. #if OPENSSL_VERSION_PREREQ(1,1)
  111. #pragma comment(lib, "libcrypto.lib")
  112. #pragma comment(lib, "libssl.lib")
  113. #else
  114. #pragma comment(lib, "libeay32.lib")
  115. #pragma comment(lib, "ssleay32.lib")
  116. #endif
  117. #else
  118. #if OPENSSL_VERSION_PREREQ(1,1)
  119. #if defined(_WIN64)
  120. #pragma comment(lib, "libcrypto64" POCO_LIB_SUFFIX)
  121. #pragma comment(lib, "libssl64" POCO_LIB_SUFFIX)
  122. #else
  123. #pragma comment(lib, "libcrypto32" POCO_LIB_SUFFIX)
  124. #pragma comment(lib, "libssl32" POCO_LIB_SUFFIX)
  125. #endif
  126. #else
  127. #pragma comment(lib, "libeay32" POCO_LIB_SUFFIX)
  128. #pragma comment(lib, "ssleay32" POCO_LIB_SUFFIX)
  129. #endif
  130. #endif
  131. #elif POCO_EXTERNAL_OPENSSL == POCO_EXTERNAL_OPENSSL_DEFAULT
  132. #if OPENSSL_VERSION_PREREQ(1,1)
  133. #pragma comment(lib, "libcrypto.lib")
  134. #pragma comment(lib, "libssl.lib")
  135. #else
  136. #pragma comment(lib, "libeay32.lib")
  137. #pragma comment(lib, "ssleay32.lib")
  138. #endif
  139. #endif
  140. #endif // POCO_INTERNAL_OPENSSL_MSVC_VER
  141. #if !defined(Crypto_EXPORTS)
  142. #pragma comment(lib, "PocoCrypto" POCO_LIB_SUFFIX)
  143. #endif
  144. #endif // POCO_NO_AUTOMATIC_LIBS
  145. #endif
  146. namespace Poco {
  147. namespace Crypto {
  148. void Crypto_API initializeCrypto();
  149. /// Initialize the Crypto library, as well as the underlying OpenSSL
  150. /// libraries, by calling OpenSSLInitializer::initialize().
  151. ///
  152. /// Should be called before using any class from the Crypto library.
  153. /// The Crypto library will be initialized automatically, through
  154. /// OpenSSLInitializer instances held by various Crypto classes
  155. /// (Cipher, CipherKey, RSAKey, X509Certificate).
  156. /// However, it is recommended to call initializeCrypto()
  157. /// in any case at application startup.
  158. ///
  159. /// Can be called multiple times; however, for every call to
  160. /// initializeCrypto(), a matching call to uninitializeCrypto()
  161. /// must be performed.
  162. void Crypto_API uninitializeCrypto();
  163. /// Uninitializes the Crypto library by calling
  164. /// OpenSSLInitializer::uninitialize().
  165. } } // namespace Poco::Crypto
  166. #endif // Crypto_Crypto_INCLUDED