Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Wire Protocol Overhaul, Message Handling Refactor #2204

Merged
merged 32 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a921575
mein gott
dr7ana Aug 29, 2023
11e54f6
More message refactoring
dr7ana Aug 31, 2023
511c20c
Message handling methods
dr7ana Sep 13, 2023
0b6506a
oxen-libquic version bump
dr7ana Sep 13, 2023
81aa044
kitware CI deps
dr7ana Sep 13, 2023
7ce517b
oxen-libquic update; no leak warn deprecated
dr7ana Sep 13, 2023
821cbea
warnings_as_errors fixes
dr7ana Sep 13, 2023
7f8207d
Nuked superfluous interface classes
dr7ana Sep 13, 2023
a4272c4
Added span backport
dr7ana Sep 14, 2023
d1b7eee
Zlib version bump 1.2.13 -> 1.3
dr7ana Sep 14, 2023
fd527d6
Carving through llarp/link
dr7ana Sep 14, 2023
ffb90e8
libquic bump
dr7ana Sep 14, 2023
aaf688c
Deleted a lot
dr7ana Sep 15, 2023
bfa9629
More carving + libquic bump
dr7ana Sep 18, 2023
a3e6cec
Address type migration + libquic bump
dr7ana Sep 19, 2023
d0c3837
libquic bparser merged
dr7ana Sep 21, 2023
ae31909
libquic bump
dr7ana Sep 26, 2023
d9ead7d
crypto and message encoding
dr7ana Sep 27, 2023
ad007ff
libquic/oxen-mq/oxenc version bumps
dr7ana Sep 29, 2023
1a9f977
Message method implementation continued
dr7ana Sep 29, 2023
206bd0b
wawaweewa
dr7ana Oct 3, 2023
f35f7fe
refactor path build message construction, no more async nonsense
tewinget Oct 4, 2023
4ed6a01
following up before Tom meeting
dr7ana Oct 4, 2023
65bd224
Exit endpoints implemented
dr7ana Oct 6, 2023
bd81357
Path message transmission
dr7ana Oct 10, 2023
2cc02d7
handle path build requests, generate responses
tewinget Oct 11, 2023
c8dae87
Path routing partially implementing
dr7ana Oct 11, 2023
5ccec24
callsafe
dr7ana Oct 12, 2023
577f5e6
De-mutexing
dr7ana Oct 12, 2023
a6f901a
RIP everything
dr7ana Oct 12, 2023
6b1e3fb
Touched up path build message handling
dr7ana Oct 16, 2023
e4315cd
More message handling underway
dr7ana Oct 16, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
168 changes: 166 additions & 2 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1062,14 +1062,178 @@ namespace llarp
void
LinkManager::handle_path_build(oxen::quic::message m)
{
if (!router.path_context().AllowingTransit())
{
log::warning("got path build request when not permitting transit");
dr7ana marked this conversation as resolved.
Show resolved Hide resolved
m.respond(serialize_response({{"STATUS", PathBuildMessage::NO_TRANSIT}}), true);
return;
}
try
{
oxenc::bt_dict_consumer btdc{m.body()};
// not using list_consumer here because we need to move the first (our) frame
// to the end and re-send, unless we're the terminal hop. This could be done
// with list_consumer, but it would involve messy mucking around with the
// encoded list. Revisit if optimization issue (shouldn't be).
auto frame_list = oxenc::bt_deserialize<oxenc::bt_list>(m.body());
if (frames.size() != path::MAX_LEN)
dr7ana marked this conversation as resolved.
Show resolved Hide resolved
{
log::info(
link_cat,
"Path build request has invalid number of records, ",
frame_list.size(),
"!=",
path::MAX_LEN);
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_FRAMES}}), true);
return;
}

const auto& hash = frame_list[0].at("hash");
dr7ana marked this conversation as resolved.
Show resolved Hide resolved
auto& frame_str = frame_list[0].at("frame");

