代码之家  ›  专栏  ›  技术社区  ›  Raphaël Pinto

使用AFNetworking的自签名SSL证书2.3.1

  •  2
  • Raphaël Pinto  · 技术社区  · 11 年前

    我正在尝试使用自签名证书 .cer 在我的iOS应用程序中。我遵循了这个很好的教程: http://initwithfunk.com/blog/2014/03/12/afnetworking-ssl-pinning-with-self-signed-certificates/

    我在项目中添加了.cer文件。 并初始化我的AFHTTPRequestOperationManager:

    self.securityPolicy = [AFSecurityPolicy policyWithPinningMode:AFSSLPinningModeCertificate];
    self.securityPolicy.allowInvalidCertificates = YES;
    

    但我总是会遇到这个-1012错误:

    错误操作无法完成。(NSURLErrorDomain错误 -1012.)

    我检查了 + (NSArray *)defaultPinnedCertificates 它正确加载我的 .cer文件 .

    但是 evaluateServerTrust:forDomain: 始终返回NO:

    return trustedCertificateCount == [serverCertificates count];

    [serverCertificates count] =2和 trustedCertificateCount = 1.

    这意味着什么?你能帮帮我吗?

    1 回复  |  直到 11 年前
        1
  •  5
  •   David Snabel-Caunt    11 年前

    AFSecurityPolicy 的默认行为是验证证书链。您应该添加所有中间证书,或禁用链的验证:

    self.securityPolicy.validatesCertificateChain = NO;

    添加中间证书是首选方法。