Repository navigation
[Mono.Android] custom validation callback for server certificates in HTTP handlers - #6972
Conversation
…HTTP handlers (dotnet#6665) Backport of: dotnet#6665 Context: dotnet/runtime#62966 The `AndroidClientHandler` and `AndroidMessageHandler` classes both have the `ServerCertificateCustomValidationCallback` property, which should be useful e.g. to allow running the Android app against a server with a self-signed SSL certificate during development, but the callback is never used. Unfortunatelly since .NET 6 the `System.Net.Http.SocketsHttpHandler` for Android doesn't support the use case anymore. That means that [the recommended way of connecting to local web server][0] won't work in MAUI. This PR introduces an implementation of `IX509TrustManger` which wraps the default Java X509 trust manager and calls the user's callback on top of the default validation. It turns out that `X509Chain` `Build` function doesn't work on Android, so I'm not calling it and I'm passing the chain to the callback directly. Additionally, we need a default proguard rule due to: https://github.com/xamarin/xamarin-android/blob/46002b49d8c0b7b1a17532a8e104b4d31afee7a6/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs#L50-L57 -keep class xamarin.android.net.X509TrustManagerWithValidationCallback { *; <init>(...); } `Mono.Android.dll` is skipped during the `GenerateProguardConfiguration` linker step. It might be worth addressing this in a future PR. [0]: https://docs.microsoft.com/en-us/xamarin/cross-platform/deploy-test/connect-to-local-web-services
|
|
||
| tmf = TrustManagerFactory.GetInstance (TrustManagerFactory.DefaultAlgorithm); | ||
| tmf?.Init (keyStore); | ||
| tmf?.Init (gotCerts ? keyStore : null); // only use the custom key store if the user defined any trusted certs |
There was a problem hiding this comment.
Should it say custom instead of trusted? Certs defined by the user will most likely not be trusted in the official CA chain sense
| keyStore?.Load (null, null); | ||
| gotCerts = TrustedCerts?.Count > 0; | ||
|
|
||
| if (gotCerts) { |
There was a problem hiding this comment.
This is an absolute nitpick on my part, but could you change the code to
if (!gotCerts) {
return keystore;
}
for (...) {
}I tend to prefer to keep large blocks of code outside the if statement sub-blocks - in my opinion it makes code more readable :)
There was a problem hiding this comment.
I agree this is generally how we prefer stuff in XA, the "early out" option is best 👍
| { | ||
| internal sealed class Helper | ||
| { | ||
| public Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> Callback { get; } |
There was a problem hiding this comment.
Should Func<> be perhaps replaced by a specific delegate to be used throughout this class but also in AndroidMessageHandler? It would make it easier to modify it in the future, should there be a need for that and also reduce the signal to noise ratio when reading the code
There was a problem hiding this comment.
If exposed publicly from a public type, you don't necessarily want "easier to modify", as changing the delegate signature is a breaking change. This should to be the same delegate type as the public AndroidMessageHandler.ServerCertificateCustomValidationCallback property, and thus is a type that cannot be changed.
| { | ||
| IX509TrustManager? x509TrustManager = trustManagers?.OfType<IX509TrustManager> ().FirstOrDefault (); | ||
| IEnumerable<ITrustManager> otherTrustManagers = trustManagers?.Where (manager => manager != x509TrustManager) ?? Enumerable.Empty<ITrustManager> (); | ||
| var trustManagerWithCallback = new X509TrustManagerWithValidationCallback (x509TrustManager, requestMessage, Callback); |
There was a problem hiding this comment.
I'm not very fond of LINQ code in frameworks or libraries, since it's not very performant. Could we use the boring loops here? :)
| private readonly HttpRequestMessage _request; | ||
| private readonly Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> _serverCertificateCustomValidationCallback; | ||
|
|
||
| private X509TrustManagerWithValidationCallback ( |
There was a problem hiding this comment.
The private modifiers are superfluous since this is the default visibility of a class member.
|
|
||
| try { | ||
| _internalTrustManager?.CheckServerTrusted (javaChain, authType); | ||
| } catch (JavaCertificateException) { |
There was a problem hiding this comment.
I feel we should log this exception, not the whole trace, just the message, using the Debug severity so that it's not shown by default.
|
@grendello this is a backport of 48540d6 to the .NET 6 release branch (I think you approved). I mostly just fixed up the Do you want @simonrozsival to address these comments in |
DotNet follows a different style, and sinde my comments are mostly stylistic, hence my lack of comments then :) |
|
|
||
| tmf = TrustManagerFactory.GetInstance (TrustManagerFactory.DefaultAlgorithm); | ||
| tmf?.Init (keyStore); | ||
| tmf?.Init (gotCerts ? keyStore : null); // only use the custom key store if the user defined any trusted certs |
There was a problem hiding this comment.
| tmf?.Init (gotCerts ? keyStore : null); // only use the custom key store if the user defined any trusted certs | |
| tmf?.Init (gotCerts ? keyStore : null); // only use the custom key store if the user defined any custom certs |
| } | ||
| } | ||
|
|
||
| private readonly IX509TrustManager? _internalTrustManager; |
There was a problem hiding this comment.
| private readonly IX509TrustManager? _internalTrustManager; | |
| readonly IX509TrustManager? _internalTrustManager; |
| } | ||
|
|
||
| private readonly IX509TrustManager? _internalTrustManager; | ||
| private readonly HttpRequestMessage _request; |
There was a problem hiding this comment.
| private readonly HttpRequestMessage _request; | |
| readonly HttpRequestMessage _request; |
|
|
||
| private readonly IX509TrustManager? _internalTrustManager; | ||
| private readonly HttpRequestMessage _request; | ||
| private readonly Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> _serverCertificateCustomValidationCallback; |
There was a problem hiding this comment.
| private readonly Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> _serverCertificateCustomValidationCallback; | |
| readonly Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> _serverCertificateCustomValidationCallback; |
| private readonly HttpRequestMessage _request; | ||
| private readonly Func<HttpRequestMessage, X509Certificate2?, X509Chain?, SslPolicyErrors, bool> _serverCertificateCustomValidationCallback; | ||
|
|
||
| private X509TrustManagerWithValidationCallback ( |
There was a problem hiding this comment.
| private X509TrustManagerWithValidationCallback ( | |
| X509TrustManagerWithValidationCallback ( |
| using System.Linq; | ||
| using System.Net.Http; | ||
| using System.Net.Security; | ||
| using System.Security.Cryptography.X509Certificates; |
There was a problem hiding this comment.
| using System.Security.Cryptography.X509Certificates; | |
| using System.Security.Cryptography.X509Certificates; | |
| using Android.Runtime; |
|
|
||
| try { | ||
| _internalTrustManager?.CheckServerTrusted (javaChain, authType); | ||
| } catch (JavaCertificateException) { |
There was a problem hiding this comment.
| } catch (JavaCertificateException) { | |
| } catch (JavaCertificateException ex) { | |
| Logger.Log (LogLevel.Debug, $"Java certificate validation exception. {ex.Message}"); |
| public JavaX509Certificate[] GetAcceptedIssuers () | ||
| => _internalTrustManager?.GetAcceptedIssuers () ?? Array.Empty<JavaX509Certificate> (); | ||
|
|
||
| private static X509Chain CreateChain (X509Certificate2[] certificates) |
There was a problem hiding this comment.
| private static X509Chain CreateChain (X509Certificate2[] certificates) | |
| static X509Chain CreateChain (X509Certificate2[] certificates) |
| return chain; | ||
| } | ||
|
|
||
| private static X509Certificate2[] ConvertCertificates (JavaX509Certificate[] certificates) |
There was a problem hiding this comment.
| private static X509Certificate2[] ConvertCertificates (JavaX509Certificate[] certificates) | |
| static X509Certificate2[] ConvertCertificates (JavaX509Certificate[] certificates) |
Backport of: #6665
Context: dotnet/runtime#62966
The
AndroidClientHandlerandAndroidMessageHandlerclasses bothhave the
ServerCertificateCustomValidationCallbackproperty, whichshould be useful e.g. to allow running the Android app against a
server with a self-signed SSL certificate during development, but the
callback is never used. Unfortunatelly since .NET 6 the
System.Net.Http.SocketsHttpHandlerfor Android doesn't support theuse case anymore. That means that the recommended way of connecting
to local web server won't work in MAUI.
This PR introduces an implementation of
IX509TrustMangerwhich wrapsthe default Java X509 trust manager and calls the user's callback on
top of the default validation.
It turns out that
X509ChainBuildfunction doesn't work onAndroid, so I'm not calling it and I'm passing the chain to the
callback directly.
Additionally, we need a default proguard rule due to:
https://github.com/xamarin/xamarin-android/blob/46002b49d8c0b7b1a17532a8e104b4d31afee7a6/src/Xamarin.Android.Build.Tasks/Linker/MonoDroid.Tuner/GenerateProguardConfiguration.cs#L50-L57
Mono.Android.dllis skipped during theGenerateProguardConfigurationlinker step. It might be worthaddressing this in a future PR.