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