Merge pull request #2237 from xmcclure/container-owner
[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                 public abstract Guid ID {
76                         get;
77                 }
78
79                 public abstract string Name {
80                         get;
81                 }
82
83 #region SslStream
84
85                 /*
86                  * This section abstracts the @SslStream class.
87                  *
88                  */
89
90                 public abstract bool SupportsSslStream {
91                         get;
92                 }
93
94                 /*
95                  * Whether or not this TLS Provider supports Mono-specific extensions
96                  * (via @MonoTlsSettings).
97                  */
98                 public abstract bool SupportsMonoExtensions {
99                         get;
100                 }
101
102                 public abstract SslProtocols SupportedProtocols {
103                         get;
104                 }
105
106                 /*
107                  * Obtain a @IMonoSslStream instance.
108                  *
109                  */
110                 public abstract IMonoSslStream CreateSslStream (
111                         Stream innerStream, bool leaveInnerStreamOpen,
112                         MonoTlsSettings settings = null);
113
114 #endregion
115
116 #region Certificate Validation
117
118                 /*
119                  * Allows a TLS provider to provide a custom system certificiate validator.
120                  */
121                 public virtual bool HasCustomSystemCertificateValidator {
122                         get { return false; }
123                 }
124
125                 /*
126                  * If @serverMode is true, then we're a server and want to validate a certificate
127                  * that we received from a client.
128                  *
129                  * On OS X and Mobile, the @chain will be initialized with the @certificates, but not actually built.
130                  *
131                  * Returns `true` if certificate validation has been performed and `false` to invoke the
132                  * default system validator.
133                  */
134                 public virtual bool InvokeSystemCertificateValidator (
135                         ICertificateValidator validator, string targetHost, bool serverMode,
136                         X509CertificateCollection certificates, X509Chain chain, out bool success,
137                         ref MonoSslPolicyErrors errors, ref int status11)
138                 {
139                         success = false;
140                         return false;
141                 }
142
143 #endregion
144
145 #region Manged SSPI
146
147                 /*
148                  * The managed SSPI implementation from the new TLS code.
149                  */
150
151                 public abstract bool SupportsTlsContext {
152                         get;
153                 }
154
155                 public abstract IMonoTlsContext CreateTlsContext (
156                         string hostname, bool serverMode, TlsProtocols protocolFlags,
157                         X509Certificate serverCertificate, X509CertificateCollection clientCertificates,
158                         bool remoteCertRequired, MonoEncryptionPolicy encryptionPolicy,
159                         MonoTlsSettings settings);
160
161 #endregion
162         }
163 }