Browse Source

moved DHKeysPair to Transport

pull/105/head
orignal 10 years ago
parent
commit
a8871d9f98
  1. 9
      Identity.cpp
  2. 15
      Identity.h
  3. 5
      NTCPSession.h
  4. 4
      SSU.h
  5. 26
      Transports.cpp
  6. 20
      Transports.h

9
Identity.cpp

@ -2,7 +2,6 @@
#include <stdio.h> #include <stdio.h>
#include <cryptopp/sha.h> #include <cryptopp/sha.h>
#include <cryptopp/osrng.h> #include <cryptopp/osrng.h>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h> #include <cryptopp/dsa.h>
#include "base64.h" #include "base64.h"
#include "CryptoConst.h" #include "CryptoConst.h"
@ -293,14 +292,6 @@ namespace data
return keys; return keys;
} }
void CreateRandomDHKeysPair (DHKeysPair * keys)
{
if (!keys) return;
CryptoPP::AutoSeededRandomPool rnd;
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(rnd, keys->privateKey, keys->publicKey);
}
IdentHash CreateRoutingKey (const IdentHash& ident) IdentHash CreateRoutingKey (const IdentHash& ident)
{ {
uint8_t buf[41]; // ident + yyyymmdd uint8_t buf[41]; // ident + yyyymmdd

15
Identity.h

@ -67,13 +67,6 @@ namespace data
typedef Tag<32> IdentHash; typedef Tag<32> IdentHash;
#pragma pack(1) #pragma pack(1)
struct DHKeysPair // transient keys for transport sessions
{
uint8_t publicKey[256];
uint8_t privateKey[256];
};
struct Keys struct Keys
{ {
uint8_t privateKey[256]; uint8_t privateKey[256];
@ -81,7 +74,8 @@ namespace data
uint8_t publicKey[256]; uint8_t publicKey[256];
uint8_t signingKey[128]; uint8_t signingKey[128];
}; };
#pragma pack()
Keys CreateRandomKeys ();
const uint8_t CERTIFICATE_TYPE_NULL = 0; const uint8_t CERTIFICATE_TYPE_NULL = 0;
const uint8_t CERTIFICATE_TYPE_HASHCASH = 1; const uint8_t CERTIFICATE_TYPE_HASHCASH = 1;
@ -184,11 +178,6 @@ namespace data
i2p::crypto::Signer * m_Signer; i2p::crypto::Signer * m_Signer;
}; };
#pragma pack()
Keys CreateRandomKeys ();
void CreateRandomDHKeysPair (DHKeysPair * keys); // for transport sessions
// kademlia // kademlia
struct XORMetric struct XORMetric
{ {

5
NTCPSession.h

@ -14,6 +14,8 @@
namespace i2p namespace i2p
{ {
class DHKeysPair;
namespace ntcp namespace ntcp
{ {
@ -65,6 +67,7 @@ namespace ntcp
const size_t NTCP_MAX_MESSAGE_SIZE = 16384; const size_t NTCP_MAX_MESSAGE_SIZE = 16384;
const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028) const size_t NTCP_BUFFER_SIZE = 1040; // fits one tunnel message (1028)
const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes const int NTCP_TERMINATION_TIMEOUT = 120; // 2 minutes
class NTCPSession class NTCPSession
{ {
public: public:
@ -127,7 +130,7 @@ namespace ntcp
boost::asio::ip::tcp::socket m_Socket; boost::asio::ip::tcp::socket m_Socket;
boost::asio::deadline_timer m_TerminationTimer; boost::asio::deadline_timer m_TerminationTimer;
bool m_IsEstablished; bool m_IsEstablished;
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
i2p::crypto::CBCDecryption m_Decryption; i2p::crypto::CBCDecryption m_Decryption;
i2p::crypto::CBCEncryption m_Encryption; i2p::crypto::CBCEncryption m_Encryption;

4
SSU.h

@ -17,6 +17,8 @@
namespace i2p namespace i2p
{ {
class DHKeysPair;
namespace ssu namespace ssu
{ {
#pragma pack(1) #pragma pack(1)
@ -131,7 +133,7 @@ namespace ssu
const i2p::data::RouterInfo * m_RemoteRouter; const i2p::data::RouterInfo * m_RemoteRouter;
i2p::data::IdentHash m_RemoteIdent; // if m_RemoteRouter is null i2p::data::IdentHash m_RemoteIdent; // if m_RemoteRouter is null
boost::asio::deadline_timer m_Timer; boost::asio::deadline_timer m_Timer;
i2p::data::DHKeysPair * m_DHKeysPair; // X - for client and Y - for server DHKeysPair * m_DHKeysPair; // X - for client and Y - for server
bool m_PeerTest; bool m_PeerTest;
SessionState m_State; SessionState m_State;
bool m_IsSessionKey; bool m_IsSessionKey;

26
Transports.cpp

@ -1,5 +1,7 @@
#include <cryptopp/dh.h>
#include <boost/bind.hpp> #include <boost/bind.hpp>
#include "Log.h" #include "Log.h"
#include "CryptoConst.h"
#include "RouterContext.h" #include "RouterContext.h"
#include "I2NPProtocol.h" #include "I2NPProtocol.h"
#include "NetDb.h" #include "NetDb.h"
@ -9,6 +11,11 @@ using namespace i2p::data;
namespace i2p namespace i2p
{ {
DHKeysPairSupplier::DHKeysPairSupplier (int size):
m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr)
{
}
DHKeysPairSupplier::~DHKeysPairSupplier () DHKeysPairSupplier::~DHKeysPairSupplier ()
{ {
Stop (); Stop ();
@ -48,17 +55,18 @@ namespace i2p
{ {
if (num > 0) if (num > 0)
{ {
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
for (int i = 0; i < num; i++) for (int i = 0; i < num; i++)
{ {
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair (); DHKeysPair * pair = new DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair); dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
std::unique_lock<std::mutex> l(m_AcquiredMutex); std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair); m_Queue.push (pair);
} }
} }
} }
i2p::data::DHKeysPair * DHKeysPairSupplier::Acquire () DHKeysPair * DHKeysPairSupplier::Acquire ()
{ {
if (!m_Queue.empty ()) if (!m_Queue.empty ())
{ {
@ -70,13 +78,14 @@ namespace i2p
} }
else // queue is empty, create new else // queue is empty, create new
{ {
i2p::data::DHKeysPair * pair = new i2p::data::DHKeysPair (); DHKeysPair * pair = new DHKeysPair ();
i2p::data::CreateRandomDHKeysPair (pair); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(m_Rnd, pair->privateKey, pair->publicKey);
return pair; return pair;
} }
} }
void DHKeysPairSupplier::Return (i2p::data::DHKeysPair * pair) void DHKeysPairSupplier::Return (DHKeysPair * pair)
{ {
std::unique_lock<std::mutex> l(m_AcquiredMutex); std::unique_lock<std::mutex> l(m_AcquiredMutex);
m_Queue.push (pair); m_Queue.push (pair);
@ -319,13 +328,12 @@ namespace i2p
} }
} }
DHKeysPair * Transports::GetNextDHKeysPair ()
i2p::data::DHKeysPair * Transports::GetNextDHKeysPair ()
{ {
return m_DHKeysPairSupplier.Acquire (); return m_DHKeysPairSupplier.Acquire ();
} }
void Transports::ReuseDHKeysPair (i2p::data::DHKeysPair * pair) void Transports::ReuseDHKeysPair (DHKeysPair * pair)
{ {
m_DHKeysPairSupplier.Return (pair); m_DHKeysPairSupplier.Return (pair);
} }

20
Transports.h

@ -8,6 +8,7 @@
#include <map> #include <map>
#include <queue> #include <queue>
#include <string> #include <string>
#include <cryptopp/osrng.h>
#include <boost/asio.hpp> #include <boost/asio.hpp>
#include "NTCPSession.h" #include "NTCPSession.h"
#include "SSU.h" #include "SSU.h"
@ -17,16 +18,22 @@
namespace i2p namespace i2p
{ {
struct DHKeysPair // transient keys for transport sessions
{
uint8_t publicKey[256];
uint8_t privateKey[256];
};
class DHKeysPairSupplier class DHKeysPairSupplier
{ {
public: public:
DHKeysPairSupplier (int size): m_QueueSize (size), m_IsRunning (false), m_Thread (nullptr) {}; DHKeysPairSupplier (int size);
~DHKeysPairSupplier (); ~DHKeysPairSupplier ();
void Start (); void Start ();
void Stop (); void Stop ();
i2p::data::DHKeysPair * Acquire (); DHKeysPair * Acquire ();
void Return (i2p::data::DHKeysPair * pair); void Return (DHKeysPair * pair);
private: private:
@ -36,12 +43,13 @@ namespace i2p
private: private:
const int m_QueueSize; const int m_QueueSize;
std::queue<i2p::data::DHKeysPair *> m_Queue; std::queue<DHKeysPair *> m_Queue;
bool m_IsRunning; bool m_IsRunning;
std::thread * m_Thread; std::thread * m_Thread;
std::condition_variable m_Acquired; std::condition_variable m_Acquired;
std::mutex m_AcquiredMutex; std::mutex m_AcquiredMutex;
CryptoPP::AutoSeededRandomPool m_Rnd;
}; };
class Transports class Transports
@ -55,8 +63,8 @@ namespace i2p
void Stop (); void Stop ();
boost::asio::io_service& GetService () { return m_Service; }; boost::asio::io_service& GetService () { return m_Service; };
i2p::data::DHKeysPair * GetNextDHKeysPair (); DHKeysPair * GetNextDHKeysPair ();
void ReuseDHKeysPair (i2p::data::DHKeysPair * pair); void ReuseDHKeysPair (DHKeysPair * pair);
void AddNTCPSession (i2p::ntcp::NTCPSession * session); void AddNTCPSession (i2p::ntcp::NTCPSession * session);
void RemoveNTCPSession (i2p::ntcp::NTCPSession * session); void RemoveNTCPSession (i2p::ntcp::NTCPSession * session);

Loading…
Cancel
Save