5 // Martin Baulig <martin.baulig@xamarin.com>
7 // Copyright (c) 2015 Xamarin, Inc.
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:
16 // The above copyright notice and this permission notice shall be included in
17 // all copies or substantial portions of the Software.
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
30 using System.Net.Security;
31 using System.Security.Authentication;
32 using System.Security.Cryptography.X509Certificates;
33 using Mono.Security.Protocol.Tls;
35 namespace Mono.Security.Interface
38 * Unfortunately, we can't use the public definitions from System.dll here, so we need to
41 * The @MonoRemoteCertificateValidationCallback also has an additional 'targetHost' argument.
46 public enum MonoSslPolicyErrors
49 RemoteCertificateNotAvailable = 1,
50 RemoteCertificateNameMismatch = 2,
51 RemoteCertificateChainErrors = 4,
54 public enum MonoEncryptionPolicy
56 // Prohibit null ciphers (current system defaults)
57 RequireEncryption = 0,
59 // Add null ciphers to current system defaults
62 // Request null ciphers only
66 public delegate bool MonoRemoteCertificateValidationCallback (
67 string targetHost, X509Certificate certificate, X509Chain chain, MonoSslPolicyErrors sslPolicyErrors);
69 public delegate X509Certificate MonoLocalCertificateSelectionCallback (
70 string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
71 string[] acceptableIssuers);
73 public abstract class MonoTlsProvider
75 internal MonoTlsProvider ()
79 public abstract Guid ID {
83 public abstract string Name {
90 * This section abstracts the @SslStream class.
94 public abstract bool SupportsSslStream {
99 * Does this provider support IMonoSslStream.GetConnectionInfo() ?
101 public abstract bool SupportsConnectionInfo {
106 * Whether or not this TLS Provider supports Mono-specific extensions
107 * (via @MonoTlsSettings).
109 public abstract bool SupportsMonoExtensions {
113 public abstract SslProtocols SupportedProtocols {
118 * Obtain a @IMonoSslStream instance.
121 public abstract IMonoSslStream CreateSslStream (
122 Stream innerStream, bool leaveInnerStreamOpen,
123 MonoTlsSettings settings = null);
127 #region Native Certificate Implementation
129 internal virtual bool HasNativeCertificates {
130 get { return false; }
133 internal virtual X509Certificate2Impl GetNativeCertificate (
134 byte[] data, string password, X509KeyStorageFlags flags)
136 throw new InvalidOperationException ();
139 internal virtual X509Certificate2Impl GetNativeCertificate (
140 X509Certificate certificate)
142 throw new InvalidOperationException ();
147 #region Certificate Validation
150 * Allows a TLS provider to provide a custom system certificiate validator.
152 internal virtual bool HasCustomSystemCertificateValidator {
153 get { return false; }
157 * If @serverMode is true, then we're a server and want to validate a certificate
158 * that we received from a client.
160 * On OS X and Mobile, the @chain will be initialized with the @certificates, but not actually built.
162 * Returns `true` if certificate validation has been performed and `false` to invoke the
163 * default system validator.
165 internal virtual bool InvokeSystemCertificateValidator (
166 ICertificateValidator2 validator, string targetHost, bool serverMode,
167 X509CertificateCollection certificates, bool wantsChain, ref X509Chain chain,
168 out bool success, ref MonoSslPolicyErrors errors, ref int status11)
170 throw new InvalidOperationException ();
178 * The managed SSPI implementation from the new TLS code.
181 internal abstract bool SupportsTlsContext {
185 internal abstract IMonoTlsContext CreateTlsContext (
186 string hostname, bool serverMode, TlsProtocols protocolFlags,
187 X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
188 bool remoteCertRequired, MonoEncryptionPolicy encryptionPolicy,
189 MonoTlsSettings settings);