2 // CertificateValidationHelper.cs
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.Threading;
32 using System.Security.Cryptography.X509Certificates;
33 using Mono.Security.Protocol.Tls;
34 using MX = Mono.Security.X509;
35 using Mono.Net.Security;
37 namespace Mono.Security.Interface
39 #if (!MONOTOUCH && !MONODROID) || INSIDE_SYSTEM
40 public class ValidationResult
45 MonoSslPolicyErrors? policy_errors;
47 public ValidationResult (bool trusted, bool user_denied, int error_code, MonoSslPolicyErrors? policy_errors)
49 this.trusted = trusted;
50 this.user_denied = user_denied;
51 this.error_code = error_code;
52 this.policy_errors = policy_errors;
55 internal ValidationResult (bool trusted, bool user_defined, int error_code)
57 this.trusted = trusted;
58 this.user_denied = user_denied;
59 this.error_code = error_code;
60 this.policy_errors = policy_errors;
64 get { return trusted; }
67 public bool UserDenied {
68 get { return user_denied; }
71 public int ErrorCode {
72 get { return error_code; }
75 public MonoSslPolicyErrors? PolicyErrors {
76 get { return policy_errors; }
81 * Internal interface - do not implement
83 public interface ICertificateValidator
85 MonoTlsSettings Settings {
90 * Returns `true` if a client certificate has been selected (which could be `null`).
92 bool SelectClientCertificate (
93 string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
94 string[] acceptableIssuers, out X509Certificate clientCertificate);
97 * If @serverMode is true, then we're a server and want to validate a certificate that we received from a client.
99 ValidationResult ValidateCertificate (string targetHost, bool serverMode, X509CertificateCollection certificates);
102 * On OS X and Mobile, the @chain will be initialized with the @certificates, but not actually built.
104 bool InvokeSystemValidator (
105 string targetHost, bool serverMode, X509CertificateCollection certificates,
106 X509Chain chain, ref MonoSslPolicyErrors errors, ref int status11);
109 public static class CertificateValidationHelper
111 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
112 static readonly bool noX509Chain;
113 static readonly bool supportsTrustAnchors;
115 static CertificateValidationHelper ()
117 #if MONOTOUCH || XAMMAC
119 supportsTrustAnchors = true;
122 supportsTrustAnchors = false;
124 if (File.Exists (SecurityLibrary)) {
126 supportsTrustAnchors = true;
129 supportsTrustAnchors = false;
134 public static bool SupportsX509Chain {
135 get { return !noX509Chain; }
138 public static bool SupportsTrustAnchors {
139 get { return supportsTrustAnchors; }
142 static ICertificateValidator GetDefaultValidator (MonoTlsProvider provider, MonoTlsSettings settings)
144 return (ICertificateValidator)NoReflectionHelper.GetDefaultCertificateValidator (provider, settings);
148 * Internal API, intended to be used by MonoTlsProvider implementations.
150 public static ICertificateValidator GetValidator (MonoTlsProvider provider, MonoTlsSettings settings)
152 return GetDefaultValidator (provider, settings);
156 * Use this overloaded version in user code.
158 public static ICertificateValidator GetValidator (MonoTlsSettings settings)
160 return GetDefaultValidator (null, settings);