← All articlesMobile applications

Certificate pinning in Android: when you need it and what happens after a pin-set expires

Certificate pinning adds a constraint to certificate validation: the chain must contain at least one pinned public key. The decision to use it involves both protection and the consequences of key rotation and the ability to update the app. We will examine how to assess whether pinning is needed and test an existing pin-set in a fictional HTTPS environment owned by your team. The central question is which additional constraint applies before expiration and what remains afterward.

When an app needs pinning

Start with the product’s threat model: does the app require an additional constraint on the public keys it accepts? Restricting the set of trusted certificate authorities and certificate pinning are different ways to narrow the set of accepted certificates. For pinning, what matters is the presence of a pinned key in the chain. Saying ‘we trust a selected CA’ therefore does not, by itself, describe pin validation.

Document the decision with the product owner: which constraint is required, who is responsible for key rotation, how readily the app can be updated, and what happens if connectivity is lost. Successful tests will demonstrate the behavior of the selected configuration, but they will not establish that every Android app needs pinning.

Where the trust boundary lies

While a pin-set is active, the chain must contain at least one pinned public key. Without a match, the additional pinning requirement is not met, even if the chain is otherwise valid and trusted by the app. Conversely, a match must not turn TLS validation into acceptance of any certificate.

For an existing pin-set, first list the pinned keys and the expiration value, if one is specified. This lets you define the expected result before testing. Keep two questions separate: does the chain pass ordinary TLS validation, and does it satisfy the additional key constraint?

A backup key and an expiration date serve different purposes

Android documentation recommends always including a backup key when using pinning. If a key or CA change breaks connectivity and no suitable backup key is available, restoring connections will require an app update. Test the backup key through a planned key rotation in your test environment.

The expiration attribute sets a date after which pinning is no longer performed. This helps avoid connection problems in apps that have not been updated, but it also ends the additional constraint: the documentation warns that pinned certificates can be bypassed after expiration. If expiration is absent, the pins do not expire. Choosing an expiration date should be a deliberate decision that accounts for both consequences.

Fictional example: preparing your own test environment

Imagine an entirely fictional app called ‘Learning Catalog’ that retrieves a synthetic product list from the team’s own HTTPS test environment. Its test pin-set includes primary key A and backup key B. The team has also prepared a test chain containing key C that is otherwise valid and trusted by the app: it contains no pinned keys.

The following are original editorial acceptance scenarios, not an official Android procedure. Use only your own test environment, test certificates, and synthetic data. For each case, record the app configuration, the test device’s date, expiration, the presented chain, and the result of a new connection. These tests check connections; a successful HTTP response alone does not confirm that any data change was accepted.

Testing matches and rejection before expiration

Begin with a positive control: expiration is clearly in the future, TLS is valid, and the chain contains key A. The acceptance criterion is a successful new connection. Then present the trusted chain containing key C while the same pin-set remains active. The criterion is connection rejection because no pinned key matches.

Compare the rejection with the successful positive control: a single failed attempt cannot distinguish a pin mismatch from general test environment unavailability. Separately test the planned rotation from A to the previously pinned B. With valid TLS, the app should remain able to establish a new connection. This gives the backup key an observable test instead of leaving it as a configuration entry.

Testing behavior after expiration

For chain C, compare two cases: expiration clearly in the future and clearly in the past relative to the test device’s date. The test certificates must remain valid in both cases. Before expiration, expect rejection due to pinning; afterward, expect a successful new connection if the other TLS checks pass. Keep chain C unchanged during the comparison so that the change in outcome can be attributed to pin-set expiration.

Then run a negative control: with the pin-set already expired, present a test chain from a CA the app does not trust. The acceptance criterion is TLS rejection. Together with the successful connection for trusted chain C, this demonstrates the required distinction: the pinning constraint has ended, but the app has not started accepting arbitrary certificates.

What to fix first and what the tests do not promise

If an untrusted chain is accepted after expiration, prioritize fixing TLS validation, which must not accept arbitrary certificates. Then investigate unexpected results with an active pin-set: a match should allow a connection when TLS is valid, while no match should cause rejection. Add and test a backup key, then agree on the expiration date and the app update plan.

The results apply to the tested configuration and your own test chains. They provide no statistical guarantee of security and do not cover every TLS variation. This article is limited to whether pinning is needed, its expiration, and observable connection behavior. For a separate assessment of your website or the web portion of your product, you can request a free CheckMyApp audit.

Sources and documentation

  1. Network security configuration
  2. Improve your app's security
CheckMyApp

Check your application’s security.

Enter your web application address and email. Get a free preliminary report and choose the depth of analysis.

Check for free