cryptocommon.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2018 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. #if defined(_USE_OPENSSL) && !defined(_WIN32)
  14. #include <memory>
  15. #include "jliball.hpp"
  16. #include <openssl/pem.h>
  17. #include <openssl/err.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/ssl.h>
  20. #include "cryptocommon.hpp"
  21. static unsigned numCryptoLocks = 0;
  22. static std::vector<std::unique_ptr<CriticalSection>> cryptoLocks;
  23. static void locking_function(int mode, int n, const char * file, int line)
  24. {
  25. assertex(n < numCryptoLocks);
  26. if (mode & CRYPTO_LOCK)
  27. cryptoLocks[n]->enter();
  28. else
  29. cryptoLocks[n]->leave();
  30. }
  31. static unsigned long pthreads_thread_id()
  32. {
  33. return (unsigned long)GetCurrentThreadId();
  34. }
  35. static void initSSLLibrary()
  36. {
  37. SSL_load_error_strings();
  38. SSLeay_add_ssl_algorithms();
  39. if (!CRYPTO_get_locking_callback())
  40. {
  41. numCryptoLocks = CRYPTO_num_locks();
  42. for (unsigned i=0; i<numCryptoLocks; i++)
  43. cryptoLocks.push_back(std::unique_ptr<CriticalSection>(new CriticalSection));
  44. CRYPTO_set_locking_callback(locking_function);
  45. }
  46. #ifndef _WIN32
  47. if (!CRYPTO_get_id_callback())
  48. CRYPTO_set_id_callback((unsigned long (*)())pthreads_thread_id);
  49. #endif
  50. OpenSSL_add_all_algorithms();
  51. }
  52. MODULE_INIT(INIT_PRIORITY_STANDARD)
  53. {
  54. initSSLLibrary();
  55. return true;
  56. }
  57. MODULE_EXIT()
  58. {
  59. }
  60. namespace cryptohelper
  61. {
  62. static void populateErrorFormat(StringBuffer &formatMessage, const char *format)
  63. {
  64. // openssl doesn't define max error string size, but 1K will do
  65. char *evpErr = formatMessage.reserve(1024);
  66. ERR_error_string_n(ERR_get_error(), evpErr, 1024);
  67. formatMessage.setLength(strlen(evpErr));
  68. formatMessage.append(" - ").append(format);
  69. }
  70. IException *makeEVPExceptionVA(int code, const char *format, va_list args)
  71. {
  72. StringBuffer formatMessage;
  73. populateErrorFormat(formatMessage, format);
  74. return makeStringExceptionVA(code, formatMessage, args);
  75. }
  76. IException *makeEVPException(int code, const char *msg)
  77. {
  78. StringBuffer formatMessage;
  79. populateErrorFormat(formatMessage, msg);
  80. return makeStringException(code, formatMessage);
  81. }
  82. IException *makeEVPExceptionV(int code, const char *format, ...)
  83. {
  84. va_list args;
  85. va_start(args, format);
  86. IException *e = makeEVPExceptionVA(code, format, args);
  87. va_end(args);
  88. return e;
  89. }
  90. void throwEVPException(int code, const char *format)
  91. {
  92. throw makeEVPException(code, format);
  93. }
  94. void throwEVPExceptionV(int code, const char *format, ...)
  95. {
  96. va_list args;
  97. va_start(args, format);
  98. IException *e = makeEVPExceptionVA(code, format, args);
  99. va_end(args);
  100. throw e;
  101. }
  102. } // end of namespace cryptohelper
  103. #endif // #if defined(_USE_OPENSSL) && !defined(_WIN32)