ios - 获取 SSL 证书详细信息

标签 ios ssl certificate signature fingerprint

我想检查 -(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge 收到的 SSL 证书,我有以下代码片段,它为我提供了 Issuer Common Name , 和 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 .

最佳答案

你可以这样获取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 显示的不受信任证书的指纹相匹配。

关于ios - 获取 SSL 证书详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7869278/

相关文章:

ios - Verifone e355 iOS 集成 - 无法使用软模式触发条码扫描仪

java - 检测要在 JAVA 中插入/删除的 PKCS#11 token

php - 通过公共(public)互联网安全发送数据的替代方法

azure - 将新的受信任 CA 添加到 Azure 上的证书颁发机构证书存储中

ios - GPSDictionary 返回 nil,uiimage 的元数据中没有 exif 位置数据

javascript - 为什么点击后是touchstart事件?

ios - 嵌套的 Collection View 单元格不会根据方向变化调整大小

c# - SSL HttpWebRequest 导致内部服务器错误?

ios - 我应该使用一个 iOS 分发证书来签署不同的应用程序吗?

java - 基本 twilio-java 应用程序中的 SSLPeerUnverifiedException