2 // MonoTlsProviderFactory.cs
5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 Xamarin, Inc.
9 // Permission is hereby granted, free of charge, to any person obtaining a copy
10 // of this software and associated documentation files (the "Software"), to deal
11 // in the Software without restriction, including without limitation the rights
12 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 // copies of the Software, and to permit persons to whom the Software is
14 // furnished to do so, subject to the following conditions:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
30 using MSI = MonoSecurity::Mono.Security.Interface;
31 using MX = MonoSecurity::Mono.Security.X509;
33 using MSI = Mono.Security.Interface;
34 using MX = Mono.Security.X509;
36 using System.Security.Cryptography.X509Certificates;
41 using System.Collections.Generic;
44 using System.Reflection;
47 namespace Mono.Net.Security
50 * Keep in sync with Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs.
53 static partial class MonoTlsProviderFactory
58 * APIs in this section are for consumption within System.dll only - do not access via
59 * reflection or from friend assemblies.
61 * @IMonoTlsProvider is defined as empty interface outside 'SECURITY_DEP', so we don't need
62 * this conditional here.
65 internal static IMonoTlsProvider GetProviderInternal ()
68 if (currentProvider != null)
69 return currentProvider;
72 defaultProvider = GetDefaultProviderInternal ();
73 } catch (Exception ex) {
74 throw new NotSupportedException ("TLS Support not available.", ex);
77 if (defaultProvider == null)
78 throw new NotSupportedException ("TLS Support not available.");
80 currentProvider = defaultProvider;
81 return currentProvider;
85 internal static IMonoTlsProvider GetDefaultProviderInternal ()
88 if (defaultProvider != null)
89 return defaultProvider;
92 defaultProvider = CreateDefaultProvider ();
93 } catch (Exception ex) {
94 throw new NotSupportedException ("TLS Support not available.", ex);
97 if (defaultProvider == null)
98 throw new NotSupportedException ("TLS Support not available.");
100 return defaultProvider;
104 static IMonoTlsProvider CreateDefaultProvider ()
107 MSI.MonoTlsProvider provider = CreateDefaultProviderImpl ();
108 if (provider != null)
109 return new Private.MonoTlsProviderWrapper (provider);
114 static object locker = new object ();
115 static IMonoTlsProvider defaultProvider;
116 static IMonoTlsProvider currentProvider;
122 static Dictionary<string,string> providerRegistration;
124 static Type LookupProviderType (string name, bool throwOnError)
127 InitializeProviderRegistration ();
129 if (!providerRegistration.TryGetValue (name, out typeName)) {
131 throw new NotSupportedException (string.Format ("No such TLS Provider: `{0}'.", name));
134 var type = Type.GetType (typeName, false);
135 if (type == null && throwOnError)
136 throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", typeName));
141 static MSI.MonoTlsProvider LookupProvider (string name, bool throwOnError)
143 var type = LookupProviderType (name, throwOnError);
148 return (MSI.MonoTlsProvider)Activator.CreateInstance (type, true);
149 } catch (Exception ex) {
150 throw new NotSupportedException (string.Format ("Unable to instantiate TLS Provider `{0}'.", type), ex);
154 static void InitializeProviderRegistration ()
157 if (providerRegistration != null)
159 providerRegistration = new Dictionary<string,string> ();
160 providerRegistration.Add ("legacy", "Mono.Net.Security.Private.MonoLegacyTlsProvider");
162 if (Mono.Btls.MonoBtlsProvider.IsSupported ())
163 providerRegistration.Add ("btls", "Mono.Btls.MonoBtlsProvider");
165 X509Helper2.Initialize ();
169 #if MOBILE_STATIC || !MOBILE
170 static MSI.MonoTlsProvider TryDynamicLoad ()
172 var variable = Environment.GetEnvironmentVariable ("MONO_TLS_PROVIDER");
173 if (variable == null)
176 if (string.Equals (variable, "default", StringComparison.OrdinalIgnoreCase))
179 return LookupProvider (variable, true);
182 static MSI.MonoTlsProvider CreateDefaultProviderImpl ()
184 var provider = TryDynamicLoad ();
185 if (provider != null)
188 return new Private.MonoLegacyTlsProvider ();
192 #region Mono.Security visible API
195 * "Public" section, intended to be consumed via reflection.
197 * Mono.Security.dll provides a public wrapper around these.
200 internal static MSI.MonoTlsProvider GetProvider ()
202 var provider = GetProviderInternal ();
203 if (provider == null)
204 throw new NotSupportedException ("No TLS Provider available.");
206 return provider.Provider;
209 internal static MSI.MonoTlsProvider GetDefaultProvider ()
211 var provider = GetDefaultProviderInternal ();
212 if (provider == null)
213 throw new NotSupportedException ("No TLS Provider available.");
215 return provider.Provider;
218 internal static MSI.MonoTlsProvider GetProvider (string name)
220 return LookupProvider (name, false);
223 internal static bool HasProvider {
226 return currentProvider != null;
231 internal static void SetDefaultProvider (string name)
234 var provider = LookupProvider (name, true);
235 currentProvider = new Private.MonoTlsProviderWrapper (provider);
239 internal static HttpWebRequest CreateHttpsRequest (Uri requestUri, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
242 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
243 return new HttpWebRequest (requestUri, internalProvider, settings);
247 internal static HttpListener CreateHttpListener (X509Certificate certificate, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
250 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
251 return new HttpListener (certificate, internalProvider, settings);