| | | 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.Linq; |
| | | 7 | | using System.Security.Cryptography.X509Certificates; |
| | | 8 | | using CoreWCF.IdentityModel.Selectors; |
| | | 9 | | using Microsoft.IdentityModel.Tokens; |
| | | 10 | | using MsIdentityTokens = Microsoft.IdentityModel.Tokens; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 13 | | { |
| | | 14 | | internal class SamlTokenValidationParameters |
| | | 15 | | { |
| | | 16 | | private const string PROPERTY_BAG_ORIGIN = "CoreWCF.IdentityModel.Tokens.SamlTokenValidationParameters."; |
| | | 17 | | private const string SAML_SECURITY_TOKEN = PROPERTY_BAG_ORIGIN + "SamlSecurityToken"; |
| | | 18 | | private const string SAML_SECURITY_TOKEN_REQ = PROPERTY_BAG_ORIGIN + "SamlSecurityTokenRequirement"; |
| | | 19 | | private const string SECURITY_TOKEN_HANDLER_CONFIG = PROPERTY_BAG_ORIGIN + "SecurityTokenHandlerConfiguration"; |
| | | 20 | | |
| | 48 | 21 | | public SamlTokenValidationParameters() |
| | | 22 | | { |
| | 48 | 23 | | } |
| | | 24 | | |
| | | 25 | | internal TokenValidationParameters ConvertToTokenValidationParameters |
| | | 26 | | (SecurityTokenHandlerConfiguration securityHandlerConfiguration, SecurityToken securityToken, SamlSecurityTo |
| | | 27 | | { |
| | 48 | 28 | | TokenValidationParameters tokenValidationParams = new TokenValidationParameters |
| | 48 | 29 | | { |
| | 48 | 30 | | PropertyBag = new Dictionary<string, object>() |
| | 48 | 31 | | }; |
| | 48 | 32 | | tokenValidationParams.PropertyBag.Add(SECURITY_TOKEN_HANDLER_CONFIG, securityHandlerConfiguration); |
| | | 33 | | |
| | 48 | 34 | | tokenValidationParams.ValidateAudience = securityTokenRequirement.ShouldEnforceAudienceRestriction |
| | 48 | 35 | | (securityHandlerConfiguration.AudienceRestriction.AudienceMode, securityToken); |
| | 48 | 36 | | tokenValidationParams.RequireAudience = tokenValidationParams.ValidateAudience; |
| | | 37 | | |
| | 48 | 38 | | if (tokenValidationParams.ValidateAudience) |
| | | 39 | | { |
| | 47 | 40 | | if (securityHandlerConfiguration == null || securityHandlerConfiguration.AudienceRestriction.AllowedAudi |
| | | 41 | | { |
| | 0 | 42 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR |
| | | 43 | | } |
| | | 44 | | |
| | 47 | 45 | | tokenValidationParams.PropertyBag.Add(SAML_SECURITY_TOKEN_REQ, securityTokenRequirement); |
| | 47 | 46 | | tokenValidationParams.AudienceValidator = AudienceValidator; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | //start of tokenreplay |
| | 48 | 50 | | if (securityHandlerConfiguration.DetectReplayedTokens) |
| | | 51 | | { |
| | 0 | 52 | | tokenValidationParams.ValidateTokenReplay = true; |
| | 0 | 53 | | tokenValidationParams.TokenReplayCache = securityHandlerConfiguration.Caches.TokenReplayCache; |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | //start of certificate validator |
| | 48 | 57 | | tokenValidationParams.IssuerSigningKeyValidator = IssuerSigningKeyValidator; |
| | | 58 | | //start of Issuer validation |
| | 48 | 59 | | tokenValidationParams.IssuerValidator = IssuerValidate; |
| | 48 | 60 | | tokenValidationParams.PropertyBag.Add(SAML_SECURITY_TOKEN, securityToken); |
| | 48 | 61 | | tokenValidationParams.IssuerSigningKeyResolver = ResolveIssuerSigningKey; |
| | 48 | 62 | | tokenValidationParams.RequireSignedTokens = true; |
| | 48 | 63 | | tokenValidationParams.ClockSkew = securityHandlerConfiguration.MaxClockSkew; |
| | 48 | 64 | | return tokenValidationParams; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private bool IssuerSigningKeyValidator(MsIdentityTokens.SecurityKey securityKey, MsIdentityTokens.SecurityToken |
| | | 68 | | { |
| | 30 | 69 | | X509CertificateValidator validator = GetSecurityTokenHandler(validationParameters).CertificateValidator; |
| | 30 | 70 | | ICollection<Microsoft.IdentityModel.Xml.X509Data> x509Datas = null; |
| | 30 | 71 | | if (securityToken is MsIdentityTokens.Saml.SamlSecurityToken token) |
| | | 72 | | { |
| | 30 | 73 | | x509Datas = token.Assertion.Signature.KeyInfo.X509Data; |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | 0 | 77 | | x509Datas = ((MsIdentityTokens.Saml2.Saml2SecurityToken)securityToken).Assertion.Signature.KeyInfo.X509D |
| | | 78 | | } |
| | | 79 | | |
| | 30 | 80 | | if(x509Datas == null || x509Datas.Count > 1) |
| | | 81 | | { |
| | 0 | 82 | | return false; |
| | | 83 | | } |
| | | 84 | | |
| | 30 | 85 | | byte[] data = Convert.FromBase64String(x509Datas.FirstOrDefault().Certificates.FirstOrDefault()); |
| | 30 | 86 | | X509Certificate2 cert = new X509Certificate2(data); |
| | 30 | 87 | | validator.Validate(cert); |
| | 30 | 88 | | return true; |
| | | 89 | | } |
| | | 90 | | |
| | | 91 | | private IEnumerable<MsIdentityTokens.SecurityKey> ResolveIssuerSigningKey(string token, MsIdentityTokens.Securit |
| | | 92 | | { |
| | 48 | 93 | | SecurityToken samlToken = (SecurityToken)validationParameters.PropertyBag[SAML_SECURITY_TOKEN]; |
| | 48 | 94 | | SecurityToken signingToken = null; |
| | | 95 | | |
| | 48 | 96 | | if (samlToken is SamlSecurityToken sst) |
| | | 97 | | { |
| | 48 | 98 | | signingToken = sst.SigningToken; |
| | | 99 | | } |
| | 0 | 100 | | else if (samlToken is Saml2SecurityToken s2t) |
| | | 101 | | { |
| | 0 | 102 | | signingToken = s2t.SigningToken; |
| | | 103 | | } |
| | | 104 | | |
| | 48 | 105 | | if (signingToken is X509SecurityToken x509Token && x509Token.Certificate != null) |
| | | 106 | | { |
| | 46 | 107 | | yield return new MsIdentityTokens.X509SecurityKey(x509Token.Certificate); |
| | 7 | 108 | | yield break; |
| | | 109 | | } |
| | | 110 | | |
| | 2 | 111 | | ICollection<Microsoft.IdentityModel.Xml.X509Data> x509Datas = null; |
| | 2 | 112 | | if (securityToken is MsIdentityTokens.Saml.SamlSecurityToken samlSecurityToken |
| | 2 | 113 | | && samlSecurityToken.Assertion?.Signature?.KeyInfo != null) |
| | | 114 | | { |
| | 2 | 115 | | x509Datas = samlSecurityToken.Assertion.Signature.KeyInfo.X509Data; |
| | | 116 | | } |
| | 0 | 117 | | else if (securityToken is MsIdentityTokens.Saml2.Saml2SecurityToken saml2SecurityToken |
| | 0 | 118 | | && saml2SecurityToken.Assertion?.Signature?.KeyInfo != null) |
| | | 119 | | { |
| | 0 | 120 | | x509Datas = saml2SecurityToken.Assertion.Signature.KeyInfo.X509Data; |
| | | 121 | | } |
| | | 122 | | |
| | 2 | 123 | | if (x509Datas == null) |
| | | 124 | | { |
| | 0 | 125 | | yield break; |
| | | 126 | | } |
| | | 127 | | |
| | 6 | 128 | | foreach (Microsoft.IdentityModel.Xml.X509Data x509Data in x509Datas) |
| | | 129 | | { |
| | 2 | 130 | | if (x509Data?.Certificates == null) |
| | | 131 | | { |
| | | 132 | | continue; |
| | | 133 | | } |
| | | 134 | | |
| | 6 | 135 | | foreach (string base64Cert in x509Data.Certificates) |
| | | 136 | | { |
| | 2 | 137 | | if (string.IsNullOrEmpty(base64Cert)) |
| | | 138 | | { |
| | | 139 | | continue; |
| | | 140 | | } |
| | | 141 | | |
| | 2 | 142 | | X509Certificate2 cert = new X509Certificate2(Convert.FromBase64String(base64Cert)); |
| | 2 | 143 | | yield return new MsIdentityTokens.X509SecurityKey(cert); |
| | | 144 | | } |
| | | 145 | | } |
| | 0 | 146 | | } |
| | | 147 | | |
| | | 148 | | private string IssuerValidate(string issuer, MsIdentityTokens.SecurityToken securityToken, TokenValidationParame |
| | | 149 | | { |
| | 32 | 150 | | IssuerNameRegistry issuerNameRegistry = GetSecurityTokenHandler(validationParameters).IssuerNameRegistry; |
| | 32 | 151 | | SecurityToken samlToken = (SecurityToken)validationParameters.PropertyBag[SAML_SECURITY_TOKEN]; |
| | 32 | 152 | | SecurityToken signingToken = null; |
| | | 153 | | |
| | 32 | 154 | | if (samlToken is SamlSecurityToken) |
| | | 155 | | { |
| | 32 | 156 | | signingToken = ((SamlSecurityToken)samlToken).SigningToken; |
| | | 157 | | } |
| | | 158 | | else |
| | | 159 | | { |
| | 0 | 160 | | signingToken = ((Saml2SecurityToken)samlToken).SigningToken; |
| | | 161 | | } |
| | | 162 | | |
| | 32 | 163 | | return issuerNameRegistry.GetIssuerName(signingToken, issuer); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | private bool AudienceValidator(IEnumerable<string> audienceUris, MsIdentityTokens.SecurityToken securityToken, T |
| | | 167 | | { |
| | 39 | 168 | | SamlSecurityTokenRequirement samlSecurityTokenRequirement = (SamlSecurityTokenRequirement)validationParamete |
| | 39 | 169 | | List<Uri> uris = new List<Uri>(); |
| | 162 | 170 | | foreach(string audienceUri in audienceUris) |
| | | 171 | | { |
| | 42 | 172 | | uris.Add(new Uri(audienceUri)); |
| | | 173 | | } |
| | | 174 | | //if there is error, throw. |
| | 39 | 175 | | samlSecurityTokenRequirement.ValidateAudienceRestriction(uris, GetSecurityTokenHandler(validationParameters) |
| | 33 | 176 | | return true; |
| | | 177 | | } |
| | | 178 | | |
| | 101 | 179 | | private SecurityTokenHandlerConfiguration GetSecurityTokenHandler(TokenValidationParameters validationParameters |
| | | 180 | | |
| | | 181 | | } |
| | | 182 | | } |