ESPHome 2026.6.0-dev
Loading...
Searching...
No Matches
ota_esphome.cpp
Go to the documentation of this file.
1#include "ota_esphome.h"
2#ifdef USE_OTA
3#ifdef USE_OTA_PASSWORD
5#endif
14#include "esphome/core/hal.h"
16#include "esphome/core/log.h"
17#include "esphome/core/util.h"
18#ifdef USE_LWIP_FAST_SELECT
20#endif
21
22#include <cerrno>
23#include <cstdio>
24#include <sys/time.h>
25
26namespace esphome {
27
28static const char *const TAG = "esphome.ota";
29static constexpr uint16_t OTA_BLOCK_SIZE = 8192;
30static constexpr size_t OTA_BUFFER_SIZE = 1024; // buffer size for OTA data transfer
31static constexpr uint32_t OTA_SOCKET_TIMEOUT_HANDSHAKE = 20000; // milliseconds for initial handshake
32static constexpr uint32_t OTA_SOCKET_TIMEOUT_DATA = 90000; // milliseconds for data transfer
33
34// Single-instance pointer — multi-port configs are rejected in final_validate.
35// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
36static ESPHomeOTAComponent *global_esphome_ota_component = nullptr;
37
38// Called from any context (LwIP TCP/IP task, RP2040 user-IRQ).
40 if (global_esphome_ota_component != nullptr) {
41 global_esphome_ota_component->enable_loop_soon_any_context();
42 }
43}
44
46 this->server_ = socket::socket_ip_loop_monitored(SOCK_STREAM, 0).release(); // monitored for incoming connections
47 if (this->server_ == nullptr) {
48 this->server_failed_(LOG_STR("creation"));
49 return;
50 }
51 int enable = 1;
52 int err = this->server_->setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable, sizeof(int));
53 if (err != 0) {
54 this->log_socket_error_(LOG_STR("reuseaddr"));
55 // we can still continue
56 }
57 err = this->server_->setblocking(false);
58 if (err != 0) {
59 this->server_failed_(LOG_STR("nonblocking"));
60 return;
61 }
62
63 struct sockaddr_storage server;
64
65 socklen_t sl = socket::set_sockaddr_any((struct sockaddr *) &server, sizeof(server), this->port_);
66 if (sl == 0) {
67 this->server_failed_(LOG_STR("set sockaddr"));
68 return;
69 }
70
71 err = this->server_->bind((struct sockaddr *) &server, sl);
72 if (err != 0) {
73 this->server_failed_(LOG_STR("bind"));
74 return;
75 }
76
77 err = this->server_->listen(1); // Only one client at a time
78 if (err != 0) {
79 this->server_failed_(LOG_STR("listen"));
80 return;
81 }
82
83 // loop() self-disables on its first idle tick; no explicit disable_loop() needed here.
84 global_esphome_ota_component = this;
85#ifdef USE_LWIP_FAST_SELECT
86 // Filter fast-select wakes to this listener only. If the sock lookup returns nullptr,
87 // no wakes fire and loop() falls back to the self-disable safety net.
89#endif
90
91#ifdef USE_OTA_PARTITIONS
93#endif
94}
95
97 ESP_LOGCONFIG(TAG,
98 "Over-The-Air updates:\n"
99 " Address: %s:%u\n"
100 " Version: %d",
101 network::get_use_address(), this->port_, USE_OTA_VERSION);
102#ifdef USE_OTA_PASSWORD
103 if (!this->password_.empty()) {
104 ESP_LOGCONFIG(TAG, " Password configured");
105 }
106#endif
107#ifdef USE_OTA_PARTITIONS
108 ESP_LOGCONFIG(TAG,
109 " Partition access allowed\n"
110 " Running app:\n"
111 " Partition address: 0x%" PRIX32 "\n"
112 " Used size: %zu bytes (0x%zX)",
114
115#ifdef USE_ESP32
116 ESP_LOGCONFIG(TAG,
117 " Partition table:\n"
118 " %-12s %-4s %-8s %-10s %-10s",
119 "Name", "Type", "Subtype", "Address", "Size");
120 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY, nullptr);
121 while (it != nullptr) {
122 const esp_partition_t *partition = esp_partition_get(it);
123 ESP_LOGCONFIG(TAG, " %-12s 0x%-2X 0x%-6X 0x%-8" PRIX32 " 0x%-8" PRIX32, partition->label, partition->type,
124 partition->subtype, partition->address, partition->size);
125 it = esp_partition_next(it);
126 }
127 esp_partition_iterator_release(it);
128 esp_bootloader_desc_t bootloader_desc;
129 esp_err_t err = esp_ota_get_bootloader_description(nullptr, &bootloader_desc);
130 ESP_LOGCONFIG(TAG, " Bootloader: ESP-IDF %s", (err == ESP_OK) ? bootloader_desc.idf_ver : "version unknown");
131#endif // USE_ESP32
132#endif // USE_OTA_PARTITIONS
133}
134
136 // Self-disable idle loop where a wake path re-enables on listener readiness
137 // (fast-select, raw-TCP accept_fn_). Host BSD select doesn't, so stay enabled.
138 if (this->client_ == nullptr && !this->server_->ready()) {
139#ifndef USE_HOST
140 this->disable_loop();
141#endif
142 return;
143 }
144 this->handle_handshake_();
145}
146
147static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_COMPRESSION = 0x01;
148static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_SHA256_AUTH = 0x02;
149static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL = 0x04;
150static constexpr uint8_t SERVER_FEATURE_SUPPORTS_COMPRESSION = 0x01;
151static constexpr uint8_t SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS = 0x02;
152
159
160 if (this->client_ == nullptr) {
161 // We already checked server_->ready() in loop(), so we can accept directly
162 struct sockaddr_storage source_addr;
163 socklen_t addr_len = sizeof(source_addr);
164 int enable = 1;
165
166 this->client_ = this->server_->accept_loop_monitored((struct sockaddr *) &source_addr, &addr_len);
167 if (this->client_ == nullptr)
168 return;
169 int err = this->client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable, sizeof(int));
170 if (err != 0) {
171 this->log_socket_error_(LOG_STR("nodelay"));
172 this->cleanup_connection_();
173 return;
174 }
175 err = this->client_->setblocking(false);
176 if (err != 0) {
177 this->log_socket_error_(LOG_STR("non-blocking"));
178 this->cleanup_connection_();
179 return;
180 }
181 this->log_start_(LOG_STR("handshake"));
183 this->handshake_buf_pos_ = 0; // Reset handshake buffer position
185 }
186
187 // Check for handshake timeout
189 if (now - this->client_connect_time_ > OTA_SOCKET_TIMEOUT_HANDSHAKE) {
190 ESP_LOGW(TAG, "Handshake timeout");
191 this->cleanup_connection_();
192 return;
193 }
194
195 switch (this->ota_state_) {
197 // Try to read remaining magic bytes (5 total)
198 if (!this->try_read_(5, LOG_STR("read magic"))) {
199 return;
200 }
201
202 // Validate magic bytes
203 static const uint8_t MAGIC_BYTES[5] = {0x6C, 0x26, 0xF7, 0x5C, 0x45};
204 if (memcmp(this->handshake_buf_, MAGIC_BYTES, 5) != 0) {
205 ESP_LOGW(TAG, "Magic bytes mismatch! 0x%02X-0x%02X-0x%02X-0x%02X-0x%02X", this->handshake_buf_[0],
206 this->handshake_buf_[1], this->handshake_buf_[2], this->handshake_buf_[3], this->handshake_buf_[4]);
208 return;
209 }
210
211 // Magic bytes valid, move to next state
214 this->handshake_buf_[1] = USE_OTA_VERSION;
215 [[fallthrough]];
216 }
217
218 case OTAState::MAGIC_ACK: {
219 // Send OK and version - 2 bytes
220 if (!this->try_write_(2, LOG_STR("ack magic"))) {
221 return;
222 }
223 // All bytes sent, create backend and move to next state
226 [[fallthrough]];
227 }
228
230 // Read features - 1 byte
231 if (!this->try_read_(1, LOG_STR("read feature"))) {
232 return;
233 }
234 this->ota_features_ = this->handshake_buf_[0];
235 ESP_LOGV(TAG, "Features: 0x%02X", this->ota_features_);
237
238 const bool supports_compression =
239 (this->ota_features_ & CLIENT_FEATURE_SUPPORTS_COMPRESSION) != 0 && this->backend_->supports_compression();
240
241 // Compose the feature-ack response. When the client negotiates the extended protocol we emit
242 // a 2-byte response (marker + server feature flags); otherwise we emit the single-byte
243 // legacy response.
244 this->extended_proto_ = (this->ota_features_ & CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL) != 0;
245 if (this->extended_proto_) {
246 static_assert(HANDSHAKE_BUF_SIZE >= 2, "handshake_buf_ must hold the 2-byte extended-protocol feature ack");
248 this->handshake_buf_[1] = (supports_compression ? SERVER_FEATURE_SUPPORTS_COMPRESSION : 0);
249#ifdef USE_OTA_PARTITIONS
250 this->handshake_buf_[1] |= SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS;
251#endif
252 } else {
253 this->handshake_buf_[0] =
255 }
256 [[fallthrough]];
257 }
258
260 static constexpr size_t STANDARD_PROTO_ACK_SIZE = 1;
261 static constexpr size_t EXTENDED_PROTO_ACK_SIZE = 2;
262 const size_t ack_size = this->extended_proto_ ? EXTENDED_PROTO_ACK_SIZE : STANDARD_PROTO_ACK_SIZE;
263 if (!this->try_write_(ack_size, LOG_STR("ack feature"))) {
264 return;
265 }
266#ifdef USE_OTA_PASSWORD
267 // If password is set, move to auth phase
268 if (!this->password_.empty()) {
270 } else
271#endif
272 {
273 // No password, move directly to data phase
275 }
276 [[fallthrough]];
277 }
278
279#ifdef USE_OTA_PASSWORD
280 case OTAState::AUTH_SEND: {
281 // Non-blocking authentication send
282 if (!this->handle_auth_send_()) {
283 return;
284 }
286 [[fallthrough]];
287 }
288
289 case OTAState::AUTH_READ: {
290 // Non-blocking authentication read & verify
291 if (!this->handle_auth_read_()) {
292 return;
293 }
295 [[fallthrough]];
296 }
297#endif
298
299 case OTAState::DATA:
300 this->handle_data_();
301 return;
302
303 default:
304 break;
305 }
306}
307
342 size_t total = 0;
343 uint32_t last_progress = 0;
344 uint32_t last_data_ms = 0;
345 uint8_t buf[OTA_BUFFER_SIZE];
346 char *sbuf = reinterpret_cast<char *>(buf);
347 size_t ota_size;
349#if USE_OTA_VERSION == 2
350 size_t size_acknowledged = 0;
351#endif
352
353 // Set socket timeouts and blocking mode (see strategy table above)
354 struct timeval tv;
355 tv.tv_sec = 2;
356 tv.tv_usec = 0;
357 this->client_->setsockopt(SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv));
358 this->client_->setsockopt(SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
359 this->client_->setblocking(true);
360
361 // Acknowledge auth OK - 1 byte
363
364 if (this->extended_proto_) {
365 // Read ota type, 1 byte
366 if (!this->readall_(buf, 1)) {
367 this->log_read_error_(LOG_STR("OTA type"));
368 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
369 }
370 ota_type = static_cast<ota::OTAType>(buf[0]);
371 }
372 ESP_LOGV(TAG, "OTA type is 0x%02x", ota_type);
373
374 // Read size, 4 bytes MSB first
375 if (!this->readall_(buf, 4)) {
376 this->log_read_error_(LOG_STR("size"));
377 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
378 }
379 ota_size = (static_cast<size_t>(buf[0]) << 24) | (static_cast<size_t>(buf[1]) << 16) |
380 (static_cast<size_t>(buf[2]) << 8) | buf[3];
381 ESP_LOGV(TAG, "Size is %zu bytes", ota_size);
382
383#ifndef USE_OTA_PARTITIONS
384 if (ota_type != ota::OTA_TYPE_UPDATE_APP) {
386 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
387 }
388#endif
389
390 // Now that we've passed authentication and are actually
391 // starting the update, set the warning status and notify
392 // listeners. This ensures that port scanners do not
393 // accidentally trigger the update process.
394 this->log_start_(LOG_STR("update"));
395 this->status_set_warning();
396#ifdef USE_OTA_STATE_LISTENER
397 this->notify_state_(ota::OTA_STARTED, 0.0f, 0);
398#endif
399
400 // begin() may block for a few seconds while it locks flash.
401 error_code = this->backend_->begin(ota_size, ota_type);
402 if (error_code != ota::OTA_RESPONSE_OK)
403 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
404
405 // Acknowledge prepare OK - 1 byte
407
408 // Read binary MD5, 32 bytes
409 if (!this->readall_(buf, 32)) {
410 this->log_read_error_(LOG_STR("MD5 checksum"));
411 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
412 }
413 sbuf[32] = '\0';
414 ESP_LOGV(TAG, "Update: Binary MD5 is %s", sbuf);
415 this->backend_->set_update_md5(sbuf);
416
417 // Acknowledge MD5 OK - 1 byte
419
420 // Track when we last received data so a silently-vanished peer (no FIN/RST
421 // delivered, e.g. uploader killed mid-transfer or NAT/router dropped state)
422 // can't wedge the device indefinitely. Without this, the loop only exits
423 // on actual data, EOF, or a non-EWOULDBLOCK error from read(), and lwIP
424 // TCP keepalive isn't enabled here.
425 last_data_ms = millis();
426 while (total < ota_size) {
427 if (millis() - last_data_ms > OTA_SOCKET_TIMEOUT_DATA) {
428 ESP_LOGW(TAG, "No data received for %u ms", (unsigned) OTA_SOCKET_TIMEOUT_DATA);
430 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
431 }
432 size_t remaining = ota_size - total;
433 size_t requested = remaining < OTA_BUFFER_SIZE ? remaining : OTA_BUFFER_SIZE;
434 ssize_t read = this->client_->read(buf, requested);
435 if (read == -1) {
436 const int err = errno;
437 if (this->would_block_(err)) {
438 // read() already waited up to SO_RCVTIMEO for data, just feed WDT
439 App.feed_wdt();
440 continue;
441 }
442 ESP_LOGW(TAG, "Read err %d", err);
443 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
444 } else if (read == 0) {
445 ESP_LOGW(TAG, "Remote closed");
446 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
447 }
448
449 last_data_ms = millis();
450 error_code = this->backend_->write(buf, read);
451 if (error_code != ota::OTA_RESPONSE_OK) {
452 ESP_LOGW(TAG, "Flash write err %d", error_code);
453 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
454 }
455 total += read;
456#if USE_OTA_VERSION == 2
457 while (size_acknowledged + OTA_BLOCK_SIZE <= total || (total == ota_size && size_acknowledged < ota_size)) {
459 size_acknowledged += OTA_BLOCK_SIZE;
460 }
461#endif
462
463 uint32_t now = millis();
464 if (now - last_progress > 1000) {
465 last_progress = now;
466 float percentage = (total * 100.0f) / ota_size;
467 ESP_LOGD(TAG, "Progress: %0.1f%%", percentage);
468#ifdef USE_OTA_STATE_LISTENER
469 this->notify_state_(ota::OTA_IN_PROGRESS, percentage, 0);
470#endif
471 // feed watchdog and give other tasks a chance to run
473 }
474 }
475
476 // Acknowledge receive OK - 1 byte
478
479 error_code = this->backend_->end();
480 if (error_code != ota::OTA_RESPONSE_OK) {
481 ESP_LOGW(TAG, "End update err %d", error_code);
482 goto error; // NOLINT(cppcoreguidelines-avoid-goto)
483 }
484
485 // Acknowledge Update end OK - 1 byte
487
488 // Read ACK
489 if (!this->readall_(buf, 1) || buf[0] != ota::OTA_RESPONSE_OK) {
490 this->log_read_error_(LOG_STR("ack"));
491 // do not go to error, this is not fatal
492 }
493
494 this->cleanup_connection_();
495 delay(10);
496 ESP_LOGI(TAG, "Update complete");
497 this->status_clear_warning();
498#ifdef USE_OTA_STATE_LISTENER
499 this->notify_state_(ota::OTA_COMPLETED, 100.0f, 0);
500#endif
501 delay(100); // NOLINT
502#ifdef USE_OTA_PARTITIONS
503 if (ota_type == ota::OTA_TYPE_UPDATE_PARTITION_TABLE) {
504 // Skip on_safe_shutdown: nvs_flash_deinit() has already invalidated open NVS handles, so
505 // preferences flush would emit ESP_ERR_NVS_INVALID_HANDLE for every entry. Reboot directly.
506 App.reboot();
507 }
508#endif
510
511error:
512 this->write_byte_(static_cast<uint8_t>(error_code));
513
514 // Abort backend before cleanup - cleanup_connection_() destroys the backend.
515 // Always call abort() unconditionally: backends register external partitions before
516 // esp_ota_begin (partition table / bootloader paths), and abort() is responsible for
517 // releasing those even if begin() failed before an OTA handle was opened. The IDF
518 // backend's esp_ota_abort(0) is documented as harmless.
519 if (this->backend_ != nullptr) {
520 this->backend_->abort();
521 }
522
523 this->cleanup_connection_();
524
525 this->status_momentary_error("err", 5000);
526#ifdef USE_OTA_STATE_LISTENER
527 this->notify_state_(ota::OTA_ERROR, 0.0f, static_cast<uint8_t>(error_code));
528#endif
529}
530
531bool ESPHomeOTAComponent::readall_(uint8_t *buf, size_t len) {
532 uint32_t start = millis();
533 uint32_t at = 0;
534 while (len - at > 0) {
535 uint32_t now = millis();
536 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
537 ESP_LOGW(TAG, "Timeout reading %zu bytes", len);
538 return false;
539 }
540
541 ssize_t read = this->client_->read(buf + at, len - at);
542 if (read == -1) {
543 const int err = errno;
544 if (!this->would_block_(err)) {
545 ESP_LOGW(TAG, "Read err %zu bytes, errno %d", len, err);
546 return false;
547 }
548 } else if (read == 0) {
549 ESP_LOGW(TAG, "Remote closed");
550 return false;
551 } else {
552 at += read;
553 }
554 // read() already waited via SO_RCVTIMEO, just yield without 1ms stall
555 App.feed_wdt();
556 delay(0);
557 }
558
559 return true;
560}
561bool ESPHomeOTAComponent::writeall_(const uint8_t *buf, size_t len) {
562 uint32_t start = millis();
563 uint32_t at = 0;
564 while (len - at > 0) {
565 uint32_t now = millis();
566 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
567 ESP_LOGW(TAG, "Timeout writing %zu bytes", len);
568 return false;
569 }
570
571 ssize_t written = this->client_->write(buf + at, len - at);
572 if (written == -1) {
573 const int err = errno;
574 if (!this->would_block_(err)) {
575 ESP_LOGW(TAG, "Write err %zu bytes, errno %d", len, err);
576 return false;
577 }
578 // EWOULDBLOCK: on raw TCP writes never block, delay(1) prevents spinning
580 } else {
581 at += written;
582 // write() may block up to SO_SNDTIMEO on BSD/lwip sockets, feed WDT
583 App.feed_wdt();
584 }
585 }
586 return true;
587}
588
590uint16_t ESPHomeOTAComponent::get_port() const { return this->port_; }
591void ESPHomeOTAComponent::set_port(uint16_t port) { this->port_ = port; }
592
593void ESPHomeOTAComponent::log_socket_error_(const LogString *msg) {
594 ESP_LOGW(TAG, "Socket %s: errno %d", LOG_STR_ARG(msg), errno);
595}
596
597void ESPHomeOTAComponent::log_read_error_(const LogString *what) { ESP_LOGW(TAG, "Read %s failed", LOG_STR_ARG(what)); }
598
599void ESPHomeOTAComponent::log_start_(const LogString *phase) {
600 char peername[socket::SOCKADDR_STR_LEN];
601 this->client_->getpeername_to(peername);
602 ESP_LOGD(TAG, "Starting %s from %s", LOG_STR_ARG(phase), peername);
603}
604
605void ESPHomeOTAComponent::log_remote_closed_(const LogString *during) {
606 ESP_LOGW(TAG, "Remote closed at %s", LOG_STR_ARG(during));
607}
608
609void ESPHomeOTAComponent::server_failed_(const LogString *msg) {
610 this->log_socket_error_(msg);
611 // No explicit close() needed — listen sockets have no active connections on
612 // failure/shutdown. Destructor handles fd cleanup (close or abort per platform).
613 delete this->server_;
614 this->server_ = nullptr;
615 this->mark_failed();
616}
617
618bool ESPHomeOTAComponent::handle_read_error_(ssize_t read, const LogString *desc) {
619 if (read == -1 && this->would_block_(errno)) {
620 return false; // No data yet, try again next loop
621 }
622
623 if (read <= 0) {
624 read == 0 ? this->log_remote_closed_(desc) : this->log_socket_error_(desc);
625 this->cleanup_connection_();
626 return false;
627 }
628 return true;
629}
630
632 if (written == -1) {
633 if (this->would_block_(errno)) {
634 return false; // Try again next loop
635 }
636 this->log_socket_error_(desc);
637 this->cleanup_connection_();
638 return false;
639 }
640 return true;
641}
642
643bool ESPHomeOTAComponent::try_read_(size_t to_read, const LogString *desc) {
644 // Read bytes into handshake buffer, starting at handshake_buf_pos_
645 size_t bytes_to_read = to_read - this->handshake_buf_pos_;
646 ssize_t read = this->client_->read(this->handshake_buf_ + this->handshake_buf_pos_, bytes_to_read);
647
648 if (!this->handle_read_error_(read, desc)) {
649 return false;
650 }
651
652 this->handshake_buf_pos_ += read;
653 // Return true only if we have all the requested bytes
654 return this->handshake_buf_pos_ >= to_read;
655}
656
657bool ESPHomeOTAComponent::try_write_(size_t to_write, const LogString *desc) {
658 // Write bytes from handshake buffer, starting at handshake_buf_pos_
659 size_t bytes_to_write = to_write - this->handshake_buf_pos_;
660 ssize_t written = this->client_->write(this->handshake_buf_ + this->handshake_buf_pos_, bytes_to_write);
661
662 if (!this->handle_write_error_(written, desc)) {
663 return false;
664 }
665
666 this->handshake_buf_pos_ += written;
667 // Return true only if we have written all the requested bytes
668 return this->handshake_buf_pos_ >= to_write;
669}
670
672 this->client_->close();
673 this->client_ = nullptr;
674 this->client_connect_time_ = 0;
675 this->handshake_buf_pos_ = 0;
677 this->ota_features_ = 0;
678 this->backend_ = nullptr;
679#ifdef USE_OTA_PASSWORD
680 this->cleanup_auth_();
681#endif
682 // Intentionally no disable_loop() — letting loop() run one more iteration catches
683 // any connection that queued on the listener mid-session (otherwise the wake flag,
684 // set while we were in LOOP state, would be lost to enable_pending_loops_()).
685}
686
691
692#ifdef USE_OTA_PASSWORD
693void ESPHomeOTAComponent::log_auth_warning_(const LogString *msg) { ESP_LOGW(TAG, "Auth: %s", LOG_STR_ARG(msg)); }
694
696 bool client_supports_sha256 = (this->ota_features_ & CLIENT_FEATURE_SUPPORTS_SHA256_AUTH) != 0;
697
698 // Require SHA256
699 if (!client_supports_sha256) {
700 this->log_auth_warning_(LOG_STR("SHA256 required"));
702 return false;
703 }
705 return true;
706}
707
709 // Initialize auth buffer if not already done
710 if (!this->auth_buf_) {
711 // Select auth type based on client capabilities and configuration
712 if (!this->select_auth_type_()) {
713 return false;
714 }
715
716 // Generate nonce - hasher must be created and used in same stack frame
717 // CRITICAL ESP32-S3 HARDWARE SHA ACCELERATION REQUIREMENTS:
718 // 1. Hash objects must NEVER be passed to another function (different stack frame)
719 // 2. NO Variable Length Arrays (VLAs) - they corrupt the stack with hardware DMA
720 // 3. All hash operations (init/add/calculate) must happen in the SAME function where object is created
721 // Violating these causes truncated hash output (20 bytes instead of 32) or memory corruption.
722 //
723 // Buffer layout after AUTH_READ completes:
724 // [0]: auth_type (1 byte)
725 // [1...hex_size]: nonce (hex_size bytes) - our random nonce sent in AUTH_SEND
726 // [1+hex_size...1+2*hex_size-1]: cnonce (hex_size bytes) - client's nonce
727 // [1+2*hex_size...1+3*hex_size-1]: response (hex_size bytes) - client's hash
728
729 // CRITICAL ESP32-S2/S3 HARDWARE SHA ACCELERATION: Hash object must stay in same stack frame
730 // (no passing to other functions). All hash operations must happen in this function.
731 sha256::SHA256 hasher;
732
733 const size_t hex_size = hasher.get_size() * 2;
734 const size_t nonce_len = hasher.get_size() / 4;
735 const size_t auth_buf_size = 1 + 3 * hex_size;
736 this->auth_buf_ = std::make_unique<uint8_t[]>(auth_buf_size);
737 this->auth_buf_pos_ = 0;
738
739 char *buf = reinterpret_cast<char *>(this->auth_buf_.get() + 1);
740 if (!random_bytes(reinterpret_cast<uint8_t *>(buf), nonce_len)) {
741 this->log_auth_warning_(LOG_STR("Random failed"));
743 return false;
744 }
745
746 hasher.init();
747 hasher.add(buf, nonce_len);
748 hasher.calculate();
749 this->auth_buf_[0] = this->auth_type_;
750 hasher.get_hex(buf);
751
752 ESP_LOGV(TAG, "Auth: Nonce is %.*s", (int) hex_size, buf);
753 }
754
755 // Try to write auth_type + nonce
756 constexpr size_t hex_size = SHA256_HEX_SIZE;
757 const size_t to_write = 1 + hex_size;
758 size_t remaining = to_write - this->auth_buf_pos_;
759
760 ssize_t written = this->client_->write(this->auth_buf_.get() + this->auth_buf_pos_, remaining);
761 if (!this->handle_write_error_(written, LOG_STR("ack auth"))) {
762 return false;
763 }
764
765 this->auth_buf_pos_ += written;
766
767 // Check if we still have more to write
768 if (this->auth_buf_pos_ < to_write) {
769 return false; // More to write, try again next loop
770 }
771
772 // All written, prepare for reading phase
773 this->auth_buf_pos_ = 0;
774 return true;
775}
776
778 constexpr size_t hex_size = SHA256_HEX_SIZE;
779 const size_t to_read = hex_size * 2; // CNonce + Response
780
781 // Try to read remaining bytes (CNonce + Response)
782 // We read cnonce+response starting at offset 1+hex_size (after auth_type and our nonce)
783 size_t cnonce_offset = 1 + hex_size; // Offset where cnonce should be stored in buffer
784 size_t remaining = to_read - this->auth_buf_pos_;
785 ssize_t read = this->client_->read(this->auth_buf_.get() + cnonce_offset + this->auth_buf_pos_, remaining);
786
787 if (!this->handle_read_error_(read, LOG_STR("read auth"))) {
788 return false;
789 }
790
791 this->auth_buf_pos_ += read;
792
793 // Check if we still need more data
794 if (this->auth_buf_pos_ < to_read) {
795 return false; // More to read, try again next loop
796 }
797
798 // We have all the data, verify it
799 const char *nonce = reinterpret_cast<char *>(this->auth_buf_.get() + 1);
800 const char *cnonce = nonce + hex_size;
801 const char *response = cnonce + hex_size;
802
803 // CRITICAL ESP32-S2/S3 HARDWARE SHA ACCELERATION: Hash object must stay in same stack frame
804 // (no passing to other functions). All hash operations must happen in this function.
805 sha256::SHA256 hasher;
806
807 hasher.init();
808 hasher.add(this->password_.c_str(), this->password_.length());
809 hasher.add(nonce, hex_size * 2); // Add both nonce and cnonce (contiguous in buffer)
810 hasher.calculate();
811
812 ESP_LOGV(TAG, "Auth: CNonce is %.*s", (int) hex_size, cnonce);
813#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
814 char computed_hash[SHA256_HEX_SIZE + 1]; // Buffer for hex-encoded hash (max expected length + null terminator)
815 hasher.get_hex(computed_hash);
816 ESP_LOGV(TAG, "Auth: Result is %.*s", (int) hex_size, computed_hash);
817#endif
818 ESP_LOGV(TAG, "Auth: Response is %.*s", (int) hex_size, response);
819
820 // Compare response
821 bool matches = hasher.equals_hex(response);
822
823 if (!matches) {
824 this->log_auth_warning_(LOG_STR("Password mismatch"));
826 return false;
827 }
828
829 // Authentication successful - clean up auth state
830 this->cleanup_auth_();
831
832 return true;
833}
834
836 this->auth_buf_ = nullptr;
837 this->auth_buf_pos_ = 0;
838 this->auth_type_ = 0;
839}
840#endif // USE_OTA_PASSWORD
841
842} // namespace esphome
843#endif
void feed_wdt()
Feed the task watchdog.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
void status_momentary_error(const char *name, uint32_t length=5000)
Set error status flag and automatically clear it after a timeout.
void enable_loop_soon_any_context()
Thread and ISR-safe version of enable_loop() that can be called from any context.
void disable_loop()
Disable this component's loop.
void status_clear_warning()
Definition component.h:289
bool would_block_(int error_code) const
Definition ota_esphome.h:69
static constexpr size_t SHA256_HEX_SIZE
Definition ota_esphome.h:55
bool writeall_(const uint8_t *buf, size_t len)
bool try_read_(size_t to_read, const LogString *desc)
ota::OTABackendPtr backend_
Definition ota_esphome.h:97
bool try_write_(size_t to_write, const LogString *desc)
std::unique_ptr< uint8_t[]> auth_buf_
Definition ota_esphome.h:92
bool handle_write_error_(ssize_t written, const LogString *desc)
void log_auth_warning_(const LogString *msg)
float get_setup_priority() const override
void send_error_and_cleanup_(ota::OTAResponseTypes error)
Definition ota_esphome.h:83
bool handle_read_error_(ssize_t read, const LogString *desc)
void log_read_error_(const LogString *what)
bool readall_(uint8_t *buf, size_t len)
void set_port(uint16_t port)
Manually set the port OTA should listen on.
bool write_byte_(uint8_t byte)
Definition ota_esphome.h:64
uint8_t handshake_buf_[HANDSHAKE_BUF_SIZE]
static constexpr size_t HANDSHAKE_BUF_SIZE
void server_failed_(const LogString *msg)
void transition_ota_state_(OTAState next_state)
Definition ota_esphome.h:72
socket::ListenSocket * server_
Definition ota_esphome.h:95
void log_remote_closed_(const LogString *during)
std::unique_ptr< socket::Socket > client_
Definition ota_esphome.h:96
void log_start_(const LogString *phase)
void log_socket_error_(const LogString *msg)
void get_hex(char *output)
Retrieve the hash as hex characters. Output buffer must hold get_size() * 2 + 1 bytes.
Definition hash_base.h:29
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash.
Definition hash_base.h:35
void notify_state_(OTAState state, float progress, uint8_t error)
SHA256 hash implementation.
Definition sha256.h:51
void calculate() override
Definition sha256.cpp:27
size_t get_size() const override
Get the size of the hash in bytes (32 for SHA256)
Definition sha256.h:64
void add(const uint8_t *data, size_t len) override
Definition sha256.cpp:25
void init() override
Definition sha256.cpp:19
bool ready() const
Check if the socket has buffered data ready to read.
Definition socket.h:85
int bind(const struct sockaddr *addr, socklen_t addrlen)
int setsockopt(int level, int optname, const void *optval, socklen_t optlen)
std::unique_ptr< BSDSocketImpl > accept_loop_monitored(struct sockaddr *addr, socklen_t *addrlen)
uint16_t addr_len
uint32_t socklen_t
Definition headers.h:99
__int64 ssize_t
Definition httplib.h:178
struct lwip_sock * esphome_lwip_get_sock(int fd)
Look up a LwIP socket struct from a file descriptor.
void esphome_fast_select_set_ota_listener_sock(struct lwip_sock *sock)
Set the listener netconn that the fast-select callback filters OTA wakes against.
ESPHOME_ALWAYS_INLINE const char * get_use_address()
Get the active network hostname.
Definition util.h:57
@ OTA_TYPE_UPDATE_PARTITION_TABLE
Definition ota_backend.h:62
void get_running_app_position(uint32_t &offset, size_t &size)
@ OTA_RESPONSE_UPDATE_PREPARE_OK
Definition ota_backend.h:22
@ OTA_RESPONSE_SUPPORTS_COMPRESSION
Definition ota_backend.h:26
@ OTA_RESPONSE_BIN_MD5_OK
Definition ota_backend.h:23
@ OTA_RESPONSE_UPDATE_END_OK
Definition ota_backend.h:25
@ OTA_RESPONSE_RECEIVE_OK
Definition ota_backend.h:24
@ OTA_RESPONSE_CHUNK_OK
Definition ota_backend.h:27
@ OTA_RESPONSE_FEATURE_FLAGS
Definition ota_backend.h:28
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
Definition ota_backend.h:44
@ OTA_RESPONSE_ERROR_AUTH_INVALID
Definition ota_backend.h:32
@ OTA_RESPONSE_ERROR_UNKNOWN
Definition ota_backend.h:49
@ OTA_RESPONSE_REQUEST_SHA256_AUTH
Definition ota_backend.h:18
@ OTA_RESPONSE_ERROR_MAGIC
Definition ota_backend.h:30
@ OTA_RESPONSE_HEADER_OK
Definition ota_backend.h:20
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
Definition component.h:53
socklen_t set_sockaddr_any(struct sockaddr *addr, socklen_t addrlen, uint16_t port)
Set a sockaddr to the any address and specified port for the IP version used by socket_ip().
Definition socket.cpp:146
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
Definition socket.cpp:98
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
Definition helpers.cpp:20
void esphome_wake_ota_component_any_context()
const void size_t len
Definition hal.h:64
void HOT delay(uint32_t ms)
Definition hal.cpp:85
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:28
int written
Definition helpers.h:1045
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t