Imported Upstream version 5.4.0.167

Former-commit-id: 5624ac747d633e885131e8349322922b6a59baaa
This commit is contained in:
Xamarin Public Jenkins (auto-signing)
2017-08-21 15:34:15 +00:00
parent e49d6f06c0
commit 536cd135cc
12856 changed files with 563812 additions and 223249 deletions

View File

@ -24,9 +24,8 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#if !ONLY_APPLETLS // ONLY_APPLETLS uses MonoTlsProviderFactory.Apple.cs instead
#if SECURITY_DEP
#if MONO_SECURITY_ALIAS
extern alias MonoSecurity;
using MSI = MonoSecurity::Mono.Security.Interface;
@ -36,13 +35,20 @@ using MSI = Mono.Security.Interface;
using MX = Mono.Security.X509;
#endif
using System.Security.Cryptography.X509Certificates;
#endif
using System;
using System.Net;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
#if MONO_FEATURE_BTLS
using Mono.Btls;
#endif
#if MONO_FEATURE_APPLETLS
using Mono.AppleTls;
#endif
#if !MOBILE
using System.Reflection;
#endif
@ -55,35 +61,30 @@ namespace Mono.Net.Security
*/
static partial class MonoTlsProviderFactory
{
#region Internal API
#region Internal API
/*
* APIs in this section are for consumption within System.dll only - do not access via
* reflection or from friend assemblies.
*
* @IMonoTlsProvider is defined as empty interface outside 'SECURITY_DEP', so we don't need
* this conditional here.
*/
internal static IMonoTlsProvider GetProviderInternal ()
internal static MSI.MonoTlsProvider GetProviderInternal ()
{
#if SECURITY_DEP
lock (locker) {
InitializeInternal ();
return defaultProvider;
}
#else
throw new NotSupportedException ("TLS Support not available.");
#endif
}
#if SECURITY_DEP
internal static void InitializeInternal ()
{
lock (locker) {
if (initialized)
return;
InitializeProviderRegistration ();
MSI.MonoTlsProvider provider;
try {
provider = CreateDefaultProviderImpl ();
@ -94,7 +95,12 @@ namespace Mono.Net.Security
if (provider == null)
throw new NotSupportedException ("TLS Support not available.");
defaultProvider = new Private.MonoTlsProviderWrapper (provider);
if (!providerCache.ContainsKey (provider.ID))
providerCache.Add (provider.ID, provider);
X509Helper2.Initialize ();
defaultProvider = provider;
initialized = true;
}
}
@ -105,90 +111,164 @@ namespace Mono.Net.Security
if (initialized)
throw new NotSupportedException ("TLS Subsystem already initialized.");
var msiProvider = LookupProvider (provider, true);
defaultProvider = new Private.MonoTlsProviderWrapper (msiProvider);
defaultProvider = LookupProvider (provider, true);
X509Helper2.Initialize ();
initialized = true;
}
}
[MethodImpl (MethodImplOptions.InternalCall)]
internal extern static bool IsBtlsSupported ();
static object locker = new object ();
static bool initialized;
static IMonoTlsProvider defaultProvider;
#endif
#endregion
static MSI.MonoTlsProvider defaultProvider;
#if SECURITY_DEP
static Dictionary<string,string> providerRegistration;
/*
* @providerRegistration maps provider names to a tuple containing its ID and full type name.
* On non-reflection enabled systems (such as XI and XM), we can use the Guid to uniquely
* identify the provider.
*
* @providerCache maps the provider's Guid to the MSI.MonoTlsProvider instance.
*
*/
static Dictionary<string,Tuple<Guid,string>> providerRegistration;
static Dictionary<Guid,MSI.MonoTlsProvider> providerCache;
#if !ONLY_APPLETLS && !MONOTOUCH && !XAMMAC
static Type LookupProviderType (string name, bool throwOnError)
{
lock (locker) {
InitializeProviderRegistration ();
string typeName;
if (!providerRegistration.TryGetValue (name, out typeName)) {
Tuple<Guid,string> entry;
if (!providerRegistration.TryGetValue (name, out entry)) {
if (throwOnError)
throw new NotSupportedException (string.Format ("No such TLS Provider: `{0}'.", name));
return null;
}
var type = Type.GetType (typeName, false);
var type = Type.GetType (entry.Item2, false);
if (type == null && throwOnError)
throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", typeName));
throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", entry.Item2));
return type;
}
}
#endif
static MSI.MonoTlsProvider LookupProvider (string name, bool throwOnError)
{
var type = LookupProviderType (name, throwOnError);
if (type == null)
return null;
lock (locker) {
InitializeProviderRegistration ();
Tuple<Guid,string> entry;
if (!providerRegistration.TryGetValue (name, out entry)) {
if (throwOnError)
throw new NotSupportedException (string.Format ("No such TLS Provider: `{0}'.", name));
return null;
}
try {
return (MSI.MonoTlsProvider)Activator.CreateInstance (type, true);
} catch (Exception ex) {
throw new NotSupportedException (string.Format ("Unable to instantiate TLS Provider `{0}'.", type), ex);
// Check cache before doing the reflection lookup.
MSI.MonoTlsProvider provider;
if (providerCache.TryGetValue (entry.Item1, out provider))
return provider;
#if !ONLY_APPLETLS && !MONOTOUCH && !XAMMAC
var type = Type.GetType (entry.Item2, false);
if (type == null && throwOnError)
throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", entry.Item2));
try {
provider = (MSI.MonoTlsProvider)Activator.CreateInstance (type, true);
} catch (Exception ex) {
throw new NotSupportedException (string.Format ("Unable to instantiate TLS Provider `{0}'.", type), ex);
}
#endif
if (provider == null) {
if (throwOnError)
throw new NotSupportedException (string.Format ("No such TLS Provider: `{0}'.", name));
return null;
}
providerCache.Add (entry.Item1, provider);
return provider;
}
}
#endregion
internal static readonly Guid AppleTlsId = new Guid ("981af8af-a3a3-419a-9f01-a518e3a17c1c");
internal static readonly Guid BtlsId = new Guid ("432d18c9-9348-4b90-bfbf-9f2a10e1f15b");
internal static readonly Guid LegacyId = new Guid ("809e77d5-56cc-4da8-b9f0-45e65ba9cceb");
static void InitializeProviderRegistration ()
{
lock (locker) {
if (providerRegistration != null)
return;
providerRegistration = new Dictionary<string,string> ();
providerRegistration = new Dictionary<string,Tuple<Guid,string>> ();
providerCache = new Dictionary<Guid,MSI.MonoTlsProvider> ();
string legacyProvider = "Mono.Net.Security.LegacyTlsProvider";
providerRegistration.Add ("legacy", legacyProvider);
var appleTlsEntry = new Tuple<Guid,String> (AppleTlsId, "Mono.AppleTls.AppleTlsProvider");
string defaultProvider = null;
#if ONLY_APPLETLS || MONOTOUCH || XAMMAC
providerRegistration.Add ("default", appleTlsEntry);
providerRegistration.Add ("apple", appleTlsEntry);
#else
var legacyEntry = new Tuple<Guid,String> (LegacyId, "Mono.Net.Security.LegacyTlsProvider");
providerRegistration.Add ("legacy", legacyEntry);
Tuple<Guid,String> btlsEntry = null;
#if MONO_FEATURE_BTLS
if (IsBtlsSupported ()) {
var btlsProvider = "Mono.Btls.MonoBtlsProvider";
providerRegistration.Add ("btls", btlsProvider);
defaultProvider = btlsProvider;
}
if (Platform.IsMacOS) {
var appleProvider = "Mono.AppleTls.AppleTlsProvider";
providerRegistration.Add ("apple", appleProvider);
defaultProvider = appleProvider;
btlsEntry = new Tuple<Guid,String> (BtlsId, "Mono.Btls.MonoBtlsProvider");
providerRegistration.Add ("btls", btlsEntry);
}
#endif
if (defaultProvider == null)
defaultProvider = legacyProvider;
if (Platform.IsMacOS)
providerRegistration.Add ("default", appleTlsEntry);
else if (btlsEntry != null)
providerRegistration.Add ("default", btlsEntry);
else
providerRegistration.Add ("default", legacyEntry);
providerRegistration.Add ("default", defaultProvider);
X509Helper2.Initialize ();
providerRegistration.Add ("apple", appleTlsEntry);
#endif
}
}
#if !MONODROID && !MONOTOUCH && !XAMMAC
static MSI.MonoTlsProvider TryDynamicLoad ()
#region Platform-Specific code
#if MONO_FEATURE_BTLS
[MethodImpl (MethodImplOptions.InternalCall)]
internal extern static bool IsBtlsSupported ();
#endif
#if MONODROID
static MSI.MonoTlsProvider CreateDefaultProviderImpl ()
{
MSI.MonoTlsProvider provider = null;
var type = Environment.GetEnvironmentVariable ("XA_TLS_PROVIDER");
switch (type) {
case null:
case "default":
case "legacy":
return new LegacyTlsProvider ();
#if MONO_FEATURE_BTLS
case "btls":
if (!IsBtlsSupported ())
throw new NotSupportedException ("BTLS in not supported!");
return new MonoBtlsProvider ();
#endif
default:
throw new NotSupportedException (string.Format ("Invalid TLS Provider: `{0}'.", provider));
}
}
#elif ONLY_APPLETLS || MONOTOUCH || XAMMAC
static MSI.MonoTlsProvider CreateDefaultProviderImpl ()
{
return new AppleTlsProvider ();
}
#else
static MSI.MonoTlsProvider CreateDefaultProviderImpl ()
{
var variable = Environment.GetEnvironmentVariable ("MONO_TLS_PROVIDER");
if (string.IsNullOrEmpty (variable))
@ -196,18 +276,11 @@ namespace Mono.Net.Security
return LookupProvider (variable, true);
}
static MSI.MonoTlsProvider CreateDefaultProviderImpl ()
{
var provider = TryDynamicLoad ();
if (provider != null)
return provider;
return new LegacyTlsProvider ();
}
#endif
#region Mono.Security visible API
#endregion
#region Mono.Security visible API
/*
* "Public" section, intended to be consumed via reflection.
@ -221,12 +294,15 @@ namespace Mono.Net.Security
if (provider == null)
throw new NotSupportedException ("No TLS Provider available.");
return provider.Provider;
return provider;
}
internal static bool IsProviderSupported (string name)
{
return LookupProvider (name, false) != null;
lock (locker) {
InitializeProviderRegistration ();
return providerRegistration.ContainsKey (name);
}
}
internal static MSI.MonoTlsProvider GetProvider (string name)
@ -244,41 +320,14 @@ namespace Mono.Net.Security
internal static void Initialize ()
{
#if SECURITY_DEP
InitializeInternal ();
#else
throw new NotSupportedException ("TLS Support not available.");
#endif
}
internal static void Initialize (string provider)
{
#if SECURITY_DEP
InitializeInternal (provider);
#else
throw new NotSupportedException ("TLS Support not available.");
#endif
}
internal static HttpWebRequest CreateHttpsRequest (Uri requestUri, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
{
lock (locker) {
var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
return new HttpWebRequest (requestUri, internalProvider, settings);
}
}
internal static HttpListener CreateHttpListener (X509Certificate certificate, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
{
lock (locker) {
var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
return new HttpListener (certificate, internalProvider, settings);
}
}
#endregion
#endif
#endregion
}
}
#endif