oxenc::bt_dict_consumer frame_dict{frame_str};

auto hop_info = frame_dict.require<ustring>("encrypted");
dr7ana marked this conversation as resolved.
Show resolved Hide resolved
TunnelNonce nonce;
nonce.from_string_view(frame_dict.require<std::string_view>("nonce"));
Pubkey otherPubkey;
otherPubkey.from_string_view(frame_dict.require<std::string_view>("pubkey"));
dr7ana marked this conversation as resolved.
Show resolved Hide resolved

auto crypto = CryptoManager::instance();

SharedSecret shared;
// derive shared secret using ephemeral pubkey and our secret key (and nonce)
if (!crypto->dh_server(shared, otherPubkey, router.identity(), nonce))
{
log::info("DH failed during path build.");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_CRYPTO}}), true);
return;
}

// hash data and check against given hash
ShortHash digest;
if (!crypto->hmac(
digest.data(),
reinterpret_cast<unsigned char*>(frame_str.data()),
frame_str.size(),
shared))
{
log::error(link_cat, "HMAC failed on path build request");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_CRYPTO}}), true);
return;
}
if (!std::equal(
digest.begin(), digest.end(), reinterpret_cast<const unsigned char*>(hash.data())))
{
log::info("HMAC mismatch on path build request");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_CRYPTO}}), true);
return;
}

// decrypt frame with our hop info
if (!crypto->xchacha20(
reinterpret_cast<unsigned char*>(hop_info.data()), hop_info.size(), shared, nonce))
dr7ana marked this conversation as resolved.
Show resolved Hide resolved
{
log::info("decrypt failed on path build request");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_CRYPTO}}), true);
return;
}

// populate transit hop object with hop info
// TODO: how to get downstream hop RouterID from here (all we have is oxen::quic::message)
// could do message->btstream->stream->connection_interface->connectionid
// and check our mapping, but that feels ugly as sin (and message->stream is private)
// TODO: also need downstream for IP / path build limiting clients
auto hop = std::make_shared<path::TransitHop>();
// hop->info.downstream = m.from(); // TODO: RouterID m.from() or similar
auto hop_dict = oxenc::bt_dict_consumer{hop_info};

// extract pathIDs and check if zero or used
hop->info.txID.from_string_view(hop_dict.require<std::string_view>("txid"));
hop->info.rxID.from_string_view(hop_dict.require<std::string_view>("rxid"));
if (info.txID.IsZero() || info.rxID.IsZero())
{
log::info("Invalid PathID; PathIDs must be non-zero.");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_PATHID}}), true);
return;
}
hop->info.upstream.from_string_view(hop_dict.require<std::string_view>("next");
dr7ana marked this conversation as resolved.
Show resolved Hide resolved

// TODO: need downstream (above), and also the whole transit hop container is garbage.
// namely the PathID uniqueness checking uses the PathIDs and upstream/downstream
// but if someone made a path with txid, rxid, and downstream the same but
// a different upstream, that would be "unique" but we wouldn't know where
// to route messages (nevermind that messages don't currently know the RouterID
// they came from).
if (router.path_context.HasTransitHop(hop->info))
{
log::info("Invalid PathID; PathIDs must be unique.");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_PATHID}}), true);
return;
}

otherPubkey.from_string_view(hop_dict.require<std::string_view>("commkey"));
nonce.from_string_view(hop_dict.require<std::string_view>("nonce"));

if (!crypto->dh_server(hop->pathKey, otherPubkey, router.identity(), nonce))
{
log::info("DH failed during path build.");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_CRYPTO}}), true);
return;
}
// generate hash of hop key for nonce mutation
crypto->shorthash(hop->nonceXOR, hop->pathKey.data(), hop->pathKey.size());

