Lock Down Your App with HTTPS and Certificate Pinning: A Swift Security Masterclass

Gizem TürkerGizem Türker
4 min read

As iOS developers, we’re not just creators; we’re guardians. Our apps often handle sensitive user data — everything from personal information to financial transactions. This puts a significant onus on us to ensure that our apps are as secure as a digital Fort Knox. One of the foundational steps in achieving this level of security is making sure our app’s communication is rock-solid and secure. This is where HTTPS and certificate pinning come into play. Let’s dive deep into how we can utilize these technologies to lock down our apps, ensuring the data our apps send and receive is as secure as possible.

HTTPS: The Secure Communication Protocol

Before we even think about certificate pinning, we need to ensure our app communicates over HTTPS. HTTPS (HyperText Transfer Protocol Secure) is the secure version of HTTP, encrypting the data sent between your app and the server. This encryption ensures that even if someone manages to intercept the data, they won’t be able to understand it. Implementing HTTPS is generally straightforward and begins with your server configuration, but let’s ensure we enforce its usage in our Swift app.

Enforcing HTTPS in iOS Apps

Starting with iOS 9, Apple introduced App Transport Security (ATS), which enforces secure connections for all internet communications by default. However, developers can still opt out for specific domains in their Info.plist. We, as security-conscious developers, want to ensure we're not doing that without a very good reason. Here's a quick checklist to make sure you're enforcing HTTPS:

  1. Review Info.plist: Ensure there are no exceptions allowing HTTP traffic unless absolutely necessary.

  2. Server Configuration: Verify your server supports the latest TLS protocol with a strong cipher suite.

With HTTPS in place, we’ve taken the first step towards securing our app’s communication. But why stop there? Let’s add another layer of security with certificate pinning.

Certificate Pinning: The Next Level of Secure Communication

Certificate pinning is a technique that enhances the security of an HTTPS connection by “pinning” the server’s certificate or public key directly within your app. This means your app will only accept the pinned certificate or public key when establishing a connection with the server, effectively mitigating man-in-the-middle (MITM) attacks.

Implementing Certificate Pinning in Swift

Implementing certificate pinning in Swift requires a bit more elbow grease, but it’s well worth the effort. Here’s a simplified example of how we can implement certificate pinning using URLSessionDelegate:

import Foundation

class URLSessionPinningDelegate: NSObject, URLSessionDelegate {
    func urlSession(_ session: URLSession, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        guard let serverTrust = challenge.protectionSpace.serverTrust else {
            completionHandler(.cancelAuthenticationChallenge, nil)
            return
        }

        var secresult = SecTrustResultType.invalid
        let status = SecTrustEvaluate(serverTrust, &secresult)

        if status == errSecSuccess {
            let certificate = SecTrustGetCertificateAtIndex(serverTrust, 0)
            // Your pinned certificate data
            let pinnedCertificateData = ... 
            let remoteCertificateData = SecCertificateCopyData(certificate!) as Data

            if pinnedCertificateData == remoteCertificateData {
                completionHandler(.useCredential, URLCredential(trust: serverTrust))
                return
            }
        }

        completionHandler(.cancelAuthenticationChallenge, nil)
    }
}

In this code snippet, we create a custom URLSessionDelegate that checks the server's certificate against a pinned certificate stored within our app. If the certificates match, the connection proceeds; if not, the connection is canceled. This ensures that even if a malicious entity were to intercept our app's communication, they couldn't decrypt or manipulate the data without the exact certificate our app expects.

The Importance of Keeping Your Pins Updated

While certificate pinning significantly increases your app’s security, it also introduces a new maintenance consideration. Certificates expire and may need to be rotated. Therefore, it’s crucial to have a strategy for updating the pinned certificates in your app, possibly through app updates or by fetching them from a secure, trusted server at runtime.

Wrapping Up: A Secure Foundation

We’ve created a secure foundation for our app’s communication by implementing HTTPS and certificate pinning. But remember, security is a journey, not a destination. These techniques are just the beginning. As we continue to explore more advanced strategies — like code obfuscation, secure storage, error handling, and biometric authentication — we’ll further enhance the fortress protecting our users’ data.

As iOS developers, our responsibility goes beyond just building features; we’re also the first line of defence against digital threats to our users. By adopting these security practices, we protect our users and gain their trust, a crucial component of any app’s success. Stay tuned for more articles where we’ll dive deeper into each of these topics, armed with code examples and best practices to secure our iOS apps against the ever-evolving landscape of digital threats.

Follow me for more adventures about entrepreneur and dev life.

Sources and Tools:

11
Subscribe to my newsletter

Read articles from Gizem Türker directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gizem Türker
Gizem Türker

🚀 Crafting apps & leading the charge in tech as an iOS Developer, Entrepreneur & Solopreuner. Ex- @PeltonTechnology founder.