2018-08-30 18:48:43 +00:00
|
|
|
#include <llarp/bencode.hpp>
|
|
|
|
#include <llarp/router_contact.hpp>
|
2018-05-10 23:32:46 +00:00
|
|
|
#include <llarp/version.h>
|
2018-06-12 11:57:14 +00:00
|
|
|
#include <llarp/crypto.hpp>
|
2018-10-08 11:56:17 +00:00
|
|
|
#include <llarp/time.h>
|
2018-10-15 12:02:32 +00:00
|
|
|
#include <llarp/net.hpp>
|
2018-05-27 13:42:55 +00:00
|
|
|
#include "buffer.hpp"
|
|
|
|
#include "logger.hpp"
|
2018-08-02 00:48:43 +00:00
|
|
|
#include "mem.hpp"
|
2018-05-27 13:42:55 +00:00
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
#include <fstream>
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
namespace llarp
|
2018-07-08 13:26:24 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
|
|
|
RouterContact::BEncode(llarp_buffer_t *buf) const
|
2018-05-13 18:07:36 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write dict begin */
|
|
|
|
if(!bencode_start_dict(buf))
|
2018-06-10 14:05:48 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write ai if they exist */
|
|
|
|
if(!bencode_write_bytestring(buf, "a", 1))
|
2018-05-13 18:07:36 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!BEncodeWriteList(addrs.begin(), addrs.end(), buf))
|
2018-05-13 18:07:36 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write signing pubkey */
|
|
|
|
if(!bencode_write_bytestring(buf, "k", 1))
|
2018-05-22 18:41:38 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!pubkey.BEncode(buf))
|
2018-05-13 18:07:36 +00:00
|
|
|
return false;
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
std::string nick = Nick();
|
|
|
|
if(nick.size())
|
2018-05-22 15:54:19 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write nickname */
|
|
|
|
if(!bencode_write_bytestring(buf, "n", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_bytestring(buf, nick.c_str(), nick.size()))
|
|
|
|
return false;
|
2018-05-22 15:54:19 +00:00
|
|
|
}
|
2018-05-13 18:07:36 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write encryption pubkey */
|
|
|
|
if(!bencode_write_bytestring(buf, "p", 1))
|
2018-05-13 18:07:36 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!enckey.BEncode(buf))
|
2018-05-13 18:07:36 +00:00
|
|
|
return false;
|
2018-05-27 13:42:55 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write last updated */
|
|
|
|
if(!bencode_write_bytestring(buf, "u", 1))
|
|
|
|
return false;
|
|
|
|
if(!bencode_write_uint64(buf, last_updated))
|
|
|
|
return false;
|
2018-05-13 18:07:36 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write version */
|
|
|
|
if(!bencode_write_version_entry(buf))
|
|
|
|
return false;
|
2018-05-25 17:52:10 +00:00
|
|
|
|
2018-05-10 23:32:46 +00:00
|
|
|
/* write ai if they exist */
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!bencode_write_bytestring(buf, "x", 1))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!BEncodeWriteList(exits.begin(), exits.end(), buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-06-10 14:05:48 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
/* write signature */
|
|
|
|
if(!bencode_write_bytestring(buf, "z", 1))
|
|
|
|
return false;
|
|
|
|
if(!signature.BEncode(buf))
|
|
|
|
return false;
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
2018-05-10 23:32:46 +00:00
|
|
|
|
2018-08-31 13:51:24 +00:00
|
|
|
void
|
|
|
|
RouterContact::Clear()
|
|
|
|
{
|
|
|
|
addrs.clear();
|
|
|
|
exits.clear();
|
|
|
|
signature.Zero();
|
|
|
|
nickname.Zero();
|
|
|
|
enckey.Zero();
|
|
|
|
pubkey.Zero();
|
|
|
|
last_updated = 0;
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
bool
|
|
|
|
RouterContact::DecodeKey(llarp_buffer_t key, llarp_buffer_t *buf)
|
2018-08-09 15:36:58 +00:00
|
|
|
{
|
2018-08-30 18:48:43 +00:00
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictList("a", addrs, read, key, buf))
|
2018-08-09 15:36:58 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("k", pubkey, read, key, buf))
|
2018-08-09 15:36:58 +00:00
|
|
|
return false;
|
2018-08-02 00:48:43 +00:00
|
|
|
|
2018-08-31 13:51:24 +00:00
|
|
|
if(llarp_buffer_eq(key, "n"))
|
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if(!bencode_read_string(buf, &strbuf))
|
|
|
|
return false;
|
|
|
|
if(strbuf.sz > nickname.size())
|
|
|
|
return false;
|
|
|
|
nickname.Zero();
|
|
|
|
memcpy(nickname.data(), strbuf.base, strbuf.sz);
|
|
|
|
return true;
|
|
|
|
}
|
2018-06-12 12:49:23 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!BEncodeMaybeReadDictEntry("p", enckey, read, key, buf))
|
|
|
|
return false;
|
2018-05-21 12:44:50 +00:00
|
|
|
|
2018-08-31 13:51:24 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, buf))
|
|
|
|
return false;
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!BEncodeMaybeReadDictInt("u", last_updated, read, key, buf))
|
|
|
|
return false;
|
2018-05-10 23:32:46 +00:00
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
if(!BEncodeMaybeReadDictList("x", exits, read, key, buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
|
|
|
|
if(!BEncodeMaybeReadDictEntry("z", signature, read, key, buf))
|
2018-05-22 15:54:19 +00:00
|
|
|
return false;
|
2018-08-30 18:48:43 +00:00
|
|
|
|
|
|
|
return read;
|
2018-05-10 23:32:46 +00:00
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
|
|
|
RouterContact::IsPublicRouter() const
|
|
|
|
{
|
|
|
|
return addrs.size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::HasNick() const
|
|
|
|
{
|
|
|
|
return nickname[0] != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RouterContact::SetNick(const std::string &nick)
|
|
|
|
{
|
|
|
|
nickname.Zero();
|
|
|
|
memcpy(nickname, nick.c_str(), std::min(nick.size(), nickname.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
RouterContact::Nick() const
|
|
|
|
{
|
|
|
|
const char *n = (const char *)nickname.data();
|
|
|
|
return std::string(n, strnlen(n, nickname.size()));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::Sign(llarp_crypto *crypto, const SecretKey &secretkey)
|
|
|
|
{
|
2018-08-31 13:51:24 +00:00
|
|
|
pubkey = llarp::seckey_topublic(secretkey);
|
2018-08-31 12:46:54 +00:00
|
|
|
byte_t tmp[MAX_RC_SIZE] = {0};
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
signature.Zero();
|
2018-08-31 13:51:24 +00:00
|
|
|
last_updated = llarp_time_now_ms();
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
return crypto->sign(signature, secretkey, buf);
|
|
|
|
}
|
|
|
|
|
2018-10-15 12:02:32 +00:00
|
|
|
bool
|
|
|
|
RouterContact::Verify(llarp_crypto *crypto) const
|
|
|
|
{
|
|
|
|
for(const auto &a : addrs)
|
|
|
|
{
|
|
|
|
if(IsBogon(a.ip))
|
|
|
|
{
|
|
|
|
llarp::LogError("invalid address info: ", a);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(const auto &exit : exits)
|
|
|
|
{
|
|
|
|
if(IsBogonRange(exit.address, exit.netmask))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return VerifySignature(crypto);
|
|
|
|
}
|
|
|
|
|
2018-08-31 12:46:54 +00:00
|
|
|
bool
|
|
|
|
RouterContact::VerifySignature(llarp_crypto *crypto) const
|
|
|
|
{
|
2018-08-31 13:51:24 +00:00
|
|
|
RouterContact copy;
|
|
|
|
copy = *this;
|
2018-08-31 12:46:54 +00:00
|
|
|
copy.signature.Zero();
|
|
|
|
byte_t tmp[MAX_RC_SIZE] = {0};
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(!copy.BEncode(&buf))
|
2018-08-31 13:51:24 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("bencode failed");
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2018-08-31 13:51:24 +00:00
|
|
|
}
|
2018-08-31 12:46:54 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
2018-08-31 13:51:24 +00:00
|
|
|
return crypto->verify(pubkey, buf, signature);
|
2018-08-31 12:46:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::Write(const char *fname) const
|
|
|
|
{
|
|
|
|
byte_t tmp[MAX_RC_SIZE] = {0};
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
{
|
|
|
|
std::ofstream f;
|
2018-08-31 13:51:24 +00:00
|
|
|
f.open(fname, std::ios::binary);
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!f.is_open())
|
|
|
|
return false;
|
|
|
|
f.write((char *)buf.base, buf.sz);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
RouterContact::Read(const char *fname)
|
|
|
|
{
|
|
|
|
byte_t tmp[MAX_RC_SIZE] = {0};
|
|
|
|
{
|
|
|
|
std::ifstream f;
|
2018-08-31 13:51:24 +00:00
|
|
|
f.open(fname, std::ios::binary);
|
2018-08-31 12:46:54 +00:00
|
|
|
if(!f.is_open())
|
2018-08-31 13:51:24 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("Failed to open ", fname);
|
2018-08-31 12:46:54 +00:00
|
|
|
return false;
|
2018-08-31 13:51:24 +00:00
|
|
|
}
|
2018-08-31 12:46:54 +00:00
|
|
|
f.seekg(0, std::ios::end);
|
|
|
|
auto l = f.tellg();
|
|
|
|
f.seekg(0, std::ios::beg);
|
|
|
|
f.read((char *)tmp, l);
|
|
|
|
}
|
|
|
|
auto buf = llarp::StackBuffer< decltype(tmp) >(tmp);
|
|
|
|
return BDecode(&buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
RouterContact &
|
|
|
|
RouterContact::operator=(const RouterContact &other)
|
|
|
|
{
|
2018-09-09 11:23:21 +00:00
|
|
|
addrs = other.addrs;
|
|
|
|
exits = other.exits;
|
|
|
|
signature = other.signature;
|
2018-08-31 12:46:54 +00:00
|
|
|
last_updated = other.last_updated;
|
|
|
|
enckey = other.enckey;
|
|
|
|
pubkey = other.pubkey;
|
|
|
|
nickname = other.nickname;
|
2018-08-31 13:51:24 +00:00
|
|
|
version = other.version;
|
2018-08-31 12:46:54 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-08-30 18:48:43 +00:00
|
|
|
} // namespace llarp
|