mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2025-01-23 00:54:14 +00:00
memory pool for IdentityEx
This commit is contained in:
parent
5ad9c8e740
commit
55b2f2c625
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2022, The PurpleI2P Project
|
* Copyright (c) 2013-2023, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@ -187,7 +187,6 @@ namespace data
|
|||||||
|
|
||||||
IdentityEx::~IdentityEx ()
|
IdentityEx::~IdentityEx ()
|
||||||
{
|
{
|
||||||
delete m_Verifier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
IdentityEx& IdentityEx::operator=(const IdentityEx& other)
|
IdentityEx& IdentityEx::operator=(const IdentityEx& other)
|
||||||
@ -201,9 +200,8 @@ namespace data
|
|||||||
if (m_ExtendedLen > MAX_EXTENDED_BUFFER_SIZE) m_ExtendedLen = MAX_EXTENDED_BUFFER_SIZE;
|
if (m_ExtendedLen > MAX_EXTENDED_BUFFER_SIZE) m_ExtendedLen = MAX_EXTENDED_BUFFER_SIZE;
|
||||||
memcpy (m_ExtendedBuffer, other.m_ExtendedBuffer, m_ExtendedLen);
|
memcpy (m_ExtendedBuffer, other.m_ExtendedBuffer, m_ExtendedLen);
|
||||||
}
|
}
|
||||||
|
|
||||||
delete m_Verifier;
|
|
||||||
m_Verifier = nullptr;
|
m_Verifier = nullptr;
|
||||||
|
CreateVerifier ();
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -212,11 +210,10 @@ namespace data
|
|||||||
{
|
{
|
||||||
m_StandardIdentity = standard;
|
m_StandardIdentity = standard;
|
||||||
m_IdentHash = m_StandardIdentity.Hash ();
|
m_IdentHash = m_StandardIdentity.Hash ();
|
||||||
|
|
||||||
m_ExtendedLen = 0;
|
m_ExtendedLen = 0;
|
||||||
|
|
||||||
delete m_Verifier;
|
|
||||||
m_Verifier = nullptr;
|
m_Verifier = nullptr;
|
||||||
|
CreateVerifier ();
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
@ -249,8 +246,8 @@ namespace data
|
|||||||
m_ExtendedLen = 0;
|
m_ExtendedLen = 0;
|
||||||
SHA256(buf, GetFullLen (), m_IdentHash);
|
SHA256(buf, GetFullLen (), m_IdentHash);
|
||||||
|
|
||||||
delete m_Verifier;
|
|
||||||
m_Verifier = nullptr;
|
m_Verifier = nullptr;
|
||||||
|
CreateVerifier ();
|
||||||
|
|
||||||
return GetFullLen ();
|
return GetFullLen ();
|
||||||
}
|
}
|
||||||
@ -286,7 +283,6 @@ namespace data
|
|||||||
|
|
||||||
size_t IdentityEx::GetSigningPublicKeyLen () const
|
size_t IdentityEx::GetSigningPublicKeyLen () const
|
||||||
{
|
{
|
||||||
if (!m_Verifier) CreateVerifier ();
|
|
||||||
if (m_Verifier)
|
if (m_Verifier)
|
||||||
return m_Verifier->GetPublicKeyLen ();
|
return m_Verifier->GetPublicKeyLen ();
|
||||||
return 128;
|
return 128;
|
||||||
@ -301,7 +297,6 @@ namespace data
|
|||||||
|
|
||||||
size_t IdentityEx::GetSigningPrivateKeyLen () const
|
size_t IdentityEx::GetSigningPrivateKeyLen () const
|
||||||
{
|
{
|
||||||
if (!m_Verifier) CreateVerifier ();
|
|
||||||
if (m_Verifier)
|
if (m_Verifier)
|
||||||
return m_Verifier->GetPrivateKeyLen ();
|
return m_Verifier->GetPrivateKeyLen ();
|
||||||
return GetSignatureLen ()/2;
|
return GetSignatureLen ()/2;
|
||||||
@ -309,14 +304,12 @@ namespace data
|
|||||||
|
|
||||||
size_t IdentityEx::GetSignatureLen () const
|
size_t IdentityEx::GetSignatureLen () const
|
||||||
{
|
{
|
||||||
if (!m_Verifier) CreateVerifier ();
|
|
||||||
if (m_Verifier)
|
if (m_Verifier)
|
||||||
return m_Verifier->GetSignatureLen ();
|
return m_Verifier->GetSignatureLen ();
|
||||||
return i2p::crypto::DSA_SIGNATURE_LENGTH;
|
return i2p::crypto::DSA_SIGNATURE_LENGTH;
|
||||||
}
|
}
|
||||||
bool IdentityEx::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
bool IdentityEx::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const
|
||||||
{
|
{
|
||||||
if (!m_Verifier) CreateVerifier ();
|
|
||||||
if (m_Verifier)
|
if (m_Verifier)
|
||||||
return m_Verifier->Verify (buf, len, signature);
|
return m_Verifier->Verify (buf, len, signature);
|
||||||
return false;
|
return false;
|
||||||
@ -373,52 +366,29 @@ namespace data
|
|||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IdentityEx::CreateVerifier () const
|
void IdentityEx::CreateVerifier ()
|
||||||
{
|
{
|
||||||
if (m_Verifier) return; // don't create again
|
if (!m_Verifier)
|
||||||
auto verifier = CreateVerifier (GetSigningKeyType ());
|
{
|
||||||
if (verifier)
|
auto verifier = CreateVerifier (GetSigningKeyType ());
|
||||||
{
|
if (verifier)
|
||||||
auto keyLen = verifier->GetPublicKeyLen ();
|
|
||||||
if (keyLen <= 128)
|
|
||||||
verifier->SetPublicKey (m_StandardIdentity.signingKey + 128 - keyLen);
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
// for P521
|
auto keyLen = verifier->GetPublicKeyLen ();
|
||||||
uint8_t * signingKey = new uint8_t[keyLen];
|
if (keyLen <= 128)
|
||||||
memcpy (signingKey, m_StandardIdentity.signingKey, 128);
|
verifier->SetPublicKey (m_StandardIdentity.signingKey + 128 - keyLen);
|
||||||
size_t excessLen = keyLen - 128;
|
else
|
||||||
memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
|
{
|
||||||
verifier->SetPublicKey (signingKey);
|
// for P521
|
||||||
delete[] signingKey;
|
uint8_t * signingKey = new uint8_t[keyLen];
|
||||||
|
memcpy (signingKey, m_StandardIdentity.signingKey, 128);
|
||||||
|
size_t excessLen = keyLen - 128;
|
||||||
|
memcpy (signingKey + 128, m_ExtendedBuffer + 4, excessLen); // right after signing and crypto key types
|
||||||
|
verifier->SetPublicKey (signingKey);
|
||||||
|
delete[] signingKey;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
m_Verifier.reset (verifier);
|
||||||
UpdateVerifier (verifier);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void IdentityEx::UpdateVerifier (i2p::crypto::Verifier * verifier) const
|
|
||||||
{
|
|
||||||
bool del = false;
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> l(m_VerifierMutex);
|
|
||||||
if (!m_Verifier)
|
|
||||||
m_Verifier = verifier;
|
|
||||||
else
|
|
||||||
del = true;
|
|
||||||
}
|
|
||||||
if (del)
|
|
||||||
delete verifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
void IdentityEx::DropVerifier () const
|
|
||||||
{
|
|
||||||
i2p::crypto::Verifier * verifier;
|
|
||||||
{
|
|
||||||
std::lock_guard<std::mutex> l(m_VerifierMutex);
|
|
||||||
verifier = m_Verifier;
|
|
||||||
m_Verifier = nullptr;
|
|
||||||
}
|
|
||||||
delete verifier;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (CryptoKeyType keyType, const uint8_t * key)
|
std::shared_ptr<i2p::crypto::CryptoKeyEncryptor> IdentityEx::CreateEncryptor (CryptoKeyType keyType, const uint8_t * key)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright (c) 2013-2022, The PurpleI2P Project
|
* Copyright (c) 2013-2023, The PurpleI2P Project
|
||||||
*
|
*
|
||||||
* This file is part of Purple i2pd project and licensed under BSD3
|
* This file is part of Purple i2pd project and licensed under BSD3
|
||||||
*
|
*
|
||||||
@ -13,9 +13,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <atomic>
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <mutex>
|
|
||||||
#include "Base.h"
|
#include "Base.h"
|
||||||
#include "Signature.h"
|
#include "Signature.h"
|
||||||
#include "CryptoKey.h"
|
#include "CryptoKey.h"
|
||||||
@ -118,7 +116,6 @@ namespace data
|
|||||||
SigningKeyType GetSigningKeyType () const;
|
SigningKeyType GetSigningKeyType () const;
|
||||||
bool IsRSA () const; // signing key type
|
bool IsRSA () const; // signing key type
|
||||||
CryptoKeyType GetCryptoKeyType () const;
|
CryptoKeyType GetCryptoKeyType () const;
|
||||||
void DropVerifier () const; // to save memory
|
|
||||||
|
|
||||||
bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); }
|
bool operator == (const IdentityEx & other) const { return GetIdentHash() == other.GetIdentHash(); }
|
||||||
void RecalculateIdentHash(uint8_t * buff=nullptr);
|
void RecalculateIdentHash(uint8_t * buff=nullptr);
|
||||||
@ -128,15 +125,13 @@ namespace data
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
void CreateVerifier () const;
|
void CreateVerifier ();
|
||||||
void UpdateVerifier (i2p::crypto::Verifier * verifier) const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
Identity m_StandardIdentity;
|
Identity m_StandardIdentity;
|
||||||
IdentHash m_IdentHash;
|
IdentHash m_IdentHash;
|
||||||
mutable i2p::crypto::Verifier * m_Verifier = nullptr;
|
std::unique_ptr<i2p::crypto::Verifier> m_Verifier;
|
||||||
mutable std::mutex m_VerifierMutex;
|
|
||||||
size_t m_ExtendedLen;
|
size_t m_ExtendedLen;
|
||||||
uint8_t m_ExtendedBuffer[MAX_EXTENDED_BUFFER_SIZE];
|
uint8_t m_ExtendedBuffer[MAX_EXTENDED_BUFFER_SIZE];
|
||||||
};
|
};
|
||||||
|
@ -50,7 +50,7 @@ namespace data
|
|||||||
void LeaseSet::ReadFromBuffer (bool readIdentity, bool verifySignature)
|
void LeaseSet::ReadFromBuffer (bool readIdentity, bool verifySignature)
|
||||||
{
|
{
|
||||||
if (readIdentity || !m_Identity)
|
if (readIdentity || !m_Identity)
|
||||||
m_Identity = std::make_shared<IdentityEx>(m_Buffer, m_BufferLen);
|
m_Identity = netdb.NewIdentity (m_Buffer, m_BufferLen);
|
||||||
size_t size = m_Identity->GetFullLen ();
|
size_t size = m_Identity->GetFullLen ();
|
||||||
if (size + 256 > m_BufferLen)
|
if (size + 256 > m_BufferLen)
|
||||||
{
|
{
|
||||||
@ -317,7 +317,7 @@ namespace data
|
|||||||
std::shared_ptr<const IdentityEx> identity;
|
std::shared_ptr<const IdentityEx> identity;
|
||||||
if (readIdentity || !GetIdentity ())
|
if (readIdentity || !GetIdentity ())
|
||||||
{
|
{
|
||||||
identity = std::make_shared<IdentityEx>(buf, len);
|
identity = netdb.NewIdentity (buf, len);
|
||||||
SetIdentity (identity);
|
SetIdentity (identity);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
@ -662,6 +662,7 @@ namespace data
|
|||||||
m_RouterInfoBuffersPool.CleanUpMt ();
|
m_RouterInfoBuffersPool.CleanUpMt ();
|
||||||
m_RouterInfoAddressesPool.CleanUpMt ();
|
m_RouterInfoAddressesPool.CleanUpMt ();
|
||||||
m_RouterInfoAddressVectorsPool.CleanUpMt ();
|
m_RouterInfoAddressVectorsPool.CleanUpMt ();
|
||||||
|
m_IdentitiesPool.CleanUpMt ();
|
||||||
|
|
||||||
if (updatedCount > 0)
|
if (updatedCount > 0)
|
||||||
LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers");
|
LogPrint (eLogInfo, "NetDb: Saved ", updatedCount, " new/updated routers");
|
||||||
|
@ -127,6 +127,7 @@ namespace data
|
|||||||
&m_RouterInfoAddressVectorsPool, std::placeholders::_1));
|
&m_RouterInfoAddressVectorsPool, std::placeholders::_1));
|
||||||
};
|
};
|
||||||
std::shared_ptr<Lease> NewLease (const Lease& lease) { return m_LeasesPool.AcquireSharedMt (lease); };
|
std::shared_ptr<Lease> NewLease (const Lease& lease) { return m_LeasesPool.AcquireSharedMt (lease); };
|
||||||
|
std::shared_ptr<IdentityEx> NewIdentity (const uint8_t * buf, size_t len) { return m_IdentitiesPool.AcquireSharedMt (buf, len); };
|
||||||
|
|
||||||
uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; };
|
uint32_t GetPublishReplyToken () const { return m_PublishReplyToken; };
|
||||||
|
|
||||||
@ -182,6 +183,7 @@ namespace data
|
|||||||
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
|
i2p::util::MemoryPoolMt<RouterInfo::Address> m_RouterInfoAddressesPool;
|
||||||
i2p::util::MemoryPoolMt<RouterInfo::Addresses> m_RouterInfoAddressVectorsPool;
|
i2p::util::MemoryPoolMt<RouterInfo::Addresses> m_RouterInfoAddressVectorsPool;
|
||||||
i2p::util::MemoryPoolMt<Lease> m_LeasesPool;
|
i2p::util::MemoryPoolMt<Lease> m_LeasesPool;
|
||||||
|
i2p::util::MemoryPoolMt<IdentityEx> m_IdentitiesPool;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern NetDb netdb;
|
extern NetDb netdb;
|
||||||
|
@ -161,7 +161,7 @@ namespace data
|
|||||||
m_IsUnreachable = true;
|
m_IsUnreachable = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_RouterIdentity = std::make_shared<IdentityEx>(m_Buffer->data (), m_BufferLen);
|
m_RouterIdentity = NewIdentity (m_Buffer->data (), m_BufferLen);
|
||||||
size_t identityLen = m_RouterIdentity->GetFullLen ();
|
size_t identityLen = m_RouterIdentity->GetFullLen ();
|
||||||
if (identityLen >= m_BufferLen)
|
if (identityLen >= m_BufferLen)
|
||||||
{
|
{
|
||||||
@ -186,7 +186,6 @@ namespace data
|
|||||||
m_IsUnreachable = true;
|
m_IsUnreachable = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
m_RouterIdentity->DropVerifier ();
|
|
||||||
}
|
}
|
||||||
// parse RI
|
// parse RI
|
||||||
std::stringstream str;
|
std::stringstream str;
|
||||||
@ -1061,6 +1060,11 @@ namespace data
|
|||||||
return netdb.NewRouterInfoAddresses ();
|
return netdb.NewRouterInfoAddresses ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<IdentityEx> RouterInfo::NewIdentity (const uint8_t * buf, size_t len) const
|
||||||
|
{
|
||||||
|
return netdb.NewIdentity (buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
void RouterInfo::RefreshTimestamp ()
|
void RouterInfo::RefreshTimestamp ()
|
||||||
{
|
{
|
||||||
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
|
m_Timestamp = i2p::util::GetMillisecondsSinceEpoch ();
|
||||||
@ -1393,6 +1397,11 @@ namespace data
|
|||||||
return boost::make_shared<Addresses> ();
|
return boost::make_shared<Addresses> ();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<IdentityEx> LocalRouterInfo::NewIdentity (const uint8_t * buf, size_t len) const
|
||||||
|
{
|
||||||
|
return std::make_shared<IdentityEx> (buf, len);
|
||||||
|
}
|
||||||
|
|
||||||
bool LocalRouterInfo::AddSSU2Introducer (const Introducer& introducer, bool v4)
|
bool LocalRouterInfo::AddSSU2Introducer (const Introducer& introducer, bool v4)
|
||||||
{
|
{
|
||||||
auto addresses = GetAddresses ();
|
auto addresses = GetAddresses ();
|
||||||
|
@ -307,6 +307,7 @@ namespace data
|
|||||||
virtual std::shared_ptr<Buffer> NewBuffer () const;
|
virtual std::shared_ptr<Buffer> NewBuffer () const;
|
||||||
virtual std::shared_ptr<Address> NewAddress () const;
|
virtual std::shared_ptr<Address> NewAddress () const;
|
||||||
virtual boost::shared_ptr<Addresses> NewAddresses () const;
|
virtual boost::shared_ptr<Addresses> NewAddresses () const;
|
||||||
|
virtual std::shared_ptr<IdentityEx> NewIdentity (const uint8_t * buf, size_t len) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
@ -350,6 +351,7 @@ namespace data
|
|||||||
std::shared_ptr<Buffer> NewBuffer () const override;
|
std::shared_ptr<Buffer> NewBuffer () const override;
|
||||||
std::shared_ptr<Address> NewAddress () const override;
|
std::shared_ptr<Address> NewAddress () const override;
|
||||||
boost::shared_ptr<Addresses> NewAddresses () const override;
|
boost::shared_ptr<Addresses> NewAddresses () const override;
|
||||||
|
std::shared_ptr<IdentityEx> NewIdentity (const uint8_t * buf, size_t len) const override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user