[Mono.Security]: Add 'X509Chain' to ICertificateValidator.InvokeSystemValidator and...
[mono.git] / mcs / class / Mono.Security / Mono.Security.Interface / MonoTlsProvider.cs
1 //
2 // MonoTlsProvider.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 using System;
28 using System.IO;
29 using System.Net;
30 using System.Net.Security;
31 using System.Security.Authentication;
32 using System.Security.Cryptography.X509Certificates;
33 using Mono.Security.Protocol.Tls;
34
35 namespace Mono.Security.Interface
36 {
37         /*
38          * Unfortunately, we can't use the public definitions from System.dll here, so we need to
39          * copy these.
40          *
41          * The @MonoRemoteCertificateValidationCallback also has an additional 'targetHost' argument.
42          *
43          */
44
45         [Flags]
46         public enum MonoSslPolicyErrors
47         {
48                 None = 0,
49                 RemoteCertificateNotAvailable = 1,
50                 RemoteCertificateNameMismatch = 2,
51                 RemoteCertificateChainErrors = 4,
52         }
53
54         public enum MonoEncryptionPolicy
55         {
56                 // Prohibit null ciphers (current system defaults)
57                 RequireEncryption = 0,
58
59                 // Add null ciphers to current system defaults
60                 AllowNoEncryption,
61
62                 // Request null ciphers only
63                 NoEncryption
64         }
65
66         public delegate bool MonoRemoteCertificateValidationCallback (
67                 string targetHost, X509Certificate certificate, X509Chain chain, MonoSslPolicyErrors sslPolicyErrors);
68
69         public delegate X509Certificate MonoLocalCertificateSelectionCallback (
70                 string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
71                 string[] acceptableIssuers);
72
73         public abstract class MonoTlsProvider
74         {
75 #region SslStream
76
77                 /*
78                  * This section abstracts the @SslStream class.
79                  *
80                  */
81
82                 public abstract bool SupportsSslStream {
83                         get;
84                 }
85
86                 /*
87                  * Whether or not this TLS Provider supports Mono-specific extensions
88                  * (via @MonoTlsSettings).
89                  */
90                 public abstract bool SupportsMonoExtensions {
91                         get;
92                 }
93
94                 public abstract SslProtocols SupportedProtocols {
95                         get;
96                 }
97
98                 /*
99                  * Obtain a @MonoSslStream instance.
100                  *
101                  */
102                 public abstract MonoSslStream CreateSslStream (
103                         Stream innerStream, bool leaveInnerStreamOpen,
104                         MonoTlsSettings settings = null);
105
106 #endregion
107
108 #region Certificate Validation
109
110                 /*
111                  * Allows a TLS provider to provide a custom system certificiate validator.
112                  */
113                 public virtual bool HasCustomSystemCertificateValidator {
114                         get { return false; }
115                 }
116
117                 /*
118                  * If @serverMode is true, then we're a server and want to validate a certificate
119                  * that we received from a client.
120                  *
121                  * On OS X and Mobile, the @chain will be initialized with the @certificates, but not actually built.
122                  *
123                  * Returns `true` if certificate validation has been performed and `false` to invoke the
124                  * default system validator.
125                  */
126                 public virtual bool InvokeSystemCertificateValidator (
127                         ICertificateValidator validator, string targetHost, bool serverMode,
128                         X509CertificateCollection certificates, X509Chain chain, out bool success,
129                         ref MonoSslPolicyErrors errors, ref int status11)
130                 {
131                         success = false;
132                         return false;
133                 }
134
135 #endregion
136
137 #region Manged SSPI
138
139                 /*
140                  * The managed SSPI implementation from the new TLS code.
141                  */
142
143                 public abstract bool SupportsTlsContext {
144                         get;
145                 }
146
147                 public abstract IMonoTlsContext CreateTlsContext (
148                         string hostname, bool serverMode, TlsProtocols protocolFlags,
149                         X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
150                         bool remoteCertRequired, MonoEncryptionPolicy encryptionPolicy,
151                         MonoTlsSettings settings);
152
153 #endregion
154         }
155 }