Skip to content

Instantly share code, notes, and snippets.

@alexkent
Last active August 29, 2015 14:04
Show Gist options
  • Save alexkent/8ec7ce5f3d9153544231 to your computer and use it in GitHub Desktop.
Save alexkent/8ec7ce5f3d9153544231 to your computer and use it in GitHub Desktop.
This is probably a bad idea, but I am not sure why: SSL pinning by comparing certificate data.
- (id)initWithCertificatePath:(NSString *)path
{
self = [super init];
if (self) {
_certData = [NSData dataWithContentsOfFile:path];
}
return self;
}
- (void)validateChallenge:(NSURLAuthenticationChallenge *)challenge {
SecTrustRef trust = challenge.protectionSpace.serverTrust;
SecCertificateRef serverCertificate = SecTrustGetCertificateAtIndex(trust, 0);
NSData *serverCertificateData = (__bridge NSData *)(SecCertificateCopyData(serverCertificate));
if ([serverCertificateData isEqualToData:_certData]) {
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:trust] forAuthenticationChallenge:challenge];
}
else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment