SDSystem DesignConcepts to case studies
System DesignDSA
x
0/311
Part 1: Foundations
Part 2: APIs and Communication
  • Rate Limiting
  • Authentication vs Authorization
  • Session vs Token Based Auth
  • JWT
  • OAuth / OAuth2
  • Single Sign-On (SSO)
  • API Keys
  • OpenID Connect (OIDC)
  • CSRF
  • Input Validation
  • mTLS and Request Signing (HMAC)
Part 3: Caching
Part 4: Data and Storage
Part 5: Architecture
Part 6: Distributed Systems
Part 7: Advanced Data Topics
Part 8: Operations and Security
Part 9: Estimation and Interview Method
Part 10: Case Studies
System Design/APIs and Communication/API Security

mTLS and Request Signing (HMAC)

Prove who is calling and that the message was not changed, using certificates or signatures.

⏱ 14 min readAdvancedInterview FavouriteHas visualizer
SummaryConceptReal-Life ExampleWhy?Where?How It WorksStepsKey TermsPros & ConsMistakesReal WorldRead NextInterview Q&ACheat-Sheet

01Quick Summary

Two ways to prove who is calling an API, and that the message was not changed on the way, without relying on a password or a key sent in the open.

  • mTLS (mutual TLS): in normal HTTPS only the server shows a certificate. In mTLS the client shows one too, so the server knows exactly which service is calling.
  • Request signing (HMAC): the caller computes a signature over the request using a shared secret key. The server recomputes it. If one byte changed, the signatures differ.
  • Both are common for service-to-service calls and for webhooks, where a plain API key is not enough.

02Concept: What is it?

An API key says “I am app X”. But a key is just a string. If someone copies it, they are app X. And the key does not say anything about the message: someone in the middle could change the body and the server would not notice.

mTLS: prove who you are with a certificate

You already know TLS from the SSL/TLS topic: the server shows a certificate, signed by a trusted CA (certificate authority), and the browser checks it. With mTLS the server also says “now show me yours”. The client sends its own certificate and proves it owns the matching private key. The server checks that the certificate was issued by a CA it trusts.

Request signing: prove the message is untouched

With HMAC signing, both sides know a secret key. The client builds a fixed text from the request (the method, the path, the query, a fingerprint of the body, and the time). It runs this text and the key through an HMAC function and sends the result as a header. The key itself is never sent. The server builds the same text from what it received, computes the HMAC with its own copy of the key, and compares.

Two ways to prove the caller
mTLS
🧾Clienthas certificate
↔both show certificates
🗄️Servertrusts our CA
Checked once, when the connection starts.
Request signing (HMAC)
✍️Clientsigns the request
→↓request + signature
🔎Serverrecomputes, compares
Checked on every single request.
Certificate based Shared-key based
mTLS proves the caller at connection time. Request signing proves each single message.
🧠A signature protects the message, not the secrecy
A signed request can still be read by anyone who sees it. Signing proves who and what was sent. To hide the content, you also need TLS.

03Real-Life Example

🍵The wax seal on a letter
A king sends orders by messenger. Anyone could write “The king says: give me the gold”. So the king presses his ring into hot wax on the letter. Only he owns the ring. If a thief opens the letter and changes a word, the seal breaks. The guard compares the seal with the royal pattern. Better still, the letter carries the date. An old, valid letter shown again next year is refused. And the guard at the gate also checks the messenger: “Show me your royal badge”.
The royal letter
Signed API call
The king's ring
The secret key (HMAC) or private key
Wax seal on the letter
The signature header
Seal breaks if a word changes
Signatures differ if the body or path changed
Date written on the letter
A timestamp inside the signed text
A letter number, never reused
A nonce, to catch replays
The messenger's royal badge
The client certificate in mTLS
The guard who knows the royal pattern
The server, which trusts a CA
One signed request, step by step
📝Build textmethod, path, time, body hash
→↓
🔏HMACtext + secret key
→↓
📤Sendrequest + signature
→↓
✅Server comparessame text, same key

04Why Do We Need It?

🪪Strong identity
A certificate says which service is calling. It is much harder to fake than a copied string.
🧱Zero trust inside
Being inside the company network no longer means “trusted”. Every call proves itself.
🧩Tamper detection
A signature covers the body and the path. A change by anyone in the middle breaks it.
🔁Replay protection
A timestamp and a one-time nonce make an old, copied request useless.
🔑The key never travels
With HMAC only the signature is sent. A stolen request does not give away the key.
📣Trusted webhooks
A receiver can check that a webhook really came from the sender and was not forged.
An API key says who you claim to be. A certificate or a signature lets you prove it.

