--- /dev/null
+//
+// MonoTlsProviderFactory.cs
+//
+// Author:
+// Chris Hamons <chris.hamons@xamarin.com>
+// Martin Baulig <martin.baulig@xamarin.com>
+//
+// Copyright (c) 2015 Xamarin, Inc.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a copy
+// of this software and associated documentation files (the "Software"), to deal
+// in the Software without restriction, including without limitation the rights
+// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+// copies of the Software, and to permit persons to whom the Software is
+// furnished to do so, subject to the following conditions:
+//
+// The above copyright notice and this permission notice shall be included in
+// all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+// THE SOFTWARE.
+
+#if !ONLY_APPLETLS
+#error Use MonoTlsProviderFactory.cs instead
+#endif
+
+#if SECURITY_DEP
+
+#if MONO_SECURITY_ALIAS
+extern alias MonoSecurity;
+using MSI = MonoSecurity::Mono.Security.Interface;
+using MX = MonoSecurity::Mono.Security.X509;
+#else
+using MSI = Mono.Security.Interface;
+using MX = Mono.Security.X509;
+#endif
+using System.Security.Cryptography.X509Certificates;
+using Mono.AppleTls;
+
+using System;
+using System.Net;
+using System.Collections.Generic;
+using System.Runtime.CompilerServices;
+
+namespace Mono.Net.Security
+{
+ /*
+ * Keep in sync with Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs.
+ * Simple implementation that does hard codes only a single provider
+ */
+ static partial class MonoTlsProviderFactory
+ {
+ #region Internal API
+
+ /*
+ * APIs in this section are for consumption within System.dll only - do not access via
+ * reflection or from friend assemblies.
+ *
+ */
+ internal static MSI.MonoTlsProvider GetProviderInternal ()
+ {
+ return GetTlsProvider ();
+ }
+
+ #endregion
+
+ static object locker = new object ();
+ static MSI.MonoTlsProvider provider;
+ static MSI.MonoTlsProvider GetTlsProvider ()
+ {
+ lock (locker) {
+ if (provider == null)
+ provider = new AppleTlsProvider ();
+ return provider;
+ }
+ }
+
+
+ #region Mono.Security visible API
+
+ /*
+ * "Public" section, intended to be consumed via reflection.
+ *
+ * Mono.Security.dll provides a public wrapper around these.
+ */
+
+ internal static MSI.MonoTlsProvider GetProvider ()
+ {
+ return GetTlsProvider ();
+ }
+
+ internal static bool IsProviderSupported (string name)
+ {
+ return true;
+ }
+
+ internal static MSI.MonoTlsProvider GetProvider (string name)
+ {
+ return GetTlsProvider ();
+ }
+
+ internal static bool IsInitialized => true;
+
+ internal static void Initialize ()
+ {
+ }
+
+ internal static void Initialize (string provider)
+ {
+ }
+ #endregion
+ }
+}
+#endif
+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
-#if SECURITY_DEP
+#if SECURITY_DEP && !ONLY_APPLETLS // ONLY_APPLETLS uses MonoTlsProviderFactory.Apple.cs instead
#if MONO_SECURITY_ALIAS
extern alias MonoSecurity;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
-#if MONO_FEATURE_BTLS
-using Mono.Btls;
-#endif
-
#if !MOBILE
using System.Reflection;
#endif
*/
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
if (provider == null)
throw new NotSupportedException ("TLS Support not available.");
- if (!providerCache.ContainsKey (provider.ID))
- providerCache.Add (provider.ID, provider);
-
- X509Helper2.Initialize ();
-
defaultProvider = provider;
initialized = true;
}
throw new NotSupportedException ("TLS Subsystem already initialized.");
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 MSI.MonoTlsProvider defaultProvider;
- /*
- * @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;
+ #endregion
+
+ static Dictionary<string,string> providerRegistration;
-#if !ONLY_APPLETLS && !MONOTOUCH && !XAMMAC
static Type LookupProviderType (string name, bool throwOnError)
{
lock (locker) {
InitializeProviderRegistration ();
- Tuple<Guid,string> entry;
- if (!providerRegistration.TryGetValue (name, out entry)) {
+ string typeName;
+ if (!providerRegistration.TryGetValue (name, out typeName)) {
if (throwOnError)
throw new NotSupportedException (string.Format ("No such TLS Provider: `{0}'.", name));
return null;
}
- var type = Type.GetType (entry.Item2, false);
+ var type = Type.GetType (typeName, false);
if (type == null && throwOnError)
- throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", entry.Item2));
+ throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", typeName));
return type;
}
}
-#endif
static MSI.MonoTlsProvider LookupProvider (string name, bool throwOnError)
{
- 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;
- }
-
- // 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;
+ var type = LookupProviderType (name, throwOnError);
+ if (type == null)
+ 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);
}
}
-#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,Tuple<Guid,string>> ();
- providerCache = new Dictionary<Guid,MSI.MonoTlsProvider> ();
-
- var appleTlsEntry = new Tuple<Guid,String> (AppleTlsId, "Mono.AppleTls.AppleTlsProvider");
-
-#if ONLY_APPLETLS || MONOTOUCH || XAMMAC
- providerRegistration.Add ("default", appleTlsEntry);
- providerRegistration.Add ("apple", appleTlsEntry);
-#else
- var legacyEntry = new Tuple<Guid,String> (BtlsId, "Mono.Net.Security.LegacyTlsProvider");
-#if MONO_FEATURE_BTLS
- var btlsEntry = new Tuple<Guid,String> (LegacyId, "Mono.Btls.MonoBtlsProvider");
-#endif
-
- providerRegistration.Add ("legacy", legacyEntry);
-
+ providerRegistration = new Dictionary<string,string> ();
+ providerRegistration.Add ("legacy", "Mono.Net.Security.LegacyTlsProvider");
+
if (Platform.IsMacOS)
- providerRegistration.Add ("default", appleTlsEntry);
+ providerRegistration.Add ("default", "Mono.AppleTls.AppleTlsProvider");
else
- providerRegistration.Add ("default", legacyEntry);
+ providerRegistration.Add ("default", "Mono.Net.Security.LegacyTlsProvider");
-#if MONO_FEATURE_BTLS
if (IsBtlsSupported ())
- providerRegistration.Add ("btls", btlsEntry);
-#endif
-
- providerRegistration.Add ("apple", appleTlsEntry);
-#endif
+ providerRegistration.Add ("btls", "Mono.Btls.MonoBtlsProvider");
+
+ providerRegistration.Add ("apple", "Mono.AppleTls.AppleTlsProvider");
+
+ X509Helper2.Initialize ();
}
}
-#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 ()
+#if !MONODROID && !MONOTOUCH && !XAMMAC
+ static MSI.MonoTlsProvider TryDynamicLoad ()
{
var variable = Environment.GetEnvironmentVariable ("MONO_TLS_PROVIDER");
if (string.IsNullOrEmpty (variable))
return LookupProvider (variable, true);
}
-#endif
-#endregion
+ static MSI.MonoTlsProvider CreateDefaultProviderImpl ()
+ {
+ var provider = TryDynamicLoad ();
+ if (provider != null)
+ return provider;
-#region Mono.Security visible API
+ return new LegacyTlsProvider ();
+ }
+#endif
+
+ #region Mono.Security visible API
/*
* "Public" section, intended to be consumed via reflection.
internal static bool IsProviderSupported (string name)
{
- lock (locker) {
- InitializeProviderRegistration ();
- return providerRegistration.ContainsKey (name);
- }
+ return LookupProvider (name, false) != null;
}
internal static MSI.MonoTlsProvider GetProvider (string name)
{
InitializeInternal (provider);
}
-#endregion
+ #endregion
}
}
#endif