2019-04-22 18:35:19 +00:00
|
|
|
#include <service/info.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
#include <crypto/crypto.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
#include <service/address.hpp>
|
2019-01-10 19:41:51 +00:00
|
|
|
#include <util/buffer.hpp>
|
2018-12-12 02:15:08 +00:00
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
#include <cassert>
|
2018-10-23 12:40:34 +00:00
|
|
|
|
|
|
|
#include <sodium/crypto_generichash.h>
|
2018-12-12 02:15:08 +00:00
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
namespace service
|
|
|
|
{
|
2019-01-13 16:30:07 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
ServiceInfo::Verify(const llarp_buffer_t& payload,
|
2019-01-13 16:30:07 +00:00
|
|
|
const Signature& sig) const
|
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
return CryptoManager::instance()->verify(signkey, payload, sig);
|
2019-01-13 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
ServiceInfo::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* val)
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("e", enckey, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("s", signkey, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictInt("v", version, read, key, val))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeMaybeReadDictEntry("x", vanity, read, key, val))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
|
|
|
if(!bencode_start_dict(buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("e", enckey, buf))
|
|
|
|
return false;
|
|
|
|
if(!BEncodeWriteDictEntry("s", signkey, buf))
|
|
|
|
return false;
|
2018-07-23 21:59:43 +00:00
|
|
|
if(!BEncodeWriteDictInt("v", LLARP_PROTO_VERSION, buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
|
|
|
if(!vanity.IsZero())
|
|
|
|
{
|
|
|
|
if(!BEncodeWriteDictEntry("x", vanity, buf))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ServiceInfo::Name() const
|
|
|
|
{
|
2018-07-18 03:10:21 +00:00
|
|
|
if(m_CachedAddr.IsZero())
|
|
|
|
{
|
|
|
|
Address addr;
|
2019-01-02 01:03:53 +00:00
|
|
|
CalculateAddress(addr.as_array());
|
2018-07-18 03:10:21 +00:00
|
|
|
return addr.ToString();
|
|
|
|
}
|
|
|
|
return m_CachedAddr.ToString();
|
2018-07-16 03:32:13 +00:00
|
|
|
}
|
|
|
|
|
2019-12-22 14:16:28 +00:00
|
|
|
bool ServiceInfo::CalculateAddress(std::array< byte_t, 32 >& data) const
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
2019-02-02 23:12:42 +00:00
|
|
|
std::array< byte_t, 256 > tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
2018-08-10 21:34:11 +00:00
|
|
|
if(!BEncode(&buf))
|
|
|
|
return false;
|
2018-12-20 14:18:03 +00:00
|
|
|
return crypto_generichash_blake2b(data.data(), data.size(), buf.base,
|
|
|
|
buf.cur - buf.base, nullptr, 0)
|
2018-07-16 03:32:13 +00:00
|
|
|
!= -1;
|
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::UpdateAddr()
|
|
|
|
{
|
2019-12-14 18:50:52 +00:00
|
|
|
if(m_CachedAddr.IsZero())
|
|
|
|
{
|
|
|
|
return CalculateAddress(m_CachedAddr.as_array());
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 23:46:37 +00:00
|
|
|
std::ostream&
|
|
|
|
ServiceInfo::print(std::ostream& stream, int level, int spaces) const
|
|
|
|
{
|
|
|
|
Printer printer(stream, level, spaces);
|
|
|
|
printer.printAttribute("e", enckey);
|
|
|
|
printer.printAttribute("s", signkey);
|
|
|
|
printer.printAttribute("v", version);
|
|
|
|
printer.printAttribute("x", vanity);
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
} // namespace service
|
2018-10-23 11:29:37 +00:00
|
|
|
} // namespace llarp
|