[Mono.Security]: Add new MonoTlsProviderFactory.CreateHttpListener() API to create...
[mono.git] / mcs / class / System / Mono.Net.Security / 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
27 #if SECURITY_DEP
28 #if MONO_SECURITY_ALIAS
29 extern alias MonoSecurity;
30 using MSI = MonoSecurity::Mono.Security.Interface;
31 using MX = MonoSecurity::Mono.Security.X509;
32 #else
33 using MSI = Mono.Security.Interface;
34 using MX = Mono.Security.X509;
35 #endif
36 using System.Security.Cryptography.X509Certificates;
37 #endif
38
39 using System;
40 using System.Net;
41
42 namespace Mono.Net.Security
43 {
44         /*
45          * Keep in sync with Mono.Security/Mono.Security.Interface/MonoTlsProvider.cs.
46          *
47          */
48         static class MonoTlsProviderFactory
49         {
50                 #region Internal API
51
52                 /*
53                  * APIs in this section are for consumption within System.dll only - do not access via
54                  * reflection or from friend assemblies.
55                  * 
56                  * @IMonoTlsProvider is defined as empty interface outside 'SECURITY_DEP', so we don't need
57                  * this conditional here.
58                  */
59
60                 internal static IMonoTlsProvider GetProviderInternal ()
61                 {
62                         lock (locker) {
63                                 if (currentProvider != null)
64                                         return currentProvider;
65
66                                 try {
67                                         defaultProvider = CreateDefaultProvider ();
68                                 } catch (Exception ex) {
69                                         throw new NotSupportedException ("TLS Support not available.", ex);
70                                 }
71
72                                 if (defaultProvider == null)
73                                         throw new NotSupportedException ("TLS Support not available.");
74
75                                 currentProvider = defaultProvider;
76                                 return currentProvider;
77                         }
78                 }
79
80                 internal static IMonoTlsProvider GetDefaultProviderInternal ()
81                 {
82                         lock (locker) {
83                                 if (defaultProvider != null)
84                                         return defaultProvider;
85
86                                 try {
87                                         defaultProvider = CreateDefaultProvider ();
88                                 } catch (Exception ex) {
89                                         throw new NotSupportedException ("TLS Support not available.", ex);
90                                 }
91
92                                 if (defaultProvider == null)
93                                         throw new NotSupportedException ("TLS Support not available.");
94
95                                 return defaultProvider;
96                         }
97                 }
98
99                 static IMonoTlsProvider CreateDefaultProvider ()
100                 {
101 #if SECURITY_DEP
102 #if MONO_FEATURE_NEW_SYSTEM_SOURCE
103                         /*
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.
108                          *
109                          * NewSystemSource needs to compile MonoTlsProviderFactory.cs, IMonoTlsProvider.cs,
110                          * MonoTlsProviderWrapper.cs and CallbackHelpers.cs from this directory and only these.
111                          */
112                         var userProvider = MSI.MonoTlsProviderFactory.GetProvider ();
113                         return new Private.MonoTlsProviderWrapper (userProvider);
114 #else
115                         return new Private.MonoDefaultTlsProvider ();
116 #endif
117 #else
118                         return null;
119 #endif
120                 }
121
122                 static object locker = new object ();
123                 static IMonoTlsProvider defaultProvider;
124                 static IMonoTlsProvider currentProvider;
125
126                 #endregion
127
128 #if SECURITY_DEP && !MONO_FEATURE_NEW_SYSTEM_SOURCE
129
130                 #region Mono.Security visible API
131
132                 /*
133                  * "Public" section, intended to be consumed via reflection.
134                  * 
135                  * Mono.Security.dll provides a public wrapper around these.
136                  */
137
138                 internal static MSI.MonoTlsProvider GetProvider ()
139                 {
140                         var provider = GetProviderInternal ();
141                         if (provider == null)
142                                 throw new NotSupportedException ("No TLS Provider available.");
143
144                         return provider.Provider;
145                 }
146
147                 internal static MSI.MonoTlsProvider GetDefaultProvider ()
148                 {
149                         var provider = GetDefaultProviderInternal ();
150                         if (provider == null)
151                                 throw new NotSupportedException ("No TLS Provider available.");
152
153                         return provider.Provider;
154                 }
155
156                 internal static bool HasProvider {
157                         get {
158                                 lock (locker) {
159                                         return currentProvider != null;
160                                 }
161                         }
162                 }
163
164                 internal static void InstallProvider (MSI.MonoTlsProvider provider)
165                 {
166                         lock (locker) {
167                                 currentProvider = new Private.MonoTlsProviderWrapper (provider);
168                         }
169                 }
170
171                 internal static HttpWebRequest CreateHttpsRequest (Uri requestUri, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
172                 {
173                         lock (locker) {
174                                 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
175                                 return new HttpWebRequest (requestUri, internalProvider, settings);
176                         }
177                 }
178
179                 internal static HttpListener CreateHttpListener (X509Certificate2 certificate, MSI.MonoTlsProvider provider, MSI.MonoTlsSettings settings)
180                 {
181                         lock (locker) {
182                                 var internalProvider = provider != null ? new Private.MonoTlsProviderWrapper (provider) : null;
183                                 return new HttpListener (certificate, internalProvider, settings);
184                         }
185                 }
186                 #endregion
187
188 #endif
189
190         }
191 }
192