Cocoa touch 在iOS应用程序中显示不受信任的证书信息
在我的iOS应用程序中,我正在尝试连接到具有不受信任证书的服务器 我按照此url中指定的程序处理此情况: 这很好用 现在我有一个要求,其中我需要显示与证书相关的详细信息,例如:Cocoa touch 在iOS应用程序中显示不受信任的证书信息,cocoa-touch,ios6,ios7,nsurlconnection,digital-certificate,Cocoa Touch,Ios6,Ios7,Nsurlconnection,Digital Certificate,在我的iOS应用程序中,我正在尝试连接到具有不受信任证书的服务器 我按照此url中指定的程序处理此情况: 这很好用 现在我有一个要求,其中我需要显示与证书相关的详细信息,例如: 名字 位置 组织单位 电子邮件地址 在日期之前无效 日期后无效 签名算法 现在我有几个问题: 问题1。我如何获得上述登记信息?是否有cocoa API提供相同的功能 问题2。通常在web浏览器中,它会显示与该证书相关的所有详细信息。我们是否需要在iOS应用程序中遵循相同的行为 请提出建议。Q1:应该完全回答您的问题。如
nsurconnectiondelegate
或nsurconnectiondatadelegate
的类(如果您还需要数据的处理程序)
然后实现以下方法:
- (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 API: 信任对象和NSURLConnection 您将看到如何覆盖我假设您正在使用的NSURLConnection的链验证行为。在提供的代码中,您可以获得对SecTrustRef的引用,可以使用安全框架中的SecTrustGetCertificateCount进一步查询该引用。在迭代证书时,可以使用SecCertificateCopyValues()查询它们,并使用OID密钥提取返回字典中的任何信息