| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Security.Cryptography.X509Certificates; |
| | | 6 | | using System.Text; |
| | | 7 | | using CoreWCF.IdentityModel.Tokens; |
| | | 8 | | using CoreWCF.Security; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.IdentityModel.Selectors |
| | | 11 | | { |
| | | 12 | | public abstract class X509CertificateValidator |
| | | 13 | | { |
| | | 14 | | internal const uint CAPI_CERT_CHAIN_POLICY_NT_AUTH = 6; |
| | | 15 | | private static X509CertificateValidator s_peerTrust; |
| | | 16 | | private static X509CertificateValidator s_chainTrust; |
| | | 17 | | private static X509CertificateValidator s_ntAuthChainTrust; |
| | | 18 | | private static X509CertificateValidator s_peerOrChainTrust; |
| | | 19 | | private static X509CertificateValidator s_none; |
| | | 20 | | |
| | | 21 | | public static X509CertificateValidator None |
| | | 22 | | { |
| | | 23 | | get |
| | | 24 | | { |
| | 6 | 25 | | if (s_none == null) |
| | | 26 | | { |
| | 4 | 27 | | s_none = new NoneX509CertificateValidator(); |
| | | 28 | | } |
| | | 29 | | |
| | 6 | 30 | | return s_none; |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | public static X509CertificateValidator PeerTrust |
| | | 35 | | { |
| | | 36 | | get |
| | | 37 | | { |
| | 2 | 38 | | if (s_peerTrust == null) |
| | | 39 | | { |
| | 2 | 40 | | s_peerTrust = new PeerTrustValidator(); |
| | | 41 | | } |
| | | 42 | | |
| | 2 | 43 | | return s_peerTrust; |
| | | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public static X509CertificateValidator ChainTrust |
| | | 48 | | { |
| | | 49 | | get |
| | | 50 | | { |
| | 0 | 51 | | if (s_chainTrust == null) |
| | | 52 | | { |
| | 0 | 53 | | s_chainTrust = new ChainTrustValidator(); |
| | | 54 | | } |
| | | 55 | | |
| | 0 | 56 | | return s_chainTrust; |
| | | 57 | | } |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | // TODO: Consider creating platform specific package which contains windows only implementations such as NTAuthC |
| | | 61 | | internal static X509CertificateValidator NTAuthChainTrust |
| | | 62 | | { |
| | | 63 | | get |
| | | 64 | | { |
| | 0 | 65 | | if (s_ntAuthChainTrust == null) |
| | | 66 | | { |
| | 0 | 67 | | s_ntAuthChainTrust = new ChainTrustValidator(false, null, CAPI_CERT_CHAIN_POLICY_NT_AUTH); |
| | | 68 | | } |
| | | 69 | | |
| | 0 | 70 | | return s_ntAuthChainTrust; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | public static X509CertificateValidator PeerOrChainTrust |
| | | 75 | | { |
| | | 76 | | get |
| | | 77 | | { |
| | 0 | 78 | | if (s_peerOrChainTrust == null) |
| | | 79 | | { |
| | 0 | 80 | | s_peerOrChainTrust = new PeerOrChainTrustValidator(); |
| | | 81 | | } |
| | | 82 | | |
| | 0 | 83 | | return s_peerOrChainTrust; |
| | | 84 | | } |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | public static X509CertificateValidator CreateChainTrustValidator(bool useMachineContext, X509ChainPolicy chainPo |
| | | 88 | | { |
| | 13 | 89 | | if (chainPolicy == null) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(chainPolicy)); |
| | | 92 | | } |
| | | 93 | | |
| | 13 | 94 | | return new ChainTrustValidator(useMachineContext, chainPolicy, X509CertificateChain.DefaultChainPolicyOID); |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | public static X509CertificateValidator CreatePeerOrChainTrustValidator(bool useMachineContext, X509ChainPolicy c |
| | | 98 | | { |
| | 2 | 99 | | if (chainPolicy == null) |
| | | 100 | | { |
| | 0 | 101 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(chainPolicy)); |
| | | 102 | | } |
| | | 103 | | |
| | 2 | 104 | | return new PeerOrChainTrustValidator(useMachineContext, chainPolicy); |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | public abstract void Validate(X509Certificate2 certificate); |
| | | 108 | | |
| | | 109 | | private class NoneX509CertificateValidator : X509CertificateValidator |
| | | 110 | | { |
| | | 111 | | public override void Validate(X509Certificate2 certificate) |
| | | 112 | | { |
| | 32 | 113 | | if (certificate == null) |
| | | 114 | | { |
| | 0 | 115 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 116 | | } |
| | 32 | 117 | | } |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | private class PeerTrustValidator : X509CertificateValidator |
| | | 121 | | { |
| | | 122 | | public override void Validate(X509Certificate2 certificate) |
| | | 123 | | { |
| | 0 | 124 | | if (certificate == null) |
| | | 125 | | { |
| | 0 | 126 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 127 | | } |
| | | 128 | | |
| | 0 | 129 | | if (!TryValidate(certificate, out Exception exception)) |
| | | 130 | | { |
| | 0 | 131 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception); |
| | | 132 | | } |
| | 0 | 133 | | } |
| | | 134 | | |
| | | 135 | | private static bool StoreContainsCertificate(StoreName storeName, X509Certificate2 certificate) |
| | | 136 | | { |
| | 0 | 137 | | X509Store store = new X509Store(storeName, StoreLocation.CurrentUser); |
| | 0 | 138 | | X509Certificate2Collection certificates = null; |
| | | 139 | | try |
| | | 140 | | { |
| | 0 | 141 | | store.Open(OpenFlags.ReadOnly); |
| | 0 | 142 | | certificates = store.Certificates.Find(X509FindType.FindByThumbprint, certificate.Thumbprint, false) |
| | 0 | 143 | | return certificates.Count > 0; |
| | | 144 | | } |
| | | 145 | | finally |
| | | 146 | | { |
| | 0 | 147 | | SecurityUtils.ResetAllCertificates(certificates); |
| | 0 | 148 | | store.Close(); |
| | 0 | 149 | | } |
| | 0 | 150 | | } |
| | | 151 | | |
| | | 152 | | internal bool TryValidate(X509Certificate2 certificate, out Exception exception) |
| | | 153 | | { |
| | | 154 | | // Checklist |
| | | 155 | | // 1) time validity of cert |
| | | 156 | | // 2) in trusted people store |
| | | 157 | | // 3) not in disallowed store |
| | | 158 | | |
| | | 159 | | // The following code could be written as: |
| | | 160 | | // DateTime now = DateTime.UtcNow; |
| | | 161 | | // if (now > certificate.NotAfter.ToUniversalTime() || now < certificate.NotBefore.ToUniversalTime()) |
| | | 162 | | // |
| | | 163 | | // this is because X509Certificate2.xxx doesn't return UT. However this would be a SMALL perf hit. |
| | | 164 | | // I put a DebugAssert so that this will ensure that the we are compatible with the CLR we shipped with |
| | | 165 | | |
| | 0 | 166 | | DateTime now = DateTime.Now; |
| | | 167 | | DiagnosticUtility.DebugAssert(now.Kind == certificate.NotAfter.Kind && now.Kind == certificate.NotBefore |
| | | 168 | | |
| | 0 | 169 | | if (now > certificate.NotAfter || now < certificate.NotBefore) |
| | | 170 | | { |
| | 0 | 171 | | exception = new SecurityTokenValidationException(SR.Format(SR.X509InvalidUsageTime, |
| | 0 | 172 | | SecurityUtils.GetCertificateId(certificate), now, certificate.NotBefore, certificate.NotAfter)); |
| | 0 | 173 | | return false; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | if (!StoreContainsCertificate(StoreName.TrustedPeople, certificate)) |
| | | 177 | | { |
| | 0 | 178 | | exception = new SecurityTokenValidationException(SR.Format(SR.X509IsNotInTrustedStore, |
| | 0 | 179 | | SecurityUtils.GetCertificateId(certificate))); |
| | 0 | 180 | | return false; |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | if (StoreContainsCertificate(StoreName.Disallowed, certificate)) |
| | | 184 | | { |
| | 0 | 185 | | exception = new SecurityTokenValidationException(SR.Format(SR.X509IsInUntrustedStore, |
| | 0 | 186 | | SecurityUtils.GetCertificateId(certificate))); |
| | 0 | 187 | | return false; |
| | | 188 | | } |
| | 0 | 189 | | exception = null; |
| | 0 | 190 | | return true; |
| | | 191 | | } |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | private class ChainTrustValidator : X509CertificateValidator |
| | | 195 | | { |
| | | 196 | | private readonly bool _useMachineContext; |
| | | 197 | | private readonly X509ChainPolicy _chainPolicy; |
| | 13 | 198 | | private readonly uint _chainPolicyOID = X509CertificateChain.DefaultChainPolicyOID; |
| | | 199 | | |
| | 0 | 200 | | public ChainTrustValidator() |
| | | 201 | | { |
| | 0 | 202 | | _chainPolicy = null; |
| | 0 | 203 | | } |
| | | 204 | | |
| | 13 | 205 | | public ChainTrustValidator(bool useMachineContext, X509ChainPolicy chainPolicy, uint chainPolicyOID) |
| | | 206 | | { |
| | 13 | 207 | | _useMachineContext = useMachineContext; |
| | 13 | 208 | | _chainPolicy = chainPolicy; |
| | 13 | 209 | | _chainPolicyOID = chainPolicyOID; |
| | 13 | 210 | | } |
| | | 211 | | |
| | | 212 | | public override void Validate(X509Certificate2 certificate) |
| | | 213 | | { |
| | 0 | 214 | | if (certificate == null) |
| | | 215 | | { |
| | 0 | 216 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | X509Chain chain = new X509Chain(); |
| | 0 | 220 | | if (_chainPolicy != null) |
| | | 221 | | { |
| | 0 | 222 | | chain.ChainPolicy = _chainPolicy; |
| | | 223 | | } |
| | | 224 | | |
| | 0 | 225 | | if (!chain.Build(certificate)) |
| | | 226 | | { |
| | 0 | 227 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenValidationException(SR.Fo |
| | 0 | 228 | | SecurityUtils.GetCertificateId(certificate), GetChainStatusInformation(chain.ChainStatus)))); |
| | | 229 | | } |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | private static string GetChainStatusInformation(X509ChainStatus[] chainStatus) |
| | | 233 | | { |
| | 0 | 234 | | if (chainStatus != null) |
| | | 235 | | { |
| | 0 | 236 | | StringBuilder error = new StringBuilder(128); |
| | 0 | 237 | | for (int i = 0; i < chainStatus.Length; ++i) |
| | | 238 | | { |
| | 0 | 239 | | error.Append(chainStatus[i].StatusInformation); |
| | 0 | 240 | | error.Append(" "); |
| | | 241 | | } |
| | 0 | 242 | | return error.ToString(); |
| | | 243 | | } |
| | 0 | 244 | | return string.Empty; |
| | | 245 | | } |
| | | 246 | | } |
| | | 247 | | |
| | | 248 | | private class PeerOrChainTrustValidator : X509CertificateValidator |
| | | 249 | | { |
| | | 250 | | private readonly X509CertificateValidator _chain; |
| | | 251 | | private readonly PeerTrustValidator _peer; |
| | | 252 | | |
| | 0 | 253 | | public PeerOrChainTrustValidator() |
| | | 254 | | { |
| | 0 | 255 | | _chain = ChainTrust; |
| | 0 | 256 | | _peer = (PeerTrustValidator)PeerTrust; |
| | 0 | 257 | | } |
| | | 258 | | |
| | 2 | 259 | | public PeerOrChainTrustValidator(bool useMachineContext, X509ChainPolicy chainPolicy) |
| | | 260 | | { |
| | 2 | 261 | | _chain = CreateChainTrustValidator(useMachineContext, chainPolicy); |
| | 2 | 262 | | _peer = (PeerTrustValidator)PeerTrust; |
| | 2 | 263 | | } |
| | | 264 | | |
| | | 265 | | public override void Validate(X509Certificate2 certificate) |
| | | 266 | | { |
| | 0 | 267 | | if (certificate == null) |
| | | 268 | | { |
| | 0 | 269 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(certificate)); |
| | | 270 | | } |
| | | 271 | | |
| | 0 | 272 | | if (_peer.TryValidate(certificate, out Exception exception)) |
| | | 273 | | { |
| | 0 | 274 | | return; |
| | | 275 | | } |
| | | 276 | | |
| | | 277 | | try |
| | | 278 | | { |
| | 0 | 279 | | _chain.Validate(certificate); |
| | 0 | 280 | | } |
| | 0 | 281 | | catch (SecurityTokenValidationException ex) |
| | | 282 | | { |
| | 0 | 283 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenValidationException(excep |
| | | 284 | | } |
| | 0 | 285 | | } |
| | | 286 | | } |
| | | 287 | | } |
| | | 288 | | } |