Blogs & Stories

SpiderLabs Blog

Attracting more than a half-million annual readers, this is the security community's go-to destination for technical breakdowns of the latest threats, critical vulnerability disclosures and cutting-edge research.

Regression Workaround for TWSL2011-007: The iOS SSL Validation Vulnerability (UPDATED)

Trustwave SpiderLabs recently released an advisory (TWSL2011-007) regarding improper x509 certificate chain validation on Apple iOS. The vulnerability (reserved as CVE-2011-0228 at the time of writing) was discovered and reported to Apple by our own Paul Kehrer. Also attributed for independently discovering and reporting the vulnerability was Gregor Kopf of Recurity Labs.

The vulnerability is also actually essentially the same as one found in IE back in 2002 by moxie, author of SSLSniff.

The iOS vulnerability has been confirmed on pretty much all iOS versions up to and including 4.3.4 and 4.2.9(CDMA). Apple released patches to iOS shortly before the Trustwave Advisory was released as iOS firmware updates (versions 4.3.5 and 4.2.10(CDMA) respectively).

While we definitely encourage all users to upgrade their iOS versions as soon as possible, the world isn't a perfect place. Developers of security sensitive mobile applications who rely on SSL for secure communications and x509 trust verification are left in a difficult position with potentially serious implications.

Say you're an application developer for iOS for something such as a mobile banking or payment-processing application. You are faced with a choice, either force all users to upgrade iOS, or, if possible identify a code workaround that you can implement in your own application for earlier iOS versions. Then, release an update to your own application and require them to at least apply that.

With this in mind, we have been looking into possible code workarounds to address the certificate validation vulnerability for individual applications. After some trial and error, we found that by using the Apple Secure Trust API directly to validate your own certificates, and by configuring certain features, you can properly detect and reject invalid certificates designed to exploit this bug.

One goal of the implementation was to stick to core Apple API's as much as possible, whilst still using only the ones supported for approval in the app store. This workaround uses the Secure Trust API to establish a SSL validation policy, requiring validation of the FQDN of the leaf certificate in the chain. The leaf certificate represents the last certificate in the chain, or the certificate signed for the site is configured for. The routine is called with a leaf name and a reference to the leaf certificate and it returns true if the certificate is validated, and false otherwise.

  #import <CoreFoundation/CoreFoundation.h>  #import <Security/Security.h>    bool isCertValid(CFStringRef leafName, SecCertificateRef leafCert) {    bool ret=false;    SecTrustRef trust;    SecTrustResultType res;        SecPolicyRef policy = SecPolicyCreateSSL(true, leafName);    OSStatus status = SecTrustCreateWithCertificates((void *)leafCert, policy, &trust);        if ((status == noErr) &&        (SecTrustEvaluate(trust, &res) == errSecSuccess) &&         ((res == kSecTrustResultProceed) || (res == kSecTrustResultUnspecified)))     { ret = true; }        if (trust) CFRelease(trust);    if (policy) CFRelease(policy);         return ret;  }

Note, you can technically pass a null leafName to this implementation, but don't, or the workaround will not validate the certificate properly.

UPDATE 8/10/2011 (thanks to Adam Goodman of DuoSecurity): The above workaround is not designed to work with intermediate certificates. Only pass the leaf certificate in as the leafCert parameter, without any additional intermediate certificates. If you use intermediate certificates
for validation, you will need something much more robust than the simple workaround we have supplied above. See the comments at the end of this blog as well as Adam's Blog Post for more information.

We've put a command-line version of the implementation up on github as a reference. The command-line utility takes as its arguments a leaf name and a certificate filename (the file should be in raw DER format, which is just the bas64-decoded data of the public-key in a PEM).

We have also confirmed this works in conjunction with the commonly used CFNetwork API as well by accessing and validating the certificate on the network stream after the API's own built-in SSL handshake is complete. We may post up a CFNetwork reference implementation too in the near future, so keep an eye on the github project.