2

TLS 検証用の特定の CA 証明書を iOS 6 アプリケーションのキーチェーンに追加したいと考えています。証明書はアプリケーション バンドルに含まれています。いくつかの例で説明されているID (秘密鍵/証明書の組み合わせ)を追加したくありません。

このSecPKCS12Import呼び出しはエラーを返しませんが、残念ながら証明書も返しません。

私の手順を再現できるように、例として Google 中間証明書 (「Google Internet Authority」) を取り上げ、ダウンロードした PEM 証明書に対して次のコマンドを実行しました。

ベビーベッド.

#convert PEM certificate to PKCS12
openssl pkcs12 -export -in google.pem -nokeys -out google.p12 -passout "pass:google"
#verification
openssl pkcs12 -in google.p12 -passin "pass:google"
MAC verified OK
Bag Attributes: <No Attributes>
subject=/C=US/O=Google Inc/CN=Google Internet Authority
issuer=/C=US/O=Equifax/OU=Equifax Secure Certificate Authority
-----BEGIN CERTIFICATE-----
MIICsDCCAhmgAwIBAgIDFXfhMA0GCSqGSIb3DQEBBQUAME4xCzAJBgNVBAYTAlVT
MRAwDgYDVQQKEwdFcXVpZmF4MS0wKwYDVQQLEyRFcXVpZmF4IFNlY3VyZSBDZXJ0
[...]
ARlIjNvrPq86fpVg0NOTawALkSqOUMl3MynBQO+spR7EHcRbADQ/JemfTEh2Ycfl
vZqhEFBfurZkX0eTANq98ZvVfpg=
-----END CERTIFICATE-----

その後、次のコードが実行されたアプリケーションにファイルをバンドルしました。

NSMutableDictionary * options = [[[NSMutableDictionary alloc] init] autorelease];
[options setObject:@"google" forKey:(id)kSecImportExportPassphrase];
CFArrayRef items = NULL;
NSData *certData = [NSData dataWithContentsOfFile:[NSBundle pathForResource:@"google" ofType:@"p12" inDirectory:[[NSBundle mainBundle] bundlePath]]];
OSStatus result = SecPKCS12Import((CFDataRef)certData, (CFDictionaryRef)options, &items);
assert(result == errSecSuccess);
CFIndex count = CFArrayGetCount(items);
NSLog(@"Certificates found: %ld",count);

コンソールの結果出力は「Certificates found: 0」です。certData変数には正しいバイト数が入力され、提供されたパスワードを変更すると、結果がerrSecAuthFailedに変わります。

何が問題なのか分かりますか?

4

1 に答える 1

4

これはバグだと思います。関連する質問SSL ID Certificate to run an HTTPS Server on iOSと bug SecPKCS12Import returns empty array when certificate expires after Jan 1st 10000を参照してください。

秘密鍵なしで証明書のみが必要な場合は、DER 形式のファイルから証明書をインポートします。

$ openssl x509 -in google.pem -out google.der -outform DER
$ openssl x509 -in google.der -noout -text

DER 証明書ファイルをバンドルしてインポートします。

NSString *path = [[NSBundle mainBundle] pathForResource:@"google" ofType:@"der"];
NSData *derData = [NSData dataWithContentsOfFile:path];
SecCertificateRef cert = SecCertificateCreateWithData(NULL, (CFDataRef)derData);

// add cert to KeyChain or use it as you need

CFRelease(cert);
于 2013-08-21T18:38:17.913 に答える