A device certificate is the passport a device shows every time it connects, and like a passport it has an end date. This is the first article of the series “Production details”. Each article takes one detail that a prototype usually leaves out and shows how to get it right in the first firmware.
What happens when a device certificate expires?
When a device certificate expires, every TLS endpoint that checks it rejects the device, and the device can no longer connect until it gets a new certificate. Every X.509 certificate carries a validity period with a notBefore and a notAfter date (RFC 5280, section 4.1.2.5), and a server that verifies client certificates refuses one outside it. The device keeps measuring and retrying. Its log shows the server’s alert, for example:
E esp-tls-mbedtls: mbedtls_ssl_handshake returned -0x7780E esp-tls: Failed to open new connection-0x7780 is MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE: the server sent a fatal alert, here certificate_expired. Nothing appears in the app’s logs, because the device never got far enough to send data. If the only way to reach the device is that connection, there is also no way to send it a fix.
Why do devices from the same batch stop on the same day?
Devices from the same batch stop on the same day because their certificates were issued in the same week with the same lifetime. Provisioning on a production line, or the first boot of a pilot fleet, takes a few days. A one-year lifetime then means that a few hundred devices reach their notAfter within the same few days a year later. For the customer, the whole installation goes quiet in the same week.

Why does renewal belong in the first firmware?
Renewal belongs in the first firmware because the logic that renews a certificate runs on the device, and new device logic can only arrive by a firmware update. An update needs a working connection, and when the certificate has expired the connection is gone. So the first firmware has to contain three things: the decision when to renew, the request for a new certificate, and a way back for a device that missed its window. The end date is useful, because it limits how long a stolen key can be misused. Without renewal, it also ends the device’s connection.
How long should a device certificate live?
A device certificate should live long enough that a sleeping device gets many chances to renew, and short enough that a stolen key does not stay useful for years. A practical default for operational certificates is one year, with renewal starting in the last third of the lifetime and at the latest four weeks before the end. Next to it, each device holds a second, long-lived bootstrap identity. IEEE 802.1AR describes this split: an initial device identity (IDevID) installed at manufacturing and meant to last as long as the device, and locally significant identities (LDevID) issued for operation. Here the bootstrap identity is used only to renew or re-enrol, and it can be revoked.
| Identity | Lifetime | Used for |
|---|---|---|
| Operational certificate | 1 year by default, shorter for always-connected devices | Every connection, every message |
| Bootstrap identity (IDevID in IEEE 802.1AR) | As long as the device lives | Only renewal and recovery |
| Root that anchors both | Longer than the devices it anchors | Signing issuing CAs, rarely used |
Some products use ten-year operational certificates to avoid renewal. A leaked key then stays valid for ten years, and only revocation stops it.
How do I build renewal into the first ESP32 firmware?
Build renewal as a window, not a date. The device asks for a new certificate on any wake inside the window and moves its own start by a per-device offset, so a batch does not renew on the same day. Each renewal creates a new key pair on the device and sends a certificate signing request (CSR), authenticated with the certificate that is still valid. Standard protocols for this exist, for example EST (RFC 7030).
The check that decides whether renewal is due, with Mbed TLS as it ships in ESP-IDF and a per-device offset derived from the MAC address:
#include <string.h>#include <time.h>#include "esp_mac.h"#include "mbedtls/x509_crt.h"static time_t to_epoch(const mbedtls_x509_time *t){ struct tm tm = { .tm_year = t->year - 1900, .tm_mon = t->mon - 1, .tm_mday = t->day, .tm_hour = t->hour, .tm_min = t->min, .tm_sec = t->sec }; return mktime(&tm); /* the device runs on UTC */}/* True when this device should ask for a new certificate now. */bool renewal_due(const char *cert_pem){ mbedtls_x509_crt crt; mbedtls_x509_crt_init(&crt); if (mbedtls_x509_crt_parse(&crt, (const unsigned char *)cert_pem, strlen(cert_pem) + 1) != 0) { mbedtls_x509_crt_free(&crt); return true; /* unreadable: re-enrol */ } time_t not_before = to_epoch(&crt.valid_from); time_t not_after = to_epoch(&crt.valid_to); mbedtls_x509_crt_free(&crt); uint8_t mac[6]; esp_read_mac(mac, ESP_MAC_WIFI_STA); uint32_t offset_permille = ((mac[4] << 8) | mac[5]) % 100; /* 0 to 9.9 % */ time_t lifetime = not_after - not_before; time_t opens = not_before + lifetime * 2 / 3 + lifetime / 1000 * offset_permille; time_t latest = not_after - 28 * 24 * 3600; /* at the latest 4 weeks before */ if (opens > latest) opens = latest; return time(NULL) >= opens;}Three details matter in the field:
- The clock must be right before the check. After a power cut an ESP32 starts at 1 January 1970. Sync time over SNTP before any certificate decision.
- Battery devices renew on any wake inside the window. A device that wakes once a day has dozens of chances inside a window of four months.
- The server confirms the decision. A server that sees a certificate inside the window can tell the device to renew, which also covers devices whose clock drifted.
Test it with certificates that live minutes instead of months, so a whole renewal cycle runs in an afternoon.
What if a device slept past its end date?
A device that slept past its end date re-enrols with its bootstrap identity. It cannot use the expired certificate, because a careful server rejects it. Instead it connects to a dedicated enrolment endpoint, proves who it is with the long-lived identity it got in the factory, sends a new CSR and receives a fresh operational certificate. The enrolment endpoint accepts the bootstrap identity for nothing else, so a stolen bootstrap key cannot be used to send data, and it can be revoked like any other identity.
Without this path, a device that was stored in a warehouse for 14 months, or switched off over a long holiday, can no longer connect at all.
Sources
- RFC 5280, Internet X.509 Public Key Infrastructure Certificate and CRL Profile: https://www.rfc-editor.org/rfc/rfc5280
- RFC 7030, Enrollment over Secure Transport (EST): https://www.rfc-editor.org/rfc/rfc7030
- IEEE 802.1AR, Secure Device Identity: https://1.ieee802.org/security/802-1ar/
- Espressif, ESP-IDF system time and SNTP: https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/system_time.html
- Microsoft, Azure IoT Hub device certificate renewal with DPS: https://learn.microsoft.com/en-us/azure/iot-dps/how-to-roll-certificates
Frequently asked questions
How long should an IoT device certificate be valid?
One year is a sensible default for operational device certificates, with renewal starting in the last third of the lifetime and at the latest four weeks before the end. Always-connected devices can use shorter lifetimes. Every lifetime needs automatic renewal, because every device outlives its first certificate.
Can an offline device renew its certificate?
Not while it is offline, because it has to reach the certificate service. That is why the renewal window opens months before the end date. A device that comes back after its end date re-enrols with a long-lived bootstrap identity that is only allowed to request a new certificate.
Is certificate renewal the same as key rotation?
No. Renewal issues a new certificate, possibly for the same key. Key rotation replaces the key pair. A good renewal flow creates a new key pair on the device and sends a new signing request, so both happen in one step.
What happens if the device clock is wrong?
A device with a wrong clock may reject valid certificates or renew at the wrong moment. Sync time before any certificate decision, and let the server confirm that renewal is due, because the server’s clock is reliable.
Do 10-year device certificates solve the problem?
No. A leaked key then stays valid for ten years, nobody remembers how to renew when the date comes, and the root that signed it must live even longer. Long lifetimes belong to the bootstrap identity, which can only renew, not to the certificate that carries data.