【问题标题】:Unable to use self signed certificate with AFNetworking 2AFNetworking 2 无法使用自签名证书
【发布时间】:2013-11-26 20:50:54
【问题描述】:

我将 Apache Server 使用的 .cer 证书放在了 Xcode 项目中。当应用程序尝试与服务器通信时,我在 Xcode 中收到此错误:

Assertion failure in id AFPublicKeyForCertificate(NSData *__strong)(),
/Users/../ProjectName/AFNetworking/AFSecurityPolicy.m:52
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
reason: 'Invalid parameter not satisfying: allowedCertificate'

这是调用服务器的代码:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
manager.responseSerializer = [AFJSONResponseSerializer serializer];
[self setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];
[manager POST:@"https://www.example.com/" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//success
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//failure
}];

我将固定模式更改为 AFSSLPinningModeCertificate,但没有成功。

当我删除这一行时:

[self setSecurityPolicy:[AFSecurityPolicy policyWithPinningMode:AFSSLPinningModePublicKey]];

服务器返回错误信息:

"The operation couldn't be completed. (NSURLErrorDomain error -1012.)"

证书是使用 OpenSSL 创建的,我什至尝试了 StartSSL.com 的免费证书

至于 Apache Server 端,这里是虚拟主机配置:

# My custom host
<VirtualHost *:443>
    ServerName www.example.com
    DocumentRoot "/path/to/folder"
    SSLEngine on
    SSLCipherSuite HIGH:!aNULL:!MD5
    SSLCertificateFile /path/to/www.example.com.cer
    SSLCertificateKeyFile /path/to/www.example.com.key
    <Directory "/the/directory/">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Require all granted
    </Directory>
    ErrorLog "logs/mysite.local-error_log"
</VirtualHost>

而且服务器确实监听 443 端口

【问题讨论】:

    标签: ios apache ssl-certificate afnetworking-2


    【解决方案1】:

    您的证书文件格式似乎不正确。您的代码在这些行(AFURLConnectionOperation/pinnedPublicKeys)失败:

    SecCertificateRef allowedCertificate = SecCertificateCreateWithData(NULL, (__bridge CFDataRef)data);
    NSParameterAssert(allowedCertificate);
    

    当我的证书看起来像这样时,我遇到了同样的错误(AFNetworking 1.1,但版本无关紧要):

    -----BEGIN CERTIFICATE-----
    ..
    -----END CERTIFICATE----- 
    

    我设法通过使用来自this answer 的命令将证书转换为 x509 格式来解决此问题:

    openssl x509 -in adn.crt -outform der -out "adn.der"
    

    之后我将adn.der 重命名回adn.cer('.cer' 似乎是AFNetworking 的预期扩展名),现在一切正常。

    【讨论】:

      【解决方案2】:

      问题不在于 AFNetworkings,而在于 iOS':您需要在设备上安装自签名证书,因为 iOS 安全设置禁止连接到不受信任的来源。

      您可以通过在 iOS 设备上打开证书(将其邮寄给自己并打开)并按照安装说明将自签名证书添加为可信来源。

      【讨论】:

      • 这是我的怀疑,因为我一直在使用iOS模拟器
      【解决方案3】:

      如果需要,您可以通过更改安全策略来禁用无效证书检查。

      [self setAllowInvalidCertificates:YES];
      

      请在文档中阅读更多内容:http://cocoadocs.org/docsets/AFNetworking/2.0.3/Classes/AFSecurityPolicy.html#//api/name/allowInvalidCertificates

      您也可以固定证书:http://cocoadocs.org/docsets/AFNetworking/2.0.3/Classes/AFSecurityPolicy.html#//api/name/pinnedCertificates

      【讨论】:

      • 这可以在 iOS 模拟器上运行吗?因为我仍然在 AFSecurityPolicy.m:52 上收到错误:'无效参数不满足:allowedCertificate'
      【解决方案4】:

      【讨论】:

      • 您能从您的链接中发布一些内容吗?
      猜你喜欢
      • 2014-06-22
      • 1970-01-01
      • 2014-04-10
      • 1970-01-01
      • 2018-06-02
      • 2010-11-05
      • 2015-11-03
      • 2020-04-16
      • 1970-01-01
      相关资源
      最近更新 更多