Merge pull request #4222 from alexanderkyte/fix_mismatch_com_disabled
[mono.git] / mcs / class / System / Mono.Btls / MonoBtlsProvider.cs
1 //
2 // MonoBtlsProvider.cs
3 //
4 // Author:
5 //       Martin Baulig <martin.baulig@xamarin.com>
6 //
7 // Copyright (c) 2016 Xamarin Inc. (http://www.xamarin.com)
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 #if SECURITY_DEP && MONO_FEATURE_BTLS
27 #if MONO_SECURITY_ALIAS
28 extern alias MonoSecurity;
29 #endif
30
31 using System;
32 using System.IO;
33 using System.Threading;
34 using System.Threading.Tasks;
35 using System.Security.Cryptography.X509Certificates;
36 using System.Security.Authentication;
37
38 #if MONO_SECURITY_ALIAS
39 using MonoSecurity::Mono.Security.Interface;
40 using MX = MonoSecurity::Mono.Security.X509;
41 #else
42 using Mono.Security.Interface;
43 using MX = Mono.Security.X509;
44 #endif
45
46 using MNS = Mono.Net.Security;
47
48 namespace Mono.Btls
49 {
50         class MonoBtlsProvider : MonoTlsProvider
51         {
52                 static readonly Guid id = new Guid ("432d18c9-9348-4b90-bfbf-9f2a10e1f15b");
53
54                 public override Guid ID {
55                         get { return id; }
56                 }
57                 public override string Name {
58                         get { return "btls"; }
59                 }
60
61                 internal MonoBtlsProvider ()
62                 {
63                         if (!MNS.MonoTlsProviderFactory.IsBtlsSupported ())
64                                 throw new NotSupportedException ("BTLS is not supported in this runtime.");
65                 }
66
67                 public override bool SupportsSslStream {
68                         get { return true; }
69                 }
70
71                 public override bool SupportsMonoExtensions {
72                         get { return true; }
73                 }
74
75                 public override bool SupportsConnectionInfo {
76                         get { return true; }
77                 }
78
79                 public override SslProtocols SupportedProtocols {
80                         get { return SslProtocols.Tls12 | SslProtocols.Tls11 | SslProtocols.Tls; }
81                 }
82
83                 public override IMonoSslStream CreateSslStream (
84                         Stream innerStream, bool leaveInnerStreamOpen,
85                         MonoTlsSettings settings = null)
86                 {
87                         return new MonoBtlsStream (
88                                 innerStream, leaveInnerStreamOpen, settings, this);
89                 }
90
91                 internal override bool HasNativeCertificates {
92                         get { return true; }
93                 }
94
95                 internal override X509Certificate2Impl GetNativeCertificate (
96                         byte[] data, string password, X509KeyStorageFlags flags)
97                 {
98                         var impl = new X509CertificateImplBtls (false);
99                         impl.Import (data, password, flags);
100                         return impl;
101                 }
102
103                 internal override X509Certificate2Impl GetNativeCertificate (
104                         X509Certificate certificate)
105                 {
106                         var impl = certificate.Impl as X509CertificateImplBtls;
107                         if (impl != null)
108                                 return (X509Certificate2Impl)impl.Clone ();
109
110                         var data = certificate.GetRawCertData ();
111                         return new X509CertificateImplBtls (data, MonoBtlsX509Format.DER, false);
112                 }
113
114                 internal static MonoBtlsX509VerifyParam GetVerifyParam (string targetHost, bool serverMode)
115                 {
116                         MonoBtlsX509VerifyParam param;
117                         if (serverMode)
118                                 param = MonoBtlsX509VerifyParam.GetSslClient ();
119                         else
120                                 param = MonoBtlsX509VerifyParam.GetSslServer ();
121
122                         if (targetHost == null)
123                                 return param;
124
125                         try {
126                                 var copy = param.Copy ();
127                                 copy.SetHost (targetHost);
128                                 return copy;
129                         } finally {
130                                 param.Dispose ();
131                         }
132                 }
133
134                 internal override bool ValidateCertificate (
135                         ICertificateValidator2 validator, string targetHost, bool serverMode,
136                         X509CertificateCollection certificates, bool wantsChain, ref X509Chain chain,
137                         ref MonoSslPolicyErrors errors, ref int status11)
138                 {
139                         if (chain != null) {
140                                 var chainImpl = (X509ChainImplBtls)chain.Impl;
141                                 var success = chainImpl.StoreCtx.VerifyResult == 1;
142                                 CheckValidationResult (
143                                         validator, targetHost, serverMode, certificates,
144                                         wantsChain, chain, chainImpl.StoreCtx,
145                                         success, ref errors, ref status11);
146                                 return success;
147                         }
148
149                         using (var store = new MonoBtlsX509Store ())
150                         using (var nativeChain = MonoBtlsProvider.GetNativeChain (certificates))
151                         using (var param = GetVerifyParam (targetHost, serverMode))
152                         using (var storeCtx = new MonoBtlsX509StoreCtx ()) {
153                                 SetupCertificateStore (store);
154
155                                 storeCtx.Initialize (store, nativeChain);
156
157                                 storeCtx.SetVerifyParam (param);
158
159                                 var ret = storeCtx.Verify ();
160
161                                 var success = ret == 1;
162
163                                 if (wantsChain && chain == null) {
164                                         chain = GetManagedChain (nativeChain);
165                                 }
166
167                                 CheckValidationResult (
168                                         validator, targetHost, serverMode, certificates,
169                                         wantsChain, null, storeCtx,
170                                         success, ref errors, ref status11);
171                                 return success;
172                         }
173                 }
174
175                 internal static bool ValidateCertificate (MonoBtlsX509Chain chain, MonoBtlsX509VerifyParam param)
176                 {
177                         using (var store = new MonoBtlsX509Store ())
178                         using (var storeCtx = new MonoBtlsX509StoreCtx ()) {
179                                 SetupCertificateStore (store);
180
181                                 storeCtx.Initialize (store, chain);
182
183                                 if (param != null)
184                                         storeCtx.SetVerifyParam (param);
185
186                                 var ret = storeCtx.Verify ();
187
188                                 return ret == 1;
189                         }
190                 }
191
192                 void CheckValidationResult (
193                         ICertificateValidator validator, string targetHost, bool serverMode,
194                         X509CertificateCollection certificates, bool wantsChain,
195                         X509Chain chain, MonoBtlsX509StoreCtx storeCtx,
196                         bool success, ref MonoSslPolicyErrors errors, ref int status11)
197                 {
198                         if (!success) {
199                                 errors = MonoSslPolicyErrors.RemoteCertificateChainErrors;
200                                 status11 = unchecked((int)0x800B010B);
201                         }
202                 }
203
204                 internal static void SetupCertificateStore (MonoBtlsX509Store store)
205                 {
206 #if MONODROID
207                         store.SetDefaultPaths ();
208                         store.AddAndroidLookup ();
209 #else
210                         var userPath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.UserTrustedRoots);
211                         if (Directory.Exists (userPath))
212                                 store.AddDirectoryLookup (userPath, MonoBtlsX509FileType.PEM);
213                         var machinePath = MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.MachineTrustedRoots);
214                         if (Directory.Exists (machinePath))
215                                 store.AddDirectoryLookup (machinePath, MonoBtlsX509FileType.PEM);
216 #endif
217                 }
218
219                 public static string GetSystemStoreLocation ()
220                 {
221 #if MONODROID
222                         return "/system/etc/security/cacerts";
223 #else
224                         return MonoBtlsX509StoreManager.GetStorePath (MonoBtlsX509StoreType.MachineTrustedRoots);
225 #endif
226                 }
227
228                 public static X509Certificate CreateCertificate (byte[] data, MonoBtlsX509Format format, bool disallowFallback = false)
229                 {
230                         using (var impl = new X509CertificateImplBtls (data, format, disallowFallback)) {
231                                 return new X509Certificate (impl);
232                         }
233                 }
234
235                 public static X509Certificate2 CreateCertificate2 (byte[] data, MonoBtlsX509Format format, bool disallowFallback = false)
236                 {
237                         using (var impl = new X509CertificateImplBtls (data, format, disallowFallback)) {
238                                 return new X509Certificate2 (impl);
239                         }
240                 }
241
242                 public static X509Certificate2 CreateCertificate2 (byte[] data, string password, bool disallowFallback = false)
243                 {
244                         using (var impl = new X509CertificateImplBtls (disallowFallback)) {
245                                 impl.Import (data, password, X509KeyStorageFlags.DefaultKeySet);
246                                 return new X509Certificate2 (impl);
247                         }
248                 }
249
250                 public static X509Certificate CreateCertificate (MonoBtlsX509 x509)
251                 {
252                         using (var impl = new X509CertificateImplBtls (x509, true))
253                                 return new X509Certificate (impl);
254                 }
255
256                 public static X509Chain CreateChain ()
257                 {
258                         using (var impl = new X509ChainImplBtls ())
259                                 return new X509Chain (impl);
260                 }
261
262                 public static X509Chain GetManagedChain (MonoBtlsX509Chain chain)
263                 {
264                         var impl = new X509ChainImplBtls (chain);
265                         return new X509Chain (impl);
266                 }
267
268                 public static MonoBtlsX509 GetBtlsCertificate (X509Certificate certificate)
269                 {
270                         var impl = certificate.Impl as X509CertificateImplBtls;
271                         if (impl != null)
272                                 return impl.X509.Copy ();
273
274                         return MonoBtlsX509.LoadFromData (certificate.GetRawCertData (), MonoBtlsX509Format.DER);
275                 }
276
277                 public static MonoBtlsX509Chain GetNativeChain (X509CertificateCollection certificates)
278                 {
279                         var chain = new MonoBtlsX509Chain ();
280                         try {
281                                 foreach (var cert in certificates) {
282                                         using (var x509 = GetBtlsCertificate (cert))
283                                                 chain.AddCertificate (x509);
284                                 }
285                                 return chain;
286                         } catch {
287                                 chain.Dispose ();
288                                 throw;
289                         }
290                 }
291         }
292 }
293 #endif