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>
|
2022-07-16 00:41:14 +00:00
|
|
|
#include <llarp/util/logging.hpp>
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
#include <oxenc/bt_producer.h>
|
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
LinkIntroMessage::decode_key(const llarp_buffer_t& key, llarp_buffer_t* buf)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("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';
|
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("n"))
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
if (nonce.BDecode(buf))
|
2019-01-14 21:46:07 +00:00
|
|
|
return true;
|
|
|
|
llarp::LogWarn("failed to decode nonce in LIM");
|
|
|
|
return false;
|
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("p"))
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
return bencode_read_integer(buf, &session_period);
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("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;
|
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("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;
|
2022-05-26 15:59:44 +00:00
|
|
|
if (version != llarp::constants::proto_version)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2022-05-26 15:59:44 +00:00
|
|
|
llarp::LogWarn(
|
|
|
|
"llarp protocol version mismatch ", version, " != ", llarp::constants::proto_version);
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
llarp::LogDebug("LIM version ", version);
|
|
|
|
return true;
|
|
|
|
}
|
2022-09-09 21:48:38 +00:00
|
|
|
if (key.startswith("z"))
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
return sig.BDecode(buf);
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
std::string
|
|
|
|
LinkIntroMessage::bt_encode() const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
oxenc::bt_dict_producer btdp;
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
btdp.append("a", "i");
|
|
|
|
btdp.append("n", nonce.ToView());
|
|
|
|
btdp.append("p", session_period);
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
{
|
|
|
|
auto subdict = btdp.append_list("r");
|
|
|
|
rc.bt_encode_subdict(subdict);
|
|
|
|
}
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
btdp.append("v", llarp::constants::proto_version);
|
|
|
|
btdp.append("z", sig.ToView());
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
log::critical(link_cat, "Error: LinkIntroMessage failed to bt encode contents!");
|
|
|
|
}
|
2019-01-14 21:46:07 +00:00
|
|
|
|
2023-08-29 14:26:59 +00:00
|
|
|
return std::move(btdp).str();
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
LinkIntroMessage::handle_message(AbstractRouter* /*router*/) const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
if (!verify())
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
return session->GotLIM(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-08-29 14:26:59 +00:00
|
|
|
LinkIntroMessage::clear()
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
session_period = 0;
|
|
|
|
nonce.Zero();
|
2019-01-14 21:46:07 +00:00
|
|
|
rc.Clear();
|
2023-08-29 14:26:59 +00:00
|
|
|
sig.Zero();
|
2019-11-03 15:31:01 +00:00
|
|
|
version = 0;
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
LinkIntroMessage::sign(std::function<bool(Signature&, const llarp_buffer_t&)> signer)
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
sig.Zero();
|
|
|
|
// need to keep this as a llarp_buffer_t for now, as all the crypto code expects
|
|
|
|
// byte_t types -- fix this later
|
|
|
|
std::array<byte_t, MAX_MSG_SIZE> tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2023-08-29 14:26:59 +00:00
|
|
|
|
|
|
|
auto bte = bt_encode();
|
|
|
|
buf.write(bte.begin(), bte.end());
|
|
|
|
|
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;
|
2023-08-29 14:26:59 +00:00
|
|
|
|
|
|
|
return signer(sig, buf);
|
2019-01-14 21:46:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-08-29 14:26:59 +00:00
|
|
|
LinkIntroMessage::verify() const
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
|
|
|
LinkIntroMessage copy;
|
|
|
|
copy = *this;
|
2023-08-29 14:26:59 +00:00
|
|
|
copy.sig.Zero();
|
|
|
|
|
|
|
|
// need to keep this as a llarp_buffer_t for now, as all the crypto code expects
|
|
|
|
// byte_t types -- fix this later
|
|
|
|
std::array<byte_t, MAX_MSG_SIZE> tmp;
|
2019-02-02 23:12:42 +00:00
|
|
|
llarp_buffer_t buf(tmp);
|
2023-08-29 14:26:59 +00:00
|
|
|
|
|
|
|
auto bte = copy.bt_encode();
|
|
|
|
buf.write(bte.begin(), bte.end());
|
|
|
|
|
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;
|
2023-08-29 14:26:59 +00:00
|
|
|
|
2019-01-14 21:46:07 +00:00
|
|
|
// outer signature
|
2023-08-29 14:26:59 +00:00
|
|
|
if (!CryptoManager::instance()->verify(rc.pubkey, buf, sig))
|
2019-01-14 21:46:07 +00:00
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
log::error(link_cat, "Error: outer signature failed!");
|
2019-01-14 21:46:07 +00:00
|
|
|
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
|
|
|
{
|
2023-08-29 14:26:59 +00:00
|
|
|
log::error(link_cat, "Error: invalid RC in link intro!");
|
2019-01-14 21:46:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp
|