mirror of
https://github.com/esphome/esphome.git
synced 2026-02-01 09:17:34 -07:00
Compare commits
1 Commits
http_reque
...
lock_names
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b5ebe91150 |
@@ -90,8 +90,8 @@ static const int CAMERA_STOP_STREAM = 5000;
|
|||||||
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
|
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
|
||||||
: parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
|
: parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
|
||||||
#if defined(USE_API_PLAINTEXT) && defined(USE_API_NOISE)
|
#if defined(USE_API_PLAINTEXT) && defined(USE_API_NOISE)
|
||||||
auto noise_ctx = parent->get_noise_ctx();
|
auto &noise_ctx = parent->get_noise_ctx();
|
||||||
if (noise_ctx->has_psk()) {
|
if (noise_ctx.has_psk()) {
|
||||||
this->helper_ =
|
this->helper_ =
|
||||||
std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), noise_ctx, &this->client_info_)};
|
std::unique_ptr<APIFrameHelper>{new APINoiseFrameHelper(std::move(sock), noise_ctx, &this->client_info_)};
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -527,7 +527,7 @@ APIError APINoiseFrameHelper::init_handshake_() {
|
|||||||
if (aerr != APIError::OK)
|
if (aerr != APIError::OK)
|
||||||
return aerr;
|
return aerr;
|
||||||
|
|
||||||
const auto &psk = ctx_->get_psk();
|
const auto &psk = this->ctx_.get_psk();
|
||||||
err = noise_handshakestate_set_pre_shared_key(handshake_, psk.data(), psk.size());
|
err = noise_handshakestate_set_pre_shared_key(handshake_, psk.data(), psk.size());
|
||||||
aerr = handle_noise_error_(err, LOG_STR("noise_handshakestate_set_pre_shared_key"),
|
aerr = handle_noise_error_(err, LOG_STR("noise_handshakestate_set_pre_shared_key"),
|
||||||
APIError::HANDSHAKESTATE_SETUP_FAILED);
|
APIError::HANDSHAKESTATE_SETUP_FAILED);
|
||||||
|
|||||||
@@ -9,9 +9,8 @@ namespace esphome::api {
|
|||||||
|
|
||||||
class APINoiseFrameHelper final : public APIFrameHelper {
|
class APINoiseFrameHelper final : public APIFrameHelper {
|
||||||
public:
|
public:
|
||||||
APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, std::shared_ptr<APINoiseContext> ctx,
|
APINoiseFrameHelper(std::unique_ptr<socket::Socket> socket, APINoiseContext &ctx, const ClientInfo *client_info)
|
||||||
const ClientInfo *client_info)
|
: APIFrameHelper(std::move(socket), client_info), ctx_(ctx) {
|
||||||
: APIFrameHelper(std::move(socket), client_info), ctx_(std::move(ctx)) {
|
|
||||||
// Noise header structure:
|
// Noise header structure:
|
||||||
// Pos 0: indicator (0x01)
|
// Pos 0: indicator (0x01)
|
||||||
// Pos 1-2: encrypted payload size (16-bit big-endian)
|
// Pos 1-2: encrypted payload size (16-bit big-endian)
|
||||||
@@ -41,8 +40,8 @@ class APINoiseFrameHelper final : public APIFrameHelper {
|
|||||||
NoiseCipherState *send_cipher_{nullptr};
|
NoiseCipherState *send_cipher_{nullptr};
|
||||||
NoiseCipherState *recv_cipher_{nullptr};
|
NoiseCipherState *recv_cipher_{nullptr};
|
||||||
|
|
||||||
// Shared pointer (8 bytes on 32-bit = 4 bytes control block pointer + 4 bytes object pointer)
|
// Reference to noise context (4 bytes on 32-bit)
|
||||||
std::shared_ptr<APINoiseContext> ctx_;
|
APINoiseContext &ctx_;
|
||||||
|
|
||||||
// Vector (12 bytes on 32-bit)
|
// Vector (12 bytes on 32-bit)
|
||||||
std::vector<uint8_t> prologue_;
|
std::vector<uint8_t> prologue_;
|
||||||
|
|||||||
@@ -227,8 +227,8 @@ void APIServer::dump_config() {
|
|||||||
" Max connections: %u",
|
" Max connections: %u",
|
||||||
network::get_use_address(), this->port_, this->listen_backlog_, this->max_connections_);
|
network::get_use_address(), this->port_, this->listen_backlog_, this->max_connections_);
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
ESP_LOGCONFIG(TAG, " Noise encryption: %s", YESNO(this->noise_ctx_->has_psk()));
|
ESP_LOGCONFIG(TAG, " Noise encryption: %s", YESNO(this->noise_ctx_.has_psk()));
|
||||||
if (!this->noise_ctx_->has_psk()) {
|
if (!this->noise_ctx_.has_psk()) {
|
||||||
ESP_LOGCONFIG(TAG, " Supports encryption: YES");
|
ESP_LOGCONFIG(TAG, " Supports encryption: YES");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
@@ -493,7 +493,7 @@ bool APIServer::save_noise_psk(psk_t psk, bool make_active) {
|
|||||||
ESP_LOGW(TAG, "Key set in YAML");
|
ESP_LOGW(TAG, "Key set in YAML");
|
||||||
return false;
|
return false;
|
||||||
#else
|
#else
|
||||||
auto &old_psk = this->noise_ctx_->get_psk();
|
auto &old_psk = this->noise_ctx_.get_psk();
|
||||||
if (std::equal(old_psk.begin(), old_psk.end(), psk.begin())) {
|
if (std::equal(old_psk.begin(), old_psk.end(), psk.begin())) {
|
||||||
ESP_LOGW(TAG, "New PSK matches old");
|
ESP_LOGW(TAG, "New PSK matches old");
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -54,8 +54,8 @@ class APIServer : public Component, public Controller {
|
|||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
bool save_noise_psk(psk_t psk, bool make_active = true);
|
bool save_noise_psk(psk_t psk, bool make_active = true);
|
||||||
bool clear_noise_psk(bool make_active = true);
|
bool clear_noise_psk(bool make_active = true);
|
||||||
void set_noise_psk(psk_t psk) { noise_ctx_->set_psk(psk); }
|
void set_noise_psk(psk_t psk) { this->noise_ctx_.set_psk(psk); }
|
||||||
std::shared_ptr<APINoiseContext> get_noise_ctx() { return noise_ctx_; }
|
APINoiseContext &get_noise_ctx() { return this->noise_ctx_; }
|
||||||
#endif // USE_API_NOISE
|
#endif // USE_API_NOISE
|
||||||
|
|
||||||
void handle_disconnect(APIConnection *conn);
|
void handle_disconnect(APIConnection *conn);
|
||||||
@@ -228,7 +228,7 @@ class APIServer : public Component, public Controller {
|
|||||||
// 7 bytes used, 1 byte padding
|
// 7 bytes used, 1 byte padding
|
||||||
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
std::shared_ptr<APINoiseContext> noise_ctx_ = std::make_shared<APINoiseContext>();
|
APINoiseContext noise_ctx_;
|
||||||
ESPPreferenceObject noise_pref_;
|
ESPPreferenceObject noise_pref_;
|
||||||
#endif // USE_API_NOISE
|
#endif // USE_API_NOISE
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -119,7 +119,7 @@ void MDNSComponent::compile_records_(StaticVector<MDNSService, MDNS_SERVICE_COUN
|
|||||||
MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION, "api_encryption");
|
MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION, "api_encryption");
|
||||||
MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION_SUPPORTED, "api_encryption_supported");
|
MDNS_STATIC_CONST_CHAR(TXT_API_ENCRYPTION_SUPPORTED, "api_encryption_supported");
|
||||||
MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
|
MDNS_STATIC_CONST_CHAR(NOISE_ENCRYPTION, "Noise_NNpsk0_25519_ChaChaPoly_SHA256");
|
||||||
bool has_psk = api::global_api_server->get_noise_ctx()->has_psk();
|
bool has_psk = api::global_api_server->get_noise_ctx().has_psk();
|
||||||
const char *encryption_key = has_psk ? TXT_API_ENCRYPTION : TXT_API_ENCRYPTION_SUPPORTED;
|
const char *encryption_key = has_psk ? TXT_API_ENCRYPTION : TXT_API_ENCRYPTION_SUPPORTED;
|
||||||
txt_records.push_back({MDNS_STR(encryption_key), MDNS_STR(NOISE_ENCRYPTION)});
|
txt_records.push_back({MDNS_STR(encryption_key), MDNS_STR(NOISE_ENCRYPTION)});
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -140,7 +140,7 @@ void MQTTClientComponent::send_device_info_() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef USE_API_NOISE
|
#ifdef USE_API_NOISE
|
||||||
root[api::global_api_server->get_noise_ctx()->has_psk() ? "api_encryption" : "api_encryption_supported"] =
|
root[api::global_api_server->get_noise_ctx().has_psk() ? "api_encryption" : "api_encryption_supported"] =
|
||||||
"Noise_NNpsk0_25519_ChaChaPoly_SHA256";
|
"Noise_NNpsk0_25519_ChaChaPoly_SHA256";
|
||||||
#endif
|
#endif
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user