AFNetworking pin public key for a trusted certificate

afnetworking, afnetworking-2, nsurlconnection, objective-c

Solution

AFNetworking has an AFSecurityPolicy object has values for security features, including the SSL pinning mode.

You can set the securityPolicy on an AFHTTPRequestOperation:

AFSecurityPolicy *securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey];
operation.securityPolicy = securityPolicy;

Your certificate must have the extension `cer` not `crt` and should be in DER format. Add it to your bundle. You can convert it to the correct format in a terminal with the following command:

`openssl x509 -in domain.crt -out domain.cer -outform der`

You should not include keys in your app bundle, only the certificate is required.

Problem

I use AFNetworking 2.3.1, I have a trusted certificate for which I'd like to pin the public key. I have the `crt`, `key`, `pfx` files, so I imagine I have to add them into my bundle. ``` AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { NSLog(@"Success"); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { return [self processError:[operation response]]; }]; [operation start]; ``` Now how can I tell AFNetworking to use the `AFSSLPinningModePublicKey` mode ? (I don't see the `setSSLPinningMode` method from `AFHTTPRequestOperation`) And how do I tell AFNetworking to use the added key ? I can't find any example on the documentation.

Original source