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 using System.Net.Security;
29 using System.Security.Cryptography.X509Certificates;
30 using Mono.Net.Security;
32 namespace Mono.Security.Interface
35 * Public API front-end to System.dll's version.
37 * Keep in sync with System/Mono.Net.Security/MonoTlsProviderFactory.cs.
39 public static partial class MonoTlsProviderFactory
42 * TLS Provider Initialization
43 * ===========================
45 * The "global" TLS Provider (returned by GetProvider()) may only be modified at
46 * application startup (before any of the TLS / Certificate code has been used).
48 * On mobile, the default provider is specified at compile time using a property
49 * in the .csproj file (which can be set from the IDE). When using the linker, all
50 * other providers will be linked-out, so you won't be able to choose a different
51 * provider at run-time.
53 * On desktop, the default provider can be specified with the MONO_TLS_PROVIDER
54 * environment variable. The following options are currently supported:
56 * "default" - let Mono pick the best one for you (recommended)
57 * "old" or "legacy" - Mono's old managed TLS implementation
58 * "appletls" (currently XamMac only, set via .csproj property)
59 * "btls" - the new boringssl based provider (coming soon).
61 * On all platforms (except mobile with linker), you can call
63 * MonoTlsProviderFactory.Initialize(string)
65 * to use a different provider.
69 #region Provider Initialization
72 * Returns the global @MonoTlsProvider, initializing the TLS Subsystem if necessary.
74 * This method throws @NotSupportedException if no TLS Provider can be found.
76 public static MonoTlsProvider GetProvider ()
78 return (MonoTlsProvider)NoReflectionHelper.GetProvider ();
82 * Check whether the TLS Subsystem is initialized.
84 public static bool IsInitialized {
86 return NoReflectionHelper.IsInitialized;
91 * Initialize the TLS Subsystem.
93 * This method may be called at any time. It ensures that the TLS Subsystem is
94 * initialized and a provider available.
96 public static void Initialize ()
98 NoReflectionHelper.Initialize ();
102 * Initialize the TLS Subsystem with a specific provider.
104 * May only be called at application startup (before any of the TLS / Certificate
105 * APIs have been used).
107 * Throws @NotSupportedException if the TLS Subsystem is already initialized
108 * (@IsInitialized returns true) or the requested provider is not supported.
110 * On mobile, this will always throw @NotSupportedException when using the linker.
112 public static void Initialize (string provider)
114 NoReflectionHelper.Initialize (provider);
118 * Checks whether @provider is supported.
120 * On mobile, this will always return false when using the linker.
122 public static bool IsProviderSupported (string provider)
124 return NoReflectionHelper.IsProviderSupported (provider);
129 #region Call-by-call selection
132 * Returns the requested TLS Provider, for use with the call-by-call APIs below.
134 * Throw @NotSupportedException if the requested provider is not supported or
135 * when using the linker on mobile.
137 public static MonoTlsProvider GetProvider (string provider)
139 return (MonoTlsProvider)NoReflectionHelper.GetProvider (provider);
143 * Create @HttpWebRequest with the specified @provider (may be null to use the default one).
145 * NOTE: This needs to be written as "System.Uri" to avoid ambiguity with Mono.Security.Uri in the
149 public static HttpWebRequest CreateHttpsRequest (System.Uri requestUri, MonoTlsProvider provider, MonoTlsSettings settings = null)
151 return NoReflectionHelper.CreateHttpsRequest (requestUri, provider, settings);
154 public static HttpListener CreateHttpListener (X509Certificate certificate, MonoTlsProvider provider = null, MonoTlsSettings settings = null)
156 return (HttpListener)NoReflectionHelper.CreateHttpListener (certificate, provider, settings);
159 public static IMonoSslStream GetMonoSslStream (SslStream stream)
161 return (IMonoSslStream)NoReflectionHelper.GetMonoSslStream (stream);
164 public static IMonoSslStream GetMonoSslStream (HttpListenerContext context)
166 return (IMonoSslStream)NoReflectionHelper.GetMonoSslStream (context);
171 #region Internal Version
174 * Internal version number (not in any way related to the TLS Version).
176 * Used by the web-tests to check whether
177 * the current Mono contains certain features or bug fixes.
179 * Negative version numbers are reserved for martin work branches.
182 internal const int InternalVersion = 1;