An ESP32 that runs ESP-IDF with its default configuration does not check the validity dates of certificates at all. Turning that check on is right for production, and it makes the device clock part of TLS. This article explains both sides, and why the fix belongs in the boot order of the first firmware. It is part of the series “Production details”, after certificate expiry, why IoT devices can take a website down and one key for every device.
Does an ESP32 check certificate dates by default?
No. In ESP-IDF the option CONFIG_MBEDTLS_HAVE_TIME_DATE, labelled “Enable mbedtls certificate expiry check”, is disabled by default. Mbed TLS then ignores the “valid from” and “valid to” fields of every certificate (ESP-IDF, Mbed TLS Kconfig). A device with the default setting accepts a server certificate that expired years ago, including one whose key leaked. The server side is not affected: a TLS server that verifies client certificates still rejects an expired device certificate.
Enable the check for production firmware. Espressif’s help text names the cost: the firmware must set a valid clock before TLS, and a device that stays offline until all of its trusted certificates have expired can no longer connect. Both are solvable, and both are cheaper than a device that trusts expired certificates forever.
Why does an ESP32 not know the time after a power cut?
An ESP32 does not know the time after a power cut because it has no battery-backed real-time clock. Its RTC timer keeps time in sleep modes and across software resets, but a power-on reset sets it back to zero, and zero is 1 January 1970 in Unix time (ESP-IDF system time). Until something sets the clock, the device’s time stays in 1970.
Why does a wrong clock break TLS?
A wrong clock breaks TLS once the date check is enabled, because Mbed TLS then rejects a certificate whose notBefore lies in the future or whose notAfter lies in the past. With a clock in 1970, the server certificate issued in 2026 is not yet valid, the verification sets the flag MBEDTLS_X509_BADCERT_FUTURE, and the log shows:
E esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x2700E esp-tls-mbedtls: Failed to verify peer certificate!I esp-tls-mbedtls: verification info: ! The certificate validity starts in the future-0x2700 is MBEDTLS_ERR_X509_CERT_VERIFY_FAILED. Wi-Fi works, DNS works, the server is up, and the handshake fails with a certificate error, which makes it look like a server problem in the logs.

Why is leaving the check off the wrong fix?
Leaving the date check off is the wrong fix because the end date is, next to revocation, the main protection against old and stolen keys. Turning the check off, or ignoring the verification flags, makes the error disappear in one line. Without the check, an expired server certificate, or one whose key leaked years ago, is accepted forever, and the setting stays as it is on every device that shipped with it.
Why does the boot order belong in the first firmware?
The boot order has to be right in the first firmware, because it can only change by an update. Whether the device syncs time before its first TLS connection, how long it waits, and what it does when no time server answers are written into the first firmware. A fleet that ships without it fails after the first power cut in the field, and the fix needs an update over the TLS connection that is failing.
How do I set the time before the first TLS connection?
Start SNTP right after the network is up, wait until the time is set, and only then open TLS. With the SNTP API in ESP-IDF 5:
#include "esp_netif_sntp.h"#include "esp_log.h"#include <time.h>#define BUILD_EPOCH 1764547200 /* set by the build: no valid time can be earlier */static bool time_is_plausible(void){ return time(NULL) > BUILD_EPOCH;}esp_err_t sync_time_before_tls(void){ esp_sntp_config_t cfg = ESP_NETIF_SNTP_DEFAULT_CONFIG_MULTIPLE(2, ESP_SNTP_SERVER_LIST("time.cloudflare.com", "pool.ntp.org")); esp_netif_sntp_init(&cfg); /* Wait up to 15 s for the first sync. */ if (esp_netif_sntp_sync_wait(pdMS_TO_TICKS(15000)) != ESP_OK || !time_is_plausible()) { ESP_LOGW("time", "no valid time yet, not opening TLS"); return ESP_ERR_TIMEOUT; /* retry later with backoff, do not skip the check */ } return ESP_OK;}Call sync_time_before_tls() before esp_mqtt_client_start() or the first HTTPS request, and treat a failure like any other connection failure: wait with backoff and try again.
Four more details:
- A lower bound. Store the build time in the firmware and the last synced time in flash. The device never accepts a time earlier than either, which also limits what a forged SNTP answer can do.
- More than one source. Use two time servers, or let the service you connect to provide time over its own authenticated channel.
- Deep sleep keeps time. A device that wakes from deep sleep still has a running RTC timer and does not need a full sync on every wake. The default internal RC oscillator drifts much more than an external 32 kHz crystal, so resync regularly; certificate checks tolerate minutes, not months.
- A root that outlives the device. Trust a root CA whose validity exceeds the device’s service life, so the date check never locks out a device that was offline for a long time.
What about renewal decisions and a drifting clock?
Renewal decisions depend on the clock too. A device that decides on its own when its certificate is due for renewal needs a correct date for that decision. Let the server confirm it when the device connects: the server sees the certificate’s dates and its own correct time, and can tell the device to renew. The device’s clock then only has to be good enough to get through the handshake.
Sources
- Espressif, ESP-IDF system time and SNTP: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/system_time.html
- Espressif, ESP-IDF Mbed TLS Kconfig (MBEDTLS_HAVE_TIME_DATE): https://github.com/espressif/esp-idf/blob/master/components/mbedtls/Kconfig
- RFC 5280, certificate validity period: https://www.rfc-editor.org/rfc/rfc5280
Frequently asked questions
Does an ESP32 check certificate expiry by default?
No. In ESP-IDF, CONFIG_MBEDTLS_HAVE_TIME_DATE is disabled by default, and Mbed TLS then ignores the valid from and valid to fields of every certificate. Enabling it makes Mbed TLS compare them with the system time and reject certificates that are not yet or no longer valid.
Why does my ESP32 think it is 1970?
The ESP32 has no battery-backed real-time clock. Its RTC timer keeps time across deep sleep and software resets, but a power-on reset sets it back to zero, which is 1 January 1970 in Unix time, until SNTP or another source sets it.
Why does TLS fail with a wrong clock?
With the expiry check enabled, Mbed TLS checks that the server certificate and the certificates in its chain are inside their validity period. With a clock in 1970, every notBefore date lies in the future, verification sets MBEDTLS_X509_BADCERT_FUTURE and the handshake fails with -0x2700.
Is it safe to leave the certificate time check off?
No. Without the check the device accepts expired server certificates forever, including certificates whose keys leaked years ago. Espressif names the trade-off: with the check on, a device that stayed offline until all trusted certificates expired cannot connect. Plan for that with a long-lived root and time sync, instead of leaving the check off.
Is SNTP secure?
Plain SNTP is not authenticated, so an attacker on the network could send a wrong time. The risk is limited if the device only uses time for validity checks and the server confirms renewal decisions. Use several time sources and reject times before the firmware build date.
What if the device cannot reach a time server?
Keep a lower bound: the firmware build time and the last time successfully synced, stored in flash. A device should never believe a time earlier than that. If the network has no time server, the gateway or the service you connect to can provide one.