NSURLCREDENITIALSTORAGEおよびクライアント証明書認証
-
09-10-2019 - |
質問
mpmoviewplayercontrollerのドキュメントで私が見たものから、nsurlconredentialstorageはnsurlconnection認証の課題の代替として設定できます(これは、URLからのリソースをロードするが、NSURLCONNECTIONを抽象化する高レベルのクラスに役立ちます。課題)。これは、ブラウザに証明書をインストールし、特定のアドレスが要求するときに必要な証明書を自動化することに似ているようです。
これをテストするために、自分の証明書権、サーバー証明書、クライアント証明書(.p12ファイル)を設定しました。 HTTPSサーバーを設定し、別のマシンからブラウザを使用してクライアント証明書を正常にテストしました。
これで、iPhoneアプリに証明書を統合する必要があります。
私はP12ファイルをバンドルしました、そして、アプリケーションの起動時に私はこれを行います:
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"clientside" ofType:@"p12"];
NSData *PKCS12Data = [[NSData alloc] initWithContentsOfFile:thePath];
CFDataRef inPKCS12Data = (CFDataRef)PKCS12Data;
SecIdentityRef identity;
SecTrustRef trust;
extractIdentityAndTrust(inPKCS12Data, &identity, &trust);
SecCertificateRef certificate = NULL;
SecIdentityCopyCertificate (identity, &certificate);
const void *certs[] = {certificate};
CFArrayRef certArray = CFArrayCreate(kCFAllocatorDefault, certs, 1, NULL);
NSURLCredential *credential = [NSURLCredential credentialWithIdentity:identity certificates:(NSArray*)certArray persistence:NSURLCredentialPersistencePermanent];
NSURLProtectionSpace *protectionSpace = [[NSURLProtectionSpace alloc]
initWithHost: @"myhostname"
port: 443
protocol: @"https"
realm: nil
authenticationMethod: NSURLAuthenticationMethodClientCertificate];
[[NSURLCredentialStorage sharedCredentialStorage]
setDefaultCredential: credential
forProtectionSpace: protectionSpace];
ExtractidentityAndTrust関数は、iOSのドキュメントページの「証明書、キー、およびトラストサービスタスク」の形式でコピーされます(証明書パスワードを自分のパスワードに置き換えました)。
アイデンティティと証明書がうまくロードされていることを伝えることができます。コンソールにnsurlcredentialオブジェクトを印刷すると、次のようなものが得られます。 <NSURLCredential: 0x140ea0>: (null)
(これが良い兆候かどうかはわかりません)。
ただし、SetDefaultCreDentialを実行すると、アプリは多くの情報を提供せずにクラッシュします。
Stacktraceは次のようになります:
#0 0x350a41c8 in CFHash
#1 0x3515180c in __CFBasicHashStandardHashKey
#2 0x351534e4 in ___CFBasicHashFindBucket_Linear
#3 0x350a4000 in CFBasicHashFindBucket
#4 0x350a3ecc in CFDictionaryGetValue
#5 0x344a0df2 in URLCredentialStorage::setDefaultCredentialForProtectionSpace
#6 0x3446a5aa in CFURLCredentialStorageSetDefaultCredentialForProtectionSpace
#7 0x339ec79e in -[NSURLCredentialStorage setDefaultCredential:forProtectionSpace:]
#8 0x00002612 in -[AVPlayerExampleViewController awakeFromNib] at AVPlayerExampleViewController.m:84
資格情報に設定されていないものがあるように見えます。
これを解決する方法についてのアイデアはありますか?
編集:
テスト用に、メールからファイルを開くことにより、iPhoneに証明書をインストールしました。 Safariを介してHTTPSを介してページにアクセスすることはできますが、アプリではアクセスできません。
機能する唯一のことは、保護されたドメイン上の任意のリソースへのnsurlConnectionを開くと、DideReveiveAuthenticationChallenge(上記のコードとまったく同じように資格情報をロードする場合、NsurlauthenticationChallengeの送信者に直接送信してからアクセスすることです。より高いレベルのクラスを使用する必要があります。
解決
nsurlcredentialStorageがユーザー名とパスワードnsurlcredentials(証明書はサポートされていない)でのみ機能することは既知の問題のようです。残念ながら、ドキュメントはこれについて何も言及していません。