2#ifdef USE_OTA_ENCRYPTION_PROVISIONED
21#ifdef USE_LWIP_FAST_SELECT
35#ifdef USE_OTA_ENCRYPTION
37#ifdef USE_OTA_ENCRYPTION_PROVISIONED
46static constexpr uint16_t OTA_BLOCK_SIZE = 8192;
47static constexpr uint32_t OTA_SOCKET_TIMEOUT_HANDSHAKE = 60000;
51static constexpr uint32_t OTA_SOCKET_TIMEOUT_DATA = 105000;
54static constexpr uint32_t OTA_HANDSHAKE_PREEMPT = 10000;
55static constexpr uint32_t OTA_PROGRESS_INTERVAL_MS = 1000;
56static constexpr size_t OTA_SIZE_FIELD_BYTES = 4;
64 if (global_esphome_ota_component !=
nullptr) {
70#ifdef USE_OTA_ENCRYPTION_PROVISIONED
86 int err = this->
server_->
setsockopt(SOL_SOCKET, SO_REUSEADDR, &enable,
sizeof(
int));
118 global_esphome_ota_component =
this;
119#ifdef USE_LWIP_FAST_SELECT
125#ifdef USE_OTA_PARTITIONS
131 char addr_buf[network::USE_ADDRESS_BUFFER_SIZE];
133 "Over-The-Air updates:\n"
136#ifdef USE_OTA_ENCRYPTION
141#ifdef USE_OTA_ENCRYPTION_REQUIRED
143 LOG_STR_LITERAL(
"required")
144#elif defined(USE_OTA_ENCRYPTION_PROVISIONED)
147 this->
noise_context_().has_psk() ? LOG_STR_LITERAL(
"offered, plaintext accepted")
148 : LOG_STR_LITERAL(
"offered once the api key is provisioned")
149#elif defined(USE_OTA_ENCRYPTION)
151 LOG_STR_LITERAL(
"offered, plaintext accepted")
154#ifdef USE_OTA_PASSWORD
156 ESP_LOGCONFIG(TAG,
" Password configured");
159#ifdef USE_OTA_PARTITIONS
161 " Partition access allowed\n"
163 " Partition address: 0x%" PRIX32
"\n"
164 " Used size: %zu bytes (0x%zX)",
169 " Partition table:\n"
170 " %-12s %-4s %-8s %-10s %-10s",
171 "Name",
"Type",
"Subtype",
"Address",
"Size");
172 esp_partition_iterator_t it = esp_partition_find(ESP_PARTITION_TYPE_ANY, ESP_PARTITION_SUBTYPE_ANY,
nullptr);
173 while (it !=
nullptr) {
174 const esp_partition_t *partition = esp_partition_get(it);
175 ESP_LOGCONFIG(TAG,
" %-12s 0x%-2X 0x%-6X 0x%-8" PRIX32
" 0x%-8" PRIX32, partition->label, partition->type,
176 partition->subtype, partition->address, partition->size);
177 it = esp_partition_next(it);
179 esp_partition_iterator_release(it);
180 esp_bootloader_desc_t bootloader_desc;
181 esp_err_t err = esp_ota_get_bootloader_description(
nullptr, &bootloader_desc);
182 ESP_LOGCONFIG(TAG,
" Bootloader: ESP-IDF %s",
183 (err == ESP_OK) ? bootloader_desc.idf_ver : LOG_STR_LITERAL(
"version unknown"));
202static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_DEFLATE_RTL8710B = 0x40;
203static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_COMPRESSION = 0x01;
204static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_SHA256_AUTH = 0x02;
205static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL = 0x04;
206static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_NOISE = 0x08;
207static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_DEFLATE = 0x10;
208#ifdef ESPHOME_OTA_UDP
209static constexpr uint8_t CLIENT_FEATURE_SUPPORTS_UDP = 0x20;
212static constexpr uint8_t CLIENT_NOISE_FEATURES =
213 CLIENT_FEATURE_SUPPORTS_NOISE | CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL;
214static constexpr uint8_t SERVER_FEATURE_SUPPORTS_COMPRESSION = 0x01;
215static constexpr uint8_t SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS = 0x02;
216static constexpr uint8_t SERVER_FEATURE_SUPPORTS_NOISE = 0x04;
219static constexpr uint8_t SERVER_FEATURE_SUPPORTS_DEFLATE = 0x08;
220#ifdef ESPHOME_OTA_UDP
221static constexpr uint8_t SERVER_FEATURE_SUPPORTS_UDP = 0x10;
222static constexpr uint8_t UDP_REQUEST_V1 = 0x01;
225#ifdef USE_OTA_ENCRYPTION
227 return (this->
handshake_buf_[1] & SERVER_FEATURE_SUPPORTS_NOISE) != 0 &&
228 (this->
ota_features_ & CLIENT_NOISE_FEATURES) == CLIENT_NOISE_FEATURES;
233#ifdef USE_OTA_ENCRYPTION_REQUIRED
237 return (this->
ota_features_ & CLIENT_FEATURE_SUPPORTS_EXTENDED_PROTOCOL) != 0;
248 if (this->
client_ ==
nullptr) {
257 int err = this->
client_->setsockopt(IPPROTO_TCP, TCP_NODELAY, &enable,
sizeof(
int));
263 err = this->
client_->setblocking(
false);
278 ESP_LOGW(TAG,
"Handshake timeout");
284 ESP_LOGD(TAG,
"Newer connection waiting");
292 if (!this->
try_read_(5, LOG_STR(
"read magic"))) {
298#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_WARN
300 ESP_LOGW(TAG,
"Magic bytes mismatch! %s",
316 if (!this->
try_write_(2, LOG_STR(
"ack magic"))) {
327 if (!this->
try_read_(1, LOG_STR(
"read feature"))) {
333#ifdef USE_OTA_ENCRYPTION_REQUIRED
335 if ((this->
ota_features_ & CLIENT_NOISE_FEATURES) != CLIENT_NOISE_FEATURES) {
336 ESP_LOGW(TAG,
"Client does not support encryption");
344 const bool supports_compression =
345 (this->
ota_features_ & CLIENT_FEATURE_SUPPORTS_COMPRESSION) != 0 && ota::OTABackend::supports_compression();
351 static_assert(
HANDSHAKE_BUF_SIZE >= 2,
"handshake_buf_ must hold the 2-byte extended-protocol feature ack");
353 this->
handshake_buf_[1] = (supports_compression ? SERVER_FEATURE_SUPPORTS_COMPRESSION : 0);
354#ifdef ESPHOME_OTA_UDP
357#ifdef USE_OTA_PARTITIONS
358 this->
handshake_buf_[1] |= SERVER_FEATURE_SUPPORTS_PARTITION_ACCESS;
360#ifdef USE_OTA_ENCRYPTION_PROVISIONED
365#elif defined(USE_OTA_ENCRYPTION)
369#ifdef USE_OTA_ENCRYPTION
376#ifdef USE_OTA_DEFLATE
378 if ((this->
ota_features_ & (CLIENT_FEATURE_SUPPORTS_DEFLATE | CLIENT_FEATURE_SUPPORTS_DEFLATE_RTL8710B)) != 0) {
387 ESP_LOGW(TAG,
"No memory to inflate");
399 static constexpr size_t STANDARD_PROTO_ACK_SIZE = 1;
400 static constexpr size_t EXTENDED_PROTO_ACK_SIZE = 2;
401 const size_t ack_size = this->
extended_proto_() ? EXTENDED_PROTO_ACK_SIZE : STANDARD_PROTO_ACK_SIZE;
402 if (!this->
try_write_(ack_size, LOG_STR(
"ack feature"))) {
405#ifdef USE_OTA_ENCRYPTION
418#ifdef USE_OTA_PASSWORD
431#ifdef USE_OTA_PASSWORD
436#ifdef USE_OTA_PASSWORD
460#ifdef USE_OTA_ENCRYPTION
513 char *sbuf =
reinterpret_cast<char *
>(buf);
516#ifdef ESPHOME_OTA_UDP
518 this->
udp_ =
nullptr;
525 this->
client_->setsockopt(SOL_SOCKET, SO_RCVTIMEO, &tv,
sizeof(tv));
526 this->
client_->setsockopt(SOL_SOCKET, SO_SNDTIMEO, &tv,
sizeof(tv));
527 if (this->
client_->setblocking(
true) != 0) {
535#ifdef ESPHOME_OTA_UDP
546 if (buf[0] == UDP_REQUEST_V1) {
548 if (udp_link.
reply[0] == 0) {
549 ESP_LOGW(TAG,
"UDP unavailable, using TCP");
554 if (udp_link.
reply[0] != 0) {
555 this->
udp_ = &udp_link;
577 ESP_LOGV(TAG,
"OTA type is 0x%02x", ota_type);
588#ifdef USE_OTA_DEFLATE
589 if (this->
inflate_ !=
nullptr && !this->
read_size_(buf, image_size, LOG_STR(
"image size"))) {
595#ifndef USE_OTA_PARTITIONS
608#ifdef USE_OTA_STATE_LISTENER
613 error_code = this->
backend_->begin(image_size, ota_type);
627 ESP_LOGV(TAG,
"Update: Binary MD5 is %s", sbuf);
628 this->
backend_->set_update_md5(sbuf);
634#ifdef USE_OTA_DEFLATE
660 ESP_LOGW(TAG,
"End update err %d", error_code);
667#ifdef ESPHOME_OTA_UDP
669 if (this->
udp_ !=
nullptr) {
681 ESP_LOGI(TAG,
"Update complete");
683#ifdef USE_OTA_STATE_LISTENER
687#ifdef USE_OTA_PARTITIONS
698#ifdef ESPHOME_OTA_UDP
714#ifdef USE_OTA_STATE_LISTENER
722 while (
len - at > 0) {
724 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
725 ESP_LOGW(TAG,
"Timeout reading %zu bytes",
len);
731 const int err = errno;
733 ESP_LOGW(TAG,
"Read err %zu bytes, errno %d",
len, err);
736 }
else if (read == 0) {
740 ESP_LOGW(TAG,
"Remote closed after %u of %zu bytes", (
unsigned) at,
len);
754#ifdef ESPHOME_OTA_UDP
755 if (this->
udp_ !=
nullptr)
760 while (
len - at > 0) {
762 if (now - start > OTA_SOCKET_TIMEOUT_DATA) {
763 ESP_LOGW(TAG,
"Timeout writing %zu bytes",
len);
769 const int err = errno;
771 ESP_LOGW(TAG,
"Write err %zu bytes, errno %d",
len, err);
788 ESP_LOGW(TAG,
"Socket %s: errno %d", LOG_STR_ARG(msg), errno);
795 ESP_LOGD(TAG,
"Client left after the handshake");
805 ESP_LOGW(TAG,
"Read %s failed", LOG_STR_ARG(what));
809 char peername[socket::SOCKADDR_STR_LEN];
810#ifdef ESPHOME_OTA_UDP
811 if (this->
client_ ==
nullptr) {
813#ifdef ESPHOME_OTA_UDP_LWIP
814 ipaddr_ntoa_r(&this->
udp_->
peer_ip, peername,
sizeof(peername));
822 this->
client_->getpeername_to(peername);
824 ESP_LOGD(TAG,
"Starting %s from %s", LOG_STR_ARG(phase), peername);
828 ESP_LOGW(TAG,
"Remote closed at %s", LOG_STR_ARG(during));
874 this->handshake_buf_pos_ += read;
876 return this->handshake_buf_pos_ >= to_read;
888 this->handshake_buf_pos_ +=
written;
890 return this->handshake_buf_pos_ >= to_write;
897 ESP_LOGV(TAG,
"%s is %zu bytes", LOG_STR_ARG(desc),
size);
904 ESP_LOGW(TAG,
"Flash write err %d", result);
919 ESP_LOGW(TAG,
"No data received for %u ms", (
unsigned) OTA_SOCKET_TIMEOUT_DATA);
922#ifdef USE_OTA_ENCRYPTION
923 if (this->
noise_ !=
nullptr) {
957 ESP_LOGD(TAG,
"Progress: %0.1f%%", percentage);
958#ifdef USE_OTA_STATE_LISTENER
968#if USE_OTA_VERSION == 2
969#ifdef ESPHOME_OTA_UDP
971 if (this->
udp_ !=
nullptr)
983#ifdef ESPHOME_OTA_UDP
984 this->
udp_ =
nullptr;
996#ifdef USE_OTA_PASSWORD
999#ifdef USE_OTA_ENCRYPTION
1002#ifdef USE_OTA_DEFLATE
1015#ifdef USE_OTA_PASSWORD
1019 bool client_supports_sha256 = (this->
ota_features_ & CLIENT_FEATURE_SUPPORTS_SHA256_AUTH) != 0;
1022 if (!client_supports_sha256) {
1056 const size_t hex_size = hasher.
get_size() * 2;
1057 const size_t nonce_len = hasher.
get_size() / 4;
1058 const size_t auth_buf_size = 1 + 3 * hex_size;
1069 char *buf =
reinterpret_cast<char *
>(this->
auth_buf_.get() + 1);
1070 if (!
random_bytes(
reinterpret_cast<uint8_t *
>(buf), nonce_len)) {
1077 hasher.
add(buf, nonce_len);
1082 ESP_LOGV(TAG,
"Auth: Nonce is %.*s", (
int) hex_size, buf);
1087 const size_t to_write = 1 + hex_size;
1095 this->auth_buf_pos_ +=
written;
1098 if (this->auth_buf_pos_ < to_write) {
1103 this->auth_buf_pos_ = 0;
1109 const size_t to_read = hex_size * 2;
1113 size_t cnonce_offset = 1 + hex_size;
1121 this->auth_buf_pos_ += read;
1124 if (this->auth_buf_pos_ < to_read) {
1129 const char *nonce =
reinterpret_cast<char *
>(this->
auth_buf_.get() + 1);
1130 const char *cnonce = nonce + hex_size;
1131 const char *response = cnonce + hex_size;
1138 hasher.
add(this->
password_.c_str(), this->password_.length());
1139 hasher.
add(nonce, hex_size * 2);
1142 ESP_LOGV(TAG,
"Auth: CNonce is %.*s", (
int) hex_size, cnonce);
1143#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
1145 hasher.
get_hex(computed_hash);
1146 ESP_LOGV(TAG,
"Auth: Result is %.*s", (
int) hex_size, computed_hash);
1148 ESP_LOGV(TAG,
"Auth: Response is %.*s", (
int) hex_size, response);
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_set_warning()
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_momentary_error(uint32_t length=5000)
Set error status flag and clear it after length ms.
void status_clear_warning()
ESPHomeOTAComponent provides a simple way to integrate Over-the-Air updates into your app using Ardui...
static constexpr size_t OTA_BUFFER_SIZE
bool handle_noise_handshake_()
Drive the non-blocking handshake from loop(); returns true once the transport ciphers are ready.
ssize_t receive_data_(uint8_t *buf, DataTransfer &xfer)
bool would_block_(int error_code) const
uint32_t running_app_offset_
uint8_t handshake_buf_pos_
static constexpr size_t SHA256_HEX_SIZE
static constexpr uint8_t MAGIC_BYTES[5]
bool udp_send_(const uint8_t *data, size_t len)
ota::OTAResponseTypes write_flash_(uint8_t *data, size_t len)
void yield_and_feed_watchdog_()
bool writeall_(const uint8_t *buf, size_t len)
bool try_read_(size_t to_read, const LogString *desc)
bool data_readall_(uint8_t *buf, size_t len)
noise::NoiseContext noise_ctx_
ssize_t transport_read_(uint8_t *buf, size_t len)
void ack_written_(DataTransfer &xfer)
bool noise_start_session_(uint8_t server_feature_flags)
Start the responder handshake, on the session reserved at offer time.
ota::OTABackendPtr backend_
bool try_write_(size_t to_write, const LogString *desc)
void dump_config() override
bool noise_offered_() const
RAMUniquePtr< NoiseSession > noise_
bool extended_proto_() const
bool handle_write_error_(ssize_t written, const LogString *desc)
void send_chunk_acks_(DataTransfer &xfer)
bool data_write_byte_(uint8_t byte)
void log_auth_warning_(const LogString *msg)
uint32_t client_connect_time_
RAMUniquePtr< InflateSession > inflate_
float get_setup_priority() const override
bool client_left_before_start_()
void send_error_and_cleanup_(ota::OTAResponseTypes error)
bool handle_read_error_(ssize_t read, const LogString *desc)
ssize_t noise_read_data_(uint8_t *buf, size_t capacity)
Blocking read of one data-phase frame, decrypted in place; returns the plaintext size,...
void log_read_error_(const LogString *what)
bool readall_(uint8_t *buf, size_t len)
bool data_write_(const uint8_t *data, size_t len)
RAMUniquePtr< uint8_t[]> auth_buf_
RAMUniquePtr< noise::psk_t > saved_psk_
void ack_received_(DataTransfer &xfer)
uint8_t handshake_buf_[HANDSHAKE_BUF_SIZE]
static constexpr size_t HANDSHAKE_BUF_SIZE
ota::OTAResponseTypes inflate_data_(uint8_t *in, size_t image_size, DataTransfer &xfer)
const noise::NoiseContext & noise_context_() const
void server_failed_(const LogString *msg)
void transition_ota_state_(OTAState next_state)
void cleanup_connection_()
bool read_size_(uint8_t *buf, size_t &size, const LogString *desc)
void noise_reserve_session_()
socket::ListenSocket * server_
void log_remote_closed_(const LogString *during)
std::unique_ptr< socket::Socket > client_
void udp_open_(UdpLink &link)
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.
bool equals_hex(const char *expected)
Compare the hash against a provided hex-encoded hash of get_size() * 2 characters,...
An STL allocator that uses SPI or internal RAM.
RAMUniquePtr< T > make_unique(Args &&...args)
Value initialize one T; empty on exhaustion.
RAMUniquePtr< T[]> make_unique_array_for_overwrite(size_t n)
n elements left uninitialized, as std::make_unique_for_overwrite does; empty on exhaustion,...
noise::NoiseContext & get_noise_ctx()
void set_psk(const uint8_t *psk)
psk points at 32 bytes that outlive the context (PROGMEM or caller owned RAM); nullptr means no key.
void notify_state_(OTAState state, float progress, uint8_t error)
SHA256 hash implementation.
void calculate() override
size_t get_size() const override
Get the size of the hash in bytes (32 for SHA256)
void add(const uint8_t *data, size_t len) override
int setblocking(bool blocking)
bool ready() const
Check if the socket has buffered data ready to read.
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)
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.
APIServer * global_api_server
bool load_saved_noise_psk(noise::psk_t &out)
One-shot read of the provisioned key for a boot without an api server (safe mode); false when there i...
ESPHOME_LOG_TAG(TAG, CRASH_LOG_TAG)
const char * get_use_address_to(std::span< char, USE_ADDRESS_BUFFER_SIZE > buf)
Get the active network address for logging.
std::array< uint8_t, 32 > psk_t
@ OTA_TYPE_UPDATE_PARTITION_TABLE
void get_running_app_position(uint32_t &offset, size_t &size)
@ OTA_RESPONSE_UPDATE_PREPARE_OK
@ OTA_RESPONSE_ERROR_ENCRYPTION_REQUIRED
@ OTA_RESPONSE_SUPPORTS_COMPRESSION
@ OTA_RESPONSE_BIN_MD5_OK
@ OTA_RESPONSE_UPDATE_END_OK
@ OTA_RESPONSE_RECEIVE_OK
@ OTA_RESPONSE_FEATURE_FLAGS
@ OTA_RESPONSE_ERROR_UNSUPPORTED_OTA_TYPE
@ OTA_RESPONSE_ERROR_AUTH_INVALID
@ OTA_RESPONSE_ERROR_UNKNOWN
@ OTA_RESPONSE_REQUEST_SHA256_AUTH
@ OTA_RESPONSE_ERROR_MAGIC
std::unique_ptr< ArduinoLibreTinyOTABackend > make_ota_backend()
constexpr float AFTER_WIFI
For components that should be initialized after WiFi is connected.
size_t format_sockaddr_to(const struct sockaddr *addr_ptr, socklen_t len, std::span< char, SOCKADDR_STR_LEN > buf)
Format sockaddr into caller-provided buffer, returns length written (excluding null)
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().
std::unique_ptr< ListenSocket > socket_ip_loop_monitored(int type, int protocol)
bool random_bytes(uint8_t *data, size_t len)
Generate len random bytes using the platform's secure RNG (hardware RNG or OS CSPRNG).
void esphome_wake_ota_component_any_context()
char * format_hex_pretty_to(char *buffer, size_t buffer_size, const uint8_t *data, size_t length, char separator)
Format byte array as uppercase hex to buffer (base implementation).
constexpr size_t format_hex_pretty_size(size_t byte_count)
Calculate buffer size needed for format_hex_pretty_to with separator: "XX:XX:...:XX\0".
constexpr uint32_t encode_uint32(uint8_t byte1, uint8_t byte2, uint8_t byte3, uint8_t byte4)
Encode a 32-bit value given four bytes in most to least significant byte order.
void HOT delay(uint32_t ms)
uint32_t IRAM_ATTR HOT millis()
Application App
Global storage of Application pointer - only one Application can exist.
struct sockaddr_storage peer