cocoa-touch - 在 iOS 应用中显示不受信任的证书信息

标签 cocoa-touch ios6 ios7 nsurlconnection digital-certificate

在我的 iOS 应用程序中,我试图连接到具有不受信任证书的服务器。

我正在按照此 url 中指定的程序处理这种情况:

https://developer.apple.com/library/mac/documentation/cocoa/conceptual/urlloadingsystem/Articles/AuthenticationChallenges.html

这工作正常。

现在我有一个要求,我需要显示与证书相关的详细信息,例如:

  • 姓名
  • 地点
  • 组织单位
  • 邮箱地址
  • 在日期之前无效
  • 日期后无效
  • 签名算法

  • 现在我有几个问题:

    一季度。我如何获得上述登记信息?是否有任何 cocoa API 提供相同的功能?

    Q2。通常在 Web 浏览器中,它会显示与该证书相关的所有详细信息。我们是否需要在 iOS 应用程序中遵循相同的行为?

    请建议。

    最佳答案

    Q1:https://stackoverflow.com/a/8903088/2957168应该完全回答你的问题。如果您需要额外的解释 - 请发表评论。

    Q2:这是你的决定 - 如果你的服务器运行你自己的证书并且你喜欢保持这种方式(就像企业应用程序一样),只需验证证书 SHA1 或 MD5 是你期望的(这就是我们在我们的应用)。

    这意味着您需要一个实现 NSURLConnectionDelegate 的类或 NSURLConnectionDataDelegate (如果您还需要数据处理程序)

    然后实现这些方法:

    - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
    {
        NSLOG(@"connection canAuthaenticateAgainstProtectionSpace");
        if (![Certificates verifyProtectionSpace:protectionSpace]) { //this to verify your own certificate which is self signed.
            NSLOG(@"Bad Certificate, canceling request");
            [connection cancel];
            self.ended = true;
            return false;
        }
        return true;
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
    {
        NSLOG(@"connection didReceiveAuthenticationChallenge");
        if ([Certificates verifyProtectionSpace:challenge.protectionSpace]) { //this is where you verify the certificates again - for non self-signed ones usually.
            [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
        } else {
            [challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
        }
    }
    

    关于cocoa-touch - 在 iOS 应用中显示不受信任的证书信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20026532/

    相关文章:

    ios - 在选择时为 uicollectionview 单元格设置动画

    iphone - 获取图像大小而不加载到内存

    iphone - UIWebView是否泄漏内存?

    iphone - 可以使用 Core Animation 使状态栏消失/溶解吗?

    iOS7 中的 UITableViewCell indexPath 崩溃

    ios - 如何在具有多个组件的 UIPickerView 中为特定组件添加图像?

    ios - 如何获得 CurrentCulture 的正确值?

    iphone - UINavigationController 和自动旋转

    ios - 如何自定义 UIActivityViewController 以在发布到 facebook 和 twitter 时显示 URL 链接?

    ios - 动态启用/禁用 UITableViewCell 上的滑动删除功能