Merge pull request #2721 from ludovic-henry/fix-mono_ms_ticks
[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                 internal MonoTlsProvider ()
76                 {
77                 }
78
79                 public abstract Guid ID {
80                         get;
81                 }
82
83                 public abstract string Name {
84                         get;
85                 }
86
87 #region SslStream
88
89                 /*
90                  * This section abstracts the @SslStream class.
91                  *
92                  */
93
94                 public abstract bool SupportsSslStream {
95                         get;
96                 }
97
98                 /*
99                  * Does this provider support IMonoSslStream.GetConnectionInfo() ?
100                  */
101                 public abstract bool SupportsConnectionInfo {
102                         get;
103                 }
104
105                 /*
106                  * Whether or not this TLS Provider supports Mono-specific extensions
107                  * (via @MonoTlsSettings).
108                  */
109                 public abstract bool SupportsMonoExtensions {
110                         get;
111                 }
112
113                 public abstract SslProtocols SupportedProtocols {
114                         get;
115                 }
116
117                 /*
118                  * Obtain a @IMonoSslStream instance.
119                  *
120                  */
121                 public abstract IMonoSslStream CreateSslStream (
122                         Stream innerStream, bool leaveInnerStreamOpen,
123                         MonoTlsSettings settings = null);
124
125 #endregion
126
127 #region Native Certificate Implementation
128
129                 internal virtual bool HasNativeCertificates {
130                         get { return false; }
131                 }
132
133                 internal virtual X509Certificate2Impl GetNativeCertificate (
134                         byte[] data, string password, X509KeyStorageFlags flags)
135                 {
136                         throw new InvalidOperationException ();
137                 }
138
139                 internal virtual X509Certificate2Impl GetNativeCertificate (
140                         X509Certificate certificate)
141                 {
142                         throw new InvalidOperationException ();
143                 }
144
145 #endregion
146
147 #region Certificate Validation
148
149                 /*
150                  * Allows a TLS provider to provide a custom system certificiate validator.
151                  */
152                 internal virtual bool HasCustomSystemCertificateValidator {
153                         get { return false; }
154                 }
155
156                 /*
157                  * If @serverMode is true, then we're a server and want to validate a certificate
158                  * that we received from a client.
159                  *
160                  * On OS X and Mobile, the @chain will be initialized with the @certificates, but not actually built.
161                  *
162                  * Returns `true` if certificate validation has been performed and `false` to invoke the
163                  * default system validator.
164                  */
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)
169                 {
170                         throw new InvalidOperationException ();
171                 }
172
173 #endregion
174
175 #region Manged SSPI
176
177                 /*
178                  * The managed SSPI implementation from the new TLS code.
179                  */
180
181                 internal abstract bool SupportsTlsContext {
182                         get;
183                 }
184
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);
190
191 #endregion
192         }
193 }