c# - Xamarin 支持 Apple TLS(不是 Mono TLS)的 SNI?

标签 c# ios xamarin xamarin.ios tls1.2

通过 Xamarin.iOS 应用程序调用我们的内部 api 服务器时,我们遇到了问题。

我们的反向代理服务器总是返回 403 HTTP 错误。服务器的日志显示:“没有通过 SNI 为基于名称的虚拟主机提供主机名”。

使用 Mono TLS:没问题。但是对于 Apple TLS,它不起作用。我们已经测试了所有 HTTP 客户端实现:托管代码、NSUrlSession 和 CFNetwork,结果相同。

我知道 Apple TLS 支持 SNI。在 iPhone 上使用 Safari 时,我们的网络服务器可以毫无问题地提供内容。

但是,对于 Xamarin,特别是对于 Apple TLS,这是不可能的。

你有什么想法吗?

PS:我们绕过证书验证

ServicePointManager.ServerCertificateValidationCallback 
   += (sender, certificate, chain, sslPolicyErrors) => {return true;};

最佳答案

使用 NSUrlSession (适用于 SNI):

webView = new WKWebView(new CGRect(40, 100, 400, 400), new WKWebViewConfiguration());
Add(webView);
button = new UIButton(UIButtonType.System);
button.Frame = new CGRect(40, 40, 100, 40);
button.SetTitle("Fetch", UIControlState.Normal);
Add(button);
button.TouchUpInside += async (object sender, EventArgs e) =>
{
    var url = new NSUrl("https://sni.velox.ch");
    var task = await NSUrlSession.SharedSession.CreateDataTaskAsync(url);
    webView.LoadHtmlString(NSString.FromData(task.Data, NSStringEncoding.UTF8), new NSUrl(""));
};

或使用 bob.sni.velox.chalice.sni.velox.ch
TLS SNI Test Site: *.sni.velox.ch
Great! Your client [TLSv12wSNI/1.0 CFNetwork/808.0.1 Darwin/15.6.0]
sent the following TLS server name indication extension (RFC 6066) 
in its ClientHello (negotiated protocol: TLSv1.2, cipher suite: 
ECDHE-RSA-AES256-GCM-SHA384):

  sni.velox.ch

In your request, this header was included:
  Host: sni.velox.ch

使用 HttpClient:
button.TouchUpInside += async (object sender, EventArgs e) =>
{
    ServicePointManager.ServerCertificateValidationCallback += (servicesender, certificate, chain, sslPolicyErrors) => { return true; };
    var client = new HttpClient();
    var response = await client.GetAsync("https://sni.velox.ch");
    webView.LoadHtmlString(new NSString(await response.Content.ReadAsStringAsync()), new NSUrl(""));
};

使用 SSL/TLS 实现设置为 Mono TLS (适用于 SNI):
TLS SNI Test Site: *.sni.velox.ch

Great! Your client [TLSv12wSNI/1.0 CFNetwork/808.0.1 Darwin/15.6.0]
sent the following TLS server name indication extension (RFC 6066) in
its ClientHello (negotiated protocol: TLSv1.2, cipher suite: 
ECDHE-RSA-AES256-GCM-SHA384):

  sni.velox.ch

In your request, this header was included:

  Host: sni.velox.ch

使用 SSL/TLS 实现设置为 Apple TLS (default) : ( 错误 )

错误:your client did not send a TLS server name indication extension
TLS SNI Test Site: alice.sni.velox.ch

Unfortunately, your client did not send a TLS server name indication
extension (RFC 4366) in its ClientHello (negotiated protocol: TLSv1.2,
cipher suite: ECDHE-RSA-AES256-GCM-SHA384), so you're probably 
getting warnings about certificate name mismatches.
In your request, this header was included:

  Host: sni.velox.ch

笔记:

我们个人避免HttpClient在 iOS 上并始终使用 iOS“ native ”网络类/API:NSUrlSession , NSURLConnection , ....

提交的错误:https://bugzilla.xamarin.com/show_bug.cgi?id=43794

关于c# - Xamarin 支持 Apple TLS(不是 Mono TLS)的 SNI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39168279/

相关文章:

C# 和 SQL 服务器 : deadlock in parallel transactions

c# - 如何从 DataAnnotations 获取 StringLength

iOS 7 导航栏隐藏内容

iphone - iPhone 上运行 App 的声音太小

xamarin - 显示操作表中的按钮事件

c# - jQUery ajax 调用 asp.net webforms 返回 html 页面而不是调用 url 中的指定方法

c# - 如何使用 interop.word 打开 Word 文件

ios - 在 iOS 模拟器上记录和回放用户操作?

xamarin - 使用隐藏在 Xamarin.Forms 中的 C# 代码访问控件

xamarin - 如何从 TFS 连接 Visual Studio 应用程序中心(本地)