SSLUtil.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2005 - 2016 Zarafa and its licensors
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Affero General Public License, version 3,
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU Affero General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Affero General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. */
  17. #include <mutex>
  18. #include <kopano/platform.h>
  19. #include <kopano/lockhelper.hpp>
  20. #include "SSLUtil.h"
  21. #include <pthread.h>
  22. #include <openssl/bn.h>
  23. #include <openssl/rand.h>
  24. #include <openssl/ssl.h>
  25. #include <openssl/conf.h>
  26. #include <openssl/engine.h>
  27. namespace KC {
  28. static std::recursive_mutex *ssl_locks;
  29. static void ssl_lock(int mode, int n, const char *file, int line)
  30. {
  31. if (mode & CRYPTO_LOCK)
  32. ssl_locks[n].lock();
  33. else
  34. ssl_locks[n].unlock();
  35. }
  36. static unsigned long ssl_id_function(void)
  37. {
  38. return ((unsigned long) pthread_self());
  39. }
  40. void ssl_threading_setup() {
  41. if (ssl_locks)
  42. return;
  43. // make recursive, because of openssl bug http://rt.openssl.org/Ticket/Display.html?id=2813&user=guest&pass=guest
  44. ssl_locks = new std::recursive_mutex[CRYPTO_num_locks()];
  45. CRYPTO_set_locking_callback(ssl_lock);
  46. CRYPTO_set_id_callback(ssl_id_function);
  47. }
  48. void ssl_threading_cleanup() {
  49. if (!ssl_locks)
  50. return;
  51. CRYPTO_set_id_callback(NULL);
  52. CRYPTO_set_locking_callback(NULL);
  53. delete[] ssl_locks;
  54. ssl_locks = nullptr;
  55. }
  56. /**
  57. * Free most of the SSL library allocated memory.
  58. *
  59. * This will remove most of the memmory used by
  60. * the ssl library. Don't use this function in libraries
  61. * because it will unload the whole SSL data.
  62. *
  63. * This function makes valgrind happy
  64. */
  65. void SSL_library_cleanup()
  66. {
  67. #ifndef OPENSSL_NO_ENGINE
  68. ENGINE_cleanup();
  69. #endif
  70. ERR_free_strings();
  71. ERR_remove_state(0);
  72. EVP_cleanup();
  73. CRYPTO_cleanup_all_ex_data();
  74. CONF_modules_unload(0);
  75. }
  76. void ssl_random_init()
  77. {
  78. rand_init();
  79. while (RAND_status() == 0) {
  80. char buffer[16];
  81. rand_get(buffer, sizeof buffer);
  82. RAND_seed(buffer, sizeof buffer);
  83. }
  84. }
  85. void ssl_random(bool b64bit, uint64_t *id)
  86. {
  87. RAND_pseudo_bytes(reinterpret_cast<unsigned char *>(id), sizeof(*id));
  88. if (!b64bit)
  89. *id &= 0xFFFFFFFF;
  90. }
  91. } /* namespace */