DEV Community

Judy Page
Judy Page

Posted on

What is SSL Pinning, and Why It’s Important?

SSL certificates secure the connection between servers and applications, making it an excellent means of reducing cyberattacks. However, it can be harmful if your SSL certificate is out-of-date or vulnerable to hackers.

SSL Pinning addresses this potential weakness of relying solely on certificates issued by a trusted CA. It prevents Man-in-the-Middle (MITM) attacks by hardcoding a server’s SSL/TLS certificate public key directly into an app.

But What is SSL pinning? This article will explain SSL pinning, including its varieties and how it works. But first, you must understand the basics.

What is SSL Pinning?

SSL pinning is a security technique used in mobile & web applications and devices to enhance the security of HTTPS connections. The application embeds an SSL/TLS certificate or public key directly within its code.

The application or device will contact the server for data whenever a user requests. The app then compares the hardcoded public key with the server’s certificate. If the key matches, secure connections will be made, and users can access data.

Types of SSL Pinning

SSL Pinning types allow you to pin an application’s certificate or public key to the application. However, which method to choose depends on your specific needs.

Static Pinning
Static SSL pinning is where you hardcode a certificate or public key in the application. It improves the security of server-app connections and ensures users’ trust by only connecting a valid server. If the certificate or public key embedded in the app does not match the one presented by the server connection, it will not be secure, so the app will not establish communication.

Dynamic Pinning
Dynamic pinning is an approach in which you don’t pin a certificate or public key to an app. Instead, the application will simply create a cache of the SSL certificate the server presents for the first time during an SSL handshake.

So, each time an app connects with the server, the same cached certificate or public key is used for validation. The certificates or security keys match to ensure secure communication between the server and the app. If you want to apply either of these types of SSL pinning, you can take two approaches.

Techniques Used in SSL Pinning

Depending on your approach, you can apply different types of SSL pinning. Two prominent approaches that businesses use are certificate pinning and public key pinning.

SSL Certificate Pinning
This method is straightforward in implementation but has a drawback requiring continuous updates. SSL certificates have expiration dates; you must update the pinned one each time you renew the SSL certificate.

In this type of SSL pinning, you can pin the entire certificate into the application. This will allow you to embed the SSL certificate and ensure secure connections. During the runtime, the client side will compare the server’s certificate with the one embedded in the app to allow the connection.

Types of SSL Certificate Pinning

SSL certificate pinning can be categorized based on the type of certificates you pin.

Leaf Certificate Pinning

Pinning the leaf certificate increases the secure communication for specific domains or entities. However, Leaf certificates have a shorter expiry, so you must update the certificate regularly.

Intermediate Certificate Pinning

Intermediate certificates are crucial for CA trust. Pinning an intermediate certificate ensures the CA’s trustworthiness and validates it across the chain of trust.

Root Certificate Pinning

A Root certificate pinning is the process of embedding the public key of the root certificate; if the certificates do not match during validation, it searches for the appropriate CA in the trust chain until a trusted root certificate is reached.

Public Key Pinning
Public key pinning is a process where a security key is embedded instead of an SSL certificate. This addresses the downside of certificate pinning, which requires updating each time certificates are renewed.

Public key pinning involves storage of the public key of a trusted server stored on the app. During the SSL/TLS handshake the stored public key is matched with the one that server offers for successful connection.

If the public key from server and other once stored in app files matches, secure connection is established. If it doesn’t match, the connection is terminated.

How SSL Pinning Works?

SSL pinning leverages the principle of questioning the certificate and its validation claims for each connection. It is a process where applications do trust certificates but only specific ones.
Image description

Apps have a list of pre-determined and trustworthy certificates. It will validate a server’s SSL certificates, compare them with the list, and validate. Here is a step-by-step process of SSL pinning.

Step 1: Embedding the certificate in the application
In this step, you embed a copy of the server’s SSL certificate or public key to the application during development. This allows the app to compare the embedded certificate or public key with the original one.

Step 2: Establishing the connection between the server and the app
The application tries to establish a secure connection with the server through validation. The server presents an SSL certificate, and the app matches the embedded one to establish a connection.

Step 3: The validation process
If there is no SSL pinning, validation occurs on a fundamental level. The certificate is considered valid if it is issued by a trusted Certificate Authority (CA). But if a public key or certificate is pinned, it’s compared with the server certificate as an additional verification step.

Step 4: Final decision on connection
The connection is deemed secure if the embedded certificate or public key matches the server certificate or public key.

Benefits of SSL pinning

SSL pinning comes with various advantages. Let’s explore some of these benefits in detail:

Enhanced Security
SSL pinning can improve security by inserting the certificate of the respective server or its public key into our application early enough so that if an attacker attempts to push a fake certificate through a system, the application can recognize that it has to use just those initially loaded certificates.

Such a move blocks out any unauthorized entrants while safeguarding communications from alterations or unauthorized access, thus raising our system’s degree of resistance to online dangers.

Mitigation of Certificate Spoofing

Certificate spoofing occurs when a hacker presents a fake SSL certificate to make the system trust an unauthorized entity, allowing them to intercept and manipulate secure communications.

SSL pinning addresses this by locking the client to accept only a specific certificate or a certificate signed by a particular authority. If a hacker attempts to spoof a certificate, the system will reject it as it is not the designated certificate, significantly enhancing security.

Protection Against Reverse Engineering

SSL pinning helps protect against reverse engineering, a technique used by hackers to analyze and replicate software. Hard-coding the server’s certificate or public key into the application, a unique, unchangeable identity for the server is created. This proactive measure can alert you to potential attacks before they cause harm.

Defense Against API Abuse

SSL pinning provides an extra layer of security against API abuse. Attackers exploit API weaknesses to gain unauthorized access and steal data. With certificate pinning, the app only communicates with the designated server, reducing the risk of MITM attacks.

Improved Trustworthiness
SSL pinning can enhance an application’s trustworthiness by reassuring users that their data is handled securely. By binding the application to a specific certificate or public key, an extra level of security is demonstrated, which can increase user confidence and loyalty.

However, you must ensure that the certificates you use for the pinning are secure. The SSL pin can be damaged if your certificates are stolen or compromised. So, you must ensure that the certificate management mechanism is in place and works optimally.

Challenges and Limitations of SSL Pinning

Although SSL pinning increases security, it also has some disadvantages.

Certificate Changes
The biggest challenge is handling changes in certificates. They are bound to change under various circumstances, such as expiring or revoking. Here, the app pins the certificate, and if it changes eventually, the application might break until it is updated—a service disruption that can frustrate users greatly.

Increased Maintenance
SSL pinning increases maintenance requirements. You must keep tabs on expiration and renew the certificate in time. This means updating the app regularly to update on such changes, a process that can be time-consuming.

Deployment Complexity
SSL pinning adds complexity to the deployment process. Having the correct certificates pinned and appropriately deployed on all the app’s environments (development, testing, production) becomes crucial. So, make sure to avoid such deployment complexity.

Limited Flexibility
The downside to SSL pinning is that it can decrease flexibility. For example, if you need to change to another certificate authority or update your SSL configuration on the server, the pinned certificates will not be valid anymore. In such a case, a new app version would be needed to update the pinned certificates.

Click here to learn the best practices and alternatives for SSL pinning.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs