Skip to content

Commit

Permalink
path-build tweaking, DRYing out path messages generally
Browse files Browse the repository at this point in the history
  • Loading branch information
dr7ana committed Oct 25, 2024
1 parent 0805844 commit f6376b4
Show file tree
Hide file tree
Showing 18 changed files with 52 additions and 58 deletions.
2 changes: 1 addition & 1 deletion external/oxen-libquic
7 changes: 5 additions & 2 deletions llarp/crypto/crypto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,12 @@ namespace llarp
}

void crypto::derive_decrypt_outer_wrapping(
const Ed25519SecretKey& local_sk, const PubKey& remote, const SymmNonce& nonce, uspan encrypted)
const Ed25519SecretKey& local_sk,
SharedSecret& shared,
const PubKey& remote,
const SymmNonce& nonce,
uspan encrypted)
{
SharedSecret shared;
// derive shared secret using ephemeral pubkey and our secret key (and nonce)
if (!crypto::dh_server(shared, remote, local_sk, nonce))
{
Expand Down
6 changes: 5 additions & 1 deletion llarp/crypto/crypto.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ namespace llarp
/// pubkey and the provided nonce. The encrypted payload is mutated in-place. Will throw on failure of either
/// the server DH derivation or the xchacha20 payload mutation
void derive_decrypt_outer_wrapping(
const Ed25519SecretKey& local, const PubKey& remote, const SymmNonce& nonce, uspan encrypted);
const Ed25519SecretKey& local,
SharedSecret& shared,
const PubKey& remote,
const SymmNonce& nonce,
uspan encrypted);

bool make_scalar(AlignedBuffer<32>& out, const PubKey& k, uint64_t i);

Expand Down
6 changes: 6 additions & 0 deletions llarp/handlers/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ namespace llarp::handlers

_running = false;

if (_cc_publisher)
{
log::debug(logcat, "ClientContact publish ticker stopped!");
_cc_publisher->stop();
}

Lock_t l{paths_mutex};

_sessions.stop_sessions(send_close);
Expand Down
6 changes: 3 additions & 3 deletions llarp/link/link_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1267,16 +1267,16 @@ namespace llarp
log::trace(logcat, "Deserializing frame: {}", buffer_printer{frames.front()});

SymmNonce nonce;
PubKey remote_pk;
ustring hop_payload;
SharedSecret shared;

std::tie(nonce, remote_pk, hop_payload) =
std::tie(nonce, shared, hop_payload) =
PathBuildMessage::deserialize_hop(oxenc::bt_dict_consumer{frames.front()}, _router.identity());

log::trace(logcat, "Deserializing hop payload: {}", buffer_printer{hop_payload});

auto hop = path::TransitHop::deserialize_hop(
oxenc::bt_dict_consumer{hop_payload}, from, _router, remote_pk, nonce);
oxenc::bt_dict_consumer{hop_payload}, from, _router, std::move(shared));

hop->started = _router.now();
set_conn_persist(hop->downstream(), hop->expiry_time() + 10s);
Expand Down
2 changes: 2 additions & 0 deletions llarp/messages/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace llarp
return std::move(btdp).str();
}

// inline static std::string serialize(const SymmNonce& nonce, std::string_view)

inline static std::string serialize(const SymmNonce& nonce, const HopID& hop_id, const ustring_view& payload)
{
return serialize(
Expand Down
16 changes: 8 additions & 8 deletions llarp/messages/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ namespace llarp
- Generate the XOR nonce by hashing the symmetric key from DH (`hop.shared`) and truncating
Bt-encoded contents:
- 'k' : shared pubkey used to derive symmetric key
- 'n' : symmetric nonce used for DH key-exchange
- 's' : shared pubkey used to derive symmetric key
- 'x' : encrypted payload
- 'l' : path lifetime
- 'r' : rxID (the path ID for messages going *to* the hop)
Expand Down Expand Up @@ -171,26 +171,26 @@ namespace llarp
buffer_printer{hop_payload});

oxenc::bt_dict_producer btdp;

btdp.append("k", ephemeral_key.to_pubkey().to_view());
btdp.append("n", hop.nonce.to_view());
btdp.append("s", ephemeral_key.to_pubkey().to_view());
btdp.append("x", hop_payload);

return std::move(btdp).str();
}

