15static const char *
const TAG =
"api.noise";
16static const char *
const PROLOGUE_INIT =
"NoiseAPIInit";
17static constexpr size_t PROLOGUE_INIT_LEN = 12;
19#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s: " msg, this->client_info_->get_combined_info().c_str(), ##__VA_ARGS__)
21#ifdef HELPER_LOG_PACKETS
22#define LOG_PACKET_RECEIVED(buffer) ESP_LOGVV(TAG, "Received frame: %s", format_hex_pretty(buffer).c_str())
23#define LOG_PACKET_SENDING(data, len) ESP_LOGVV(TAG, "Sending raw: %s", format_hex_pretty(data, len).c_str())
25#define LOG_PACKET_RECEIVED(buffer) ((void) 0)
26#define LOG_PACKET_SENDING(data, len) ((void) 0)
31 if (err == NOISE_ERROR_NO_MEMORY)
33 if (err == NOISE_ERROR_UNKNOWN_ID)
35 if (err == NOISE_ERROR_UNKNOWN_NAME)
36 return "UNKNOWN_NAME";
37 if (err == NOISE_ERROR_MAC_FAILURE)
39 if (err == NOISE_ERROR_NOT_APPLICABLE)
40 return "NOT_APPLICABLE";
41 if (err == NOISE_ERROR_SYSTEM)
43 if (err == NOISE_ERROR_REMOTE_KEY_REQUIRED)
44 return "REMOTE_KEY_REQUIRED";
45 if (err == NOISE_ERROR_LOCAL_KEY_REQUIRED)
46 return "LOCAL_KEY_REQUIRED";
47 if (err == NOISE_ERROR_PSK_REQUIRED)
48 return "PSK_REQUIRED";
49 if (err == NOISE_ERROR_INVALID_LENGTH)
50 return "INVALID_LENGTH";
51 if (err == NOISE_ERROR_INVALID_PARAM)
52 return "INVALID_PARAM";
53 if (err == NOISE_ERROR_INVALID_STATE)
54 return "INVALID_STATE";
55 if (err == NOISE_ERROR_INVALID_NONCE)
56 return "INVALID_NONCE";
57 if (err == NOISE_ERROR_INVALID_PRIVATE_KEY)
58 return "INVALID_PRIVATE_KEY";
59 if (err == NOISE_ERROR_INVALID_PUBLIC_KEY)
60 return "INVALID_PUBLIC_KEY";
61 if (err == NOISE_ERROR_INVALID_FORMAT)
62 return "INVALID_FORMAT";
63 if (err == NOISE_ERROR_INVALID_SIGNATURE)
64 return "INVALID_SIGNATURE";
65 return to_string(err);
77 prologue_.resize(old_size + PROLOGUE_INIT_LEN);
78 std::memcpy(
prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
137 if (frame ==
nullptr) {
138 HELPER_LOG(
"Bad argument for try_read_frame_");
152 if (
static_cast<uint8_t
>(received) != to_read) {
171 HELPER_LOG(
"Bad packet len for handshake: %d", msg_size);
176 if (
rx_buf_.size() != msg_size) {
189 if (
static_cast<uint16_t
>(received) != to_read) {
217 HELPER_LOG(
"Bad state for method: %d", (
int)
state_);
222 std::vector<uint8_t> frame;
230 prologue_.resize(old_size + 2 + frame.size());
231 prologue_[old_size] = (uint8_t) (frame.size() >> 8);
232 prologue_[old_size + 1] = (uint8_t) frame.size();
233 std::memcpy(
prologue_.data() + old_size + 2, frame.data(), frame.size());
242 std::vector<uint8_t> msg;
244 size_t name_len = name.size() + 1;
245 size_t mac_len = mac.size() + 1;
246 size_t name_offset = 1;
247 size_t mac_offset = name_offset + name_len;
248 size_t total_size = 1 + name_len + mac_len;
250 msg.resize(total_size);
256 std::memcpy(msg.data() + name_offset, name.c_str(), name_len);
258 std::memcpy(msg.data() + mac_offset, mac.c_str(), mac_len);
272 int action = noise_handshakestate_get_action(
handshake_);
273 if (action == NOISE_ACTION_READ_MESSAGE) {
275 std::vector<uint8_t> frame;
284 }
else if (frame[0] != 0x00) {
285 HELPER_LOG(
"Bad handshake error byte: %u", frame[0]);
291 noise_buffer_init(mbuf);
292 noise_buffer_set_input(mbuf, frame.data() + 1, frame.size() - 1);
293 err = noise_handshakestate_read_message(
handshake_, &mbuf,
nullptr);
303 }
else if (action == NOISE_ACTION_WRITE_MESSAGE) {
306 noise_buffer_init(mbuf);
307 noise_buffer_set_output(mbuf, buffer + 1,
sizeof(buffer) - 1);
309 err = noise_handshakestate_write_message(
handshake_, &mbuf,
nullptr);
325 HELPER_LOG(
"Bad action for handshake: %d", action);
335 std::vector<uint8_t> data;
336 data.resize(reason.length() + 1);
340 if (!reason.empty()) {
341 std::memcpy(data.data() + 1, reason.c_str(), reason.length());
362 std::vector<uint8_t> frame;
368 noise_buffer_init(mbuf);
369 noise_buffer_set_inout(mbuf, frame.data(), frame.size(), frame.size());
375 uint16_t msg_size = mbuf.size;
376 uint8_t *msg_data = frame.data();
379 HELPER_LOG(
"Bad data packet: size %d too short", msg_size);
383 uint16_t
type = (((uint16_t) msg_data[0]) << 8) | msg_data[1];
384 uint16_t data_len = (((uint16_t) msg_data[2]) << 8) | msg_data[3];
385 if (data_len > msg_size - 4) {
387 HELPER_LOG(
"Bad data packet: data_len %u greater than msg_size %u", data_len, msg_size);
415 if (packets.empty()) {
419 std::vector<uint8_t> *raw_buffer = buffer.
get_buffer();
420 uint8_t *buffer_data = raw_buffer->data();
424 uint16_t total_write_len = 0;
427 for (
const auto &packet : packets) {
429 uint8_t *buf_start = buffer_data + packet.offset;
436 const uint8_t msg_offset = 3;
437 buf_start[msg_offset] =
static_cast<uint8_t
>(packet.message_type >> 8);
438 buf_start[msg_offset + 1] =
static_cast<uint8_t
>(packet.message_type);
439 buf_start[msg_offset + 2] =
static_cast<uint8_t
>(packet.payload_size >> 8);
440 buf_start[msg_offset + 3] =
static_cast<uint8_t
>(packet.payload_size);
448 noise_buffer_init(mbuf);
449 noise_buffer_set_inout(mbuf, buf_start + msg_offset, 4 + packet.payload_size,
452 int err = noise_cipherstate_encrypt(
send_cipher_, &mbuf);
458 buf_start[1] =
static_cast<uint8_t
>(mbuf.size >> 8);
459 buf_start[2] =
static_cast<uint8_t
>(mbuf.size);
462 size_t packet_len =
static_cast<size_t>(3 + mbuf.size);
464 total_write_len += packet_len;
474 header[1] = (uint8_t) (
len >> 8);
475 header[2] = (uint8_t)
len;
483 iov[1].
iov_base =
const_cast<uint8_t *
>(data);
498 nid_.pattern_id = NOISE_PATTERN_NN;
499 nid_.cipher_id = NOISE_CIPHER_CHACHAPOLY;
500 nid_.dh_id = NOISE_DH_CURVE25519;
501 nid_.prefix_id = NOISE_PREFIX_STANDARD;
502 nid_.hybrid_id = NOISE_DH_NONE;
503 nid_.hash_id = NOISE_HASH_SHA256;
504 nid_.modifier_ids[0] = NOISE_MODIFIER_PSK0;
506 err = noise_handshakestate_new_by_id(&
handshake_, &
nid_, NOISE_ROLE_RESPONDER);
511 const auto &psk =
ctx_->get_psk();
512 err = noise_handshakestate_set_pre_shared_key(
handshake_, psk.data(), psk.size());
534 int action = noise_handshakestate_get_action(
handshake_);
535 if (action == NOISE_ACTION_READ_MESSAGE || action == NOISE_ACTION_WRITE_MESSAGE)
537 if (action != NOISE_ACTION_SPLIT) {
539 HELPER_LOG(
"Bad action for handshake: %d", action);
549 HELPER_LOG(
"Handshake complete!");
575 ESP_LOGE(TAG,
"Acquiring random bytes failed; rebooting");
const std::string & get_name() const
Get the name of this Application set by pre_setup().
APIError handle_socket_read_result_(ssize_t received)
std::vector< uint8_t > rx_buf_
std::vector< struct iovec > reusable_iovs_
uint8_t frame_header_padding_
uint8_t frame_footer_size_
APIError write_raw_(const struct iovec *iov, int iovcnt, uint16_t total_write_len)
APIError write_protobuf_packet(uint8_t type, ProtoWriteBuffer buffer) override
uint8_t rx_header_buf_len_
NoiseCipherState * send_cipher_
APIError read_packet(ReadPacketBuffer *buffer) override
APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span< const PacketInfo > packets) override
NoiseCipherState * recv_cipher_
~APINoiseFrameHelper() override
std::vector< uint8_t > prologue_
APIError try_read_frame_(std::vector< uint8_t > *frame)
Read a packet into the rx_buf_.
APIError state_action_()
To be called from read/write methods.
APIError handle_noise_error_(int err, const char *func_name, APIError api_err)
APIError loop() override
Run through handshake messages (if in that phase)
NoiseHandshakeState * handshake_
uint8_t rx_header_buf_[3]
APIError handle_handshake_frame_error_(APIError aerr)
APIError write_frame_(const uint8_t *data, uint16_t len)
std::shared_ptr< APINoiseContext > ctx_
APIError init() override
Initialize the frame helper, returns OK if successful.
APIError check_handshake_finished_()
void send_explicit_handshake_reject_(const std::string &reason)
APIError init_handshake_()
Initiate the data structures for the handshake.
std::vector< uint8_t > * get_buffer() const
bool ready() const
Check if socket has data ready to read For loop-monitored sockets, checks with the Application's sele...
virtual ssize_t read(void *buf, size_t len)=0
void noise_rand_bytes(void *output, size_t len)
@ HANDSHAKESTATE_READ_FAILED
@ HANDSHAKESTATE_BAD_STATE
@ HANDSHAKESTATE_SPLIT_FAILED
@ BAD_HANDSHAKE_PACKET_LEN
@ BAD_HANDSHAKE_ERROR_BYTE
@ HANDSHAKESTATE_SETUP_FAILED
@ CIPHERSTATE_ENCRYPT_FAILED
@ CIPHERSTATE_DECRYPT_FAILED
@ HANDSHAKESTATE_WRITE_FAILED
std::string noise_err_to_str(int err)
Convert a noise error code to a readable error.
bool random_bytes(uint8_t *data, size_t len)
Generate len number of random bytes.
std::string get_mac_address()
Get the device MAC address as a string, in lowercase hex notation.
Application App
Global storage of Application pointer - only one Application can exist.
std::vector< uint8_t > container