我查看过安全框架文档,但似乎无法找到获取给定钥匙串上所有证书的方法。有没有方法来完成这个?获取钥匙串中的证书
11
A
回答
13
挖掘的文档,头文件和源文件后,我想出了下面的代码:
#import <Security/Security.h>
- (void)logMessageForStatus:(OSStatus)status
functionName:(NSString *)functionName
{
CFStringRef errorMessage;
errorMessage = SecCopyErrorMessageString(status, NULL);
NSLog(@"error after %@: %@", functionName, (NSString *)errorMessage);
CFRelease(errorMessage);
}
- (void)listCertificates
{
OSStatus status;
SecKeychainSearchRef search = NULL;
// The first argument being NULL indicates the user's current keychain list
status = SecKeychainSearchCreateFromAttributes(NULL,
kSecCertificateItemClass, NULL, &search);
if (status != errSecSuccess) {
[self logMessageForStatus:status
functionName:@"SecKeychainSearchCreateFromAttributes()"];
return;
}
SecKeychainItemRef searchItem = NULL;
while (SecKeychainSearchCopyNext(search, &searchItem) != errSecItemNotFound) {
SecKeychainAttributeList attrList;
CSSM_DATA certData;
attrList.count = 0;
attrList.attr = NULL;
status = SecKeychainItemCopyContent(searchItem, NULL, &attrList,
(UInt32 *)(&certData.Length),
(void **)(&certData.Data));
if (status != errSecSuccess) {
[self logMessageForStatus:status
functionName:@"SecKeychainItemCopyContent()"];
CFRelease(searchItem);
continue;
}
// At this point you should have a valid CSSM_DATA structure
// representing the certificate
SecCertificateRef certificate;
status = SecCertificateCreateFromData(&certData, CSSM_CERT_X_509v3,
CSSM_CERT_ENCODING_BER, &certificate);
if (status != errSecSuccess) {
[self logMessageForStatus:status
functionName:@"SecCertificateCreateFromData()"];
SecKeychainItemFreeContent(&attrList, certData.Data);
CFRelease(searchItem);
continue;
}
// Do whatever you want to do with the certificate
// For instance, print its common name (if there's one)
CFStringRef commonName = NULL;
SecCertificateCopyCommonName(certificate, &commonName);
NSLog(@"common name = %@", (NSString *)commonName);
if (commonName) CFRelease(commonName);
SecKeychainItemFreeContent(&attrList, certData.Data);
CFRelease(searchItem);
}
CFRelease(search);
}
13
如果目的MAC OS 10.6或更高版本,可以使用SecItemCopyMatching
轻松查询钥匙扣:
SecKeychainRef keychain = ...
NSDictionary *query = [NSDictionary dictionaryWithObjectsAndKeys:
kSecClassCertificate, kSecClass,
[NSArray arrayWithObject:(id)keychain], kSecMatchSearchList,
kCFBooleanTrue, kSecReturnRef,
kSecMatchLimitAll, kSecMatchLimit,
nil];
NSArray *items = nil;
OSStatus status = SecItemCopyMatching((CFDictionaryRef)query, (CFTypeRef *)&items);
if (status) {
if (status != errSecItemNotFound)
LKKCReportError(status, @"Can't search keychain");
return nil;
}
return [items autorelease]; // items contains all SecCertificateRefs in keychain
请注意,你不能使用这个实现证书验证 - 在钥匙链CA证书的存在并不表示它是信任签署证书的任何特定的政策。请参阅证书,密钥和信任编程指南以了解如何使用钥匙串进行证书验证。
+0
这是一个很好的答案,因为'SecKeychainSearchCreateFromAttributes'和'SecKeychainSearchCopyNext'从10.7开始被弃用。这确实需要一些疯狂的强制转换才能使用ARC,但使用'CFMutableDictionaryRef'并设置这些键也同样适用。谢谢! – 2013-02-09 07:14:43
相关问题
- 1. 钥匙串证书
- 2. 停止Chrome从Mac中的钥匙串中获取SSL证书
- 3. 插入证书导入到钥匙串
- 4. iPhone配置:证书不安装钥匙钥匙
- 5. 的APN证书定制钥匙扣
- 6. 获取钥匙串项目的属性
- 7. 将证书链存储在应用程序钥匙串中
- 8. 在钥匙串中安装证书失败 - Xcode,Mac OSX
- 9. 在钥匙链上安装证书
- 10. Javascript - 钥匙/证书来自USB令牌
- 11. 钥匙串访问中没有钥匙
- 12. Apple Xcode 4.3.2默认钥匙串中没有有效的证书/私钥对
- 13. 将客户端证书导入到iPhone的钥匙串
- 14. 使用CFDataRef将证书保存到钥匙串
- 15. 如何将证书添加到钥匙串访问?
- 16. 在钥匙串上安装WWDR Apple证书时出现问题
- 17. CSR请求钥匙串iOS分发证书
- 18. 当证书被添加到钥匙串时出现SSL错误
- 19. 如何将安全标识(证书+私钥)添加到iPhone钥匙串?
- 20. GIDSignIn验证钥匙串未保存
- 21. 如何在我的Mac钥匙串中使用证书签署Java小程序?
- 22. 一次性获取钥匙串中的所有物品?
- 23. 从BouncyCastle X509证书获取私钥? C#
- 24. PHP:从证书获取公钥ID。
- 25. 从证书获取Sha256公钥
- 26. 钥匙串问题
- 27. 获取异常,同时试图从证书中获取私钥
- 28. iOS:在钥匙串中预先安装SSL证书 - 以编程方式
- 29. SecTrustEvaluate()是否在应用程序钥匙串中查找根证书?
- 30. 在MacOS上使用swift 3在登录钥匙串中安装证书
绝对的辉煌。非常感谢! – 2011-04-24 16:05:20
@Dylan干杯。考虑向苹果提交一个改进请求雷达(实际上是三个):这可能是示例代码,这可能在安全编程指南中,它们可以为此提供更简单的基于Cocoa的API。 – 2011-04-24 22:45:38
'SecKeychainSearchCopyNext'返回的'searchItem'已经是'SecCertificateRef'。 (您正在查找证书类中的项目,因此每个结果都是一个证书项目。)无需提取并重新解释其数据:简单的类型转换将获得更好的结果。 – 2011-10-21 18:50:17