Introducing a general-purpose dependency injector to use `System` code from within...
commit23a209198b3f662fadabeaae8d23e4d630970315
authorMartin Baulig <mabaul@microsoft.com>
Mon, 9 Jul 2018 23:54:47 +0000 (9 19:54 -0400)
committerMartin Baulig <mabaul@microsoft.com>
Thu, 12 Jul 2018 17:28:40 +0000 (12 13:28 -0400)
tree85448f57616470c32f130f641748df90b1de7a0f
parent7394cfaa25d5cc7897b3d1066e76a9ed854ce47f
Introducing a general-purpose dependency injector to use `System` code from within `corlib`.

Dependency Injector
===================

* `corlib/Mono/DependencyInjector.cs`: this is corlib's entry point.

* `corlib/Mono/ISystemDependencyProvider.cs`: this interface is implemented in `System.dll`.

* `System/Mono/SystemDependencyProvider.cs`: the `System.dll` side of it.

To use this, we need to call `SystemDependencyProvider.Initialize()` during system startup on Mobile
(this is already done for Android, but still needs to be done for XI/XM).

For Desktop, this will be triggered by a few `System.dll` based APIs such as for instance `SslStream`
or `X509Certificate2` - and the corlib side can use reflection if needed.

The corlib side should use `DependencyInjector.GetSystemDependency()`.

X509Certificate - corlib side
=============================

We previously had a mechanic called `INativeCertificateHelper` (which unfortunately was not really
working as intended) to use the BTLS code (which lives in System) in corlib.  This has now been
replaced to use the new dependency injector.

The actual implementation as well as all the platform conditionals have now been moved into `System.dll`
where they can take advantage of existing PAL code.

This also removes all `Mono.Security.X509` usage from the `System.Security.Cryptography.X509Certificates`
namespace (it is still used by some of the other crypto classes, so we can't remove it yet).

* `Mono/ISystemCertificateProvider.cs`: this interface is now hooked up via the new
  dependency injector and replaces `INativeCertificateHelper`.

* `System.Security.Cryptography.X509Certificates/X509Helper.cs`: this file has been completely
  reworked and is now just a tiny stub around `ISystemCertificateProvider` and will eventually go
  away completely.  The entire implementation has been moved into `System.dll`.

* `System.Security.Cryptography.X509Certificates/X509CertificateImplMono.cs`: removed, we now use
  `X509Certificate2ImplMono` from System via `ISystemCertificateProvider`.

* `System.Security.Cryptography.X509Certificates/X509CertificateImplApple.cs`: moved into `System.dll`
  and slightly reworked there.

* `System.Security.Cryptography.X509Certificates/X509Helper.Apple.cs`: removed, we do not have any
  platform-specific code here anymore; the new `ISystemCertificateProvider` provides this for us.

X509Certificate - System side
=============================

The actual implementation as well as all the platform-specific code now lives in `System.dll`.

* `Mono/SystemCertificateProvider.cs`: the `System.dll` implementation of `ISystemCertificateProvider`.
  We do the platform / TLS Provider checks in here, then construct a custom PAL class.

* `Mono/X509Pal.cs`: New internal static class.

* `Mono/X509PalImpl.cs`: New abstract internal class.  It contains some common code, including the
  fallback implementation.

* `Mono/X509PalImpl.Mono.cs`: Fallback PAL, used by platforms without SslStream / MonoTlsProvider.

* `Mono.Btls/X509PalImpl.Btls.cs`: BTLS implementation.

* `Mono.AppleTls/X509PalImpl.Apple.cs`: We are using the `MONO_FEATURE_APPLE_X509` conditional in
  here, so it can be used without the actual Apple TLS code.

* `Mono.AppleTls/X509CertificateImplApple.cs`: moved here from corlib.

* `Mono.AppleTls/*.cs`: Use the `X509CertificateImplApple(IntPtr)` constructor everywhere instead of
  `X509Certificate(IntPtr)` to make sure we are using the "correct" PAL.
28 files changed:
mcs/class/System/Mono.AppleTls/Certificate.cs
mcs/class/System/Mono.AppleTls/Trust.cs
mcs/class/System/Mono.AppleTls/X509CertificateImplApple.cs [moved from mcs/class/corlib/System.Security.Cryptography.X509Certificates/X509CertificateImplApple.cs with 93% similarity]
mcs/class/System/Mono.AppleTls/X509PalImpl.Apple.cs [copied from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 52% similarity]
mcs/class/System/Mono.Btls/X509CertificateImplBtls.cs
mcs/class/System/Mono.Btls/X509PalImpl.Btls.cs [new file with mode: 0644]
mcs/class/System/Mono.Net.Security/MonoTlsProviderFactory.cs
mcs/class/System/Mono/SystemCertificateProvider.cs [new file with mode: 0644]
mcs/class/System/Mono/SystemDependencyProvider.cs [copied from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 50% similarity]
mcs/class/System/Mono/X509Pal.cs [copied from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 75% similarity]
mcs/class/System/Mono/X509PalImpl.Mono.cs [copied from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 61% similarity]
mcs/class/System/Mono/X509PalImpl.cs [new file with mode: 0644]
mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2.cs
mcs/class/System/System.Security.Cryptography.X509Certificates/X509Certificate2ImplMono.cs
mcs/class/System/System.Security.Cryptography.X509Certificates/X509Helper2.cs
mcs/class/System/System/AndroidPlatform.cs
mcs/class/System/common.sources
mcs/class/corlib/Mono/CertificateImportFlags.cs [copied from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 81% similarity]
mcs/class/corlib/Mono/DependencyInjector.cs [new file with mode: 0644]
mcs/class/corlib/Mono/ISystemCertificateProvider.cs [copied from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 67% similarity]
mcs/class/corlib/Mono/ISystemDependencyProvider.cs [moved from mcs/class/corlib/System.Security.Cryptography.X509Certificates/INativeCertificateHelper.cs with 75% similarity]
mcs/class/corlib/System.Security.Cryptography.X509Certificates/X509Certificate.cs
mcs/class/corlib/System.Security.Cryptography.X509Certificates/X509Certificate20.cs
mcs/class/corlib/System.Security.Cryptography.X509Certificates/X509CertificateImplMono.cs [deleted file]
mcs/class/corlib/System.Security.Cryptography.X509Certificates/X509Helper.Apple.cs [deleted file]
mcs/class/corlib/System.Security.Cryptography.X509Certificates/X509Helper.cs
mcs/class/corlib/corlib.dll.sources
mcs/class/corlib/monotouch_corlib.dll.sources