1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-30 06:33:51 +00:00

safe a write

This commit is contained in:
J. Nick Koston
2025-09-26 21:14:56 -05:00
parent abf522bbb9
commit 78333ef795
2 changed files with 13 additions and 11 deletions

View File

@@ -541,9 +541,6 @@ bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string
// Small stack buffer for nonce seed bytes // Small stack buffer for nonce seed bytes
uint8_t nonce_bytes[8]; // Max 8 bytes (2 x uint32_t for SHA256) uint8_t nonce_bytes[8]; // Max 8 bytes (2 x uint32_t for SHA256)
// Send auth request type
this->writeall_(&auth_request, 1);
hasher->init(); hasher->init();
// Generate nonce seed bytes using random_bytes // Generate nonce seed bytes using random_bytes
@@ -554,20 +551,24 @@ bool ESPHomeOTAComponent::perform_hash_auth_(HashBase *hasher, const std::string
hasher->add(nonce_bytes, nonce_len); hasher->add(nonce_bytes, nonce_len);
hasher->calculate(); hasher->calculate();
// Generate and send nonce // Prepare buffer: auth_type (1 byte) + nonce (hex_size bytes)
hasher->get_hex(buf); buf[0] = auth_request;
buf[hex_size] = '\0'; hasher->get_hex(buf + 1);
ESP_LOGV(TAG, "Auth: %s Nonce is %s", LOG_STR_ARG(name), buf);
if (!this->writeall_(reinterpret_cast<uint8_t *>(buf), hex_size)) { // Log nonce for debugging
this->log_auth_warning_(LOG_STR("Writing nonce"), name); buf[1 + hex_size] = '\0';
ESP_LOGV(TAG, "Auth: %s Nonce is %s", LOG_STR_ARG(name), buf + 1);
// Send auth_type + nonce in a single write
if (!this->writeall_(reinterpret_cast<uint8_t *>(buf), 1 + hex_size)) {
this->log_auth_warning_(LOG_STR("Writing auth type and nonce"), name);
return false; return false;
} }
// Start challenge: password + nonce // Start challenge: password + nonce (nonce is at buf + 1)
hasher->init(); hasher->init();
hasher->add(password.c_str(), password.length()); hasher->add(password.c_str(), password.length());
hasher->add(buf, hex_size); hasher->add(buf + 1, hex_size);
// Read cnonce and add to hash // Read cnonce and add to hash
if (!this->readall_(reinterpret_cast<uint8_t *>(buf), hex_size)) { if (!this->readall_(reinterpret_cast<uint8_t *>(buf), hex_size)) {

View File

@@ -126,6 +126,7 @@
#define USE_OTA_MD5 #define USE_OTA_MD5
#define USE_OTA_PASSWORD #define USE_OTA_PASSWORD
#define USE_OTA_SHA256 #define USE_OTA_SHA256
#define ALLOW_OTA_DOWNGRADE_MD5
#define USE_OTA_STATE_CALLBACK #define USE_OTA_STATE_CALLBACK
#define USE_OTA_VERSION 2 #define USE_OTA_VERSION 2
#define USE_TIME_TIMEZONE #define USE_TIME_TIMEZONE