2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "info.hpp"
|
|
|
|
#include "intro.hpp"
|
|
|
|
#include "protocol_type.hpp"
|
|
|
|
#include "tag.hpp"
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/types.hpp>
|
|
|
|
#include <llarp/dns/srv_data.hpp>
|
2021-04-14 15:07:06 +00:00
|
|
|
#include <llarp/net/ip_range.hpp>
|
|
|
|
#include <llarp/net/traffic_policy.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/util/bencode.hpp>
|
|
|
|
#include <llarp/util/status.hpp>
|
|
|
|
#include <llarp/util/time.hpp>
|
|
|
|
|
2018-09-29 08:16:54 +00:00
|
|
|
#include <algorithm>
|
2019-01-22 01:14:02 +00:00
|
|
|
#include <functional>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <iostream>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <optional>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <vector>
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace llarp::service
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
constexpr std::size_t MAX_INTROSET_SIZE = 4096;
|
|
|
|
// 10 seconds clock skew permitted for introset expiration
|
|
|
|
constexpr llarp_time_t MAX_INTROSET_TIME_DELTA = 10s;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
struct IntroSet
|
|
|
|
{
|
2023-09-27 14:09:48 +00:00
|
|
|
ServiceInfo address_keys;
|
2023-08-31 16:28:02 +00:00
|
|
|
std::vector<Introduction> intros;
|
|
|
|
PQPubKey sntru_pubkey;
|
|
|
|
Tag topic;
|
|
|
|
std::vector<llarp::dns::SRVTuple> SRVs;
|
|
|
|
llarp_time_t time_signed = 0s;
|
|
|
|
|
2023-10-03 20:00:23 +00:00
|
|
|
IntroSet() = default;
|
|
|
|
|
|
|
|
explicit IntroSet(std::string bt_payload);
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/// ethertypes we advertise that we speak
|
|
|
|
std::vector<ProtocolType> supported_protocols;
|
|
|
|
/// aonnuce that these ranges are reachable via our endpoint
|
|
|
|
/// only set when we support exit traffic ethertype is supported
|
|
|
|
std::set<IPRange> owned_ranges;
|
|
|
|
|
|
|
|
/// policies about traffic that we are willing to carry
|
|
|
|
/// a protocol/range whitelist or blacklist
|
|
|
|
/// only set when we support exit traffic ethertype
|
|
|
|
std::optional<net::TrafficPolicy> exit_policy;
|
|
|
|
|
|
|
|
Signature signature;
|
|
|
|
uint64_t version = llarp::constants::proto_version;
|
|
|
|
|
|
|
|
bool
|
|
|
|
OtherIsNewer(const IntroSet& other) const
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return time_signed < other.time_signed;
|
|
|
|
}
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
llarp_time_t
|
|
|
|
GetNewestIntroExpiration() const;
|
2021-04-14 15:07:06 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
GetNewestIntro(Introduction& intro) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
HasExpiredIntros(llarp_time_t now) const;
|
2018-09-10 17:37:28 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/// return true if any of our intros expires soon given a delta
|
|
|
|
bool
|
|
|
|
HasStaleIntros(llarp_time_t now, llarp_time_t delta) const;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const;
|
2018-08-10 21:34:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::vector<llarp::dns::SRVData>
|
|
|
|
GetMatchingSRVRecords(std::string_view service_proto) const;
|
2019-05-07 12:31:34 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
bt_encode() const;
|
2018-07-17 06:17:13 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
2021-06-02 19:52:13 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
2018-07-18 00:25:24 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
2023-09-27 14:09:48 +00:00
|
|
|
verify(llarp_time_t now) const;
|
2020-08-31 23:25:58 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const;
|
|
|
|
};
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline bool
|
|
|
|
operator<(const IntroSet& lhs, const IntroSet& rhs)
|
|
|
|
{
|
2023-09-27 14:09:48 +00:00
|
|
|
return lhs.address_keys < rhs.address_keys;
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
2019-05-24 02:01:36 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline bool
|
|
|
|
operator==(const IntroSet& lhs, const IntroSet& rhs)
|
|
|
|
{
|
|
|
|
return std::tie(
|
2023-09-27 14:09:48 +00:00
|
|
|
lhs.address_keys,
|
2023-08-31 16:28:02 +00:00
|
|
|
lhs.intros,
|
|
|
|
lhs.sntru_pubkey,
|
|
|
|
lhs.time_signed,
|
|
|
|
lhs.version,
|
|
|
|
lhs.topic,
|
|
|
|
lhs.signature)
|
|
|
|
== std::tie(
|
2023-09-27 14:09:48 +00:00
|
|
|
rhs.address_keys,
|
2023-08-31 16:28:02 +00:00
|
|
|
rhs.intros,
|
|
|
|
rhs.sntru_pubkey,
|
|
|
|
rhs.time_signed,
|
|
|
|
rhs.version,
|
|
|
|
rhs.topic,
|
|
|
|
rhs.signature);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline bool
|
|
|
|
operator!=(const IntroSet& lhs, const IntroSet& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/// public version of the introset that is encrypted
|
|
|
|
struct EncryptedIntroSet
|
|
|
|
{
|
|
|
|
PubKey derivedSigningKey;
|
|
|
|
llarp_time_t signedAt = 0s;
|
2023-10-03 20:00:23 +00:00
|
|
|
ustring introsetPayload;
|
2023-11-06 20:59:33 +00:00
|
|
|
SymmNonce nonce;
|
2023-08-31 16:28:02 +00:00
|
|
|
std::optional<Tag> topic;
|
|
|
|
Signature sig;
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-09-29 21:00:13 +00:00
|
|
|
EncryptedIntroSet() = default;
|
|
|
|
|
|
|
|
explicit EncryptedIntroSet(
|
|
|
|
std::string signing_key,
|
|
|
|
std::chrono::milliseconds signed_at,
|
|
|
|
std::string enc_payload,
|
|
|
|
std::string nonce,
|
|
|
|
std::string sig);
|
|
|
|
|
2023-10-03 20:00:23 +00:00
|
|
|
explicit EncryptedIntroSet(std::string bt_payload);
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
Sign(const PrivateKey& k);
|
2019-04-23 09:25:03 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
IsExpired(llarp_time_t now) const;
|
2019-04-23 09:25:03 +00:00
|
|
|
|
2023-09-27 14:09:48 +00:00
|
|
|
std::string
|
|
|
|
bt_encode() const;
|
2019-04-23 09:25:03 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
BDecode(llarp_buffer_t* buf)
|
2020-01-27 21:30:41 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
return bencode_decode_dict(*this, buf);
|
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf);
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
OtherIsNewer(const EncryptedIntroSet& other) const;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/// verify signature and timestamp
|
|
|
|
bool
|
2023-09-27 14:09:48 +00:00
|
|
|
verify(llarp_time_t now) const;
|
|
|
|
|
2023-09-29 21:00:13 +00:00
|
|
|
static bool
|
|
|
|
verify(uint8_t* introset, size_t introset_size, uint8_t* key, uint8_t* sig);
|
|
|
|
|
2023-09-27 14:09:48 +00:00
|
|
|
static bool
|
|
|
|
verify(std::string introset, std::string key, std::string sig);
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
ToString() const;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
util::StatusObject
|
|
|
|
ExtractStatus() const;
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-10-03 20:00:23 +00:00
|
|
|
IntroSet
|
|
|
|
decrypt(const PubKey& root) const;
|
2023-08-31 16:28:02 +00:00
|
|
|
};
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline bool
|
|
|
|
operator<(const EncryptedIntroSet& lhs, const EncryptedIntroSet& rhs)
|
|
|
|
{
|
|
|
|
return lhs.derivedSigningKey < rhs.derivedSigningKey;
|
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline bool
|
|
|
|
operator==(const EncryptedIntroSet& lhs, const EncryptedIntroSet& rhs)
|
|
|
|
{
|
2023-11-06 20:59:33 +00:00
|
|
|
return std::tie(lhs.signedAt, lhs.derivedSigningKey, lhs.nonce, lhs.sig)
|
|
|
|
== std::tie(rhs.signedAt, rhs.derivedSigningKey, rhs.nonce, rhs.sig);
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
inline bool
|
|
|
|
operator!=(const EncryptedIntroSet& lhs, const EncryptedIntroSet& rhs)
|
|
|
|
{
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
2020-01-27 21:30:41 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
using EncryptedIntroSetLookupHandler = std::function<void(const std::vector<EncryptedIntroSet>&)>;
|
|
|
|
using IntroSetLookupHandler = std::function<void(const std::vector<IntroSet>&)>;
|
2019-01-22 01:14:02 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
} // namespace llarp::service
|
2022-07-16 00:41:14 +00:00
|
|
|
|
|
|
|
template <>
|
|
|
|
constexpr inline bool llarp::IsToStringFormattable<llarp::service::IntroSet> = true;
|
|
|
|
template <>
|
|
|
|
constexpr inline bool llarp::IsToStringFormattable<llarp::service::EncryptedIntroSet> = true;
|