0

iOSアプリケーションを開発していて、.p12証明書からIDを抽出するのに苦労しています。私はまだobjective-cに慣れていないので、何か大きなものが欠けていると確信しています。コードは次のとおりです。

@implementation P12Extractor

-(SecIdentityRef)getIdentity{
NSString *path = [[NSBundle mainBundle] pathForResource:@"ServerCert" ofType:@"p12"];
NSData *p12data = [NSData dataWithContentsOfFile:path];
CFDataRef inP12Data = (__bridge CFDataRef)p12data;
SecIdentityRef myIdentity;

OSStatus status = extractIdentity(inP12Data, &myIdentity);

if (status != 0) {
    NSLog(@"%@",status);
}
return myIdentity;

}

OSStatus extractIdentity(CFDataRef inP12Data, SecIdentityRef *identity){
OSStatus securityError = errSecSuccess;

CFStringRef password = CFSTR("password");
const void *keys[] = { kSecImportExportPassphrase };
const void *values[] = { password };

CFDictionaryRef options = CFDictionaryCreate(NULL, keys, values, 1, NULL, NULL);

CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
securityError = SecPKCS12Import(inP12Data, options, &items);

if (securityError == 0) {
    CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0); // <<<at this point i get an EXC_BAD_ACCESS(code=2,adress=0x0) error
    const void *tempIdentity = NULL;
    tempIdentity = CFDictionaryGetValue(ident, kSecImportItemIdentity);
    *identity = (SecIdentityRef)tempIdentity;
}

if (options) {
    CFRelease(options);
}

return securityError;
}

@end

私はコメントでエラーのポイントをマークしました、私は本当にこれを取得し続ける理由がわかりません。このコードは、Apple開発サイトから承認されたソリューションである必要があります。

4

1 に答える 1

0

最初の要素を取得しようとすると、配列はまだ空です...

 CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL);
 securityError = SecPKCS12Import(inP12Data, options, &items);

 if (securityError == 0) {
   CFDictionaryRef ident = CFArrayGetValueAtIndex(items,0); // <<<at this point i get an   EXC_BAD_ACCESS(code=2,adress=0x0) error

問題は、証明書または渡したオプションにある可能性があると思います。

于 2012-10-30T18:40:38.207 に答える