xamarin / Xamarin.Forms

Xamarin.Forms is no longer supported. Migrate your apps to .NET MAUI.
https://aka.ms/xamarin-upgrade
Other
5.63k stars 1.87k forks source link

[Bug] Xamarin Forms iOS doesn't work with TLS 1.3 #15610

Open muffadal53 opened 1 year ago

muffadal53 commented 1 year ago

Description

In my Xamarin forms iOS project I have set "HttpClient Implementation" as "NSUrlSession (iOS 7+)" and in code we create HttpClient as below:

HttpClient httpClient = new HttpClient(new HttpClientHandler() { AllowAutoRedirect = true, MaxAutomaticRedirections = 100, AutomaticDecompression = DecompressionMethods.GZip })

But this doesn't work with TLS 1.3 server in the blog here it has been mentioned that "NSUrlSession (iOS 7+) supports TLS 1.2+. Below is the link to the blog:

https://devblogs.microsoft.com/xamarin/securing-network-traffic-with-tls-1-2/

The exception I am getting is as mentioned below:

{System.Net.Http.HttpRequestException: The SSL connection could not be established, see inner exception. ---> System.Security.Authentication.AuthenticationException: Authentication failed, see inner exception. ---> Mono.Security.Interface.TlsException: Unknown Secure Transport error `PeerProtocolVersion'.

So, please let me know if TLS 1.3 is supported in Xamarin forms iOS and if yes then how do I fix this issue.

Steps to Reproduce

  1. Configure server with TLS 1.3 support along with respective Ciphers.
  2. Set "NSUrlSession (iOS 7+)" in Xamarin iOS project settings and create HttpClient using "HttpClientHandler" as mentioned above.
  3. Now try to open any connection with server and we see the connection exception.

Expected Behavior

The application should work without any error with TLS 1.3.

Actual Behavior

The application doesn't work with TLS 1.3 server.

Basic Information

Environment

Show/Hide Visual Studio info ``` Microsoft Visual Studio Professional 2022 Version 17.3.6 VisualStudio.17.Release/17.3.6+32929.385 Microsoft .NET Framework Version 4.8.04084 Installed Version: Professional ADL Tools Service Provider 1.0 This package contains services used by Data Lake tools ASA Service Provider 1.0 ASP.NET and Web Tools 17.3.376.3011 ASP.NET and Web Tools Azure App Service Tools v3.0.0 17.3.376.3011 Azure App Service Tools v3.0.0 Azure Data Lake Tools for Visual Studio 2.6.5000.0 Microsoft Azure Data Lake Tools for Visual Studio Azure Functions and Web Jobs Tools 17.3.376.3011 Azure Functions and Web Jobs Tools Azure Stream Analytics Tools for Visual Studio 2.6.5000.0 Microsoft Azure Stream Analytics Tools for Visual Studio C# Tools 4.3.0-3.22470.13+80a8ce8d5fdb9ceda4101e2acb8e8eb7be4ebcea C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Common Azure Tools 1.10 Provides common services for use by Azure Mobile Services and Microsoft Azure Tools. Cookiecutter 17.0.22179.3 Provides tools for finding, instantiating and customizing templates in cookiecutter format. Extensibility Message Bus 1.2.6 (master@34d6af2) Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration. Microsoft Azure Hive Query Language Service 2.6.5000.0 Language service for Hive query Microsoft Azure Stream Analytics Language Service 2.6.5000.0 Language service for Azure Stream Analytics Microsoft Azure Tools for Visual Studio 2.9 Support for Azure Cloud Services projects Microsoft JVM Debugger 1.0 Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines Mono Debugging for Visual Studio 17.3.20 (3f4cb00) Support for debugging Mono processes with Visual Studio. Node.js Tools 1.5.40629.1 Commit Hash:3f5cc0329815af3ffb948f08857446d206a9af36 Adds support for developing and debugging Node.js apps in Visual Studio NuGet Package Manager 6.3.0 NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/ Python - Profiling support 17.0.22179.3 Profiling support for Python projects. Python with Pylance 17.0.22179.3 Provides IntelliSense, projects, templates, debugging, interactive windows, and other support for Python developers. Razor (ASP.NET Core) 17.0.0.2232702+e1d654e792aa2fe6646a6935bcca80ff0aff4387 Provides languages services for ASP.NET Core Razor. SQL Server Data Tools 17.0.62207.04100 Microsoft SQL Server Data Tools ToolWindowHostedEditor 1.0 Hosting json editor into a tool window TypeScript Tools 17.0.10701.2001 TypeScript Tools for Microsoft Visual Studio Visual Basic Tools 4.3.0-3.22470.13+80a8ce8d5fdb9ceda4101e2acb8e8eb7be4ebcea Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used. Visual F# Tools 17.1.0-beta.22363.4+1b94f89d4d1f41f20f9be73c76f4b229d4e49078 Microsoft Visual F# Tools Visual Studio IntelliCode 2.2 AI-assisted development for Visual Studio. VisualStudio.DeviceLog 1.0 Information about my package VisualStudio.Mac 1.0 Mac Extension for Visual Studio VSPackage Extension 1.0 VSPackage Visual Studio Extension Detailed Info Xamarin 17.3.0.308 (d17-3@56b9f7e) Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android. Xamarin Designer 17.3.0.217 (remotes/origin/d17-3@b81db1dcc0) Visual Studio extension to enable Xamarin Designer tools in Visual Studio. Xamarin Templates 17.2.28 (9b3d244) Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms. Xamarin.Android SDK 13.0.0.0 (d17-3/030cd63) Xamarin.Android Reference Assemblies and MSBuild support. Mono: dffa5ab Java.Interop: xamarin/java.interop/d17-3@7716ae53 SQLite: xamarin/sqlite/3.38.5@df4deab Xamarin.Android Tools: xamarin/xamarin-android-tools/main@14076a6 Xamarin.iOS and Xamarin.Mac SDK 16.0.0.75 (e4f48bed7) Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support. ```
AlleSchonWeg commented 1 year ago

Hi, i had similar problems with TLS 1.3 and compression on Android and iOS. Here is my implementation:

    HttpClient result = null;
            if(Device.RuntimePlatform == Device.Android) {
                try {
                    var handler = DependencyService.Get<HttpClientHandler>();
                    if(handler != null) {
                        handler.AutomaticDecompression = DecompressionMethods.GZip;
                        result = new HttpClient(handler);
                    }
                    else
                        result = new HttpClient();
                }
                catch {
                    if(result == null) {
                        result = new HttpClient();
                    }
                }
            }
            else {
                result = new HttpClient();
                result.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            }

Perhaps it helps you.