< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.SecurityProtocolFactory
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SecurityProtocolFactory.cs
Line coverage
60%
Covered lines: 232
Uncovered lines: 154
Coverable lines: 386
Total lines: 1013
Line coverage: 60.1%
Branch coverage
52%
Covered branches: 99
Total branches: 190
Branch coverage: 52.1%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.cctor()100%11100%
.ctor()100%11100%
.ctor(...)0%440%
OnAbort()0%880%
OnClose(...)0%880%
CreateListenerSecurityState()100%110%
CreateSecurityProtocol(...)50%2280%
GetIdentityOfSelf()100%110%
GetProperty()0%880%
VerifyTypeUniqueness(...)75%121284.61%
GetSupportingTokenAuthenticators(...)83.33%121241.17%
MergeSupportingTokenAuthenticators(...)75%323290%
CreateRecipientSecurityTokenRequirement()50%2290.9%
CreateRecipientSecurityTokenRequirement(...)100%11100%
AddSupportingTokenAuthenticators(...)29.16%242448.78%
OpenAsync(...)100%11100%
OnOpenAsync(...)68%505074.5%
OnCloseAsync(...)100%110%
Open(...)0%220%
Open(...)0%220%
OnPropertySettingsError(...)0%440%
ThrowIfImmutable()100%11100%
ThrowIfNotOpen()100%11100%
OnClosed()100%110%
OnClosing()100%110%
OnFaulted()100%110%
OnOpened()100%110%
OnOpening()100%110%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/SecurityProtocolFactory.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.Security.Authentication.ExtendedProtection;
 9using System.Threading.Tasks;
 10using CoreWCF.Channels;
 11using CoreWCF.Description;
 12using CoreWCF.Dispatcher;
 13using CoreWCF.IdentityModel.Selectors;
 14using CoreWCF.IdentityModel.Tokens;
 15using CoreWCF.Runtime;
 16using CoreWCF.Security.Tokens;
 17
 18namespace CoreWCF.Security
 19{
 20    /*
 21     * See
 22     * http://xws/gxa/main/specs/security/security_profiles/SecurityProfiles.doc
 23     * for details on security protocols
 24
 25     * Concrete implementations are required to me thread safe after
 26     * Open() is called;
 27
 28     * instances of concrete protocol factories are scoped to a
 29     * channel/listener factory;
 30
 31     * Each channel/listener factory must have a
 32     * SecurityProtocolFactory set on it before open/first use; the
 33     * factory instance cannot be changed once the factory is opened
 34     * or listening;
 35
 36     * security protocol instances are scoped to a channel and will be
 37     * created by the Create calls on protocol factories;
 38
 39     * security protocol instances are required to be thread-safe.
 40
 41     * for typical subclasses, factory wide state and immutable
 42     * settings are expected to be on the ProtocolFactory itself while
 43     * channel-wide state is maintained internally in each security
 44     * protocol instance;
 45
 46     * the security protocol instance set on a channel cannot be
 47     * changed; however, the protocol instance may change internal
 48     * state; this covers RM's SCT renego case; by keeping state
 49     * change internal to protocol instances, we get better
 50     * coordination with concurrent message security on channels;
 51
 52     * the primary pivot in creating a security protocol instance is
 53     * initiator (client) vs. responder (server), NOT sender vs
 54     * receiver
 55
 56     * Create calls for input and reply channels will contain the
 57     * listener-wide state (if any) created by the corresponding call
 58     * on the factory;
 59
 60     */
 61
 62    // Whether we need to add support for targetting different SOAP roles is tracked by 19144
 63    public abstract class SecurityProtocolFactory : ISecurityCommunicationObject
 64    {
 65        internal const bool defaultAddTimestamp = true;
 66        internal const bool defaultDeriveKeys = true;
 67        internal const bool defaultDetectReplays = true;
 68        internal const string defaultMaxClockSkewString = "00:05:00";
 69        internal const string defaultReplayWindowString = "00:05:00";
 470        internal static readonly TimeSpan defaultMaxClockSkew = TimeSpan.Parse(defaultMaxClockSkewString, CultureInfo.In
 471        internal static readonly TimeSpan defaultReplayWindow = TimeSpan.Parse(defaultReplayWindowString, CultureInfo.In
 72        internal const int defaultMaxCachedNonces = 900000;
 73        internal const string defaultTimestampValidityDurationString = "00:05:00";
 474        internal static readonly TimeSpan defaultTimestampValidityDuration = TimeSpan.Parse(defaultTimestampValidityDura
 75        internal const SecurityHeaderLayout defaultSecurityHeaderLayout = SecurityHeaderLayout.Strict;
 76        private static ReadOnlyCollection<SupportingTokenAuthenticatorSpecification> s_emptyTokenAuthenticators;
 5577        private bool _addTimestamp = defaultAddTimestamp;
 5578        private bool _detectReplays = defaultDetectReplays;
 5579        private SecurityAlgorithmSuite _incomingAlgorithmSuite = SecurityAlgorithmSuite.Default;
 80        private Dictionary<string, MergedSupportingTokenAuthenticatorSpecification> _mergedSupportingTokenAuthenticators
 5581        private int _maxCachedNonces = defaultMaxCachedNonces;
 5582        private TimeSpan _maxClockSkew = defaultMaxClockSkew;
 83        private NonceCache _nonceCache = null;
 5584        private SecurityAlgorithmSuite _outgoingAlgorithmSuite = SecurityAlgorithmSuite.Default;
 5585        private TimeSpan _replayWindow = defaultReplayWindow;
 5586        private SecurityStandardsManager _standardsManager = SecurityStandardsManager.DefaultInstance;
 87        private SecurityTokenManager _securityTokenManager;
 88        private SecurityBindingElement _securityBindingElement;
 89        private string _requestReplyErrorPropertyName;
 5590        private TimeSpan _timestampValidityDuration = defaultTimestampValidityDuration;
 91
 92        // AuditLogLocation auditLogLocation;
 93        private readonly bool _suppressAuditFailure;
 94        private SecurityHeaderLayout _securityHeaderLayout;
 95        private bool _expectChannelBasicTokens;
 96        private bool _expectChannelSignedTokens;
 97        private bool _expectChannelEndorsingTokens;
 98        private Uri _listenUri;
 99        private Uri _privacyNoticeUri;
 100        private int _privacyNoticeVersion;
 101        private IMessageFilterTable<EndpointAddress> _endpointFilterTable;
 102        private BufferManager _streamBufferManager = null;
 103
 55104        protected SecurityProtocolFactory()
 105        {
 55106            ChannelSupportingTokenAuthenticatorSpecification = new Collection<SupportingTokenAuthenticatorSpecification>
 55107            ScopedSupportingTokenAuthenticatorSpecification = new Dictionary<string, ICollection<SupportingTokenAuthenti
 55108            CommunicationObject = new WrapperSecurityCommunicationObject(this);
 55109        }
 110
 0111        internal SecurityProtocolFactory(SecurityProtocolFactory factory) : this()
 112        {
 0113            if (factory == null)
 114            {
 0115                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(factory));
 116            }
 117
 0118            ActAsInitiator = factory.ActAsInitiator;
 0119            _addTimestamp = factory._addTimestamp;
 0120            _detectReplays = factory._detectReplays;
 0121            _incomingAlgorithmSuite = factory._incomingAlgorithmSuite;
 0122            _maxCachedNonces = factory._maxCachedNonces;
 0123            _maxClockSkew = factory._maxClockSkew;
 0124            _outgoingAlgorithmSuite = factory._outgoingAlgorithmSuite;
 0125            _replayWindow = factory._replayWindow;
 0126            ChannelSupportingTokenAuthenticatorSpecification = new Collection<SupportingTokenAuthenticatorSpecification>
 0127            ScopedSupportingTokenAuthenticatorSpecification = new Dictionary<string, ICollection<SupportingTokenAuthenti
 0128            _standardsManager = factory._standardsManager;
 0129            _timestampValidityDuration = factory._timestampValidityDuration;
 130            // this.auditLogLocation = factory.auditLogLocation;
 0131            _suppressAuditFailure = factory._suppressAuditFailure;
 132            // this.serviceAuthorizationAuditLevel = factory.serviceAuthorizationAuditLevel;
 133            // this.messageAuthenticationAuditLevel = factory.messageAuthenticationAuditLevel;
 0134            if (factory._securityBindingElement != null)
 135            {
 0136                _securityBindingElement = (SecurityBindingElement)factory._securityBindingElement.Clone();
 137            }
 0138            _securityTokenManager = factory._securityTokenManager;
 0139            _privacyNoticeUri = factory._privacyNoticeUri;
 0140            _privacyNoticeVersion = factory._privacyNoticeVersion;
 0141            _endpointFilterTable = factory._endpointFilterTable;
 0142            ExtendedProtectionPolicy = factory.ExtendedProtectionPolicy;
 0143            _nonceCache = factory._nonceCache;
 0144        }
 145
 958146        internal WrapperSecurityCommunicationObject CommunicationObject { get; }
 147
 148        // The ActAsInitiator value is set automatically on Open and
 149        // remains unchanged thereafter.  ActAsInitiator is true for
 150        // the initiator of the message exchange, such as the sender
 151        // of a datagram, sender of a request and sender of either leg
 152        // of a duplex exchange.
 526153        public bool ActAsInitiator { get; }
 154
 155        public BufferManager StreamBufferManager
 156        {
 157            get
 158            {
 63159                if (_streamBufferManager == null)
 160                {
 27161                    _streamBufferManager = BufferManager.CreateBufferManager(0, int.MaxValue);
 162                }
 163
 63164                return _streamBufferManager;
 165            }
 166            set
 167            {
 0168                _streamBufferManager = value;
 0169            }
 170        }
 171
 204172        public ExtendedProtectionPolicy ExtendedProtectionPolicy { get; set; }
 173
 0174        internal bool IsDuplexReply { get; set; }
 175
 176        public bool AddTimestamp
 177        {
 178            get
 179            {
 126180                return _addTimestamp;
 181            }
 182            set
 183            {
 77184                ThrowIfImmutable();
 77185                _addTimestamp = value;
 77186            }
 187        }
 188
 189        //public AuditLogLocation AuditLogLocation
 190        //{
 191        //    get
 192        //    {
 193        //        return this.auditLogLocation;
 194        //    }
 195        //    set
 196        //    {
 197        //        ThrowIfImmutable();
 198        //        AuditLogLocationHelper.Validate(value);
 199        //        this.auditLogLocation = value;
 200        //    }
 201        //}
 202
 203        //public bool SuppressAuditFailure
 204        //{
 205        //    get
 206        //    {
 207        //        return this.suppressAuditFailure;
 208        //    }
 209        //    set
 210        //    {
 211        //        ThrowIfImmutable();
 212        //        this.suppressAuditFailure = value;
 213        //    }
 214        //}
 215
 216        //public AuditLevel ServiceAuthorizationAuditLevel
 217        //{
 218        //    get
 219        //    {
 220        //        return this.serviceAuthorizationAuditLevel;
 221        //    }
 222        //    set
 223        //    {
 224        //        ThrowIfImmutable();
 225        //        AuditLevelHelper.Validate(value);
 226        //        this.serviceAuthorizationAuditLevel = value;
 227        //    }
 228        //}
 229
 230        //public AuditLevel MessageAuthenticationAuditLevel
 231        //{
 232        //    get
 233        //    {
 234        //        return this.messageAuthenticationAuditLevel;
 235        //    }
 236        //    set
 237        //    {
 238        //        ThrowIfImmutable();
 239        //        AuditLevelHelper.Validate(value);
 240        //        this.messageAuthenticationAuditLevel = value;
 241        //    }
 242        //}
 243
 244        public bool DetectReplays
 245        {
 246            get
 247            {
 149248                return _detectReplays;
 249            }
 250            set
 251            {
 88252                ThrowIfImmutable();
 88253                _detectReplays = value;
 88254            }
 255        }
 256
 257        public Uri PrivacyNoticeUri
 258        {
 259            get
 260            {
 0261                return _privacyNoticeUri;
 262            }
 263            set
 264            {
 0265                ThrowIfImmutable();
 0266                _privacyNoticeUri = value;
 0267            }
 268        }
 269
 270        public int PrivacyNoticeVersion
 271        {
 272            get
 273            {
 0274                return _privacyNoticeVersion;
 275            }
 276            set
 277            {
 0278                ThrowIfImmutable();
 0279                _privacyNoticeVersion = value;
 0280            }
 281        }
 282
 283        internal IMessageFilterTable<EndpointAddress> EndpointFilterTable
 284        {
 285            get
 286            {
 22287                return _endpointFilterTable;
 288            }
 289            set
 290            {
 0291                ThrowIfImmutable();
 0292                _endpointFilterTable = value;
 0293            }
 294        }
 295
 296        private static ReadOnlyCollection<SupportingTokenAuthenticatorSpecification> EmptyTokenAuthenticators
 297        {
 298            get
 299            {
 116300                if (s_emptyTokenAuthenticators == null)
 301                {
 3302                    s_emptyTokenAuthenticators = Array.AsReadOnly(Array.Empty<SupportingTokenAuthenticatorSpecification>
 303                }
 116304                return s_emptyTokenAuthenticators;
 305            }
 306        }
 307
 0308        internal NonValidatingSecurityTokenAuthenticator<DerivedKeySecurityToken> DerivedKeyTokenAuthenticator { get; }
 309
 55310        internal bool ExpectIncomingMessages { get; private set; }
 311
 55312        internal bool ExpectOutgoingMessages { get; private set; }
 313
 74314        internal bool ExpectKeyDerivation { get; set; }
 315
 55316        internal bool ExpectSupportingTokens { get; set; }
 317
 318        public SecurityAlgorithmSuite IncomingAlgorithmSuite
 319        {
 320            get
 321            {
 171322                return _incomingAlgorithmSuite;
 323            }
 324            set
 325            {
 55326                ThrowIfImmutable();
 55327                _incomingAlgorithmSuite = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argumen
 55328            }
 329        }
 330
 331        public int MaxCachedNonces
 332        {
 333            get
 334            {
 0335                return _maxCachedNonces;
 336            }
 337            set
 338            {
 55339                ThrowIfImmutable();
 55340                if (value <= 0)
 341                {
 0342                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 343                }
 55344                _maxCachedNonces = value;
 55345            }
 346        }
 347
 348        public TimeSpan MaxClockSkew
 349        {
 350            get
 351            {
 94352                return _maxClockSkew;
 353            }
 354            set
 355            {
 55356                ThrowIfImmutable();
 55357                if (value < TimeSpan.Zero)
 358                {
 0359                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 360                }
 55361                _maxClockSkew = value;
 55362            }
 363        }
 364
 365        public NonceCache NonceCache
 366        {
 367            get
 368            {
 94369                return _nonceCache;
 370            }
 371            set
 372            {
 0373                ThrowIfImmutable();
 0374                _nonceCache = value;
 0375            }
 376        }
 377
 378        public SecurityAlgorithmSuite OutgoingAlgorithmSuite
 379        {
 380            get
 381            {
 63382                return _outgoingAlgorithmSuite;
 383            }
 384            set
 385            {
 55386                ThrowIfImmutable();
 55387                _outgoingAlgorithmSuite = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Argumen
 55388            }
 389        }
 390
 391        public TimeSpan ReplayWindow
 392        {
 393            get
 394            {
 94395                return _replayWindow;
 396            }
 397            set
 398            {
 55399                ThrowIfImmutable();
 55400                if (value <= TimeSpan.Zero)
 401                {
 0402                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 403                }
 55404                _replayWindow = value;
 55405            }
 406        }
 407
 641408        public ICollection<SupportingTokenAuthenticatorSpecification> ChannelSupportingTokenAuthenticatorSpecification {
 409
 121410        public Dictionary<string, ICollection<SupportingTokenAuthenticatorSpecification>> ScopedSupportingTokenAuthentic
 411
 412        public SecurityBindingElement SecurityBindingElement
 413        {
 385414            get { return _securityBindingElement; }
 415            set
 416            {
 55417                ThrowIfImmutable();
 55418                if (value != null)
 419                {
 55420                    value = (SecurityBindingElement)value.Clone();
 421                }
 55422                _securityBindingElement = value;
 55423            }
 424        }
 425
 426        internal SecurityTokenManager SecurityTokenManager
 427        {
 209428            get { return _securityTokenManager; }
 429            set
 430            {
 77431                ThrowIfImmutable();
 77432                _securityTokenManager = value;
 77433            }
 434        }
 435
 0436        public virtual bool SupportsDuplex => false;
 437
 438        public SecurityHeaderLayout SecurityHeaderLayout
 439        {
 440            get
 441            {
 147442                return _securityHeaderLayout;
 443            }
 444            set
 445            {
 55446                ThrowIfImmutable();
 55447                _securityHeaderLayout = value;
 55448            }
 449        }
 450
 0451        public virtual bool SupportsReplayDetection => true;
 452
 55453        public virtual bool SupportsRequestReply => true;
 454
 455        internal SecurityStandardsManager StandardsManager
 456        {
 457            get
 458            {
 296459                return _standardsManager;
 460            }
 461            set
 462            {
 55463                ThrowIfImmutable();
 55464                _standardsManager = value ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullE
 55465            }
 466        }
 467
 468        public TimeSpan TimestampValidityDuration
 469        {
 470            get
 471            {
 63472                return _timestampValidityDuration;
 473            }
 474            set
 475            {
 55476                ThrowIfImmutable();
 55477                if (value <= TimeSpan.Zero)
 478                {
 0479                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(val
 480                }
 55481                _timestampValidityDuration = value;
 55482            }
 483        }
 484
 485        public Uri ListenUri
 486        {
 22487            get { return _listenUri; }
 488            set
 489            {
 55490                ThrowIfImmutable();
 55491                _listenUri = value;
 55492            }
 493        }
 494
 132495        internal MessageSecurityVersion MessageSecurityVersion { get; private set; }
 496
 110497        public TimeSpan DefaultOpenTimeout => ServiceDefaults.OpenTimeout;
 498
 0499        public TimeSpan DefaultCloseTimeout => ServiceDefaults.CloseTimeout;
 500
 501        public virtual void OnAbort()
 502        {
 0503            if (!ActAsInitiator)
 504            {
 0505                foreach (SupportingTokenAuthenticatorSpecification spec in ChannelSupportingTokenAuthenticatorSpecificat
 506                {
 0507                    SecurityUtils.AbortTokenAuthenticatorIfRequired(spec.TokenAuthenticator);
 508                }
 0509                foreach (string action in ScopedSupportingTokenAuthenticatorSpecification.Keys)
 510                {
 0511                    ICollection<SupportingTokenAuthenticatorSpecification> supportingAuthenticators = ScopedSupportingTo
 0512                    foreach (SupportingTokenAuthenticatorSpecification spec in supportingAuthenticators)
 513                    {
 0514                        SecurityUtils.AbortTokenAuthenticatorIfRequired(spec.TokenAuthenticator);
 515                    }
 516                }
 517            }
 0518        }
 519
 520        public virtual void OnClose(TimeSpan timeout)
 521        {
 0522            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 0523            if (!ActAsInitiator)
 524            {
 0525                foreach (SupportingTokenAuthenticatorSpecification spec in ChannelSupportingTokenAuthenticatorSpecificat
 526                {
 0527                    SecurityUtils.CloseTokenAuthenticatorIfRequiredAsync(spec.TokenAuthenticator, timeoutHelper.GetCance
 528                }
 0529                foreach (string action in ScopedSupportingTokenAuthenticatorSpecification.Keys)
 530                {
 0531                    ICollection<SupportingTokenAuthenticatorSpecification> supportingAuthenticators = ScopedSupportingTo
 0532                    foreach (SupportingTokenAuthenticatorSpecification spec in supportingAuthenticators)
 533                    {
 0534                        SecurityUtils.CloseTokenAuthenticatorIfRequiredAsync(spec.TokenAuthenticator, timeoutHelper.GetC
 535                    }
 536                }
 537            }
 0538        }
 539
 540        public virtual object CreateListenerSecurityState()
 541        {
 0542            return null;
 543        }
 544
 545        internal SecurityProtocol CreateSecurityProtocol(EndpointAddress target, Uri via, bool isReturnLegSecurityRequir
 546        {
 34547            ThrowIfNotOpen();
 34548            SecurityProtocol securityProtocol = OnCreateSecurityProtocol(target, via, timeout);
 34549            if (securityProtocol == null)
 550            {
 0551                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.ProtocolFactor
 552            }
 34553            return securityProtocol;
 554        }
 555
 556        public virtual EndpointIdentity GetIdentityOfSelf()
 557        {
 0558            return null;
 559        }
 560
 561        public virtual T GetProperty<T>()
 562        {
 0563            if (typeof(T) == typeof(Collection<ISecurityContextSecurityTokenCache>))
 564            {
 0565                ThrowIfNotOpen();
 0566                Collection<ISecurityContextSecurityTokenCache> result = new Collection<ISecurityContextSecurityTokenCach
 0567                if (ChannelSupportingTokenAuthenticatorSpecification != null)
 568                {
 0569                    foreach (SupportingTokenAuthenticatorSpecification spec in ChannelSupportingTokenAuthenticatorSpecif
 570                    {
 0571                        if (spec.TokenAuthenticator is ISecurityContextSecurityTokenCacheProvider cacheProvider)
 572                        {
 0573                            result.Add(cacheProvider.TokenCache);
 574                        }
 575                    }
 576                }
 0577                return (T)(object)(result);
 578            }
 579            else
 580            {
 0581                return default;
 582            }
 583        }
 584        internal abstract SecurityProtocol OnCreateSecurityProtocol(EndpointAddress target, Uri via, TimeSpan timeout);
 585
 586        private void VerifyTypeUniqueness(ICollection<SupportingTokenAuthenticatorSpecification> supportingTokenAuthenti
 587        {
 588            // its ok to go brute force here since we are dealing with a small number of authenticators
 308589            foreach (SupportingTokenAuthenticatorSpecification spec in supportingTokenAuthenticators)
 590            {
 77591                Type authenticatorType = spec.TokenAuthenticator.GetType();
 77592                int numSkipped = 0;
 396593                foreach (SupportingTokenAuthenticatorSpecification spec2 in supportingTokenAuthenticators)
 594                {
 121595                    Type spec2AuthenticatorType = spec2.TokenAuthenticator.GetType();
 121596                    if (ReferenceEquals(spec, spec2))
 597                    {
 77598                        if (numSkipped > 0)
 599                        {
 0600                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR
 601                        }
 77602                        ++numSkipped;
 77603                        continue;
 604                    }
 44605                    else if (authenticatorType.IsAssignableFrom(spec2AuthenticatorType) || spec2AuthenticatorType.IsAssi
 606                    {
 0607                        throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(SR.Format(SR.Mul
 608                    }
 609                }
 610            }
 77611        }
 612
 613        internal IList<SupportingTokenAuthenticatorSpecification> GetSupportingTokenAuthenticators(string action, out bo
 614        {
 94615            if (_mergedSupportingTokenAuthenticatorsMap != null && _mergedSupportingTokenAuthenticatorsMap.Count > 0)
 616            {
 13617                if (action != null && _mergedSupportingTokenAuthenticatorsMap.ContainsKey(action))
 618                {
 0619                    MergedSupportingTokenAuthenticatorSpecification mergedSpec = _mergedSupportingTokenAuthenticatorsMap
 0620                    expectSignedTokens = mergedSpec.ExpectSignedTokens;
 0621                    expectBasicTokens = mergedSpec.ExpectBasicTokens;
 0622                    expectEndorsingTokens = mergedSpec.ExpectEndorsingTokens;
 0623                    return mergedSpec.SupportingTokenAuthenticators;
 624                }
 13625                else if (_mergedSupportingTokenAuthenticatorsMap.ContainsKey(MessageHeaders.WildcardAction))
 626                {
 0627                    MergedSupportingTokenAuthenticatorSpecification mergedSpec = _mergedSupportingTokenAuthenticatorsMap
 0628                    expectSignedTokens = mergedSpec.ExpectSignedTokens;
 0629                    expectBasicTokens = mergedSpec.ExpectBasicTokens;
 0630                    expectEndorsingTokens = mergedSpec.ExpectEndorsingTokens;
 0631                    return mergedSpec.SupportingTokenAuthenticators;
 632                }
 633            }
 94634            expectSignedTokens = _expectChannelSignedTokens;
 94635            expectBasicTokens = _expectChannelBasicTokens;
 94636            expectEndorsingTokens = _expectChannelEndorsingTokens;
 637            // in case the channelSupportingTokenAuthenticators is empty return null so that its Count does not get acce
 94638            return (ReferenceEquals(ChannelSupportingTokenAuthenticatorSpecification, EmptyTokenAuthenticators)) ? null 
 639        }
 640
 641        private void MergeSupportingTokenAuthenticators(TimeSpan timeout)
 642        {
 55643            if (ScopedSupportingTokenAuthenticatorSpecification.Count == 0)
 644            {
 33645                _mergedSupportingTokenAuthenticatorsMap = null;
 646            }
 647            else
 648            {
 22649                TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 22650                ExpectSupportingTokens = true;
 22651                _mergedSupportingTokenAuthenticatorsMap = new Dictionary<string, MergedSupportingTokenAuthenticatorSpeci
 88652                foreach (string action in ScopedSupportingTokenAuthenticatorSpecification.Keys)
 653                {
 22654                    ICollection<SupportingTokenAuthenticatorSpecification> scopedAuthenticators = ScopedSupportingTokenA
 22655                    if (scopedAuthenticators == null || scopedAuthenticators.Count == 0)
 656                    {
 657                        continue;
 658                    }
 22659                    Collection<SupportingTokenAuthenticatorSpecification> mergedAuthenticators = new Collection<Supporti
 22660                    bool expectSignedTokens = _expectChannelSignedTokens;
 22661                    bool expectBasicTokens = _expectChannelBasicTokens;
 22662                    bool expectEndorsingTokens = _expectChannelEndorsingTokens;
 88663                    foreach (SupportingTokenAuthenticatorSpecification spec in ChannelSupportingTokenAuthenticatorSpecif
 664                    {
 22665                        mergedAuthenticators.Add(spec);
 666                    }
 88667                    foreach (SupportingTokenAuthenticatorSpecification spec in scopedAuthenticators)
 668                    {
 22669                        SecurityUtils.OpenTokenAuthenticatorIfRequiredAsync(spec.TokenAuthenticator, timeoutHelper.GetCa
 670
 22671                        mergedAuthenticators.Add(spec);
 22672                        if (spec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Endorsing ||
 22673                            spec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.SignedEndorsing)
 674                        {
 22675                            if (spec.TokenParameters.RequireDerivedKeys && !spec.TokenParameters.HasAsymmetricKey)
 676                            {
 0677                                ExpectKeyDerivation = true;
 678                            }
 679                        }
 22680                        SecurityTokenAttachmentMode mode = spec.SecurityTokenAttachmentMode;
 22681                        if (mode == SecurityTokenAttachmentMode.SignedEncrypted
 22682                            || mode == SecurityTokenAttachmentMode.Signed
 22683                            || mode == SecurityTokenAttachmentMode.SignedEndorsing)
 684                        {
 0685                            expectSignedTokens = true;
 0686                            if (mode == SecurityTokenAttachmentMode.SignedEncrypted)
 687                            {
 0688                                expectBasicTokens = true;
 689                            }
 690                        }
 22691                        if (mode == SecurityTokenAttachmentMode.Endorsing || mode == SecurityTokenAttachmentMode.SignedE
 692                        {
 22693                            expectEndorsingTokens = true;
 694                        }
 695                    }
 22696                    VerifyTypeUniqueness(mergedAuthenticators);
 22697                    MergedSupportingTokenAuthenticatorSpecification mergedSpec = new MergedSupportingTokenAuthenticatorS
 22698                    {
 22699                        SupportingTokenAuthenticators = mergedAuthenticators,
 22700                        ExpectBasicTokens = expectBasicTokens,
 22701                        ExpectEndorsingTokens = expectEndorsingTokens,
 22702                        ExpectSignedTokens = expectSignedTokens
 22703                    };
 22704                    _mergedSupportingTokenAuthenticatorsMap.Add(action, mergedSpec);
 705                }
 706            }
 22707        }
 708
 709        protected RecipientServiceModelSecurityTokenRequirement CreateRecipientSecurityTokenRequirement()
 710        {
 55711            RecipientServiceModelSecurityTokenRequirement requirement = new RecipientServiceModelSecurityTokenRequiremen
 55712            {
 55713                SecurityBindingElement = _securityBindingElement,
 55714                SecurityAlgorithmSuite = IncomingAlgorithmSuite,
 55715                ListenUri = _listenUri,
 55716                MessageSecurityVersion = MessageSecurityVersion.SecurityTokenVersion
 55717            };
 718            // requirement.AuditLogLocation = this.auditLogLocation;
 719            // requirement.SuppressAuditFailure = this.suppressAuditFailure;
 720            // requirement.MessageAuthenticationAuditLevel = this.messageAuthenticationAuditLevel;
 55721            requirement.Properties[ServiceModelSecurityTokenRequirement.ExtendedProtectionPolicy] = ExtendedProtectionPo
 55722            if (_endpointFilterTable != null)
 723            {
 0724                requirement.Properties.Add(ServiceModelSecurityTokenRequirement.EndpointFilterTableProperty, _endpointFi
 725            }
 55726            return requirement;
 727        }
 728
 729        private RecipientServiceModelSecurityTokenRequirement CreateRecipientSecurityTokenRequirement(SecurityTokenParam
 730        {
 55731            RecipientServiceModelSecurityTokenRequirement requirement = CreateRecipientSecurityTokenRequirement();
 55732            parameters.InitializeSecurityTokenRequirement(requirement);
 55733            requirement.KeyUsage = SecurityKeyUsage.Signature;
 55734            requirement.Properties[ServiceModelSecurityTokenRequirement.MessageDirectionProperty] = MessageDirection.Inp
 55735            requirement.Properties[ServiceModelSecurityTokenRequirement.SupportingTokenAttachmentModeProperty] = attachm
 55736            requirement.Properties[ServiceModelSecurityTokenRequirement.ExtendedProtectionPolicy] = ExtendedProtectionPo
 55737            return requirement;
 738        }
 739
 740        private void AddSupportingTokenAuthenticators(SupportingTokenParameters supportingTokenParameters, bool isOption
 741        {
 332742            for (int i = 0; i < supportingTokenParameters.Endorsing.Count; ++i)
 743            {
 34744                SecurityTokenRequirement requirement = CreateRecipientSecurityTokenRequirement(supportingTokenParameters
 745                try
 746                {
 34747                    CoreWCF.IdentityModel.Selectors.SecurityTokenAuthenticator authenticator = SecurityTokenManager.Crea
 34748                    SupportingTokenAuthenticatorSpecification authenticatorSpec = new SupportingTokenAuthenticatorSpecif
 34749                    authenticatorSpecList.Add(authenticatorSpec);
 34750                }
 0751                catch (Exception e)
 752                {
 0753                    if (!isOptional || Fx.IsFatal(e))
 754                    {
 0755                        throw;
 756                    }
 0757                }
 758            }
 264759            for (int i = 0; i < supportingTokenParameters.SignedEndorsing.Count; ++i)
 760            {
 0761                SecurityTokenRequirement requirement = CreateRecipientSecurityTokenRequirement(supportingTokenParameters
 762                try
 763                {
 0764                    CoreWCF.IdentityModel.Selectors.SecurityTokenAuthenticator authenticator = SecurityTokenManager.Crea
 0765                    SupportingTokenAuthenticatorSpecification authenticatorSpec = new SupportingTokenAuthenticatorSpecif
 0766                    authenticatorSpecList.Add(authenticatorSpec);
 0767                }
 0768                catch (Exception e)
 769                {
 0770                    if (!isOptional || Fx.IsFatal(e))
 771                    {
 0772                        throw;
 773                    }
 0774                }
 775            }
 302776            for (int i = 0; i < supportingTokenParameters.SignedEncrypted.Count; ++i)
 777            {
 19778                SecurityTokenRequirement requirement = CreateRecipientSecurityTokenRequirement(supportingTokenParameters
 779                try
 780                {
 19781                    CoreWCF.IdentityModel.Selectors.SecurityTokenAuthenticator authenticator = SecurityTokenManager.Crea
 19782                    SupportingTokenAuthenticatorSpecification authenticatorSpec = new SupportingTokenAuthenticatorSpecif
 19783                    authenticatorSpecList.Add(authenticatorSpec);
 19784                }
 0785                catch (Exception e)
 786                {
 0787                    if (!isOptional || Fx.IsFatal(e))
 788                    {
 0789                        throw;
 790                    }
 0791                }
 792            }
 268793            for (int i = 0; i < supportingTokenParameters.Signed.Count; ++i)
 794            {
 2795                SecurityTokenRequirement requirement = CreateRecipientSecurityTokenRequirement(supportingTokenParameters
 796                try
 797                {
 2798                    CoreWCF.IdentityModel.Selectors.SecurityTokenAuthenticator authenticator = SecurityTokenManager.Crea
 2799                    SupportingTokenAuthenticatorSpecification authenticatorSpec = new SupportingTokenAuthenticatorSpecif
 2800                    authenticatorSpecList.Add(authenticatorSpec);
 2801                }
 0802                catch (Exception e)
 803                {
 0804                    if (!isOptional || Fx.IsFatal(e))
 805                    {
 0806                        throw;
 807                    }
 0808                }
 809            }
 132810        }
 811
 812        public Task OpenAsync(TimeSpan timeout)
 813        {
 55814            return CommunicationObject.OpenAsync();
 815        }
 816
 817        public virtual Task OnOpenAsync(TimeSpan timeout)
 818        {
 55819            if (SecurityBindingElement == null)
 820            {
 0821                OnPropertySettingsError(nameof(SecurityBindingElement), true);
 822            }
 55823            if (SecurityTokenManager == null)
 824            {
 0825                OnPropertySettingsError(nameof(SecurityTokenManager), true);
 826            }
 55827            MessageSecurityVersion = _standardsManager.MessageSecurityVersion;
 55828            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
 55829            ExpectOutgoingMessages = ActAsInitiator || SupportsRequestReply;
 55830            ExpectIncomingMessages = !ActAsInitiator || SupportsRequestReply;
 55831            if (!ActAsInitiator)
 832            {
 55833                AddSupportingTokenAuthenticators(_securityBindingElement.EndpointSupportingTokenParameters, false, (ILis
 55834                AddSupportingTokenAuthenticators(_securityBindingElement.OptionalEndpointSupportingTokenParameters, true
 154835                foreach (string action in _securityBindingElement.OperationSupportingTokenParameters.Keys)
 836                {
 22837                    Collection<SupportingTokenAuthenticatorSpecification> authenticatorSpecList = new Collection<Support
 22838                    AddSupportingTokenAuthenticators(_securityBindingElement.OperationSupportingTokenParameters[action],
 22839                    ScopedSupportingTokenAuthenticatorSpecification.Add(action, authenticatorSpecList);
 840                }
 110841                foreach (string action in _securityBindingElement.OptionalOperationSupportingTokenParameters.Keys)
 842                {
 843                    Collection<SupportingTokenAuthenticatorSpecification> authenticatorSpecList;
 0844                    if (ScopedSupportingTokenAuthenticatorSpecification.TryGetValue(action, out ICollection<SupportingTo
 845                    {
 0846                        authenticatorSpecList = ((Collection<SupportingTokenAuthenticatorSpecification>)existingList);
 847                    }
 848                    else
 849                    {
 0850                        authenticatorSpecList = new Collection<SupportingTokenAuthenticatorSpecification>();
 0851                        ScopedSupportingTokenAuthenticatorSpecification.Add(action, authenticatorSpecList);
 852                    }
 0853                    AddSupportingTokenAuthenticators(_securityBindingElement.OptionalOperationSupportingTokenParameters[
 854                }
 855                // validate the token authenticator types and create a merged map if needed.
 55856                if (!ChannelSupportingTokenAuthenticatorSpecification.IsReadOnly)
 857                {
 55858                    if (ChannelSupportingTokenAuthenticatorSpecification.Count == 0)
 859                    {
 22860                        ChannelSupportingTokenAuthenticatorSpecification = EmptyTokenAuthenticators;
 861                    }
 862                    else
 863                    {
 33864                        ExpectSupportingTokens = true;
 132865                        foreach (SupportingTokenAuthenticatorSpecification tokenAuthenticatorSpec in ChannelSupportingTo
 866                        {
 33867                            SecurityUtils.OpenTokenAuthenticatorIfRequiredAsync(tokenAuthenticatorSpec.TokenAuthenticato
 33868                            if (tokenAuthenticatorSpec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Endors
 33869                                || tokenAuthenticatorSpec.SecurityTokenAttachmentMode == SecurityTokenAttachmentMode.Sig
 870                            {
 12871                                if (tokenAuthenticatorSpec.TokenParameters.RequireDerivedKeys && !tokenAuthenticatorSpec
 872                                {
 0873                                    ExpectKeyDerivation = true;
 874                                }
 875                            }
 33876                            SecurityTokenAttachmentMode mode = tokenAuthenticatorSpec.SecurityTokenAttachmentMode;
 33877                            if (mode == SecurityTokenAttachmentMode.SignedEncrypted
 33878                                || mode == SecurityTokenAttachmentMode.Signed
 33879                                || mode == SecurityTokenAttachmentMode.SignedEndorsing)
 880                            {
 21881                                _expectChannelSignedTokens = true;
 21882                                if (mode == SecurityTokenAttachmentMode.SignedEncrypted)
 883                                {
 19884                                    _expectChannelBasicTokens = true;
 885                                }
 886                            }
 33887                            if (mode == SecurityTokenAttachmentMode.Endorsing || mode == SecurityTokenAttachmentMode.Sig
 888                            {
 12889                                _expectChannelEndorsingTokens = true;
 890                            }
 891                        }
 33892                        ChannelSupportingTokenAuthenticatorSpecification =
 33893                            new ReadOnlyCollection<SupportingTokenAuthenticatorSpecification>((Collection<SupportingToke
 894                    }
 895                }
 55896                VerifyTypeUniqueness(ChannelSupportingTokenAuthenticatorSpecification);
 55897                MergeSupportingTokenAuthenticators(timeoutHelper.RemainingTime());
 898            }
 899
 55900            if (DetectReplays)
 901            {
 0902                if (!SupportsReplayDetection)
 903                {
 0904                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(DetectReplays), SR.Format(SR.Sec
 905                }
 0906                if (MaxClockSkew == TimeSpan.MaxValue || ReplayWindow == TimeSpan.MaxValue)
 907                {
 0908                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.NoncesCac
 909                }
 910
 911                // If DetectReplays is true and nonceCache is null then use the default InMemoryNonceCache.
 0912                if (_nonceCache == null)
 913                {
 914                    //TODO below (InMemoryNonceCache) is coming along with WindowsAuth, so uncomment
 915                    // The nonce needs to be cached for replayWindow + 2*clockSkew to eliminate replays
 916                    // this.nonceCache = new InMemoryNonceCache(this.ReplayWindow + this.MaxClockSkew + this.MaxClockSke
 917                }
 918            }
 919
 920            //this.derivedKeyTokenAuthenticator = new NonValidatingSecurityTokenAuthenticator<DerivedKeySecurityToken>()
 55921            return Task.CompletedTask;
 922        }
 923
 924        public virtual Task OnCloseAsync(TimeSpan timeout)
 925        {
 0926            OnClose(timeout);
 0927            return Task.CompletedTask;
 928        }
 929
 930
 931        internal void Open(string propertyName, bool requiredForForwardDirection, SecurityTokenAuthenticator authenticat
 932        {
 0933            if (authenticator != null)
 934            {
 0935                TimeoutHelper helper = new TimeoutHelper(timeout);
 0936                SecurityUtils.OpenTokenAuthenticatorIfRequiredAsync(authenticator, helper.GetCancellationToken());
 937            }
 938            else
 939            {
 0940                OnPropertySettingsError(propertyName, requiredForForwardDirection);
 941            }
 0942        }
 943
 944        internal void Open(string propertyName, bool requiredForForwardDirection, SecurityTokenProvider provider, TimeSp
 945        {
 0946            if (provider != null)
 947            {
 0948                SecurityUtils.OpenTokenProviderIfRequiredAsync(provider, new TimeoutHelper(timeout).GetCancellationToken
 949            }
 950            else
 951            {
 0952                OnPropertySettingsError(propertyName, requiredForForwardDirection);
 953            }
 0954        }
 955
 956        internal void OnPropertySettingsError(string propertyName, bool requiredForForwardDirection)
 957        {
 0958            if (requiredForForwardDirection)
 959            {
 0960                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentException(
 0961                    SR.Format(SR.PropertySettingErrorOnProtocolFactory, propertyName, this),
 0962                    propertyName));
 963            }
 0964            else if (_requestReplyErrorPropertyName == null)
 965            {
 0966                _requestReplyErrorPropertyName = propertyName;
 967            }
 0968        }
 969
 970        internal void ThrowIfImmutable()
 971        {
 836972            CommunicationObject.ThrowIfDisposedOrImmutable();
 836973        }
 974
 975        private void ThrowIfNotOpen()
 976        {
 34977            CommunicationObject.ThrowIfNotOpened();
 34978        }
 979
 980        public void OnClosed()
 981        {
 0982            throw new NotImplementedException();
 983        }
 984
 985        public void OnClosing()
 986        {
 0987            throw new NotImplementedException();
 988        }
 989
 990        public void OnFaulted()
 991        {
 0992            throw new NotImplementedException();
 993        }
 994
 995        public void OnOpened()
 996        {
 0997            throw new NotImplementedException();
 998        }
 999
 1000        public void OnOpening()
 1001        {
 01002            throw new NotImplementedException();
 1003        }
 1004    }
 1005
 1006    internal struct MergedSupportingTokenAuthenticatorSpecification
 1007    {
 1008        public Collection<SupportingTokenAuthenticatorSpecification> SupportingTokenAuthenticators;
 1009        public bool ExpectSignedTokens;
 1010        public bool ExpectEndorsingTokens;
 1011        public bool ExpectBasicTokens;
 1012    }
 1013}

Methods/Properties

.cctor()
.ctor()
.ctor(CoreWCF.Security.SecurityProtocolFactory)
CommunicationObject()
ActAsInitiator()
StreamBufferManager()
StreamBufferManager(CoreWCF.Channels.BufferManager)
ExtendedProtectionPolicy()
IsDuplexReply()
AddTimestamp()
AddTimestamp(System.Boolean)
DetectReplays()
DetectReplays(System.Boolean)
PrivacyNoticeUri()
PrivacyNoticeUri(System.Uri)
PrivacyNoticeVersion()
PrivacyNoticeVersion(System.Int32)
EndpointFilterTable()
EndpointFilterTable(CoreWCF.Dispatcher.IMessageFilterTable`1<CoreWCF.EndpointAddress>)
EmptyTokenAuthenticators()
DerivedKeyTokenAuthenticator()
ExpectIncomingMessages()
ExpectOutgoingMessages()
ExpectKeyDerivation()
ExpectSupportingTokens()
IncomingAlgorithmSuite()
IncomingAlgorithmSuite(CoreWCF.Security.SecurityAlgorithmSuite)
MaxCachedNonces()
MaxCachedNonces(System.Int32)
MaxClockSkew()
MaxClockSkew(System.TimeSpan)
NonceCache()
NonceCache(CoreWCF.Security.NonceCache)
OutgoingAlgorithmSuite()
OutgoingAlgorithmSuite(CoreWCF.Security.SecurityAlgorithmSuite)
ReplayWindow()
ReplayWindow(System.TimeSpan)
ChannelSupportingTokenAuthenticatorSpecification()
ScopedSupportingTokenAuthenticatorSpecification()
SecurityBindingElement()
SecurityBindingElement(CoreWCF.Channels.SecurityBindingElement)
SecurityTokenManager()
SecurityTokenManager(CoreWCF.IdentityModel.Selectors.SecurityTokenManager)
SupportsDuplex()
SecurityHeaderLayout()
SecurityHeaderLayout(CoreWCF.Channels.SecurityHeaderLayout)
SupportsReplayDetection()
SupportsRequestReply()
StandardsManager()
StandardsManager(CoreWCF.Security.SecurityStandardsManager)
TimestampValidityDuration()
TimestampValidityDuration(System.TimeSpan)
ListenUri()
ListenUri(System.Uri)
MessageSecurityVersion()
DefaultOpenTimeout()
DefaultCloseTimeout()
OnAbort()
OnClose(System.TimeSpan)
CreateListenerSecurityState()
CreateSecurityProtocol(CoreWCF.EndpointAddress,System.Uri,System.Boolean,System.TimeSpan)
GetIdentityOfSelf()
GetProperty()
VerifyTypeUniqueness(System.Collections.Generic.ICollection`1<CoreWCF.Security.SupportingTokenAuthenticatorSpecification>)
GetSupportingTokenAuthenticators(System.String,System.Boolean&,System.Boolean&,System.Boolean&)
MergeSupportingTokenAuthenticators(System.TimeSpan)
CreateRecipientSecurityTokenRequirement()
CreateRecipientSecurityTokenRequirement(CoreWCF.Security.Tokens.SecurityTokenParameters,CoreWCF.Security.SecurityTokenAttachmentMode)
AddSupportingTokenAuthenticators(CoreWCF.Security.Tokens.SupportingTokenParameters,System.Boolean,System.Collections.Generic.IList`1<CoreWCF.Security.SupportingTokenAuthenticatorSpecification>)
OpenAsync(System.TimeSpan)
OnOpenAsync(System.TimeSpan)
OnCloseAsync(System.TimeSpan)
Open(System.String,System.Boolean,CoreWCF.IdentityModel.Selectors.SecurityTokenAuthenticator,System.TimeSpan)
Open(System.String,System.Boolean,CoreWCF.IdentityModel.Selectors.SecurityTokenProvider,System.TimeSpan)
OnPropertySettingsError(System.String,System.Boolean)
ThrowIfImmutable()
ThrowIfNotOpen()
OnClosed()
OnClosing()
OnFaulted()
OnOpened()
OnOpening()