我有一个certificate
p12-format
,希望从中获取一些信息。
我有以下两个功能
-(OSStatus)extractIdentityAndTrust: (CFDataRef) inPKCS12Data withIdentity:(SecIdentityRef *) outIdentity withTrust:(SecTrustRef *) outTrust withPassword:(CFStringRef) keyPassword
{
OSStatus securityError = errSecSuccess;
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { keyPassword };
CFDictionaryRef optionsDictionary = NULL;
optionsDictionary = CFDictionaryCreate(NULL, keys, values, (keyPassword ? 1 : 0), NULL, NULL);
CFArrayRef items = NULL;
securityError = SecPKCS12Import(inPKCS12Data, optionsDictionary, &items);
if(securityError == 0)
{
CFDictionaryRef myIdentityAndTrust = CFArrayGetValueAtIndex (items, 0);
const void *tempIdentity = NULL;
tempIdentity = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemIdentity);
CFRetain(tempIdentity);
*outIdentity = (SecIdentityRef)tempIdentity;
const void *tempTrust = NULL;
tempTrust = CFDictionaryGetValue (myIdentityAndTrust, kSecImportItemTrust);
CFRetain(tempTrust);
*outTrust = (SecTrustRef)tempTrust;
}
if(optionsDictionary) CFRelease(optionsDictionary);
if(items) CFRelease(items);
return securityError;
}
和
-(NSString *)copySummaryString:(SecIdentityRef *) identity
{
// Get the certificate from the identity.
SecCertificateRef myReturnedCertificate = NULL;
OSStatus status = SecIdentityCopyCertificate (*identity, &myReturnedCertificate);
if(status)
{
NSLog(@"SecIdentityCopyCertificate failed.\n");
return NULL;
}
CFStringRef certSummary = SecCertificateCopySubjectSummary(myReturnedCertificate);
NSString* summaryString = [[NSString alloc] initWithString:(__bridge NSString *)certSummary];
CFRelease(certSummary);
return summaryString;
}
我在viewDidLoad
中的以下几行中将这两种方法称为
SecIdentityRef identity = nil;
SecTrustRef trust = nil;
NSData *certPath = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"certificate" ofType:@"p12"]];
CFDataRef certData = (__bridge_retained CFDataRef)(certPath);
[self extractIdentityAndTrust:certData withIdentity:&identity withTrust:&trust withPassword:CFSTR("")];
NSString* summaryString = [self copySummaryString:&identity];
NSLog(@"%@", summaryString);
但是在我的函数copySummaryString
中,我在下一行得到了错误
OSStatus status = SecIdentityCopyCertificate (*identity, &myReturnedCertificate);
我没有找到任何好的例子。如何正确调用这些函数,为什么会出现此错误以及此错误意味着什么?
我还发现了https://stackoverflow.com/a/20913426/5629933,https://stackoverflow.com/a/19219932/5629933或Custom Certificate in iOS App等帖子。
我还在https://developer.apple.com/library/ios/documentation/Security/Conceptual/CertKeyTrustProgGuide/iPhone_Tasks/iPhone_Tasks.html#//apple_ref/doc/uid/TP40001358-CH208-SW13上阅读了Apple的文档,但它不是很有帮助。
答案 0 :(得分:0)
解决了我的问题:
问题出在我的viewDidLoad
。我没有设置密码。函数securityError
中的变量extractIdentityAndTrust
不是< t 0 0