↓ Skip to main content

Assignment 1: Establish and Verify a TLS Connection

·5 mins·
Table of Contents
ESP32-S31-SECURITY - This article is part of a series.
Part 6: This Article

Assignment 1: Establish and verify a TLS connection
#

In this assignment, you will run the ESP-IDF https_mbedtls example on the ESP32-S31-Function-CoreBoard-1: configure trust anchors, complete a TLS handshake to a public HTTPS server, and run controlled tests that should fail verification. Read Lecture 1 first for the threat model, API flow, and menuconfig paths.

Before you begin
#

Tools: you can follow every assignment from the command line (CLI) or from the CLion IDE.

Prepared project (recommended): use the complete Assignment 1 project from the workshop project setup. The steps below explain the supplied implementation and its verification.

For the manual cumulative route, start from $IDF_PATH/examples/protocols/https_mbedtls and carry the same project through later assignments.

cd assignment-1
idf.py --preview set-target esp32s31

Run this from the code-examples directory created during workshop setup. For the manual route, replace assignment-1 with $IDF_PATH/examples/protocols/https_mbedtls.

Replace PORT in the commands below with your serial port.

  1. Follow the JetBrains ESP-IDF setup instructions to configure a CLion toolchain with the ESP-IDF environment.
  2. Select File → Open and open the assignment-1 project directory. For the manual route, open $IDF_PATH/examples/protocols/https_mbedtls.
  3. Open View → Tool Windows → Terminal and run:
idf.py --preview set-target esp32s31

Reload the CMake project if CLion prompts you.

Network: a Wi-Fi access point with internet access and outbound TCP port 443. The example connects to www.howsmyssl.com.

Step 1: Configure the trust anchor
#

Goal: enable the embedded CA bundle used by esp_crt_bundle_attach() (see Trust anchors and the certificate bundle in Lecture 1).

Open the project configuration:

idf.py menuconfig

Open View → Tool Windows → Terminal and run:

idf.py menuconfig

JetBrains documents menuconfig as a terminal-based step in the CLion ESP-IDF workflow.

Then:

  1. Open Component config → mbedTLS → Certificate Bundle.
  2. Enable Certificate Bundle (CONFIG_MBEDTLS_CERTIFICATE_BUNDLE).
  3. Leave the default filter list bundle unless your instructor asks for the full Mozilla set (larger binary, broader coverage).
  4. (Optional, for Step 3 untrusted-server tests) Set Custom Certificate Bundle Path to a folder with a lab CA PEM file: CONFIG_MBEDTLS_CUSTOM_CERTIFICATE_BUNDLE_PATH. Use .pem or .der extensions only, as documented in ESP x509 Certificate Bundle.
  5. Open Serial flasher config → Flash size and select 4 MB.
  6. Open Partition Table and select Single factory app (large), no OTA. This gives the application a 1.5 MB partition instead of the default 1 MB partition.

Save the configuration. Add these settings to sdkconfig.defaults in the project root so they survive a clean configuration:

CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
CONFIG_PARTITION_TABLE_SINGLE_APP_LARGE=y

Then build the project:

idf.py build
Select the generated app configuration in the configuration switcher, then click Build (the hammer icon). See Build the project.

Confirm that the build reports the application fits in the 1.5 MB factory partition.

Step 2: Connect with server authentication
#

Goal: bring up the network and complete one successful HTTPS GET (see Network setup and Reference example architecture).

  1. In the project configuration, open Example Connection Configuration.
  2. Under Connect using, select Wi-Fi. Set the lab SSID and password (see protocols README).
  3. Build, flash, and open the monitor:
idf.py -p PORT flash monitor

To quit the monitor, press Ctrl+].

  1. If the serial port is not detected, add ESPPORT with your port as an environment variable under Settings → Build, Execution, Deployment → CMake, then reload the project.
  2. Select the generated flash configuration and click Build.
  3. Select the generated monitor configuration and click Build to open the serial output.

See Flash and monitor an ESP32 chip in CLion.

Success criteria (order may vary slightly):

  • example_connect reports an IPv4 address on Wi-Fi
  • Log lines similar to: Attaching the certificate bundle..., Performing the SSL/TLS handshake...
  • esp-x509-crt-bundle: Certificate validated
  • Certificate verified.
  • A cipher suite line, for example Cipher suite is TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
  • HTTP response body containing JSON from howsmyssl (TLS version and cipher list)

Capture a screenshot or log excerpt for your workshop notes. This is useful test evidence for protection in transit (see CRA relevance in Lecture 1).

Step 3: Test certificate validation
#

Goal: confirm that verification fails closed when trust or hostname is wrong (see Hostname and certificate verification).

Revert to a known-good tree or git stash between tests. Rebuild and flash after each change.

TestChangeExpected result
Wrong hostnameIn main/https_mbedtls_example_main.c, call mbedtls_ssl_set_hostname(&ssl, "invalid.example.com") while still connecting to www.howsmyssl.com, or mismatch WEB_SERVER vs the cert nameNon-zero verify flags; Failed to verify peer certificate! and verification info in the log
Untrusted serverPoint WEB_SERVER / connect target at an HTTPS host that uses a self-signed or private-CA certificate, without adding that CA to the bundleHandshake failure or verify failure; connection must not treat the peer as trusted
Invalid lifetime (optional)With SNTP configured so wall time is correct, use an instructor-provided expired server cert or test endpointVerification failure related to certificate dates
Warning

Do not disable MBEDTLS_SSL_VERIFY_REQUIRED or set authmode to VERIFY_NONE in production firmware. Use misconfiguration only on lab builds to observe failures.

Document each test: what you changed, log output, and whether the device continued to reject the bad case after reboot.

Step 4: Add mutual TLS (optional)
#

The stock https_mbedtls example does not implement mTLS. This step is optional.

If your instructor assigns it:

  1. Read One-way TLS vs mutual TLS in Lecture 1.
  2. Follow ESP-IDF guidance for client certificates (for example ESP-TLS or mbedTLS mbedtls_ssl_conf_own_cert with PEM buffers embedded at build time).
  3. Use demonstration client cert and key from your instructor. Store keys in encrypted NVS in real products (Lecture 2).

What you learned
#

You should now be able to:

  • Enable the ESP-IDF certificate bundle and attach it with esp_crt_bundle_attach()
  • Run https_mbedtls on ESP32-S31 over Wi-Fi via example_connect()
  • Interpret success logs (Certificate validated, cipher suite, HTTP JSON response)
  • Trigger and recognize verification failures (hostname, untrusted CA, optional expiry)
  • Describe how monitor logs and test notes support a product trust store policy and CRA-oriented test evidence

Closing demo: TLS power consumption
#

After you finish the TLS exercises, the instructor will demonstrate TLS power consumption on the ESP32-S31-Function-CoreBoard-1 using a Qoitech Otii Ace Pro Power Profiler. You will observe the supply current during Wi-Fi startup and the TLS handshake while comparing plain HTTP, a full TLS handshake, and a resumed TLS session. No student wiring is required.

The measurements, results, and instructions for exploring the recordings are available in Power consumption measurement.

Previous and next steps
#

Previous: Lecture 1

Next: Power consumption measurement

Return to the workshop main page

ESP32-S31-SECURITY - This article is part of a series.
Part 6: This Article

Related