【问题标题】:iPhone Custom CA certificate for an application which uses NSURLConnection?使用 NSURLConnection 的应用程序的 iPhone 自定义 CA 证书?
【发布时间】:2011-03-02 20:30:39
【问题描述】:

我有一个与许多不同站点通信的应用程序,每个站点都有自己的 SSL 证书,由我们自己的内部 CA 签名。这样做可以避免我们为每个站点(数百或数千个)购买 SSL 证书,并且比在每个站点上使用带有共享密钥的通配符证书更安全。因此,基本上使用 CA 证书是唯一的方法。

现在,我有一个 mobileprovision 文件,它将 CA 证书作为配置文件安装在手机上。当我们的 iPhone 应用程序启动时,如果它收到 SSL 证书错误,它会通过 Safari 重定向到此移动配置文件,并且将提示用户安装 CA。

问题是我担心 Apple AppStore 可能会拒绝我的应用程序这样做(此时只是其他开发人员的一些反馈),我想研究其他方法来实现这一点。

基本上,我需要完成的是允许 SSL 连接,该连接将根据嵌入在我的应用程序中的自定义 CA 证书进行验证。这将使 CA 证书仅对我进行的调用有效。我正在使用标准的 NSURLConnection 方法来与服务通信。

这可能吗?有人可以告诉我如何加载 CA(什么形式的 PEM?)并将其添加到我的应用程序的受信任 CA 证书列表中吗?如果那不可能,我还有什么其他选择?仅仅信任所有证书并不是任何选择,我们希望防止中间人攻击,并且只信任我们的 CA 颁发的证书。

谢谢!

【问题讨论】:

    标签: iphone macos ssl nsurlconnection


    【解决方案1】:

    使用下面两个 NSURLConnection 的委托方法来访问任何证书无效的站点

       - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
        {
    
                NSLog(@"canAuthenticateAgainstProtectionSpace");
            if([[protectionSpace authenticationMethod] isEqualToString:NSURLAuthenticationMethodServerTrust]) {
                // Note: this is presently only called once per server (or URL?) until
                //       you restart the app
                    NSLog(@"Authentication checking is doing");
                return YES; // Self-signed cert will be accepted
                // Note: it doesn't seem to matter what you return for a proper SSL cert
                //       only self-signed certs
            }
            return NO;
        }
    
        - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
        {
                NSLog(@"didReceiveAuthenticationChallenge");
            if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust])
            {
                [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
                    NSLog(@"chalenging protection space authentication checking");
            }
        }
    

    【讨论】:

    • 目前我已经实现了这些但没有被调用:( [NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[[NSURL URLWithString:url] host]];
    猜你喜欢
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2010-09-25
    • 2016-08-22
    • 2017-01-14
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    相关资源
    最近更新 更多