iOS NSURLConnection 与 SSL : Accepting an expired self-signed certificate

标签 ios ssl nsurlconnection nsurlconnectiondelegate security-framework

我有一个已发布的应用程序,它使用以下代码通过应用程序随附的自签名证书来保护 SSL 连接。

- (void) connection:(NSURLConnection *)conn willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"didReceiveAuthenticationChallenge %@ FAILURES=%d", [[challenge protectionSpace] authenticationMethod], (int)[challenge previousFailureCount]);

    /* Setup */
    NSURLProtectionSpace *protectionSpace   = [challenge protectionSpace];
    assert(protectionSpace);
    SecTrustRef trust                       = [protectionSpace serverTrust];
    assert(trust);
    CFRetain(trust);  // Make sure this thing stays around until we're done with it
    NSURLCredential *credential             = [NSURLCredential credentialForTrust:trust];

    /* Build up the trust anchor using our root cert */

    int err;
    SecTrustResultType trustResult = 0;
    err = SecTrustSetAnchorCertificates(trust, certs);
    if (err == noErr) {
        err = SecTrustEvaluate(trust,&trustResult);
    }
    CFRelease(trust);  // OK, now we're done with it

    // http://developer.apple.com/library/mac/#qa/qa1360/_index.html
    BOOL trusted = (err == noErr) && ((trustResult == kSecTrustResultProceed) || (trustResult == kSecTrustResultConfirm) || (trustResult == kSecTrustResultUnspecified));

    // Return based on whether we decided to trust or not
    if (trusted) {
        [[challenge sender] useCredential:credential forAuthenticationChallenge:challenge];
    } else {
        NSLog(@"Trust evaluation failed for service root certificate");
        [[challenge sender] cancelAuthenticationChallenge:challenge];
    }
}

不幸的是,我犯了一个巨大的疏忽。 SSL 证书过期。因此,当到期日期过去时,我假设该应用程序将停止正常工作!对于当前版本的应用程序,我无能为力 - 它很快就会停止工作。

我需要发布更新,为了避免将来出现这种情况,我想允许使用自签名证书,即使它已经过期。

即使证书已过期,我如何修改上面的代码以信任证书?

最佳答案

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:Nil];
...
...
- (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler{
  if([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]){
    if([challenge.protectionSpace.host isEqualToString:@"mydomain.com"]){
      NSURLCredential *credential = [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust];
      completionHandler(NSURLSessionAuthChallengeUseCredential,credential);
    }
  }
}

关于iOS NSURLConnection 与 SSL : Accepting an expired self-signed certificate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18739415/

相关文章:

qt - 在支持 OpenSSL 的 Windows 上构建 PyQt5?

c++ - 从 X509* 获取主题和发行者信息

cocoa - 带有 UTF8 密码的 NSURLRequest

ios - 连接到服务器然后在 mkmapview 上绘制坐标

ios - 如何在AppDelegate中隐藏和显示UIViewController按钮? - swift

ios - 如何在表格单元格中打印联系人的电话号码

java - 如何在 Jetty-9 上使用证书执行自定义客户端身份验证?

iphone - 当我设置值和 forKey 时,iOS NSMutabledictionary 为空

iOS NSTextAttachment 图片不显示

ios - 此服务器的证书无效