diff --git a/Identity.cpp b/Identity.cpp index cce9d098..b473c5eb 100644 --- a/Identity.cpp +++ b/Identity.cpp @@ -186,12 +186,34 @@ namespace data PrivateKeys& PrivateKeys::operator=(const Keys& keys) { - pub = keys; - memcpy (privateKey, keys.privateKey, 276); // 256 + 20 + m_Public = Identity (keys); + memcpy (m_PrivateKey, keys.privateKey, 256); // 256 + memcpy (m_SigningPrivateKey, keys.signingPrivateKey, 20); // 20 - DSA return *this; } - + size_t PrivateKeys::FromBuffer (const uint8_t * buf, size_t len) + { + size_t ret = m_Public.FromBuffer (buf, len); + memcpy (m_PrivateKey, buf + ret, 256); // private key always 256 + ret += 256; + size_t signingPrivateKeySize = m_Public.GetSignatureLen ()/2; // 20 for DSA + memcpy (m_SigningPrivateKey, buf + ret, signingPrivateKeySize); + ret += signingPrivateKeySize; + return ret; + } + + size_t PrivateKeys::ToBuffer (uint8_t * buf, size_t len) const + { + size_t ret = m_Public.ToBuffer (buf, len); + memcpy (buf + ret, m_PrivateKey, 256); // private key always 256 + ret += 256; + size_t signingPrivateKeySize = m_Public.GetSignatureLen ()/2; // 20 for DSA + memcpy (buf + ret, m_SigningPrivateKey, signingPrivateKeySize); + ret += signingPrivateKeySize; + return ret; + } + Keys CreateRandomKeys () { Keys keys; diff --git a/Identity.h b/Identity.h index 959239cd..a2df2ada 100644 --- a/Identity.h +++ b/Identity.h @@ -93,6 +93,8 @@ namespace data uint16_t length; } certificate; + Identity () = default; + Identity (const Keys& keys) { *this = keys; }; Identity& operator=(const Keys& keys); bool FromBase64(const std::string& ); size_t FromBuffer (const uint8_t * buf, size_t len); @@ -133,17 +135,27 @@ namespace data uint8_t * m_ExtendedBuffer; }; - struct PrivateKeys // for eepsites + class PrivateKeys // for eepsites { - Identity pub; - uint8_t privateKey[256]; - uint8_t signingPrivateKey[20]; + public: + + PrivateKeys () = default; + PrivateKeys (const PrivateKeys& ) = default; + PrivateKeys (const Keys& keys) { *this = keys; }; + PrivateKeys& operator=(const Keys& keys); - PrivateKeys () = default; - PrivateKeys (const PrivateKeys& ) = default; - PrivateKeys (const Keys& keys) { *this = keys; }; - - PrivateKeys& operator=(const Keys& keys); + const IdentityEx& GetPublic () const { return m_Public; }; + const uint8_t * GetPrivateKey () const { return m_PrivateKey; }; + const uint8_t * GetSigningPrivateKey () const { return m_SigningPrivateKey; }; + + size_t FromBuffer (const uint8_t * buf, size_t len); + size_t ToBuffer (uint8_t * buf, size_t len) const; + + private: + + IdentityEx m_Public; + uint8_t m_PrivateKey[256]; + uint8_t m_SigningPrivateKey[128]; // assume private key doesn't exceed 128 bytes }; #pragma pack() diff --git a/Streaming.cpp b/Streaming.cpp index d81f6511..20eb8663 100644 --- a/Streaming.cpp +++ b/Streaming.cpp @@ -506,10 +506,8 @@ namespace stream m_Service (service), m_LeaseSet (nullptr), m_IsPublic (false) { m_Keys = i2p::data::CreateRandomKeys (); - - m_Identity = m_Keys.pub; m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, - CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); + CryptoPP::Integer (m_Keys.GetSigningPrivateKey (), 20)); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel @@ -520,13 +518,20 @@ namespace stream { std::ifstream s(fullPath.c_str (), std::ifstream::binary); if (s.is_open ()) - s.read ((char *)&m_Keys, sizeof (m_Keys)); + { + s.seekg (0, std::ios::end); + size_t len = s.tellg(); + s.seekg (0, std::ios::beg); + uint8_t * buf = new uint8_t[len]; + s.read ((char *)buf, len); + m_Keys.FromBuffer (buf, len); + delete[] buf; + } else LogPrint ("Can't open file ", fullPath); - m_Identity = m_Keys.pub; m_SigningPrivateKey.Initialize (i2p::crypto::dsap, i2p::crypto::dsaq, i2p::crypto::dsag, - CryptoPP::Integer (m_Keys.signingPrivateKey, 20)); + CryptoPP::Integer (m_Keys.GetSigningPrivateKey (), 20)); CryptoPP::DH dh (i2p::crypto::elgp, i2p::crypto::elgg); dh.GenerateKeyPair(i2p::context.GetRandomNumberGenerator (), m_EncryptionPrivateKey, m_EncryptionPublicKey); m_Pool = i2p::tunnel::tunnels.CreateTunnelPool (*this, 3); // 3-hops tunnel diff --git a/Streaming.h b/Streaming.h index 644d9243..bd59c420 100644 --- a/Streaming.h +++ b/Streaming.h @@ -153,7 +153,7 @@ namespace stream void HandleNextPacket (Packet * packet); // implements LocalDestination - const i2p::data::IdentityEx& GetIdentity () const { return m_Identity; }; + const i2p::data::IdentityEx& GetIdentity () const { return m_Keys.GetPublic (); }; const uint8_t * GetEncryptionPrivateKey () const { return m_EncryptionPrivateKey; }; const uint8_t * GetEncryptionPublicKey () const { return m_EncryptionPublicKey; }; void Sign (const uint8_t * buf, int len, uint8_t * signature) const; @@ -169,7 +169,6 @@ namespace stream boost::asio::io_service& m_Service; std::map m_Streams; i2p::data::PrivateKeys m_Keys; - i2p::data::IdentityEx m_Identity; uint8_t m_EncryptionPublicKey[256], m_EncryptionPrivateKey[256]; i2p::tunnel::TunnelPool * m_Pool; diff --git a/version.h b/version.h index 68797867..c99233cc 100644 --- a/version.h +++ b/version.h @@ -5,7 +5,7 @@ #define CODENAME "Purple" -#define I2P_VERSION "0.9.11" +#define I2P_VERSION "0.9.14" /* TEMPORARY STORE FOR IMAGE CODES */