TLS를 사용하여 웹 서버와 통신하는 iOS 응용 프로그램을 만들고 있습니다. 웹 서버에 액세스하면 내 장치에 인증서가 표시되고 신뢰할 수 있음을 확인하고 싶습니다.수동으로 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];
}
}
인증서가 결국 만료되어 재생성되어 더 이상 기대되는 해시와 일치하지 않기 때문에 정상적으로 작동하지 않습니다. 신뢰 객체에서 공개 키를 가져 와서 예상 된 키와 비교하는 것이 좋습니다. 그렇게하면 관리자가 cert를 재생성 한 후에도 서버를 계속 신뢰하게됩니다 (관리자가 키를 변경하지 않는다고 가정 할 때). – dgatwood
링크를 제공해 주셔서 감사합니다! 나는 모양을 볼 것이다 –
이 링크가 작동 했습니까? – Harsh