首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SSL固定不起作用(目标-C,使用NSURLSession)

SSL固定不起作用(目标-C,使用NSURLSession)
EN

Stack Overflow用户
提问于 2019-12-12 18:26:01
回答 1查看 2K关注 0票数 0

注意:在Android对等应用程序上使用相同的SSL定位。那么iOS改变了什么吗?我的url服务器上有很多证书吗?它们每天都在旋转吗?

问题如下:

  1. SSL钉扎只能通过证书钉扎来实现,那么为什么它不能工作呢?如果有人对如何使用公钥进行清楚的解释,那么
  2. ?请解释一下,

首先,我想解释一下我是如何获得本地证书的。我觉得挺直的。我刚输入了https://ez-pay.io,然后点击了锁图标并下载了证书。如果你知道我指的是什么。现在我想这也可能是问题所在。我的问题是:这是下载证书的正确方式吗?

好吧,假设这是对的。我将证书嵌入或复制到Xcode项目中。

下面是获取远程(服务器)证书并与本地证书进行比较的代码:

代码语言:javascript
复制
- (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;
}

现在的问题是,证书数据的比较总是失败:

代码语言:javascript
复制
[localCerData isEqualToData:remoteCertData] // Always false

我已经尝试了许多不同的方法,检查了谷歌上的5-6链接,都有相同的方法。这个项目有点老了,3-4年了,写在目标-C里。

我尝试了三种方法:

work.

  • Converting

  • 将证书转换为NSData并进行比较

  • 也是一个黑客--我打印数据并以字符串格式保存,并在应用程序中对其进行硬编码,然后下次继续将其与服务器的证书数据进行比较。此方法只适用于一天和第二天服务器的证书数据更改。不知道发生了什么。

请建议一下这里有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-05-23 16:24:12

  • 我解决了这个问题。

因此,

代码语言:javascript
复制
- 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或您自己的数据计划下载证书,然后将该证书插入您的代码中,我确信它将与远程证书匹配。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59310730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档