【问题标题】:Get SSL certificate details获取 SSL 证书详细信息
【发布时间】:2011-12-13 17:35:26
【问题描述】:

我想检查-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 收到的 SSL 证书,我有以下 sn-p,它提供了颁发者公用名和 DER。

SecTrustRef trustRef = [[challenge protectionSpace] serverTrust];
SecTrustEvaluate(trustRef, NULL);
CFIndex count = SecTrustGetCertificateCount(trustRef); 

for (CFIndex i = 0; i < count; i++)
{
    SecCertificateRef certRef = SecTrustGetCertificateAtIndex(trustRef, i);
    CFStringRef certSummary = SecCertificateCopySubjectSummary(certRef);
    CFDataRef certData = SecCertificateCopyData(certRef);
}

另外我想获取指纹和签名。 我的 SSL 知识没有那么深。我可以从 DER 表示中提取上述内容吗?

文档没有帮助。 http://developer.apple.com/library/ios/#documentation/Security/Reference/certifkeytrustservices/Reference/reference.html.

【问题讨论】:

  • 我很惊讶他们没有提供使用 DER 表示的工具(另见“ASN.1”和“X.509”),因为如果你不这样做,它真的很复杂定期这样做。
  • 我惊讶地发现这些重要信息无法通过 Cocoa 获得,我不得不“深入”并使用 CF……我会看看我能从 DER 中提取什么;复杂与否,它必须完成......

标签: ios ssl certificate signature fingerprint


【解决方案1】:

你可以这样获取sha1指纹。

// #import <CommonCrypto/CommonDigest.h>
+(NSString*)sha1:(NSData*)certData {
    unsigned char sha1Buffer[CC_SHA1_DIGEST_LENGTH]; 
    CC_SHA1(certData.bytes, certData.length, sha1Buffer); 
    NSMutableString *fingerprint = [NSMutableString stringWithCapacity:CC_SHA1_DIGEST_LENGTH * 3]; 
    for (int i = 0; i < CC_SHA1_DIGEST_LENGTH; ++i) 
        [fingerprint appendFormat:@"%02x ",sha1Buffer[i]]; 
    return [fingerprint stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
}

md5指纹可以通过类似的方式获得。以这种方式获得的 sha1 和 md5 哈希与 Safari 和 Chrome 显示的指纹相匹配,以获得不受信任的证书。

【讨论】:

  • 有没有办法获取证书的开始和到期日期。如果是,请分享代码。提前致谢。
猜你喜欢
  • 1970-01-01
  • 2012-07-06
  • 2011-03-24
  • 2021-09-12
  • 2013-07-18
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
相关资源
最近更新 更多