diff --git a/Identity.cpp b/Identity.cpp index 23bc63a1..cce9d098 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -61,9 +61,6 @@ namespace data memcpy (&m_StandardIdentity, &other.m_StandardIdentity, DEFAULT_IDENTITY_SIZE); m_IdentHash = other.m_IdentHash; - delete m_Verifier; - m_Verifier = nullptr; - delete[] m_ExtendedBuffer; m_ExtendedLen = other.m_ExtendedLen; if (m_ExtendedLen > 0) @@ -73,7 +70,10 @@ namespace data } else m_ExtendedBuffer = nullptr; - + + delete m_Verifier; + CreateVerifier (); + return *this; } @@ -82,22 +82,21 @@ namespace data m_StandardIdentity = standard; m_IdentHash = m_StandardIdentity.Hash (); - delete m_Verifier; - m_Verifier = nullptr; delete[] m_ExtendedBuffer; m_ExtendedBuffer = nullptr; m_ExtendedLen = 0; + + delete m_Verifier; + CreateVerifier (); return *this; } size_t IdentityEx::FromBuffer (const uint8_t * buf, size_t len) { - delete m_Verifier; - m_Verifier = nullptr; - delete[] m_ExtendedBuffer; - memcpy (&m_StandardIdentity, buf, DEFAULT_IDENTITY_SIZE); + + delete[] m_ExtendedBuffer; if (m_StandardIdentity.certificate.length) { m_ExtendedLen = be16toh (m_StandardIdentity.certificate.length); @@ -110,6 +109,10 @@ namespace data m_ExtendedBuffer = nullptr; } CryptoPP::SHA256().CalculateDigest(m_IdentHash, buf, GetFullLen ()); + + delete m_Verifier; + CreateVerifier (); + return GetFullLen (); } @@ -123,8 +126,6 @@ namespace data size_t IdentityEx::GetSigningPublicKeyLen () const { - if (!m_Verifier) - CreateVerifier (); if (m_Verifier) return m_Verifier->GetPublicKeyLen (); return 128; @@ -132,22 +133,18 @@ namespace data size_t IdentityEx::GetSignatureLen () const { - if (!m_Verifier) - CreateVerifier (); if (m_Verifier) return m_Verifier->GetSignatureLen (); return 40; } - bool IdentityEx::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) + bool IdentityEx::Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { - if (!m_Verifier) - CreateVerifier (); if (m_Verifier) return m_Verifier->Verify (buf, len, signature); return false; } - void IdentityEx::CreateVerifier () const + void IdentityEx::CreateVerifier () { switch (m_StandardIdentity.certificate.type) { diff --git a/Identity.h b/Identity.h index a864a3a8..959239cd 100644 --- a/Identity.h +++ b/Identity.h @@ -118,17 +118,17 @@ namespace data size_t GetFullLen () const { return m_ExtendedLen + DEFAULT_IDENTITY_SIZE; }; size_t GetSigningPublicKeyLen () const; size_t GetSignatureLen () const; - bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature); + bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const; private: - void CreateVerifier () const; + void CreateVerifier (); private: Identity m_StandardIdentity; IdentHash m_IdentHash; - mutable i2p::crypto::Verifier * m_Verifier; + i2p::crypto::Verifier * m_Verifier; size_t m_ExtendedLen; uint8_t * m_ExtendedBuffer; }; diff --git a/LeaseSet.cpp b/LeaseSet.cpp index f3bf63f9..26211d13 100644 --- a/LeaseSet.cpp +++ b/LeaseSet.cpp @@ -46,9 +46,8 @@ namespace data m_BufferLen += sizeof (Lease); } // signature - // TODO: signer localDestination.Sign (m_Buffer, m_BufferLen, m_Buffer + m_BufferLen); - m_BufferLen += 40; // TODO: + m_BufferLen += localDestination.GetIdentity ().GetSignatureLen (); LogPrint ("Local LeaseSet of ", tunnels.size (), " leases created"); ReadFromBuffer (); diff --git a/Signature.h b/Signature.h index d83329e7..3bd99744 100644 --- a/Signature.h +++ b/Signature.h @@ -18,7 +18,7 @@ namespace crypto public: virtual ~Verifier () {}; - virtual bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) = 0; + virtual bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const = 0; virtual size_t GetPublicKeyLen () const = 0; virtual size_t GetSignatureLen () const = 0; }; @@ -28,7 +28,7 @@ namespace crypto public: virtual ~Signer () {}; - virtual void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) = 0; + virtual void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) const = 0; }; class DSAVerifier: public Verifier @@ -40,7 +40,7 @@ namespace crypto m_PublicKey.Initialize (dsap, dsaq, dsag, CryptoPP::Integer (signingKey, 128)); } - bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) + bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { CryptoPP::DSA::Verifier verifier (m_PublicKey); return verifier.VerifyMessage (buf, len, signature, 40); @@ -63,7 +63,7 @@ namespace crypto m_PrivateKey.Initialize (dsap, dsaq, dsag, CryptoPP::Integer (signingPrivateKey, 20)); } - void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) + void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) const { CryptoPP::DSA::Signer signer (m_PrivateKey); signer.SignMessage (rnd, buf, len, signature); @@ -95,7 +95,7 @@ namespace crypto CryptoPP::Integer (signingKey + 32, 32))); } - bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) + bool Verify (const uint8_t * buf, size_t len, const uint8_t * signature) const { CryptoPP::ECDSA::Verifier verifier (m_PublicKey); return verifier.VerifyMessage (buf, len, signature, 64); @@ -118,7 +118,7 @@ namespace crypto m_PrivateKey.Initialize (CryptoPP::ASN1::secp256r1(), CryptoPP::Integer (signingPrivateKey, 32)); } - void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) + void Sign (CryptoPP::RandomNumberGenerator& rnd, const uint8_t * buf, int len, uint8_t * signature) const { CryptoPP::ECDSA::Signer signer (m_PrivateKey); signer.SignMessage (rnd, buf, len, signature);