2021-03-09 22:24:35 +00:00
|
|
|
#include "protocol.hpp"
|
2023-10-18 12:48:09 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "endpoint.hpp"
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/path/path.hpp>
|
2023-09-15 14:55:32 +00:00
|
|
|
#include <llarp/router/router.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/util/buffer.hpp>
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <utility>
|
2023-10-19 21:59:57 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
namespace llarp::service
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
ProtocolMessage::ProtocolMessage()
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
tag.Zero();
|
|
|
|
}
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
ProtocolMessage::ProtocolMessage(const ConvoTag& t) : tag(t)
|
|
|
|
{}
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
ProtocolMessage::~ProtocolMessage() = default;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
void
|
2023-10-18 12:48:09 +00:00
|
|
|
ProtocolMessage::put_buffer(std::string buf)
|
2023-08-31 16:28:02 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
payload.resize(buf.size());
|
|
|
|
memcpy(payload.data(), buf.data(), buf.size());
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
void
|
|
|
|
ProtocolMessage::ProcessAsync(
|
|
|
|
path::Path_ptr path, PathID_t from, std::shared_ptr<ProtocolMessage> self)
|
|
|
|
{
|
|
|
|
if (!self->handler->HandleDataMessage(path, from, self))
|
2023-10-04 13:25:25 +00:00
|
|
|
LogWarn("failed to handle data message from ", path->name());
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
bool
|
|
|
|
ProtocolMessage::decode_key(const llarp_buffer_t& k, llarp_buffer_t* buf)
|
|
|
|
{
|
|
|
|
bool read = false;
|
2023-10-24 19:25:42 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
if (k.startswith("d"))
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if (!bencode_read_string(buf, &strbuf))
|
2018-08-09 19:02:17 +00:00
|
|
|
return false;
|
2023-10-18 12:48:09 +00:00
|
|
|
put_buffer(strbuf.to_string());
|
2023-08-31 16:28:02 +00:00
|
|
|
return true;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("i", introReply, read, k, buf))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("s", sender, read, k, buf))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("t", tag, read, k, buf))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ProtocolMessage::bt_encode() const
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_producer btdp;
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
try
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
// btdp.append("a", static_cast<uint64_t>(proto));
|
2023-08-31 16:28:02 +00:00
|
|
|
|
2020-06-17 13:07:05 +00:00
|
|
|
if (not payload.empty())
|
2023-08-31 16:28:02 +00:00
|
|
|
btdp.append(
|
|
|
|
"d", std::string_view{reinterpret_cast<const char*>(payload.data()), payload.size()});
|
|
|
|
|
2020-06-17 13:07:05 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
auto subdict = btdp.append_dict("i");
|
|
|
|
introReply.bt_encode(subdict);
|
2020-06-17 13:07:05 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
auto subdict = btdp.append_dict("s");
|
|
|
|
sender.bt_encode(subdict);
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
btdp.append("t", tag.ToView());
|
|
|
|
}
|
|
|
|
catch (...)
|
2020-06-17 13:07:05 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
log::critical(logcat, "Error: ProtocolMessage failed to bt encode contents!");
|
2020-06-17 13:07:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
return std::move(btdp).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<char>
|
|
|
|
ProtocolMessage::EncodeAuthInfo() const
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_producer btdp;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
try
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
// btdp.append("a", static_cast<uint64_t>(proto));
|
2018-08-13 23:22:31 +00:00
|
|
|
|
2019-03-08 16:00:45 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
auto subdict = btdp.append_dict("s");
|
|
|
|
sender.bt_encode(subdict);
|
2019-03-08 16:00:45 +00:00
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
btdp.append("t", tag.ToView());
|
|
|
|
}
|
|
|
|
catch (...)
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
log::critical(logcat, "Error: ProtocolMessage failed to bt encode auth info");
|
2018-07-22 23:14:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
auto view = btdp.view();
|
|
|
|
std::vector<char> data;
|
|
|
|
data.resize(view.size());
|
|
|
|
|
|
|
|
std::copy_n(view.data(), view.size(), data.data());
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string
|
|
|
|
ProtocolFrameMessage::bt_encode() const
|
|
|
|
{
|
|
|
|
oxenc::bt_dict_producer btdp;
|
|
|
|
|
|
|
|
try
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
btdp.append("A", "H");
|
|
|
|
btdp.append("C", cipher.ToView());
|
|
|
|
btdp.append("D", std::string_view{reinterpret_cast<const char*>(enc.data()), enc.size()});
|
|
|
|
btdp.append("F", path_id.ToView());
|
|
|
|
btdp.append("N", nonce.ToView());
|
|
|
|
btdp.append("R", flag);
|
|
|
|
btdp.append("T", convo_tag.ToView());
|
|
|
|
btdp.append("Z", sig.ToView());
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
catch (...)
|
2019-03-08 16:00:45 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
log::critical(logcat, "Error: ProtocolFrameMessage failed to bt encode contents!");
|
2019-03-08 16:00:45 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
return std::move(btdp).str();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::decode_key(const llarp_buffer_t& key, llarp_buffer_t* val)
|
|
|
|
{
|
|
|
|
bool read = false;
|
|
|
|
if (key.startswith("A"))
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
llarp_buffer_t strbuf;
|
|
|
|
if (!bencode_read_string(val, &strbuf))
|
2018-08-14 21:17:18 +00:00
|
|
|
return false;
|
2023-08-31 16:28:02 +00:00
|
|
|
if (strbuf.sz != 1)
|
2018-09-19 17:04:55 +00:00
|
|
|
return false;
|
2023-08-31 16:28:02 +00:00
|
|
|
return *strbuf.cur == 'H';
|
2018-07-22 23:14:29 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
if (!BEncodeMaybeReadDictEntry("D", enc, read, key, val))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("F", path_id, read, key, val))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("C", cipher, read, key, val))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("N", nonce, read, key, val))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictInt("R", flag, read, key, val))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("T", convo_tag, read, key, val))
|
|
|
|
return false;
|
|
|
|
if (!BEncodeMaybeReadDictEntry("Z", sig, read, key, val))
|
|
|
|
return false;
|
|
|
|
return read;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::DecryptPayloadInto(
|
|
|
|
const SharedSecret& sharedkey, ProtocolMessage& msg) const
|
|
|
|
{
|
2023-09-27 14:09:48 +00:00
|
|
|
Encrypted<2048> tmp = enc;
|
2023-10-19 23:09:22 +00:00
|
|
|
crypto::xchacha20(tmp.data(), tmp.size(), sharedkey, nonce);
|
2023-09-27 14:09:48 +00:00
|
|
|
|
|
|
|
return bencode_decode_dict(msg, tmp.Buffer());
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::Sign(const Identity& localIdent)
|
|
|
|
{
|
|
|
|
sig.Zero();
|
|
|
|
std::array<byte_t, MAX_PROTOCOL_MESSAGE_SIZE> tmp;
|
|
|
|
llarp_buffer_t buf(tmp);
|
|
|
|
// encode
|
|
|
|
auto bte = bt_encode();
|
|
|
|
buf.write(bte.begin(), bte.end());
|
|
|
|
|
|
|
|
// rewind
|
|
|
|
buf.sz = buf.cur - buf.base;
|
|
|
|
buf.cur = buf.base;
|
|
|
|
// sign
|
2023-09-27 14:09:48 +00:00
|
|
|
return localIdent.Sign(sig, reinterpret_cast<uint8_t*>(bte.data()), bte.size());
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::EncryptAndSign(
|
|
|
|
const ProtocolMessage& msg, const SharedSecret& sessionKey, const Identity& localIdent)
|
|
|
|
{
|
|
|
|
// encode message
|
|
|
|
auto bte1 = msg.bt_encode();
|
|
|
|
// encrypt
|
2023-10-23 22:58:58 +00:00
|
|
|
crypto::xchacha20(reinterpret_cast<uint8_t*>(bte1.data()), bte1.size(), sessionKey, nonce);
|
2023-08-31 16:28:02 +00:00
|
|
|
// put encrypted buffer
|
2023-09-27 14:09:48 +00:00
|
|
|
std::memcpy(enc.data(), bte1.data(), bte1.size());
|
2023-08-31 16:28:02 +00:00
|
|
|
// zero out signature
|
|
|
|
sig.Zero();
|
|
|
|
|
|
|
|
auto bte2 = bt_encode();
|
|
|
|
// sign
|
2023-09-27 14:09:48 +00:00
|
|
|
if (!localIdent.Sign(sig, reinterpret_cast<uint8_t*>(bte2.data()), bte2.size()))
|
2018-08-14 21:17:18 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
LogError("failed to sign? wtf?!");
|
|
|
|
return false;
|
2018-08-14 21:17:18 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-08-14 21:17:18 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
struct AsyncFrameDecrypt
|
|
|
|
{
|
|
|
|
path::Path_ptr path;
|
|
|
|
EventLoop_ptr loop;
|
|
|
|
std::shared_ptr<ProtocolMessage> msg;
|
|
|
|
const Identity& m_LocalIdentity;
|
|
|
|
Endpoint* handler;
|
|
|
|
const ProtocolFrameMessage frame;
|
|
|
|
const Introduction fromIntro;
|
|
|
|
|
|
|
|
AsyncFrameDecrypt(
|
|
|
|
EventLoop_ptr l,
|
2020-04-07 18:38:56 +00:00
|
|
|
const Identity& localIdent,
|
2023-08-31 16:28:02 +00:00
|
|
|
Endpoint* h,
|
|
|
|
std::shared_ptr<ProtocolMessage> m,
|
|
|
|
const ProtocolFrameMessage& f,
|
|
|
|
const Introduction& recvIntro)
|
|
|
|
: loop(std::move(l))
|
|
|
|
, msg(std::move(m))
|
|
|
|
, m_LocalIdentity(localIdent)
|
|
|
|
, handler(h)
|
|
|
|
, frame(f)
|
|
|
|
, fromIntro(recvIntro)
|
|
|
|
{}
|
|
|
|
|
|
|
|
static void
|
|
|
|
Work(std::shared_ptr<AsyncFrameDecrypt> self)
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
SharedSecret K;
|
2023-09-27 14:09:48 +00:00
|
|
|
SharedSecret shared_key;
|
2023-08-31 16:28:02 +00:00
|
|
|
// copy
|
|
|
|
ProtocolFrameMessage frame(self->frame);
|
2023-10-19 23:09:22 +00:00
|
|
|
if (!crypto::pqe_decrypt(
|
2023-10-31 20:49:01 +00:00
|
|
|
self->frame.cipher, K, pq_keypair_to_seckey(self->m_LocalIdentity.pq)))
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
LogError("pqke failed C=", self->frame.cipher);
|
|
|
|
self->msg.reset();
|
|
|
|
return;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
// decrypt
|
2023-09-27 14:09:48 +00:00
|
|
|
// auto buf = frame.enc.Buffer();
|
|
|
|
uint8_t* buf = frame.enc.data();
|
|
|
|
size_t sz = frame.enc.size();
|
2023-10-19 23:09:22 +00:00
|
|
|
crypto::xchacha20(buf, sz, K, self->frame.nonce);
|
2023-09-27 14:09:48 +00:00
|
|
|
|
|
|
|
auto bte = self->msg->bt_encode();
|
|
|
|
|
|
|
|
if (bte.empty())
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-09-27 14:09:48 +00:00
|
|
|
log::error(logcat, "Failed to decode inner protocol message");
|
2023-10-19 11:49:46 +00:00
|
|
|
// DumpBuffer(*buf);
|
2023-08-31 16:28:02 +00:00
|
|
|
self->msg.reset();
|
|
|
|
return;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2023-09-27 14:09:48 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
// verify signature of outer message after we parsed the inner message
|
|
|
|
if (!self->frame.Verify(self->msg->sender))
|
2021-03-27 18:54:09 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
LogError(
|
|
|
|
"intro frame has invalid signature Z=",
|
|
|
|
self->frame.sig,
|
|
|
|
" from ",
|
|
|
|
self->msg->sender.Addr());
|
|
|
|
Dump<MAX_PROTOCOL_MESSAGE_SIZE>(self->frame);
|
|
|
|
Dump<MAX_PROTOCOL_MESSAGE_SIZE>(*self->msg);
|
|
|
|
self->msg.reset();
|
|
|
|
return;
|
2021-03-27 18:54:09 +00:00
|
|
|
}
|
2019-11-28 23:08:02 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
if (self->handler->HasConvoTag(self->msg->tag))
|
2018-08-09 19:02:17 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
LogError("dropping duplicate convo tag T=", self->msg->tag);
|
|
|
|
// TODO: send convotag reset
|
|
|
|
self->msg.reset();
|
|
|
|
return;
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
|
|
|
|
// PKE (A, B, N)
|
2023-09-27 14:09:48 +00:00
|
|
|
SharedSecret shared_secret;
|
2023-11-06 21:02:20 +00:00
|
|
|
if (!crypto::dh_server(
|
|
|
|
shared_secret,
|
|
|
|
self->msg->sender.EncryptionPublicKey(),
|
|
|
|
self->m_LocalIdentity.enckey,
|
|
|
|
self->frame.nonce))
|
2021-03-27 18:54:09 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
LogError("x25519 key exchange failed");
|
|
|
|
Dump<MAX_PROTOCOL_MESSAGE_SIZE>(self->frame);
|
|
|
|
self->msg.reset();
|
|
|
|
return;
|
2021-03-27 18:54:09 +00:00
|
|
|
}
|
2023-09-27 14:09:48 +00:00
|
|
|
std::array<uint8_t, 64> tmp;
|
2023-08-31 16:28:02 +00:00
|
|
|
// K
|
2023-09-27 14:09:48 +00:00
|
|
|
std::memcpy(tmp.begin(), K.begin(), K.size());
|
2023-08-31 16:28:02 +00:00
|
|
|
// S = HS( K + PKE( A, B, N))
|
2023-09-27 14:09:48 +00:00
|
|
|
std::memcpy(tmp.begin() + 32, shared_secret.begin(), shared_secret.size());
|
|
|
|
|
2023-10-19 23:09:22 +00:00
|
|
|
crypto::shorthash(shared_key, tmp.data(), tmp.size());
|
2023-08-31 16:28:02 +00:00
|
|
|
|
|
|
|
std::shared_ptr<ProtocolMessage> msg = std::move(self->msg);
|
|
|
|
path::Path_ptr path = std::move(self->path);
|
|
|
|
const PathID_t from = self->frame.path_id;
|
|
|
|
msg->handler = self->handler;
|
|
|
|
self->handler->AsyncProcessAuthMessage(
|
|
|
|
msg,
|
2023-09-27 14:09:48 +00:00
|
|
|
[path, msg, from, handler = self->handler, fromIntro = self->fromIntro, shared_key](
|
2023-10-18 12:48:09 +00:00
|
|
|
std::string result, bool success) {
|
|
|
|
if (success)
|
2021-01-01 18:55:31 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
if (handler->WantsOutboundSession(msg->sender.Addr()))
|
|
|
|
{
|
|
|
|
handler->PutSenderFor(msg->tag, msg->sender, false);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
handler->PutSenderFor(msg->tag, msg->sender, true);
|
|
|
|
}
|
|
|
|
handler->PutReplyIntroFor(msg->tag, msg->introReply);
|
2023-09-27 14:09:48 +00:00
|
|
|
handler->PutCachedSessionKeyFor(msg->tag, shared_key);
|
2023-10-18 12:48:09 +00:00
|
|
|
handler->SendAuthResult(path, from, msg->tag, result, success);
|
|
|
|
|
|
|
|
log::info(
|
|
|
|
logcat, "Auth accepted for tag {} from sender {}", msg->tag, msg->sender.Addr());
|
2023-08-31 16:28:02 +00:00
|
|
|
ProtocolMessage::ProcessAsync(path, from, msg);
|
2021-01-01 18:55:31 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
else
|
2021-01-01 18:55:31 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
log::warning(logcat, "Auth invalid for tag {} (code: {})", msg->tag, result);
|
2021-01-01 18:55:31 +00:00
|
|
|
}
|
2023-10-18 12:48:09 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
handler->Pump(time_now_ms());
|
2021-01-01 18:55:31 +00:00
|
|
|
});
|
2018-08-09 19:02:17 +00:00
|
|
|
}
|
2023-08-31 16:28:02 +00:00
|
|
|
};
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
struct AsyncDecrypt
|
|
|
|
{
|
|
|
|
ServiceInfo si;
|
|
|
|
SharedSecret shared;
|
|
|
|
ProtocolFrameMessage frame;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::AsyncDecryptAndVerify(
|
|
|
|
EventLoop_ptr loop,
|
|
|
|
path::Path_ptr recvPath,
|
|
|
|
const Identity& localIdent,
|
|
|
|
Endpoint* handler,
|
|
|
|
std::function<void(std::shared_ptr<ProtocolMessage>)> hook) const
|
|
|
|
{
|
|
|
|
auto msg = std::make_shared<ProtocolMessage>();
|
|
|
|
msg->handler = handler;
|
|
|
|
if (convo_tag.IsZero())
|
2018-09-17 15:32:37 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
// we need to dh
|
|
|
|
auto dh = std::make_shared<AsyncFrameDecrypt>(
|
|
|
|
loop, localIdent, handler, msg, *this, recvPath->intro);
|
|
|
|
dh->path = recvPath;
|
2023-09-15 14:55:32 +00:00
|
|
|
handler->router()->queue_work([dh = std::move(dh)] { return AsyncFrameDecrypt::Work(dh); });
|
2023-08-31 16:28:02 +00:00
|
|
|
return true;
|
2018-09-17 15:32:37 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
auto v = std::make_shared<AsyncDecrypt>();
|
2018-09-17 15:32:37 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
if (!handler->GetCachedSessionKeyFor(convo_tag, v->shared))
|
|
|
|
{
|
|
|
|
LogError("No cached session for T=", convo_tag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (v->shared.IsZero())
|
|
|
|
{
|
|
|
|
LogError("bad cached session key for T=", convo_tag);
|
|
|
|
return false;
|
2018-07-22 23:14:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
if (!handler->GetSenderFor(convo_tag, v->si))
|
|
|
|
{
|
|
|
|
LogError("No sender for T=", convo_tag);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (v->si.Addr().IsZero())
|
2018-07-23 07:38:29 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
LogError("Bad sender for T=", convo_tag);
|
|
|
|
return false;
|
2018-07-23 07:38:29 +00:00
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
v->frame = *this;
|
|
|
|
auto callback = [loop, hook](std::shared_ptr<ProtocolMessage> msg) {
|
|
|
|
if (hook)
|
|
|
|
{
|
|
|
|
loop->call([msg, hook]() { hook(msg); });
|
|
|
|
}
|
|
|
|
};
|
2023-09-15 14:55:32 +00:00
|
|
|
handler->router()->queue_work(
|
2023-08-31 16:28:02 +00:00
|
|
|
[v, msg = std::move(msg), recvPath = std::move(recvPath), callback, handler]() {
|
|
|
|
auto resetTag =
|
|
|
|
[handler, tag = v->frame.convo_tag, from = v->frame.path_id, path = recvPath]() {
|
|
|
|
handler->ResetConvoTag(tag, path, from);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (not v->frame.Verify(v->si))
|
|
|
|
{
|
|
|
|
LogError("Signature failure from ", v->si.Addr());
|
|
|
|
handler->Loop()->call_soon(resetTag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (not v->frame.DecryptPayloadInto(v->shared, *msg))
|
|
|
|
{
|
|
|
|
LogError("failed to decrypt message from ", v->si.Addr());
|
|
|
|
handler->Loop()->call_soon(resetTag);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(msg);
|
|
|
|
RecvDataEvent ev;
|
|
|
|
ev.fromPath = std::move(recvPath);
|
|
|
|
ev.pathid = v->frame.path_id;
|
|
|
|
auto* handler = msg->handler;
|
|
|
|
ev.msg = std::move(msg);
|
|
|
|
handler->QueueRecvData(std::move(ev));
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::operator==(const ProtocolFrameMessage& other) const
|
|
|
|
{
|
|
|
|
return cipher == other.cipher && enc == other.enc && nonce == other.nonce && sig == other.sig
|
2023-10-18 12:48:09 +00:00
|
|
|
&& convo_tag == other.convo_tag;
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ProtocolFrameMessage::Verify(const ServiceInfo& svc) const
|
|
|
|
{
|
|
|
|
ProtocolFrameMessage copy(*this);
|
|
|
|
copy.sig.Zero();
|
|
|
|
|
|
|
|
auto bte = copy.bt_encode();
|
2023-09-27 14:09:48 +00:00
|
|
|
return svc.verify(reinterpret_cast<uint8_t*>(bte.data()), bte.size(), sig);
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2023-10-18 12:48:09 +00:00
|
|
|
ProtocolFrameMessage::handle_message(Router* /*r*/) const
|
2023-08-31 16:28:02 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
return true;
|
2023-08-31 16:28:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace llarp::service
|