Mobile SSL Pinning - Implementation and Security

Strengthen your mobile application security with certificate pinning.

SSL Pinning (or Certificate Pinning) is a security technique that associates a mobile application with a specific SSL certificate or its public key. This prevents man-in-the-middle attacks even if the attacker has a valid certificate issued by a trusted certificate authority.

Without pinning, an application trusts any certificate signed by a CA in the system trust store. With pinning, the application only trusts the certificate or public key you have specifically configured.

MoniTao helps you monitor your certificates and alerts you when they change, which is crucial if you use SSL Pinning because you'll need to update your applications during renewals.

What is SSL Pinning?

Understanding the concept of certificate pinning:

Types of SSL Pinning

Different pinning approaches:

Implementing SSL Pinning

Steps to implement pinning:

  1. Extract the pin: get the SPKI hash of your certificate or public key.
  2. Configure the application: add the pin in your application (iOS: Info.plist, Android: network-security-config).
  3. Add backup pins: include at least one backup pin to avoid lockouts during renewals.
  4. Test rigorously: test that the application rejects unpinned certificates and accepts correct ones.

Implementation Examples

Code for different platforms:

# Extract SPKI hash from a certificate
openssl s_client -connect api.mysite.com:443 2>/dev/null | \
  openssl x509 -pubkey -noout | \
  openssl pkey -pubin -outform DER | \
  openssl dgst -sha256 -binary | base64

# Result: AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=

# Android (network_security_config.xml)
<network-security-config>
  <domain-config>
    <domain includeSubdomains="true">api.mysite.com</domain>
    <pin-set expiration="2025-01-01">
      <pin digest="SHA-256">AAAA...=</pin>
      <pin digest="SHA-256">BBBB...=</pin> <!-- backup -->
    </pin-set>
  </domain-config>
</network-security-config>

The SPKI hash is added to the application's network security configuration. Always include a backup pin.

Best Practices

Tips for successful SSL Pinning:

SSL Pinning Checklist

  • SPKI hash correctly extracted
  • Backup pin configured
  • Configuration tested with valid certificate
  • Rejection test with invalid certificate
  • Update process documented
  • Expiration monitoring configured

Frequently Asked Questions

Is SSL Pinning mandatory?

No, but it's strongly recommended for applications handling sensitive data (banking, health, etc.).

What happens if I change the certificate without updating the app?

The application will refuse to connect to your server. This is why backup pins are essential.

How do I handle pinning with Let's Encrypt?

Let's Encrypt renews certificates often. Pin the public key and keep the same key during renewals, or pin the intermediate certificate.

Does pinning work with CDNs?

It's complex because CDNs can have multiple certificates. Consult your CDN's documentation.

How do I debug pinning errors?

Use Charles Proxy or Wireshark to capture traffic and check which certificate is presented.

Can MoniTao help with SSL Pinning?

MoniTao monitors your certificates and alerts you before expiration, giving you time to prepare new pins.

Secure Your Mobile Applications

SSL Pinning is an essential security layer for mobile applications handling sensitive data. Well implemented, it protects against even sophisticated MITM attacks.

With MoniTao, monitor your certificates and anticipate renewals to maintain a smooth user experience while keeping optimal security.

Ready to Sleep Soundly?

Start free, no credit card required.