Tuesday, May 30, 2017

Certificate pinning in Xcode

Leave a Comment

I got below code for certificate pinning in Android

CertificatePinner certificatePinner = new CertificatePinner.Builder() .add("publicobject.com", "sha1/DmxUShsZuNiqPQsX2Oi9uv2sCnw=") .add("publicobject.com", "sha1/SXxoaOSEzPC6BgGmxAt/EAcsajw=") .add("publicobject.com", "sha1/blhOM3W9V/bVQhsWAcLYwPU6n24=") .add("publicobject.com", "sha1/T5x9IXmcrQ7YuQxXnxoCmeeQ84c=") .build(); 

How do i achieve same task in IOS using NSURLSession method?

Got some reference code here

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { SecTrustRef serverTrust = challenge.protectionSpace.serverTrust; SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0); NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate)); NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"MyLocalCertificate" ofType:@"cer"]; NSData *localCertData = [NSData dataWithContentsOfFile:cerPath]; if ([remoteCertificateData isEqualToData:localCertData]) { NSURLCredential *credential = [NSURLCredential credentialForTrust:serverTrust]; [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge]; } else { [[challenge sender] cancelAuthenticationChallenge:challenge]; } 

EDIT PART

I got below solution, which delegate function is called automatically in NSURLSession, can anyone explain how it will work ? ALSO Need to send multiplier certificate how do i do it?

 (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential * _Nullable))completionHandler {     NSString *authMethod = [[challenge protectionSpace] authenticationMethod];      if ([authMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {          NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];         completionHandler(NSURLSessionAuthChallengeUseCredential,credential);     } else {         SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;         SecCertificateRef certificate = SecTrustGetCertificateAtIndex(serverTrust, 0);         NSData *remoteCertificateData = CFBridgingRelease(SecCertificateCopyData(certificate));         NSString *cerPath = [[NSBundle mainBundle] pathForResource:@"MyLocalCertificate" ofType:@"cer"];         NSData *localCertData = [NSData dataWithContentsOfFile:cerPath];         NSURLCredential *credential;          if ([remoteCertificateData isEqualToData:localCertData]) {              credential = [NSURLCredential credentialForTrust:serverTrust];             [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];         }         else {             [[challenge sender] cancelAuthenticationChallenge:challenge];         }            completionHandler(NSURLSessionAuthChallengeUseCredential, credential);         NSLog(@"Finished Challenge");     } } 

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment