2021-03-09 22:24:35 +00:00
|
|
|
#include "info.hpp"
|
2019-01-10 19:41:51 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/crypto.hpp>
|
|
|
|
#include "address.hpp"
|
|
|
|
#include <llarp/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>
|
2020-01-27 21:30:41 +00:00
|
|
|
#include <sodium/crypto_sign_ed25519.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
|
2020-04-07 18:38:56 +00:00
|
|
|
ServiceInfo::Verify(const llarp_buffer_t& payload, const Signature& sig) const
|
2019-01-13 16:30:07 +00:00
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
return CryptoManager::instance()->verify(signkey, payload, sig);
|
2019-01-13 16:30:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-27 21:30:41 +00:00
|
|
|
bool
|
2020-05-20 22:48:13 +00:00
|
|
|
ServiceInfo::Update(
|
|
|
|
const byte_t* sign, const byte_t* enc, const std::optional<VanityNonce>& nonce)
|
2020-01-27 21:30:41 +00:00
|
|
|
{
|
2020-02-03 22:20:56 +00:00
|
|
|
signkey = sign;
|
2020-04-07 18:38:56 +00:00
|
|
|
enckey = enc;
|
|
|
|
if (nonce)
|
2020-01-27 21:30:41 +00:00
|
|
|
{
|
2020-05-20 19:46:08 +00:00
|
|
|
vanity = *nonce;
|
2020-01-27 21:30:41 +00:00
|
|
|
}
|
|
|
|
return UpdateAddr();
|
|
|
|
}
|
|
|
|
|
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;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("e", enckey, read, key, val))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("s", signkey, read, key, val))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictInt("v", version, read, key, val))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("x", vanity, read, key, val))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("e", enckey, buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("s", signkey, buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2022-05-26 15:59:44 +00:00
|
|
|
if (!BEncodeWriteDictInt("v", llarp::constants::proto_version, buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!vanity.IsZero())
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncodeWriteDictEntry("x", vanity, buf))
|
2018-07-16 03:32:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ServiceInfo::Name() const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_CachedAddr.IsZero())
|
2018-07-18 03:10:21 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
bool
|
|
|
|
ServiceInfo::CalculateAddress(std::array<byte_t, 32>& data) const
|
2018-07-16 03:32:13 +00:00
|
|
|
{
|
2020-01-27 21:30:41 +00:00
|
|
|
data = signkey.as_array();
|
|
|
|
return true;
|
2018-07-16 03:32:13 +00:00
|
|
|
}
|
2018-07-18 03:10:21 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
ServiceInfo::UpdateAddr()
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (m_CachedAddr.IsZero())
|
2019-12-14 18:50:52 +00:00
|
|
|
{
|
|
|
|
return CalculateAddress(m_CachedAddr.as_array());
|
|
|
|
}
|
|
|
|
return true;
|
2018-07-18 03:10:21 +00:00
|
|
|
}
|
|
|
|
|
2022-07-16 00:41:14 +00:00
|
|
|
std::string
|
|
|
|
ServiceInfo::ToString() const
|
|
|
|
{
|
2022-07-18 17:56:09 +00:00
|
|
|
return fmt::format("[ServiceInfo e={} s={} v={} x={}]", enckey, signkey, version, vanity);
|
2022-07-16 00:41:14 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 03:32:13 +00:00
|
|
|
} // namespace service
|
2018-10-23 11:29:37 +00:00
|
|
|
} // namespace llarp
|