CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
crypto_util.cc
Go to the documentation of this file.
1 
5 #include "crypto/crypto_util.h"
6 
7 #include <pthread.h>
8 
9 #include <openssl/crypto.h>
10 #include <openssl/rand.h>
11 
12 #include <cassert>
13 
14 #include "crypto/openssl_version.h"
15 #include "util/platform.h"
16 
17 #ifdef CVMFS_NAMESPACE_GUARD
18 namespace CVMFS_NAMESPACE_GUARD {
19 #endif
20 
21 #ifndef OPENSSL_API_INTERFACE_V11
22 namespace {
23 
24 pthread_mutex_t *gLibcryptoLocks = NULL;
25 
26 static void CallbackLibcryptoLock(int mode, int type,
27  const char * /* file */, int /* line */)
28 {
29  int retval;
30 
31  if (mode & CRYPTO_LOCK) {
32  retval = pthread_mutex_lock(&(gLibcryptoLocks[type]));
33  } else {
34  retval = pthread_mutex_unlock(&(gLibcryptoLocks[type]));
35  }
36  assert(retval == 0);
37 }
38 
39 static unsigned long CallbackLibcryptoThreadId() { // NOLINT(runtime/int)
40  return platform_gettid();
41 }
42 
43 } // anonymous namespace
44 #endif
45 
46 
48  RAND_poll();
49 }
50 
51 
53 #ifndef OPENSSL_API_INTERFACE_V11
54  if (gLibcryptoLocks != NULL)
55  return;
56 
57  gLibcryptoLocks = static_cast<pthread_mutex_t *>(OPENSSL_malloc(
58  CRYPTO_num_locks() * sizeof(pthread_mutex_t)));
59  for (int i = 0; i < CRYPTO_num_locks(); ++i) {
60  int retval = pthread_mutex_init(&(gLibcryptoLocks[i]), NULL);
61  assert(retval == 0);
62  }
63 
64  CRYPTO_set_id_callback(CallbackLibcryptoThreadId);
65  CRYPTO_set_locking_callback(CallbackLibcryptoLock);
66 #endif
67 }
68 
69 
71 #ifndef OPENSSL_API_INTERFACE_V11
72  if (gLibcryptoLocks == NULL)
73  return;
74 
75  CRYPTO_set_locking_callback(NULL);
76  for (int i = 0; i < CRYPTO_num_locks(); ++i)
77  pthread_mutex_destroy(&(gLibcryptoLocks[i]));
78 
79  OPENSSL_free(gLibcryptoLocks);
80  gLibcryptoLocks = NULL;
81 #endif
82 }
83 
84 #ifdef CVMFS_NAMESPACE_GUARD
85 } // namespace CVMFS_NAMESPACE_GUARD
86 #endif
CVMFS_EXPORT void InitRng()
Definition: crypto_util.cc:47
pthread_t platform_gettid()
static void CallbackLibcryptoLock(int mode, int type, const char *, int)
Definition: crypto_util.cc:26
CVMFS_EXPORT void CleanupLibcryptoMt()
Definition: crypto_util.cc:70
assert((mem||(size==0))&&"Out Of Memory")
static unsigned long CallbackLibcryptoThreadId()
Definition: crypto_util.cc:39
CVMFS_EXPORT void SetupLibcryptoMt()
Definition: crypto_util.cc:52