Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 为什么可以';我是否覆盖XPC服务中NSURLSessionDataTask的SSL ServerTrust?_Objective C_Macos_Ssl_Certificate_Nsurlsession - Fatal编程技术网

Objective c 为什么可以';我是否覆盖XPC服务中NSURLSessionDataTask的SSL ServerTrust?

Objective c 为什么可以';我是否覆盖XPC服务中NSURLSessionDataTask的SSL ServerTrust?,objective-c,macos,ssl,certificate,nsurlsession,Objective C,Macos,Ssl,Certificate,Nsurlsession,我正在尝试使用NSURLSessionDataTask在XPC服务中执行HTTPS请求。这是失败的,甚至可能会失败,因为服务器正在使用由系统未知的CA签名的TLS证书 因此,我实现了NSURLSessionDelegate协议,实现了URLSession:didReceiveChallenge:completionHandler:方法,获得了SecTrustRef对象,向其添加了CA证书(与我的XPC服务一起提供),并使用SecTrustEvaluate()评估了信任。如果我在没有先添加CA的情

我正在尝试使用
NSURLSessionDataTask
在XPC服务中执行HTTPS请求。这是失败的,甚至可能会失败,因为服务器正在使用由系统未知的CA签名的TLS证书

因此,我实现了
NSURLSessionDelegate
协议,实现了
URLSession:didReceiveChallenge:completionHandler:
方法,获得了
SecTrustRef
对象,向其添加了CA证书(与我的XPC服务一起提供),并使用
SecTrustEvaluate()
评估了信任。如果我在没有先添加CA的情况下进行评估,系统会说证书不受信任,如果我在添加CA后进行评估,系统会说它受信任,因此显然我做的一切都是正确的。最后我打电话

NSURLCredential * credential = [NSURLCredential credentialForTrust:trust];
completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
和连接。。。仍然失败。它仍然失败,错误与我根本没有使用该委托时完全相同(
nsurErrorSecureConnectionFailed
)。完全相同的代码在另一个项目中运行良好。即使我根本不进行评估,只执行以下方法:

- (void)URLSession:(NSURLSession *)session
    didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge
    completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, 
        NSURLCredential * _Nullable))completionHandler
{
    NSString * method = challenge.protectionSpace.authenticationMethod;
    if (![method isEqualToString:NSURLAuthenticationMethodServerTrust]) {
        completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
        return;
    }

    SecTrustRef serverTrust = challenge.protectionSpace.serverTrust;
    if (!serverTrust) {
        completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
        return;
    }

    NSURLCredential * credential = [NSURLCredential credentialForTrust:serverTrust];
    completionHandler(NSURLSessionAuthChallengeUseCredential, credential);
}
连接仍然失败,出现错误
nsurlersecureconnectionfailed

结果表明,当ATS(应用程序传输安全)处于活动状态时(这是针对iOS 9.0/macOS 11.0或更高版本SDK链接的所有应用程序的默认设置),
nsurlsessionelegate
只能收紧安全约束,例如,使用证书/公钥固定,但它不能再松开它们,因为不管学员说什么,ATS将“总是做自己的事情”。除了强制使用HTTPS而不是HTTP之外,ATS还执行自己的证书检查

如果您想独自执行证书验证,则必须禁用ATS。因此,您的
Info.plist
需要一个键
NSAppTransportSecurity
,其值是一个字典,包含布尔键
NSAllowsArbitraryLoads
设置为
YES

从文件中:

NSURLSession强制应用程序传输安全性(ATS),前提是您所连接的域已启用。这适用于连接使用的证书、TLS版本和密码的安全要求您不能放松ATS保护域的服务器信任要求,但可以使用本文所示的手动评估技术来收紧这些要求

资料来源:

而且:

如果没有ATS,您也可以自由放宽默认服务器信任要求,如执行手动服务器信任验证中所述

资料来源:

注意:
当为应用商店构建禁用ATS时,苹果要求您提供书面理由,说明为什么您的应用程序实际需要该例外,以便苹果