From d3062d29942a82cccc94905ad49464f7f2def965 Mon Sep 17 00:00:00 2001 From: orignal Date: Thu, 25 Jul 2024 20:36:46 -0400 Subject: [PATCH] don't create full identity from buffer if only ident hash is needed --- libi2pd/Identity.cpp | 8 ++++++++ libi2pd/Identity.h | 2 ++ libi2pd_client/I2CP.cpp | 11 ++++++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/libi2pd/Identity.cpp b/libi2pd/Identity.cpp index 4168abb7..2b8b454e 100644 --- a/libi2pd/Identity.cpp +++ b/libi2pd/Identity.cpp @@ -420,6 +420,14 @@ namespace data return CreateEncryptor (GetCryptoKeyType (), key); } + size_t GetIdentityBufferLen (const uint8_t * buf, size_t len) + { + if (len < DEFAULT_IDENTITY_SIZE) return 0; + size_t l = DEFAULT_IDENTITY_SIZE + bufbe16toh (buf + DEFAULT_IDENTITY_SIZE - 2); + if (l > len) return 0; + return l; + } + PrivateKeys& PrivateKeys::operator=(const Keys& keys) { m_Public = std::make_shared(Identity (keys)); diff --git a/libi2pd/Identity.h b/libi2pd/Identity.h index 9ff4b4ae..5edd4545 100644 --- a/libi2pd/Identity.h +++ b/libi2pd/Identity.h @@ -136,6 +136,8 @@ namespace data uint8_t m_ExtendedBuffer[MAX_EXTENDED_BUFFER_SIZE]; }; + size_t GetIdentityBufferLen (const uint8_t * buf, size_t len); // return actual identity length in buffer + class PrivateKeys // for eepsites { public: diff --git a/libi2pd_client/I2CP.cpp b/libi2pd_client/I2CP.cpp index 8f677203..4ed56f05 100644 --- a/libi2pd_client/I2CP.cpp +++ b/libi2pd_client/I2CP.cpp @@ -777,11 +777,12 @@ namespace client size_t offset = 2; if (m_Destination) { - i2p::data::IdentityEx identity; - size_t identsize = identity.FromBuffer (buf + offset, len - offset); - if (identsize) + size_t identSize = i2p::data::GetIdentityBufferLen (buf + offset, len - offset); + if (identSize) { - offset += identsize; + i2p::data::IdentHash identHash; + SHA256(buf + offset, identSize, identHash); // caclulate ident hash, because we don't need full identity + offset += identSize; uint32_t payloadLen = bufbe32toh (buf + offset); if (payloadLen + offset <= len) { @@ -791,7 +792,7 @@ namespace client { if (m_IsSendAccepted) SendMessageStatusMessage (nonce, eI2CPMessageStatusAccepted); // accepted - m_Destination->SendMsgTo (buf + offset, payloadLen, identity.GetIdentHash (), nonce); + m_Destination->SendMsgTo (buf + offset, payloadLen, identHash, nonce); } else SendMessageStatusMessage (nonce, eI2CPMessageStatusNoLocalTunnels);