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;
42 namespace Mono.Net.Security
45 * Keep in sync with Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs.
48 static class MonoTlsProviderFactory
53 * APIs in this section are for consumption within System.dll only - do not access via
54 * reflection or from friend assemblies.
56 * @IMonoTlsProvider is defined as empty interface outside 'SECURITY_DEP', so we don't need
57 * this conditional here.
60 internal static IMonoTlsProvider GetProviderInternal ()
63 if (currentProvider != null)
64 return currentProvider;
67 defaultProvider = CreateDefaultProvider ();
68 } catch (Exception ex) {
69 throw new NotSupportedException ("TLS Support not available.", ex);
72 if (defaultProvider == null)
73 throw new NotSupportedException ("TLS Support not available.");
75 currentProvider = defaultProvider;
76 return currentProvider;
80 internal static IMonoTlsProvider GetDefaultProviderInternal ()
83 if (defaultProvider != null)
84 return defaultProvider;
87 defaultProvider = CreateDefaultProvider ();
88 } catch (Exception ex) {
89 throw new NotSupportedException ("TLS Support not available.", ex);
92 if (defaultProvider == null)
93 throw new NotSupportedException ("TLS Support not available.");
95 return defaultProvider;
99 static IMonoTlsProvider CreateDefaultProvider ()
102 #if MONO_FEATURE_NEW_SYSTEM_SOURCE
104 * This is a hack, which is used in the Mono.Security.Providers.NewSystemSource
105 * assembly, which will provide a "fake" System.dll. Use the public Mono.Security
106 * API to get the "real" System.dll's provider via reflection, then wrap it with
107 * the "fake" version's perceived view.
109 * NewSystemSource needs to compile MonoTlsProviderFactory.cs, IMonoTlsProvider.cs,
110 * MonoTlsProviderWrapper.cs and CallbackHelpers.cs from this directory and only these.
112 var userProvider = MSI.MonoTlsProviderFactory.GetProvider ();
113 return new Private.MonoTlsProviderWrapper (userProvider);
115 return new Private.MonoDefaultTlsProvider ();
122 static object locker = new object ();
123 static IMonoTlsProvider defaultProvider;
124 static IMonoTlsProvider currentProvider;
128 #if SECURITY_DEP && !MONO_FEATURE_NEW_SYSTEM_SOURCE
130 #region Mono.Security visible API
133 * "Public" section, intended to be consumed via reflection.
135 * Mono.Security.dll provides a public wrapper around these.
138 internal static MSI.MonoTlsProvider GetProvider ()
140 var provider = GetProviderInternal ();
141 if (provider == null)
142 throw new NotSupportedException ("No TLS Provider available.");
144 return provider.Provider;
147 internal static MSI.MonoTlsProvider GetDefaultProvider ()
149 var provider = GetDefaultProviderInternal ();
150 if (provider == null)
151 throw new NotSupportedException ("No TLS Provider available.");
153 return provider.Provider;
156 internal static bool HasProvider {
159 return currentProvider != null;
164 internal static void InstallProvider (MSI.MonoTlsProvider provider)
167 currentProvider = new Private.MonoTlsProviderWrapper (provider);
171 internal static HttpWebRequest CreateHttpsRequest (Uri requestUri, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
174 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
175 return new HttpWebRequest (requestUri, internalProvider, settings);
179 internal static HttpListener CreateHttpListener (X509Certificate2 certificate, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
182 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
183 return new HttpListener (certificate, internalProvider, settings);