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;
41 namespace Mono.Net.Security
44 * Keep in sync with Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs.
47 static class MonoTlsProviderFactory
52 * APIs in this section are for consumption within System.dll only - do not access via
53 * reflection or from friend assemblies.
55 * @IMonoTlsProvider is defined as empty interface outside 'SECURITY_DEP', so we don't need
56 * this conditional here.
59 internal static IMonoTlsProvider GetProviderInternal ()
62 if (currentProvider != null)
63 return currentProvider;
66 defaultProvider = CreateDefaultProvider ();
67 } catch (Exception ex) {
68 throw new NotSupportedException ("TLS Support not available.", ex);
71 if (defaultProvider == null)
72 throw new NotSupportedException ("TLS Support not available.");
74 currentProvider = defaultProvider;
75 return currentProvider;
79 internal static IMonoTlsProvider GetDefaultProviderInternal ()
82 if (defaultProvider != null)
83 return defaultProvider;
86 defaultProvider = CreateDefaultProvider ();
87 } catch (Exception ex) {
88 throw new NotSupportedException ("TLS Support not available.", ex);
91 if (defaultProvider == null)
92 throw new NotSupportedException ("TLS Support not available.");
94 return defaultProvider;
98 static IMonoTlsProvider CreateDefaultProvider ()
101 #if MONO_FEATURE_NEW_SYSTEM_SOURCE
103 * This is a hack, which is used in the Mono.Security.Providers.NewSystemSource
104 * assembly, which will provide a "fake" System.dll. Use the public Mono.Security
105 * API to get the "real" System.dll's provider via reflection, then wrap it with
106 * the "fake" version's perceived view.
108 * NewSystemSource needs to compile MonoTlsProviderFactory.cs, IMonoTlsProvider.cs,
109 * MonoTlsProviderWrapper.cs and CallbackHelpers.cs from this directory and only these.
111 var userProvider = MSI.MonoTlsProviderFactory.GetProvider ();
112 return new Private.MonoTlsProviderWrapper (userProvider);
114 return new Private.MonoDefaultTlsProvider ();
121 static object locker = new object ();
122 static IMonoTlsProvider defaultProvider;
123 static IMonoTlsProvider currentProvider;
127 #if SECURITY_DEP && !MONO_FEATURE_NEW_SYSTEM_SOURCE
129 #region Mono.Security visible API
132 * "Public" section, intended to be consumed via reflection.
134 * Mono.Security.dll provides a public wrapper around these.
137 internal static MSI.MonoTlsProvider GetProvider ()
139 var provider = GetProviderInternal ();
140 if (provider == null)
141 throw new NotSupportedException ("No TLS Provider available.");
143 return provider.Provider;
146 internal static MSI.MonoTlsProvider GetDefaultProvider ()
148 var provider = GetDefaultProviderInternal ();
149 if (provider == null)
150 throw new NotSupportedException ("No TLS Provider available.");
152 return provider.Provider;
155 internal static bool HasProvider {
158 return currentProvider != null;
163 internal static void InstallProvider (MSI.MonoTlsProvider provider)
166 currentProvider = new Private.MonoTlsProviderWrapper (provider);
170 internal static HttpWebRequest CreateHttpsRequest (Uri requestUri, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
173 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
174 return new HttpWebRequest (requestUri, internalProvider, settings);