123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- #include <mutex>
- #include <kopano/platform.h>
- #include <kopano/lockhelper.hpp>
- #include "SSLUtil.h"
- #include <pthread.h>
- #include <openssl/bn.h>
- #include <openssl/rand.h>
- #include <openssl/ssl.h>
- #include <openssl/conf.h>
- #include <openssl/engine.h>
- namespace KC {
- static std::recursive_mutex *ssl_locks;
- static void ssl_lock(int mode, int n, const char *file, int line)
- {
- if (mode & CRYPTO_LOCK)
- ssl_locks[n].lock();
- else
- ssl_locks[n].unlock();
- }
- static unsigned long ssl_id_function(void)
- {
- return ((unsigned long) pthread_self());
- }
- void ssl_threading_setup() {
- if (ssl_locks)
- return;
-
- ssl_locks = new std::recursive_mutex[CRYPTO_num_locks()];
- CRYPTO_set_locking_callback(ssl_lock);
- CRYPTO_set_id_callback(ssl_id_function);
- }
- void ssl_threading_cleanup() {
- if (!ssl_locks)
- return;
- CRYPTO_set_id_callback(NULL);
- CRYPTO_set_locking_callback(NULL);
- delete[] ssl_locks;
- ssl_locks = nullptr;
- }
- void SSL_library_cleanup()
- {
- #ifndef OPENSSL_NO_ENGINE
- ENGINE_cleanup();
- #endif
- ERR_free_strings();
- ERR_remove_state(0);
- EVP_cleanup();
- CRYPTO_cleanup_all_ex_data();
- CONF_modules_unload(0);
- }
- void ssl_random_init()
- {
- rand_init();
- while (RAND_status() == 0) {
- char buffer[16];
- rand_get(buffer, sizeof buffer);
- RAND_seed(buffer, sizeof buffer);
- }
- }
- void ssl_random(bool b64bit, uint64_t *id)
- {
- RAND_pseudo_bytes(reinterpret_cast<unsigned char *>(id), sizeof(*id));
- if (!b64bit)
- *id &= 0xFFFFFFFF;
- }
- }
|