inline static std::tuple<SymmNonce, PubKey, ustring> deserialize_hop(
inline static std::tuple<SymmNonce, SharedSecret, ustring> deserialize_hop(
oxenc::bt_dict_consumer&& btdc, const Ed25519SecretKey& local_sk)
{
SymmNonce nonce;
PubKey remote_pk;
ustring hop_payload;
SharedSecret shared;

try
{
remote_pk.from_string(btdc.require<std::string_view>("k"));
nonce.from_string(btdc.require<std::string_view>("n"));
remote_pk.from_string(btdc.require<std::string_view>("s"));
hop_payload = btdc.require<ustring>("x");
hop_payload = btdc.require<ustring_view>("x");
}
catch (const std::exception& e)
{
Expand All @@ -207,7 +207,7 @@ namespace llarp

try
{
crypto::derive_decrypt_outer_wrapping(local_sk, remote_pk, nonce, to_uspan(hop_payload));
crypto::derive_decrypt_outer_wrapping(local_sk, shared, remote_pk, nonce, to_uspan(hop_payload));
}
catch (...)
{
Expand All @@ -222,7 +222,7 @@ namespace llarp
remote_pk.to_string(),
buffer_printer{hop_payload});

return {std::move(nonce), std::move(remote_pk), std::move(hop_payload)};
return {std::move(nonce), std::move(shared), std::move(hop_payload)};
}
} // namespace PathBuildMessage
} // namespace llarp
3 changes: 2 additions & 1 deletion llarp/messages/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,15 @@ namespace llarp
SymmNonce nonce;
RouterID shared_pubkey;
ustring payload;
SharedSecret shared;

try
{
nonce = SymmNonce::make(btdc.require<std::string>("n"));
shared_pubkey = RouterID{btdc.require<std::string>("s")};
payload = btdc.require<ustring>("x");

crypto::derive_decrypt_outer_wrapping(local, shared_pubkey, nonce, to_uspan(payload));
crypto::derive_decrypt_outer_wrapping(local, shared, shared_pubkey, nonce, to_uspan(payload));

{
RouterID remote;
Expand Down
6 changes: 2 additions & 4 deletions llarp/path/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,11 +219,9 @@ namespace llarp::path
});
}

bool Path::is_ready() const
bool Path::is_ready(std::chrono::milliseconds now) const
{
// if (is_expired(llarp::time_now_ms()))
// return false;
return _established;
return _established ? is_expired(now) : false;
}

RouterID Path::upstream_rid()
Expand Down
2 changes: 1 addition & 1 deletion llarp/path/path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace llarp

bool send_path_data_message(std::string body);

bool is_ready() const;
bool is_ready(std::chrono::milliseconds now = llarp::time_now_ms()) const;

RouterID upstream_rid();
const RouterID& upstream_rid() const;
Expand Down
16 changes: 0 additions & 16 deletions llarp/path/path_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,6 @@ namespace llarp::path
}
}

intro_set PathContext::get_recent_ccs() const
{
Lock_t l{paths_mutex};

intro_set intros;
auto now = llarp::time_now_ms();

for (auto& [_, p] : _path_map)
{
if (p->is_ready() and not p->is_expired(now))
intros.emplace(p->intro);
}

return intros;
}

