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;
59 get { return trusted; }
62 public bool UserDenied {
63 get { return user_denied; }
66 public int ErrorCode {
67 get { return error_code; }
70 public MonoSslPolicyErrors PolicyErrors {
71 get { return policy_errors; }
76 * Internal interface - do not implement
78 public interface ICertificateValidator
80 MonoTlsSettings Settings {
84 X509Certificate SelectClientCertificate (
85 string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
86 string[] acceptableIssuers);
88 ValidationResult ValidateChain (string targetHost, X509CertificateCollection certificates);
90 ValidationResult ValidateClientCertificate (X509CertificateCollection certificates);
97 static class CertificateValidationHelper
99 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
100 static readonly bool noX509Chain;
103 const string InternalHelperTypeName = "Mono.Net.Security.ChainValidationHelper";
104 static readonly Type internalHelperType;
105 static readonly MethodInfo createMethod;
106 static readonly MethodInfo validateClientCertificateMethod;
107 static readonly MethodInfo validateChainMethod;
110 static CertificateValidationHelper ()
113 internalHelperType = Type.GetType (InternalHelperTypeName + ", " + Consts.AssemblySystem, true);
114 if (internalHelperType == null)
115 throw new NotSupportedException ();
116 createMethod = internalHelperType.GetMethod ("GetDefaultValidator", BindingFlags.Static | BindingFlags.NonPublic);
117 if (createMethod == null)
118 throw new NotSupportedException ();
119 validateClientCertificateMethod = internalHelperType.GetMethod ("ValidateClientCertificate", BindingFlags.Instance | BindingFlags.Public);
120 if (validateClientCertificateMethod == null)
121 throw new NotSupportedException ();
122 validateChainMethod = internalHelperType.GetMethod ("ValidateChain", BindingFlags.Instance | BindingFlags.Public);
123 if (validateChainMethod == null)
124 throw new NotSupportedException ();
127 #if MONOTOUCH || XAMMAC
130 noX509Chain = File.Exists (SecurityLibrary);
134 public static bool SupportsX509Chain {
135 get { return !noX509Chain; }
138 internal static ICertificateValidator GetDefaultValidator (MonoTlsSettings settings)
142 return ChainValidationHelper.GetDefaultValidator (settings);
144 throw new NotImplementedException ();
147 return (ICertificateValidator)createMethod.Invoke (null, new object[] { settings });
152 public static ICertificateValidator GetValidator (MonoTlsSettings settings)
154 return GetDefaultValidator (settings);