19static const char *
const TAG =
"api.noise";
21static const char PROLOGUE_INIT[] PROGMEM =
"NoiseAPIInit";
23static const char *
const PROLOGUE_INIT =
"NoiseAPIInit";
25static constexpr size_t PROLOGUE_INIT_LEN = 12;
27#define HELPER_LOG(msg, ...) \
28 ESP_LOGVV(TAG, "%s (%s): " msg, this->client_info_->name.c_str(), this->client_info_->peername.c_str(), ##__VA_ARGS__)
30#ifdef HELPER_LOG_PACKETS
31#define LOG_PACKET_RECEIVED(buffer) ESP_LOGVV(TAG, "Received frame: %s", format_hex_pretty(buffer).c_str())
32#define LOG_PACKET_SENDING(data, len) ESP_LOGVV(TAG, "Sending raw: %s", format_hex_pretty(data, len).c_str())
34#define LOG_PACKET_RECEIVED(buffer) ((void) 0)
35#define LOG_PACKET_SENDING(data, len) ((void) 0)
40 if (err == NOISE_ERROR_NO_MEMORY)
41 return LOG_STR(
"NO_MEMORY");
42 if (err == NOISE_ERROR_UNKNOWN_ID)
43 return LOG_STR(
"UNKNOWN_ID");
44 if (err == NOISE_ERROR_UNKNOWN_NAME)
45 return LOG_STR(
"UNKNOWN_NAME");
46 if (err == NOISE_ERROR_MAC_FAILURE)
47 return LOG_STR(
"MAC_FAILURE");
48 if (err == NOISE_ERROR_NOT_APPLICABLE)
49 return LOG_STR(
"NOT_APPLICABLE");
50 if (err == NOISE_ERROR_SYSTEM)
51 return LOG_STR(
"SYSTEM");
52 if (err == NOISE_ERROR_REMOTE_KEY_REQUIRED)
53 return LOG_STR(
"REMOTE_KEY_REQUIRED");
54 if (err == NOISE_ERROR_LOCAL_KEY_REQUIRED)
55 return LOG_STR(
"LOCAL_KEY_REQUIRED");
56 if (err == NOISE_ERROR_PSK_REQUIRED)
57 return LOG_STR(
"PSK_REQUIRED");
58 if (err == NOISE_ERROR_INVALID_LENGTH)
59 return LOG_STR(
"INVALID_LENGTH");
60 if (err == NOISE_ERROR_INVALID_PARAM)
61 return LOG_STR(
"INVALID_PARAM");
62 if (err == NOISE_ERROR_INVALID_STATE)
63 return LOG_STR(
"INVALID_STATE");
64 if (err == NOISE_ERROR_INVALID_NONCE)
65 return LOG_STR(
"INVALID_NONCE");
66 if (err == NOISE_ERROR_INVALID_PRIVATE_KEY)
67 return LOG_STR(
"INVALID_PRIVATE_KEY");
68 if (err == NOISE_ERROR_INVALID_PUBLIC_KEY)
69 return LOG_STR(
"INVALID_PUBLIC_KEY");
70 if (err == NOISE_ERROR_INVALID_FORMAT)
71 return LOG_STR(
"INVALID_FORMAT");
72 if (err == NOISE_ERROR_INVALID_SIGNATURE)
73 return LOG_STR(
"INVALID_SIGNATURE");
74 return LOG_STR(
"UNKNOWN");
86 prologue_.resize(old_size + PROLOGUE_INIT_LEN);
88 memcpy_P(
prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
90 std::memcpy(
prologue_.data() + old_size, PROLOGUE_INIT, PROLOGUE_INIT_LEN);
110 HELPER_LOG(
"%s failed: %s", LOG_STR_ARG(func_name), LOG_STR_ARG(
noise_err_to_logstr(err)));
155 if (
static_cast<uint8_t
>(received) != to_read) {
172 uint16_t limit = (
state_ ==
State::DATA) ? MAX_MESSAGE_SIZE : MAX_HANDSHAKE_SIZE;
173 if (msg_size > limit) {
175 HELPER_LOG(
"Bad packet: message size %u exceeds maximum %u", msg_size, limit);
180 if (this->
rx_buf_.size() != msg_size) {
181 this->
rx_buf_.resize(msg_size);
193 if (
static_cast<uint16_t
>(received) != to_read) {
199 LOG_PACKET_RECEIVED(this->
rx_buf_);
221 HELPER_LOG(
"Bad state for method: %d", (
int)
state_);
232 size_t old_size = this->
prologue_.size();
236 std::memcpy(this->
prologue_.data() + old_size + 2, this->rx_buf_.data(), this->rx_buf_.size());
246 size_t name_len = name.size() + 1;
247 size_t mac_len = mac.size() + 1;
248 size_t name_offset = 1;
249 size_t mac_offset = name_offset + name_len;
250 size_t total_size = 1 + name_len + mac_len;
252 auto msg = std::make_unique<uint8_t[]>(total_size);
258 std::memcpy(msg.get() + name_offset, name.c_str(), name_len);
260 std::memcpy(msg.get() + mac_offset, mac.c_str(), mac_len);
274 int action = noise_handshakestate_get_action(
handshake_);
275 if (action == NOISE_ACTION_READ_MESSAGE) {
285 }
else if (this->
rx_buf_[0] != 0x00) {
286 HELPER_LOG(
"Bad handshake error byte: %u", this->
rx_buf_[0]);
292 noise_buffer_init(mbuf);
293 noise_buffer_set_input(mbuf, this->
rx_buf_.data() + 1, this->rx_buf_.size() - 1);
294 err = noise_handshakestate_read_message(
handshake_, &mbuf,
nullptr);
298 : LOG_STR(
"Handshake error"));
306 }
else if (action == NOISE_ACTION_WRITE_MESSAGE) {
309 noise_buffer_init(mbuf);
310 noise_buffer_set_output(mbuf, buffer + 1,
sizeof(buffer) - 1);
312 err = noise_handshakestate_write_message(
handshake_, &mbuf,
nullptr);
328 HELPER_LOG(
"Bad action for handshake: %d", action);
338#ifdef USE_STORE_LOG_STR_IN_FLASH
340 size_t reason_len = strlen_P(
reinterpret_cast<PGM_P
>(reason));
341 size_t data_size = reason_len + 1;
342 auto data = std::make_unique<uint8_t[]>(data_size);
346 if (reason_len > 0) {
347 memcpy_P(data.get() + 1,
reinterpret_cast<PGM_P
>(reason), reason_len);
351 const char *reason_str = LOG_STR_ARG(reason);
352 size_t reason_len = strlen(reason_str);
353 size_t data_size = reason_len + 1;
354 auto data = std::make_unique<uint8_t[]>(data_size);
358 if (reason_len > 0) {
359 std::memcpy(data.get() + 1, reason_str, reason_len);
384 noise_buffer_init(mbuf);
385 noise_buffer_set_inout(mbuf, this->
rx_buf_.data(), this->rx_buf_.size(), this->rx_buf_.size());
386 int err = noise_cipherstate_decrypt(this->
recv_cipher_, &mbuf);
393 uint16_t msg_size = mbuf.size;
394 uint8_t *msg_data = this->
rx_buf_.data();
397 HELPER_LOG(
"Bad data packet: size %d too short", msg_size);
401 uint16_t
type = (((uint16_t) msg_data[0]) << 8) | msg_data[1];
402 uint16_t data_len = (((uint16_t) msg_data[2]) << 8) | msg_data[3];
403 if (data_len > msg_size - 4) {
405 HELPER_LOG(
"Bad data packet: data_len %u greater than msg_size %u", data_len, msg_size);
433 if (packets.empty()) {
437 uint8_t *buffer_data = buffer.
get_buffer()->data();
441 uint16_t total_write_len = 0;
444 for (
const auto &packet : packets) {
446 uint8_t *buf_start = buffer_data + packet.offset;
453 const uint8_t msg_offset = 3;
454 buf_start[msg_offset] =
static_cast<uint8_t
>(packet.message_type >> 8);
455 buf_start[msg_offset + 1] =
static_cast<uint8_t
>(packet.message_type);
456 buf_start[msg_offset + 2] =
static_cast<uint8_t
>(packet.payload_size >> 8);
457 buf_start[msg_offset + 3] =
static_cast<uint8_t
>(packet.payload_size);
465 noise_buffer_init(mbuf);
466 noise_buffer_set_inout(mbuf, buf_start + msg_offset, 4 + packet.payload_size,
469 int err = noise_cipherstate_encrypt(
send_cipher_, &mbuf);
476 buf_start[1] =
static_cast<uint8_t
>(mbuf.size >> 8);
477 buf_start[2] =
static_cast<uint8_t
>(mbuf.size);
480 size_t packet_len =
static_cast<size_t>(3 + mbuf.size);
482 total_write_len += packet_len;
492 header[1] = (uint8_t) (
len >> 8);
493 header[2] = (uint8_t)
len;
501 iov[1].
iov_base =
const_cast<uint8_t *
>(data);
516 nid_.pattern_id = NOISE_PATTERN_NN;
517 nid_.cipher_id = NOISE_CIPHER_CHACHAPOLY;
518 nid_.dh_id = NOISE_DH_CURVE25519;
519 nid_.prefix_id = NOISE_PREFIX_STANDARD;
520 nid_.hybrid_id = NOISE_DH_NONE;
521 nid_.hash_id = NOISE_HASH_SHA256;
522 nid_.modifier_ids[0] = NOISE_MODIFIER_PSK0;
524 err = noise_handshakestate_new_by_id(&
handshake_, &
nid_, NOISE_ROLE_RESPONDER);
530 const auto &psk =
ctx_->get_psk();
531 err = noise_handshakestate_set_pre_shared_key(
handshake_, psk.data(), psk.size());
554 int action = noise_handshakestate_get_action(
handshake_);
555 if (action == NOISE_ACTION_READ_MESSAGE || action == NOISE_ACTION_WRITE_MESSAGE)
557 if (action != NOISE_ACTION_SPLIT) {
559 HELPER_LOG(
"Bad action for handshake: %d", action);
570 HELPER_LOG(
"Handshake complete!");
596 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 handle_noise_error_(int err, const LogString *func_name, APIError api_err)
APIError state_action_()
To be called from read/write methods.
APIError try_read_frame_()
Read a packet into the rx_buf_.
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)
void send_explicit_handshake_reject_(const LogString *reason)
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_()
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
const LogString * noise_err_to_logstr(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