I am creating an iPad app that accesses HTTPS web services. I want to implement pinning, but am having issues.
This class creates the Alamofire Manager (mostly taken from documentation):
class NetworkManager {
var manager: Manager?
init() {
let serverTrustPolicies: [String: ServerTrustPolicy] = [
"www.google.co.uk": .PinCertificates(
certificates: ServerTrustPolicy.certificatesInBundle(),
validateCertificateChain: true,
validateHost: true
),
"insecure.expired-apis.com": .DisableEvaluation
]
manager = Alamofire.Manager(
configuration: NSURLSessionConfiguration.defaultSessionConfiguration(),
serverTrustPolicyManager: ServerTrustPolicyManager(policies: serverTrustPolicies)
)
}
}
This function makes the call:
static let networkManager = NetworkManager()
public static func testPinning() {
networkManager.manager!.request(.GET, "https://www.google.co.uk").response { response in
if response.1 != nil {
print("Success")
print(response.1)
print(response.1?.statusCode)
} else {
print("Error")
print(response.3)
}
}
}
The certificate is saved in the project and shows under 'Targets > Build Phases > Copy Bundle Resources'.
I am currently receiving the following error every time I make the request (from the else block in testPinning()
):
Optional(Error Domain=NSURLErrorDomain Code=-999 "cancelled" UserInfo={NSErrorFailingURLKey=https://www.google.co.uk/, NSLocalizedDescription=cancelled, NSErrorFailingURLStringKey=https://www.google.co.uk/})
ServerTrustPolicy.certificatesInBundle()
returns (i.e. if it actually includes the certificate)? Wouldn't be too surprised if Google used multiple certificates for the same domain. – UnconformableServerTrustPolicy.certificatesInBundle()
returns 0, which I don't understand. Is there a specific way in which I should add the .cer certificates into the bundle? Regarding Google, that is an example site to replace the one I am developing, but the above problem is the same. – DiluvialNSBundle.mainBundle().pathsForResourcesOfType(".cer", inDirectory: nil)
to see if the file is found in the bundle (if it is, it would then probably point to an issue with the certificate file contents). Anything showing up in the logs whenServerTrustPolicy.certificatesInBundle()
is called? – UnconformablecertificatesInBundle()
. In the for loop there, the certificate is found, but line 138 is preventing the certificate from being added to the array. I suspect it might also be the contents of the .cer, although it opens fine in Xcode (any suggestions on how I should save the certificate, instead of the method linked in the question?). Also, how could I check those logs? – DiluvialSecCertificateCreateWithData
which states "Returns NULL if the data passed in thedata
parameter is not a valid DER-encoded X.509 certificate" (emphasis mine). – Unconformable