| | | 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.Security.Cryptography.X509Certificates; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Xml; |
| | | 10 | | using CoreWCF.IdentityModel.Selectors; |
| | | 11 | | using Microsoft.IdentityModel.Tokens.Saml; |
| | | 12 | | using MSSAmlSerializer = Microsoft.IdentityModel.Tokens.Saml.SamlSerializer; |
| | | 13 | | |
| | | 14 | | namespace CoreWCF.IdentityModel.Tokens |
| | | 15 | | { |
| | | 16 | | public class SamlSerializer |
| | | 17 | | { |
| | | 18 | | private readonly MSSAmlSerializer _mSSamlSerializer; |
| | | 19 | | |
| | 135 | 20 | | public SamlSerializer() |
| | | 21 | | { |
| | 135 | 22 | | _mSSamlSerializer = new MSSAmlSerializer(); |
| | 135 | 23 | | _mSSamlSerializer.DSigSerializer = new Saml.DSigSerializerExtended(); |
| | 135 | 24 | | } |
| | | 25 | | |
| | | 26 | | public virtual SamlSecurityToken ReadToken(XmlDictionaryReader reader, SecurityTokenSerializer keyInfoSerializer |
| | | 27 | | { |
| | 3 | 28 | | if (reader == null) |
| | | 29 | | { |
| | 0 | 30 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 31 | | } |
| | | 32 | | |
| | 3 | 33 | | if (keyInfoSerializer == null) |
| | | 34 | | { |
| | 0 | 35 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyInfoSerializer)); |
| | | 36 | | } |
| | | 37 | | |
| | 3 | 38 | | SecurityToken? signingToken = null; |
| | 3 | 39 | | SecurityKey? verificationKey = null; |
| | 3 | 40 | | var assertion = LoadAssertion(reader, keyInfoSerializer, outOfBandTokenResolver, out signingToken, out verif |
| | | 41 | | |
| | 3 | 42 | | if (assertion == null) |
| | | 43 | | { |
| | 0 | 44 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new SecurityTokenException(SR.Format(SR.SAMLUn |
| | | 45 | | } |
| | | 46 | | |
| | 3 | 47 | | var imSamlSecurityToken = new Microsoft.IdentityModel.Tokens.Saml.SamlSecurityToken(assertion); |
| | 3 | 48 | | var samlSecurityToken = new SamlSecurityToken(imSamlSecurityToken, new ReadOnlyCollection<SecurityKey?>(new |
| | 3 | 49 | | samlSecurityToken.SigningToken = signingToken; |
| | 3 | 50 | | SecurityKeyIdentifier securityKeyIdentifier = CreateSecurityKeyIdentifier(assertion.Signature.KeyInfo, keyIn |
| | | 51 | | |
| | 14 | 52 | | foreach (var stmt in assertion.Statements) |
| | | 53 | | { |
| | 4 | 54 | | SamlSubjectStatement samlSubject = (SamlSubjectStatement)stmt; |
| | 4 | 55 | | SecurityKeyIdentifier keyIdentifier = null; |
| | 4 | 56 | | SecurityToken securityToken = null; |
| | 4 | 57 | | if (samlSubject.Subject.KeyInfo != null) |
| | | 58 | | { |
| | 1 | 59 | | keyIdentifier = CreateSecurityKeyIdentifier(samlSubject.Subject.KeyInfo, keyInfoSerializer); |
| | 1 | 60 | | securityToken = ResolveSecurityToken(securityKeyIdentifier, outOfBandTokenResolver); |
| | | 61 | | } |
| | 4 | 62 | | var internalSamlSubjectStatement = new InternalSamlSubjectStatement(samlSubject, keyIdentifier, security |
| | 4 | 63 | | samlSecurityToken.SamlStatements.Add(internalSamlSubjectStatement); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | //do a final validation on signature. |
| | 3 | 67 | | if (signingToken is X509SecurityToken x509signingToken) |
| | | 68 | | { |
| | 1 | 69 | | Microsoft.IdentityModel.Tokens.X509SecurityKey? imX509SecurityKey = null; |
| | 1 | 70 | | var cryptoProviderFactory = new CoreWCF.Security.Sha1CryptoProviderFactory(); |
| | | 71 | | try |
| | | 72 | | { |
| | 1 | 73 | | imX509SecurityKey = new Microsoft.IdentityModel.Tokens.X509SecurityKey(x509signingToken.Certificate) |
| | 1 | 74 | | imX509SecurityKey.CryptoProviderFactory = cryptoProviderFactory; |
| | 1 | 75 | | assertion.Signature.Verify(imX509SecurityKey, cryptoProviderFactory); |
| | 1 | 76 | | } |
| | 0 | 77 | | catch (Microsoft.IdentityModel.Xml.XmlValidationException xve) |
| | | 78 | | { |
| | 0 | 79 | | if (xve.Message.Contains("SignatureMethod is not supported")) |
| | | 80 | | { |
| | 0 | 81 | | if (imX509SecurityKey?.PublicKey is null) |
| | | 82 | | { |
| | 0 | 83 | | throw new SecurityTokenException(SR.Format(SR.PublicKeyNotFoundInX509SecurityKey, assertion. |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | throw new SecurityTokenException(SR.Format(SR.SignatureMethodNotSupported, assertion.Signature.S |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | throw; |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | else |
| | | 93 | | { |
| | 2 | 94 | | Microsoft.IdentityModel.Tokens.SecurityKey msVerificationKey = TryCreateMicrosoftIdentityVerificationKey |
| | 2 | 95 | | if (msVerificationKey == null) |
| | | 96 | | { |
| | 0 | 97 | | throw new SecurityTokenException(SR.Format(SR.SamlSignatureVerificationKeyNotSupported, verification |
| | | 98 | | } |
| | | 99 | | |
| | 2 | 100 | | var cryptoProviderFactory = new CoreWCF.Security.Sha1CryptoProviderFactory(); |
| | 2 | 101 | | msVerificationKey.CryptoProviderFactory = cryptoProviderFactory; |
| | | 102 | | try |
| | | 103 | | { |
| | 2 | 104 | | assertion.Signature.Verify(msVerificationKey, cryptoProviderFactory); |
| | 1 | 105 | | } |
| | 1 | 106 | | catch (Microsoft.IdentityModel.Xml.XmlValidationException xve) |
| | | 107 | | { |
| | 1 | 108 | | if (xve.Message.Contains("SignatureMethod is not supported")) |
| | | 109 | | { |
| | 0 | 110 | | throw new SecurityTokenException(SR.Format(SR.SignatureMethodNotSupported, assertion.Signature.S |
| | | 111 | | } |
| | | 112 | | |
| | 1 | 113 | | throw; |
| | | 114 | | } |
| | | 115 | | } |
| | | 116 | | |
| | 2 | 117 | | return samlSecurityToken; |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | private static Microsoft.IdentityModel.Tokens.SecurityKey TryCreateMicrosoftIdentityVerificationKey(SecurityKey |
| | | 121 | | { |
| | 2 | 122 | | if (verificationKey is SymmetricSecurityKey symmetricKey) |
| | | 123 | | { |
| | 2 | 124 | | return new Microsoft.IdentityModel.Tokens.SymmetricSecurityKey(symmetricKey.GetSymmetricKey()); |
| | | 125 | | } |
| | | 126 | | |
| | 0 | 127 | | if (verificationKey is RsaSecurityKey rsaKey) |
| | | 128 | | { |
| | 0 | 129 | | System.Security.Cryptography.AsymmetricAlgorithm asym = |
| | 0 | 130 | | rsaKey.GetAsymmetricAlgorithm(SecurityAlgorithms.RsaSha256Signature, false) |
| | 0 | 131 | | ?? rsaKey.GetAsymmetricAlgorithm(SecurityAlgorithms.RsaSha1Signature, false); |
| | | 132 | | |
| | 0 | 133 | | if (asym is System.Security.Cryptography.RSA rsa) |
| | | 134 | | { |
| | 0 | 135 | | return new Microsoft.IdentityModel.Tokens.RsaSecurityKey(rsa); |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | 0 | 139 | | if (verificationKey is X509AsymmetricSecurityKey x509AsymKey) |
| | | 140 | | { |
| | 0 | 141 | | System.Security.Cryptography.AsymmetricAlgorithm asym = |
| | 0 | 142 | | x509AsymKey.GetAsymmetricAlgorithm(SecurityAlgorithms.RsaSha256Signature, false) |
| | 0 | 143 | | ?? x509AsymKey.GetAsymmetricAlgorithm(SecurityAlgorithms.RsaSha1Signature, false); |
| | | 144 | | |
| | 0 | 145 | | if (asym is System.Security.Cryptography.RSA rsa) |
| | | 146 | | { |
| | 0 | 147 | | return new Microsoft.IdentityModel.Tokens.RsaSecurityKey(rsa); |
| | | 148 | | } |
| | | 149 | | } |
| | | 150 | | |
| | 0 | 151 | | return null; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | private SamlAssertion LoadAssertion(XmlDictionaryReader reader, SecurityTokenSerializer keyInfoSerializer, Secur |
| | | 155 | | { |
| | 3 | 156 | | if (reader == null) |
| | 0 | 157 | | throw new ArgumentNullException(nameof(reader)); |
| | | 158 | | |
| | 3 | 159 | | SamlAssertion assertion = _mSSamlSerializer.ReadAssertion(reader); |
| | 3 | 160 | | SecurityKeyIdentifier securityKeyIdentifier = CreateSecurityKeyIdentifier(assertion.Signature.KeyInfo, keyIn |
| | 3 | 161 | | CoreWCF.IdentityModel.SecurityKeyIdentifierClause? securityKeyIdentifierClause = null; |
| | 3 | 162 | | verificationKey = null; |
| | 3 | 163 | | signingToken = null; |
| | 3 | 164 | | if (securityKeyIdentifier.Count < 2 /*|| LocalAppContextSwitches.ProcessMultipleSecurityKeyIdentifierClauses |
| | | 165 | | { |
| | 3 | 166 | | verificationKey = SamlSerializer.ResolveSecurityKey(securityKeyIdentifier, outOfBandTokenResolver); |
| | | 167 | | } |
| | | 168 | | else |
| | | 169 | | { |
| | 0 | 170 | | verificationKey = ResolveSecurityKey(securityKeyIdentifier, outOfBandTokenResolver, out securityKeyIdent |
| | | 171 | | } |
| | | 172 | | |
| | 3 | 173 | | if (verificationKey == null) |
| | | 174 | | { |
| | 0 | 175 | | throw new SecurityTokenException(SR.Format(SR.SAMLUnableToResolveSignatureKey, assertion.Issuer)); |
| | | 176 | | } |
| | | 177 | | |
| | 3 | 178 | | if (securityKeyIdentifier.Count < 2 /*|| LocalAppContextSwitches.ProcessMultipleSecurityKeyIdentifierClauses |
| | | 179 | | { |
| | 3 | 180 | | signingToken = SamlSerializer.ResolveSecurityToken(securityKeyIdentifier, outOfBandTokenResolver); |
| | | 181 | | } |
| | | 182 | | else |
| | | 183 | | { |
| | 0 | 184 | | signingToken = SamlSerializer.ResolveSecurityToken(new SecurityKeyIdentifier(securityKeyIdentifierClause |
| | | 185 | | } |
| | | 186 | | |
| | 3 | 187 | | if (signingToken == null) |
| | | 188 | | { |
| | 0 | 189 | | throw new SecurityTokenException(SR.SamlSigningTokenNotFound); |
| | | 190 | | } |
| | | 191 | | |
| | 3 | 192 | | return assertion; |
| | | 193 | | } |
| | | 194 | | |
| | | 195 | | private static SecurityKey ResolveSecurityKey(SecurityKeyIdentifier ski, SecurityTokenResolver tokenResolver, ou |
| | | 196 | | { |
| | 0 | 197 | | if (ski == null) |
| | 0 | 198 | | throw new ArgumentNullException(nameof(ski)); |
| | | 199 | | |
| | 0 | 200 | | clause = null; |
| | | 201 | | |
| | 0 | 202 | | if (tokenResolver != null) |
| | | 203 | | { |
| | 0 | 204 | | for (int i = 0; i < ski.Count; ++i) |
| | | 205 | | { |
| | 0 | 206 | | SecurityKey? key = null; |
| | 0 | 207 | | if (tokenResolver.TryResolveSecurityKey(ski[i], out key)) |
| | | 208 | | { |
| | 0 | 209 | | clause = ski[i]; |
| | 0 | 210 | | return key; |
| | | 211 | | } |
| | | 212 | | } |
| | | 213 | | } |
| | | 214 | | |
| | 0 | 215 | | if (ski.CanCreateKey) |
| | | 216 | | { |
| | 0 | 217 | | foreach (var skiClause in ski) |
| | | 218 | | { |
| | 0 | 219 | | if (skiClause.CanCreateKey) |
| | | 220 | | { |
| | 0 | 221 | | clause = skiClause; |
| | 0 | 222 | | return clause.CreateKey(); |
| | | 223 | | } |
| | | 224 | | } |
| | | 225 | | |
| | 0 | 226 | | throw new InvalidOperationException(SR.KeyIdentifierCannotCreateKey); |
| | | 227 | | } |
| | | 228 | | |
| | 0 | 229 | | return null; |
| | 0 | 230 | | } |
| | | 231 | | |
| | | 232 | | |
| | | 233 | | internal static SecurityKey ResolveSecurityKey(SecurityKeyIdentifier ski, SecurityTokenResolver tokenResolver) |
| | | 234 | | { |
| | 3 | 235 | | if (ski == null) |
| | 0 | 236 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(ski)); |
| | | 237 | | |
| | 3 | 238 | | if (tokenResolver != null) |
| | | 239 | | { |
| | 6 | 240 | | for (int i = 0; i < ski.Count; ++i) |
| | | 241 | | { |
| | 3 | 242 | | if (tokenResolver.TryResolveSecurityKey(ski[i], out SecurityKey key)) |
| | 3 | 243 | | return key; |
| | | 244 | | } |
| | | 245 | | } |
| | | 246 | | |
| | 0 | 247 | | if (ski.CanCreateKey) |
| | 0 | 248 | | return ski.CreateKey(); |
| | | 249 | | |
| | 0 | 250 | | return null; |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | internal static SecurityToken ResolveSecurityToken(SecurityKeyIdentifier ski, SecurityTokenResolver tokenResolve |
| | | 254 | | { |
| | 4 | 255 | | SecurityToken token = null; |
| | | 256 | | |
| | 4 | 257 | | if (tokenResolver != null) |
| | | 258 | | { |
| | 4 | 259 | | tokenResolver.TryResolveToken(ski, out token); |
| | | 260 | | } |
| | | 261 | | |
| | 4 | 262 | | if (token == null) |
| | | 263 | | { |
| | | 264 | | // Check if this is a RSA key. |
| | 0 | 265 | | if (ski.TryFind(out RsaKeyIdentifierClause rsaClause)) |
| | 0 | 266 | | token = new RsaSecurityToken(rsaClause.Rsa); |
| | | 267 | | } |
| | | 268 | | |
| | 4 | 269 | | if (token == null) |
| | | 270 | | { |
| | | 271 | | // Check if this is a X509RawDataKeyIdentifier Clause. |
| | 0 | 272 | | if (ski.TryFind(out X509RawDataKeyIdentifierClause rawDataKeyIdentifierClause)) |
| | 0 | 273 | | token = new X509SecurityToken(new X509Certificate2(rawDataKeyIdentifierClause.GetX509RawData())); |
| | | 274 | | } |
| | | 275 | | |
| | 4 | 276 | | return token; |
| | | 277 | | } |
| | | 278 | | |
| | | 279 | | internal static SecurityKeyIdentifier CreateSecurityKeyIdentifier(Microsoft.IdentityModel.Xml.KeyInfo keyInfo, S |
| | | 280 | | { |
| | 7 | 281 | | var ski = new SecurityKeyIdentifier(); |
| | 7 | 282 | | if (keyInfo == null) |
| | | 283 | | { |
| | 0 | 284 | | return ski; |
| | | 285 | | } |
| | | 286 | | |
| | 7 | 287 | | if (keyInfo.RSAKeyValue != null) |
| | | 288 | | { |
| | 0 | 289 | | throw new InvalidOperationException(SR.SamlRSANotSupported); |
| | | 290 | | } |
| | | 291 | | |
| | 14 | 292 | | foreach (var objdata in keyInfo.X509Data) |
| | | 293 | | { |
| | 0 | 294 | | foreach (string certificateStr in objdata.Certificates) |
| | | 295 | | { |
| | 0 | 296 | | byte[] data = Convert.FromBase64String(certificateStr); |
| | 0 | 297 | | ski.Add(new X509RawDataKeyIdentifierClause(data)); |
| | | 298 | | } |
| | | 299 | | |
| | 0 | 300 | | if (objdata.IssuerSerial != null) |
| | | 301 | | { |
| | 0 | 302 | | ski.Add(new X509IssuerSerialKeyIdentifierClause(objdata.IssuerSerial.IssuerName, objdata.IssuerSeria |
| | | 303 | | } |
| | | 304 | | |
| | 0 | 305 | | if (!string.IsNullOrWhiteSpace(objdata.SKI)) |
| | | 306 | | { |
| | 0 | 307 | | byte[] data = Convert.FromBase64String(objdata.SKI); |
| | 0 | 308 | | ski.Add(new X509SubjectKeyIdentifierClause(data)); |
| | | 309 | | } |
| | | 310 | | } |
| | | 311 | | |
| | 7 | 312 | | if (keyInfo is Saml.KeyInfo coreWcfKeyInfo) |
| | | 313 | | { |
| | 7 | 314 | | if (coreWcfKeyInfo.SecurityTokenReference != null) |
| | | 315 | | { |
| | 6 | 316 | | var strIdentifierClause = CreateStrSecurityKeyIdentifier(coreWcfKeyInfo.SecurityTokenReference, keyI |
| | 6 | 317 | | if (strIdentifierClause != null) |
| | | 318 | | { |
| | 6 | 319 | | ski.Add(strIdentifierClause); |
| | | 320 | | } |
| | | 321 | | } |
| | | 322 | | } |
| | | 323 | | |
| | 7 | 324 | | return ski; |
| | | 325 | | } |
| | | 326 | | |
| | | 327 | | private static SecurityKeyIdentifierClause? CreateStrSecurityKeyIdentifier(Saml.SecurityTokenReference securityT |
| | | 328 | | { |
| | 6 | 329 | | var ski = securityTokenReference.SecurityKeyIdentifier; |
| | 6 | 330 | | var strString = $""" |
| | 6 | 331 | | <o:SecurityTokenReference xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-se |
| | 6 | 332 | | <o:KeyIdentifier ValueType="{ski.ValueType}" EncodingType="{ski.EncodingType}">{ski.Value}</o:KeyIdent |
| | 6 | 333 | | </o:SecurityTokenReference> |
| | 6 | 334 | | """; |
| | | 335 | | |
| | 6 | 336 | | var dictReader = XmlDictionaryReader.CreateTextReader(Encoding.UTF8.GetBytes(strString), XmlDictionaryReader |
| | 6 | 337 | | dictReader.MoveToContent(); |
| | 6 | 338 | | if (!keyInfoSerializer.CanReadKeyIdentifierClause(dictReader)) |
| | | 339 | | { |
| | 0 | 340 | | return null; |
| | | 341 | | } |
| | | 342 | | |
| | 6 | 343 | | return keyInfoSerializer.ReadKeyIdentifierClause(dictReader); |
| | | 344 | | } |
| | | 345 | | |
| | | 346 | | } |
| | | 347 | | } |