注意:在Android对等应用程序上使用相同的SSL定位。那么iOS改变了什么吗?我的url服务器上有很多证书吗?它们每天都在旋转吗?
问题如下:
首先,我想解释一下我是如何获得本地证书的。我觉得挺直的。我刚输入了https://ez-pay.io,然后点击了锁图标并下载了证书。如果你知道我指的是什么。现在我想这也可能是问题所在。我的问题是:这是下载证书的正确方式吗?
好吧,假设这是对的。我将证书嵌入或复制到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年了,写在目标-C里。
我尝试了三种方法:
work.
请建议一下这里有什么问题吗?
发布于 2020-05-23 16:24:12
因此,
- You must always download ssl certificate when you are out of your local office network or wifi . Because if you download your certificate within vpn / office LAN / office wifi , your certificate will be tempered / overwritten .
- Thus when you will pin the certificate which you downloaded in your secure private office network then , then it will never match the remote certificate as remote certificate is always public. 从开放的wifi或您自己的数据计划下载证书,然后将该证书插入您的代码中,我确信它将与远程证书匹配。
https://stackoverflow.com/questions/59310730
复制相似问题