如何在Cocoa WebView中显示服务器证书(https请求)信息?

时间:2013-09-27 12:45:37

标签: cocoa https webview cfnetwork

在我的 mac app 中,我有网络视图&当用户访问任何带有“ https ”的网站时,我想显示一个小按钮(如在safari中)。点击此按钮,我想显示证书信息。

这是我到现在为止所尝试的......

1)已实现 - (void)webView:(WebView *)发件人资源:(id)标识符didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)质询fromDataSource:(WebDataSource *)dataSource

但是,当我访问任何带有“https”

的网站时,系统未调用此代理

2)由于上述失败,我写了另一段代码(在示例应用程序中)

// Create the request.
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.onlinesbi.com"]];

// Create url connection and fire request
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

并实施

(void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge

{

if (challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust) {
    // create trust from protection space
    SecTrustRef trustRef;
    int trustCertificateCount = (int)SecTrustGetCertificateCount(challenge.protectionSpace.serverTrust);

    NSMutableArray* trustCertificates = [[NSMutableArray alloc] initWithCapacity:trustCertificateCount];
    for (int i = 0; i < trustCertificateCount; i++) {
        SecCertificateRef trustCertificate =  SecTrustGetCertificateAtIndex(challenge.protectionSpace.serverTrust, i);
        [trustCertificates addObject:(__bridge id) trustCertificate];
    }
}

}

现在,当我运行应用程序时,它来到此委托,但证书的计数为0

3)此特定链接提及使用CFNetwork API How to display certificate of HTTPS request in embedded WebView

现在我很困惑我是否真的需要去CFNetwork级别&amp;实现代码或我在上面做错了1)&amp; 2),因为我没有收到信息...

1 个答案:

答案 0 :(得分:3)

(2)中的代码对我来说很好。

 NSLog(@"%@", trustCertificates)

打印以下内容:

(
    "<SecCertificate 0x100107c80 [0x7fff7bb4af00]>",
    "<SecCertificate 0x100107ec0 [0x7fff7bb4af00]>",
    "<SecCertificate 0x100526fe0 [0x7fff7bb4af00]>"
)

此外,导入<SecurityInterface/SFCertificatePanel.h>并在for循环后添加以下行会显示一个显示证书链的弹出窗口:

[[SFCertificatePanel sharedCertificatePanel] runModalForCertificates:trustCertificates showGroup:YES];