Ios NSURLConnection:使用设备上安装的所有CA证书
我想在自定义iOS 6应用程序中使用HTTPS访问一些web资源。某些目标服务器正在使用由CA签名的证书,该CA默认情况下不包括在iOS中,但已手动添加到设备的密钥链中。因此,可以在Safari中打开所有URL,而不会出现任何警告或错误 我想实现的是与Safari相同的行为:如果Safari信任这些网站,我想加载它们,或者在出现任何错误时拒绝加载它们。由于安装的证书可能会因情况而异,因此我不想在应用程序资源中手动包含任何证书,这就是SO中的许多问题 我的问题是,我没有获取Ios NSURLConnection:使用设备上安装的所有CA证书,ios,ssl,ios6,Ios,Ssl,Ios6,我想在自定义iOS 6应用程序中使用HTTPS访问一些web资源。某些目标服务器正在使用由CA签名的证书,该CA默认情况下不包括在iOS中,但已手动添加到设备的密钥链中。因此,可以在Safari中打开所有URL,而不会出现任何警告或错误 我想实现的是与Safari相同的行为:如果Safari信任这些网站,我想加载它们,或者在出现任何错误时拒绝加载它们。由于安装的证书可能会因情况而异,因此我不想在应用程序资源中手动包含任何证书,这就是SO中的许多问题 我的问题是,我没有获取SecTrustEval
SecTrustEvaluate
以返回kSecTrustResultProceed。你知道我能做什么吗
如果myCanAuthenticationAgainstProtectionSpace
返回否,则iOS会自行处理服务器证书检查,但它似乎不会检查额外安装的证书(就像Safari那样)
以下是一些代码,以尝试了解我目前得到的信息:
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadURLWithString:@"https://myserver.com"];
}
+ (BOOL) isChallenge: (NSURLAuthenticationChallenge*) challenge validForConnection: (NSURLConnection*)conn{
SecTrustRef serverTrust=[[challenge protectionSpace] serverTrust];
//Some magic here?
// Check Server Certificate
SecTrustResultType evalResult;
if(SecTrustEvaluate(serverTrust,&evalResult) != errSecSuccess){
NSLog(@"Call to SecTrustEvaluate failed");
return NO;
}
if(evalResult != kSecTrustResultProceed){
NSLog(@"Server certificate invalid");
return NO;
}
NSLog(@"Server certificate valid");
return YES;
}
- (void)loadURLWithString: (NSString*)str{
NSURLConnection *conn =
[NSURLConnection connectionWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]] delegate:self];
[conn start];
}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
if([[self class] isChallenge:challenge validForConnection:connection])
[challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
else
[challenge.sender cancelAuthenticationChallenge:challenge];
}
- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace{
return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"Failed with error: %@",error);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSLog(@"loading complete");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
}
你试图做的是不允许的。有关更多信息,请参阅Apple开发者论坛中的以下帖子: