TLSを使用してWebサーバーと通信するiOSアプリケーションを作成しています。 Webサーバーにアクセスすると、デバイスに証明書が提示され、信頼できることを検証したいと考えています。TLS証明書を手動で検証する(ios objective-c)
xcodeプロジェクトにderファイルとしてルート証明書が組み込まれています。私は今までNSURLConnectionの認証チャレンジのデリゲート関数で両方の証明書のNSStringバージョンを取得しています。
手動で検証する方法はありますか?私はこの記事があなたの目的を解決するためだと思う
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
NSLog(@"Certificate challenge");
if (self.stageNum==0) {
id <NSURLAuthenticationChallengeSender> sender=challenge.sender;
NSURLProtectionSpace *protectionSpace=challenge.protectionSpace;
SecTrustRef trust=[protectionSpace serverTrust];
//Get server certificate
SecCertificateRef certificate=SecTrustGetCertificateAtIndex(trust, 0);
NSData *serverCertificateData=(__bridge NSData *)SecCertificateCopyData(certificate);
NSString *serverBase64Certificate=[serverCertificateData base64EncodedStringWithOptions:0];
//Get our certificate
NSData *certData1 = [[NSData alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"SUMOSRoot" ofType:@"der"]];
NSString *certBase64=[certData1 base64EncodedStringWithOptions:0];
//Heres where I need to compare the certificates
//
//
[sender useCredential:[NSURLCredential credentialForTrust:protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
}
}
証明書は最終的に期限切れになり再生されるため、期待されるハッシュとはもはや一致しないため、理想的ではありません。信頼オブジェクトから公開鍵を取得し、それを予想される鍵と比較する方が良いでしょう。そうすれば、管理者が証明書を再生成した後でも(管理者が鍵を変更しないと仮定しても)サーバーを信頼し続けるでしょう。 – dgatwood
リンクありがとう!私は見てみましょう –
このリンクは機能しましたか? – Harsh