2021-03-09 22:24:35 +00:00
|
|
|
#pragma once
|
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include "identity.hpp"
|
|
|
|
#include "info.hpp"
|
|
|
|
#include "intro.hpp"
|
|
|
|
#include "protocol_type.hpp"
|
|
|
|
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/crypto/encrypted.hpp>
|
|
|
|
#include <llarp/crypto/types.hpp>
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <llarp/ev/ev.hpp>
|
|
|
|
#include <llarp/path/pathset.hpp>
|
2023-10-03 20:00:23 +00:00
|
|
|
#include <llarp/service/convotag.hpp>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include <llarp/util/bencode.hpp>
|
|
|
|
#include <llarp/util/time.hpp>
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2023-10-24 13:18:03 +00:00
|
|
|
#include <vector>
|
2023-10-19 21:59:57 +00:00
|
|
|
|
2019-01-13 16:30:07 +00:00
|
|
|
struct llarp_threadpool;
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
namespace llarp
|
|
|
|
{
|
2019-04-10 13:19:32 +00:00
|
|
|
namespace path
|
|
|
|
{
|
|
|
|
/// forward declare
|
|
|
|
struct Path;
|
|
|
|
} // namespace path
|
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
namespace service
|
|
|
|
{
|
2020-05-28 11:21:47 +00:00
|
|
|
struct Endpoint;
|
|
|
|
|
2018-09-21 13:52:10 +00:00
|
|
|
constexpr std::size_t MAX_PROTOCOL_MESSAGE_SIZE = 2048 * 2;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
|
|
|
/// inner message
|
2019-05-24 02:01:36 +00:00
|
|
|
struct ProtocolMessage
|
2018-07-19 04:58:39 +00:00
|
|
|
{
|
2018-08-09 19:02:17 +00:00
|
|
|
ProtocolMessage(const ConvoTag& tag);
|
2018-07-22 23:14:29 +00:00
|
|
|
ProtocolMessage();
|
2018-07-19 04:58:39 +00:00
|
|
|
~ProtocolMessage();
|
2021-03-08 20:48:11 +00:00
|
|
|
ProtocolType proto = ProtocolType::TrafficV4;
|
2020-02-24 19:40:45 +00:00
|
|
|
llarp_time_t queued = 0s;
|
2023-09-27 14:09:48 +00:00
|
|
|
std::vector<byte_t> payload; // encrypted AbstractLinkMessage
|
2018-07-22 23:14:29 +00:00
|
|
|
Introduction introReply;
|
|
|
|
ServiceInfo sender;
|
2020-05-28 11:21:47 +00:00
|
|
|
Endpoint* handler = nullptr;
|
2018-08-09 19:02:17 +00:00
|
|
|
ConvoTag tag;
|
2023-10-18 12:48:09 +00:00
|
|
|
std::chrono::milliseconds creation_time{time_now_ms()};
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2020-06-24 13:24:07 +00:00
|
|
|
/// encode metainfo for lmq endpoint auth
|
|
|
|
std::vector<char>
|
|
|
|
EncodeAuthInfo() const;
|
2020-06-17 13:07:05 +00:00
|
|
|
|
2018-07-19 04:58:39 +00:00
|
|
|
bool
|
2023-08-31 16:28:02 +00:00
|
|
|
decode_key(const llarp_buffer_t& key, llarp_buffer_t* val);
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
std::string
|
|
|
|
bt_encode() const;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
|
|
|
void
|
2023-10-18 12:48:09 +00:00
|
|
|
put_buffer(std::string buf);
|
2018-08-09 19:02:17 +00:00
|
|
|
|
|
|
|
static void
|
2020-04-07 18:38:56 +00:00
|
|
|
ProcessAsync(path::Path_ptr p, PathID_t from, std::shared_ptr<ProtocolMessage> self);
|
2019-05-22 16:20:03 +00:00
|
|
|
|
2019-05-22 16:20:50 +00:00
|
|
|
bool
|
2022-04-04 21:50:20 +00:00
|
|
|
operator>(const ProtocolMessage& other) const
|
2019-05-22 16:20:03 +00:00
|
|
|
{
|
2023-10-18 12:48:09 +00:00
|
|
|
return creation_time > other.creation_time;
|
2019-05-22 16:20:03 +00:00
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/// outer message
|
2023-10-18 12:48:09 +00:00
|
|
|
struct ProtocolFrameMessage
|
2018-07-22 23:14:29 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
PQCipherBlock cipher;
|
2023-09-27 14:09:48 +00:00
|
|
|
Encrypted<2048> enc;
|
2023-08-31 16:28:02 +00:00
|
|
|
uint64_t flag; // set to indicate in plaintext a nack, aka "dont try again"
|
2023-11-06 20:59:33 +00:00
|
|
|
SymmNonce nonce;
|
2023-08-31 16:28:02 +00:00
|
|
|
Signature sig;
|
|
|
|
PathID_t path_id;
|
|
|
|
service::ConvoTag convo_tag;
|
|
|
|
|
2023-10-18 12:48:09 +00:00
|
|
|
ProtocolFrameMessage(const ProtocolFrameMessage& other) = default;
|
2018-09-17 13:28:26 +00:00
|
|
|
|
2023-10-18 12:48:09 +00:00
|
|
|
ProtocolFrameMessage()
|
2018-09-17 16:12:42 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
clear();
|
2018-09-17 16:12:42 +00:00
|
|
|
}
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2023-10-18 12:48:09 +00:00
|
|
|
~ProtocolFrameMessage() = default;
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2018-09-17 15:32:37 +00:00
|
|
|
bool
|
2023-08-31 16:28:02 +00:00
|
|
|
operator==(const ProtocolFrameMessage& other) const;
|
2018-09-17 15:32:37 +00:00
|
|
|
|
|
|
|
bool
|
2023-08-31 16:28:02 +00:00
|
|
|
operator!=(const ProtocolFrameMessage& other) const
|
2018-09-17 15:32:37 +00:00
|
|
|
{
|
|
|
|
return !(*this == other);
|
|
|
|
}
|
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
ProtocolFrameMessage&
|
2023-10-18 12:48:09 +00:00
|
|
|
operator=(const ProtocolFrameMessage& other) = default;
|
2018-08-14 21:17:18 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
EncryptAndSign(
|
|
|
|
const ProtocolMessage& msg, const SharedSecret& sharedkey, const Identity& localIdent);
|
2018-07-22 23:14:29 +00:00
|
|
|
|
2019-03-08 16:00:45 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Sign(const Identity& localIdent);
|
2019-03-08 16:00:45 +00:00
|
|
|
|
2018-08-09 19:02:17 +00:00
|
|
|
bool
|
2019-07-09 13:47:24 +00:00
|
|
|
AsyncDecryptAndVerify(
|
2021-03-02 15:23:38 +00:00
|
|
|
EventLoop_ptr loop,
|
2020-04-07 18:38:56 +00:00
|
|
|
path::Path_ptr fromPath,
|
|
|
|
const Identity& localIdent,
|
2021-01-01 18:55:31 +00:00
|
|
|
Endpoint* handler,
|
|
|
|
std::function<void(std::shared_ptr<ProtocolMessage>)> hook = nullptr) const;
|
2018-08-09 19:02:17 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2020-04-07 18:38:56 +00:00
|
|
|
DecryptPayloadInto(const SharedSecret& sharedkey, ProtocolMessage& into) const;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2023-10-18 12:48:09 +00:00
|
|
|
decode_key(const llarp_buffer_t& key, llarp_buffer_t* val);
|
2018-07-20 04:50:28 +00:00
|
|
|
|
2023-08-31 16:28:02 +00:00
|
|
|
/** Note: this method needs to be re-examined where it is called in the other class methods,
|
|
|
|
like ::Sign(), ::EncryptAndSign(), and ::Verify(). In all 3 of these cases, the subsequent
|
|
|
|
methods that the llarp_buffer_t is passed to must be refactored to take either a string, a
|
|
|
|
redesigned llarp_buffer, or some span backport.
|
|
|
|
*/
|
|
|
|
std::string
|
2023-10-18 12:48:09 +00:00
|
|
|
bt_encode() const;
|
2018-07-19 04:58:39 +00:00
|
|
|
|
2018-12-27 14:32:37 +00:00
|
|
|
void
|
2023-10-18 12:48:09 +00:00
|
|
|
clear()
|
2018-12-27 14:32:37 +00:00
|
|
|
{
|
2023-08-31 16:28:02 +00:00
|
|
|
cipher.Zero();
|
|
|
|
enc.Clear();
|
|
|
|
path_id.Zero();
|
|
|
|
convo_tag.Zero();
|
|
|
|
nonce.Zero();
|
|
|
|
sig.Zero();
|
|
|
|
flag = 0;
|
2018-12-27 14:32:37 +00:00
|
|
|
}
|
|
|
|
|
2018-07-22 23:14:29 +00:00
|
|
|
bool
|
2019-05-28 19:45:08 +00:00
|
|
|
Verify(const ServiceInfo& from) const;
|
2018-07-23 07:38:29 +00:00
|
|
|
|
|
|
|
bool
|
2023-10-18 12:48:09 +00:00
|
|
|
handle_message(Router* r) const;
|
2018-07-19 04:58:39 +00:00
|
|
|
};
|
|
|
|
} // namespace service
|
|
|
|
} // namespace llarp
|