lokinet/llarp/service/intro_set.cpp

345 lines
8.8 KiB
C++
Raw Normal View History

#include <service/intro_set.hpp>
2020-01-27 21:30:41 +00:00
#include <crypto/crypto.hpp>
2019-01-11 01:19:36 +00:00
#include <path/path.hpp>
namespace llarp
{
namespace service
{
2020-01-27 21:30:41 +00:00
util::StatusObject
EncryptedIntroSet::ExtractStatus() const
{
const auto sz = introsetPayload.size();
return {{"location", derivedSigningKey.ToString()},
{"signedAt", to_json(signedAt)},
2020-01-27 21:30:41 +00:00
{"size", sz}};
}
bool
EncryptedIntroSet::BEncode(llarp_buffer_t* buf) const
{
if (not bencode_start_dict(buf))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeWriteDictEntry("d", derivedSigningKey, buf))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeWriteDictEntry("n", nounce, buf))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeWriteDictInt("s", signedAt.count(), buf))
2020-01-27 21:30:41 +00:00
return false;
if (not bencode_write_bytestring(buf, "x", 1))
2020-01-27 21:30:41 +00:00
return false;
if (not bencode_write_bytestring(buf, introsetPayload.data(), introsetPayload.size()))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeWriteDictEntry("z", sig, buf))
2020-01-27 21:30:41 +00:00
return false;
return bencode_end(buf);
}
bool
EncryptedIntroSet::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
bool read = false;
if (key == "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;
}
if (not BEncodeMaybeReadDictEntry("d", derivedSigningKey, read, key, buf))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeMaybeReadDictEntry("n", nounce, read, key, buf))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeMaybeReadDictInt("s", signedAt, read, key, buf))
2020-01-27 21:30:41 +00:00
return false;
if (not BEncodeMaybeReadDictEntry("z", sig, read, key, buf))
2020-01-27 21:30:41 +00:00
return false;
return read;
}
bool
EncryptedIntroSet::OtherIsNewer(const EncryptedIntroSet& other) const
{
return signedAt < other.signedAt;
}
std::ostream&
EncryptedIntroSet::print(std::ostream& out, int levels, int spaces) const
{
Printer printer(out, levels, spaces);
printer.printAttribute("d", derivedSigningKey);
printer.printAttribute("n", nounce);
2020-02-24 19:40:45 +00:00
printer.printAttribute("s", signedAt.count());
printer.printAttribute("x", "[" + std::to_string(introsetPayload.size()) + " bytes]");
2020-01-27 21:30:41 +00:00
printer.printAttribute("z", sig);
return out;
}
std::optional<IntroSet>
2020-01-27 21:30:41 +00:00
EncryptedIntroSet::MaybeDecrypt(const PubKey& root) const
{
SharedSecret k(root);
IntroSet i;
std::vector<byte_t> payload = introsetPayload;
2020-01-27 21:30:41 +00:00
llarp_buffer_t buf(payload);
CryptoManager::instance()->xchacha20(buf, k, nounce);
if (not i.BDecode(&buf))
2020-01-27 21:30:41 +00:00
return {};
return i;
}
bool
EncryptedIntroSet::IsExpired(llarp_time_t now) const
{
return now >= signedAt + path::default_lifetime;
}
bool
EncryptedIntroSet::Sign(const PrivateKey& k)
2020-01-27 21:30:41 +00:00
{
signedAt = llarp::time_now_ms();
if (not k.toPublic(derivedSigningKey))
return false;
2020-01-27 21:30:41 +00:00
sig.Zero();
std::array<byte_t, MAX_INTROSET_SIZE + 128> tmp;
2020-01-27 21:30:41 +00:00
llarp_buffer_t buf(tmp);
if (not BEncode(&buf))
2020-01-27 21:30:41 +00:00
return false;
buf.sz = buf.cur - buf.base;
2020-01-27 21:30:41 +00:00
buf.cur = buf.base;
if (not CryptoManager::instance()->sign(sig, k, buf))
2020-01-28 21:55:36 +00:00
return false;
LogDebug("signed encrypted introset: ", *this);
2020-01-28 21:55:36 +00:00
return true;
2020-01-27 21:30:41 +00:00
}
bool
EncryptedIntroSet::Verify(llarp_time_t now) const
{
if (IsExpired(now))
2020-01-27 21:30:41 +00:00
return false;
std::array<byte_t, MAX_INTROSET_SIZE + 128> tmp;
2020-01-27 21:30:41 +00:00
llarp_buffer_t buf(tmp);
EncryptedIntroSet copy(*this);
copy.sig.Zero();
if (not copy.BEncode(&buf))
2020-01-27 21:30:41 +00:00
return false;
LogDebug("verify encrypted introset: ", copy, " sig = ", sig);
buf.sz = buf.cur - buf.base;
2020-01-27 21:30:41 +00:00
buf.cur = buf.base;
return CryptoManager::instance()->verify(derivedSigningKey, buf, sig);
}
2019-02-11 17:14:43 +00:00
util::StatusObject
IntroSet::ExtractStatus() const
2019-02-08 19:43:25 +00:00
{
util::StatusObject obj{{"published", to_json(T)}};
std::vector<util::StatusObject> introsObjs;
std::transform(
I.begin(),
I.end(),
std::back_inserter(introsObjs),
[](const auto& intro) -> util::StatusObject { return intro.ExtractStatus(); });
obj["intros"] = introsObjs;
if (!topic.IsZero())
obj["topic"] = topic.ToString();
2019-02-11 17:14:43 +00:00
return obj;
2019-02-08 19:43:25 +00:00
}
bool
2019-02-02 23:12:42 +00:00
IntroSet::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
{
bool read = false;
if (!BEncodeMaybeReadDictEntry("a", A, read, key, buf))
return false;
if (key == "i")
{
return BEncodeReadList(I, buf);
}
if (!BEncodeMaybeReadDictEntry("k", K, read, key, buf))
return false;
if (!BEncodeMaybeReadDictEntry("n", topic, read, key, buf))
return false;
if (!BEncodeMaybeReadDictInt("t", T, read, key, buf))
return false;
if (key == "w")
{
W.emplace();
2019-05-24 02:01:36 +00:00
return bencode_decode_dict(*W, buf);
}
if (!BEncodeMaybeReadDictInt("v", version, read, key, buf))
return false;
if (!BEncodeMaybeReadDictEntry("z", Z, read, key, buf))
return false;
if (read)
return true;
return bencode_discard(buf);
}
bool
IntroSet::BEncode(llarp_buffer_t* buf) const
{
if (!bencode_start_dict(buf))
return false;
if (!BEncodeWriteDictEntry("a", A, buf))
return false;
// start introduction list
if (!bencode_write_bytestring(buf, "i", 1))
return false;
if (!BEncodeWriteList(I.begin(), I.end(), buf))
return false;
// end introduction list
// pq pubkey
if (!BEncodeWriteDictEntry("k", K, buf))
return false;
// topic tag
if (topic.ToString().size())
{
if (!BEncodeWriteDictEntry("n", topic, buf))
return false;
}
// Timestamp published
if (!BEncodeWriteDictInt("t", T.count(), buf))
return false;
// write version
if (!BEncodeWriteDictInt("v", version, buf))
return false;
if (W)
{
if (!BEncodeWriteDictEntry("w", *W, buf))
return false;
}
if (!BEncodeWriteDictEntry("z", Z, buf))
return false;
return bencode_end(buf);
}
bool
IntroSet::HasExpiredIntros(llarp_time_t now) const
{
for (const auto& i : I)
if (now >= i.expiresAt)
return true;
return false;
}
bool
IntroSet::IsExpired(llarp_time_t now) const
{
return GetNewestIntroExpiration() < now;
}
bool
IntroSet::Verify(llarp_time_t now) const
{
std::array<byte_t, MAX_INTROSET_SIZE> tmp;
2019-02-02 23:12:42 +00:00
llarp_buffer_t buf(tmp);
IntroSet copy;
copy = *this;
copy.Z.Zero();
if (!copy.BEncode(&buf))
2019-01-26 12:33:34 +00:00
{
return false;
2019-01-26 12:33:34 +00:00
}
// rewind and resize buffer
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
if (!A.Verify(buf, Z))
2019-01-26 12:33:34 +00:00
{
return false;
2019-01-26 12:33:34 +00:00
}
// validate PoW
if (W && !W->IsValid(now))
2019-01-26 12:33:34 +00:00
{
return false;
2019-01-26 12:33:34 +00:00
}
// valid timestamps
// add max clock skew
now += MAX_INTROSET_TIME_DELTA;
for (const auto& intro : I)
{
if (intro.expiresAt > now && intro.expiresAt - now > path::default_lifetime)
{
if (!W)
2019-04-22 17:55:04 +00:00
{
LogWarn("intro has too high expire time");
return false;
2019-04-22 17:55:04 +00:00
}
if (intro.expiresAt - W->extendedLifetime > path::default_lifetime)
{
return false;
}
}
}
if (IsExpired(now))
{
LogWarn("introset expired: ", *this);
return false;
}
return true;
}
llarp_time_t
IntroSet::GetNewestIntroExpiration() const
{
2020-02-24 19:40:45 +00:00
llarp_time_t t = 0s;
for (const auto& intro : I)
t = std::max(intro.expiresAt, t);
return t;
}
2019-02-24 23:46:37 +00:00
std::ostream&
IntroSet::print(std::ostream& stream, int level, int spaces) const
{
Printer printer(stream, level, spaces);
printer.printAttribute("A", A);
printer.printAttribute("I", I);
printer.printAttribute("K", K);
std::string _topic = topic.ToString();
if (!_topic.empty())
2019-02-24 23:46:37 +00:00
{
printer.printAttribute("topic", _topic);
}
else
{
printer.printAttribute("topic", topic);
}
2020-02-24 19:40:45 +00:00
printer.printAttribute("T", T.count());
if (W)
2019-04-23 09:25:03 +00:00
{
printer.printAttribute("W", *W);
2019-04-23 09:25:03 +00:00
}
else
{
printer.printAttribute("W", "NULL");
}
2019-02-24 23:46:37 +00:00
printer.printAttribute("V", version);
printer.printAttribute("Z", Z);
return stream;
}
} // namespace service
} // namespace llarp