aboutsummaryrefslogtreecommitdiff
path: root/src/libutil/hash.cc
diff options
context:
space:
mode:
authorRobert Hensing <robert@roberthensing.nl>2023-02-01 13:34:32 +0100
committerRobert Hensing <robert@roberthensing.nl>2023-04-07 16:24:18 +0200
commit6e0b7109abb40ded327b15599b29f861d9acb3c9 (patch)
treeec76c73232d9440ecabb5bc4a2263c351b77a0a7 /src/libutil/hash.cc
parent0746951be1563b1dd590690f9ee48a2fe964bd93 (diff)
Move OpenSSL init to initLibUtil
Part of an effort to make it easier to initialize the right things, by moving code into the appropriate libraries.
Diffstat (limited to 'src/libutil/hash.cc')
-rw-r--r--src/libutil/hash.cc23
1 files changed, 23 insertions, 0 deletions
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 5735e4715..9df8bcfb4 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -1,6 +1,7 @@
#include <iostream>
#include <cstring>
+#include <openssl/crypto.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
@@ -16,6 +17,28 @@
namespace nix {
+#if OPENSSL_VERSION_NUMBER < 0x10101000L
+/* OpenSSL is not thread-safe by default - it will randomly crash
+ unless the user supplies a mutex locking function. So let's do
+ that. */
+static std::vector<std::mutex> opensslLocks;
+
+static void opensslLockCallback(int mode, int type, const char * file, int line)
+{
+ if (mode & CRYPTO_LOCK)
+ opensslLocks[type].lock();
+ else
+ opensslLocks[type].unlock();
+}
+#endif
+
+void initOpenSSL() {
+#if OPENSSL_VERSION_NUMBER < 0x10101000L
+ /* Initialise OpenSSL locking. */
+ opensslLocks = std::vector<std::mutex>(CRYPTO_num_locks());
+ CRYPTO_set_locking_callback(opensslLockCallback);
+#endif
+}
static size_t regularHashSize(HashType type) {
switch (type) {