da03e384a56fb289ce98ba396767708cd57a6d57
[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                 internal ValidationResult (bool trusted, bool user_defined, int error_code)
59                 {
60                         this.trusted = trusted;
61                         this.user_denied = user_denied;
62                         this.error_code = error_code;
63                         this.policy_errors = policy_errors;
64                 }
65
66                 public bool Trusted {
67                         get { return trusted; }
68                 }
69
70                 public bool UserDenied {
71                         get { return user_denied; }
72                 }
73
74                 public int ErrorCode {
75                         get { return error_code; }
76                 }
77
78                 public MonoSslPolicyErrors? PolicyErrors {
79                         get { return policy_errors; }
80                 }
81         }
82
83         /**
84          * Internal interface - do not implement
85          */
86         public interface ICertificateValidator
87         {
88                 MonoTlsSettings Settings {
89                         get;
90                 }
91
92                 X509Certificate SelectClientCertificate (
93                         string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
94                         string[] acceptableIssuers);
95
96                 ValidationResult ValidateChain (string targetHost, X509CertificateCollection certificates);
97
98                 ValidationResult ValidateClientCertificate (X509CertificateCollection certificates);
99         }
100         #endif
101
102         #if !INSIDE_SYSTEM
103         public
104         #endif
105         static class CertificateValidationHelper
106         {
107                 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
108                 static readonly bool noX509Chain;
109
110                 #if !INSIDE_SYSTEM
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;
116                 #endif
117
118                 static CertificateValidationHelper ()
119                 {
120                         #if !INSIDE_SYSTEM
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 ();
133                         #endif
134
135                         #if MONOTOUCH || XAMMAC
136                         noX509Chain = true;
137                         #else
138                         noX509Chain = File.Exists (SecurityLibrary);
139                         #endif
140                 }
141
142                 public static bool SupportsX509Chain {
143                         get { return !noX509Chain; }
144                 }
145
146                 internal static ICertificateValidator GetDefaultValidator (MonoTlsSettings settings)
147                 {
148                         #if INSIDE_SYSTEM
149                         return ChainValidationHelper.GetDefaultValidator (settings);
150                         #else
151                         return (ICertificateValidator)createMethod.Invoke (null, new object[] { settings });
152                         #endif
153                 }
154
155                 #if !INSIDE_SYSTEM
156                 public static ICertificateValidator GetValidator (MonoTlsSettings settings)
157                 {
158                         return GetDefaultValidator (settings);
159                 }
160                 #endif
161         }
162 }