Merge pull request #2130 from sandyarmstrong/mt-runtime-updates
[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.Reflection;
33 using System.Security.Cryptography.X509Certificates;
34 using Mono.Security.Protocol.Tls;
35 using MX = Mono.Security.X509;
36 #if MOBILE
37 using Mono.Net.Security;
38 #endif
39
40 namespace Mono.Security.Interface
41 {
42         #if (!MONOTOUCH && !MONODROID) || INSIDE_SYSTEM
43         public class ValidationResult
44         {
45                 bool trusted;
46                 bool user_denied;
47                 int error_code;
48                 MonoSslPolicyErrors policy_errors;
49
50                 public ValidationResult (bool trusted, bool user_denied, int error_code, MonoSslPolicyErrors policy_errors)
51                 {
52                         this.trusted = trusted;
53                         this.user_denied = user_denied;
54                         this.error_code = error_code;
55                         this.policy_errors = policy_errors;
56                 }
57
58                 public bool Trusted {
59                         get { return trusted; }
60                 }
61
62                 public bool UserDenied {
63                         get { return user_denied; }
64                 }
65
66                 public int ErrorCode {
67                         get { return error_code; }
68                 }
69
70                 public MonoSslPolicyErrors PolicyErrors {
71                         get { return policy_errors; }
72                 }
73         }
74
75         /**
76          * Internal interface - do not implement
77          */
78         public interface ICertificateValidator
79         {
80                 MonoTlsSettings Settings {
81                         get;
82                 }
83
84                 X509Certificate SelectClientCertificate (
85                         string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
86                         string[] acceptableIssuers);
87
88                 ValidationResult ValidateChain (string targetHost, X509CertificateCollection certificates);
89
90                 ValidationResult ValidateClientCertificate (X509CertificateCollection certificates);
91         }
92         #endif
93
94         #if !INSIDE_SYSTEM
95         public
96         #endif
97         static class CertificateValidationHelper
98         {
99                 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
100                 static readonly bool noX509Chain;
101
102                 #if !INSIDE_SYSTEM
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;
108                 #endif
109
110                 static CertificateValidationHelper ()
111                 {
112                         #if !INSIDE_SYSTEM
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 ();
125                         #endif
126
127                         #if MONOTOUCH || XAMMAC
128                         noX509Chain = true;
129                         #else
130                         noX509Chain = File.Exists (SecurityLibrary);
131                         #endif
132                 }
133
134                 public static bool SupportsX509Chain {
135                         get { return !noX509Chain; }
136                 }
137
138                 internal static ICertificateValidator GetDefaultValidator (MonoTlsSettings settings)
139                 {
140                         #if INSIDE_SYSTEM
141                         #if MARTIN_FIXME
142                         return ChainValidationHelper.GetDefaultValidator (settings);
143                         #else
144                         throw new NotImplementedException ();
145                         #endif
146                         #else
147                         return (ICertificateValidator)createMethod.Invoke (null, new object[] { settings });
148                         #endif
149                 }
150
151                 #if !INSIDE_SYSTEM
152                 public static ICertificateValidator GetValidator (MonoTlsSettings settings)
153                 {
154                         return GetDefaultValidator (settings);
155                 }
156                 #endif
157         }
158 }