सामग्री पर जाएँ

लेसन 28 · Security and Privacy

Device Identity, TLS and Access Control

Give every IoT device its own identity: credentials and certificates, TLS for encryption, message signing against tampering and replay, and per-topic ACLs.

After threat modeling you know the main questions: who is this device, can anyone read or change its messages, and what is it allowed to do? Three building blocks answer them. Identity (authentication) proves who a device is. Encryption and integrity keep messages private and unaltered on the wire. Authorization limits what an authenticated device may do.

Identity: one device, one credential

A device needs a credential that is unique, secret and revocable. Options from weakest to strongest:

Method How it works Notes
Shared password same for all devices One leak ruins the fleet. Avoid.
Per-device username and password stored on the device Better; still secret in flash
Pre-shared key (PSK) per-device symmetric key Fine for constrained devices (TLS-PSK)
X.509 client certificate device holds a private key and a certificate signed by your CA Standard for fleets; private key can live in a secure element
Hardware-backed identity key generated in a TPM or secure element, never leaves the chip Strongest; used by cloud platforms

Provisioning is the moment a device gets its identity, ideally in the factory or during a secure first-boot onboarding, never by a shared default password left in the field. Cloud services also offer just-in-time registration: a device presents a certificate signed by your CA and is created automatically.

TLS (Transport Layer Security) encrypts a TCP connection (MQTT on port 8883, HTTPS on 443). DTLS does the same for UDP (CoAP). What TLS gives you:

  1. Server authentication. The device checks the broker's certificate against a trusted CA, so it cannot be tricked into talking to an impostor.
  2. Confidentiality. Readings are encrypted.
  3. Integrity. Tampering is detected.
  4. Optionally mutual TLS (mTLS): the broker also demands the device's client certificate.
sequenceDiagram
  participant D as Device
  participant B as Broker
  D->>B: ClientHello (supported ciphers)
  B-->>D: Certificate (signed by a CA the device trusts)
  D->>D: verify chain, name and expiry
  D->>B: key exchange; client certificate (mTLS)
  Note over D,B: Both derive session keys, MQTT CONNECT is now encrypted

The most common mistake in prototypes is disabling certificate verification ("setInsecure()") to get a connection working. The link is then encrypted but to anyone, which defeats the point. Another is forgetting that certificates expire: a device needs a correct clock (or a way to update the trust store) to validate them.

Signing messages

TLS protects a hop. When messages pass through several systems (broker, queue, database) you may also want the message itself to prove where it came from. A message authentication code (MAC) such as HMAC-SHA256 uses a shared secret key. To stop attackers from capturing a valid message and resending it (a replay), include a timestamp or a counter in the signed data.

Node.js HMAC-signed readings with replay protection

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Notice the order: first check the signature, then the counter. Compare MACs with timingSafeEqual so response time does not leak how many bytes matched.

For fleets, public-key signatures (Ed25519, ECDSA) are better than a shared HMAC key: the device keeps a private key, the server stores only public keys, and a stolen server database cannot forge device messages.

Node.js Device signs with a private key, server verifies with the public key

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Authorization: access control lists

Identity says who; an ACL (access control list) says what. A good broker policy gives each device the minimum topics it needs. Mosquitto style rules use %c for the client ID, so one rule covers every device:

TEXT
# mosquitto acl file (read-only example)
pattern write plant/%c/state
pattern read  plant/%c/set
user dashboard
topic read plant/#

The simulation below implements the same logic, so you can test a policy before deploying it.

Node.js Per-client topic ACLs

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Default deny is the key: anything not explicitly allowed is refused.

Checklist

  • Unique credential per device, stored in secure hardware if you can.
  • TLS 1.2 or later with certificate verification ON; mTLS for fleets.
  • Rotate and revoke: keep a certificate revocation process and short-lived tokens where possible.
  • Default-deny ACLs with the client ID in the topic.
  • Sign critical commands (unlock, firmware) end to end with a counter or nonce.
  • Never log secrets; never reuse keys across products.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What does disabling TLS certificate verification ("insecure mode") do?

    Without verification the device cannot tell the real broker from an attacker, so a man in the middle can read and change everything.
  2. 2. Why include a counter or timestamp in a signed message?

    An attacker cannot forge the signature, but can resend a captured message unless freshness is checked.
  3. 3. Why prefer per-device public-key credentials over one shared key?

    Each device has its own private key; a stolen server database holds only public keys.
  4. 4. What is the safest default for an ACL?

    Default deny prevents mistakes from opening access.

Summary

  • Give each device a unique, revocable identity; certificates and secure elements are strongest.
  • TLS (or DTLS) authenticates the server, encrypts and protects integrity; never disable verification.
  • Sign important messages and include a counter or timestamp to stop replay; prefer public-key signatures for fleets.
  • Use default-deny ACLs with the client ID in topics so a device can only touch its own topics.

Next: Secure Updates, Privacy and Compliance

अपनी प्रगति ट्रैक करें

मुफ़्त अकाउंट बनाएँ, लेसन पूरे के रूप में चिह्नित करें और किसी भी डिवाइस पर वहीं से शुरू करें जहाँ छोड़ा था।