[Mono.Security]: Add internal 'MonoTlsProviderFactory.InternalVersion' constant....
[mono.git] / mcs / class / Mono.Security / Mono.Security.Interface / MonoTlsProviderFactory.cs
1 //
2 // MonoTlsProviderFactory.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2015 Xamarin, Inc.
8 //
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:
15 //
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
18 //
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
25 // THE SOFTWARE.
26 using System;
27 using System.Net;
28 using System.Net.Security;
29 using System.Security.Cryptography.X509Certificates;
30 using Mono.Net.Security;
31
32 namespace Mono.Security.Interface
33 {
34         /*
35          * Public API front-end to System.dll's version.
36          *
37          * Keep in sync with System/Mono.Net.Security/MonoTlsProviderFactory.cs.
38          */
39         public static partial class MonoTlsProviderFactory
40         {
41                 /*
42                  * TLS Provider Initialization
43                  * ===========================
44                  * 
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).
47                  * 
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.
52                  * 
53                  * On desktop, the default provider can be specified with the MONO_TLS_PROVIDER
54                  * environment variable.  The following options are currently supported:
55                  * 
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).
60                  * 
61                  * On all platforms (except mobile with linker), you can call
62                  * 
63                  *     MonoTlsProviderFactory.Initialize(string)
64                  * 
65                  * to use a different provider.
66                  * 
67                  */
68
69                 #region Provider Initialization
70
71                 /*
72                  * Returns the global @MonoTlsProvider, initializing the TLS Subsystem if necessary.
73                  *
74                  * This method throws @NotSupportedException if no TLS Provider can be found.
75                  */
76                 public static MonoTlsProvider GetProvider ()
77                 {
78                         return (MonoTlsProvider)NoReflectionHelper.GetProvider ();
79                 }
80
81                 /*
82                  * Check whether the TLS Subsystem is initialized.
83                  */
84                 public static bool IsInitialized {
85                         get {
86                                 return NoReflectionHelper.IsInitialized;
87                         }
88                 }
89
90                 /*
91                  * Initialize the TLS Subsystem.
92                  * 
93                  * This method may be called at any time.  It ensures that the TLS Subsystem is
94                  * initialized and a provider available.
95                  */
96                 public static void Initialize ()
97                 {
98                         NoReflectionHelper.Initialize ();
99                 }
100
101                 /*
102                  * Initialize the TLS Subsystem with a specific provider.
103                  * 
104                  * May only be called at application startup (before any of the TLS / Certificate
105                  * APIs have been used).
106                  * 
107                  * Throws @NotSupportedException if the TLS Subsystem is already initialized
108                  * (@IsInitialized returns true) or the requested provider is not supported.
109                  * 
110                  * On mobile, this will always throw @NotSupportedException when using the linker.
111                  */
112                 public static void Initialize (string provider)
113                 {
114                         NoReflectionHelper.Initialize (provider);
115                 }
116
117                 /*
118                  * Checks whether @provider is supported.
119                  *
120                  * On mobile, this will always return false when using the linker.
121                  */
122                 public static bool IsProviderSupported (string provider)
123                 {
124                         return NoReflectionHelper.IsProviderSupported (provider);
125                 }
126
127                 #endregion
128
129                 #region Call-by-call selection
130
131                 /*
132                  * Returns the requested TLS Provider, for use with the call-by-call APIs below.
133                  * 
134                  * Throw @NotSupportedException if the requested provider is not supported or
135                  * when using the linker on mobile.
136                  */
137                 public static MonoTlsProvider GetProvider (string provider)
138                 {
139                         return (MonoTlsProvider)NoReflectionHelper.GetProvider (provider);
140                 }
141
142                 /*
143                  * Create @HttpWebRequest with the specified @provider (may be null to use the default one).
144                  * 
145                  * NOTE: This needs to be written as "System.Uri" to avoid ambiguity with Mono.Security.Uri in the
146                  *        mobile build.
147                  * 
148                  */
149                 public static HttpWebRequest CreateHttpsRequest (System.Uri requestUri, MonoTlsProvider provider, MonoTlsSettings settings = null)
150                 {
151                         return NoReflectionHelper.CreateHttpsRequest (requestUri, provider, settings);
152                 }
153
154                 public static HttpListener CreateHttpListener (X509Certificate certificate, MonoTlsProvider provider = null, MonoTlsSettings settings = null)
155                 {
156                         return (HttpListener)NoReflectionHelper.CreateHttpListener (certificate, provider, settings);
157                 }
158
159                 public static IMonoSslStream GetMonoSslStream (SslStream stream)
160                 {
161                         return (IMonoSslStream)NoReflectionHelper.GetMonoSslStream (stream);
162                 }
163
164                 public static IMonoSslStream GetMonoSslStream (HttpListenerContext context)
165                 {
166                         return (IMonoSslStream)NoReflectionHelper.GetMonoSslStream (context);
167                 }
168
169                 #endregion
170
171                 #region Internal Version
172
173                 /*
174                  * Internal version number (not in any way related to the TLS Version).
175                  *
176                  * Used by the web-tests to check whether
177                  * the current Mono contains certain features or bug fixes.
178                  *
179                  * Negative version numbers are reserved for martin work branches.
180                  *
181                  */
182                 internal const int InternalVersion = 1;
183
184                 #endregion
185         }
186 }
187