From 4211c733a21f2820cfa6358ed8d1f4fdf251936a Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 5 Feb 2022 17:14:25 -0500 Subject: [PATCH] s and i keys for all addresses --- libi2pd/NTCP2.cpp | 4 ++-- libi2pd/RouterContext.cpp | 2 +- libi2pd/RouterInfo.cpp | 18 +++++++----------- libi2pd/RouterInfo.h | 10 ++-------- libi2pd/SSU2.cpp | 3 +-- libi2pd/Tag.h | 18 +++++------------- 6 files changed, 18 insertions(+), 37 deletions(-) diff --git a/libi2pd/NTCP2.cpp b/libi2pd/NTCP2.cpp index 50be749b..252a85b7 100644 --- a/libi2pd/NTCP2.cpp +++ b/libi2pd/NTCP2.cpp @@ -339,8 +339,8 @@ namespace transport m_Establisher->m_RemoteIdentHash = GetRemoteIdentity ()->GetIdentHash (); if (addr) { - memcpy (m_Establisher->m_RemoteStaticKey, addr->ntcp2->staticKey, 32); - memcpy (m_Establisher->m_IV, addr->ntcp2->iv, 16); + memcpy (m_Establisher->m_RemoteStaticKey, addr->s, 32); + memcpy (m_Establisher->m_IV, addr->i, 16); m_RemoteEndpoint = boost::asio::ip::tcp::endpoint (addr->host, addr->port); } else diff --git a/libi2pd/RouterContext.cpp b/libi2pd/RouterContext.cpp index 14067739..e442df01 100644 --- a/libi2pd/RouterContext.cpp +++ b/libi2pd/RouterContext.cpp @@ -265,7 +265,7 @@ namespace i2p } if (port) address->port = port; address->published = publish; - address->ntcp2->iv = m_NTCP2Keys->iv; + memcpy (address->i, m_NTCP2Keys->iv, 16); updated = true; } } diff --git a/libi2pd/RouterInfo.cpp b/libi2pd/RouterInfo.cpp index ed6439df..706186d4 100644 --- a/libi2pd/RouterInfo.cpp +++ b/libi2pd/RouterInfo.cpp @@ -220,10 +220,7 @@ namespace data char transportStyle[6]; ReadString (transportStyle, 6, s); if (!strncmp (transportStyle, "NTCP", 4)) // NTCP or NTCP2 - { address->transportStyle = eTransportNTCP; - address->ntcp2.reset (new NTCP2Ext ()); - } else if (!strcmp (transportStyle, "SSU")) { address->transportStyle = eTransportSSU; @@ -271,12 +268,12 @@ namespace data address->caps = ExtractAddressCaps (value); else if (!strcmp (key, "s")) // ntcp2 static key { - Base64ToByteStream (value, strlen (value), address->ntcp2->staticKey, 32); + Base64ToByteStream (value, strlen (value), address->s, 32); isStaticKey = true; } else if (!strcmp (key, "i")) // ntcp2 iv { - Base64ToByteStream (value, strlen (value), address->ntcp2->iv, 16); + Base64ToByteStream (value, strlen (value), address->i, 16); address->published = true; // presence if "i" means "published" } else if (key[0] == 'i') @@ -614,10 +611,9 @@ namespace data addr->transportStyle = eTransportNTCP; addr->caps = caps; addr->date = 0; - addr->ntcp2.reset (new NTCP2Ext ()); if (port) addr->published = true; - memcpy (addr->ntcp2->staticKey, staticKey, 32); - memcpy (addr->ntcp2->iv, iv, 16); + memcpy (addr->s, staticKey, 32); + memcpy (addr->i, iv, 16); if (addr->IsV4 ()) { m_SupportedTransports |= eNTCP2V4; @@ -845,7 +841,7 @@ namespace data return GetAddress ( [key](std::shared_ptr address)->bool { - return address->IsNTCP2 () && !memcmp (address->ntcp2->staticKey, key, 32); + return address->IsNTCP2 () && !memcmp (address->s, key, 32); }); } @@ -1197,7 +1193,7 @@ namespace data { // publish i for NTCP2 WriteString ("i", properties); properties << '='; - WriteString (address.ntcp2->iv.ToBase64 (), properties); properties << ';'; + WriteString (address.i.ToBase64 (16), properties); properties << ';'; } if (isPublished || address.ssu) @@ -1211,7 +1207,7 @@ namespace data { // publish s and v for NTCP2 WriteString ("s", properties); properties << '='; - WriteString (address.ntcp2->staticKey.ToBase64 (), properties); properties << ';'; + WriteString (address.s.ToBase64 (), properties); properties << ';'; WriteString ("v", properties); properties << '='; WriteString ("2", properties); properties << ';'; } diff --git a/libi2pd/RouterInfo.h b/libi2pd/RouterInfo.h index ff682495..3bb7f614 100644 --- a/libi2pd/RouterInfo.h +++ b/libi2pd/RouterInfo.h @@ -113,22 +113,16 @@ namespace data std::vector introducers; }; - struct NTCP2Ext - { - Tag<32> staticKey; - Tag<16> iv; - }; - struct Address { TransportStyle transportStyle; boost::asio::ip::address host; + Tag<32> s, i; // keys, i is first 16 bytes for NTCP2 int port; uint64_t date; uint8_t caps; bool published = false; std::unique_ptr ssu; // not null for SSU - std::unique_ptr ntcp2; // not null for NTCP2 bool IsCompatible (const boost::asio::ip::address& other) const { @@ -147,7 +141,7 @@ namespace data return !(*this == other); } - bool IsNTCP2 () const { return (bool)ntcp2; }; + bool IsNTCP2 () const { return transportStyle == eTransportNTCP; }; bool IsPublishedNTCP2 () const { return IsNTCP2 () && published; }; bool IsReachableSSU () const { return (bool)ssu && (published || !ssu->introducers.empty ()); }; bool UsesIntroducer () const { return (bool)ssu && !ssu->introducers.empty (); }; diff --git a/libi2pd/SSU2.cpp b/libi2pd/SSU2.cpp index e46b9954..8f5eda33 100644 --- a/libi2pd/SSU2.cpp +++ b/libi2pd/SSU2.cpp @@ -21,8 +21,7 @@ namespace transport if (in_RemoteRouter && addr) { // outgoing - if (addr->ntcp2) // TODO: should be SSU - InitNoiseXKState1 (*m_NoiseState, addr->ntcp2->staticKey); + InitNoiseXKState1 (*m_NoiseState, addr->s); } } diff --git a/libi2pd/Tag.h b/libi2pd/Tag.h index 3856abd9..d898395f 100644 --- a/libi2pd/Tag.h +++ b/libi2pd/Tag.h @@ -1,5 +1,5 @@ /* -* Copyright (c) 2013-2020, The PurpleI2P Project +* Copyright (c) 2013-2022, The PurpleI2P Project * * This file is part of Purple i2pd project and licensed under BSD3 * @@ -9,14 +9,6 @@ #ifndef TAG_H__ #define TAG_H__ -/* -* Copyright (c) 2013-2017, The PurpleI2P Project -* -* This file is part of Purple i2pd project and licensed under BSD3 -* -* See full license text in LICENSE file at top of project tree -*/ - #include #include #include @@ -64,17 +56,17 @@ namespace data { RAND_bytes(m_Buf, sz); } - std::string ToBase64 () const + std::string ToBase64 (size_t len = sz) const { char str[sz*2]; - size_t l = i2p::data::ByteStreamToBase64 (m_Buf, sz, str, sz*2); + size_t l = i2p::data::ByteStreamToBase64 (m_Buf, len, str, sz*2); return std::string (str, str + l); } - std::string ToBase32 () const + std::string ToBase32 (size_t len = sz) const { char str[sz*2]; - size_t l = i2p::data::ByteStreamToBase32 (m_Buf, sz, str, sz*2); + size_t l = i2p::data::ByteStreamToBase32 (m_Buf, len, str, sz*2); return std::string (str, str + l); }