XML RSA秘密鍵をPEMファイルに変換するという以前の問題は解決しましたが、P12秘密鍵をインポートするときにnullデータを取得するという別の問題が発生しました。以下は私の手順です。
PEMファイルをP12ファイルに変換します
openssl> pkcs12 -export -in rsa.pem -inkey rsa.pem -out rsa.p12 -nocerts
P12ファイルをiOSプロジェクトに読み込む
NSString *path = [[NSBundle bundleForClass:[self class]] pathForResource:@"MyPrivateKey" ofType:@"p12"]; NSData *p12data = [NSData dataWithContentsOfFile:path]; if (![self getPrivateKeyRef]) RSAPrivateKey = getPrivateKeywithRawKey(p12data);
P12秘密鍵をインポートする
SecKeyRef getPrivateKeywithRawKey(NSData *pfxkeydata) { NSMutableDictionary * options = [[[NSMutableDictionary alloc] init] autorelease]; // Set the public key query dictionary //change to your .pfx password here [options setObject:@"MyPassword" forKey:(id)kSecImportExportPassphrase]; CFArrayRef items = CFArrayCreate(NULL, 0, 0, NULL); OSStatus securityError = SecPKCS12Import((CFDataRef) pfxkeydata, (CFDictionaryRef)options, &items); CFDictionaryRef identityDict = CFArrayGetValueAtIndex(items, 0); SecIdentityRef identityApp = (SecIdentityRef)CFDictionaryGetValue(identityDict, kSecImportItemIdentity); //NSLog(@"%@", securityError); assert(securityError == noErr); SecKeyRef privateKeyRef; SecIdentityCopyPrivateKey(identityApp, &privateKeyRef); return privateKeyRef; }
err(OSStatus値は0)はないと思いましたが、items配列はIDデータを取得しませんでした。OpenSSlの使用法が間違っているために、正しいp12ファイル形式を取得できなかったのではないかと思います。誰かがp12ファイルを正常にインポートしましたか?私はこの問題に数日間立ち往生しています。手がかりがあればアドバイスをください、ありがとう!
ヒューバート