2018-07-05 15:44:06 +00:00
|
|
|
#include <llarp/service.hpp>
|
2018-07-06 16:08:30 +00:00
|
|
|
#include "buffer.hpp"
|
2018-07-27 00:21:57 +00:00
|
|
|
#include "fs.hpp"
|
2018-07-11 16:11:19 +00:00
|
|
|
#include "ini.hpp"
|
2018-07-09 17:32:11 +00:00
|
|
|
#include "router.hpp"
|
2018-07-05 15:44:06 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
|
|
|
IntroSet::~IntroSet()
|
|
|
|
{
|
2018-07-06 16:08:30 +00:00
|
|
|
if(W)
|
|
|
|
delete W;
|
2018-07-05 15:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-07-09 17:32:11 +00:00
|
|
|
IntroSet::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
|
2018-07-05 15:44:06 +00:00
|
|
|
{
|
2018-07-09 17:32:11 +00:00
|
|
|
bool read = false;
|
2018-07-11 16:11:19 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("a", A, read, key, buf))
|
2018-07-09 17:32:11 +00:00
|
|
|
return false;
|
2018-07-05 15:44:06 +00:00
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
if(llarp_buffer_eq(key, "i"))
|
2018-07-09 17:32:11 +00:00
|
|
|
{
|
2018-07-20 04:50:28 +00:00
|
|
|
return BEncodeReadList(I, buf);
|
2018-07-09 17:32:11 +00:00
|
|
|
}
|
2018-08-13 23:22:31 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("k", K, read, key, buf))
|
|
|
|
return false;
|
2018-07-05 15:44:06 +00:00
|
|
|
|
2018-07-17 07:30:03 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("n", topic, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2018-09-23 13:57:21 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("t", T, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2018-07-18 00:25:24 +00:00
|
|
|
if(llarp_buffer_eq(key, "w"))
|
|
|
|
{
|
|
|
|
if(W)
|
|
|
|
delete W;
|
|
|
|
W = new PoW();
|
|
|
|
return W->BDecode(buf);
|
|
|
|
}
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
2018-07-09 17:32:11 +00:00
|
|
|
return false;
|
|
|
|
|
2018-07-11 16:11:19 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("z", Z, read, key, buf))
|
2018-07-09 17:32:11 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return read;
|
2018-07-05 15:44:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IntroSet::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
2018-07-06 16:08:30 +00:00
|
|
|
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
|
|
|
|
|
2018-08-13 23:22:31 +00:00
|
|
|
// pq pubkey
|
|
|
|
if(!BEncodeWriteDictEntry("k", K, buf))
|
|
|
|
return false;
|
|
|
|
|
2018-07-17 07:30:03 +00:00
|
|
|
// topic tag
|
2018-08-10 03:51:38 +00:00
|
|
|
if(topic.ToString().size())
|
2018-07-17 07:30:03 +00:00
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("n", topic, buf))
|
|
|
|
return false;
|
|
|
|
}
|
2018-09-23 13:57:21 +00:00
|
|
|
// Timestamp published
|
|
|
|
if(!BEncodeWriteDictInt("t", T, buf))
|
|
|
|
return false;
|
|
|
|
|
2018-07-06 16:08:30 +00:00
|
|
|
// write version
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
2018-07-06 16:08:30 +00:00
|
|
|
return false;
|
|
|
|
if(W)
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("w", *W, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(!BEncodeWriteDictEntry("z", Z, buf))
|
|
|
|
return false;
|
2018-07-05 15:44:06 +00:00
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
2018-07-17 06:17:13 +00:00
|
|
|
bool
|
2018-07-18 22:50:05 +00:00
|
|
|
IntroSet::HasExpiredIntros(llarp_time_t now) const
|
2018-07-17 06:17:13 +00:00
|
|
|
{
|
|
|
|
for(const auto& i : I)
|
|
|
|
if(now >= i.expiresAt)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-07-18 00:25:24 +00:00
|
|
|
bool
|
|
|
|
IntroSet::IsExpired(llarp_time_t now) const
|
|
|
|
{
|
2018-10-10 21:37:45 +00:00
|
|
|
return GetNewestIntroExpiration() < now;
|
2018-07-18 00:25:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:08:30 +00:00
|
|
|
Introduction::~Introduction()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-07-09 17:32:11 +00:00
|
|
|
Introduction::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
|
2018-07-06 16:08:30 +00:00
|
|
|
{
|
2018-07-09 17:32:11 +00:00
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("k", router, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("l", latency, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("p", pathID, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("x", expiresAt, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Introduction::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
2018-07-09 17:32:11 +00:00
|
|
|
|
2018-07-06 16:08:30 +00:00
|
|
|
if(!BEncodeWriteDictEntry("k", router, buf))
|
|
|
|
return false;
|
2018-07-09 17:32:11 +00:00
|
|
|
if(latency)
|
|
|
|
{
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("l", latency, buf))
|
2018-07-09 17:32:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-07-06 16:08:30 +00:00
|
|
|
if(!BEncodeWriteDictEntry("p", pathID, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
2018-07-06 16:08:30 +00:00
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("x", expiresAt, buf))
|
2018-07-06 16:08:30 +00:00
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
void
|
|
|
|
Introduction::Clear()
|
|
|
|
{
|
|
|
|
router.Zero();
|
|
|
|
pathID.Zero();
|
|
|
|
latency = 0;
|
|
|
|
expiresAt = 0;
|
|
|
|
}
|
|
|
|
|
2018-07-06 16:08:30 +00:00
|
|
|
Identity::~Identity()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Identity::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2018-07-27 00:21:57 +00:00
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("e", enckey, buf))
|
|
|
|
return false;
|
2018-08-13 23:22:31 +00:00
|
|
|
if(!BEncodeWriteDictEntry("q", pq, buf))
|
|
|
|
return false;
|
2018-07-27 00:21:57 +00:00
|
|
|
if(!BEncodeWriteDictEntry("s", signkey, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictInt("v", version, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("x", vanity, buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Identity::DecodeKey(llarp_buffer_t key, llarp_buffer_t* buf)
|
|
|
|
{
|
2018-07-27 00:21:57 +00:00
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, buf))
|
|
|
|
return false;
|
2018-08-13 23:22:31 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("q", pq, read, key, buf))
|
|
|
|
return false;
|
2018-07-27 00:21:57 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Identity::RegenerateKeys(llarp_crypto* crypto)
|
|
|
|
{
|
|
|
|
crypto->encryption_keygen(enckey);
|
|
|
|
crypto->identity_keygen(signkey);
|
2018-08-10 21:34:11 +00:00
|
|
|
pub.Update(llarp::seckey_topublic(enckey),
|
|
|
|
llarp::seckey_topublic(signkey));
|
2018-08-13 23:22:31 +00:00
|
|
|
crypto->pqe_keygen(pq);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Identity::KeyExchange(llarp_path_dh_func dh, byte_t* result,
|
|
|
|
const ServiceInfo& other, const byte_t* N) const
|
|
|
|
{
|
|
|
|
return dh(result, other.EncryptionPublicKey(), enckey, N);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Identity::Sign(llarp_crypto* c, byte_t* sig, llarp_buffer_t buf) const
|
|
|
|
{
|
|
|
|
return c->sign(sig, signkey, buf);
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-10 11:36:55 +00:00
|
|
|
bool
|
|
|
|
Identity::EnsureKeys(const std::string& fname, llarp_crypto* c)
|
|
|
|
{
|
2018-08-13 23:22:31 +00:00
|
|
|
byte_t tmp[4096];
|
2018-07-27 00:21:57 +00:00
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
std::error_code ec;
|
|
|
|
// check for file
|
|
|
|
if(!fs::exists(fname, ec))
|
|
|
|
{
|
|
|
|
if(ec)
|
|
|
|
{
|
|
|
|
llarp::LogError(ec);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// regen and encode
|
|
|
|
RegenerateKeys(c);
|
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
// write
|
|
|
|
std::ofstream f;
|
|
|
|
f.open(fname, std::ios::binary);
|
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
f.write((char*)buf.cur, buf.sz);
|
|
|
|
}
|
|
|
|
// read file
|
|
|
|
std::ifstream inf(fname, std::ios::binary);
|
|
|
|
inf.seekg(0, std::ios::end);
|
|
|
|
size_t sz = inf.tellg();
|
|
|
|
inf.seekg(0, std::ios::beg);
|
|
|
|
|
|
|
|
if(sz > sizeof(tmp))
|
|
|
|
return false;
|
|
|
|
// decode
|
|
|
|
inf.read((char*)buf.base, sz);
|
2018-09-08 16:45:38 +00:00
|
|
|
if(!BDecode(&buf))
|
|
|
|
return false;
|
2018-12-02 18:07:07 +00:00
|
|
|
|
|
|
|
const byte_t* ptr = nullptr;
|
2018-11-27 21:54:06 +00:00
|
|
|
if(!vanity.IsZero())
|
|
|
|
ptr = vanity.data();
|
2018-11-27 21:48:12 +00:00
|
|
|
// update pubkeys
|
2018-09-08 16:45:38 +00:00
|
|
|
pub.Update(llarp::seckey_topublic(enckey),
|
2018-11-27 21:54:06 +00:00
|
|
|
llarp::seckey_topublic(signkey), ptr);
|
2018-09-08 16:45:38 +00:00
|
|
|
return true;
|
2018-07-10 11:36:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-06 16:08:30 +00:00
|
|
|
bool
|
2018-10-29 23:56:05 +00:00
|
|
|
Identity::SignIntroSet(IntroSet& i, llarp_crypto* crypto,
|
|
|
|
llarp_time_t now) const
|
2018-07-06 16:08:30 +00:00
|
|
|
{
|
|
|
|
if(i.I.size() == 0)
|
|
|
|
return false;
|
2018-09-23 13:57:21 +00:00
|
|
|
// set timestamp
|
|
|
|
// TODO: round to nearest 1000 ms
|
2018-10-29 16:48:36 +00:00
|
|
|
i.T = now;
|
2018-08-13 23:22:31 +00:00
|
|
|
// set service info
|
2018-07-06 16:08:30 +00:00
|
|
|
i.A = pub;
|
2018-08-13 23:22:31 +00:00
|
|
|
// set public encryption key
|
|
|
|
i.K = pq_keypair_to_public(pq);
|
2018-07-06 16:08:30 +00:00
|
|
|
// zero out signature for signing process
|
|
|
|
i.Z.Zero();
|
|
|
|
byte_t tmp[MAX_INTROSET_SIZE];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(!i.BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind and resize buffer
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
2018-08-13 23:22:31 +00:00
|
|
|
return Sign(crypto, i.Z, buf);
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2018-10-29 16:48:36 +00:00
|
|
|
IntroSet::Verify(llarp_crypto* crypto, llarp_time_t now) const
|
2018-07-06 16:08:30 +00:00
|
|
|
{
|
|
|
|
byte_t tmp[MAX_INTROSET_SIZE];
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
IntroSet copy;
|
|
|
|
copy = *this;
|
|
|
|
copy.Z.Zero();
|
|
|
|
if(!copy.BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
// rewind and resize buffer
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
2018-09-20 11:27:18 +00:00
|
|
|
if(!A.Verify(crypto, buf, Z))
|
|
|
|
return false;
|
|
|
|
// validate PoW
|
2018-10-29 16:48:36 +00:00
|
|
|
if(W && !W->IsValid(crypto->shorthash, now))
|
2018-09-20 11:27:18 +00:00
|
|
|
return false;
|
|
|
|
// valid timestamps
|
2018-09-21 15:24:00 +00:00
|
|
|
// add max clock skew
|
|
|
|
now += MAX_INTROSET_TIME_DELTA;
|
2018-09-20 11:27:18 +00:00
|
|
|
for(const auto& intro : I)
|
|
|
|
{
|
2018-09-20 11:50:07 +00:00
|
|
|
if(intro.expiresAt > now
|
2018-09-20 11:27:18 +00:00
|
|
|
&& intro.expiresAt - now > DEFAULT_PATH_LIFETIME)
|
|
|
|
{
|
|
|
|
if(W && intro.expiresAt - W->extendedLifetime > DEFAULT_PATH_LIFETIME)
|
|
|
|
return false;
|
|
|
|
else if(W == nullptr)
|
2018-09-20 11:50:07 +00:00
|
|
|
{
|
|
|
|
llarp::LogWarn("intro has too high expire time");
|
2018-09-20 11:27:18 +00:00
|
|
|
return false;
|
2018-09-20 11:50:07 +00:00
|
|
|
}
|
2018-09-20 11:27:18 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-20 11:50:07 +00:00
|
|
|
if(IsExpired(now))
|
|
|
|
{
|
2018-10-19 11:34:27 +00:00
|
|
|
llarp::LogWarn("introset expired: ", *this);
|
2018-09-20 11:50:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-06 16:08:30 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 11:41:10 +00:00
|
|
|
llarp_time_t
|
|
|
|
IntroSet::GetNewestIntroExpiration() const
|
|
|
|
{
|
|
|
|
llarp_time_t t = 0;
|
|
|
|
for(const auto& intro : I)
|
|
|
|
t = std::max(intro.expiresAt, t);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:36:55 +00:00
|
|
|
bool
|
|
|
|
Config::Load(const std::string& fname)
|
|
|
|
{
|
2018-07-11 16:11:19 +00:00
|
|
|
ini::Parser parser(fname);
|
|
|
|
for(const auto& sec : parser.top().ordered_sections)
|
|
|
|
{
|
|
|
|
services.push_back({sec->first, sec->second.values});
|
|
|
|
}
|
|
|
|
return services.size() > 0;
|
2018-07-10 11:36:55 +00:00
|
|
|
}
|
|
|
|
|
2018-07-05 15:44:06 +00:00
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|