2014-01-15 05:57:33 +04:00
|
|
|
#include "I2PEndian.h"
|
2014-01-09 07:47:22 +04:00
|
|
|
#include <cryptopp/dsa.h>
|
|
|
|
#include "CryptoConst.h"
|
2013-11-25 03:10:27 +04:00
|
|
|
#include "Log.h"
|
2014-01-15 05:57:33 +04:00
|
|
|
#include "Timestamp.h"
|
2014-01-26 18:07:31 +04:00
|
|
|
#include "NetDb.h"
|
2014-07-29 21:44:54 +04:00
|
|
|
#include "TunnelPool.h"
|
2013-11-25 03:10:27 +04:00
|
|
|
#include "LeaseSet.h"
|
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace data
|
|
|
|
{
|
2013-12-20 07:05:45 +04:00
|
|
|
|
2014-07-31 20:59:43 +04:00
|
|
|
LeaseSet::LeaseSet (const uint8_t * buf, int len, bool unsolicited):
|
|
|
|
m_IsUnsolicited (unsolicited)
|
2013-11-25 03:10:27 +04:00
|
|
|
{
|
2014-07-29 21:44:54 +04:00
|
|
|
memcpy (m_Buffer, buf, len);
|
|
|
|
m_BufferLen = len;
|
|
|
|
ReadFromBuffer ();
|
|
|
|
}
|
|
|
|
|
2014-07-31 20:59:43 +04:00
|
|
|
LeaseSet::LeaseSet (const i2p::tunnel::TunnelPool& pool):
|
|
|
|
m_IsUnsolicited (false)
|
2014-07-29 21:44:54 +04:00
|
|
|
{
|
|
|
|
m_BufferLen = 0;
|
|
|
|
// header
|
|
|
|
const i2p::data::LocalDestination& localDestination = pool.GetLocalDestination ();
|
|
|
|
LeaseSetHeader * header = (LeaseSetHeader *)m_Buffer;
|
|
|
|
header->destination = localDestination.GetIdentity ();
|
|
|
|
memcpy (header->encryptionKey, localDestination.GetEncryptionPublicKey (), 256);
|
|
|
|
memset (header->signingKey, 0, 128);
|
|
|
|
auto tunnels = pool.GetInboundTunnels (5); // 5 tunnels maximum
|
|
|
|
header->num = tunnels.size (); // num leases
|
|
|
|
m_BufferLen += sizeof (LeaseSetHeader);
|
|
|
|
// leases
|
|
|
|
for (auto it: tunnels)
|
|
|
|
{
|
|
|
|
Lease * lease = (Lease *)(m_Buffer + m_BufferLen);
|
|
|
|
memcpy (lease->tunnelGateway, it->GetNextIdentHash (), 32);
|
|
|
|
lease->tunnelID = htobe32 (it->GetNextTunnelID ());
|
|
|
|
uint64_t ts = it->GetCreationTime () + i2p::tunnel::TUNNEL_EXPIRATION_TIMEOUT - 60; // 1 minute before expiration
|
|
|
|
ts *= 1000; // in milliseconds
|
|
|
|
lease->endDate = htobe64 (ts);
|
|
|
|
m_BufferLen += sizeof (Lease);
|
|
|
|
}
|
|
|
|
// signature
|
|
|
|
localDestination.Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen);
|
|
|
|
m_BufferLen += 40;
|
|
|
|
LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created");
|
|
|
|
|
|
|
|
ReadFromBuffer ();
|
2014-07-22 04:14:11 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void LeaseSet::Update (const uint8_t * buf, int len)
|
|
|
|
{
|
|
|
|
m_Leases.clear ();
|
2014-07-29 21:44:54 +04:00
|
|
|
memcpy (m_Buffer, buf, len);
|
|
|
|
m_BufferLen = len;
|
|
|
|
ReadFromBuffer ();
|
2014-07-22 04:14:11 +04:00
|
|
|
}
|
|
|
|
|
2014-07-29 21:44:54 +04:00
|
|
|
void LeaseSet::ReadFromBuffer ()
|
2014-07-22 04:14:11 +04:00
|
|
|
{
|
2014-07-29 21:44:54 +04:00
|
|
|
const LeaseSetHeader * header = (const LeaseSetHeader *)m_Buffer;
|
2013-12-20 07:05:45 +04:00
|
|
|
m_Identity = header->destination;
|
2014-04-08 11:11:48 +04:00
|
|
|
m_IdentHash = m_Identity.Hash();
|
2013-11-25 03:10:27 +04:00
|
|
|
memcpy (m_EncryptionKey, header->encryptionKey, 256);
|
|
|
|
LogPrint ("LeaseSet num=", (int)header->num);
|
|
|
|
|
2014-01-26 18:07:31 +04:00
|
|
|
// process leases
|
2014-07-29 21:44:54 +04:00
|
|
|
const uint8_t * leases = m_Buffer + sizeof (LeaseSetHeader);
|
2013-11-25 03:10:27 +04:00
|
|
|
for (int i = 0; i < header->num; i++)
|
|
|
|
{
|
2014-01-09 07:47:22 +04:00
|
|
|
Lease lease = *(Lease *)leases;
|
|
|
|
lease.tunnelID = be32toh (lease.tunnelID);
|
2014-01-15 07:35:00 +04:00
|
|
|
lease.endDate = be64toh (lease.endDate);
|
2014-01-09 07:47:22 +04:00
|
|
|
m_Leases.push_back (lease);
|
|
|
|
leases += sizeof (Lease);
|
|
|
|
|
2014-01-26 18:07:31 +04:00
|
|
|
// check if lease's gateway is in our netDb
|
|
|
|
if (!netdb.FindRouter (lease.tunnelGateway))
|
|
|
|
{
|
|
|
|
// if not found request it
|
|
|
|
LogPrint ("Lease's tunnel gateway not found. Requested");
|
|
|
|
netdb.RequestDestination (lease.tunnelGateway);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-09 07:47:22 +04:00
|
|
|
// verify
|
|
|
|
CryptoPP::DSA::PublicKey pubKey;
|
|
|
|
pubKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag,
|
|
|
|
CryptoPP::Integer (m_Identity.signingKey, 128));
|
|
|
|
CryptoPP::DSA::Verifier verifier (pubKey);
|
2014-07-29 21:44:54 +04:00
|
|
|
if (!verifier.VerifyMessage (m_Buffer, leases - m_Buffer, leases, 40))
|
2014-01-09 07:47:22 +04:00
|
|
|
LogPrint ("LeaseSet verification failed");
|
2014-07-22 04:14:11 +04:00
|
|
|
}
|
|
|
|
|
2014-03-23 17:25:16 +04:00
|
|
|
const std::vector<Lease> LeaseSet::GetNonExpiredLeases () const
|
2014-01-15 05:57:33 +04:00
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
2014-03-23 17:25:16 +04:00
|
|
|
std::vector<Lease> leases;
|
2014-01-15 05:57:33 +04:00
|
|
|
for (auto& it: m_Leases)
|
|
|
|
if (ts < it.endDate)
|
2014-03-23 17:25:16 +04:00
|
|
|
leases.push_back (it);
|
2014-01-15 05:57:33 +04:00
|
|
|
return leases;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LeaseSet::HasExpiredLeases () const
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
|
|
|
for (auto& it: m_Leases)
|
|
|
|
if (ts >= it.endDate) return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LeaseSet::HasNonExpiredLeases () const
|
|
|
|
{
|
|
|
|
auto ts = i2p::util::GetMillisecondsSinceEpoch ();
|
|
|
|
for (auto& it: m_Leases)
|
|
|
|
if (ts < it.endDate) return true;
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-25 03:10:27 +04:00
|
|
|
}
|
|
|
|
}
|