Browse Source

RouterContext added

pull/6/head
orignal 11 years ago
parent
commit
a0533dd987
  1. 83
      RouterContext.cpp
  2. 45
      RouterContext.h

83
RouterContext.cpp

@ -0,0 +1,83 @@
#include <fstream>
#include <cryptopp/dh.h>
#include <cryptopp/dsa.h>
#include "CryptoConst.h"
#include "RouterContext.h"
namespace i2p
{
RouterContext context;
RouterContext::RouterContext ()
{
if (!Load ())
CreateNewRouter ();
Save ();
}
const uint8_t * RouterContext::GetSigningPrivateKey () const
{
return m_SigningPrivateKeyStr;
}
void RouterContext::CreateNewRouter ()
{
i2p::data::RouterIdentity ident;
CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg);
dh.GenerateKeyPair(m_Rnd, m_PrivateKey, ident.publicKey);
m_SigningPrivateKey.Initialize (m_Rnd, i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag);
m_SigningPrivateKey.GetPrivateExponent ().Encode (m_SigningPrivateKeyStr, 20);
CryptoPP::DSA::PublicKey publicKey;
m_SigningPrivateKey.MakePublicKey (publicKey);
publicKey.GetPublicElement ().Encode (ident.signingKey, 128);
memset (ident.certificate, 0, sizeof (ident.certificate));
m_RouterInfo.SetRouterIdentity (ident);
m_RouterInfo.AddNTCPAddress ("127.0.0.1", 17007); // TODO:
m_RouterInfo.SetProperty ("caps", "LR");
m_RouterInfo.SetProperty ("coreVersion", "0.9.7");
m_RouterInfo.SetProperty ("netId", "2");
m_RouterInfo.SetProperty ("router.version", "0.9.7");
m_RouterInfo.SetProperty ("start_uptime", "90m");
m_RouterInfo.CreateBuffer ();
}
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);
if (!fk.is_open ()) return false;
fk.read ((char *)m_PrivateKey, 256);
fk.read ((char *)m_SigningPrivateKeyStr, 20);
m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
CryptoPP::Integer (m_SigningPrivateKeyStr, 20));
m_RouterInfo = i2p::data::RouterInfo (ROUTER_INFO); // TODO
return true;
}
void RouterContext::Save ()
{
std::ofstream fk (ROUTER_KEYS);
fk.write ((char *)m_PrivateKey, 256);
fk.write ((char *)m_SigningPrivateKeyStr, 20);
fk.write ((char *)m_RouterInfo.GetRouterIdentity ().publicKey, 256);
fk.write ((char *)m_RouterInfo.GetRouterIdentity ().signingKey, 128);
std::ofstream fi (ROUTER_INFO);
fi.write ((char *)m_RouterInfo.GetBuffer (), m_RouterInfo.GetBufferLen ());
}
}

45
RouterContext.h

@ -0,0 +1,45 @@
#ifndef ROUTER_CONTEXT_H__
#define ROUTER_CONTEXT_H__
#include <inttypes.h>
#include <cryptopp/dsa.h>
#include <cryptopp/osrng.h>
#include "RouterInfo.h"
namespace i2p
{
const char ROUTER_INFO[] = "router.info";
const char ROUTER_KEYS[] = "router.keys";
class RouterContext
{
public:
RouterContext ();
i2p::data::RouterInfo& GetRouterInfo () { return m_RouterInfo; };
const uint8_t * GetPrivateKey () const { return m_PrivateKey; };
const uint8_t * GetSigningPrivateKey () const;
const i2p::data::RouterIdentity& GetRouterIdentity () const { return m_RouterInfo.GetRouterIdentity (); };
CryptoPP::RandomNumberGenerator& GetRandomNumberGenerator () { return m_Rnd; };
void Sign (uint8_t * buf, int len, uint8_t * signature);
private:
void CreateNewRouter ();
bool Load ();
void Save ();
private:
i2p::data::RouterInfo m_RouterInfo;
CryptoPP::DSA::PrivateKey m_SigningPrivateKey;
uint8_t m_PrivateKey[256], m_SigningPrivateKeyStr[20];
CryptoPP::AutoSeededRandomPool m_Rnd;
};
extern RouterContext context;
}
#endif
Loading…
Cancel
Save