SSL (Secure Sockets Layer) pinning is a technique used in mobile app development to ensure that the app communicates only with a server that has a specific certificate. This is done by embedding the server’s certificate in the app itself and then validating the server’s certificate against this embedded certificate during communication. If the server’s certificate does not match the pinned certificate, the connection is rejected.
Use openssl
to retrieve the certificate from your server and save it to a file. This can be done with the following command:
openssl s_client -connect yourserver.com:443 -showcerts < /dev/null | openssl x509 -outform DER -out yourserver.cer
Alternatively, to save it in PEM format:
openssl s_client -connect yourserver.com:443 -showcerts < /dev/null | openssl x509 -outform PEM -out yourserver.crt
Name the certificate files using the domain name as the filename with either .cer
or .crt
as the extension. For example, if your server’s domain is example.com
, your files should be named example.com.cer
and/or example.com.crt
.
Optionally you can have both file types (.cer
and .crt
) to match the server trust and to ensure continued app functionality during certificate rotations. Coordinate certificate rotations with the deployment teams to avoid disruptions between the app and the server.
Place the certificate files in the assets/certs
folder of your project. This is necessary for the app to access and use the certificates during runtime.
Follow the instructions in the Build the iOS app or Build the Android app sections to build your app with SSL pinning enabled.