iOS HTTPS 请求 101

标签 ios objective-c node.js ssl https

NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9813)

非常非常令人沮丧!我已经为此拉了几个小时的头发。我在我的 Linode 服务器上使用自签名证书。端口是 8000,无法让它在 443 上工作。我不相信这是原因。这是我的代码,它是 99% 的样板:

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.myserver.com:8000/test.json"]];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];

在底部:

#pragma mark NSURLConnectionDelegate

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    NSLog(@"protectionSpace: %@", [protectionSpace authenticationMethod]);

    // We only know how to handle NTLM authentication.
    if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodNTLM])
        return YES;

    // Explicitly reject ServerTrust. This is occasionally sent by IIS.
    if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust])
        return NO;

    return NO;
}

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    [[challenge sender] continueWithoutCredentialForAuthenticationChallenge:challenge];
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"%@", response);
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"%@", data);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"didFailWithError");
    NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}

天哪,救命!

更新

它与这个委托(delegate)方法一起工作。我正在收到回复,但有问题。

- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
    [[challenge sender] useCredential:[NSURLCredential
                                       credentialWithUser:@"user"
                                       password:@"password"
                                       persistence:NSURLCredentialPersistencePermanent] forAuthenticationChallenge:challenge];

}

我提供的“用户”和“密码”是完全随机的,服务器不会检查。 如何在我的服务器上接受连接之前验证凭据?

编辑:我正在运行 Node.js 服务器

最佳答案

获取相应的错误描述可能会有所帮助:

所以,首先错误域kCFStreamErrorDomainSSL表示错误代码是Security/SecureTransport.h中定义的SSL错误代码:

kCFStreamErrorDomainSSL, -9813 表示:

errSSLNoRootCert = -9813,/* 证书链未经 root 验证 */

这只是意味着,您没有受信任的根证书,并且由于身份验证失败而导致连接失败。

在设备上为服务器信任身份验证提供根证书就可以了。

有几种方法可以使用自签名证书实现服务器信任身份验证,其中一种比另一种更安全。

最简单的方法需要存储在应用程序包中的自签名证书,然后检索并简单地进行字节比较。这是一个例子:

Implementing server trust authentication with a self-signed certificate .

这些也是必读的:Technical Note TN2232 HTTPS Server Trust EvaluationTechnical Q&A QA1360 Describing the kSecTrustResultUnspecified error .

更优选的方法是使用您可以自己的 CA(证书颁发机构)。也就是说,您创建自己的 CA 并使用此 CA 签署证书。

步骤类似:

  1. 在您的应用中捆绑 CA 根证书的 DER 文件。
  2. 如下处理服务器信任认证:

    1. 获取身份验证质询
    2. 从挑战中检索信任对象
    3. 根据 bundle 中的数据创建证书对象
    4. 使用函数 SecTrustSetAnchorCertificates 将证书对象设置为信任对象的 anchor 。
    5. 评估信任度

关于iOS HTTPS 请求 101,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18033525/

相关文章:

iOS - 来自后台时上一屏幕闪烁

ios - 如何检查 UITextField 中看不见的字符?

node.js - MongoDB:计算集合中文档出现的次数

html - 如何在index.html 中指定构建编译指示?

javascript - 为什么即使没有 salt 参数,哈希比较输出也为 true?

ios - iOS项目中如何集成MuPDF

ios - UITableView 固定部分与动态大小

ios - 在应用发布后更改支持的iOS版本吗?

ios - SwiftUI 仅显示黑屏

objective-c - SceneKit跟随相机?