| Line |
Branch |
Exec |
Source |
| 1 |
|
|
/** |
| 2 |
|
|
* This file is part of the CernVM File System. |
| 3 |
|
|
*/ |
| 4 |
|
|
|
| 5 |
|
|
#include "crypto/crypto_util.h" |
| 6 |
|
|
|
| 7 |
|
|
#include <openssl/crypto.h> |
| 8 |
|
|
#include <openssl/rand.h> |
| 9 |
|
|
#include <pthread.h> |
| 10 |
|
|
|
| 11 |
|
|
#include <cassert> |
| 12 |
|
|
|
| 13 |
|
|
#include "crypto/openssl_version.h" |
| 14 |
|
|
#include "util/platform.h" |
| 15 |
|
|
|
| 16 |
|
|
#ifdef CVMFS_NAMESPACE_GUARD |
| 17 |
|
|
namespace CVMFS_NAMESPACE_GUARD { |
| 18 |
|
|
#endif |
| 19 |
|
|
|
| 20 |
|
|
#ifndef OPENSSL_API_INTERFACE_V11 |
| 21 |
|
|
namespace { |
| 22 |
|
|
|
| 23 |
|
|
pthread_mutex_t *gLibcryptoLocks = NULL; |
| 24 |
|
|
|
| 25 |
|
|
static void CallbackLibcryptoLock(int mode, int type, const char * /* file */, |
| 26 |
|
|
int /* line */) { |
| 27 |
|
|
int retval; |
| 28 |
|
|
|
| 29 |
|
|
if (mode & CRYPTO_LOCK) { |
| 30 |
|
|
retval = pthread_mutex_lock(&(gLibcryptoLocks[type])); |
| 31 |
|
|
} else { |
| 32 |
|
|
retval = pthread_mutex_unlock(&(gLibcryptoLocks[type])); |
| 33 |
|
|
} |
| 34 |
|
|
assert(retval == 0); |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
|
static unsigned long CallbackLibcryptoThreadId() { // NOLINT(runtime/int) |
| 38 |
|
|
return platform_gettid(); |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
} // anonymous namespace |
| 42 |
|
|
#endif |
| 43 |
|
|
|
| 44 |
|
|
|
| 45 |
|
6 |
void crypto::InitRng() { RAND_poll(); } |
| 46 |
|
|
|
| 47 |
|
|
|
| 48 |
|
102 |
void crypto::SetupLibcryptoMt() { |
| 49 |
|
|
#ifndef OPENSSL_API_INTERFACE_V11 |
| 50 |
|
|
if (gLibcryptoLocks != NULL) |
| 51 |
|
|
return; |
| 52 |
|
|
|
| 53 |
|
|
gLibcryptoLocks = static_cast<pthread_mutex_t *>( |
| 54 |
|
|
OPENSSL_malloc(CRYPTO_num_locks() * sizeof(pthread_mutex_t))); |
| 55 |
|
|
for (int i = 0; i < CRYPTO_num_locks(); ++i) { |
| 56 |
|
|
int retval = pthread_mutex_init(&(gLibcryptoLocks[i]), NULL); |
| 57 |
|
|
assert(retval == 0); |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
CRYPTO_set_id_callback(CallbackLibcryptoThreadId); |
| 61 |
|
|
CRYPTO_set_locking_callback(CallbackLibcryptoLock); |
| 62 |
|
|
#endif |
| 63 |
|
102 |
} |
| 64 |
|
|
|
| 65 |
|
|
|
| 66 |
|
101 |
void crypto::CleanupLibcryptoMt() { |
| 67 |
|
|
#ifndef OPENSSL_API_INTERFACE_V11 |
| 68 |
|
|
if (gLibcryptoLocks == NULL) |
| 69 |
|
|
return; |
| 70 |
|
|
|
| 71 |
|
|
CRYPTO_set_locking_callback(NULL); |
| 72 |
|
|
for (int i = 0; i < CRYPTO_num_locks(); ++i) |
| 73 |
|
|
pthread_mutex_destroy(&(gLibcryptoLocks[i])); |
| 74 |
|
|
|
| 75 |
|
|
OPENSSL_free(gLibcryptoLocks); |
| 76 |
|
|
gLibcryptoLocks = NULL; |
| 77 |
|
|
#endif |
| 78 |
|
101 |
} |
| 79 |
|
|
|
| 80 |
|
|
#ifdef CVMFS_NAMESPACE_GUARD |
| 81 |
|
|
} // namespace CVMFS_NAMESPACE_GUARD |
| 82 |
|
|
#endif |
| 83 |
|
|
|