Last active
August 29, 2015 14:04
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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