I2P: End-to-End encrypted and anonymous Internet https://i2pd.website/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.6 KiB

#include <fstream>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
#include "RouterContext.h"
#include "util.h"
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ()
{
if (!Load ())
CreateNewRouter ();
Save ();
11 years ago
// we generate LeaseSet at every start-up
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(m_Rnd, m_LeaseSetPrivateKey, m_LeaseSetPublicKey);
}
void RouterContext::CreateNewRouter ()
{
11 years ago
m_Keys = i2p::data::CreateRandomKeys ();
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
11 years ago
i2p::data::Identity ident;
ident = m_Keys;
m_RouterInfo.SetRouterIdentity (ident);
m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO:
m_RouterInfo.SetProperty ("caps", "LR");
11 years ago
m_RouterInfo.SetProperty ("coreVersion", "0.9.8.1");
m_RouterInfo.SetProperty ("netId", "2");
11 years ago
m_RouterInfo.SetProperty ("router.version", "0.9.8.1");
m_RouterInfo.SetProperty ("start_uptime", "90m");
m_RouterInfo.CreateBuffer ();
}
11 years ago
void RouterContext::OverrideNTCPAddress (const char * host, int port)
{
m_RouterInfo.CreateBuffer ();
auto address = const_cast<i2p::data::RouterInfo::Address *>(m_RouterInfo.GetNTCPAddress ());
11 years ago
if (address)
{
address->host = boost::asio::ip::address::from_string (host);
11 years ago
address->port = port;
}
m_RouterInfo.CreateBuffer ();
}
void RouterContext::UpdateAddress (const char * host)
{
for (auto& address : m_RouterInfo.GetAddresses ())
address.host = boost::asio::ip::address::from_string (host);
m_RouterInfo.CreateBuffer ();
}
11 years ago
void RouterContext::Sign (uint8_t * buf, int len, uint8_t * signature)
{
CryptoPP::DSA::Signer signer (m_SigningPrivateKey);
signer.SignMessage (m_Rnd, buf, len, signature);
}
bool RouterContext::Load ()
{
std::ifstream fk (ROUTER_KEYS, std::ifstream::binary | std::ofstream::in);
if (!fk.is_open ()) return false;
11 years ago
fk.read ((char *)&m_Keys, sizeof (m_Keys));
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
11 years ago
CryptoPP::Integer (m_Keys.signingPrivateKey, 20));
m_RouterInfo = i2p::data::RouterInfo (ROUTER_INFO); // TODO
return true;
}
void RouterContext::Save ()
{
std::ofstream fk (ROUTER_KEYS, std::ofstream::binary | std::ofstream::out);
11 years ago
fk.write ((char *)&m_Keys, sizeof (m_Keys));
std::ofstream fi (ROUTER_INFO, std::ofstream::binary | std::ofstream::out);
fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ());
}
}