1 //
2 // LegacyTlsProvider.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 #if SECURITY_DEP
27 #if MONO_SECURITY_ALIAS
28 extern alias MonoSecurity;
29 #endif
30 
31 #if MONO_SECURITY_ALIAS
32 using MSI = MonoSecurity::Mono.Security.Interface;
33 #else
34 using MSI = Mono.Security.Interface;
35 #endif
36 
37 using System;
38 using System.IO;
39 using System.Net;
40 using System.Net.Security;
41 using System.Security.Cryptography.X509Certificates;
42 using System.Security.Authentication;
43 
44 namespace Mono.Net.Security
45 {
46 	/*
47 	 * Strictly private - do not use outside the Mono.Net.Security directory.
48 	 */
49 	class LegacyTlsProvider : MSI.MonoTlsProvider
50 	{
51 		public override Guid ID {
52 			get { return MonoTlsProviderFactory.LegacyId; }
53 		}
54 
55 		public override string Name {
56 			get { return "legacy"; }
57 		}
58 
59 		public override bool SupportsSslStream {
60 			get { return true; }
61 		}
62 
63 		public override bool SupportsConnectionInfo {
64 			get { return false; }
65 		}
66 
67 		public override bool SupportsMonoExtensions {
68 			get { return false; }
69 		}
70 
71 		internal override bool SupportsCleanShutdown {
72 			get { return false; }
73 		}
74 
75 		public override SslProtocols SupportedProtocols {
76 			get { return SslProtocols.Tls; }
77 		}
78 
CreateSslStream( Stream innerStream, bool leaveInnerStreamOpen, MSI.MonoTlsSettings settings = null)79 		public override MSI.IMonoSslStream CreateSslStream (
80 			Stream innerStream, bool leaveInnerStreamOpen,
81 			MSI.MonoTlsSettings settings = null)
82 		{
83 			return SslStream.CreateMonoSslStream (innerStream, leaveInnerStreamOpen, this, settings);
84 		}
85 
CreateSslStreamInternal( SslStream sslStream, Stream innerStream, bool leaveInnerStreamOpen, MSI.MonoTlsSettings settings)86 		internal override MSI.IMonoSslStream CreateSslStreamInternal (
87 			SslStream sslStream, Stream innerStream, bool leaveInnerStreamOpen,
88 			MSI.MonoTlsSettings settings)
89 		{
90 			return new Private.LegacySslStream (innerStream, leaveInnerStreamOpen, sslStream, this, settings);
91 		}
92 
ValidateCertificate( MSI.ICertificateValidator2 validator, string targetHost, bool serverMode, X509CertificateCollection certificates, bool wantsChain, ref X509Chain chain, ref MSI.MonoSslPolicyErrors errors, ref int status11)93 		internal override bool ValidateCertificate (
94 			MSI.ICertificateValidator2 validator, string targetHost, bool serverMode,
95 			X509CertificateCollection certificates, bool wantsChain, ref X509Chain chain,
96 			ref MSI.MonoSslPolicyErrors errors, ref int status11)
97 		{
98 			if (wantsChain)
99 				chain = SystemCertificateValidator.CreateX509Chain (certificates);
100 			var xerrors = (SslPolicyErrors)errors;
101 			var result = SystemCertificateValidator.Evaluate (validator.Settings, targetHost, certificates, chain, ref xerrors, ref status11);
102 			errors = (MSI.MonoSslPolicyErrors)xerrors;
103 			return result;
104 		}
105 	}
106 }
107 #endif
108 
109