[sgen] Untag the vtable during concurrent mark
[mono.git] / mcs / class / Mono.Security / Mono.Security.Interface / CertificateValidationHelper.cs
1 //
2 // CertificateValidationHelper.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.Threading;
32 using System.Security.Cryptography.X509Certificates;
33 using Mono.Security.Protocol.Tls;
34 using MX = Mono.Security.X509;
35 using Mono.Net.Security;
36
37 namespace Mono.Security.Interface
38 {
39         public class ValidationResult
40         {
41                 bool trusted;
42                 bool user_denied;
43                 int error_code;
44                 MonoSslPolicyErrors? policy_errors;
45
46                 public ValidationResult (bool trusted, bool user_denied, int error_code, MonoSslPolicyErrors? policy_errors)
47                 {
48                         this.trusted = trusted;
49                         this.user_denied = user_denied;
50                         this.error_code = error_code;
51                         this.policy_errors = policy_errors;
52                 }
53
54                 internal ValidationResult (bool trusted, bool user_denied, int error_code)
55                 {
56                         this.trusted = trusted;
57                         this.user_denied = user_denied;
58                         this.error_code = error_code;
59                 }
60
61                 public bool Trusted {
62                         get { return trusted; }
63                 }
64
65                 public bool UserDenied {
66                         get { return user_denied; }
67                 }
68
69                 public int ErrorCode {
70                         get { return error_code; }
71                 }
72
73                 public MonoSslPolicyErrors? PolicyErrors {
74                         get { return policy_errors; }
75                 }
76         }
77
78         /**
79          * Internal interface - do not implement
80          */
81         public interface ICertificateValidator
82         {
83                 MonoTlsSettings Settings {
84                         get;
85                 }
86
87                 /*
88                  * Returns `true` if a client certificate has been selected (which could be `null`).
89                  */
90                 bool SelectClientCertificate (
91                         string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
92                         string[] acceptableIssuers, out X509Certificate clientCertificate);
93
94                 /*
95                  * If @serverMode is true, then we're a server and want to validate a certificate that we received from a client.
96                  */
97                 ValidationResult ValidateCertificate (string targetHost, bool serverMode, X509CertificateCollection certificates);
98         }
99
100         internal interface ICertificateValidator2 : ICertificateValidator
101         {
102                 /*
103                  * Internal use only.
104                  */
105                 ValidationResult ValidateCertificate (string targetHost, bool serverMode, X509Certificate leaf, X509Chain chain);
106
107                 /*
108                  * On OS X and Mobile, the @chain will be initialized with the @certificates, but not actually built.
109                  */
110                 bool InvokeSystemValidator (
111                         string targetHost, bool serverMode, X509CertificateCollection certificates,
112                         X509Chain chain, ref MonoSslPolicyErrors errors, ref int status11);
113         }
114
115         public static class CertificateValidationHelper
116         {
117                 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
118                 static readonly bool noX509Chain;
119                 static readonly bool supportsTrustAnchors;
120
121                 static CertificateValidationHelper ()
122                 {
123                         #if MONOTOUCH || XAMMAC
124                         noX509Chain = true;
125                         supportsTrustAnchors = true;
126                         #elif MONODROID
127                         noX509Chain = true;
128                         supportsTrustAnchors = false;
129                         #else
130                         if (File.Exists (SecurityLibrary)) {
131                                 noX509Chain = true;
132                                 supportsTrustAnchors = true;
133                         } else {
134                                 noX509Chain = false;
135                                 supportsTrustAnchors = false;
136                         }
137                         #endif
138                 }
139
140                 public static bool SupportsX509Chain {
141                         get { return !noX509Chain; }
142                 }
143
144                 public static bool SupportsTrustAnchors {
145                         get { return supportsTrustAnchors; }
146                 }
147
148                 /*
149                  * Internal API, intended to be used by MonoTlsProvider implementations.
150                  */
151                 internal static ICertificateValidator2 GetDefaultValidator (MonoTlsSettings settings, MonoTlsProvider provider)
152                 {
153                         return (ICertificateValidator2)NoReflectionHelper.GetDefaultCertificateValidator (provider, settings);
154                 }
155
156                 /*
157                  * Use this overloaded version in user code.
158                  */
159                 public static ICertificateValidator GetValidator (MonoTlsSettings settings, MonoTlsProvider provider = null)
160                 {
161                         return GetDefaultValidator (settings, provider);
162                 }
163         }
164 }