Ios SSL固定不起作用(Objective-C,使用NSURLSession)
注意:在Android应用程序上使用相同的SSL固定。那么iOS有什么改变吗?我的url服务器上有很多证书,并且它们每天都在轮换吗 问题是:Ios SSL固定不起作用(Objective-C,使用NSURLSession),ios,objective-c,ssl,ssl-certificate,Ios,Objective C,Ssl,Ssl Certificate,注意:在Android应用程序上使用相同的SSL固定。那么iOS有什么改变吗?我的url服务器上有很多证书,并且它们每天都在轮换吗 问题是: SSL固定只能通过证书固定来实现,那么为什么它不起作用呢 如果有人对如何使用公钥有明确的解释?请解释一下 首先,我想解释一下我是如何获得本地证书的。我觉得很直。我只需输入,然后单击锁图标并下载证书。如果你知道我的意思。现在我想这也可能是问题所在。我的问题是:这是下载证书的正确方式吗 好吧,我们假设这是对的。我将证书嵌入或复制粘贴到我的Xcode项目中 下面
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential * _Nullable credential))completionHandler
{
NSLog(@"SECURITY : didReceiveChallenge");
// REMOTE CERTIFICATE
SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
// Evaluate server certificate [Whether the certificate is valid or not] --
SecTrustResultType result;
SecTrustEvaluate(serverTrust, &result);
BOOL isRemoteCertValid = (result == kSecTrustResultUnspecified || result == kSecTrustResultProceed);
// Remote certificate object --
SecCertificateRef certificateRemote = SecTrustGetCertificateAtIndex(serverTrust, 0);
// Returns a DER representation of a certificate --
NSData *remoteCertData = CFBridgingRelease(SecCertificateCopyData(certificateRemote));
NSData *localCerData = [self GetLocalCertificateData];
// base 64 encoding of remote certificate --
NSString* strRemote = [remoteCertData base64EncodedStringWithOptions:0]; // get string from certificate --
NSString *strLocal = [self GetLocalCertificateStringForPublicKey];
// The pinning check -- compare the remote and local certificate data / string --
// if (isRemoteCertValid && [strLocal isEqualToString:strRemote] )
if (isRemoteCertValid && [localCerData isEqualToData:remoteCertData] )
{
NSLog(@"SECURITY : OK ");
}
else
{
NSLog(@"SECURITY : FAILS");
isCertificateValid = false;
}
}
- (NSData *)GetLocalCertificateData
{
if ( MODE == PROD_US)
{
NSString *pathToCert = [[NSBundle mainBundle]pathForResource:@"prod_ezpay" ofType:@"cer"];
NSData *localCertificate = [NSData dataWithContentsOfFile:pathToCert];
return localCertificate;
}
else if (MODE == PREP_US)
{
NSString *pathToCert = [[NSBundle mainBundle]pathForResource:@"prepEZPAY" ofType:@"cer"];
NSData *localCertificate = [NSData dataWithContentsOfFile:pathToCert];
return localCertificate;
}
return nil;
}
现在的问题是,证书数据的比较总是失败:
[localCerData isEqualToData:remoteCertData] // Always false
我一直在尝试许多不同的方法检查谷歌上的5-6个链接,都有相同的方法。这个项目有点老,3-4年,用Objective-C编写
我尝试了3种方法:
- 我解决了这个问题
- 当您离开本地办公网络或wifi时,必须始终下载ssl证书。因为如果你下载你的 vpn/办公室局域网/办公室wifi内的证书,您的证书 将被调整/覆盖李>
- 因此,当您将您下载的证书锁定在您的安全专用办公室网络中时,它将永远不会匹配 作为远程证书的远程证书始终是公共的 因此,请从open wifi或您自己的数据计划下载证书:),然后将此证书固定在您的代码中,我确信 然后它将匹配远程证书