< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.Tokens.IssuedSecurityTokenParameters
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/Tokens/IssuedSecurityTokenParameters.cs
Line coverage
13%
Covered lines: 51
Uncovered lines: 324
Coverable lines: 375
Total lines: 905
Line coverage: 13.6%
Branch coverage
2%
Covered branches: 9
Total branches: 316
Branch coverage: 2.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/Tokens/IssuedSecurityTokenParameters.cs

#LineLine coverage
 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
 4using System;
 5using System.Collections.ObjectModel;
 6using System.Globalization;
 7using System.Text;
 8using System.Xml;
 9using CoreWCF.Channels;
 10using CoreWCF.IdentityModel;
 11using CoreWCF.IdentityModel.Selectors;
 12using CoreWCF.IdentityModel.Tokens;
 13using CoreWCF.Runtime;
 14
 15namespace CoreWCF.Security.Tokens
 16{
 17    public class IssuedSecurityTokenParameters : SecurityTokenParameters
 18    {
 19        private const string WsidPrefix = "wsid";
 20        private const string WsidNamespace = "http://schemas.xmlsoap.org/ws/2005/05/identity";
 021        private static readonly string s_wsidPPIClaim = string.Format(CultureInfo.InvariantCulture, "{0}/claims/privatep
 22        internal const SecurityKeyType DefaultKeyType = SecurityKeyType.SymmetricKey;
 23        internal const bool DefaultUseStrTransform = false;
 24        private int _keySize;
 25        private SecurityKeyType _keyType = DefaultKeyType;
 26
 27        protected IssuedSecurityTokenParameters(IssuedSecurityTokenParameters other)
 7028            : base(other)
 29        {
 7030            DefaultMessageSecurityVersion = other.DefaultMessageSecurityVersion;
 7031            IssuerAddress = other.IssuerAddress;
 7032            _keyType = other._keyType;
 7033            TokenType = other.TokenType;
 7034            _keySize = other._keySize;
 7035            UseStrTransform = other.UseStrTransform;
 36
 42037            foreach (XmlElement parameter in other.AdditionalRequestParameters)
 38            {
 14039                AdditionalRequestParameters.Add((XmlElement)parameter.Clone());
 40            }
 14041            foreach (ClaimTypeRequirement c in other.ClaimTypeRequirements)
 42            {
 043                ClaimTypeRequirements.Add(c);
 44            }
 7045            if (other.IssuerBinding != null)
 46            {
 047                IssuerBinding = new CustomBinding(other.IssuerBinding);
 48            }
 7049            IssuerMetadataAddress = other.IssuerMetadataAddress;
 7050        }
 51
 52        public IssuedSecurityTokenParameters()
 253            : this(null, null, null)
 54        {
 55            // empty
 256        }
 57
 58        public IssuedSecurityTokenParameters(string tokenType)
 059            : this(tokenType, null, null)
 60        {
 61            // empty
 062        }
 63
 64        public IssuedSecurityTokenParameters(string tokenType, EndpointAddress issuerAddress)
 065            : this(tokenType, issuerAddress, null)
 66        {
 67            // empty
 068        }
 69
 70        public IssuedSecurityTokenParameters(string tokenType, EndpointAddress issuerAddress, Binding issuerBinding)
 5271            : base()
 72        {
 5273            TokenType = tokenType;
 5274            IssuerAddress = issuerAddress;
 5275            IssuerBinding = issuerBinding;
 5276        }
 77
 078        protected internal override bool HasAsymmetricKey { get { return KeyType == SecurityKeyType.AsymmetricKey; } }
 79
 43280        public Collection<XmlElement> AdditionalRequestParameters { get; } = new Collection<XmlElement>();
 81
 14482        public MessageSecurityVersion DefaultMessageSecurityVersion { get; set; }
 83
 19484        public EndpointAddress IssuerAddress { get; set; }
 85
 19086        public EndpointAddress IssuerMetadataAddress { get; set; }
 87
 12488        public Binding IssuerBinding { get; set; }
 89
 90        public SecurityKeyType KeyType
 91        {
 92            get
 93            {
 5294                return _keyType;
 95            }
 96            set
 97            {
 5298                SecurityKeyTypeHelper.Validate(value);
 5299                _keyType = value;
 52100            }
 101        }
 102
 103        public int KeySize
 104        {
 105            get
 106            {
 2107                return _keySize;
 108            }
 109            set
 110            {
 52111                if (value < 0)
 112                {
 0113                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 114                }
 115
 52116                _keySize = value;
 52117            }
 118        }
 119
 142120        public bool UseStrTransform { get; set; } = DefaultUseStrTransform;
 121
 192122        public Collection<ClaimTypeRequirement> ClaimTypeRequirements { get; } = new Collection<ClaimTypeRequirement>();
 123
 194124        public string TokenType { get; set; }
 125
 0126        protected internal override bool SupportsClientAuthentication { get { return true; } }
 0127        protected internal override bool SupportsServerAuthentication { get { return true; } }
 0128        protected internal override bool SupportsClientWindowsIdentity { get { return false; } }
 129
 130        protected override SecurityTokenParameters CloneCore()
 131        {
 70132            return new IssuedSecurityTokenParameters(this);
 133        }
 134
 135        protected internal override SecurityKeyIdentifierClause CreateKeyIdentifierClause(SecurityToken token, SecurityT
 136        {
 0137            if (token is GenericXmlSecurityToken)
 138            {
 0139                return CreateGenericXmlTokenKeyIdentifierClause(token, referenceStyle);
 140            }
 141            else
 142            {
 0143                throw new NotImplementedException();
 144            }
 145            //TODO
 146            //  return this.CreateKeyIdentifierClause<SamlAssertionKeyIdentifierClause, SamlAssertionKeyIdentifierClause
 147        }
 148
 149        internal void SetRequestParameters(Collection<XmlElement> requestParameters, TrustDriver trustDriver)
 150        {
 0151            if (requestParameters == null)
 152            {
 0153                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(requestParameters));
 154            }
 155
 0156            if (trustDriver == null)
 157            {
 0158                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(trustDriver));
 159            }
 160
 0161            Collection<XmlElement> unknownRequestParameters = new Collection<XmlElement>();
 162
 0163            foreach (XmlElement element in requestParameters)
 164            {
 0165                if (trustDriver.TryParseKeySizeElement(element, out int keySize))
 166                {
 0167                    _keySize = keySize;
 168                }
 0169                else if (trustDriver.TryParseKeyTypeElement(element, out SecurityKeyType keyType))
 170                {
 0171                    KeyType = keyType;
 172                }
 0173                else if (trustDriver.TryParseTokenTypeElement(element, out string tokenType))
 174                {
 0175                    TokenType = tokenType;
 176                }
 177                // Only copy RP policy to client policy for TrustFeb2005
 0178                else if (trustDriver.StandardsManager.TrustVersion == TrustVersion.WSTrustFeb2005)
 179                {
 0180                    if (trustDriver.TryParseRequiredClaimsElement(element, out Collection<XmlElement> requiredClaims))
 181                    {
 0182                        Collection<XmlElement> unrecognizedRequiredClaims = new Collection<XmlElement>();
 0183                        foreach (XmlElement claimRequirement in requiredClaims)
 184                        {
 0185                            if (claimRequirement.LocalName == "ClaimType" && claimRequirement.NamespaceURI == WsidNamesp
 186                            {
 0187                                string claimValue = claimRequirement.GetAttribute("Uri", string.Empty);
 0188                                if (!string.IsNullOrEmpty(claimValue))
 189                                {
 190                                    ClaimTypeRequirement claimTypeRequirement;
 0191                                    string optional = claimRequirement.GetAttribute("Optional", string.Empty);
 0192                                    if (string.IsNullOrEmpty(optional))
 193                                    {
 0194                                        claimTypeRequirement = new ClaimTypeRequirement(claimValue);
 195                                    }
 196                                    else
 197                                    {
 0198                                        claimTypeRequirement = new ClaimTypeRequirement(claimValue, XmlConvert.ToBoolean
 199                                    }
 200
 0201                                    ClaimTypeRequirements.Add(claimTypeRequirement);
 202                                }
 203                            }
 204                            else
 205                            {
 0206                                unrecognizedRequiredClaims.Add(claimRequirement);
 207                            }
 208                        }
 0209                        if (unrecognizedRequiredClaims.Count > 0)
 210                        {
 0211                            unknownRequestParameters.Add(trustDriver.CreateRequiredClaimsElement(unrecognizedRequiredCla
 212                        }
 213                    }
 214                    else
 215                    {
 0216                        unknownRequestParameters.Add(element);
 217                    }
 218                }
 219            }
 220
 0221            unknownRequestParameters = trustDriver.ProcessUnknownRequestParameters(unknownRequestParameters, requestPara
 0222            if (unknownRequestParameters.Count > 0)
 223            {
 0224                for (int i = 0; i < unknownRequestParameters.Count; ++i)
 225                {
 0226                    AdditionalRequestParameters.Add(unknownRequestParameters[i]);
 227                }
 228            }
 0229        }
 230
 231        public Collection<XmlElement> CreateRequestParameters(MessageSecurityVersion messageSecurityVersion, SecurityTok
 232        {
 0233            return CreateRequestParameters(SecurityUtils.CreateSecurityStandardsManager(messageSecurityVersion, security
 234        }
 235
 236        internal Collection<XmlElement> CreateRequestParameters(TrustDriver driver)
 237        {
 0238            if (driver == null)
 239            {
 0240                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(driver));
 241            }
 242
 0243            Collection<XmlElement> result = new Collection<XmlElement>();
 244
 0245            if (TokenType != null)
 246            {
 0247                result.Add(driver.CreateTokenTypeElement(TokenType));
 248            }
 249
 0250            result.Add(driver.CreateKeyTypeElement(_keyType));
 251
 0252            if (_keySize != 0)
 253            {
 0254                result.Add(driver.CreateKeySizeElement(_keySize));
 255            }
 0256            if (ClaimTypeRequirements.Count > 0)
 257            {
 0258                Collection<XmlElement> claimsElements = new Collection<XmlElement>();
 0259                XmlDocument doc = new XmlDocument();
 0260                foreach (ClaimTypeRequirement claimType in ClaimTypeRequirements)
 261                {
 0262                    XmlElement element = doc.CreateElement(WsidPrefix, "ClaimType", WsidNamespace);
 0263                    XmlAttribute attr = doc.CreateAttribute("Uri");
 0264                    attr.Value = claimType.ClaimType;
 0265                    element.Attributes.Append(attr);
 0266                    if (claimType.IsOptional != ClaimTypeRequirement.DefaultIsOptional)
 267                    {
 0268                        attr = doc.CreateAttribute("Optional");
 0269                        attr.Value = XmlConvert.ToString(claimType.IsOptional);
 0270                        element.Attributes.Append(attr);
 271                    }
 0272                    claimsElements.Add(element);
 273                }
 0274                result.Add(driver.CreateRequiredClaimsElement(claimsElements));
 275            }
 276
 0277            if (AdditionalRequestParameters.Count > 0)
 278            {
 0279                Collection<XmlElement> trustNormalizedParameters = NormalizeAdditionalParameters(AdditionalRequestParame
 0280                                                                                                 driver,
 0281                                                                                                 (ClaimTypeRequirements.
 282
 0283                foreach (XmlElement parameter in trustNormalizedParameters)
 284                {
 0285                    result.Add(parameter);
 286                }
 287            }
 288
 0289            return result;
 290        }
 291
 292        private Collection<XmlElement> NormalizeAdditionalParameters(Collection<XmlElement> additionalParameters,
 293                                                                     TrustDriver driver,
 294                                                                     bool clientSideClaimTypeRequirementsSpecified)
 295        {
 296            // Ensure STS trust version is one of the currently supported versions: Feb 05 / Trust 1.3
 297            Fx.Assert(((driver.StandardsManager.TrustVersion == TrustVersion.WSTrustFeb2005) ||
 298                                           (driver.StandardsManager.TrustVersion == TrustVersion.WSTrust13)),
 299                                           "Unsupported trust version specified for the STS.");
 300
 301            // We have a mismatch. Make a local copy of additionalParameters for making any potential modifications
 302            // as part of normalization
 0303            Collection<XmlElement> tmpCollection = new Collection<XmlElement>();
 0304            foreach (XmlElement e in additionalParameters)
 305            {
 0306                tmpCollection.Add(e);
 307            }
 308
 309
 310            // 1. For Trust 1.3 EncryptionAlgorithm, CanonicalizationAlgorithm and KeyWrapAlgorithm should not be
 311            //    specified as top-level element if "SecondaryParameters" element already specifies this.
 0312            if (driver.StandardsManager.TrustVersion == TrustVersion.WSTrust13)
 313            {
 314                Fx.Assert(driver.GetType() == typeof(WSTrustDec2005.DriverDec2005), "Invalid Trust Driver specified for 
 315
 0316                XmlElement encryptionAlgorithmElement = null;
 0317                XmlElement canonicalizationAlgorithmElement = null;
 0318                XmlElement keyWrapAlgorithmElement = null;
 0319                XmlElement secondaryParameter = null;
 320
 0321                for (int i = 0; i < tmpCollection.Count; ++i)
 322                {
 0323                    if (driver.IsEncryptionAlgorithmElement(tmpCollection[i], out string algorithm))
 324                    {
 0325                        encryptionAlgorithmElement = tmpCollection[i];
 326                    }
 0327                    else if (driver.IsCanonicalizationAlgorithmElement(tmpCollection[i], out algorithm))
 328                    {
 0329                        canonicalizationAlgorithmElement = tmpCollection[i];
 330                    }
 0331                    else if (driver.IsKeyWrapAlgorithmElement(tmpCollection[i], out algorithm))
 332                    {
 0333                        keyWrapAlgorithmElement = tmpCollection[i];
 334                    }
 0335                    else if (((WSTrustDec2005.DriverDec2005)driver).IsSecondaryParametersElement(tmpCollection[i]))
 336                    {
 0337                        secondaryParameter = tmpCollection[i];
 338                    }
 339                }
 340
 0341                if (secondaryParameter != null)
 342                {
 0343                    foreach (XmlNode node in secondaryParameter.ChildNodes)
 344                    {
 0345                        if (node is XmlElement child)
 346                        {
 0347                            if (driver.IsEncryptionAlgorithmElement(child, out string algorithm) && (encryptionAlgorithm
 348                            {
 0349                                tmpCollection.Remove(encryptionAlgorithmElement);
 350                            }
 0351                            else if (driver.IsCanonicalizationAlgorithmElement(child, out algorithm) && (canonicalizatio
 352                            {
 0353                                tmpCollection.Remove(canonicalizationAlgorithmElement);
 354                            }
 0355                            else if (driver.IsKeyWrapAlgorithmElement(child, out algorithm) && (keyWrapAlgorithmElement 
 356                            {
 0357                                tmpCollection.Remove(keyWrapAlgorithmElement);
 358                            }
 359                        }
 360                    }
 361                }
 362            }
 363
 364            // 2. Check for Mismatch.
 365            //      a. Trust Feb 2005 -> Trust 1.3. do the following,
 366            //          (i) Copy EncryptionAlgorithm and CanonicalizationAlgorithm as the top-level elements.
 367            //              Note, this is in contradiction to step 1. But we don't have a choice here as we cannot say f
 368            //              Additional Parameters section in the config what came from the service and what came from th
 369            //          (ii) Convert SignWith and EncryptWith elements to Trust 1.3 namespace.
 370            //      b. For Trust 1.3 -> Trust Feb 2005, do the following,
 371            //          (i) Find EncryptionAlgorithm, CanonicalizationAlgorithm from inside the "SecondaryParameters" el
 372            //              If found, then promote these as the top-level elements replacing the existing values.
 373            //          (ii) Convert the SignWith and EncryptWith elements to the Trust Feb 2005 namespace and drop the 
 374            //               element.
 375
 376            // make an optimistic check to detect mismatched trust-versions between STS and RP
 0377            bool mismatch = (((driver.StandardsManager.TrustVersion == TrustVersion.WSTrustFeb2005) &&
 0378                              !CollectionContainsElementsWithTrustNamespace(additionalParameters, TrustFeb2005Strings.Na
 0379                             ((driver.StandardsManager.TrustVersion == TrustVersion.WSTrust13) &&
 0380                              !CollectionContainsElementsWithTrustNamespace(additionalParameters, TrustDec2005Strings.Na
 381            // if no mismatch, return unmodified collection
 0382            if (!mismatch)
 383            {
 0384                return tmpCollection;
 385            }
 386
 387            // 2.a
 388            // If we are talking to a Trust 1.3 STS, replace any Feb '05 algorithm parameters with their Trust 1.3 count
 0389            if (driver.StandardsManager.TrustVersion == TrustVersion.WSTrust13)
 390            {
 0391                SecurityStandardsManager trustFeb2005StandardsManager = SecurityStandardsManager.DefaultInstance;
 392                // the following cast is guaranteed to succeed
 0393                WSTrustFeb2005.DriverFeb2005 trustFeb2005Driver = (WSTrustFeb2005.DriverFeb2005)trustFeb2005StandardsMan
 394
 0395                for (int i = 0; i < tmpCollection.Count; i++)
 396                {
 0397                    if (trustFeb2005Driver.IsSignWithElement(tmpCollection[i], out string algorithmParameter))
 398                    {
 0399                        tmpCollection[i] = driver.CreateSignWithElement(algorithmParameter);
 400                    }
 0401                    else if (trustFeb2005Driver.IsEncryptWithElement(tmpCollection[i], out algorithmParameter))
 402                    {
 0403                        tmpCollection[i] = driver.CreateEncryptWithElement(algorithmParameter);
 404                    }
 0405                    else if (trustFeb2005Driver.IsEncryptionAlgorithmElement(tmpCollection[i], out algorithmParameter))
 406                    {
 0407                        tmpCollection[i] = driver.CreateEncryptionAlgorithmElement(algorithmParameter);
 408                    }
 0409                    else if (trustFeb2005Driver.IsCanonicalizationAlgorithmElement(tmpCollection[i], out algorithmParame
 410                    {
 0411                        tmpCollection[i] = driver.CreateCanonicalizationAlgorithmElement(algorithmParameter);
 412                    }
 413                }
 414            }
 415            else
 416            {
 417                // 2.b
 418                // We are talking to a Feb 05 STS. Filter out any SecondaryParameters element.
 0419                Collection<XmlElement> childrenToPromote = null;
 0420                WSSecurityTokenSerializer trust13Serializer = new WSSecurityTokenSerializer(SecurityVersion.WSSecurity11
 0421                                                                                            TrustVersion.WSTrust13,
 0422                                                                                            SecureConversationVersion.WS
 0423                                                                                            true, null, null, null);
 0424                SecurityStandardsManager trust13StandardsManager = new SecurityStandardsManager(MessageSecurityVersion.W
 425                // the following cast is guaranteed to succeed
 0426                WSTrustDec2005.DriverDec2005 trust13Driver = (WSTrustDec2005.DriverDec2005)trust13StandardsManager.Trust
 427
 0428                foreach (XmlElement parameter in tmpCollection)
 429                {
 430                    // check if SecondaryParameters is present
 0431                    if (trust13Driver.IsSecondaryParametersElement(parameter))
 432                    {
 0433                        childrenToPromote = new Collection<XmlElement>();
 434                        // walk SecondaryParameters and collect any 'non-standard' children
 0435                        foreach (XmlNode innerNode in parameter.ChildNodes)
 436                        {
 0437                            if ((innerNode is XmlElement innerElement) && CanPromoteToRoot(innerElement, trust13Driver, 
 438                            {
 0439                                childrenToPromote.Add(innerElement);
 440                            }
 441                        }
 442
 443                        // remove SecondaryParameters element
 0444                        tmpCollection.Remove(parameter);
 445
 446                        // we are done - break out of the loop
 0447                        break;
 448                    }
 449                }
 450
 451                // Probe of standard Trust elements and remember them.
 0452                if ((childrenToPromote != null) && (childrenToPromote.Count > 0))
 453                {
 0454                    XmlElement encryptionElement = null;
 0455                    XmlElement canonicalizationElement = null;
 0456                    XmlElement requiredClaimsElement = null;
 0457                    Collection<XmlElement> processedElements = new Collection<XmlElement>();
 458
 0459                    foreach (XmlElement e in childrenToPromote)
 460                    {
 0461                        if ((encryptionElement == null) && trust13Driver.IsEncryptionAlgorithmElement(e, out string encr
 462                        {
 0463                            encryptionElement = driver.CreateEncryptionAlgorithmElement(encryptionAlgorithm);
 0464                            processedElements.Add(e);
 465                        }
 0466                        else if ((canonicalizationElement == null) && trust13Driver.IsCanonicalizationAlgorithmElement(e
 467                        {
 0468                            canonicalizationElement = driver.CreateCanonicalizationAlgorithmElement(canonicalizationAlgo
 0469                            processedElements.Add(e);
 470                        }
 0471                        else if ((requiredClaimsElement == null) && trust13Driver.TryParseRequiredClaimsElement(e, out C
 472                        {
 0473                            requiredClaimsElement = driver.CreateRequiredClaimsElement(requiredClaims);
 0474                            processedElements.Add(e);
 475                        }
 476                    }
 477
 0478                    for (int i = 0; i < processedElements.Count; ++i)
 479                    {
 0480                        childrenToPromote.Remove(processedElements[i]);
 481                    }
 482
 0483                    XmlElement keyWrapAlgorithmElement = null;
 484
 485                    // Replace the appropriate elements.
 0486                    for (int i = 0; i < tmpCollection.Count; ++i)
 487                    {
 0488                        if (trust13Driver.IsSignWithElement(tmpCollection[i], out string algorithmParameter))
 489                        {
 0490                            tmpCollection[i] = driver.CreateSignWithElement(algorithmParameter);
 491                        }
 0492                        else if (trust13Driver.IsEncryptWithElement(tmpCollection[i], out algorithmParameter))
 493                        {
 0494                            tmpCollection[i] = driver.CreateEncryptWithElement(algorithmParameter);
 495                        }
 0496                        else if (trust13Driver.IsEncryptionAlgorithmElement(tmpCollection[i], out algorithmParameter) &&
 497                        {
 0498                            tmpCollection[i] = encryptionElement;
 0499                            encryptionElement = null;
 500                        }
 0501                        else if (trust13Driver.IsCanonicalizationAlgorithmElement(tmpCollection[i], out algorithmParamet
 502                        {
 0503                            tmpCollection[i] = canonicalizationElement;
 0504                            canonicalizationElement = null;
 505                        }
 0506                        else if (trust13Driver.IsKeyWrapAlgorithmElement(tmpCollection[i], out algorithmParameter) && (k
 507                        {
 0508                            keyWrapAlgorithmElement = tmpCollection[i];
 509                        }
 0510                        else if (trust13Driver.TryParseRequiredClaimsElement(tmpCollection[i], out Collection<XmlElement
 511                        {
 0512                            tmpCollection[i] = requiredClaimsElement;
 0513                            requiredClaimsElement = null;
 514                        }
 515                    }
 516
 0517                    if (keyWrapAlgorithmElement != null)
 518                    {
 519                        // Remove KeyWrapAlgorithmElement as this is not define in Trust Feb 2005.
 0520                        tmpCollection.Remove(keyWrapAlgorithmElement);
 521                    }
 522
 523                    // Add the remaining elements to the additionaParameters list to the end.
 0524                    if (encryptionElement != null)
 525                    {
 0526                        tmpCollection.Add(encryptionElement);
 527                    }
 528
 0529                    if (canonicalizationElement != null)
 530                    {
 0531                        tmpCollection.Add(canonicalizationElement);
 532                    }
 533
 0534                    if (requiredClaimsElement != null)
 535                    {
 0536                        tmpCollection.Add(requiredClaimsElement);
 537                    }
 538
 0539                    if (childrenToPromote.Count > 0)
 540                    {
 541                        // There are some non-standard elements. Just bump them to the top-level element.
 0542                        for (int i = 0; i < childrenToPromote.Count; ++i)
 543                        {
 0544                            tmpCollection.Add(childrenToPromote[i]);
 545                        }
 546                    }
 547                }
 548            }
 549
 0550            return tmpCollection;
 551        }
 552
 553        private bool CollectionContainsElementsWithTrustNamespace(Collection<XmlElement> collection, string trustNamespa
 554        {
 0555            for (int i = 0; i < collection.Count; i++)
 556            {
 0557                if ((collection[i] != null) && (collection[i].NamespaceURI == trustNamespace))
 558                {
 0559                    return true;
 560                }
 561            }
 0562            return false;
 563        }
 564
 565        private bool CanPromoteToRoot(XmlElement innerElement, WSTrustDec2005.DriverDec2005 trust13Driver, bool clientSi
 566        {
 567            // check if SecondaryParameters has claim requirements specified
 0568            if (trust13Driver.TryParseRequiredClaimsElement(innerElement, out Collection<XmlElement> dummyOutParamForReq
 569            {
 570                // if client has not specified any claim requirements, promote claim requirements
 571                // in SecondaryParameters to root level (and subsequently fix up the trust namespace)
 0572                return !clientSideClaimTypeRequirementsSpecified;
 573            }
 574
 575            // KeySize, KeyType and TokenType were converted to top-level property values when the WSDL was
 576            // imported, so drop it here. We check for EncryptWith and SignWith as these are Client specific algorithm v
 577            // don't have to promote the service specified values. KeyWrapAlgorithm was never sent in the RST
 578            // in V1 and hence we are dropping it here as well.
 0579            return (!trust13Driver.TryParseKeyTypeElement(innerElement, out SecurityKeyType dummyOutParamForKeyType) &&
 0580                    !trust13Driver.TryParseKeySizeElement(innerElement, out int dummyOutParamForKeySize) &&
 0581                    !trust13Driver.TryParseTokenTypeElement(innerElement, out string dummyStringOutParam) &&
 0582                    !trust13Driver.IsSignWithElement(innerElement, out dummyStringOutParam) &&
 0583                    !trust13Driver.IsEncryptWithElement(innerElement, out dummyStringOutParam) &&
 0584                    !trust13Driver.IsKeyWrapAlgorithmElement(innerElement, out dummyStringOutParam));
 585        }
 586
 587        internal void AddAlgorithmParameters(SecurityAlgorithmSuite algorithmSuite, SecurityStandardsManager standardsMa
 588        {
 50589            AdditionalRequestParameters.Insert(0, standardsManager.TrustDriver.CreateEncryptionAlgorithmElement(algorith
 50590            AdditionalRequestParameters.Insert(0, standardsManager.TrustDriver.CreateCanonicalizationAlgorithmElement(al
 591
 50592            if (_keyType == SecurityKeyType.BearerKey)
 593            {
 594                // As the client does not have a proof token in the Bearer case
 595                // we don't have any specific algorithms to request for.
 50596                return;
 597            }
 598
 0599            string signWithAlgorithm = (_keyType == SecurityKeyType.SymmetricKey) ? algorithmSuite.DefaultSymmetricSigna
 0600            AdditionalRequestParameters.Insert(0, standardsManager.TrustDriver.CreateSignWithElement(signWithAlgorithm))
 601            string encryptWithAlgorithm;
 0602            if (issuedKeyType == SecurityKeyType.SymmetricKey)
 603            {
 0604                encryptWithAlgorithm = algorithmSuite.DefaultEncryptionAlgorithm;
 605            }
 606            else
 607            {
 0608                encryptWithAlgorithm = algorithmSuite.DefaultAsymmetricKeyWrapAlgorithm;
 609            }
 0610            AdditionalRequestParameters.Insert(0, standardsManager.TrustDriver.CreateEncryptWithElement(encryptWithAlgor
 611
 0612            if (standardsManager.TrustVersion != TrustVersion.WSTrustFeb2005)
 613            {
 0614                AdditionalRequestParameters.Insert(0, ((WSTrustDec2005.DriverDec2005)standardsManager.TrustDriver).Creat
 615            }
 616
 0617            return;
 618        }
 619
 620        internal bool DoAlgorithmsMatch(SecurityAlgorithmSuite algorithmSuite, SecurityStandardsManager standardsManager
 621        {
 0622            bool doesSignWithAlgorithmMatch = false;
 0623            bool doesEncryptWithAlgorithmMatch = false;
 0624            bool doesEncryptionAlgorithmMatch = false;
 0625            bool doesCanonicalizationAlgorithmMatch = false;
 0626            bool doesKeyWrapAlgorithmMatch = false;
 0627            otherRequestParameters = new Collection<XmlElement>();
 0628            bool trustNormalizationPerformed = false;
 629
 630            Collection<XmlElement> trustVersionNormalizedParameterCollection;
 631
 632            // For Trust 1.3 we move all the additional parameters into the secondaryParameters
 633            // element. So the list contains just one element called SecondaryParameters that
 634            // contains all the other elements as child elements.
 0635            if ((standardsManager.TrustVersion == TrustVersion.WSTrust13) &&
 0636                (AdditionalRequestParameters.Count == 1) &&
 0637                (((WSTrustDec2005.DriverDec2005)standardsManager.TrustDriver).IsSecondaryParametersElement(AdditionalReq
 638            {
 0639                trustNormalizationPerformed = true;
 0640                trustVersionNormalizedParameterCollection = new Collection<XmlElement>();
 0641                foreach (XmlElement innerElement in AdditionalRequestParameters[0])
 642                {
 0643                    trustVersionNormalizedParameterCollection.Add(innerElement);
 644                }
 645            }
 646            else
 647            {
 0648                trustVersionNormalizedParameterCollection = AdditionalRequestParameters;
 649            }
 650
 0651            for (int i = 0; i < trustVersionNormalizedParameterCollection.Count; i++)
 652            {
 0653                XmlElement element = trustVersionNormalizedParameterCollection[i];
 0654                if (standardsManager.TrustDriver.IsCanonicalizationAlgorithmElement(element, out string algorithm))
 655                {
 0656                    if (algorithmSuite.DefaultCanonicalizationAlgorithm != algorithm)
 657                    {
 0658                        return false;
 659                    }
 0660                    doesCanonicalizationAlgorithmMatch = true;
 661                }
 0662                else if (standardsManager.TrustDriver.IsSignWithElement(element, out algorithm))
 663                {
 0664                    if ((_keyType == SecurityKeyType.SymmetricKey && algorithm != algorithmSuite.DefaultSymmetricSignatu
 0665                        || (_keyType == SecurityKeyType.AsymmetricKey && algorithm != algorithmSuite.DefaultAsymmetricSi
 666                    {
 0667                        return false;
 668                    }
 0669                    doesSignWithAlgorithmMatch = true;
 670                }
 0671                else if (standardsManager.TrustDriver.IsEncryptWithElement(element, out algorithm))
 672                {
 0673                    if ((_keyType == SecurityKeyType.SymmetricKey && algorithm != algorithmSuite.DefaultEncryptionAlgori
 0674                        || (_keyType == SecurityKeyType.AsymmetricKey && algorithm != algorithmSuite.DefaultAsymmetricKe
 675                    {
 0676                        return false;
 677                    }
 0678                    doesEncryptWithAlgorithmMatch = true;
 679                }
 0680                else if (standardsManager.TrustDriver.IsEncryptionAlgorithmElement(element, out algorithm))
 681                {
 0682                    if (algorithm != algorithmSuite.DefaultEncryptionAlgorithm)
 683                    {
 0684                        return false;
 685                    }
 0686                    doesEncryptionAlgorithmMatch = true;
 687                }
 0688                else if (standardsManager.TrustDriver.IsKeyWrapAlgorithmElement(element, out algorithm))
 689                {
 0690                    if (algorithm != algorithmSuite.DefaultAsymmetricKeyWrapAlgorithm)
 691                    {
 0692                        return false;
 693                    }
 0694                    doesKeyWrapAlgorithmMatch = true;
 695                }
 696                else
 697                {
 0698                    otherRequestParameters.Add(element);
 699                }
 700            }
 701
 702            // Undo normalization if performed
 703            // move all back into secondaryParameters
 0704            if (trustNormalizationPerformed)
 705            {
 0706                otherRequestParameters = AdditionalRequestParameters;
 707            }
 708
 0709            if (_keyType == SecurityKeyType.BearerKey)
 710            {
 711                // As the client does not have a proof token in the Bearer case
 712                // we don't have any specific algorithms to request for.
 0713                return true;
 714            }
 0715            if (standardsManager.TrustVersion == TrustVersion.WSTrustFeb2005)
 716            {
 717                // For V1 compatibility check all algorithms
 0718                return (doesSignWithAlgorithmMatch && doesCanonicalizationAlgorithmMatch && doesEncryptionAlgorithmMatch
 719            }
 720            else
 721            {
 0722                return (doesSignWithAlgorithmMatch && doesCanonicalizationAlgorithmMatch && doesEncryptionAlgorithmMatch
 723            }
 724        }
 725
 726        internal static IssuedSecurityTokenParameters CreateInfoCardParameters(SecurityStandardsManager standardsManager
 727        {
 0728            IssuedSecurityTokenParameters result = new IssuedSecurityTokenParameters(SecurityXXX2005Strings.SamlTokenTyp
 0729            {
 0730                KeyType = SecurityKeyType.AsymmetricKey
 0731            };
 0732            result.ClaimTypeRequirements.Add(new ClaimTypeRequirement(s_wsidPPIClaim));
 0733            result.IssuerAddress = null;
 0734            result.AddAlgorithmParameters(algorithm, standardsManager, result.KeyType);
 0735            return result;
 736        }
 737
 738        internal static bool IsInfoCardParameters(IssuedSecurityTokenParameters parameters, SecurityStandardsManager sta
 739        {
 0740            if (parameters == null)
 741            {
 0742                return false;
 743            }
 744
 0745            if (parameters.TokenType != SecurityXXX2005Strings.SamlTokenType)
 746            {
 0747                return false;
 748            }
 749
 0750            if (parameters.KeyType != SecurityKeyType.AsymmetricKey)
 751            {
 0752                return false;
 753            }
 754
 0755            if (parameters.ClaimTypeRequirements.Count == 1)
 756            {
 0757                if (!(parameters.ClaimTypeRequirements[0] is ClaimTypeRequirement claimTypeRequirement))
 758                {
 0759                    return false;
 760                }
 761
 0762                if (claimTypeRequirement.ClaimType != s_wsidPPIClaim)
 763                {
 0764                    return false;
 765                }
 766            }
 0767            else if ((parameters.AdditionalRequestParameters != null) && (parameters.AdditionalRequestParameters.Count >
 768            {
 769                // Check the AdditionalRequest Parameters to see if ClaimTypeRequirements got imported there.
 0770                bool claimTypeRequirementMatched = false;
 0771                XmlElement claimTypeRequirement = GetClaimTypeRequirement(parameters.AdditionalRequestParameters, standa
 0772                if (claimTypeRequirement != null && claimTypeRequirement.ChildNodes.Count == 1)
 773                {
 0774                    if (claimTypeRequirement.ChildNodes[0] is XmlElement claimTypeElement)
 775                    {
 0776                        XmlNode claimType = claimTypeElement.Attributes.GetNamedItem("Uri");
 0777                        if (claimType != null && claimType.Value == s_wsidPPIClaim)
 778                        {
 0779                            claimTypeRequirementMatched = true;
 780                        }
 781                    }
 782                }
 783
 0784                if (!claimTypeRequirementMatched)
 785                {
 0786                    return false;
 787                }
 788            }
 789            else
 790            {
 0791                return false;
 792            }
 0793            if (parameters.IssuerAddress != null)
 794            {
 0795                return false;
 796            }
 797
 0798            return true;
 799        }
 800
 801        // The method walks through the entire set of AdditionalRequestParameters and return the Claims Type requirement
 802        internal static XmlElement GetClaimTypeRequirement(Collection<XmlElement> additionalRequestParameters, SecurityS
 803        {
 0804            foreach (XmlElement requestParameter in additionalRequestParameters)
 805            {
 0806                if ((requestParameter.LocalName == ((CoreWCF.Security.WSTrust.Driver)standardsManager.TrustDriver).Drive
 0807                    (requestParameter.NamespaceURI == ((CoreWCF.Security.WSTrust.Driver)standardsManager.TrustDriver).Dr
 808                {
 0809                    return requestParameter;
 810                }
 811
 0812                if ((requestParameter.LocalName == DXD.TrustDec2005Dictionary.SecondaryParameters.Value) &&
 0813                    (requestParameter.NamespaceURI == DXD.TrustDec2005Dictionary.Namespace.Value))
 814                {
 0815                    Collection<XmlElement> secondaryParameters = new Collection<XmlElement>();
 0816                    foreach (XmlNode node in requestParameter.ChildNodes)
 817                    {
 0818                        if (node is XmlElement nodeAsElement)
 819                        {
 0820                            secondaryParameters.Add(nodeAsElement);
 821                        }
 822                    }
 0823                    XmlElement claimTypeRequirement = GetClaimTypeRequirement(secondaryParameters, standardsManager);
 0824                    if (claimTypeRequirement != null)
 825                    {
 0826                        return claimTypeRequirement;
 827                    }
 828                }
 829            }
 830
 0831            return null;
 0832        }
 833
 834        public override string ToString()
 835        {
 0836            StringBuilder sb = new StringBuilder();
 0837            sb.AppendLine(base.ToString());
 838
 0839            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "TokenType: {0}", TokenType ?? "null"));
 0840            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "KeyType: {0}", _keyType.ToString()));
 0841            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "KeySize: {0}", _keySize.ToString(CultureInfo.Inva
 0842            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "IssuerAddress: {0}", IssuerAddress == null ? "nul
 0843            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "IssuerMetadataAddress: {0}", IssuerMetadataAddres
 0844            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "DefaultMessgeSecurityVersion: {0}", DefaultMessag
 0845            sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "UseStrTransform: {0}", UseStrTransform.ToString()
 846
 0847            if (IssuerBinding == null)
 848            {
 0849                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "IssuerBinding: null"));
 850            }
 851            else
 852            {
 0853                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "IssuerBinding:"));
 0854                BindingElementCollection bindingElements = IssuerBinding.CreateBindingElements();
 0855                for (int i = 0; i < bindingElements.Count; i++)
 856                {
 0857                    sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "  BindingElement[{0}]:", i.ToString(Cultu
 0858                    sb.AppendLine("    " + bindingElements[i].ToString().Trim().Replace("\n", "\n    "));
 859                }
 860            }
 861
 0862            if (ClaimTypeRequirements.Count == 0)
 863            {
 0864                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "ClaimTypeRequirements: none"));
 865            }
 866            else
 867            {
 0868                sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "ClaimTypeRequirements:"));
 0869                for (int i = 0; i < ClaimTypeRequirements.Count; i++)
 870                {
 0871                    sb.AppendLine(string.Format(CultureInfo.InvariantCulture, "  {0}, optional={1}", ClaimTypeRequiremen
 872                }
 873            }
 874
 0875            return sb.ToString().Trim();
 876        }
 877
 878        protected internal override void InitializeSecurityTokenRequirement(SecurityTokenRequirement requirement)
 879        {
 2880            requirement.TokenType = TokenType;
 2881            requirement.RequireCryptographicToken = true;
 2882            requirement.KeyType = KeyType;
 883
 2884            if (requirement is ServiceModelSecurityTokenRequirement serviceModelSecurityTokenRequirement)
 885            {
 2886                serviceModelSecurityTokenRequirement.DefaultMessageSecurityVersion = DefaultMessageSecurityVersion;
 887            }
 888            else
 889            {
 0890                requirement.Properties[ServiceModelSecurityTokenRequirement.DefaultMessageSecurityVersionProperty] = Def
 891            }
 892
 2893            if (KeySize > 0)
 894            {
 0895                requirement.KeySize = KeySize;
 896            }
 2897            requirement.Properties[ServiceModelSecurityTokenRequirement.IssuerAddressProperty] = IssuerAddress;
 2898            if (IssuerBinding != null)
 899            {
 0900                requirement.Properties[ServiceModelSecurityTokenRequirement.IssuerBindingProperty] = IssuerBinding;
 901            }
 2902            requirement.Properties[ServiceModelSecurityTokenRequirement.IssuedSecurityTokenParametersProperty] = Clone()
 2903        }
 904    }
 905}

Methods/Properties

.cctor()
.ctor(CoreWCF.Security.Tokens.IssuedSecurityTokenParameters)
.ctor()
.ctor(System.String)
.ctor(System.String,CoreWCF.EndpointAddress)
.ctor(System.String,CoreWCF.EndpointAddress,CoreWCF.Channels.Binding)
HasAsymmetricKey()
AdditionalRequestParameters()
DefaultMessageSecurityVersion()
IssuerAddress()
IssuerMetadataAddress()
IssuerBinding()
KeyType()
KeyType(CoreWCF.IdentityModel.Tokens.SecurityKeyType)
KeySize()
KeySize(System.Int32)
UseStrTransform()
ClaimTypeRequirements()
TokenType()
SupportsClientAuthentication()
SupportsServerAuthentication()
SupportsClientWindowsIdentity()
CloneCore()
CreateKeyIdentifierClause(CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.Security.Tokens.SecurityTokenReferenceStyle)
SetRequestParameters(System.Collections.ObjectModel.Collection`1<System.Xml.XmlElement>,CoreWCF.Security.TrustDriver)
CreateRequestParameters(CoreWCF.MessageSecurityVersion,CoreWCF.IdentityModel.Selectors.SecurityTokenSerializer)
CreateRequestParameters(CoreWCF.Security.TrustDriver)
NormalizeAdditionalParameters(System.Collections.ObjectModel.Collection`1<System.Xml.XmlElement>,CoreWCF.Security.TrustDriver,System.Boolean)
CollectionContainsElementsWithTrustNamespace(System.Collections.ObjectModel.Collection`1<System.Xml.XmlElement>,System.String)
CanPromoteToRoot(System.Xml.XmlElement,CoreWCF.Security.WSTrustDec2005/DriverDec2005,System.Boolean)
AddAlgorithmParameters(CoreWCF.Security.SecurityAlgorithmSuite,CoreWCF.Security.SecurityStandardsManager,CoreWCF.IdentityModel.Tokens.SecurityKeyType)
DoAlgorithmsMatch(CoreWCF.Security.SecurityAlgorithmSuite,CoreWCF.Security.SecurityStandardsManager,System.Collections.ObjectModel.Collection`1<System.Xml.XmlElement>&)
CreateInfoCardParameters(CoreWCF.Security.SecurityStandardsManager,CoreWCF.Security.SecurityAlgorithmSuite)
IsInfoCardParameters(CoreWCF.Security.Tokens.IssuedSecurityTokenParameters,CoreWCF.Security.SecurityStandardsManager)
GetClaimTypeRequirement(System.Collections.ObjectModel.Collection`1<System.Xml.XmlElement>,CoreWCF.Security.SecurityStandardsManager)
ToString()
InitializeSecurityTokenRequirement(CoreWCF.IdentityModel.Selectors.SecurityTokenRequirement)