05Where Do We Use It?

🕸️Service-to-service calls
Microservices inside a company often use mTLS, frequently set up by a service mesh or a sidecar proxy.
📣Webhooks
Payment and code-hosting services sign each webhook, so you can check it came from them.
☁️Cloud APIs
Many cloud providers require every API request to be signed with a secret key and a timestamp.
🏦Finance and health
Rules often demand strong client identity and proof that a message was not changed.
📟Devices and IoT
A device holds a certificate from the factory and proves itself to the cloud.
🚪Gateways
An API gateway can check client certificates or signatures before a call reaches your code.

06Types / Parts / How It Works

The mTLS handshake in short

  1. 1
    Client says hello
    The normal TLS start: supported versions and ciphers.
  2. 2
    Server shows its certificate
    The client checks the issuer, the name and the dates, as in ordinary HTTPS.
  3. 3
    Server requests a client certificate
    This is the one extra step that makes it mutual.
  4. 4
    Client sends its certificate and proves ownership
    It signs part of the handshake with its private key.
  5. 5
    Server checks the client certificate
    Is the issuer a trusted CA? Is it in date? Is it revoked? Is the name allowed?
  6. 6
    Encrypted session starts
    Both sides are now authenticated, and the traffic is encrypted.

What goes into a signed request

General idea (not a specific provider's exact format)
Text to sign (lines joined with newlines):
  POST
  /v1/payments
  amount=100&currency=USD        <- query sorted by name
  2026-12-03T10:15:00Z           <- timestamp
  n-7f3a                         <- one-time nonce
  <fingerprint of the body>      <- a hash of the exact bytes

Signature = HMAC(secret key, text to sign)

Headers sent:
  X-Key-Id:    key-123            <- tells the server which secret to use
  X-Timestamp: 2026-12-03T10:15:00Z
  X-Nonce:     n-7f3a
  X-Signature: <the signature>

Comparing the three ways to identify a caller

WayWhat the server learnsIf it is stolenProtects the body?
API keyWhich app the caller claims to beThe thief is the app, until the key is revokedNo
HMAC signatureThe caller holds the shared secret, and the request is unchangedOne request is copied. It dies after the time windowYes
mTLS client certificateExactly which service connected, backed by a CANeeds the private key. Short lifetimes limit the damageYes, plus encryption
VisualizerSign a request, attack it, and check a client certificateIn the signing tab, change the body or replay the request and see the server say no. In the mTLS tab, try a missing, expired or unknown certificate.
Which idea do you want to play with?
What does the attacker do?

Someone on the way changes the amount from 100 to 9000 and the account number.

Server remembers used nonces?
What the client signed
POST
/v1/payments
1000
n-7f3a
024f618d
Signature sent (toy): 27ac7b27
What the server received and recomputed
POST
/v1/payments
1000
n-7f3a
8d060f17
Server signature: 0c338d6a
Signatures
differ
server vs client
Message age
0 s
window 300 s
Result
rejected
by the server
Rejected: the signature does not match what the server computed. The message was changed, or the sender does not know the key.

The signature here comes from a small toy hash so the demo is repeatable. It is NOT real cryptography. A real system uses a proper HMAC (for example HMAC with SHA-256), a secret key and a constant-time comparison. Times and numbers are illustrative.

07How to Use It / Steps / Implementation

Doing it properly

  1. Sign the right things. Include the method, the path, the sorted query, a hash of the body and the timestamp. Anything left out can be changed freely by an attacker.
  2. Use the same exact bytes on both sides. Agree on one way to write the text (“canonical form”): same order, same spacing, same encoding. Small differences cause false failures.
  3. Use a real HMAC from your language's crypto library, with a strong hash such as SHA-256. Never invent your own.
  4. Compare in constant time. A normal string compare stops at the first different character, which leaks information through timing.
  5. Add a time window. Reject requests older than a few minutes. Keep the servers' clocks in sync.
  6. Add a nonce where replays are dangerous, and remember used nonces for the length of the window. Or make the operation idempotent.
  7. Use a key id and rotate keys. Let two keys work at the same time during a change.
  8. For mTLS, automate certificates. Use a private CA, short lifetimes and automatic renewal. Handle revocation. Often a mesh or proxy does this for you.
⚠️Do not roll your own crypto
The demo on this page uses a toy hash only to keep results repeatable. In real code use well-tested libraries, and never put secret keys in code or in logs.

08Key Terms to Know

TermMeaning in easy words
mTLSMutual TLS: both the client and the server show certificates and check each other
CertificateA signed document that ties a name to a public key
CA (certificate authority)A trusted body that signs certificates. A company can run a private CA
Private keyThe secret half of a key pair. Whoever holds it can prove the identity
HMACA keyed hash: a signature made from a message and a shared secret key
Canonical stringThe exact, agreed text of the request that gets signed
NonceA number used once. Remembering it stops a copied request being replayed
Replay attackSending a valid, recorded request again later
Constant-time compareComparing two values in the same time wherever they differ, so timing leaks nothing
Key rotationReplacing keys or certificates on a schedule, or after a leak
RevocationCancelling a certificate before its date runs out

09Pros, Cons and Trade-offs

✅ Pros
  • Strong proof of who is calling
  • Signatures catch changed bodies and paths
  • Replays can be blocked
  • The secret never travels on the wire (HMAC)
  • mTLS rejects bad callers before any app code runs
⚠️ Cons
  • More set-up than an API key
  • Clock drift can reject good requests
  • Canonical text mismatches cause confusing errors
  • Certificates must be issued, renewed and revoked
  • Shared HMAC secrets must be stored safely on both sides

Trade-offs

ChoiceWe gainWe lose
mTLS instead of API keysStrong identity at the connectionCertificate management work
HMAC instead of mTLSWorks through proxies that end TLS, and per message proofBoth sides share a secret, and you handle replays yourself
Short certificate lifetimeA stolen key stops working soonNeeds reliable automatic renewal
Wide time windowTolerates clock differencesA longer replay window
Signing the whole bodyAny change is caughtThe body must be read fully before it is checked

10Common Problems and Mistakes

Common mistakes

  1. Signing too little. If the path or a header is not signed, an attacker can change it.
  2. No timestamp or nonce. A valid request can be replayed forever.
  3. Normal string comparison. Timing differences can leak the signature, one character at a time.
  4. Different JSON formatting. Re-serialising the body before checking gives a different hash. Sign the raw bytes.
  5. One shared key for everyone. One leak breaks every client. Give each client its own key.
  6. Expired certificates. A forgotten renewal causes an outage in the middle of the night.
  7. Trusting too many CAs. An mTLS server should trust only the CA that issues its clients.

Things to watch

🕰️Clock skew
If a server clock is far off, good requests look too old or too new.
📅Certificate expiry
Alert well before dates run out, or automate renewal.
🔄Proxies that end TLS
A load balancer may end TLS, so the app never sees the client certificate unless it is passed on safely.
🗝️Secret storage
Keep keys in a secret manager, not in code, logs or chat.

11Real-World Examples

☁️Cloud request signing
Large cloud providers sign every API request with a secret key and a timestamp.
💳Payment webhooks
Payment services send a signature header with each webhook, built from a shared secret and the raw body.
🕸️Service meshes
Meshes such as Istio and Linkerd can turn on mTLS between services, with certificates renewed automatically.
☸️Cluster internals
Parts of Kubernetes use client certificates to prove their identity to each other.
📟Device fleets
Devices hold a certificate and connect with mTLS to the cloud.
📝Note
Header names and signing formats differ between providers. Always follow the exact steps in the provider's own documentation.

12Related Topics to Read Next

SSL/TLS→

mTLS is TLS with a certificate on both sides.

API Keys→

The simple option that signing and mTLS improve on.

Webhooks→

A very common place to check a signature.

Service Mesh→

Often turns on mTLS between services for you.

Authentication vs Authorization→

Signing and certificates are about authentication.

Idempotency→

Makes a replayed request harmless.

13Interview Questions with Short Answers

14Quick Revision Cheat-Sheet

  • ✔mTLS: both sides show certificates. Strong identity, plus encryption.
  • ✔HMAC signing: signature = HMAC(secret, canonical request). Server recomputes and compares.
  • ✔Sign: method, path, sorted query, body hash, timestamp (and a nonce).
  • ✔Replay: short time window plus remembered nonces, or an idempotent action.
  • ✔Always: a real crypto library and a constant-time comparison.
  • ✔Keys: one per client, with a key id, rotated, kept in a secret manager.
  • ✔Certificates: private CA, short lifetimes, automatic renewal, revocation.
  • ✔One line: “An API key claims who you are. A certificate or a signature proves it.”
← Previous
Input Validation
Next →
Long Polling