gpt4 book ai didi

objective-c - 是否可以在 iPhone 应用程序中加载带有自签名安全证书的 SSL 加密网站?

转载 作者:太空宇宙 更新时间:2023-11-03 13:13:18 26 4
gpt4 key购买 nike

这个问题似乎到处都是,但到目前为止我还没有找到明确的答案。

我的任务是为我的公司编写一个 iPhone 应用程序。我的公司有一个带有自签名证书的 SSL 加密 网站。该网站使用频率很高,我的雇主希望可以通过 iPhone 轻松访问该网站。我的应用程序的功能是提供一种存储用于访问站点的凭据的方法,当用户打开应用程序时,它会自动将存储的凭据发送到站点并跳过登录对话框直接进入站点。

我已将我的应用程序设置为使用 UIWebView 并加载站点请求。我已经设置了常用的身份验证方法,例如:

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {...}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge {...}

当我的应用程序到达我告诉 UIWebView 加载 NSURLRequest 的代码部分时,它没有遇到我的任何身份验证方法。它直接跳转到 didFailLoadWithError,错误告诉我我正在连接到一个可能只是假装是我的网站的网站,因为它有一个自签名证书。

我该如何解决这个问题?我找到了一些涉及 NSURLConnection 方法的答案,但在我遇到错误并被迫停止加载页面之前,这些方法似乎没有被调用。我还找到了一些涉及覆盖“未记录”方法的答案,但是当我尝试实现该解决方案时,我的 Web View 从未到达“didFailLoadWithError”或“didFinishLoad”并且从不显示任何内容。

最佳答案

UIWebKit 委托(delegate)不会通过任何 NSURLConnection 委托(delegate)方法转发到您的应用程序。解决此问题的一种方法是使用 NSURLConnection 加载页面,然后使用 -loadData:MIMEType:textEncodingName:baseURL: 将其推送到 UIWebView。完成后,您已经验证了第一页,该页(只要您的站点没有链接)应该保持安全。那么,我们如何验证自签名证书呢?

我不得不在今年早些时候使用 OSX 应用程序解决这个问题,一旦我弄清楚我在做什么,假设您有类似的设置,它就非常简单。我在这里提出的解决方案实际上验证了服务器证书(虽然在我的例子中我使用的是私有(private) CA,所以我将 CA 证书添加到信任根,而不是服务器证书,它应该与它一样工作)。

您需要为 NSURLAuthenticationMethodServerTrust 添加测试到 -connection:canAuthenticateAgainstProtectionSpace:-connection:didReceiveAuthenticationChallenge: 方法,以便您既可以请求对安全挑战的兴趣,也可以处理安全挑战。

希望这对您有所帮助。

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
// ... implement any other authentication here, such as your client side certificates or user name/password

if ([[protectionSpace authenticationMethod] isEqualToString: NSURLAuthenticationMethodServerTrust])
return YES;
return NO;
}

-(void)connection:(NSURLConnection *)aConnection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
NSURLProtectionSpace *protectionSpace = challenge.protectionSpace;

// implement your client side auth here for NSURLAuthenticationMethodHTTPDigest or basic or your client-side cert

if ([[protectionSpace authenticationMethod] isEqualToString: NSURLAuthenticationMethodServerTrust]) {
// install our private certificates or CAs
NSString *myCAString = @"<string containing your CA cert>";

SecCertificateRef certRef = SecCertificateCreateWithData ( NULL, (CFDataRef)[NSData dataFromBase64String: myCAString]);
NSArray *anchorArray = [NSArray arrayWithObject: (NSObject*)certRef];
SecTrustSetAnchorCertificates( challenge.protectionSpace.serverTrust, (CFArrayRef) anchorArray);

SecTrustResultType trustResultType;
OSStatus err=SecTrustEvaluate(challenge.protectionSpace.serverTrust, &trustResultType);

if ((!err) && (trustResultType == kSecTrustResultProceed || trustResultType == kSecTrustResultConfirm || trustResultType == kSecTrustResultUnspecified)) {
[challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
} else {
CFArrayRef certChain=NULL;
CSSM_TP_APPLE_EVIDENCE_INFO *statusChain;
SecTrustGetResult( challenge.protectionSpace.serverTrust, &trustResultType, &certChain, &statusChain);
[challenge.sender cancelAuthenticationChallenge:challenge];
}

} else {
// Cancel if we don't know what it is about... this is supposed to be secure
[challenge.sender cancelAuthenticationChallenge:challenge];
}
}

关于objective-c - 是否可以在 iPhone 应用程序中加载带有自签名安全证书的 SSL 加密网站?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9539733/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com