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 = CreateDefaultProvider ();
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 #if MONO_FEATURE_NEW_SYSTEM_SOURCE || (!MONOTOUCH && !XAMMAC)
105 static IMonoTlsProvider CreateDefaultProvider ()
108 #if MONO_FEATURE_NEW_SYSTEM_SOURCE
110 * This is a hack, which is used in the Mono.Security.Providers.NewSystemSource
111 * assembly, which will provide a "fake" System.dll. Use the public Mono.Security
112 * API to get the "real" System.dll's provider via reflection, then wrap it with
113 * the "fake" version's perceived view.
115 * NewSystemSource needs to compile MonoTlsProviderFactory.cs, IMonoTlsProvider.cs,
116 * MonoTlsProviderWrapper.cs and CallbackHelpers.cs from this directory and only these.
118 var userProvider = MSI.MonoTlsProviderFactory.GetProvider ();
119 return new Private.MonoTlsProviderWrapper (userProvider);
121 return CreateDefaultProviderImpl ();
129 static object locker = new object ();
130 static IMonoTlsProvider defaultProvider;
131 static IMonoTlsProvider currentProvider;
135 #if SECURITY_DEP && !MONO_FEATURE_NEW_SYSTEM_SOURCE
138 static Dictionary<string,string> providerRegistration;
140 internal static void RegisterProvider (string name, string type)
143 InitializeProviderRegistration ();
144 providerRegistration.Add (name, type);
148 static string LookupProvider (string name)
151 InitializeProviderRegistration ();
153 if (!providerRegistration.TryGetValue (name, out type))
159 static void InitializeProviderRegistration ()
162 if (providerRegistration != null)
164 providerRegistration = new Dictionary<string,string> ();
165 providerRegistration.Add ("newtls", "Mono.Security.Providers.NewTls.NewTlsProvider, Mono.Security.Providers.NewTls, Version=4.0.0.0, Culture=neutral, PublicKeyToken=84e3aee7225169c2");
166 providerRegistration.Add ("oldtls", "Mono.Security.Providers.OldTls.OldTlsProvider, Mono.Security.Providers.OldTls, Version=4.0.0.0, Culture=neutral, PublicKeyToken=84e3aee7225169c2");
170 static IMonoTlsProvider TryDynamicLoad ()
172 var variable = Environment.GetEnvironmentVariable ("MONO_TLS_PROVIDER");
173 if (variable == null)
176 if (string.Equals (variable, "default", StringComparison.OrdinalIgnoreCase))
180 if (variable.IndexOfAny (new char[] { ',', '.', '=' }) > 0) {
183 typeName = LookupProvider (variable);
184 if (typeName == null)
185 throw new NotSupportedException (string.Format ("No such TLS Provider: `{0}'.", typeName));
188 var type = Type.GetType (typeName, false);
190 throw new NotSupportedException (string.Format ("Could not find TLS Provider: `{0}'.", typeName));
192 MSI.MonoTlsProvider provider;
194 provider = (MSI.MonoTlsProvider)Activator.CreateInstance (type);
195 } catch (Exception ex) {
196 throw new NotSupportedException (string.Format ("Unable to instantiate TLS Provider `{0}'.", typeName), ex);
199 return new Private.MonoTlsProviderWrapper (provider);
203 static IMonoTlsProvider CreateDefaultProviderImpl ()
206 var provider = TryDynamicLoad ();
207 if (provider != null)
211 return new Private.MonoDefaultTlsProvider ();
214 #region Mono.Security visible API
217 * "Public" section, intended to be consumed via reflection.
219 * Mono.Security.dll provides a public wrapper around these.
222 internal static MSI.MonoTlsProvider GetProvider ()
224 var provider = GetProviderInternal ();
225 if (provider == null)
226 throw new NotSupportedException ("No TLS Provider available.");
228 return provider.Provider;
231 internal static MSI.MonoTlsProvider GetDefaultProvider ()
233 var provider = GetDefaultProviderInternal ();
234 if (provider == null)
235 throw new NotSupportedException ("No TLS Provider available.");
237 return provider.Provider;
240 internal static bool HasProvider {
243 return currentProvider != null;
248 internal static void InstallProvider (MSI.MonoTlsProvider provider)
251 currentProvider = new Private.MonoTlsProviderWrapper (provider);
255 internal static HttpWebRequest CreateHttpsRequest (Uri requestUri, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
258 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
259 return new HttpWebRequest (requestUri, internalProvider, settings);
263 internal static HttpListener CreateHttpListener (X509Certificate certificate, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
266 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
267 return new HttpListener (certificate, internalProvider, settings);