Browse Source

router's NTCP2 private keys

pull/1194/head
orignal 7 years ago
parent
commit
a8278fc78b
  1. 1
      libi2pd/Config.cpp
  2. 31
      libi2pd/RouterContext.cpp
  3. 11
      libi2pd/RouterContext.h

1
libi2pd/Config.cpp

@ -59,6 +59,7 @@ namespace config {
("ntcp", value<bool>()->default_value(true), "Enable NTCP transport") ("ntcp", value<bool>()->default_value(true), "Enable NTCP transport")
("ssu", value<bool>()->default_value(true), "Enable SSU transport") ("ssu", value<bool>()->default_value(true), "Enable SSU transport")
("ntcpproxy", value<std::string>()->default_value(""), "Proxy URL for NTCP transport") ("ntcpproxy", value<std::string>()->default_value(""), "Proxy URL for NTCP transport")
("ntcp2", value<bool>()->zero_tokens()->default_value(false), "Enable NTCP2 (experimental)")
#ifdef _WIN32 #ifdef _WIN32
("svcctl", value<std::string>()->default_value(""), "Windows service management ('install' or 'remove')") ("svcctl", value<std::string>()->default_value(""), "Windows service management ('install' or 'remove')")
("insomnia", value<bool>()->zero_tokens()->default_value(false), "Prevent system from sleeping") ("insomnia", value<bool>()->zero_tokens()->default_value(false), "Prevent system from sleeping")

31
libi2pd/RouterContext.cpp

@ -1,4 +1,5 @@
#include <fstream> #include <fstream>
#include <openssl/rand.h>
#include "Config.h" #include "Config.h"
#include "Crypto.h" #include "Crypto.h"
#include "Timestamp.h" #include "Timestamp.h"
@ -98,6 +99,16 @@ namespace i2p
m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch (); m_LastUpdateTime = i2p::util::GetSecondsSinceEpoch ();
} }
void RouterContext::NewNTCP2Keys ()
{
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
RAND_bytes (m_NTCP2Keys->staticKey, 32);
RAND_bytes (m_NTCP2Keys->iv, 16);
// save
std::ofstream fk (i2p::fs::DataDirPath (NTCP2_KEYS), std::ofstream::binary | std::ofstream::out);
fk.write ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
}
void RouterContext::SetStatus (RouterStatus status) void RouterContext::SetStatus (RouterStatus status)
{ {
if (status != m_Status) if (status != m_Status)
@ -429,6 +440,26 @@ namespace i2p
if (IsUnreachable ()) if (IsUnreachable ())
SetReachable (); // we assume reachable until we discover firewall through peer tests SetReachable (); // we assume reachable until we discover firewall through peer tests
// read NTCP2
bool ntcp2; i2p::config::GetOption("ntcp2", ntcp2);
if (ntcp2)
{
std::ifstream n2k (i2p::fs::DataDirPath (NTCP2_KEYS), std::ifstream::in | std::ifstream::binary);
if (n2k)
{
n2k.seekg (0, std::ios::end);
len = fk.tellg();
n2k.seekg (0, std::ios::beg);
if (len == sizeof (NTCP2PrivateKeys))
{
m_NTCP2Keys.reset (new NTCP2PrivateKeys ());
n2k.read ((char *)m_NTCP2Keys.get (), sizeof (NTCP2PrivateKeys));
}
}
if (!m_NTCP2Keys)
NewNTCP2Keys ();
}
return true; return true;
} }

11
libi2pd/RouterContext.h

@ -14,6 +14,7 @@ namespace i2p
{ {
const char ROUTER_INFO[] = "router.info"; const char ROUTER_INFO[] = "router.info";
const char ROUTER_KEYS[] = "router.keys"; const char ROUTER_KEYS[] = "router.keys";
const char NTCP2_KEYS[] = "ntcp2.keys";
const int ROUTER_INFO_UPDATE_INTERVAL = 1800; // 30 minutes const int ROUTER_INFO_UPDATE_INTERVAL = 1800; // 30 minutes
enum RouterStatus enum RouterStatus
@ -32,6 +33,14 @@ namespace i2p
class RouterContext: public i2p::garlic::GarlicDestination class RouterContext: public i2p::garlic::GarlicDestination
{ {
private:
struct NTCP2PrivateKeys
{
uint8_t staticKey[32];
uint8_t iv[16];
};
public: public:
RouterContext (); RouterContext ();
@ -108,6 +117,7 @@ namespace i2p
void CreateNewRouter (); void CreateNewRouter ();
void NewRouterInfo (); void NewRouterInfo ();
void UpdateRouterInfo (); void UpdateRouterInfo ();
void NewNTCP2Keys ();
bool Load (); bool Load ();
void SaveKeys (); void SaveKeys ();
@ -125,6 +135,7 @@ namespace i2p
RouterError m_Error; RouterError m_Error;
int m_NetID; int m_NetID;
std::mutex m_GarlicMutex; std::mutex m_GarlicMutex;
std::unique_ptr<NTCP2PrivateKeys> m_NTCP2Keys;
}; };
extern RouterContext context; extern RouterContext context;

Loading…
Cancel
Save