6
我試圖接受NSURLConnection中的自簽名證書,就像我之前的許多證書一樣。問題在於,我只想接受來自我信任的證書白名單的證書。我會解決如何接受一個單一的證書。下面是我在我的NSURLConnectionDelegate這麼遠代碼:如何接受iOS中的白名單中的自簽名服務器證書?
- (void)connection:(NSURLConnection *)connection willSendRequestForAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {
if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
NSString *thePath = [[NSBundle mainBundle] pathForResource:@"trusted" ofType:@"der"];
NSData *certData = [[NSData alloc] initWithContentsOfFile:thePath];
CFDataRef myCertData = (__bridge_retained CFDataRef)certData;
SecCertificateRef myCert = SecCertificateCreateWithData(NULL, myCertData);
SecPolicyRef myPolicy = SecPolicyCreateBasicX509();
SecCertificateRef certArray[1] = { myCert };
CFArrayRef myCerts = CFArrayCreate(NULL, (void *)certArray, 1, NULL);
SecTrustRef myTrust;
OSStatus status = SecTrustCreateWithCertificates(myCerts, myPolicy, &myTrust);
SecTrustResultType trustResult;
if (status == noErr) {
status = SecTrustEvaluate(myTrust, &trustResult);
}
BOOL trusted = NO;
if (trustResult == kSecTrustResultUnspecified) {
// I never get here. Instead, trustResult is always kSecTrustResultRecoverableTrustFailure
trusted = YES;
}
if (trusted) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]
forAuthenticationChallenge:challenge];
} else {
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
CFRelease(myTrust);
CFRelease(myCerts);
CFRelease(myPolicy);
CFRelease(myCert);
CFRelease(myCertData);
} else {
[challenge.sender performDefaultHandlingForAuthenticationChallenge:challenge];
}
}
正如你可以在註釋中看到的,我從來沒有真正得到kSecTrustResultUnspecified,這是我希望得到的。我驗證了我的證書是正確的,並且格式正確(DER)。
如何獲得'trusted.der'文件? – Hlung
您可以在線查看教程以創建教程,然後將其與您的應用程序一起打包。 Google應該可以幫助其他人。 –
我只是好奇,可能有一種方式,像谷歌瀏覽器,只是在應用程序本身自動生成。謝謝btw。 – Hlung