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.Reflection;
33 using System.Security.Cryptography.X509Certificates;
34 using Mono.Security.Protocol.Tls;
35 using MX = Mono.Security.X509;
37 using Mono.Net.Security;
40 namespace Mono.Security.Interface
42 #if (!MONOTOUCH && !MONODROID) || INSIDE_SYSTEM
43 public class ValidationResult
48 MonoSslPolicyErrors? policy_errors;
50 public ValidationResult (bool trusted, bool user_denied, int error_code, MonoSslPolicyErrors? policy_errors)
52 this.trusted = trusted;
53 this.user_denied = user_denied;
54 this.error_code = error_code;
55 this.policy_errors = policy_errors;
58 internal ValidationResult (bool trusted, bool user_defined, int error_code)
60 this.trusted = trusted;
61 this.user_denied = user_denied;
62 this.error_code = error_code;
63 this.policy_errors = policy_errors;
67 get { return trusted; }
70 public bool UserDenied {
71 get { return user_denied; }
74 public int ErrorCode {
75 get { return error_code; }
78 public MonoSslPolicyErrors? PolicyErrors {
79 get { return policy_errors; }
84 * Internal interface - do not implement
86 public interface ICertificateValidator
88 MonoTlsSettings Settings {
92 X509Certificate SelectClientCertificate (
93 string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
94 string[] acceptableIssuers);
96 ValidationResult ValidateChain (string targetHost, X509CertificateCollection certificates);
98 ValidationResult ValidateClientCertificate (X509CertificateCollection certificates);
105 static class CertificateValidationHelper
107 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
108 static readonly bool noX509Chain;
111 const string InternalHelperTypeName = "Mono.Net.Security.ChainValidationHelper";
112 static readonly Type internalHelperType;
113 static readonly MethodInfo createMethod;
114 static readonly MethodInfo validateClientCertificateMethod;
115 static readonly MethodInfo validateChainMethod;
118 static CertificateValidationHelper ()
121 internalHelperType = Type.GetType (InternalHelperTypeName + ", " + Consts.AssemblySystem, true);
122 if (internalHelperType == null)
123 throw new NotSupportedException ();
124 createMethod = internalHelperType.GetMethod ("GetDefaultValidator", BindingFlags.Static | BindingFlags.NonPublic);
125 if (createMethod == null)
126 throw new NotSupportedException ();
127 validateClientCertificateMethod = internalHelperType.GetMethod ("ValidateClientCertificate", BindingFlags.Instance | BindingFlags.Public);
128 if (validateClientCertificateMethod == null)
129 throw new NotSupportedException ();
130 validateChainMethod = internalHelperType.GetMethod ("ValidateChain", BindingFlags.Instance | BindingFlags.Public);
131 if (validateChainMethod == null)
132 throw new NotSupportedException ();
135 #if MONOTOUCH || XAMMAC
138 noX509Chain = File.Exists (SecurityLibrary);
142 public static bool SupportsX509Chain {
143 get { return !noX509Chain; }
146 internal static ICertificateValidator GetDefaultValidator (MonoTlsSettings settings)
149 return ChainValidationHelper.GetDefaultValidator (settings);
151 return (ICertificateValidator)createMethod.Invoke (null, new object[] { settings });
156 public static ICertificateValidator GetValidator (MonoTlsSettings settings)
158 return GetDefaultValidator (settings);