Skip to content

Example scripts using SSL/TLS in MicroPython. Use OpenSSL to create keys and certificates

License

Notifications You must be signed in to change notification settings

shariltumin/ssl-tls-examples-micropython

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🔐 Secure Socket Communication with TLS/SSL in MicroPython

Setting up secure communication between devices—especially in the world of microcontrollers—isn’t always easy. If you’re confused, don’t worry! Even experienced sysadmins sometimes scratch their heads over SSL and TLS.

This guide walks you through the basics of secure socket communication, with a focus on resource-constrained environments like MicroPython. Whether you're building a connected sensor or a secure IoT gateway, this will help you understand how to protect your data.

🛡️ 1. What is SSL/TLS and Why It Matters

SSL (Secure Sockets Layer) and TLS (Transport Layer Security) are cryptographic protocols that secure data sent over networks.

They provide:

  • Confidentiality – Encrypts data to prevent eavesdropping.
  • Integrity – Ensures data isn’t tampered with during transmission.
  • Authentication – Verifies the identity of the server or client.

Why Use It in Embedded Projects?

  • Secure sensitive data like sensor readings or API keys.
  • Enable secure communication with cloud services (e.g., HTTPS, MQTT).
  • Prevent man-in-the-middle attacks and device impersonation.

🤝 2. TLS Handshake: How Devices Establish Trust

When two devices connect securely, they go through a TLS handshake to negotiate encryption and authentication. There are three handshake modes:

🔓 Mode 1: No Authentication

🔓 TLS Mode 1 – No Authentication

Client                             Server
  |                                  |
  | --- Propose Cipher Suites -----> |
  |                                  |
  | <----- Select Cipher Suite ------|
  |                                  |
  | --- Generate Session Key ------> |
  |                                  |
  | <----- Acknowledge Key --------- |
  |                                  |
  | <=== Encrypted Communication ===>|

How it works:

  • Client and server agree on encryption (e.g., AES).
  • Generate session keys.
  • No certificates involved.

Use case:

  • Testing on trusted local networks.

⚠️ Warning: Do not use in production! Vulnerable to MITM attacks.

🔐 Mode 2: Server Authentication (Standard HTTPS)

Client                             Server
  |                                  |
  | --- Propose Cipher Suites -----> |
  |                                  |
  | <--- Server Certificate ---------|
  |                                  |
  | --- Verify Certificate --------->| (Checks: CA, Expiry, Domain)
  |                                  |
  | --- Generate Encrypted Key ----> |
  |                                  |
  | <----- Acknowledge Key --------- |
  |                                  |
  | <=== Encrypted Communication ===>|

How it works:

  • Server sends a certificate (from a trusted Certificate Authority).
  • Client verifies:
    • Valid certificate chain.
    • Certificate hasn't expired or been revoked.
    • Domain name matches.
  • Secure session is established.

Use case:

  • IoT devices communicating with cloud APIs (e.g., https://api.example.com).

Benefit: Protects against fake servers or impersonation.

🔐🔐 Mode 3: Mutual TLS (mTLS)

Client                             Server
  |                                  |
  | --- Propose Cipher Suites -----> |
  |                                  |
  | <--- Server Certificate ---------|
  |                                  |
  | --- Verify Server Certificate -->|
  |                                  |
  | --- Client Certificate --------->|
  |                                  |
  | <--- Verify Client Certificate --|
  |                                  |
  | --- Generate Encrypted Key ----> |
  |                                  |
  | <----- Acknowledge Key --------- |
  |                                  |
  | <=== Encrypted Communication ===>|

How it works:

  • Server authenticates itself (same as Mode 2).
  • Client also sends its certificate.
  • Server verifies the client before continuing.

Use case:

  • Device-to-device communication.
  • High-security industrial IoT.

Benefit: Blocks unauthorized devices from connecting.

🔑 3. Public/Private Keys Explained

TLS encryption relies on asymmetric cryptography, using a key pair:

  • Public Key

    • Shared openly.
    • Used to encrypt data or verify signatures.
  • Private Key

    • Kept secret.
    • Used to decrypt data or sign messages.

Key Operations

  • Encryption/Decryption

    • Data encrypted with the public key can only be decrypted by the private key.
    • Used to securely exchange session keys.
  • Signing/Verification

    • A private key signs data (like certificates).
    • A public key verifies that signature.

🔁 RSA vs. ECC (Elliptic Curve Cryptography)

Property RSA (e.g., 2048 bits) ECC (e.g., 256 bits)
Key Size Large Small
Speed Slower Faster (great for MicroPython)
Best For Legacy systems Modern IoT and low-power boards

MicroPython Tip: Prefer ECC for devices like ESP32.

📜 4. Certificates: Your Device's ID

Certificates are digital documents that link a public key to a verified identity (like a domain name).

What's Inside a Certificate?

  • Subject: Domain or entity name (e.g., example.com).
  • SANs: Alternative names or IPs.
  • Issuer: The Certificate Authority that signed it.
  • Validity: Start and end dates.
  • Public Key: Used for encryption.
  • Digital Signature: Proof that the CA trusts this certificate.

Types of Certificates

  • CA-Signed

    • Trusted by browsers and OS by default.
    • Issued by authorities like Let's Encrypt.
  • Self-Signed

    • Created by you.
    • Great for testing but requires manual trust.

Trust Chain Breakdown

  1. Root CA – Pre-installed in browsers and systems.
  2. Intermediate CA – Signed by the root.
  3. Server Certificate – Issued by intermediate.

Validation Process:

  • The client checks the certificate chain.
  • Validates the domain.
  • Checks for revocation (CRL or OCSP).

⚙️ 5. Practical Tips for Embedded Devices (MicroPython)

Running TLS on a microcontroller means working around limited memory and CPU.

Here are some suggestions:

  • Use ECC certificates to reduce size and improve performance.
  • 🔧 Trim unused CA certificates to save flash memory.
  • Avoid RSA-4096—too heavy for small devices.
  • 🛡️ Use secure elements or hardware security modules to store private keys.
  • ⚖️ Choose your mode wisely:
    • Use Mode 2 for cloud APIs and general use.
    • Use Mode 3 (mTLS) for sensitive or critical applications.

✅ Summary

TLS and SSL may sound intimidating at first, but once you understand the basics—encryption, trust, and identity verification—it gets much easier.

Start small:

  • Try setting up server-authenticated connections.
  • Test with self-signed certs in local environments.
  • Explore mutual TLS when you're ready for more security.

You're building secure systems. Keep going, one encrypted handshake at a time! 💪

Just for a laugh 😂

Alice and Bob are exchanging encrypted messages.

Eve is trying to eavesdrop.

Then Mallory shows up, intercepts the keys, forges a message, and sends it to Bob.

Bob reads it, nods, and says:

"I don't know who you are, but if Alice says she loves me, I believe her."

Eve facepalms 🤦. Mallory smiles.

Meanwhile, Trent, the trusted third party, is off on vacation 🏖️ because he “trusted everyone to do the right thing.”

About

Example scripts using SSL/TLS in MicroPython. Use OpenSSL to create keys and certificates

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages