Certificate and public key pinning

Security Senior Swift 5.0+ iOS 17+ Reviewed By Alok Singh

Pick the fix

Which pinning approach is safest to run in production?

Options
Hint 1 of 1

What happens when the certificate is renewed?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Pin SPKI hashes of your keys plus a backup key, declared in NSPinnedDomains, with a rotation plan

Correct. Renewals don't break it, and there is a path for key changes.

Why not the others?
  • B Bundle the leaf certificate file and compare it byte for byte. It breaks on every renewal.
  • C Accept any certificate in the authentication challenge. That disables TLS validation completely.
  • D Skip pinning but turn off ATS. That lowers security.

30-second answer

Pinning makes the app trust only specific keys for your servers, which blocks interception through other trusted or user-installed certificates. Pin the public key (SPKI hash) rather than the whole certificate, include at least one backup pin, and declare it with NSPinnedDomains in Info.plist or check it in a URLSession authentication challenge. Plan key rotation, because a wrong pin locks every user out until they update.

Practise saying it · 45 s. No recording, just a timer

Explain pinning and its risks.

Say your answer out loud, then check which of these points you covered:

  • Pin public keys
  • Backup pins
  • NSPinnedDomains
  • Rotation risk

Deep dive

Pinning an intermediate CA key is more flexible than pinning the leaf.

Ship backup pins for the next key before rotating.

Consider a remote kill switch (signed config) for pins.

Pinning is extra protection on top of ATS, not a replacement.

Common mistakes

  • Pinning a leaf certificate with no backup.
  • Disabling validation in debug code that ships.

Senior take & follow-ups

Seniors talk about rotation and outage risk as much as the implementation.

Follow-up questions

Think of your answer first, then open the hint.

  1. Why pin the public key, not the certificate?

    Show hint

    The key can stay the same when the certificate is renewed.

  2. What happens if the pinned key changes unexpectedly?

    Show hint

    Every connection fails until an app update.

Suggest a fix if something here is wrong or out of date.