Merge pull request #2081 from BrzVlad/feature-concurrent-work
[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.Security.Cryptography.X509Certificates;
33 using Mono.Security.Protocol.Tls;
34 using MX = Mono.Security.X509;
35 using Mono.Net.Security;
36
37 namespace Mono.Security.Interface
38 {
39         #if (!MONOTOUCH && !MONODROID) || INSIDE_SYSTEM
40         public class ValidationResult
41         {
42                 bool trusted;
43                 bool user_denied;
44                 int error_code;
45                 MonoSslPolicyErrors? policy_errors;
46
47                 public ValidationResult (bool trusted, bool user_denied, int error_code, MonoSslPolicyErrors? policy_errors)
48                 {
49                         this.trusted = trusted;
50                         this.user_denied = user_denied;
51                         this.error_code = error_code;
52                         this.policy_errors = policy_errors;
53                 }
54
55                 internal ValidationResult (bool trusted, bool user_defined, int error_code)
56                 {
57                         this.trusted = trusted;
58                         this.user_denied = user_denied;
59                         this.error_code = error_code;
60                         this.policy_errors = policy_errors;
61                 }
62
63                 public bool Trusted {
64                         get { return trusted; }
65                 }
66
67                 public bool UserDenied {
68                         get { return user_denied; }
69                 }
70
71                 public int ErrorCode {
72                         get { return error_code; }
73                 }
74
75                 public MonoSslPolicyErrors? PolicyErrors {
76                         get { return policy_errors; }
77                 }
78         }
79
80         /**
81          * Internal interface - do not implement
82          */
83         public interface ICertificateValidator
84         {
85                 MonoTlsSettings Settings {
86                         get;
87                 }
88
89                 X509Certificate SelectClientCertificate (
90                         string targetHost, X509CertificateCollection localCertificates, X509Certificate remoteCertificate,
91                         string[] acceptableIssuers);
92
93                 ValidationResult ValidateChain (string targetHost, X509CertificateCollection certificates);
94
95                 ValidationResult ValidateClientCertificate (X509CertificateCollection certificates);
96         }
97
98         public static class CertificateValidationHelper
99         {
100                 const string SecurityLibrary = "/System/Library/Frameworks/Security.framework/Security";
101                 static readonly bool noX509Chain;
102                 static readonly bool supportsTrustAnchors;
103
104                 static CertificateValidationHelper ()
105                 {
106                         #if MONOTOUCH || XAMMAC
107                         noX509Chain = true;
108                         supportsTrustAnchors = true;
109                         #elif MONODROID
110                         noX509Chain = true;
111                         supportsTrustAnchors = false;
112                         #else
113                         if (File.Exists (SecurityLibrary)) {
114                                 noX509Chain = true;
115                                 supportsTrustAnchors = true;
116                         } else {
117                                 noX509Chain = false;
118                                 supportsTrustAnchors = false;
119                         }
120                         #endif
121                 }
122
123                 public static bool SupportsX509Chain {
124                         get { return !noX509Chain; }
125                 }
126
127                 public static bool SupportsTrustAnchors {
128                         get { return supportsTrustAnchors; }
129                 }
130
131                 internal static ICertificateValidator GetDefaultValidator (MonoTlsSettings settings)
132                 {
133                         return (ICertificateValidator)NoReflectionHelper.GetDefaultCertificateValidator (settings);
134                 }
135
136                 public static ICertificateValidator GetValidator (MonoTlsSettings settings)
137                 {
138                         return GetDefaultValidator (settings);
139                 }
140         }
141 #endif
142 }