// set and check path lifetime
hop->lifetime = 1ms * hop_dict.require<int64_t>("lifetime");
if (hop->lifetime >= path::DEFAULT_LIFETIME)
{
log::info("path build attempt with too long of a lifetime.");
m.respond(serialize_response({{"STATUS", PathBuildMessage::BAD_LIFETIME}}), true);
return;
}
hop->started = router.now();
router.persist_connection_until(hop->info.downstream, hop->ExpireTime() + 10s);

if (hop->info.upstream == router.pubkey())
{
// we are terminal hop and everything is okay
router.path_context.PutTransitHop(hop);
m.respond(serialize_response({{"STATUS", PathBuildMessage::OK}}), false);
return;
}
else
{
// rotate our frame to the end of the list and forward upstream
frame_list.splice(frame_list.end(), frame_list, frame_list.begin());

send_control_message(
hop->info.upstream,
"path_build",
bt_serialize(frame_list),
[hop, this, prev_message = std::move(m)](oxen::quic::message response) {
if (response)
{
log::info(
link_cat,
"Upstream returned successful path build response; giving hop info to Router, "
"then relaying response");
router.path_context.PutTransitHop(hop);
m.respond(response.body_str(), false);
return;
}
else if (response.timed_out)
log::info(link_cat, "Upstream timed out on path build; relaying timeout");
else
log::info(link_cat, "Upstream returned path build failure; relaying response");

m.respond(response.body_str(), true);
});
}
}
catch (const std::exception& e)
{
log::warning(link_cat, "Exception: {}", e.what());
m.respond(serialize_response({{"STATUS", "EXCEPTION"}}), true);
m.respond(serialize_response({{"STATUS", PathBuildMessage::EXCEPTION}}), true);
return;
}
}
Expand Down
13 changes: 12 additions & 1 deletion llarp/messages/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ namespace llarp
{
namespace PathBuildMessage
{
inline auto OK = "OK"sv;
inline auto EXCEPTION = "EXCEPTION"sv;
inline auto BAD_FRAMES = "BAD_FRAMES"sv;
inline auto BAD_CRYPTO = "BAD_CRYPTO"sv;
inline auto NO_TRANSIT = "NOT ALLOWING TRANSIT"sv;
inline auto BAD_PATHID = "BAD PATH ID"sv;
inline auto BAD_LIFETIME = "BAD PATH LIFETIME (TOO LONG)"sv;

inline static void
setup_hop_keys(path::PathHopConfig& hop, const RouterID& nextHop)
{
Expand Down Expand Up @@ -64,7 +72,10 @@ namespace llarp

// encrypt hop_info (mutates in-place)
if (!crypto->xchacha20(
reinterpret_cast<uint8_t*>(hop_info.data()), hop_info.size(), shared, outer_nonce))
reinterpret_cast<unsigned char*>(hop_info.data()),
hop_info.size(),
shared,
outer_nonce))
{
log::error(path_cat, "Hop info encryption failed!");
throw std::runtime_error{"Hop info encrypttion failed"};
Expand Down
3 changes: 3 additions & 0 deletions llarp/path/pathbuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,9 @@ namespace llarp
dummy.reserve(last_len);

// append dummy frames; path build request must always have MAX_LEN frames
// TODO: with the data structured as it is now (bt-encoded dict as each frame)
// the dummy frames can't be completely random; they need to look like
// normal frames
for (size_t i = 0; i < path::MAX_LEN - n_hops; i++)
{
randombytes(reinterpret_cast<uint8_t*>(dummy.data()), dummy.size());
Expand Down
8 changes: 7 additions & 1 deletion llarp/util/aligned.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ namespace llarp
}

bool
from_string(std::string b)
from_string_view(std::string_view b)
dr7ana marked this conversation as resolved.
Show resolved Hide resolved
{
if (b.size() != sz)
{
Expand All @@ -254,6 +254,12 @@ namespace llarp
return true;
}

bool
from_string(std::string b)
{
return from_string_view(b);
}

bool
bt_encode(llarp_buffer_t* buf) const
{
Expand Down