i2pd/libi2pd/SSU2.cpp

274 lines
9.8 KiB
C++
Raw Normal View History

2022-02-04 23:01:18 +03:00
/*
* Copyright (c) 2022, The PurpleI2P Project
2022-02-04 23:01:18 +03:00
*
* This file is part of Purple i2pd project and licensed under BSD3
*
* See full license text in LICENSE file at top of project tree
*/
2022-02-12 03:21:04 +03:00
#include <string.h>
#include <openssl/rand.h>
2022-03-01 05:46:00 +03:00
#include "Log.h"
2022-02-28 04:15:14 +03:00
#include "RouterContext.h"
2022-02-05 23:58:39 +03:00
#include "Transports.h"
2022-02-04 23:01:18 +03:00
#include "SSU2.h"
namespace i2p
{
namespace transport
{
2022-03-05 05:51:40 +03:00
static uint64_t CreateHeaderMask (const uint8_t * kh, const uint8_t * nonce)
{
uint64_t data = 0;
i2p::crypto::ChaCha20 ((uint8_t *)&data, 8, kh, nonce, (uint8_t *)&data);
return data;
}
2022-02-28 04:15:14 +03:00
SSU2Session::SSU2Session (SSU2Server& server, std::shared_ptr<const i2p::data::RouterInfo> in_RemoteRouter,
2022-02-04 23:01:18 +03:00
std::shared_ptr<const i2p::data::RouterInfo::Address> addr, bool peerTest):
2022-02-10 22:03:09 +03:00
TransportSession (in_RemoteRouter, SSU2_TERMINATION_TIMEOUT),
2022-03-01 05:46:00 +03:00
m_Server (server), m_Address (addr), m_DestConnID (0), m_SourceConnID (0)
2022-02-04 23:01:18 +03:00
{
2022-02-05 23:58:39 +03:00
m_NoiseState.reset (new i2p::crypto::NoiseSymmetricState);
2022-03-09 05:33:21 +03:00
if (in_RemoteRouter && m_Address)
2022-02-05 23:58:39 +03:00
{
// outgoing
2022-03-09 05:33:21 +03:00
InitNoiseXKState1 (*m_NoiseState, m_Address->s);
m_RemoteEndpoint = boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port);
2022-02-05 23:58:39 +03:00
}
2022-03-06 02:39:27 +03:00
else
{
// incoming
InitNoiseXKState1 (*m_NoiseState, i2p::context.GetSSU2StaticPublicKey ());
}
2022-02-04 23:01:18 +03:00
}
SSU2Session::~SSU2Session ()
{
}
2022-02-10 22:03:09 +03:00
void SSU2Session::SendSessionRequest ()
{
2022-03-09 05:33:21 +03:00
// we are Alice
2022-02-10 22:03:09 +03:00
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
2022-03-05 05:51:40 +03:00
2022-02-12 03:21:04 +03:00
Header header;
2022-03-01 05:46:00 +03:00
uint8_t headerX[48], payload[1200]; // TODO: correct payload size
2022-02-12 03:21:04 +03:00
size_t payloadSize = 8;
2022-02-10 22:03:09 +03:00
// fill packet
2022-03-01 05:46:00 +03:00
RAND_bytes ((uint8_t *)&m_DestConnID, 8);
header.h.connID = m_DestConnID; // dest id
2022-03-09 05:33:21 +03:00
memset (header.h.packetNum, 0, 4);
header.h.type = eSSU2SessionRequest;
header.h.flags[0] = 2; // ver
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
header.h.flags[2] = 0; // flag
2022-03-01 05:46:00 +03:00
RAND_bytes ((uint8_t *)&m_SourceConnID, 8);
memcpy (headerX, &m_SourceConnID, 8); // source id
2022-03-08 02:20:06 +03:00
RAND_bytes (headerX + 8, 8); // token
2022-03-01 05:46:00 +03:00
memcpy (headerX + 16, m_EphemeralKeys->GetPublicKey (), 32); // X
m_Server.AddPendingOutgoingSession (boost::asio::ip::udp::endpoint (m_Address->host, m_Address->port), shared_from_this ());
2022-03-05 05:51:40 +03:00
// KDF for session request
2022-03-08 02:20:06 +03:00
m_NoiseState->MixHash ({ {header.buf, 16}, {headerX, 16} }); // h = SHA256(h || header)
2022-03-05 05:51:40 +03:00
m_NoiseState->MixHash (m_EphemeralKeys->GetPublicKey (), 32); // h = SHA256(h || aepk);
uint8_t sharedSecret[32];
m_EphemeralKeys->Agree (m_Address->s, sharedSecret);
m_NoiseState->MixKey (sharedSecret);
2022-02-10 22:03:09 +03:00
// encrypt
2022-03-06 02:39:27 +03:00
const uint8_t nonce[12] = {0};
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
2022-03-09 05:33:21 +03:00
payloadSize += 16;
2022-03-05 05:51:40 +03:00
header.ll[0] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 24));
header.ll[1] ^= CreateHeaderMask (m_Address->i, payload + (payloadSize - 12));
i2p::crypto::ChaCha20 (headerX, 48, m_Address->i, nonce, headerX);
2022-03-08 02:20:06 +03:00
m_NoiseState->MixHash (payload, 32); // h = SHA256(h || 32 byte encrypted payload from Session Request) for SessionCreated
2022-03-05 05:51:40 +03:00
// send
2022-03-09 05:33:21 +03:00
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
2022-02-10 22:03:09 +03:00
}
2022-03-06 02:39:27 +03:00
void SSU2Session::ProcessSessionRequest (uint64_t connID, uint8_t * buf, size_t len)
{
// we are Bob
m_SourceConnID = connID;
2022-03-08 02:20:06 +03:00
Header header;
header.h.connID = connID;
2022-03-09 05:33:21 +03:00
memcpy (header.buf + 8, buf + 8, 8);
header.ll[1] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 12));
if (header.h.type != eSSU2SessionRequest)
2022-03-06 02:39:27 +03:00
{
2022-03-09 05:33:21 +03:00
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
2022-03-06 02:39:27 +03:00
return;
}
const uint8_t nonce[12] = {0};
uint8_t headerX[48];
i2p::crypto::ChaCha20 (buf + 16, 48, i2p::context.GetSSU2IntroKey (), nonce, headerX);
memcpy (&m_DestConnID, headerX, 8);
// KDF for session request
2022-03-08 02:20:06 +03:00
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
2022-03-06 02:39:27 +03:00
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || aepk);
uint8_t sharedSecret[32];
i2p::context.GetSSU2StaticKeys ().Agree (headerX + 16, sharedSecret);
m_NoiseState->MixKey (sharedSecret);
// decrypt
uint8_t * payload = buf + 64;
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, len - 80, false))
{
LogPrint (eLogWarning, "SSU2: SessionRequest AEAD verification failed ");
return;
}
// process payload
m_Server.AddSession (m_SourceConnID, shared_from_this ());
2022-03-09 05:33:21 +03:00
SendSessionCreated (headerX + 16);
}
void SSU2Session::SendSessionCreated (const uint8_t * X)
{
// we are Bob
m_EphemeralKeys = i2p::transport::transports.GetNextX25519KeysPair ();
// fill packet
Header header;
uint8_t headerX[48], payload[1200]; // TODO: correct payload size
size_t payloadSize = 8;
header.h.connID = m_DestConnID; // dest id
memset (header.h.packetNum, 0, 4);
header.h.type = eSSU2SessionCreated;
header.h.flags[0] = 2; // ver
header.h.flags[1] = (uint8_t)i2p::context.GetNetID (); // netID
header.h.flags[2] = 0; // flag
memcpy (headerX, &m_SourceConnID, 8); // source id
RAND_bytes (headerX + 8, 8); // token
memcpy (headerX + 16, m_EphemeralKeys->GetPublicKey (), 32); // Y
// KDF for SessionCreated
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
uint8_t sharedSecret[32];
m_EphemeralKeys->Agree (X, sharedSecret);
m_NoiseState->MixKey (sharedSecret);
// encrypt
const uint8_t nonce[12] = {0};
i2p::crypto::AEADChaCha20Poly1305 (payload, payloadSize, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, payloadSize + 16, true);
payloadSize += 16;
header.ll[0] ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), payload + (payloadSize - 24));
uint8_t kh2[32];
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32)
header.ll[1] ^= CreateHeaderMask (kh2, payload + (payloadSize - 12));
i2p::crypto::ChaCha20 (headerX, 48, kh2, nonce, headerX);
// send
m_Server.Send (header.buf, 16, headerX, 48, payload, payloadSize, m_RemoteEndpoint);
2022-03-06 02:39:27 +03:00
}
2022-03-05 05:51:40 +03:00
bool SSU2Session::ProcessSessionCreated (uint8_t * buf, size_t len)
2022-02-10 22:03:09 +03:00
{
2022-03-06 02:39:27 +03:00
// we are Alice
2022-03-08 02:20:06 +03:00
Header header;
header.h.connID = m_SourceConnID;
2022-03-09 05:33:21 +03:00
memcpy (header.buf + 8, buf + 8, 8);
2022-03-05 05:51:40 +03:00
uint8_t kh2[32];
i2p::crypto::HKDF (m_NoiseState->m_CK, nullptr, 0, "SessCreateHeader", kh2, 32); // k_header_2 = HKDF(chainKey, ZEROLEN, "SessCreateHeader", 32)
2022-03-09 05:33:21 +03:00
header.ll[1] ^= CreateHeaderMask (kh2, buf + (len - 12));
if (header.h.type != eSSU2SessionCreated)
2022-03-08 02:20:06 +03:00
{
2022-03-09 05:33:21 +03:00
LogPrint (eLogWarning, "SSU2: Unexpected message type ", (int)header.h.type);
2022-03-08 02:20:06 +03:00
return false;
}
const uint8_t nonce[12] = {0};
uint8_t headerX[48];
i2p::crypto::ChaCha20 (buf + 16, 48, kh2, nonce, headerX);
// KDF for SessionCreated
m_NoiseState->MixHash ( { {header.buf, 16}, {headerX, 16} } ); // h = SHA256(h || header)
m_NoiseState->MixHash (headerX + 16, 32); // h = SHA256(h || bepk);
uint8_t sharedSecret[32];
m_EphemeralKeys->Agree (headerX + 16, sharedSecret);
m_NoiseState->MixKey (sharedSecret);
// decrypt
uint8_t * payload = buf + 64;
if (!i2p::crypto::AEADChaCha20Poly1305 (payload, len - 80, m_NoiseState->m_H, 32, m_NoiseState->m_CK + 32, nonce, payload, len - 80, false))
2022-03-05 05:51:40 +03:00
{
2022-03-08 02:20:06 +03:00
LogPrint (eLogWarning, "SSU2: SessionCreated AEAD verification failed ");
2022-03-05 05:51:40 +03:00
return false;
}
2022-03-08 02:20:06 +03:00
// process payload
m_Server.AddSession (m_SourceConnID, shared_from_this ());
2022-03-05 05:51:40 +03:00
return true;
2022-02-10 22:03:09 +03:00
}
2022-02-28 04:15:14 +03:00
2022-03-01 05:46:00 +03:00
SSU2Server::SSU2Server (int port):
m_Socket (m_Service), m_Endpoint (boost::asio::ip::udp::v6 (), port)
{
}
void SSU2Server::OpenSocket ()
{
try
{
m_Socket.open (boost::asio::ip::udp::v6());
m_Socket.set_option (boost::asio::socket_base::receive_buffer_size (SSU2_SOCKET_RECEIVE_BUFFER_SIZE));
m_Socket.set_option (boost::asio::socket_base::send_buffer_size (SSU2_SOCKET_SEND_BUFFER_SIZE));
m_Socket.bind (m_Endpoint);
LogPrint (eLogInfo, "SSU2: Start listening port ", m_Endpoint.port());
}
catch (std::exception& ex )
{
LogPrint (eLogError, "SSU2: Failed to bind to port ", m_Endpoint.port(), ": ", ex.what());
ThrowFatal ("Unable to start SSU2 transport at port ", m_Endpoint.port(), ": ", ex.what ());
}
}
2022-02-28 04:15:14 +03:00
void SSU2Server::AddSession (uint64_t connID, std::shared_ptr<SSU2Session> session)
{
m_Sessions.emplace (connID, session);
}
2022-03-01 05:46:00 +03:00
void SSU2Server::AddPendingOutgoingSession (const boost::asio::ip::udp::endpoint& ep, std::shared_ptr<SSU2Session> session)
{
m_PendingOutgoingSessions.emplace (ep, session);
}
void SSU2Server::ProcessNextPacket (uint8_t * buf, size_t len, const boost::asio::ip::udp::endpoint& senderEndpoint)
2022-02-28 04:15:14 +03:00
{
2022-03-05 05:51:40 +03:00
uint64_t connID;
2022-02-28 04:15:14 +03:00
memcpy (&connID, buf, 8);
2022-03-05 05:51:40 +03:00
connID ^= CreateHeaderMask (i2p::context.GetSSU2IntroKey (), buf + (len - 24));
2022-02-28 04:15:14 +03:00
auto it = m_Sessions.find (connID);
if (it != m_Sessions.end ())
{
}
2022-03-01 05:46:00 +03:00
else
{
// check pending sessions if it's SessionCreated
auto it1 = m_PendingOutgoingSessions.find (senderEndpoint);
if (it1 != m_PendingOutgoingSessions.end ())
{
2022-03-05 05:51:40 +03:00
if (it1->second->ProcessSessionCreated (buf, len))
m_PendingOutgoingSessions.erase (it1);
2022-03-01 05:46:00 +03:00
}
else
{
// assume new incoming session
2022-03-06 02:39:27 +03:00
auto session = std::make_shared<SSU2Session> (*this);
2022-03-09 05:33:21 +03:00
session->SetRemoteEndpoint (senderEndpoint);
2022-03-06 02:39:27 +03:00
session->ProcessSessionRequest (connID, buf, len);
2022-03-01 05:46:00 +03:00
}
}
2022-02-28 04:15:14 +03:00
}
2022-03-01 05:46:00 +03:00
void SSU2Server::Send (const uint8_t * header, size_t headerLen, const uint8_t * headerX, size_t headerXLen,
const uint8_t * payload, size_t payloadLen, const boost::asio::ip::udp::endpoint& to)
{
std::vector<boost::asio::const_buffer> bufs
{
boost::asio::buffer (header, headerLen),
boost::asio::buffer (headerX, headerXLen),
boost::asio::buffer (payload, payloadLen)
};
boost::system::error_code ec;
m_Socket.send_to (bufs, to, 0, ec);
}
2022-02-04 23:01:18 +03:00
}
}