void PathContext::drop_path(const std::shared_ptr<Path>& path)
{
Lock_t l{paths_mutex};
Expand Down
2 changes: 0 additions & 2 deletions llarp/path/path_context.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ namespace llarp::path

void drop_paths(std::vector<std::shared_ptr<Path>> droplist);

intro_set get_recent_ccs() const;

private:
const RouterID _local_rid;

Expand Down
7 changes: 4 additions & 3 deletions llarp/path/path_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ namespace llarp::path

for (const auto& [_, p] : _paths)
{
if (p->is_ready() and not p->intro.is_expired(now))
if (p and p->is_ready(now))
intros.emplace(p->intro);
}

Expand Down Expand Up @@ -350,7 +350,8 @@ namespace llarp::path

for (auto& [_, p] : _paths)
{
dissociate_hop_ids(p);
if (p)
dissociate_hop_ids(p);
}

_paths.clear();
Expand Down Expand Up @@ -555,7 +556,7 @@ namespace llarp::path
// the same entity from knowing they are part of the same path
// (unless they're adjacent in the path; nothing we can do about that obviously).

// i from n_hops downto 0
// i from n_hops down to 0
for (int i = n_hops - 1; i >= 0; --i)
{
const auto& next_rid = i == n_hops - 1 ? path_hops[i].rc.router_id() : path_hops[i + 1].rc.router_id();
Expand Down
2 changes: 1 addition & 1 deletion llarp/path/path_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ namespace llarp
Router& _router;
size_t num_hops;
std::chrono::milliseconds last_build{0s};
std::chrono::milliseconds build_interval_limit = MIN_PATH_BUILD_INTERVAL;
std::chrono::milliseconds build_interval_limit{MIN_PATH_BUILD_INTERVAL};

std::set<RouterID> snode_blacklist;

Expand Down
6 changes: 3 additions & 3 deletions llarp/path/path_types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ namespace llarp
/// next hop's router id
RouterID upstream;
// lifetime
std::chrono::milliseconds lifetime = DEFAULT_LIFETIME;
std::chrono::milliseconds lifetime{DEFAULT_LIFETIME};

nlohmann::json ExtractStatus() const;

Expand All @@ -51,8 +51,8 @@ namespace llarp
};

// milliseconds waiting between builds on a path per router
static constexpr auto MIN_PATH_BUILD_INTERVAL = 500ms;
static constexpr auto PATH_BUILD_RATE = 100ms;
static constexpr auto MIN_PATH_BUILD_INTERVAL{500ms};
static constexpr auto PATH_BUILD_RATE{100ms};
} // namespace path
} // namespace llarp

Expand Down
7 changes: 4 additions & 3 deletions llarp/path/transit_hop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace llarp::path
static auto logcat = log::Cat("transit-hop");

std::shared_ptr<TransitHop> TransitHop::deserialize_hop(
oxenc::bt_dict_consumer&& btdc, const RouterID& src, Router& r, const PubKey& remote_pk, const SymmNonce& nonce)
oxenc::bt_dict_consumer&& btdc, const RouterID& src, Router& r, SharedSecret secret)
{
auto hop = std::make_shared<TransitHop>();

Expand All @@ -34,13 +34,14 @@ namespace llarp::path
throw std::runtime_error{PathBuildMessage::BAD_LIFETIME};

hop->downstream() = src;
hop->shared = std::move(secret);

if (r.path_context()->has_transit_hop(hop))
throw std::runtime_error{PathBuildMessage::BAD_PATHID};

// TODO: get this from the first dh
if (!crypto::dh_server(hop->shared, remote_pk, r.identity(), nonce))
throw std::runtime_error{PathBuildMessage::BAD_CRYPTO};
// if (!crypto::dh_server(hop->shared, remote_pk, r.identity(), nonce))
// throw std::runtime_error{PathBuildMessage::BAD_CRYPTO};

// generate hash of hop key for nonce mutation
ShortHash xor_hash;
Expand Down
12 changes: 4 additions & 8 deletions llarp/path/transit_hop.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,15 @@ namespace llarp
// This static factory function is used in path-build logic. The exceptions thrown are the exact response
// bodies passed to message::respond(...) function
static std::shared_ptr<TransitHop> deserialize_hop(
oxenc::bt_dict_consumer&& btdc,
const RouterID& src,
Router& r,
const PubKey& remote_pk,
const SymmNonce& nonce);
oxenc::bt_dict_consumer&& btdc, const RouterID& src, Router& r, SharedSecret secret);

SharedSecret shared;
SymmNonce nonceXOR;
std::chrono::milliseconds started = 0s;
std::chrono::milliseconds started{0s};
// 10 minutes default
std::chrono::milliseconds lifetime = DEFAULT_LIFETIME;
std::chrono::milliseconds lifetime{DEFAULT_LIFETIME};
uint8_t version;
std::chrono::milliseconds _last_activity = 0s;
std::chrono::milliseconds _last_activity{0s};
bool terminal_hop{false};

RouterID& upstream() { return _upstream; }
Expand Down
2 changes: 1 addition & 1 deletion llarp/router/router.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ namespace llarp
}
else
{
_session_endpoint->start_tickers();
// _session_endpoint->start_tickers();
// Resolve needed ONS values now that we have the necessary things prefigured
_session_endpoint->resolve_ons_mappings();
}
Expand Down

0 comments on commit f6376b4

Please sign in to comment.