1

我正在尝试将我的应用程序连接到具有错误主机名的服务器证书的开发服务器,但它由受信任的锚证书签名。当我评估服务器信任对象时,它按预期失败。我正在尝试更改服务器主机名的信任评估策略,但这似乎没有帮助。

// In -connection:willSendRequestForAuthenticationChallenge:
// NSURLAuthenticationMethodServerTrust
SecTrustRef trust = [challenge.protectionSpace serverTrust];
SecTrustResultType trustResult;
SecTrustEvaluate(trust, &trustResult);
// trustResult == kSecTrustResultRecoverableTrustFailure

SecPolicyRef policyOverride = SecPolicyCreateSSL(true, (CFStringRef)@"devhost");
CFArrayRef policies = (CFArrayRef)@[policyOverride];
SecTrustSetPolicies(trust, policies);
CFRelease(policyOverride);
SecTrustEvaluate(trust, &trustResult);
// trustResult == kSecTrustResultRecoverableTrustFailure

据我了解,第二次调用 SecTrustEvaluate() 时,它应该返回kSecTrustResultUnspecified. 当我初始化 NSURLConnection 时,我已经使用“devhost”连接到开发服务器challenge.protectionSpace.host == @"devhost"。我在这里做错了什么?

4

1 回答 1

1

我在调用中使用了错误的主机名SecPolicyCreateSSL

使用SecPolicyCreateSSL覆盖主机名验证时,主机名参数应该是与您正在验证的证书匹配的参数。然后验证假装您正在与之通信的主机具有新指定的主机名。

在我的情况下,服务器有一个“* .mydomain.tld”的证书,所以我打电话

SecPolicyCreateSSL(true, (CFStringRef)@"devhost.mydomain.tld");

然后可以成功验证证书链。

于 2014-05-22T15:51:21.370 回答