| | | 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.Collections.Generic; |
| | | 6 | | using System.Collections.ObjectModel; |
| | | 7 | | using System.Globalization; |
| | | 8 | | using System.Security.Principal; |
| | | 9 | | using CoreWCF.Channels; |
| | | 10 | | using CoreWCF.IdentityModel.Claims; |
| | | 11 | | using CoreWCF.IdentityModel.Policy; |
| | | 12 | | using CoreWCF.Runtime.Diagnostics; |
| | | 13 | | using CoreWCF.Security.Tokens; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Security |
| | | 16 | | { |
| | | 17 | | public abstract class IdentityVerifier |
| | | 18 | | { |
| | 3 | 19 | | protected IdentityVerifier() |
| | | 20 | | { |
| | | 21 | | // empty |
| | 3 | 22 | | } |
| | | 23 | | |
| | | 24 | | public static IdentityVerifier CreateDefault() |
| | | 25 | | { |
| | 17 | 26 | | return DefaultIdentityVerifier.Instance; |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | internal bool CheckAccess(EndpointAddress reference, Message message) |
| | | 30 | | { |
| | 0 | 31 | | if (reference == null) |
| | | 32 | | { |
| | 0 | 33 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reference)); |
| | | 34 | | } |
| | | 35 | | |
| | 0 | 36 | | if (message == null) |
| | | 37 | | { |
| | 0 | 38 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); |
| | | 39 | | } |
| | | 40 | | |
| | 0 | 41 | | if (!TryGetIdentity(reference, out EndpointIdentity identity)) |
| | | 42 | | { |
| | 0 | 43 | | return false; |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | //SecurityMessageProperty securityContextProperty = null; |
| | | 47 | | //if (message.Properties != null) |
| | | 48 | | // securityContextProperty = message.Properties.Security; |
| | | 49 | | |
| | | 50 | | //if (securityContextProperty == null || securityContextProperty.ServiceSecurityContext == null) |
| | | 51 | | // return false; |
| | | 52 | | |
| | | 53 | | //return this.CheckAccess(identity, securityContextProperty.ServiceSecurityContext.AuthorizationContext); |
| | | 54 | | return false; |
| | | 55 | | } |
| | | 56 | | |
| | | 57 | | public abstract bool CheckAccess(EndpointIdentity identity, AuthorizationContext authContext); |
| | | 58 | | |
| | | 59 | | public abstract bool TryGetIdentity(EndpointAddress reference, out EndpointIdentity identity); |
| | | 60 | | |
| | | 61 | | private static void AdjustAddress(ref EndpointAddress reference, Uri via) |
| | | 62 | | { |
| | | 63 | | // if we don't have an identity and we have differing Uris, we should use the Via |
| | 0 | 64 | | if (reference.Identity == null && reference.Uri != via) |
| | | 65 | | { |
| | 0 | 66 | | reference = new EndpointAddress(via); |
| | | 67 | | } |
| | 0 | 68 | | } |
| | | 69 | | |
| | | 70 | | internal bool TryGetIdentity(EndpointAddress reference, Uri via, out EndpointIdentity identity) |
| | | 71 | | { |
| | 0 | 72 | | AdjustAddress(ref reference, via); |
| | 0 | 73 | | return TryGetIdentity(reference, out identity); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | internal void EnsureIncomingIdentity(EndpointAddress serviceReference, AuthorizationContext authorizationContext |
| | | 77 | | { |
| | 0 | 78 | | EnsureIdentity(serviceReference, authorizationContext, SR.IdentityCheckFailedForIncomingMessage); |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | internal void EnsureOutgoingIdentity(EndpointAddress serviceReference, Uri via, AuthorizationContext authorizati |
| | | 82 | | { |
| | 0 | 83 | | AdjustAddress(ref serviceReference, via); |
| | 0 | 84 | | EnsureIdentity(serviceReference, authorizationContext, SR.IdentityCheckFailedForOutgoingMessage); |
| | 0 | 85 | | } |
| | | 86 | | |
| | | 87 | | internal void EnsureOutgoingIdentity(EndpointAddress serviceReference, ReadOnlyCollection<IAuthorizationPolicy> |
| | | 88 | | { |
| | 0 | 89 | | if (authorizationPolicies == null) |
| | | 90 | | { |
| | 0 | 91 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(authorizationPolicies)); |
| | | 92 | | } |
| | 0 | 93 | | AuthorizationContext ac = AuthorizationContext.CreateDefaultAuthorizationContext(authorizationPolicies); |
| | 0 | 94 | | EnsureIdentity(serviceReference, ac, SR.IdentityCheckFailedForOutgoingMessage); |
| | 0 | 95 | | } |
| | | 96 | | |
| | | 97 | | private void EnsureIdentity(EndpointAddress serviceReference, AuthorizationContext authorizationContext, string |
| | | 98 | | { |
| | 0 | 99 | | if (authorizationContext == null) |
| | | 100 | | { |
| | 0 | 101 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(authorizationContext)); |
| | | 102 | | } |
| | 0 | 103 | | if (!TryGetIdentity(serviceReference, out EndpointIdentity identity)) |
| | | 104 | | { |
| | | 105 | | //SecurityTraceRecordHelper.TraceIdentityVerificationFailure(identity, authorizationContext, this.GetTyp |
| | 0 | 106 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new MessageSecurityException(SR.Format(error |
| | | 107 | | } |
| | | 108 | | else |
| | | 109 | | { |
| | 0 | 110 | | if (!CheckAccess(identity, authorizationContext)) |
| | | 111 | | { |
| | | 112 | | // CheckAccess performs a Trace on failure, no need to do it twice |
| | 0 | 113 | | Exception e = CreateIdentityCheckException(identity, authorizationContext, errorString, serviceRefer |
| | 0 | 114 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(e); |
| | | 115 | | } |
| | | 116 | | } |
| | 0 | 117 | | } |
| | | 118 | | |
| | | 119 | | private Exception CreateIdentityCheckException(EndpointIdentity identity, AuthorizationContext authorizationCont |
| | | 120 | | { |
| | | 121 | | Exception result; |
| | | 122 | | |
| | 0 | 123 | | if (identity.IdentityClaim != null |
| | 0 | 124 | | && identity.IdentityClaim.ClaimType == ClaimTypes.Dns |
| | 0 | 125 | | && identity.IdentityClaim.Right == Rights.PossessProperty |
| | 0 | 126 | | && identity.IdentityClaim.Resource is string) |
| | | 127 | | { |
| | 0 | 128 | | string expectedDnsName = (string)identity.IdentityClaim.Resource; |
| | 0 | 129 | | string actualDnsName = null; |
| | 0 | 130 | | for (int i = 0; i < authorizationContext.ClaimSets.Count; ++i) |
| | | 131 | | { |
| | 0 | 132 | | ClaimSet claimSet = authorizationContext.ClaimSets[i]; |
| | 0 | 133 | | foreach (Claim claim in claimSet.FindClaims(ClaimTypes.Dns, Rights.PossessProperty)) |
| | | 134 | | { |
| | 0 | 135 | | if (claim.Resource is string) |
| | | 136 | | { |
| | 0 | 137 | | actualDnsName = (string)claim.Resource; |
| | 0 | 138 | | break; |
| | | 139 | | } |
| | | 140 | | } |
| | 0 | 141 | | if (actualDnsName != null) |
| | | 142 | | { |
| | | 143 | | break; |
| | | 144 | | } |
| | | 145 | | } |
| | 0 | 146 | | if (SR.IdentityCheckFailedForIncomingMessage.Equals(errorString)) |
| | | 147 | | { |
| | 0 | 148 | | if (actualDnsName == null) |
| | | 149 | | { |
| | 0 | 150 | | result = new MessageSecurityException(SR.Format(SR.DnsIdentityCheckFailedForIncomingMessageLackO |
| | | 151 | | } |
| | | 152 | | else |
| | | 153 | | { |
| | 0 | 154 | | result = new MessageSecurityException(SR.Format(SR.DnsIdentityCheckFailedForIncomingMessage, exp |
| | | 155 | | } |
| | | 156 | | } |
| | 0 | 157 | | else if (SR.IdentityCheckFailedForOutgoingMessage.Equals(errorString)) |
| | | 158 | | { |
| | 0 | 159 | | if (actualDnsName == null) |
| | | 160 | | { |
| | 0 | 161 | | result = new MessageSecurityException(SR.Format(SR.DnsIdentityCheckFailedForOutgoingMessageLackO |
| | | 162 | | } |
| | | 163 | | else |
| | | 164 | | { |
| | 0 | 165 | | result = new MessageSecurityException(SR.Format(SR.DnsIdentityCheckFailedForOutgoingMessage, exp |
| | | 166 | | } |
| | | 167 | | } |
| | | 168 | | else |
| | | 169 | | { |
| | 0 | 170 | | result = new MessageSecurityException(SR.Format(errorString, identity, serviceReference)); |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | else |
| | | 174 | | { |
| | 0 | 175 | | result = new MessageSecurityException(SR.Format(errorString, identity, serviceReference)); |
| | | 176 | | } |
| | | 177 | | |
| | 0 | 178 | | return result; |
| | | 179 | | } |
| | | 180 | | |
| | | 181 | | private class DefaultIdentityVerifier : IdentityVerifier |
| | | 182 | | { |
| | 20 | 183 | | public static DefaultIdentityVerifier Instance { get; } = new DefaultIdentityVerifier(); |
| | | 184 | | |
| | | 185 | | public override bool TryGetIdentity(EndpointAddress reference, out EndpointIdentity identity) |
| | | 186 | | { |
| | 0 | 187 | | if (reference == null) |
| | | 188 | | { |
| | 0 | 189 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reference)); |
| | | 190 | | } |
| | | 191 | | |
| | 0 | 192 | | identity = reference.Identity; |
| | | 193 | | |
| | 0 | 194 | | if (identity == null) |
| | | 195 | | { |
| | 0 | 196 | | identity = TryCreateDnsIdentity(reference); |
| | | 197 | | } |
| | | 198 | | |
| | 0 | 199 | | if (identity == null) |
| | | 200 | | { |
| | 0 | 201 | | SecurityTraceRecordHelper.TraceIdentityDeterminationFailure(reference, typeof(DefaultIdentityVerifie |
| | 0 | 202 | | return false; |
| | | 203 | | } |
| | | 204 | | else |
| | | 205 | | { |
| | 0 | 206 | | SecurityTraceRecordHelper.TraceIdentityDeterminationSuccess(reference, identity, typeof(DefaultIdent |
| | 0 | 207 | | return true; |
| | | 208 | | } |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | private EndpointIdentity TryCreateDnsIdentity(EndpointAddress reference) |
| | | 212 | | { |
| | 0 | 213 | | Uri toAddress = reference.Uri; |
| | | 214 | | |
| | 0 | 215 | | if (!toAddress.IsAbsoluteUri) |
| | | 216 | | { |
| | 0 | 217 | | return null; |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | return EndpointIdentity.CreateDnsIdentity(toAddress.DnsSafeHost); |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | private SecurityIdentifier GetSecurityIdentifier(Claim claim) |
| | | 224 | | { |
| | | 225 | | // if the incoming claim is a SID and the EndpointIdentity is UPN/SPN/DNS, try to find the SID correspon |
| | | 226 | | // the UPN/SPN/DNS (transactions case) |
| | 0 | 227 | | if (claim.Resource is WindowsIdentity) |
| | | 228 | | { |
| | 0 | 229 | | return ((WindowsIdentity)claim.Resource).User; |
| | | 230 | | } |
| | 0 | 231 | | else if (claim.Resource is WindowsSidIdentity) |
| | | 232 | | { |
| | 0 | 233 | | return ((WindowsSidIdentity)claim.Resource).SecurityIdentifier; |
| | | 234 | | } |
| | | 235 | | |
| | 0 | 236 | | return claim.Resource as SecurityIdentifier; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private Claim CheckDnsEquivalence(ClaimSet claimSet, string expectedSpn) |
| | | 240 | | { |
| | | 241 | | // host/<machine-name> satisfies the DNS identity claim |
| | 0 | 242 | | IEnumerable<Claim> claims = claimSet.FindClaims(ClaimTypes.Spn, Rights.PossessProperty); |
| | 0 | 243 | | foreach (Claim claim in claims) |
| | | 244 | | { |
| | 0 | 245 | | if (expectedSpn.Equals((string)claim.Resource, StringComparison.OrdinalIgnoreCase)) |
| | | 246 | | { |
| | 0 | 247 | | return claim; |
| | | 248 | | } |
| | | 249 | | } |
| | 0 | 250 | | return null; |
| | 0 | 251 | | } |
| | | 252 | | |
| | | 253 | | private Claim CheckSidEquivalence(SecurityIdentifier identitySid, ClaimSet claimSet) |
| | | 254 | | { |
| | 0 | 255 | | foreach (Claim claim in claimSet) |
| | | 256 | | { |
| | 0 | 257 | | SecurityIdentifier sid = GetSecurityIdentifier(claim); |
| | 0 | 258 | | if (sid != null) |
| | | 259 | | { |
| | 0 | 260 | | if (identitySid.Equals(sid)) |
| | | 261 | | { |
| | 0 | 262 | | return claim; |
| | | 263 | | } |
| | | 264 | | } |
| | | 265 | | } |
| | 0 | 266 | | return null; |
| | 0 | 267 | | } |
| | | 268 | | |
| | | 269 | | public override bool CheckAccess(EndpointIdentity identity, AuthorizationContext authContext) |
| | | 270 | | { |
| | | 271 | | //EventTraceActivity eventTraceActivity = null; |
| | | 272 | | |
| | 0 | 273 | | if (identity == null) |
| | | 274 | | { |
| | 0 | 275 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 276 | | } |
| | | 277 | | |
| | 0 | 278 | | if (authContext == null) |
| | | 279 | | { |
| | 0 | 280 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(authContext)); |
| | | 281 | | } |
| | | 282 | | |
| | | 283 | | |
| | | 284 | | //if (FxTrace.Trace.IsEnd2EndActivityTracingEnabled) |
| | | 285 | | //{ |
| | | 286 | | // eventTraceActivity = EventTraceActivityHelper.TryExtractActivity((OperationContext.Current != null |
| | | 287 | | //} |
| | | 288 | | |
| | 0 | 289 | | for (int i = 0; i < authContext.ClaimSets.Count; ++i) |
| | | 290 | | { |
| | 0 | 291 | | ClaimSet claimSet = authContext.ClaimSets[i]; |
| | 0 | 292 | | if (claimSet.ContainsClaim(identity.IdentityClaim)) |
| | | 293 | | { |
| | | 294 | | //SecurityTraceRecordHelper.TraceIdentityVerificationSuccess(eventTraceActivity, identity, ident |
| | 0 | 295 | | return true; |
| | | 296 | | } |
| | | 297 | | |
| | | 298 | | // try Claim equivalence |
| | 0 | 299 | | string expectedSpn = null; |
| | 0 | 300 | | if (ClaimTypes.Dns.Equals(identity.IdentityClaim.ClaimType)) |
| | | 301 | | { |
| | 0 | 302 | | expectedSpn = string.Format(CultureInfo.InvariantCulture, "host/{0}", (string)identity.IdentityC |
| | 0 | 303 | | Claim claim = CheckDnsEquivalence(claimSet, expectedSpn); |
| | 0 | 304 | | if (claim != null) |
| | | 305 | | { |
| | | 306 | | //SecurityTraceRecordHelper.TraceIdentityVerificationSuccess(eventTraceActivity, identity, c |
| | 0 | 307 | | return true; |
| | | 308 | | } |
| | | 309 | | } |
| | | 310 | | // Allow a Sid claim to support UPN, and SPN identities |
| | 0 | 311 | | SecurityIdentifier identitySid = null; |
| | 0 | 312 | | if (ClaimTypes.Sid.Equals(identity.IdentityClaim.ClaimType)) |
| | | 313 | | { |
| | 0 | 314 | | identitySid = GetSecurityIdentifier(identity.IdentityClaim); |
| | | 315 | | } |
| | 0 | 316 | | else if (ClaimTypes.Upn.Equals(identity.IdentityClaim.ClaimType)) |
| | | 317 | | { |
| | 0 | 318 | | identitySid = ((UpnEndpointIdentity)identity).GetUpnSid(); |
| | | 319 | | } |
| | 0 | 320 | | else if (ClaimTypes.Spn.Equals(identity.IdentityClaim.ClaimType)) |
| | | 321 | | { |
| | 0 | 322 | | identitySid = ((SpnEndpointIdentity)identity).GetSpnSid(); |
| | | 323 | | } |
| | 0 | 324 | | else if (ClaimTypes.Dns.Equals(identity.IdentityClaim.ClaimType)) |
| | | 325 | | { |
| | 0 | 326 | | identitySid = new SpnEndpointIdentity(expectedSpn).GetSpnSid(); |
| | | 327 | | } |
| | 0 | 328 | | if (identitySid != null) |
| | | 329 | | { |
| | 0 | 330 | | Claim claim = CheckSidEquivalence(identitySid, claimSet); |
| | 0 | 331 | | if (claim != null) |
| | | 332 | | { |
| | | 333 | | //SecurityTraceRecordHelper.TraceIdentityVerificationSuccess(eventTraceActivity, identity, c |
| | 0 | 334 | | return true; |
| | | 335 | | } |
| | | 336 | | } |
| | | 337 | | } |
| | 0 | 338 | | SecurityTraceRecordHelper.TraceIdentityVerificationFailure(identity, authContext, GetType()); |
| | | 339 | | //if (TD.SecurityIdentityVerificationFailureIsEnabled()) |
| | | 340 | | //{ |
| | | 341 | | // TD.SecurityIdentityVerificationFailure(eventTraceActivity); |
| | | 342 | | //} |
| | | 343 | | |
| | 0 | 344 | | return false; |
| | | 345 | | } |
| | | 346 | | } |
| | | 347 | | } |
| | | 348 | | } |