3
我試圖按照蘋果文檔的處理客戶端P12的證書在此:在鑰匙串中存儲一個的.p12證書使用後
我已成功加載從文件系統中的.p12證書:
- (SecIdentityRef)getClientCertificate:(NSString *) certificatePath {
SecIdentityRef identity = nil;
NSData *PKCS12Data = [NSData dataWithContentsOfFile:certificatePath];
CFDataRef inPKCS12Data = (__bridge CFDataRef)PKCS12Data;
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);
OSStatus securityError = SecPKCS12Import(inPKCS12Data, options, &items);
CFRelease(options);
CFRelease(password);
if (securityError == errSecSuccess) {
NSLog(@"Success opening p12 certificate. Items: %ld", CFArrayGetCount(items));
CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0);
identity = (SecIdentityRef) CFDictionaryGetValue(identityDict, kSecImportItemIdentity);
} else {
NSLog(@"Error opening Certificate.");
}
return identity;
}
然後我得到該身份證書:
- (CFArrayRef)getCertificate:(SecIdentityRef) identity {
SecCertificateRef certificate = nil;
SecIdentityCopyCertificate(identity, &certificate);
SecCertificateRef certs[1] = { certificate };
CFArrayRef array = CFArrayCreate(NULL, (const void **) certs, 1, NULL);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecTrustRef myTrust;
OSStatus status = SecTrustCreateWithCertificates(array, myPolicy, &myTrust);
if (status == noErr) {
NSLog(@"No Err creating certificate");
} else {
NSLog(@"Possible Err Creating certificate");
}
return array;
}
但我真正想做的是將證書(或身份)存儲在我的應用程序鑰匙串中,所以我沒有從文件系統讀取它。
幾個問題:
- 我應該來存儲?證書或身份?
- 如何存儲並檢索它?
上面的鏈接是關於'獲取和使用持久性鑰匙串引用',這對我來說非常混亂。
它還談論'在鑰匙串中查找證書',但它提到使用證書的名稱來查找它。我不確定'姓名'的來源。
謝謝!我一直試圖保存/檢索持久的引用/從NSUserDefaults沒有運氣。想知道如果我可以寫到NSUserDefaults,如果是的話,如果我做得正確。 – lostintranslation
我會建議不要在NSUserDefaults中存儲該引用,它只是不適合它的地方,並且可能很挑剔。只需使用[-writeToFile:atomically:](https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSData_Class/index.html#//apple_ref/occ/instm/NSData/writeToFile:原子:)方法 – RyanR