< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.WSTrust
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WSTrust.cs
Line coverage
24%
Covered lines: 184
Uncovered lines: 561
Coverable lines: 745
Total lines: 1685
Line coverage: 24.6%
Branch coverage
20%
Covered branches: 103
Total branches: 494
Branch coverage: 20.8%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor(...)100%11100%
PopulateTokenEntries(...)100%11100%
.ctor(...)83.33%6690%
GetTokenTypesCore()100%11100%
CanReadTokenCore(...)0%880%
CanReadTokenCore(...)50%44100%
CreateKeyIdentifierClauseFromTokenXmlCore(...)0%440%
ReadTokenCore(...)64.28%141475%
WriteTokenCore(...)100%44100%
.ctor(...)100%11100%
CreateRequestSecurityToken(...)95%202095.83%
GetIssuedTokenBuffer(...)0%440%
CreateRequestSecurityTokenResponse(...)0%38380%
CreateRequestSecurityTokenResponseCollection(...)0%440%
GetAppliesToElement(...)70%101075%
GetAppliesTo(...)0%220%
GetAppliesTo(...)0%220%
GetAppliesTo(...)0%220%
IsAppliesTo(...)0%220%
GetAppliesToQName(...)50%2240%
GetAppliesToQName(...)50%2275%
GetAppliesToQName(...)0%220%
GetAuthenticator(...)0%18180%
GetBinaryNegotiation(...)0%220%
GetBinaryNegotiation(...)0%220%
GetBinaryNegotiation(...)0%10100%
GetEntropy(...)50%2266.66%
GetEntropy(...)0%220%
GetEntropy(...)75%121280%
GetIssuedAndProofXml(...)0%20200%
GetIssuedToken(...)0%50500%
GetIssuedToken(...)0%12120%
IsAtRequestSecurityTokenResponse(...)0%220%
IsAtRequestSecurityTokenResponseCollection(...)0%220%
IsRequestedSecurityTokenElement(...)0%220%
IsRequestedProofTokenElement(...)0%220%
ReadBinaryNegotiation(...)0%24240%
ReadReferences(...)0%16160%
TryReadKeyIdentifierClause(...)0%440%
CreateGenericXmlSecurityKeyIdentifierClause(...)0%220%
GetKeyIdentifierXmlReferenceClause(...)0%220%
ReadRequestedTokenClosed(...)100%110%
ReadTargets(...)100%110%
OnRSTRorRSTRCMissingException()100%110%
WriteAppliesTo(...)100%110%
WriteBinaryNegotiation(...)0%220%
WriteRequestSecurityToken(...)0%24240%
WriteTargets(...)100%110%
WriteReferences(...)0%220%
WriteRequestedTokenClosed(...)100%110%
WriteRequestSecurityTokenResponse(...)72.22%363676.47%
WriteRequestSecurityTokenResponseCollection(...)75%4487.5%
SetProtectionLevelForFederation(...)0%10100%
TryParseKeySizeElement(...)0%660%
CreateKeySizeElement(...)0%220%
CreateKeyTypeElement(...)0%440%
TryParseKeyTypeElement(...)0%660%
TryParseSymmetricKeyElement(...)0%660%
CreateSymmetricKeyTypeElement()100%110%
TryParsePublicKeyElement(...)0%660%
CreatePublicKeyTypeElement()100%110%
TryParseTokenTypeElement(...)0%660%
CreateTokenTypeElement(...)0%220%
CreateUseKeyElement(...)0%440%
CreateSignWithElement(...)0%220%
IsSignWithElement(...)100%110%
CreateEncryptWithElement(...)0%220%
CreateEncryptionAlgorithmElement(...)50%2285.71%
IsEncryptWithElement(...)100%110%
IsEncryptionAlgorithmElement(...)100%110%
CreateComputedKeyAlgorithmElement(...)0%220%
CreateCanonicalizationAlgorithmElement(...)50%2285.71%
IsCanonicalizationAlgorithmElement(...)100%110%
TryParseRequiredClaimsElement(...)0%10100%
CreateRequiredClaimsElement(...)0%440%
ValidateRequestedKeySize(...)50%4466.66%
ValidateRequestorEntropy(...)70%101066.66%
ProcessRstAndIssueKey(...)35.71%141451.61%
CheckElement(...)0%660%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/WSTrust.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.Generic;
 6using System.Collections.ObjectModel;
 7using System.Globalization;
 8using System.IO;
 9using System.Runtime.Serialization;
 10using System.Security.Cryptography;
 11using System.Text;
 12using System.Xml;
 13using CoreWCF.Description;
 14using CoreWCF.Dispatcher;
 15using CoreWCF.IdentityModel;
 16using CoreWCF.IdentityModel.Policy;
 17using CoreWCF.IdentityModel.Selectors;
 18using CoreWCF.IdentityModel.Tokens;
 19using CoreWCF.Runtime;
 20using CoreWCF.Security.Tokens;
 21using HexBinary = CoreWCF.Security.SoapHexBinary;
 22using TokenEntry = CoreWCF.Security.WSSecurityTokenSerializer.TokenEntry;
 23
 24namespace CoreWCF.Security
 25{
 26    internal abstract class WSTrust : WSSecurityTokenSerializer.SerializerEntries
 27    {
 13228        public WSTrust(WSSecurityTokenSerializer tokenSerializer)
 29        {
 13230            WSSecurityTokenSerializer = tokenSerializer;
 13231        }
 32
 033        public WSSecurityTokenSerializer WSSecurityTokenSerializer { get; }
 34
 35        public abstract TrustDictionary SerializerDictionary
 36        {
 37            get;
 38        }
 39
 40        public override void PopulateTokenEntries(IList<TokenEntry> tokenEntryList)
 41        {
 13242            tokenEntryList.Add(new BinarySecretTokenEntry(this));
 13243        }
 44
 45        private class BinarySecretTokenEntry : TokenEntry
 46        {
 47            private readonly WSTrust _parent;
 48            private readonly TrustDictionary _otherDictionary;
 49
 13250            public BinarySecretTokenEntry(WSTrust parent)
 51            {
 13252                _parent = parent;
 13253                _otherDictionary = null;
 54
 13255                if (parent.SerializerDictionary is TrustDec2005Dictionary)
 56                {
 6757                    _otherDictionary = XD.TrustFeb2005Dictionary;
 58                }
 59
 13260                if (parent.SerializerDictionary is TrustFeb2005Dictionary)
 61                {
 6562                    _otherDictionary = DXD.TrustDec2005Dictionary;
 63                }
 64
 65                // always set it, so we don't have to worry about null
 13266                if (_otherDictionary == null)
 67                {
 068                    _otherDictionary = _parent.SerializerDictionary;
 69                }
 13270            }
 71
 1072            protected override XmlDictionaryString LocalName => _parent.SerializerDictionary.BinarySecret;
 1073            protected override XmlDictionaryString NamespaceUri => _parent.SerializerDictionary.Namespace;
 1074            protected override Type[] GetTokenTypesCore() { return new Type[] { typeof(BinarySecretSecurityToken) }; }
 075            public override string TokenTypeUri => null;
 1076            protected override string ValueTypeUri => null;
 77
 78            public override bool CanReadTokenCore(XmlElement element)
 79            {
 080                string valueTypeUri = null;
 81
 082                if (element.HasAttribute(SecurityJan2004Strings.ValueType, null))
 83                {
 084                    valueTypeUri = element.GetAttribute(SecurityJan2004Strings.ValueType, null);
 85                }
 86
 087                return element.LocalName == LocalName.Value && (element.NamespaceURI == NamespaceUri.Value || element.Na
 88            }
 89
 90            public override bool CanReadTokenCore(XmlDictionaryReader reader)
 91            {
 1092                return (reader.IsStartElement(LocalName, NamespaceUri) || reader.IsStartElement(LocalName, _otherDiction
 1093                       reader.GetAttribute(XD.SecurityJan2004Dictionary.ValueType, null) == ValueTypeUri;
 94            }
 95
 96
 97            public override SecurityKeyIdentifierClause CreateKeyIdentifierClauseFromTokenXmlCore(XmlElement issuedToken
 98                SecurityTokenReferenceStyle tokenReferenceStyle)
 99            {
 0100                TokenReferenceStyleHelper.Validate(tokenReferenceStyle);
 101
 102                switch (tokenReferenceStyle)
 103                {
 104                    case SecurityTokenReferenceStyle.Internal:
 0105                        return CreateDirectReference(issuedTokenXml, UtilityStrings.IdAttribute, UtilityStrings.Namespac
 106                    case SecurityTokenReferenceStyle.External:
 107                        // Binary Secret tokens aren't referred to externally
 0108                        return null;
 109                    default:
 0110                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof
 111                }
 112            }
 113
 114            public override SecurityToken ReadTokenCore(XmlDictionaryReader reader, SecurityTokenResolver tokenResolver)
 115            {
 10116                string secretType = reader.GetAttribute(XD.SecurityJan2004Dictionary.TypeAttribute, null);
 10117                string id = reader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace);
 10118                bool isNonce = false;
 119
 10120                if (secretType != null && secretType.Length > 0)
 121                {
 10122                    if (secretType == _parent.SerializerDictionary.NonceBinarySecret.Value || secretType == _otherDictio
 123                    {
 10124                        isNonce = true;
 125                    }
 0126                    else if (secretType != _parent.SerializerDictionary.SymmetricKeyBinarySecret.Value && secretType != 
 127                    {
 0128                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format
 129                    }
 130                }
 131
 10132                byte[] secret = reader.ReadElementContentAsBase64();
 10133                if (isNonce)
 134                {
 10135                    return new NonceToken(id, secret);
 136                }
 137                else
 138                {
 0139                    return new BinarySecretSecurityToken(id, secret);
 140                }
 141            }
 142
 143            public override void WriteTokenCore(XmlDictionaryWriter writer, SecurityToken token)
 144            {
 10145                BinarySecretSecurityToken simpleToken = token as BinarySecretSecurityToken;
 10146                byte[] secret = simpleToken.GetKeyBytes();
 10147                writer.WriteStartElement(_parent.SerializerDictionary.Prefix.Value, _parent.SerializerDictionary.BinaryS
 10148                if (simpleToken.Id != null)
 149                {
 10150                    writer.WriteAttributeString(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.IdAttribute, XD.
 151                }
 10152                if (token is NonceToken)
 153                {
 10154                    writer.WriteAttributeString(XD.SecurityJan2004Dictionary.TypeAttribute, null, _parent.SerializerDict
 155                }
 10156                writer.WriteBase64(secret, 0, secret.Length);
 10157                writer.WriteEndElement();
 10158            }
 159        }
 160
 161        public abstract class Driver : TrustDriver
 162        {
 163            private const string Base64Uri = SecurityJan2004Strings.EncodingTypeValueBase64Binary;
 164            private const string HexBinaryUri = SecurityJan2004Strings.EncodingTypeValueHexBinary;
 165            private readonly SecurityStandardsManager _standardsManager;
 166            private readonly List<SecurityTokenAuthenticator> _entropyAuthenticators;
 167
 131168            public Driver(SecurityStandardsManager standardsManager)
 169            {
 131170                _standardsManager = standardsManager;
 131171                _entropyAuthenticators = new List<SecurityTokenAuthenticator>(2);
 131172            }
 173
 174            public abstract TrustDictionary DriverDictionary
 175            {
 176                get;
 177            }
 178
 0179            public override XmlDictionaryString RequestSecurityTokenAction => DriverDictionary.RequestSecurityTokenIssua
 180
 0181            public override XmlDictionaryString RequestSecurityTokenResponseAction => DriverDictionary.RequestSecurityTo
 182
 10183            public override string RequestTypeIssue => DriverDictionary.RequestTypeIssue.Value;
 184
 0185            public override string ComputedKeyAlgorithm => DriverDictionary.Psha1ComputedKeyUri.Value;
 186
 30187            public override SecurityStandardsManager StandardsManager => _standardsManager;
 188
 0189            public override XmlDictionaryString Namespace => DriverDictionary.Namespace;
 190
 191            public override RequestSecurityToken CreateRequestSecurityToken(XmlReader xmlReader)
 192            {
 20193                XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader);
 20194                reader.MoveToStartElement(DriverDictionary.RequestSecurityToken, DriverDictionary.Namespace);
 20195                string context = null;
 20196                string tokenTypeUri = null;
 20197                string requestType = null;
 20198                int keySize = 0;
 20199                XmlDocument doc = new XmlDocument();
 20200                XmlElement rstXml = (doc.ReadNode(reader) as XmlElement);
 80201                for (int i = 0; i < rstXml.Attributes.Count; ++i)
 202                {
 20203                    XmlAttribute attr = rstXml.Attributes[i];
 20204                    if (attr.LocalName == DriverDictionary.Context.Value)
 205                    {
 0206                        context = attr.Value;
 207                    }
 208                }
 160209                for (int i = 0; i < rstXml.ChildNodes.Count; ++i)
 210                {
 60211                    XmlElement child = (rstXml.ChildNodes[i] as XmlElement);
 60212                    if (child != null)
 213                    {
 60214                        if (child.LocalName == DriverDictionary.TokenType.Value && child.NamespaceURI == DriverDictionar
 215                        {
 10216                            tokenTypeUri = XmlHelper.ReadTextElementAsTrimmedString(child);
 217                        }
 50218                        else if (child.LocalName == DriverDictionary.RequestType.Value && child.NamespaceURI == DriverDi
 219                        {
 20220                            requestType = XmlHelper.ReadTextElementAsTrimmedString(child);
 221                        }
 30222                        else if (child.LocalName == DriverDictionary.KeySize.Value && child.NamespaceURI == DriverDictio
 223                        {
 10224                            keySize = int.Parse(XmlHelper.ReadTextElementAsTrimmedString(child), NumberFormatInfo.Invari
 225                        }
 226                    }
 227                }
 228
 20229                ReadTargets(rstXml, out SecurityKeyIdentifierClause renewTarget, out SecurityKeyIdentifierClause closeTa
 230
 20231                RequestSecurityToken rst = new RequestSecurityToken(_standardsManager, rstXml, context, tokenTypeUri, re
 20232                return rst;
 233            }
 234
 235            private CoreWCF.XmlBuffer GetIssuedTokenBuffer(CoreWCF.XmlBuffer rstrBuffer)
 236            {
 0237                CoreWCF.XmlBuffer issuedTokenBuffer = null;
 0238                using (XmlDictionaryReader reader = rstrBuffer.GetReader(0))
 239                {
 0240                    reader.ReadFullStartElement();
 0241                    while (reader.IsStartElement())
 242                    {
 0243                        if (reader.IsStartElement(DriverDictionary.RequestedSecurityToken, DriverDictionary.Namespace))
 244                        {
 0245                            reader.ReadStartElement();
 0246                            reader.MoveToContent();
 0247                            issuedTokenBuffer = new CoreWCF.XmlBuffer(int.MaxValue);
 0248                            using (XmlDictionaryWriter writer = issuedTokenBuffer.OpenSection(reader.Quotas))
 249                            {
 0250                                writer.WriteNode(reader, false);
 0251                                issuedTokenBuffer.CloseSection();
 0252                                issuedTokenBuffer.Close();
 0253                            }
 0254                            reader.ReadEndElement();
 0255                            break;
 256                        }
 257                        else
 258                        {
 0259                            reader.Skip();
 260                        }
 261                    }
 0262                }
 0263                return issuedTokenBuffer;
 264            }
 265
 266            public override RequestSecurityTokenResponse CreateRequestSecurityTokenResponse(XmlReader xmlReader)
 267            {
 0268                if (xmlReader == null)
 269                {
 0270                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(xmlReader));
 271                }
 0272                XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader);
 0273                if (reader.IsStartElement(DriverDictionary.RequestSecurityTokenResponse, DriverDictionary.Namespace) == 
 274                {
 0275                    XmlHelper.OnRequiredElementMissing(DriverDictionary.RequestSecurityTokenResponse.Value, DriverDictio
 276                }
 277
 0278                CoreWCF.XmlBuffer rstrBuffer = new CoreWCF.XmlBuffer(int.MaxValue);
 0279                using (XmlDictionaryWriter writer = rstrBuffer.OpenSection(reader.Quotas))
 280                {
 0281                    writer.WriteNode(reader, false);
 0282                    rstrBuffer.CloseSection();
 0283                    rstrBuffer.Close();
 0284                }
 0285                XmlDocument doc = new XmlDocument();
 286                XmlElement rstrXml;
 0287                using (XmlReader reader2 = rstrBuffer.GetReader(0))
 288                {
 0289                    rstrXml = (doc.ReadNode(reader2) as XmlElement);
 0290                }
 291
 0292                CoreWCF.XmlBuffer issuedTokenBuffer = GetIssuedTokenBuffer(rstrBuffer);
 0293                string context = null;
 0294                string tokenTypeUri = null;
 0295                int keySize = 0;
 0296                bool computeKey = false;
 0297                DateTime created = DateTime.UtcNow;
 0298                DateTime expires = SecurityUtils.MaxUtcDateTime;
 0299                for (int i = 0; i < rstrXml.Attributes.Count; ++i)
 300                {
 0301                    XmlAttribute attr = rstrXml.Attributes[i];
 0302                    if (attr.LocalName == DriverDictionary.Context.Value)
 303                    {
 0304                        context = attr.Value;
 305                    }
 306                }
 307
 0308                for (int i = 0; i < rstrXml.ChildNodes.Count; ++i)
 309                {
 0310                    XmlElement child = (rstrXml.ChildNodes[i] as XmlElement);
 0311                    if (child != null)
 312                    {
 0313                        if (child.LocalName == DriverDictionary.TokenType.Value && child.NamespaceURI == DriverDictionar
 314                        {
 0315                            tokenTypeUri = XmlHelper.ReadTextElementAsTrimmedString(child);
 316                        }
 0317                        else if (child.LocalName == DriverDictionary.KeySize.Value && child.NamespaceURI == DriverDictio
 318                        {
 0319                            keySize = int.Parse(XmlHelper.ReadTextElementAsTrimmedString(child), NumberFormatInfo.Invari
 320                        }
 0321                        else if (child.LocalName == DriverDictionary.RequestedProofToken.Value && child.NamespaceURI == 
 322                        {
 0323                            XmlElement proofXml = XmlHelper.GetChildElement(child);
 0324                            if (proofXml.LocalName == DriverDictionary.ComputedKey.Value && proofXml.NamespaceURI == Dri
 325                            {
 0326                                string computedKeyAlgorithm = XmlHelper.ReadTextElementAsTrimmedString(proofXml);
 0327                                if (computedKeyAlgorithm != DriverDictionary.Psha1ComputedKeyUri.Value)
 328                                {
 0329                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationE
 330                                }
 0331                                computeKey = true;
 332                            }
 333                        }
 0334                        else if (child.LocalName == DriverDictionary.Lifetime.Value && child.NamespaceURI == DriverDicti
 335                        {
 0336                            XmlElement createdXml = XmlHelper.GetChildElement(child, UtilityStrings.CreatedElement, Util
 0337                            if (createdXml != null)
 338                            {
 0339                                created = DateTime.ParseExact(XmlHelper.ReadTextElementAsTrimmedString(createdXml),
 0340                                    WSUtilitySpecificationVersion.AcceptedDateTimeFormats, DateTimeFormatInfo.InvariantI
 341                            }
 0342                            XmlElement expiresXml = XmlHelper.GetChildElement(child, UtilityStrings.ExpiresElement, Util
 0343                            if (expiresXml != null)
 344                            {
 0345                                expires = DateTime.ParseExact(XmlHelper.ReadTextElementAsTrimmedString(expiresXml),
 0346                                    WSUtilitySpecificationVersion.AcceptedDateTimeFormats, DateTimeFormatInfo.InvariantI
 347                            }
 348                        }
 349                    }
 350                }
 351
 0352                bool isRequestedTokenClosed = ReadRequestedTokenClosed(rstrXml);
 0353                ReadReferences(rstrXml, out SecurityKeyIdentifierClause requestedAttachedReference, out SecurityKeyIdent
 354
 0355                return new RequestSecurityTokenResponse(_standardsManager, rstrXml, context, tokenTypeUri, keySize, requ
 0356                                                        computeKey, created, expires, isRequestedTokenClosed, issuedToke
 357            }
 358
 359            public override RequestSecurityTokenResponseCollection CreateRequestSecurityTokenResponseCollection(XmlReade
 360            {
 0361                XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader);
 0362                List<RequestSecurityTokenResponse> rstrCollection = new List<RequestSecurityTokenResponse>(2);
 0363                string rootName = reader.Name;
 0364                reader.ReadStartElement(DriverDictionary.RequestSecurityTokenResponseCollection, DriverDictionary.Namesp
 0365                while (reader.IsStartElement(DriverDictionary.RequestSecurityTokenResponse.Value, DriverDictionary.Names
 366                {
 0367                    RequestSecurityTokenResponse rstr = CreateRequestSecurityTokenResponse(reader);
 0368                    rstrCollection.Add(rstr);
 369                }
 0370                reader.ReadEndElement();
 0371                if (rstrCollection.Count == 0)
 372                {
 0373                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.NoRequestSec
 374                }
 375
 0376                return new RequestSecurityTokenResponseCollection(rstrCollection.AsReadOnly(), StandardsManager);
 377            }
 378
 379            private XmlElement GetAppliesToElement(XmlElement rootElement)
 380            {
 10381                if (rootElement == null)
 382                {
 0383                    return null;
 384                }
 100385                for (int i = 0; i < rootElement.ChildNodes.Count; ++i)
 386                {
 40387                    XmlElement elem = (rootElement.ChildNodes[i] as XmlElement);
 40388                    if (elem != null)
 389                    {
 40390                        if (elem.LocalName == DriverDictionary.AppliesTo.Value && elem.NamespaceURI == Namespaces.WSPoli
 391                        {
 0392                            return elem;
 393                        }
 394                    }
 395                }
 10396                return null;
 397            }
 398
 399            private T GetAppliesTo<T>(XmlElement rootXml, XmlObjectSerializer serializer)
 400            {
 0401                XmlElement appliesToElement = GetAppliesToElement(rootXml);
 0402                if (appliesToElement != null)
 403                {
 0404                    using (XmlReader reader = new XmlNodeReader(appliesToElement))
 405                    {
 0406                        reader.ReadStartElement();
 0407                        lock (serializer)
 408                        {
 0409                            return (T)serializer.ReadObject(reader);
 410                        }
 411                    }
 412                }
 413                else
 414                {
 0415                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 416                }
 0417            }
 418
 419            public override T GetAppliesTo<T>(RequestSecurityToken rst, XmlObjectSerializer serializer)
 420            {
 0421                if (rst == null)
 422                {
 0423                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rst));
 424                }
 425
 0426                return GetAppliesTo<T>(rst.RequestSecurityTokenXml, serializer);
 427            }
 428
 429            public override T GetAppliesTo<T>(RequestSecurityTokenResponse rstr, XmlObjectSerializer serializer)
 430            {
 0431                if (rstr == null)
 432                {
 0433                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstr));
 434                }
 435
 0436                return GetAppliesTo<T>(rstr.RequestSecurityTokenResponseXml, serializer);
 437            }
 438
 439            public override bool IsAppliesTo(string localName, string namespaceUri)
 440            {
 0441                return (localName == DriverDictionary.AppliesTo.Value && namespaceUri == Namespaces.WSPolicy);
 442            }
 443
 444            private void GetAppliesToQName(XmlElement rootElement, out string localName, out string namespaceUri)
 445            {
 10446                localName = namespaceUri = null;
 10447                XmlElement appliesToElement = GetAppliesToElement(rootElement);
 10448                if (appliesToElement != null)
 449                {
 0450                    using (XmlReader reader = new XmlNodeReader(appliesToElement))
 451                    {
 0452                        reader.ReadStartElement();
 0453                        reader.MoveToContent();
 0454                        localName = reader.LocalName;
 0455                        namespaceUri = reader.NamespaceURI;
 0456                    }
 457                }
 10458            }
 459
 460            public override void GetAppliesToQName(RequestSecurityToken rst, out string localName, out string namespaceU
 461            {
 10462                if (rst == null)
 463                {
 0464                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rst));
 465                }
 466
 10467                GetAppliesToQName(rst.RequestSecurityTokenXml, out localName, out namespaceUri);
 10468            }
 469
 470            public override void GetAppliesToQName(RequestSecurityTokenResponse rstr, out string localName, out string n
 471            {
 0472                if (rstr == null)
 473                {
 0474                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstr));
 475                }
 476
 0477                GetAppliesToQName(rstr.RequestSecurityTokenResponseXml, out localName, out namespaceUri);
 0478            }
 479
 480            public override byte[] GetAuthenticator(RequestSecurityTokenResponse rstr)
 481            {
 0482                if (rstr != null && rstr.RequestSecurityTokenResponseXml != null && rstr.RequestSecurityTokenResponseXml
 483                {
 0484                    for (int i = 0; i < rstr.RequestSecurityTokenResponseXml.ChildNodes.Count; ++i)
 485                    {
 0486                        if (rstr.RequestSecurityTokenResponseXml.ChildNodes[i] is XmlElement element)
 487                        {
 0488                            if (element.LocalName == DriverDictionary.Authenticator.Value && element.NamespaceURI == Dri
 489                            {
 0490                                XmlElement combinedHashElement = XmlHelper.GetChildElement(element);
 0491                                if (combinedHashElement.LocalName == DriverDictionary.CombinedHash.Value && combinedHash
 492                                {
 0493                                    string authenticatorString = XmlHelper.ReadTextElementAsTrimmedString(combinedHashEl
 0494                                    return Convert.FromBase64String(authenticatorString);
 495                                }
 496                            }
 497                        }
 498                    }
 499                }
 0500                return null;
 501            }
 502
 503            public override BinaryNegotiation GetBinaryNegotiation(RequestSecurityTokenResponse rstr)
 504            {
 0505                if (rstr == null)
 506                {
 0507                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstr));
 508                }
 509
 0510                return GetBinaryNegotiation(rstr.RequestSecurityTokenResponseXml);
 511            }
 512
 513            public override BinaryNegotiation GetBinaryNegotiation(RequestSecurityToken rst)
 514            {
 0515                if (rst == null)
 516                {
 0517                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rst));
 518                }
 519
 0520                return GetBinaryNegotiation(rst.RequestSecurityTokenXml);
 521            }
 522
 523            private BinaryNegotiation GetBinaryNegotiation(XmlElement rootElement)
 524            {
 0525                if (rootElement == null)
 526                {
 0527                    return null;
 528                }
 0529                for (int i = 0; i < rootElement.ChildNodes.Count; ++i)
 530                {
 0531                    if (rootElement.ChildNodes[i] is XmlElement elem)
 532                    {
 0533                        if (elem.LocalName == DriverDictionary.BinaryExchange.Value && elem.NamespaceURI == DriverDictio
 534                        {
 0535                            return ReadBinaryNegotiation(elem);
 536                        }
 537                    }
 538                }
 0539                return null;
 540            }
 541
 542            public override SecurityToken GetEntropy(RequestSecurityToken rst, SecurityTokenResolver resolver)
 543            {
 10544                if (rst == null)
 545                {
 0546                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rst));
 547                }
 548
 10549                return GetEntropy(rst.RequestSecurityTokenXml, resolver);
 550            }
 551
 552            public override SecurityToken GetEntropy(RequestSecurityTokenResponse rstr, SecurityTokenResolver resolver)
 553            {
 0554                if (rstr == null)
 555                {
 0556                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstr));
 557                }
 558
 0559                return GetEntropy(rstr.RequestSecurityTokenResponseXml, resolver);
 560            }
 561
 562            private SecurityToken GetEntropy(XmlElement rootElement, SecurityTokenResolver resolver)
 563            {
 10564                if (rootElement == null || rootElement.ChildNodes == null)
 565                {
 0566                    return null;
 567                }
 60568                for (int i = 0; i < rootElement.ChildNodes.Count; ++i)
 569                {
 30570                    if (rootElement.ChildNodes[i] is XmlElement element)
 571                    {
 30572                        if (element.LocalName == DriverDictionary.Entropy.Value && element.NamespaceURI == DriverDiction
 573                        {
 10574                            XmlElement tokenXml = XmlHelper.GetChildElement(element);
 10575                            string valueTypeUri = element.GetAttribute(SecurityJan2004Strings.ValueType);
 10576                            if (valueTypeUri.Length == 0)
 577                            {
 578                            }
 579
 10580                            return _standardsManager.SecurityTokenSerializer.ReadToken(new XmlNodeReader(tokenXml), reso
 581                        }
 582                    }
 583                }
 0584                return null;
 585            }
 586
 587            private void GetIssuedAndProofXml(RequestSecurityTokenResponse rstr, out XmlElement issuedTokenXml, out XmlE
 588            {
 0589                issuedTokenXml = null;
 0590                proofTokenXml = null;
 0591                if ((rstr.RequestSecurityTokenResponseXml != null) && (rstr.RequestSecurityTokenResponseXml.ChildNodes !
 592                {
 0593                    for (int i = 0; i < rstr.RequestSecurityTokenResponseXml.ChildNodes.Count; ++i)
 594                    {
 0595                        if (rstr.RequestSecurityTokenResponseXml.ChildNodes[i] is XmlElement elem)
 596                        {
 0597                            if (elem.LocalName == DriverDictionary.RequestedSecurityToken.Value && elem.NamespaceURI == 
 598                            {
 0599                                if (issuedTokenXml != null)
 600                                {
 0601                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept
 602                                }
 0603                                issuedTokenXml = XmlHelper.GetChildElement(elem);
 604                            }
 0605                            else if (elem.LocalName == DriverDictionary.RequestedProofToken.Value && elem.NamespaceURI =
 606                            {
 0607                                if (proofTokenXml != null)
 608                                {
 0609                                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationExcept
 610                                }
 0611                                proofTokenXml = XmlHelper.GetChildElement(elem);
 612                            }
 613                        }
 614                    }
 615                }
 0616            }
 617
 618            /// <summary>
 619            /// The algorithm for computing the key is:
 620            /// 1. If there is requestorEntropy:
 621            ///    a. If there is no <RequestedProofToken> use the requestorEntropy as the key
 622            ///    b. If there is a <RequestedProofToken> with a ComputedKeyUri, combine the client and server entropies
 623            ///    c. Anything else, throw
 624            /// 2. If there is no requestorEntropy:
 625            ///    a. THere has to be a <RequestedProofToken> that contains the proof key
 626            /// </summary>
 627            public override GenericXmlSecurityToken GetIssuedToken(RequestSecurityTokenResponse rstr, SecurityTokenResol
 628                ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, int defaultKeySize, bool isBearerKeyType
 629            {
 0630                SecurityKeyEntropyModeHelper.Validate(keyEntropyMode);
 631
 0632                if (defaultKeySize < 0)
 633                {
 0634                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(def
 635                }
 636
 0637                if (rstr == null)
 638                {
 0639                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstr));
 640                }
 641
 642                string tokenType;
 0643                if (rstr.TokenType != null)
 644                {
 0645                    if (expectedTokenType != null && expectedTokenType != rstr.TokenType)
 646                    {
 0647                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 648                    }
 0649                    tokenType = rstr.TokenType;
 650                }
 651                else
 652                {
 653                }
 654
 655                // search the response elements for licenseXml, proofXml, and lifetime
 0656                DateTime created = rstr.ValidFrom;
 0657                DateTime expires = rstr.ValidTo;
 0658                GetIssuedAndProofXml(rstr, out XmlElement issuedTokenXml, out XmlElement proofXml);
 659
 0660                if (issuedTokenXml == null)
 661                {
 0662                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 663                }
 664
 0665                if (isBearerKeyType)
 666                {
 0667                    if (proofXml != null)
 668                    {
 0669                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 670                    }
 671
 0672                    return new GenericXmlSecurityToken(issuedTokenXml, null, created, expires, rstr.RequestedAttachedRef
 673                }
 674
 675                SecurityToken proofToken;
 0676                SecurityToken entropyToken = GetEntropy(rstr, resolver);
 0677                if (keyEntropyMode == SecurityKeyEntropyMode.ClientEntropy)
 678                {
 0679                    if (requestorEntropy == null)
 680                    {
 0681                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 682                    }
 683                    // enforce that there is no entropy or proof token in the RSTR
 0684                    if (proofXml != null || entropyToken != null)
 685                    {
 0686                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 687                    }
 0688                    proofToken = new BinarySecretSecurityToken(requestorEntropy);
 689                }
 0690                else if (keyEntropyMode == SecurityKeyEntropyMode.ServerEntropy)
 691                {
 0692                    if (requestorEntropy != null)
 693                    {
 0694                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 695                    }
 0696                    if (rstr.ComputeKey || entropyToken != null)
 697                    {
 0698                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 699                    }
 0700                    if (proofXml == null)
 701                    {
 0702                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 703                    }
 0704                    string valueTypeUri = proofXml.GetAttribute(SecurityJan2004Strings.ValueType);
 0705                    if (valueTypeUri.Length == 0)
 706                    {
 707                    }
 708
 0709                    proofToken = _standardsManager.SecurityTokenSerializer.ReadToken(new XmlNodeReader(proofXml), resolv
 710                }
 711                else
 712                {
 0713                    if (!rstr.ComputeKey)
 714                    {
 0715                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 716                    }
 0717                    if (entropyToken == null)
 718                    {
 0719                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 720                    }
 0721                    if (requestorEntropy == null)
 722                    {
 0723                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 724                    }
 0725                    if (rstr.KeySize == 0 && defaultKeySize == 0)
 726                    {
 0727                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 728                    }
 0729                    int issuedKeySize = (rstr.KeySize != 0) ? rstr.KeySize : defaultKeySize;
 730                    byte[] issuerEntropy;
 0731                    if (entropyToken is BinarySecretSecurityToken)
 732                    {
 0733                        issuerEntropy = ((BinarySecretSecurityToken)entropyToken).GetKeyBytes();
 734                    }
 0735                    else if (entropyToken is WrappedKeySecurityToken)
 736                    {
 0737                        issuerEntropy = ((WrappedKeySecurityToken)entropyToken).GetWrappedKey();
 738                    }
 739                    else
 740                    {
 0741                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException(SR.Format(SR
 742                    }
 743                    // compute the PSHA1 derived key
 0744                    byte[] issuedKey = RequestSecurityTokenResponse.ComputeCombinedKey(requestorEntropy, issuerEntropy, 
 0745                    proofToken = new BinarySecretSecurityToken(issuedKey);
 746                }
 747
 0748                SecurityKeyIdentifierClause internalReference = rstr.RequestedAttachedReference;
 0749                SecurityKeyIdentifierClause externalReference = rstr.RequestedUnattachedReference;
 750
 0751                return new BufferedGenericXmlSecurityToken(issuedTokenXml, proofToken, created, expires, internalReferen
 752            }
 753
 754            public override GenericXmlSecurityToken GetIssuedToken(RequestSecurityTokenResponse rstr, string expectedTok
 755                ReadOnlyCollection<IAuthorizationPolicy> authorizationPolicies, RSA clientKey)
 756            {
 0757                if (rstr == null)
 758                {
 0759                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(rstr)));
 760                }
 761
 762                string tokenType;
 0763                if (rstr.TokenType != null)
 764                {
 0765                    if (expectedTokenType != null && expectedTokenType != rstr.TokenType)
 766                    {
 0767                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Forma
 768                    }
 0769                    tokenType = rstr.TokenType;
 770                }
 771                else
 772                {
 773                }
 774
 775                // search the response elements for licenseXml, proofXml, and lifetime
 0776                DateTime created = rstr.ValidFrom;
 0777                DateTime expires = rstr.ValidTo;
 0778                GetIssuedAndProofXml(rstr, out XmlElement issuedTokenXml, out XmlElement proofXml);
 779
 0780                if (issuedTokenXml == null)
 781                {
 0782                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 783                }
 784
 785                // enforce that there is no proof token in the RSTR
 0786                if (proofXml != null)
 787                {
 0788                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 789                }
 0790                SecurityKeyIdentifierClause internalReference = rstr.RequestedAttachedReference;
 0791                SecurityKeyIdentifierClause externalReference = rstr.RequestedUnattachedReference;
 792
 0793                SecurityToken proofToken = new RsaSecurityToken(clientKey);
 0794                return new BufferedGenericXmlSecurityToken(issuedTokenXml, proofToken, created, expires, internalReferen
 795            }
 796
 797            public override bool IsAtRequestSecurityTokenResponse(XmlReader reader)
 798            {
 0799                if (reader == null)
 800                {
 0801                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 802                }
 803
 0804                return reader.IsStartElement(DriverDictionary.RequestSecurityTokenResponse.Value, DriverDictionary.Names
 805            }
 806
 807            public override bool IsAtRequestSecurityTokenResponseCollection(XmlReader reader)
 808            {
 0809                if (reader == null)
 810                {
 0811                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader));
 812                }
 813
 0814                return reader.IsStartElement(DriverDictionary.RequestSecurityTokenResponseCollection.Value, DriverDictio
 815            }
 816
 817            public override bool IsRequestedSecurityTokenElement(string name, string nameSpace)
 818            {
 0819                return (name == DriverDictionary.RequestedSecurityToken.Value && nameSpace == DriverDictionary.Namespace
 820            }
 821
 822            public override bool IsRequestedProofTokenElement(string name, string nameSpace)
 823            {
 0824                return (name == DriverDictionary.RequestedProofToken.Value && nameSpace == DriverDictionary.Namespace.Va
 825            }
 826
 827            public static BinaryNegotiation ReadBinaryNegotiation(XmlElement elem)
 828            {
 0829                if (elem == null)
 830                {
 0831                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(elem));
 832                }
 833
 834                // get the encoding and valueType attributes
 0835                string encodingUri = null;
 0836                string valueTypeUri = null;
 0837                if (elem.Attributes != null)
 838                {
 0839                    for (int i = 0; i < elem.Attributes.Count; ++i)
 840                    {
 0841                        XmlAttribute attr = elem.Attributes[i];
 0842                        if (attr.LocalName == SecurityJan2004Strings.EncodingType && attr.NamespaceURI.Length == 0)
 843                        {
 0844                            encodingUri = attr.Value;
 0845                            if (encodingUri != Base64Uri && encodingUri != HexBinaryUri)
 846                            {
 0847                                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.
 848                            }
 849                        }
 0850                        else if (attr.LocalName == SecurityJan2004Strings.ValueType && attr.NamespaceURI.Length == 0)
 851                        {
 0852                            valueTypeUri = attr.Value;
 853                        }
 854                        // ignore all other attributes
 855                    }
 856                }
 0857                if (encodingUri == null)
 858                {
 0859                    XmlHelper.OnRequiredAttributeMissing("EncodingType", elem.Name);
 860                }
 0861                if (valueTypeUri == null)
 862                {
 0863                    XmlHelper.OnRequiredAttributeMissing("ValueType", elem.Name);
 864                }
 0865                string encodedBlob = XmlHelper.ReadTextElementAsTrimmedString(elem);
 866                byte[] negotiationData;
 0867                if (encodingUri == Base64Uri)
 868                {
 0869                    negotiationData = Convert.FromBase64String(encodedBlob);
 870                }
 871                else
 872                {
 0873                    negotiationData = HexBinary.Parse(encodedBlob).Value;
 874                }
 0875                return new BinaryNegotiation(valueTypeUri, negotiationData);
 876            }
 877
 878            // Note in Apr2004, internal & external references aren't supported -
 879            // our strategy is to see if there's a token reference (and use it for external ref) and backup is to scan t
 880            protected virtual void ReadReferences(XmlElement rstrXml, out SecurityKeyIdentifierClause requestedAttachedR
 881                    out SecurityKeyIdentifierClause requestedUnattachedReference)
 882            {
 0883                XmlElement issuedTokenXml = null;
 0884                requestedAttachedReference = null;
 0885                requestedUnattachedReference = null;
 0886                for (int i = 0; i < rstrXml.ChildNodes.Count; ++i)
 887                {
 0888                    if (rstrXml.ChildNodes[i] is XmlElement child)
 889                    {
 0890                        if (child.LocalName == DriverDictionary.RequestedSecurityToken.Value && child.NamespaceURI == Dr
 891                        {
 0892                            issuedTokenXml = XmlHelper.GetChildElement(child);
 893                        }
 0894                        else if (child.LocalName == DriverDictionary.RequestedTokenReference.Value && child.NamespaceURI
 895                        {
 0896                            requestedUnattachedReference = GetKeyIdentifierXmlReferenceClause(XmlHelper.GetChildElement(
 897                        }
 898                    }
 899                }
 900
 0901                if (issuedTokenXml != null)
 902                {
 0903                    requestedAttachedReference = _standardsManager.CreateKeyIdentifierClauseFromTokenXml(issuedTokenXml,
 0904                    if (requestedUnattachedReference == null)
 905                    {
 906                        try
 907                        {
 0908                            requestedUnattachedReference = _standardsManager.CreateKeyIdentifierClauseFromTokenXml(issue
 0909                        }
 0910                        catch (XmlException)
 911                        {
 0912                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.Trus
 913                        }
 914                    }
 915                }
 0916            }
 917
 918            internal bool TryReadKeyIdentifierClause(XmlNodeReader reader, out SecurityKeyIdentifierClause keyIdentifier
 919            {
 920                try
 921                {
 0922                    keyIdentifierClause = _standardsManager.SecurityTokenSerializer.ReadKeyIdentifierClause(reader);
 0923                }
 924                catch (XmlException e)
 925                {
 0926                    if (Fx.IsFatal(e))
 927                    {
 0928                        throw;
 929                    }
 930
 0931                    keyIdentifierClause = null;
 0932                    return false;
 933                }
 934                catch (Exception e)
 935                {
 0936                    if (Fx.IsFatal(e))
 937                    {
 0938                        throw;
 939                    }
 940
 0941                    keyIdentifierClause = null;
 0942                    return false;
 943                }
 944
 0945                return true;
 0946            }
 947
 948            internal SecurityKeyIdentifierClause CreateGenericXmlSecurityKeyIdentifierClause(XmlNodeReader reader, XmlEl
 949            {
 0950                XmlDictionaryReader localReader = XmlDictionaryReader.CreateDictionaryReader(reader);
 0951                string strId = localReader.GetAttribute(XD.UtilityDictionary.IdAttribute, XD.UtilityDictionary.Namespace
 0952                SecurityKeyIdentifierClause keyIdentifierClause = new GenericXmlSecurityKeyIdentifierClause(keyIdentifie
 0953                if (!string.IsNullOrEmpty(strId))
 954                {
 0955                    keyIdentifierClause.Id = strId;
 956                }
 0957                return keyIdentifierClause;
 958            }
 959
 960            internal SecurityKeyIdentifierClause GetKeyIdentifierXmlReferenceClause(XmlElement keyIdentifierReferenceXml
 961            {
 0962                XmlNodeReader reader = new XmlNodeReader(keyIdentifierReferenceXmlElement);
 0963                if (!TryReadKeyIdentifierClause(reader, out SecurityKeyIdentifierClause keyIdentifierClause))
 964                {
 0965                    keyIdentifierClause = CreateGenericXmlSecurityKeyIdentifierClause(new XmlNodeReader(keyIdentifierRef
 966                }
 967
 0968                return keyIdentifierClause;
 969            }
 970
 971            protected virtual bool ReadRequestedTokenClosed(XmlElement rstrXml)
 972            {
 0973                return false;
 974            }
 975
 976            protected virtual void ReadTargets(XmlElement rstXml, out SecurityKeyIdentifierClause renewTarget, out Secur
 977            {
 0978                renewTarget = null;
 0979                closeTarget = null;
 0980            }
 981
 982            public override void OnRSTRorRSTRCMissingException()
 983            {
 0984                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.ExpectedOneOfTwo
 0985                    DriverDictionary.RequestSecurityTokenResponse, DriverDictionary.RequestSecurityTokenResponseCollecti
 0986                    DriverDictionary.Namespace)));
 987            }
 988
 989            private void WriteAppliesTo(object appliesTo, Type appliesToType, XmlObjectSerializer serializer, XmlWriter 
 990            {
 0991                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
 0992                writer.WriteStartElement(Namespaces.WSPolicyPrefix, DriverDictionary.AppliesTo.Value, Namespaces.WSPolic
 0993                lock (serializer)
 994                {
 0995                    serializer.WriteObject(writer, appliesTo);
 0996                }
 0997                writer.WriteEndElement();
 0998            }
 999
 1000            public void WriteBinaryNegotiation(BinaryNegotiation negotiation, XmlWriter xmlWriter)
 1001            {
 01002                if (negotiation == null)
 1003                {
 01004                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(negotiation));
 1005                }
 1006
 01007                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
 01008                negotiation.WriteTo(writer, DriverDictionary.Prefix.Value,
 01009                                            DriverDictionary.BinaryExchange, DriverDictionary.Namespace,
 01010                                            XD.SecurityJan2004Dictionary.ValueType, null);
 01011            }
 1012
 1013            public override void WriteRequestSecurityToken(RequestSecurityToken rst, XmlWriter xmlWriter)
 1014            {
 01015                if (rst == null)
 1016                {
 01017                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rst));
 1018                }
 01019                if (xmlWriter == null)
 1020                {
 01021                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(xmlWriter));
 1022                }
 01023                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
 01024                if (rst.IsReceiver)
 1025                {
 01026                    rst.WriteTo(writer);
 01027                    return;
 1028                }
 01029                writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestSecurityToken, DriverDic
 01030                XmlHelper.AddNamespaceDeclaration(writer, DriverDictionary.Prefix.Value, DriverDictionary.Namespace);
 01031                if (rst.Context != null)
 1032                {
 01033                    writer.WriteAttributeString(DriverDictionary.Context, null, rst.Context);
 1034                }
 1035
 01036                rst.OnWriteCustomAttributes(writer);
 01037                if (rst.TokenType != null)
 1038                {
 01039                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.TokenType, DriverDictionary
 01040                    writer.WriteString(rst.TokenType);
 01041                    writer.WriteEndElement();
 1042                }
 01043                if (rst.RequestType != null)
 1044                {
 01045                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestType, DriverDictiona
 01046                    writer.WriteString(rst.RequestType);
 01047                    writer.WriteEndElement();
 1048                }
 1049
 01050                if (rst.AppliesTo != null)
 1051                {
 01052                    WriteAppliesTo(rst.AppliesTo, rst.AppliesToType, rst.AppliesToSerializer, writer);
 1053                }
 1054
 01055                SecurityToken entropyToken = rst.GetRequestorEntropy();
 01056                if (entropyToken != null)
 1057                {
 01058                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.Entropy, DriverDictionary.N
 01059                    _standardsManager.SecurityTokenSerializer.WriteToken(writer, entropyToken);
 01060                    writer.WriteEndElement();
 1061                }
 1062
 01063                if (rst.KeySize != 0)
 1064                {
 01065                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.KeySize, DriverDictionary.N
 01066                    writer.WriteValue(rst.KeySize);
 01067                    writer.WriteEndElement();
 1068                }
 1069
 01070                BinaryNegotiation negotiationData = rst.GetBinaryNegotiation();
 01071                if (negotiationData != null)
 1072                {
 01073                    WriteBinaryNegotiation(negotiationData, writer);
 1074                }
 1075
 01076                WriteTargets(rst, writer);
 1077
 01078                if (rst.RequestProperties != null)
 1079                {
 01080                    foreach (XmlElement property in rst.RequestProperties)
 1081                    {
 01082                        property.WriteTo(writer);
 1083                    }
 1084                }
 1085
 01086                rst.OnWriteCustomElements(writer);
 01087                writer.WriteEndElement();
 01088            }
 1089
 1090            protected virtual void WriteTargets(RequestSecurityToken rst, XmlDictionaryWriter writer)
 1091            {
 01092            }
 1093
 1094            // Note in Apr2004, internal & external references aren't supported - our strategy is to generate the extern
 1095            protected virtual void WriteReferences(RequestSecurityTokenResponse rstr, XmlDictionaryWriter writer)
 1096            {
 01097                if (rstr.RequestedUnattachedReference != null)
 1098                {
 01099                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestedTokenReference, Dr
 01100                    _standardsManager.SecurityTokenSerializer.WriteKeyIdentifierClause(writer, rstr.RequestedUnattachedR
 01101                    writer.WriteEndElement();
 1102                }
 01103            }
 1104
 1105            protected virtual void WriteRequestedTokenClosed(RequestSecurityTokenResponse rstr, XmlDictionaryWriter writ
 1106            {
 01107            }
 1108
 1109            public override void WriteRequestSecurityTokenResponse(RequestSecurityTokenResponse rstr, XmlWriter xmlWrite
 1110            {
 201111                if (rstr == null)
 1112                {
 01113                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstr));
 1114                }
 1115
 201116                if (xmlWriter == null)
 1117                {
 01118                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(xmlWriter));
 1119                }
 1120
 201121                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
 201122                if (rstr.IsReceiver)
 1123                {
 01124                    rstr.WriteTo(writer);
 01125                    return;
 1126                }
 201127                writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestSecurityTokenResponse, D
 201128                if (rstr.Context != null)
 1129                {
 01130                    writer.WriteAttributeString(DriverDictionary.Context, null, rstr.Context);
 1131                }
 1132                // define WSUtility at the top level to avoid multiple definitions below
 201133                XmlHelper.AddNamespaceDeclaration(writer, UtilityStrings.Prefix, XD.UtilityDictionary.Namespace);
 201134                rstr.OnWriteCustomAttributes(writer);
 1135
 201136                if (rstr.TokenType != null)
 1137                {
 101138                    writer.WriteElementString(DriverDictionary.Prefix.Value, DriverDictionary.TokenType, DriverDictionar
 1139                }
 1140
 201141                if (rstr.RequestedSecurityToken != null)
 1142                {
 101143                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestedSecurityToken, Dri
 101144                    _standardsManager.SecurityTokenSerializer.WriteToken(writer, rstr.RequestedSecurityToken);
 101145                    writer.WriteEndElement();
 1146                }
 1147
 201148                if (rstr.AppliesTo != null)
 1149                {
 01150                    WriteAppliesTo(rstr.AppliesTo, rstr.AppliesToType, rstr.AppliesToSerializer, writer);
 1151                }
 1152
 201153                WriteReferences(rstr, writer);
 1154
 201155                if (rstr.ComputeKey || rstr.RequestedProofToken != null)
 1156                {
 101157                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestedProofToken, Driver
 101158                    if (rstr.ComputeKey)
 1159                    {
 101160                        writer.WriteElementString(DriverDictionary.Prefix.Value, DriverDictionary.ComputedKey, DriverDic
 1161                    }
 1162                    else
 1163                    {
 01164                        _standardsManager.SecurityTokenSerializer.WriteToken(writer, rstr.RequestedProofToken);
 1165                    }
 101166                    writer.WriteEndElement();
 1167                }
 1168
 201169                SecurityToken entropyToken = rstr.GetIssuerEntropy();
 201170                if (entropyToken != null)
 1171                {
 101172                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.Entropy, DriverDictionary.N
 101173                    _standardsManager.SecurityTokenSerializer.WriteToken(writer, entropyToken);
 101174                    writer.WriteEndElement();
 1175                }
 1176
 1177                // To write out the lifetime, the following algorithm is used
 1178                //   1. If the lifetime is explicitly set, write it out.
 1179                //   2. Else, if a token/tokenbuilder has been set, use the lifetime in that.
 1180                //   3. Else do not serialize lifetime
 201181                if (rstr.IsLifetimeSet || rstr.RequestedSecurityToken != null)
 1182                {
 101183                    DateTime effectiveTime = SecurityUtils.MinUtcDateTime;
 101184                    DateTime expirationTime = SecurityUtils.MaxUtcDateTime;
 1185
 101186                    if (rstr.IsLifetimeSet)
 1187                    {
 101188                        effectiveTime = rstr.ValidFrom.ToUniversalTime();
 101189                        expirationTime = rstr.ValidTo.ToUniversalTime();
 1190                    }
 01191                    else if (rstr.RequestedSecurityToken != null)
 1192                    {
 01193                        effectiveTime = rstr.RequestedSecurityToken.ValidFrom.ToUniversalTime();
 01194                        expirationTime = rstr.RequestedSecurityToken.ValidTo.ToUniversalTime();
 1195                    }
 1196
 1197                    // write out the lifetime
 101198                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.Lifetime, DriverDictionary.
 1199                    // write out Created
 101200                    writer.WriteStartElement(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.CreatedElement, XD.
 101201                    writer.WriteString(effectiveTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture.D
 101202                    writer.WriteEndElement(); // wsu:Created
 1203                    // write out Expires
 101204                    writer.WriteStartElement(XD.UtilityDictionary.Prefix.Value, XD.UtilityDictionary.ExpiresElement, XD.
 101205                    writer.WriteString(expirationTime.ToString("yyyy-MM-ddTHH:mm:ss.fffZ", CultureInfo.InvariantCulture.
 101206                    writer.WriteEndElement(); // wsu:Expires
 101207                    writer.WriteEndElement(); // wsse:Lifetime
 1208                }
 1209
 201210                byte[] authenticator = rstr.GetAuthenticator();
 201211                if (authenticator != null)
 1212                {
 01213                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.Authenticator, DriverDictio
 01214                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.CombinedHash, DriverDiction
 01215                    writer.WriteBase64(authenticator, 0, authenticator.Length);
 01216                    writer.WriteEndElement();
 01217                    writer.WriteEndElement();
 1218                }
 1219
 201220                if (rstr.KeySize > 0)
 1221                {
 101222                    writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.KeySize, DriverDictionary.N
 101223                    writer.WriteValue(rstr.KeySize);
 101224                    writer.WriteEndElement();
 1225                }
 1226
 201227                WriteRequestedTokenClosed(rstr, writer);
 1228
 201229                BinaryNegotiation negotiationData = rstr.GetBinaryNegotiation();
 201230                if (negotiationData != null)
 1231                {
 01232                    WriteBinaryNegotiation(negotiationData, writer);
 1233                }
 1234
 201235                rstr.OnWriteCustomElements(writer);
 201236                writer.WriteEndElement();
 201237            }
 1238
 1239            public override void WriteRequestSecurityTokenResponseCollection(RequestSecurityTokenResponseCollection rstr
 1240            {
 81241                if (rstrCollection == null)
 1242                {
 01243                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(rstrCollection));
 1244                }
 1245
 81246                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(xmlWriter);
 81247                writer.WriteStartElement(DriverDictionary.Prefix.Value, DriverDictionary.RequestSecurityTokenResponseCol
 321248                foreach (RequestSecurityTokenResponse rstr in rstrCollection.RstrCollection)
 1249                {
 81250                    rstr.WriteTo(writer);
 1251                }
 81252                writer.WriteEndElement();
 81253            }
 1254
 1255            protected void SetProtectionLevelForFederation(OperationDescriptionCollection operations)
 1256            {
 01257                foreach (OperationDescription operation in operations)
 1258                {
 01259                    foreach (MessageDescription message in operation.Messages)
 1260                    {
 01261                        if (message.Body.Parts.Count > 0)
 1262                        {
 01263                            foreach (MessagePartDescription part in message.Body.Parts)
 1264                            {
 01265                                part.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
 1266                            }
 1267                        }
 01268                        if (OperationFormatter.IsValidReturnValue(message.Body.ReturnValue))
 1269                        {
 01270                            message.Body.ReturnValue.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSig
 1271                        }
 1272                    }
 1273                }
 01274            }
 1275
 1276            public override bool TryParseKeySizeElement(XmlElement element, out int keySize)
 1277            {
 01278                if (element == null)
 1279                {
 01280                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 1281                }
 1282
 01283                if (element.LocalName == DriverDictionary.KeySize.Value
 01284                    && element.NamespaceURI == DriverDictionary.Namespace.Value)
 1285                {
 01286                    keySize = int.Parse(XmlHelper.ReadTextElementAsTrimmedString(element), NumberFormatInfo.InvariantInf
 01287                    return true;
 1288                }
 1289
 01290                keySize = 0;
 01291                return false;
 1292            }
 1293
 1294            public override XmlElement CreateKeySizeElement(int keySize)
 1295            {
 01296                if (keySize < 0)
 1297                {
 01298                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(key
 1299                }
 01300                XmlDocument doc = new XmlDocument();
 01301                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.KeySize.Value,
 01302                    DriverDictionary.Namespace.Value);
 01303                result.AppendChild(doc.CreateTextNode(keySize.ToString(CultureInfo.InvariantCulture.NumberFormat)));
 01304                return result;
 1305            }
 1306
 1307            public override XmlElement CreateKeyTypeElement(SecurityKeyType keyType)
 1308            {
 01309                if (keyType == SecurityKeyType.SymmetricKey)
 1310                {
 01311                    return CreateSymmetricKeyTypeElement();
 1312                }
 01313                else if (keyType == SecurityKeyType.AsymmetricKey)
 1314                {
 01315                    return CreatePublicKeyTypeElement();
 1316                }
 1317                else
 1318                {
 01319                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR
 1320                }
 1321            }
 1322
 1323            public override bool TryParseKeyTypeElement(XmlElement element, out SecurityKeyType keyType)
 1324            {
 01325                if (element == null)
 1326                {
 01327                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 1328                }
 1329
 01330                if (TryParseSymmetricKeyElement(element))
 1331                {
 01332                    keyType = SecurityKeyType.SymmetricKey;
 01333                    return true;
 1334                }
 01335                else if (TryParsePublicKeyElement(element))
 1336                {
 01337                    keyType = SecurityKeyType.AsymmetricKey;
 01338                    return true;
 1339                }
 1340
 01341                keyType = SecurityKeyType.SymmetricKey;
 01342                return false;
 1343            }
 1344
 1345            public bool TryParseSymmetricKeyElement(XmlElement element)
 1346            {
 01347                if (element == null)
 1348                {
 01349                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 1350                }
 1351
 01352                return element.LocalName == DriverDictionary.KeyType.Value
 01353                    && element.NamespaceURI == DriverDictionary.Namespace.Value
 01354                    && element.InnerText == DriverDictionary.SymmetricKeyType.Value;
 1355            }
 1356
 1357            private XmlElement CreateSymmetricKeyTypeElement()
 1358            {
 01359                XmlDocument doc = new XmlDocument();
 01360                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.KeyType.Value,
 01361                    DriverDictionary.Namespace.Value);
 01362                result.AppendChild(doc.CreateTextNode(DriverDictionary.SymmetricKeyType.Value));
 01363                return result;
 1364            }
 1365
 1366            private bool TryParsePublicKeyElement(XmlElement element)
 1367            {
 01368                if (element == null)
 1369                {
 01370                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 1371                }
 1372
 01373                return element.LocalName == DriverDictionary.KeyType.Value
 01374                    && element.NamespaceURI == DriverDictionary.Namespace.Value
 01375                    && element.InnerText == DriverDictionary.PublicKeyType.Value;
 1376            }
 1377
 1378            private XmlElement CreatePublicKeyTypeElement()
 1379            {
 01380                XmlDocument doc = new XmlDocument();
 01381                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.KeyType.Value,
 01382                    DriverDictionary.Namespace.Value);
 01383                result.AppendChild(doc.CreateTextNode(DriverDictionary.PublicKeyType.Value));
 01384                return result;
 1385            }
 1386
 1387            public override bool TryParseTokenTypeElement(XmlElement element, out string tokenType)
 1388            {
 01389                if (element == null)
 1390                {
 01391                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 1392                }
 1393
 01394                if (element.LocalName == DriverDictionary.TokenType.Value
 01395                    && element.NamespaceURI == DriverDictionary.Namespace.Value)
 1396                {
 01397                    tokenType = element.InnerText;
 01398                    return true;
 1399                }
 1400
 01401                tokenType = null;
 01402                return false;
 1403            }
 1404
 1405            public override XmlElement CreateTokenTypeElement(string tokenTypeUri)
 1406            {
 01407                if (tokenTypeUri == null)
 1408                {
 01409                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(tokenTypeUri));
 1410                }
 01411                XmlDocument doc = new XmlDocument();
 01412                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.TokenType.Value,
 01413                    DriverDictionary.Namespace.Value);
 01414                result.AppendChild(doc.CreateTextNode(tokenTypeUri));
 01415                return result;
 1416            }
 1417
 1418            public override XmlElement CreateUseKeyElement(SecurityKeyIdentifier keyIdentifier, SecurityStandardsManager
 1419            {
 01420                if (keyIdentifier == null)
 1421                {
 01422                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(keyIdentifier));
 1423                }
 01424                if (standardsManager == null)
 1425                {
 01426                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(standardsManager));
 1427                }
 01428                XmlDocument doc = new XmlDocument();
 01429                XmlElement result = doc.CreateElement(DriverDictionary.UseKey.Value, DriverDictionary.Namespace.Value);
 01430                MemoryStream stream = new MemoryStream();
 01431                using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateDictionaryWriter(new XmlTextWriter(stream,
 1432                {
 01433                    standardsManager.SecurityTokenSerializer.WriteKeyIdentifier(writer, keyIdentifier);
 01434                    writer.Flush();
 01435                    stream.Seek(0, SeekOrigin.Begin);
 1436                    XmlNode skiNode;
 01437                    using (XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(new XmlTextReader(str
 1438                    {
 01439                        reader.MoveToContent();
 01440                        skiNode = doc.ReadNode(reader);
 01441                    }
 01442                    result.AppendChild(skiNode);
 01443                }
 01444                return result;
 1445            }
 1446
 1447            public override XmlElement CreateSignWithElement(string signatureAlgorithm)
 1448            {
 01449                if (signatureAlgorithm == null)
 1450                {
 01451                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(signatureAlgorithm));
 1452                }
 01453                XmlDocument doc = new XmlDocument();
 01454                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.SignWith.Value,
 01455                    DriverDictionary.Namespace.Value);
 01456                result.AppendChild(doc.CreateTextNode(signatureAlgorithm));
 01457                return result;
 1458            }
 1459
 1460            internal override bool IsSignWithElement(XmlElement element, out string signatureAlgorithm)
 1461            {
 01462                return CheckElement(element, DriverDictionary.SignWith.Value, DriverDictionary.Namespace.Value, out sign
 1463            }
 1464
 1465            public override XmlElement CreateEncryptWithElement(string encryptionAlgorithm)
 1466            {
 01467                if (encryptionAlgorithm == null)
 1468                {
 01469                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encryptionAlgorithm));
 1470                }
 01471                XmlDocument doc = new XmlDocument();
 01472                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.EncryptWith.Value,
 01473                    DriverDictionary.Namespace.Value);
 01474                result.AppendChild(doc.CreateTextNode(encryptionAlgorithm));
 01475                return result;
 1476            }
 1477
 1478            public override XmlElement CreateEncryptionAlgorithmElement(string encryptionAlgorithm)
 1479            {
 501480                if (encryptionAlgorithm == null)
 1481                {
 01482                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(encryptionAlgorithm));
 1483                }
 501484                XmlDocument doc = new XmlDocument();
 501485                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.EncryptionAlgorith
 501486                    DriverDictionary.Namespace.Value);
 501487                result.AppendChild(doc.CreateTextNode(encryptionAlgorithm));
 501488                return result;
 1489            }
 1490
 1491            internal override bool IsEncryptWithElement(XmlElement element, out string encryptWithAlgorithm)
 1492            {
 01493                return CheckElement(element, DriverDictionary.EncryptWith.Value, DriverDictionary.Namespace.Value, out e
 1494            }
 1495
 1496            internal override bool IsEncryptionAlgorithmElement(XmlElement element, out string encryptionAlgorithm)
 1497            {
 01498                return CheckElement(element, DriverDictionary.EncryptionAlgorithm.Value, DriverDictionary.Namespace.Valu
 1499            }
 1500
 1501            public override XmlElement CreateComputedKeyAlgorithmElement(string algorithm)
 1502            {
 01503                if (algorithm == null)
 1504                {
 01505                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm));
 1506                }
 01507                XmlDocument doc = new XmlDocument();
 01508                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.ComputedKeyAlgorit
 01509                    DriverDictionary.Namespace.Value);
 01510                result.AppendChild(doc.CreateTextNode(algorithm));
 01511                return result;
 1512            }
 1513
 1514            public override XmlElement CreateCanonicalizationAlgorithmElement(string algorithm)
 1515            {
 501516                if (algorithm == null)
 1517                {
 01518                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(algorithm));
 1519                }
 501520                XmlDocument doc = new XmlDocument();
 501521                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.CanonicalizationAl
 501522                    DriverDictionary.Namespace.Value);
 501523                result.AppendChild(doc.CreateTextNode(algorithm));
 501524                return result;
 1525            }
 1526
 1527            internal override bool IsCanonicalizationAlgorithmElement(XmlElement element, out string canonicalizationAlg
 1528            {
 01529                return CheckElement(element, DriverDictionary.CanonicalizationAlgorithm.Value, DriverDictionary.Namespac
 1530            }
 1531
 1532            public override bool TryParseRequiredClaimsElement(XmlElement element, out System.Collections.ObjectModel.Co
 1533            {
 01534                if (element == null)
 1535                {
 01536                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(element));
 1537                }
 1538
 01539                if (element.LocalName == DriverDictionary.Claims.Value
 01540                    && element.NamespaceURI == DriverDictionary.Namespace.Value)
 1541                {
 01542                    requiredClaims = new System.Collections.ObjectModel.Collection<XmlElement>();
 01543                    foreach (XmlNode node in element.ChildNodes)
 1544                    {
 01545                        if (node is XmlElement)
 1546                        {
 01547                            requiredClaims.Add((XmlElement)node);
 1548                        }
 1549                    }
 1550
 01551                    return true;
 1552                }
 1553
 01554                requiredClaims = null;
 01555                return false;
 1556            }
 1557
 1558            public override XmlElement CreateRequiredClaimsElement(IEnumerable<XmlElement> claimsList)
 1559            {
 01560                if (claimsList == null)
 1561                {
 01562                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(claimsList));
 1563                }
 01564                XmlDocument doc = new XmlDocument();
 01565                XmlElement result = doc.CreateElement(DriverDictionary.Prefix.Value, DriverDictionary.Claims.Value,
 01566                    DriverDictionary.Namespace.Value);
 01567                foreach (XmlElement claimElement in claimsList)
 1568                {
 01569                    XmlElement element = (XmlElement)doc.ImportNode(claimElement, true);
 01570                    result.AppendChild(element);
 1571                }
 01572                return result;
 1573            }
 1574
 1575            internal static void ValidateRequestedKeySize(int keySize, SecurityAlgorithmSuite algorithmSuite)
 1576            {
 101577                if ((keySize % 8 == 0) && algorithmSuite.IsSymmetricKeyLengthSupported(keySize))
 1578                {
 101579                    return;
 1580                }
 1581                else
 1582                {
 01583                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new SecurityNegotiationException(SR.Form
 1584                }
 1585            }
 1586
 1587            private static void ValidateRequestorEntropy(SecurityToken entropy, SecurityKeyEntropyMode mode)
 1588            {
 101589                if ((mode == SecurityKeyEntropyMode.ClientEntropy || mode == SecurityKeyEntropyMode.CombinedEntropy)
 101590                    && (entropy == null))
 1591                {
 01592                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(
 1593                }
 101594                if (mode == SecurityKeyEntropyMode.ServerEntropy && entropy != null)
 1595                {
 01596                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.Format(
 1597                }
 101598            }
 1599
 1600            internal static void ProcessRstAndIssueKey(RequestSecurityToken requestSecurityToken, SecurityTokenResolver 
 1601                out SecurityToken proofToken)
 1602            {
 101603                SecurityToken requestorEntropyToken = requestSecurityToken.GetRequestorEntropy(resolver);
 101604                ValidateRequestorEntropy(requestorEntropyToken, keyEntropyMode);
 1605                byte[] requestorEntropy;
 101606                if (requestorEntropyToken != null)
 1607                {
 101608                    if (requestorEntropyToken is BinarySecretSecurityToken skToken)
 1609                    {
 101610                        requestorEntropy = skToken.GetKeyBytes();
 1611                    }
 01612                    else if (requestorEntropyToken is WrappedKeySecurityToken)
 1613                    {
 01614                        requestorEntropy = ((WrappedKeySecurityToken)requestorEntropyToken).GetWrappedKey();
 1615                    }
 1616                    else
 1617                    {
 01618                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperWarning(new InvalidOperationException(SR.For
 1619                    }
 1620                }
 1621                else
 1622                {
 01623                    requestorEntropy = null;
 1624                }
 1625
 101626                if (keyEntropyMode == SecurityKeyEntropyMode.ClientEntropy)
 1627                {
 01628                    if (requestorEntropy != null)
 1629                    {
 1630                        // validate that the entropy length matches the algorithm suite
 01631                        ValidateRequestedKeySize(requestorEntropy.Length * 8, algorithmSuite);
 1632                    }
 01633                    proofKey = requestorEntropy;
 01634                    issuerEntropy = null;
 01635                    issuedKeySize = 0;
 01636                    proofToken = null;
 1637                }
 1638                else
 1639                {
 101640                    if (requestSecurityToken.KeySize != 0)
 1641                    {
 101642                        ValidateRequestedKeySize(requestSecurityToken.KeySize, algorithmSuite);
 101643                        issuedKeySize = requestSecurityToken.KeySize;
 1644                    }
 1645                    else
 1646                    {
 01647                        issuedKeySize = algorithmSuite.DefaultSymmetricKeyLength;
 1648                    }
 101649                    RNGCryptoServiceProvider random = new RNGCryptoServiceProvider();
 101650                    if (keyEntropyMode == SecurityKeyEntropyMode.ServerEntropy)
 1651                    {
 01652                        proofKey = new byte[issuedKeySize / 8];
 1653                        // proof key is completely issued by the server
 01654                        random.GetNonZeroBytes(proofKey);
 01655                        issuerEntropy = null;
 01656                        proofToken = new BinarySecretSecurityToken(proofKey);
 1657                    }
 1658                    else
 1659                    {
 101660                        issuerEntropy = new byte[issuedKeySize / 8];
 101661                        random.GetNonZeroBytes(issuerEntropy);
 101662                        proofKey = RequestSecurityTokenResponse.ComputeCombinedKey(requestorEntropy, issuerEntropy, issu
 101663                        proofToken = null;
 1664                    }
 1665                }
 101666            }
 1667        }
 1668
 1669        protected static bool CheckElement(XmlElement element, string name, string ns, out string value)
 1670        {
 01671            value = null;
 01672            if (element.LocalName != name || element.NamespaceURI != ns)
 1673            {
 01674                return false;
 1675            }
 1676
 01677            if (element.FirstChild is XmlText)
 1678            {
 01679                value = ((XmlText)element.FirstChild).Value;
 01680                return true;
 1681            }
 01682            return false;
 1683        }
 1684    }
 1685}

Methods/Properties

.ctor(CoreWCF.Security.WSSecurityTokenSerializer)
WSSecurityTokenSerializer()
PopulateTokenEntries(System.Collections.Generic.IList`1<CoreWCF.Security.WSSecurityTokenSerializer/TokenEntry>)
.ctor(CoreWCF.Security.WSTrust)
LocalName()
NamespaceUri()
GetTokenTypesCore()
TokenTypeUri()
ValueTypeUri()
CanReadTokenCore(System.Xml.XmlElement)
CanReadTokenCore(System.Xml.XmlDictionaryReader)
CreateKeyIdentifierClauseFromTokenXmlCore(System.Xml.XmlElement,CoreWCF.Security.Tokens.SecurityTokenReferenceStyle)
ReadTokenCore(System.Xml.XmlDictionaryReader,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
WriteTokenCore(System.Xml.XmlDictionaryWriter,CoreWCF.IdentityModel.Tokens.SecurityToken)
.ctor(CoreWCF.Security.SecurityStandardsManager)
RequestSecurityTokenAction()
RequestSecurityTokenResponseAction()
RequestTypeIssue()
ComputedKeyAlgorithm()
StandardsManager()
Namespace()
CreateRequestSecurityToken(System.Xml.XmlReader)
GetIssuedTokenBuffer(CoreWCF.XmlBuffer)
CreateRequestSecurityTokenResponse(System.Xml.XmlReader)
CreateRequestSecurityTokenResponseCollection(System.Xml.XmlReader)
GetAppliesToElement(System.Xml.XmlElement)
GetAppliesTo(System.Xml.XmlElement,System.Runtime.Serialization.XmlObjectSerializer)
GetAppliesTo(CoreWCF.Security.RequestSecurityToken,System.Runtime.Serialization.XmlObjectSerializer)
GetAppliesTo(CoreWCF.Security.RequestSecurityTokenResponse,System.Runtime.Serialization.XmlObjectSerializer)
IsAppliesTo(System.String,System.String)
GetAppliesToQName(System.Xml.XmlElement,System.String&,System.String&)
GetAppliesToQName(CoreWCF.Security.RequestSecurityToken,System.String&,System.String&)
GetAppliesToQName(CoreWCF.Security.RequestSecurityTokenResponse,System.String&,System.String&)
GetAuthenticator(CoreWCF.Security.RequestSecurityTokenResponse)
GetBinaryNegotiation(CoreWCF.Security.RequestSecurityTokenResponse)
GetBinaryNegotiation(CoreWCF.Security.RequestSecurityToken)
GetBinaryNegotiation(System.Xml.XmlElement)
GetEntropy(CoreWCF.Security.RequestSecurityToken,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
GetEntropy(CoreWCF.Security.RequestSecurityTokenResponse,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
GetEntropy(System.Xml.XmlElement,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver)
GetIssuedAndProofXml(CoreWCF.Security.RequestSecurityTokenResponse,System.Xml.XmlElement&,System.Xml.XmlElement&)
GetIssuedToken(CoreWCF.Security.RequestSecurityTokenResponse,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver,System.Collections.Generic.IList`1<CoreWCF.IdentityModel.Selectors.SecurityTokenAuthenticator>,CoreWCF.Security.SecurityKeyEntropyMode,System.Byte[],System.String,System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Policy.IAuthorizationPolicy>,System.Int32,System.Boolean)
GetIssuedToken(CoreWCF.Security.RequestSecurityTokenResponse,System.String,System.Collections.ObjectModel.ReadOnlyCollection`1<CoreWCF.IdentityModel.Policy.IAuthorizationPolicy>,System.Security.Cryptography.RSA)
IsAtRequestSecurityTokenResponse(System.Xml.XmlReader)
IsAtRequestSecurityTokenResponseCollection(System.Xml.XmlReader)
IsRequestedSecurityTokenElement(System.String,System.String)
IsRequestedProofTokenElement(System.String,System.String)
ReadBinaryNegotiation(System.Xml.XmlElement)
ReadReferences(System.Xml.XmlElement,CoreWCF.IdentityModel.SecurityKeyIdentifierClause&,CoreWCF.IdentityModel.SecurityKeyIdentifierClause&)
TryReadKeyIdentifierClause(System.Xml.XmlNodeReader,CoreWCF.IdentityModel.SecurityKeyIdentifierClause&)
CreateGenericXmlSecurityKeyIdentifierClause(System.Xml.XmlNodeReader,System.Xml.XmlElement)
GetKeyIdentifierXmlReferenceClause(System.Xml.XmlElement)
ReadRequestedTokenClosed(System.Xml.XmlElement)
ReadTargets(System.Xml.XmlElement,CoreWCF.IdentityModel.SecurityKeyIdentifierClause&,CoreWCF.IdentityModel.SecurityKeyIdentifierClause&)
OnRSTRorRSTRCMissingException()
WriteAppliesTo(System.Object,System.Type,System.Runtime.Serialization.XmlObjectSerializer,System.Xml.XmlWriter)
WriteBinaryNegotiation(CoreWCF.Security.BinaryNegotiation,System.Xml.XmlWriter)
WriteRequestSecurityToken(CoreWCF.Security.RequestSecurityToken,System.Xml.XmlWriter)
WriteTargets(CoreWCF.Security.RequestSecurityToken,System.Xml.XmlDictionaryWriter)
WriteReferences(CoreWCF.Security.RequestSecurityTokenResponse,System.Xml.XmlDictionaryWriter)
WriteRequestedTokenClosed(CoreWCF.Security.RequestSecurityTokenResponse,System.Xml.XmlDictionaryWriter)
WriteRequestSecurityTokenResponse(CoreWCF.Security.RequestSecurityTokenResponse,System.Xml.XmlWriter)
WriteRequestSecurityTokenResponseCollection(CoreWCF.Security.RequestSecurityTokenResponseCollection,System.Xml.XmlWriter)
SetProtectionLevelForFederation(CoreWCF.Description.OperationDescriptionCollection)
TryParseKeySizeElement(System.Xml.XmlElement,System.Int32&)
CreateKeySizeElement(System.Int32)
CreateKeyTypeElement(CoreWCF.IdentityModel.Tokens.SecurityKeyType)
TryParseKeyTypeElement(System.Xml.XmlElement,CoreWCF.IdentityModel.Tokens.SecurityKeyType&)
TryParseSymmetricKeyElement(System.Xml.XmlElement)
CreateSymmetricKeyTypeElement()
TryParsePublicKeyElement(System.Xml.XmlElement)
CreatePublicKeyTypeElement()
TryParseTokenTypeElement(System.Xml.XmlElement,System.String&)
CreateTokenTypeElement(System.String)
CreateUseKeyElement(CoreWCF.IdentityModel.Tokens.SecurityKeyIdentifier,CoreWCF.Security.SecurityStandardsManager)
CreateSignWithElement(System.String)
IsSignWithElement(System.Xml.XmlElement,System.String&)
CreateEncryptWithElement(System.String)
CreateEncryptionAlgorithmElement(System.String)
IsEncryptWithElement(System.Xml.XmlElement,System.String&)
IsEncryptionAlgorithmElement(System.Xml.XmlElement,System.String&)
CreateComputedKeyAlgorithmElement(System.String)
CreateCanonicalizationAlgorithmElement(System.String)
IsCanonicalizationAlgorithmElement(System.Xml.XmlElement,System.String&)
TryParseRequiredClaimsElement(System.Xml.XmlElement,System.Collections.ObjectModel.Collection`1<System.Xml.XmlElement>&)
CreateRequiredClaimsElement(System.Collections.Generic.IEnumerable`1<System.Xml.XmlElement>)
ValidateRequestedKeySize(System.Int32,CoreWCF.Security.SecurityAlgorithmSuite)
ValidateRequestorEntropy(CoreWCF.IdentityModel.Tokens.SecurityToken,CoreWCF.Security.SecurityKeyEntropyMode)
ProcessRstAndIssueKey(CoreWCF.Security.RequestSecurityToken,CoreWCF.IdentityModel.Selectors.SecurityTokenResolver,CoreWCF.Security.SecurityKeyEntropyMode,CoreWCF.Security.SecurityAlgorithmSuite,System.Int32&,System.Byte[]&,System.Byte[]&,CoreWCF.IdentityModel.Tokens.SecurityToken&)
CheckElement(System.Xml.XmlElement,System.String,System.String,System.String&)