lokinet/llarp/service/intro_set.cpp

518 lines
13 KiB
C++
Raw Normal View History

#include "intro_set.hpp"
2023-10-19 21:59:57 +00:00
#include <llarp/crypto/crypto.hpp>
#include <oxenc/bt_serialize.h>
namespace llarp::service
{
EncryptedIntroSet::EncryptedIntroSet(
std::string signing_key,
std::chrono::milliseconds signed_at,
std::string enc_payload,
std::string nonce,
std::string s)
2023-10-03 20:00:23 +00:00
: signedAt{signed_at}
, introsetPayload{reinterpret_cast<uint8_t*>(enc_payload.data()), enc_payload.size()}
, nonce{reinterpret_cast<uint8_t*>(nonce.data())}
{
derivedSigningKey = PubKey::from_string(signing_key);
sig.from_string(std::move(s));
}
2023-10-03 20:00:23 +00:00
EncryptedIntroSet::EncryptedIntroSet(std::string bt_payload)
{
try
{
oxenc::bt_dict_consumer btdc{bt_payload};
derivedSigningKey = PubKey::from_string(btdc.require<std::string>("d"));
nonce.from_string(btdc.require<std::string>("n"));
2023-10-03 20:00:23 +00:00
signedAt = std::chrono::milliseconds{btdc.require<uint64_t>("s")};
introsetPayload = btdc.require<ustring>("x");
sig.from_string(btdc.require<std::string>("z"));
}
catch (...)
{
log::critical(net_cat, "Error: EncryptedIntroSet failed to bt encode contents!");
}
}
util::StatusObject
EncryptedIntroSet::ExtractStatus() const
{
const auto sz = introsetPayload.size();
return {
{"location", derivedSigningKey.ToString()}, {"signedAt", to_json(signedAt)}, {"size", sz}};
}
std::string
EncryptedIntroSet::bt_encode() const
{
oxenc::bt_dict_producer btdp;
try
{
btdp.append("d", derivedSigningKey.ToView());
btdp.append("n", nonce.ToView());
btdp.append("s", signedAt.count());
2023-10-03 20:00:23 +00:00
btdp.append(
"x",
std::string_view{
reinterpret_cast<const char*>(introsetPayload.data()), introsetPayload.size()});
btdp.append("z", sig.ToView());
}
catch (...)
{
log::critical(net_cat, "Error: EncryptedIntroSet failed to bt encode contents!");
}
return std::move(btdp).str();
}
2020-01-27 21:30:41 +00:00
bool
EncryptedIntroSet::decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
bool read = false;
if (key.startswith("x"))
2020-01-27 21:30:41 +00:00
{
llarp_buffer_t strbuf;
if (not bencode_read_string(buf, &strbuf))
2020-01-27 21:30:41 +00:00
return false;
if (strbuf.sz > MAX_INTROSET_SIZE)
2020-01-27 21:30:41 +00:00
return false;
introsetPayload.resize(strbuf.sz);
std::copy_n(strbuf.base, strbuf.sz, introsetPayload.data());
return true;
2020-01-27 21:30:41 +00:00
}
if (not BEncodeMaybeReadDictEntry("d", derivedSigningKey, read, key, buf))
return false;
2020-01-27 21:30:41 +00:00
if (not BEncodeMaybeReadDictEntry("n", nonce, read, key, buf))
return false;
2020-01-27 21:30:41 +00:00
if (not BEncodeMaybeReadDictInt("s", signedAt, read, key, buf))
return false;
2020-01-27 21:30:41 +00:00
if (not BEncodeMaybeReadDictEntry("z", sig, read, key, buf))
return false;
return read;
}
2020-01-27 21:30:41 +00:00
bool
EncryptedIntroSet::OtherIsNewer(const EncryptedIntroSet& other) const
{
return signedAt < other.signedAt;
}
2020-01-27 21:30:41 +00:00
std::string
EncryptedIntroSet::ToString() const
{
2022-07-18 17:56:09 +00:00
return fmt::format(
"[EncIntroSet d={} n={} s={} x=[{} bytes] z={}]",
derivedSigningKey,
nonce,
2022-07-18 17:56:09 +00:00
signedAt.count(),
introsetPayload.size(),
sig);
}
2023-10-03 20:00:23 +00:00
IntroSet
EncryptedIntroSet::decrypt(const PubKey& root) const
{
SharedSecret k(root);
2023-10-03 20:00:23 +00:00
std::string payload{
reinterpret_cast<const char*>(introsetPayload.data()), introsetPayload.size()};
crypto::xchacha20(reinterpret_cast<uint8_t*>(payload.data()), payload.size(), k, nonce);
2023-10-03 20:00:23 +00:00
return IntroSet{payload};
}
bool
EncryptedIntroSet::IsExpired(llarp_time_t now) const
{
return now >= signedAt + path::DEFAULT_LIFETIME;
}
2020-01-27 21:30:41 +00:00
bool
EncryptedIntroSet::Sign(const PrivateKey& k)
{
signedAt = llarp::time_now_ms();
if (not k.toPublic(derivedSigningKey))
return false;
sig.Zero();
auto bte = bt_encode();
2023-10-23 22:58:58 +00:00
if (not crypto::sign(sig, k, reinterpret_cast<uint8_t*>(bte.data()), bte.size()))
return false;
LogDebug("signed encrypted introset: ", *this);
return true;
}
2020-01-27 21:30:41 +00:00
bool
EncryptedIntroSet::verify(llarp_time_t now) const
{
if (IsExpired(now))
return false;
EncryptedIntroSet copy(*this);
copy.sig.Zero();
auto bte = copy.bt_encode();
return crypto::verify(
derivedSigningKey, reinterpret_cast<uint8_t*>(bte.data()), bte.size(), sig);
}
bool
EncryptedIntroSet::verify(uint8_t* introset, size_t introset_size, uint8_t* key, uint8_t* sig)
{
return crypto::verify(key, introset, introset_size, sig);
}
bool
EncryptedIntroSet::verify(std::string introset, std::string key, std::string sig)
{
return crypto::verify(
reinterpret_cast<uint8_t*>(key.data()),
reinterpret_cast<uint8_t*>(introset.data()),
introset.size(),
reinterpret_cast<uint8_t*>(sig.data()));
}
util::StatusObject
IntroSet::ExtractStatus() const
{
util::StatusObject obj{{"published", to_json(time_signed)}};
// TODO: this
2023-10-19 21:59:57 +00:00
// std::vector<util::StatusObject> introsObjs;
// std::transform(
// intros.begin(),
// intros.end(),
// std::back_inserter(introsObjs),
// [](const auto& intro) -> util::StatusObject { return intro.ExtractStatus(); });
// obj["intros"] = introsObjs;
// if (!topic.IsZero())
// obj["topic"] = topic.ToString();
// std::vector<util::StatusObject> protocols;
// std::transform(
// supported_protocols.begin(),
// supported_protocols.end(),
// std::back_inserter(protocols),
// [](const auto& proto) -> util::StatusObject { return service::ToString(proto); });
// obj["protos"] = protocols;
// std::vector<util::StatusObject> ranges;
// std::transform(
// owned_ranges.begin(),
// owned_ranges.end(),
// std::back_inserter(ranges),
// [](const auto& range) -> util::StatusObject { return range.ToString(); });
// obj["advertisedRanges"] = ranges;
// if (exit_policy)
// obj["exitPolicy"] = exit_policy->ExtractStatus();
2021-04-14 15:07:06 +00:00
return obj;
}
bool
IntroSet::decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
bool read = false;
if (!BEncodeMaybeReadDictEntry("a", address_keys, read, key, buf))
return false;
2020-01-27 21:30:41 +00:00
if (key.startswith("e"))
2021-04-14 15:07:06 +00:00
{
net::TrafficPolicy policy;
if (not policy.BDecode(buf))
return false;
exit_policy = policy;
2021-04-14 15:07:06 +00:00
return true;
}
if (key.startswith("i"))
2020-01-27 21:30:41 +00:00
{
2021-04-14 15:07:06 +00:00
return BEncodeReadList(intros, buf);
2020-01-27 21:30:41 +00:00
}
if (!BEncodeMaybeReadDictEntry("k", sntru_pubkey, read, key, buf))
return false;
2020-01-27 21:30:41 +00:00
if (!BEncodeMaybeReadDictEntry("n", topic, read, key, buf))
return false;
2020-01-27 21:30:41 +00:00
if (key.startswith("p"))
2021-04-14 15:07:06 +00:00
{
return bencode_read_list(
[&](llarp_buffer_t* buf, bool more) {
if (more)
{
uint64_t protoval;
if (not bencode_read_integer(buf, &protoval))
return false;
supported_protocols.emplace_back(static_cast<ProtocolType>(protoval));
2021-04-14 15:07:06 +00:00
}
return true;
},
buf);
}
if (key.startswith("r"))
2021-04-14 15:07:06 +00:00
{
return BEncodeReadSet(owned_ranges, buf);
2021-04-14 15:07:06 +00:00
}
if (key.startswith("s"))
2020-01-27 21:30:41 +00:00
{
byte_t* begin = buf->cur;
if (not bencode_discard(buf))
2020-01-27 21:30:41 +00:00
return false;
byte_t* end = buf->cur;
2019-02-08 19:43:25 +00:00
2021-04-14 15:07:06 +00:00
std::string_view srvString(
reinterpret_cast<const char*>(begin), static_cast<size_t>(end - begin));
try
{
oxenc::bt_deserialize(srvString, SRVs);
}
catch (const oxenc::bt_deserialize_invalid& err)
{
LogError("Error decoding SRV records from IntroSet: ", err.what());
return false;
}
read = true;
}
if (!BEncodeMaybeReadDictInt("t", time_signed, read, key, buf))
return false;
if (!BEncodeMaybeReadDictInt("v", version, read, key, buf))
return false;
2021-04-14 15:07:06 +00:00
if (!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
return false;
2021-04-14 15:07:06 +00:00
return read or bencode_discard(buf);
}
2023-10-03 20:00:23 +00:00
IntroSet::IntroSet(std::string bt_payload)
{
try
{
oxenc::bt_dict_consumer btdc{bt_payload};
if (btdc.key() == "a")
{
auto subdict = btdc.consume_dict_consumer();
address_keys.bt_decode(subdict);
}
if (btdc.key() == "e")
{
auto subdict = btdc.consume_dict_consumer();
exit_policy->bt_decode(subdict);
}
if (btdc.key() == "i")
{
auto sublist = btdc.consume_list_consumer();
while (not sublist.is_finished())
{
intros.emplace_back(sublist.consume_string());
}
}
sntru_pubkey.from_string(btdc.require<std::string>("k"));
topic.from_string(btdc.require<std::string>("n"));
if (btdc.key() == "p")
{
auto sublist = btdc.consume_list_consumer();
while (not sublist.is_finished())
{
supported_protocols.emplace_back(ProtocolType{sublist.consume_integer<uint64_t>()});
2023-10-03 20:00:23 +00:00
}
}
if (btdc.key() == "r")
{
auto sublist = btdc.consume_list_consumer();
while (not sublist.is_finished())
{
owned_ranges.emplace(sublist.consume_string());
}
}
if (btdc.key() == "s")
{
// TODO: fuck everything about these tuples
// auto sublist = btdc.consume_list_consumer();
// while (not sublist.is_finished())
// {
// // auto s = oxenc::
// auto sd = SRVs.emplace_back();
// }
}
time_signed = std::chrono::milliseconds{btdc.require<uint64_t>("t")};
signature.from_string(btdc.require<std::string>("z"));
}
catch (...)
{
log::critical(net_cat, "Error: EncryptedIntroSet failed to bt encode contents!");
}
}
std::string
IntroSet::bt_encode() const
{
oxenc::bt_dict_producer btdp;
2021-04-14 15:07:06 +00:00
try
2021-04-14 15:07:06 +00:00
{
{
auto subdict = btdp.append_dict("a");
address_keys.bt_encode(subdict);
}
if (exit_policy)
{
auto subdict = btdp.append_dict("e");
exit_policy->bt_encode(subdict);
}
2021-04-14 15:07:06 +00:00
{
2023-10-03 20:00:23 +00:00
auto sublist = btdp.append_list("i");
for (auto& i : intros)
2023-10-03 20:00:23 +00:00
i.bt_encode(sublist);
}
2021-04-14 15:07:06 +00:00
btdp.append("k", sntru_pubkey.ToView());
btdp.append("n", topic.ToView());
2021-04-14 15:07:06 +00:00
if (not supported_protocols.empty())
2021-04-14 15:07:06 +00:00
{
auto sublist = btdp.append_list("p");
for (auto& p : supported_protocols)
sublist.append(static_cast<uint64_t>(p));
2021-04-14 15:07:06 +00:00
}
if (not owned_ranges.empty())
{
2023-10-03 20:00:23 +00:00
auto sublist = btdp.append_list("r");
for (auto& r : owned_ranges)
r.bt_encode(sublist);
}
if (not SRVs.empty())
2023-10-03 20:00:23 +00:00
{
auto sublist = btdp.append_list("s");
for (auto& s : SRVs)
sublist.append(oxenc::bt_serialize(s));
}
2021-04-14 19:40:57 +00:00
btdp.append("t", time_signed.count());
btdp.append("z", signature.ToView());
2021-04-14 19:40:57 +00:00
}
catch (...)
{
log::critical(net_cat, "Error: IntroSet failed to bt encode contents!");
}
return std::move(btdp).str();
}
bool
IntroSet::HasExpiredIntros(llarp_time_t now) const
{
2021-04-14 15:07:06 +00:00
for (const auto& intro : intros)
if (now >= intro.expiry)
return true;
return false;
}
bool
IntroSet::HasStaleIntros(llarp_time_t now, llarp_time_t delta) const
{
for (const auto& intro : intros)
if (intro.ExpiresSoon(now, delta))
return true;
return false;
}
bool
IntroSet::IsExpired(llarp_time_t now) const
{
return GetNewestIntroExpiration() < now;
}
std::vector<llarp::dns::SRVData>
IntroSet::GetMatchingSRVRecords(std::string_view service_proto) const
{
std::vector<llarp::dns::SRVData> records;
for (const auto& tuple : SRVs)
{
if (std::get<0>(tuple) == service_proto)
{
records.push_back(llarp::dns::SRVData::fromTuple(tuple));
}
}
return records;
}
bool
IntroSet::verify(llarp_time_t now) const
{
IntroSet copy;
copy = *this;
2021-04-14 15:07:06 +00:00
copy.signature.Zero();
auto bte = copy.bt_encode();
if (!address_keys.verify(reinterpret_cast<uint8_t*>(bte.data()), bte.size(), signature))
{
return false;
}
// valid timestamps
// add max clock skew
now += MAX_INTROSET_TIME_DELTA;
2021-04-14 15:07:06 +00:00
for (const auto& intro : intros)
{
if (intro.expiry > now && intro.expiry - now > path::DEFAULT_LIFETIME)
2019-01-26 12:33:34 +00:00
{
2021-04-14 15:07:06 +00:00
return false;
}
}
2021-04-14 15:07:06 +00:00
return not IsExpired(now);
}
2019-02-24 23:46:37 +00:00
llarp_time_t
IntroSet::GetNewestIntroExpiration() const
{
2021-04-14 15:07:06 +00:00
llarp_time_t maxTime = 0s;
for (const auto& intro : intros)
maxTime = std::max(intro.expiry, maxTime);
2021-04-14 15:07:06 +00:00
return maxTime;
}
std::string
IntroSet::ToString() const
{
2022-07-18 17:56:09 +00:00
return fmt::format(
"[IntroSet addressKeys={} intros={{{}}} sntrupKey={} topic={} signedAt={} v={} sig={}]",
address_keys,
2022-07-18 17:56:09 +00:00
fmt::format("{}", fmt::join(intros, ",")),
sntru_pubkey,
2022-07-18 17:56:09 +00:00
topic,
time_signed.count(),
2022-07-18 17:56:09 +00:00
version,
signature);
}
} // namespace llarp::service