2021-03-09 22:24:35 +00:00
|
|
|
#include "link_intro.hpp"
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/crypto.hpp>
|
|
|
|
#include <llarp/router_contact.hpp>
|
|
|
|
#include <llarp/router/abstractrouter.hpp>
|
|
|
|
#include <llarp/util/bencode.h>
|
|
|
|
#include <llarp/util/logging/logger.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
bool
|
2019-02-01 01:58:06 +00:00
|
|
|
LinkIntroMessage::DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "a")
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
llarp_buffer_t strbuf;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (strbuf.sz != 1)
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return *strbuf.cur == 'i';
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "n")
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (N.BDecode(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return true;
|
|
|
|
llarp::LogWarn("failed to decode nonce in LIM");
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "p")
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
return bencode_read_integer(buf, &P);
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "r")
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (rc.BDecode(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return true;
|
|
|
|
llarp::LogWarn("failed to decode RC in LIM");
|
|
|
|
llarp::DumpBuffer(*buf);
|
|
|
|
return false;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "v")
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_read_integer(buf, &version))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (version != LLARP_PROTO_VERSION)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-05-14 01:35:15 +00:00
|
|
|
llarp::LogWarn("llarp protocol version mismatch ", version, " != ", LLARP_PROTO_VERSION);
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::LogDebug("LIM version ", version);
|
|
|
|
return true;
|
|
|
|
}
|
2020-04-07 18:38:56 +00:00
|
|
|
if (key == "z")
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
return Z.BDecode(buf);
|
|
|
|
}
|
2019-07-30 23:42:13 +00:00
|
|
|
|
|
|
|
llarp::LogWarn("invalid LIM key: ", *key.cur);
|
|
|
|
return false;
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
LinkIntroMessage::BEncode(llarp_buffer_t* buf) const
|
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_start_dict(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "a", 1))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "i", 1))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "n", 1))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!N.BEncode(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "p", 1))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_uint64(buf, P))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "r", 1))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!rc.BEncode(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_uint64_entry(buf, "v", 1, LLARP_PROTO_VERSION))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!bencode_write_bytestring(buf, "z", 1))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!Z.BEncode(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return bencode_end(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-02-23 02:21:38 +00:00
|
|
|
LinkIntroMessage::HandleMessage(AbstractRouter* /*router*/) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!Verify())
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return session->GotLIM(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
LinkIntroMessage::Clear()
|
|
|
|
{
|
|
|
|
P = 0;
|
|
|
|
N.Zero();
|
|
|
|
rc.Clear();
|
|
|
|
Z.Zero();
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
LinkIntroMessage::Sign(std::function<bool(Signature&, const llarp_buffer_t&)> signer)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
Z.Zero();
|
2020-04-07 18:38:56 +00:00
|
|
|
std::array<byte_t, MaxSize> tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!BEncode(&buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2019-01-14 21:46:07 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
return signer(Z, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
LinkIntroMessage::Verify() const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
LinkIntroMessage copy;
|
|
|
|
copy = *this;
|
|
|
|
copy.Z.Zero();
|
2020-04-07 18:38:56 +00:00
|
|
|
std::array<byte_t, MaxSize> tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!copy.BEncode(&buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
2020-04-07 18:38:56 +00:00
|
|
|
buf.sz = buf.cur - buf.base;
|
2019-01-14 21:46:07 +00:00
|
|
|
buf.cur = buf.base;
|
|
|
|
// outer signature
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!CryptoManager::instance()->verify(rc.pubkey, buf, Z))
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("outer signature failure");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// verify RC
|
2020-04-07 18:38:56 +00:00
|
|
|
if (!rc.Verify(llarp::time_now_ms()))
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
llarp::LogError("invalid RC in link intro");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|