Skip to content

Commit 01c4028

Browse files
github-actions[bot]Mitchell Hwangakoeplinger
authored
[release/6.0] Add System.Net.Http ServerCertificateCustomValidationCallback ILLink Suppression (#58508)
Co-authored-by: Mitchell Hwang <[email protected]> Co-authored-by: Alexander Köplinger <[email protected]>
1 parent c7e5075 commit 01c4028

File tree

7 files changed

+52
-419
lines changed

7 files changed

+52
-419
lines changed

src/libraries/System.Net.Http/src/ILLink/ILLink.Suppressions.LibraryBuild.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,18 @@
141141
<property name="Target">M:System.Net.Http.HttpClientHandler.SetMaxAutomaticRedirections(System.Int32)</property>
142142
<property name="Justification">The Xamarin.iOS and Mono.Android libraries are not present when running the trimmer analysis during our build. A consuming application will get a warning if these libraries aren't present when trimming the full app.</property>
143143
</attribute>
144+
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
145+
<argument>ILLink</argument>
146+
<argument>IL2035</argument>
147+
<property name="Scope">member</property>
148+
<property name="Target">M:System.Net.Http.HttpClientHandler.GetServerCertificateCustomValidationCallback</property>
149+
</attribute>
150+
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
151+
<argument>ILLink</argument>
152+
<argument>IL2035</argument>
153+
<property name="Scope">member</property>
154+
<property name="Target">M:System.Net.Http.HttpClientHandler.SetServerCertificateCustomValidationCallback(System.Func{System.Net.Http.HttpRequestMessage,System.Security.Cryptography.X509Certificates.X509Certificate2,System.Security.Cryptography.X509Certificates.X509Chain,System.Net.Security.SslPolicyErrors,System.Boolean})</property>
155+
</attribute>
144156
<attribute fullname="System.Diagnostics.CodeAnalysis.UnconditionalSuppressMessageAttribute">
145157
<argument>ILLink</argument>
146158
<argument>IL2035</argument>

src/libraries/System.Net.Http/src/System.Net.Http.csproj

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
<PropertyGroup Condition="'$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsTVOS)' == 'true'">
1313
<DefineConstants>$(DefineConstants);TARGET_MOBILE</DefineConstants>
1414
</PropertyGroup>
15+
<PropertyGroup Condition="'$(TargetsAndroid)' == 'true'">
16+
<DefineConstants>$(DefineConstants);TARGET_ANDROID</DefineConstants>
17+
</PropertyGroup>
18+
<PropertyGroup Condition="'$(TargetsiOS)' == 'true'">
19+
<DefineConstants>$(DefineConstants);TARGET_IOS</DefineConstants>
20+
</PropertyGroup>
21+
<PropertyGroup Condition="'$(TargetsMacCatalyst)' == 'true'">
22+
<DefineConstants>$(DefineConstants);TARGET_MACCATALYST</DefineConstants>
23+
</PropertyGroup>
24+
<PropertyGroup Condition="'$(TargetsTVOS)' == 'true'">
25+
<DefineConstants>$(DefineConstants);TARGET_TVOS</DefineConstants>
26+
</PropertyGroup>
1527
<PropertyGroup Condition="'$(TargetsBrowser)' == 'true'">
1628
<DefineConstants>$(DefineConstants);TARGET_BROWSER</DefineConstants>
1729
</PropertyGroup>
@@ -41,10 +53,8 @@
4153
<Compile Include="System\Net\Http\HttpClient.cs" />
4254
<Compile Condition="'$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsTVOS)' == 'true'"
4355
Include="System\Net\Http\HttpClientHandler.AnyMobile.cs" />
44-
<Compile Condition="'$(TargetsiOS)' == 'true'" Include="System\Net\Http\HttpClientHandler.iOS.cs" />
45-
<Compile Condition="'$(TargetsMacCatalyst)' == 'true'" Include="System\Net\Http\HttpClientHandler.MacCatalyst.cs" />
46-
<Compile Condition="'$(TargetsTVOS)' == 'true'" Include="System\Net\Http\HttpClientHandler.tvOS.cs" />
47-
<Compile Condition="'$(TargetsAndroid)' == 'true'" Include="System\Net\Http\HttpClientHandler.Android.cs" />
56+
<Compile Condition="'$(TargetsAndroid)' == 'true' or '$(TargetsiOS)' == 'true' or '$(TargetsMacCatalyst)' == 'true' or '$(TargetsTVOS)' == 'true'"
57+
Include="System\Net\Http\HttpClientHandler.AnyMobile.InvokeNativeHandler.cs" />
4858
<Compile Condition="'$(TargetsAndroid)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetsMacCatalyst)' != 'true' and '$(TargetsTVOS)' != 'true'"
4959
Include="System\Net\Http\HttpClientHandler.cs" />
5060
<Compile Include="System\Net\Http\HttpCompletionOption.cs" />

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.Android.cs

Lines changed: 0 additions & 138 deletions
This file was deleted.

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.MacCatalyst.cs renamed to src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.InvokeNativeHandler.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,25 @@ public partial class HttpClientHandler : HttpMessageHandler
1515
{
1616
private static MethodInfo? _nativeHandlerMethod;
1717

18+
#if TARGET_ANDROID
19+
private const string NativeHandlerType = "Xamarin.Android.Net.AndroidMessageHandler";
20+
private const string AssemblyName = "Mono.Android";
21+
private const string GetHttpMessageHandlerType = "Android.Runtime.AndroidEnvironment, Mono.Android";
22+
#elif TARGET_IOS
23+
private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler";
24+
private const string AssemblyName = "Xamarin.iOS";
25+
private const string GetHttpMessageHandlerType = "ObjCRuntime.RuntimeOptions, Xamarin.iOS";
26+
#elif TARGET_MACCATALYST
1827
private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler";
1928
private const string AssemblyName = "Xamarin.MacCatalyst";
29+
private const string GetHttpMessageHandlerType = "ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst";
30+
#elif TARGET_TVOS
31+
private const string NativeHandlerType = "System.Net.Http.NSUrlSessionHandler";
32+
private const string AssemblyName = "Xamarin.TVOS";
33+
private const string GetHttpMessageHandlerType = "ObjCRuntime.RuntimeOptions, Xamarin.TVOS";
34+
#else
35+
#error Unknown target
36+
#endif
2037

2138
[DynamicDependency("get_DefaultProxyCredentials", NativeHandlerType, AssemblyName)]
2239
private ICredentials? GetDefaultProxyCredentials() => (ICredentials?)InvokeNativeHandlerMethod("get_DefaultProxyCredentials");
@@ -45,6 +62,12 @@ public partial class HttpClientHandler : HttpMessageHandler
4562
[DynamicDependency("get_ClientCertificates", NativeHandlerType, AssemblyName)]
4663
private X509CertificateCollection GetClientCertificates() => (X509CertificateCollection)InvokeNativeHandlerMethod("get_ClientCertificates");
4764

65+
[DynamicDependency("get_ServerCertificateCustomValidationCallback", NativeHandlerType, AssemblyName)]
66+
private Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> GetServerCertificateCustomValidationCallback() => (Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool>)InvokeNativeHandlerMethod("get_ServerCertificateCustomValidationCallback");
67+
68+
[DynamicDependency("set_ServerCertificateCustomValidationCallback", NativeHandlerType, AssemblyName)]
69+
private void SetServerCertificateCustomValidationCallback(Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool>? value) => InvokeNativeHandlerMethod("set_ServerCertificateCustomValidationCallback", value);
70+
4871
[DynamicDependency("get_CheckCertificateRevocationList", NativeHandlerType, AssemblyName)]
4972
private bool GetCheckCertificateRevocationList() => (bool)InvokeNativeHandlerMethod("get_CheckCertificateRevocationList");
5073

@@ -127,7 +150,7 @@ private HttpMessageHandler CreateNativeHandler()
127150
{
128151
if (_nativeHandlerMethod == null)
129152
{
130-
Type? runtimeOptions = Type.GetType("ObjCRuntime.RuntimeOptions, Xamarin.MacCatalyst");
153+
Type? runtimeOptions = Type.GetType(GetHttpMessageHandlerType);
131154
_nativeHandlerMethod = runtimeOptions!.GetMethod("GetHttpMessageHandler", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static);
132155
}
133156

src/libraries/System.Net.Http/src/System/Net/Http/HttpClientHandler.AnyMobile.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ public X509CertificateCollection ClientCertificates
426426
{
427427
if (IsNativeHandlerEnabled)
428428
{
429-
throw new PlatformNotSupportedException();
429+
return GetServerCertificateCustomValidationCallback();
430430
}
431431
else
432432
{
@@ -437,7 +437,7 @@ public X509CertificateCollection ClientCertificates
437437
{
438438
if (IsNativeHandlerEnabled)
439439
{
440-
throw new PlatformNotSupportedException();
440+
SetServerCertificateCustomValidationCallback(value);
441441
}
442442
else
443443
{

0 commit comments

Comments
 (0)