Try Embedded

HTTPS and certificates (ESP32-C3)

Fetch JSON over HTTPS, verifying the server against a certificate authority, and see a spoofed server refused.

Communication · Advanced

In a real product: Connected payment terminal (Telecom and networking): Talks only to its verified server over HTTPS, refusing a spoofed one.

HTTPS is HTTP inside TLS: the server proves who it is with a certificate, and everything after the handshake is encrypted. The proof is only as good as the check the client makes. Here the ESP32-C3 trusts one certificate authority, the lab's, and fetches from two servers: api.weather.lab, whose certificate that authority signed, and spoofed.weather.lab, which presents a certificate from an authority the sketch does not know.

What you'll learn

  • WiFiClientSecure with setCACert: verifying the chain and the host name
  • What a refused handshake looks like, and how to read lastError
  • Why setInsecure() is never the fix

How it works

setCACert(LAB_CA) gives mbedTLS the root it trusts. During the handshake the server sends its certificate and the chain up to its authority; the client checks each signature up to LAB_CA and that the certificate names the host it asked for. The first server passes and the request goes through. The second fails: no request is ever sent to it, and lastError says why.

The servers are simulated, and so is the lab's authority: the Network panel's Copy lab CA button gives its certificate, the same text as LAB_CA. In diagram.json, a host's "certificate" can be "untrusted" (another authority) or "wrong-host" (issued for another name). The Arduino ESP32 core does not check certificate dates.

Try this

  • Replace setCACert(LAB_CA) with setInsecure(): the spoofed server's -40 comes through. That is the attack.
  • Set "certificate": "wrong-host" on api.weather.lab and run: the chain is fine, but the name is not.
  • Watch the TLS rows in the Network panel: each handshake, verified or refused, with the alert the board sent.

sketch.ino

// HTTPS with certificate checking: fetches JSON from a server whose certificate the lab's authority signed, then
// tries a server signed by an authority the sketch does not trust, and shows why the handshake was refused.
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

// The lab's certificate authority (the Network panel's "Copy lab CA" gives the same text). A real device carries the
// root certificate of the authority its servers use.
const char *LAB_CA = R"PEM(
-----BEGIN CERTIFICATE-----
MIIDLzCCAhegAwIBAgIBATANBgkqhkiG9w0BAQsFADA5MRkwFwYDVQQKDBBUcnkg
RW1iZWRkZWQgTGFiMRwwGgYDVQQDDBNUcnkgRW1iZWRkZWQgTGFiIENBMB4XDTI1
MDEwMTAwMDAwMFoXDTQ1MDEwMTAwMDAwMFowOTEZMBcGA1UECgwQVHJ5IEVtYmVk
ZGVkIExhYjEcMBoGA1UEAwwTVHJ5IEVtYmVkZGVkIExhYiBDQTCCASIwDQYJKoZI
hvcNAQEBBQADggEPADCCAQoCggEBANre9H2KT07xjk1yIebmct4ngM6RHCvVEG3D
l805J9nUG5CVeCbIUsiAXzUtKSHtDUS/PSfXhwu60Q76/bEZdI95SkYiNdmVe185
W5pllFpdo6yVX39HSqKBOQ2vb54kLm3vQnNE+X/MMy7G0jGx+LO/R8C7Dild1LQ8
334NMc6SnjSy1/MHEodsgQCnoTuOGQwuVrgiKOb/j5vU9HFSrMq18uR7YbIrKHsQ
wRBWGk3hVRv++z/mHBF98CJrL1F5AsJTs17hF5bF2hme+JJKpOr4tErXkTYXhrXk
SJR8ulkvTaD4c0GlWC2vWgcHBZ0I+0SrVFLYVtd6306GzRHQPm0CAwEAAaNCMEAw
DwYDVR0TAQH/BAUwAwEB/zAOBgNVHQ8BAf8EBAMCAAYwHQYDVR0OBBYEFJTfmqQY
asexWklso6zRvnttfTnEMA0GCSqGSIb3DQEBCwUAA4IBAQBU+zc87KbPA+E8nEtM
IYSq7HQb6z0k44R8sIZPytSO0o2Y8Hss2z++tYoOHVQfHHQYXcoN8R3A5RL3CyGi
6wkIGsJ2+VsaaYjYm3GdHazr1qELS9PNUGywE2asBRYYqf8cJHzHlvo8bjHaWk0/
N1h4S8obBb0yJfaJ0Hy2x8n7U/Bon9F7fJYw/46oNxibSPE9wLCHEuClxrnT4tFg
Ksg8GJ74D+MKs3guVNqfHZBdLnR7cGhXxWEiebthMwX/Psu+2EcZpykUGYTneWY6
E5T42ln7SRt4b7/6vs5EZVG72bS+p/jtO9Ok8TMtXJuWT2ePJSB9TiX1pDFZZfek
NYGz
-----END CERTIFICATE-----
)PEM";

void fetch(const char *url) {
  WiFiClientSecure client;
  client.setCACert(LAB_CA);   // verify the server's certificate chain and its host name
  HTTPClient http;
  http.begin(client, url);
  int code = http.GET();
  if (code > 0) {
    Serial.printf("%s -> %d %s\n", url, code, http.getString().c_str());
  } else {
    char why[100];
    client.lastError(why, sizeof why);
    Serial.printf("%s -> refused: %s\n", url, why);
  }
  http.end();
}

void setup() {
  Serial.begin(115200);
  WiFi.begin("TryEmbedded", "");
  while (WiFi.status() != WL_CONNECTED) delay(100);
  fetch("https://api.weather.lab/now");
  fetch("https://spoofed.weather.lab/now");
}

void loop() {}

Lessons behind it