mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-02-02 11:04:00 +01:00
fixed race condition of openssl calls
This commit is contained in:
parent
8daa7561fa
commit
ef4dc3cbc9
31
Crypto.cpp
31
Crypto.cpp
|
@ -1,9 +1,13 @@
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <mutex>
|
||||||
#include <openssl/sha.h>
|
#include <openssl/sha.h>
|
||||||
#include <openssl/dh.h>
|
#include <openssl/dh.h>
|
||||||
#include <openssl/md5.h>
|
#include <openssl/md5.h>
|
||||||
#include <openssl/rand.h>
|
#include <openssl/rand.h>
|
||||||
|
#include <openssl/ssl.h>
|
||||||
|
#include <openssl/crypto.h>
|
||||||
#include "Log.h"
|
#include "Log.h"
|
||||||
//#include "TunnelBase.h"
|
//#include "TunnelBase.h"
|
||||||
#include "Crypto.h"
|
#include "Crypto.h"
|
||||||
|
@ -677,6 +681,33 @@ namespace crypto
|
||||||
m_IVDecryption.Decrypt ((ChipherBlock *)out, (ChipherBlock *)out); // double iv
|
m_IVDecryption.Decrypt ((ChipherBlock *)out, (ChipherBlock *)out); // double iv
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::vector <std::unique_ptr<std::mutex> > m_OpenSSLMutexes;
|
||||||
|
static void OpensslLockingCallback(int mode, int type, const char * file, int line)
|
||||||
|
{
|
||||||
|
if (type > 0 && (size_t)type < m_OpenSSLMutexes.size ())
|
||||||
|
{
|
||||||
|
if (mode & CRYPTO_LOCK)
|
||||||
|
m_OpenSSLMutexes[type]->lock ();
|
||||||
|
else
|
||||||
|
m_OpenSSLMutexes[type]->unlock ();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void InitCrypto ()
|
||||||
|
{
|
||||||
|
SSL_library_init ();
|
||||||
|
auto numLocks = CRYPTO_num_locks();
|
||||||
|
for (int i = 0; i < numLocks; i++)
|
||||||
|
m_OpenSSLMutexes.emplace_back (new std::mutex);
|
||||||
|
CRYPTO_set_locking_callback (OpensslLockingCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
void TerminateCrypto ()
|
||||||
|
{
|
||||||
|
CRYPTO_set_locking_callback (nullptr);
|
||||||
|
m_OpenSSLMutexes.clear ();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
3
Crypto.h
3
Crypto.h
|
@ -272,6 +272,9 @@ namespace crypto
|
||||||
CBCDecryption m_LayerDecryption;
|
CBCDecryption m_LayerDecryption;
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void InitCrypto ();
|
||||||
|
void TerminateCrypto ();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -19,8 +19,7 @@
|
||||||
#include "HTTPServer.h"
|
#include "HTTPServer.h"
|
||||||
#include "I2PControl.h"
|
#include "I2PControl.h"
|
||||||
#include "ClientContext.h"
|
#include "ClientContext.h"
|
||||||
// ssl.h somehow pulls Windows.h stuff that has to go after asio
|
#include "Crypto.h"
|
||||||
#include <openssl/ssl.h>
|
|
||||||
|
|
||||||
#ifdef USE_UPNP
|
#ifdef USE_UPNP
|
||||||
#include "UPnP.h"
|
#include "UPnP.h"
|
||||||
|
@ -60,7 +59,7 @@ namespace i2p
|
||||||
|
|
||||||
bool Daemon_Singleton::init(int argc, char* argv[])
|
bool Daemon_Singleton::init(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
SSL_library_init ();
|
i2p::crypto::InitCrypto ();
|
||||||
i2p::util::config::OptionParser(argc, argv);
|
i2p::util::config::OptionParser(argc, argv);
|
||||||
i2p::context.Init ();
|
i2p::context.Init ();
|
||||||
|
|
||||||
|
@ -171,6 +170,7 @@ namespace i2p
|
||||||
d.m_I2PControlService->Stop ();
|
d.m_I2PControlService->Stop ();
|
||||||
d.m_I2PControlService = nullptr;
|
d.m_I2PControlService = nullptr;
|
||||||
}
|
}
|
||||||
|
i2p::crypto::TerminateCrypto ();
|
||||||
StopLog ();
|
StopLog ();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
|
|
7
api.cpp
7
api.cpp
|
@ -7,6 +7,7 @@
|
||||||
#include "RouterContext.h"
|
#include "RouterContext.h"
|
||||||
#include "Identity.h"
|
#include "Identity.h"
|
||||||
#include "Destination.h"
|
#include "Destination.h"
|
||||||
|
#include "Crypto.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "api.h"
|
#include "api.h"
|
||||||
|
|
||||||
|
@ -18,9 +19,15 @@ namespace api
|
||||||
{
|
{
|
||||||
i2p::util::filesystem::SetAppName (appName);
|
i2p::util::filesystem::SetAppName (appName);
|
||||||
i2p::util::config::OptionParser(argc, argv);
|
i2p::util::config::OptionParser(argc, argv);
|
||||||
|
i2p::crypto::InitCrypto ();
|
||||||
i2p::context.Init ();
|
i2p::context.Init ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void TerminateI2P ()
|
||||||
|
{
|
||||||
|
i2p::crypto::TerminateCrypto ();
|
||||||
|
}
|
||||||
|
|
||||||
void StartI2P (std::ostream * logStream)
|
void StartI2P (std::ostream * logStream)
|
||||||
{
|
{
|
||||||
if (logStream)
|
if (logStream)
|
||||||
|
|
1
api.h
1
api.h
|
@ -13,6 +13,7 @@ namespace api
|
||||||
{
|
{
|
||||||
// initialization start and stop
|
// initialization start and stop
|
||||||
void InitI2P (int argc, char* argv[], const char * appName);
|
void InitI2P (int argc, char* argv[], const char * appName);
|
||||||
|
void TerminateI2P ();
|
||||||
void StartI2P (std::ostream * logStream = nullptr);
|
void StartI2P (std::ostream * logStream = nullptr);
|
||||||
// write system log to logStream, if not specified to <appName>.log in application's folder
|
// write system log to logStream, if not specified to <appName>.log in application's folder
|
||||||
void StopI2P ();
|
void StopI2P ();
|
||||||
|
|
Loading…
Reference in a new issue