< Summary - CoreWCF Coverage — PR #1766

Information
Class: CoreWCF.Security.MessageSecurityProtocolFactory
Assembly: CoreWCF.Primitives
File(s): /home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/MessageSecurityProtocolFactory.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 98
Coverable lines: 98
Total lines: 290
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 50
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity NPath complexity Sequence coverage
.ctor()100%110%
.ctor(...)0%220%
ValidateCorrelationSecuritySettings()0%10100%
OnOpenAsync(...)0%880%
ExtractMessageParts(...)0%660%
GetIncomingEncryptionParts(...)0%660%
GetIncomingSignatureParts(...)0%660%
GetOutgoingEncryptionParts(...)0%660%
GetOutgoingSignatureParts(...)0%660%

File(s)

/home/runner/work/CoreWCF/CoreWCF/src/CoreWCF.Primitives/src/CoreWCF/Security/MessageSecurityProtocolFactory.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.Threading.Tasks;
 7using CoreWCF.Channels;
 8using CoreWCF.IdentityModel.Selectors;
 9using CoreWCF.Security.Tokens;
 10
 11namespace CoreWCF.Security
 12{
 13    internal abstract class MessageSecurityProtocolFactory : SecurityProtocolFactory
 14    {
 15        internal const MessageProtectionOrder defaultMessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
 16        internal const bool defaultDoRequestSignatureConfirmation = false;
 017        private bool _applyIntegrity = true;
 018        private bool _applyConfidentiality = true;
 19        private bool _doRequestSignatureConfirmation = defaultDoRequestSignatureConfirmation;
 20        private IdentityVerifier _identityVerifier;
 21        private MessageProtectionOrder _messageProtectionOrder = defaultMessageProtectionOrder;
 022        private bool _requireIntegrity = true;
 023        private bool _requireConfidentiality = true;
 24
 025        protected MessageSecurityProtocolFactory()
 26        {
 027        }
 28
 29        internal MessageSecurityProtocolFactory(MessageSecurityProtocolFactory factory)
 030            : base(factory)
 31        {
 032            if (factory == null)
 33            {
 034                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(factory));
 35            }
 36
 037            _applyIntegrity = factory._applyIntegrity;
 038            _applyConfidentiality = factory._applyConfidentiality;
 039            _identityVerifier = factory._identityVerifier;
 040            ProtectionRequirements = new ChannelProtectionRequirements(factory.ProtectionRequirements);
 041            _messageProtectionOrder = factory._messageProtectionOrder;
 042            _requireIntegrity = factory._requireIntegrity;
 043            _requireConfidentiality = factory._requireConfidentiality;
 044            _doRequestSignatureConfirmation = factory._doRequestSignatureConfirmation;
 045        }
 46
 47        public bool ApplyConfidentiality
 48        {
 49            get
 50            {
 051                return _applyConfidentiality;
 52            }
 53            set
 54            {
 055                ThrowIfImmutable();
 056                _applyConfidentiality = value;
 057            }
 58        }
 59
 60        public bool ApplyIntegrity
 61        {
 62            get
 63            {
 064                return _applyIntegrity;
 65            }
 66            set
 67            {
 068                ThrowIfImmutable();
 069                _applyIntegrity = value;
 070            }
 71        }
 72
 73        public bool DoRequestSignatureConfirmation
 74        {
 75            get
 76            {
 077                return _doRequestSignatureConfirmation;
 78            }
 79            set
 80            {
 081                ThrowIfImmutable();
 082                _doRequestSignatureConfirmation = value;
 083            }
 84        }
 85
 86        public IdentityVerifier IdentityVerifier
 87        {
 88            get
 89            {
 090                return _identityVerifier;
 91            }
 92            set
 93            {
 094                ThrowIfImmutable();
 095                _identityVerifier = value;
 096            }
 97        }
 98
 099        public ChannelProtectionRequirements ProtectionRequirements { get; } = new ChannelProtectionRequirements();
 100
 101        public MessageProtectionOrder MessageProtectionOrder
 102        {
 103            get
 104            {
 0105                return _messageProtectionOrder;
 106            }
 107            set
 108            {
 0109                ThrowIfImmutable();
 0110                _messageProtectionOrder = value;
 0111            }
 112        }
 113
 114        public bool RequireIntegrity
 115        {
 116            get
 117            {
 0118                return _requireIntegrity;
 119            }
 120            set
 121            {
 0122                ThrowIfImmutable();
 0123                _requireIntegrity = value;
 0124            }
 125        }
 126
 127        public bool RequireConfidentiality
 128        {
 129            get
 130            {
 0131                return _requireConfidentiality;
 132            }
 133            set
 134            {
 0135                ThrowIfImmutable();
 0136                _requireConfidentiality = value;
 0137            }
 138        }
 139
 0140        internal List<SecurityTokenAuthenticator> WrappedKeySecurityTokenAuthenticator { get; private set; }
 141
 142        protected virtual void ValidateCorrelationSecuritySettings()
 143        {
 0144            if (ActAsInitiator && SupportsRequestReply)
 145            {
 0146                bool savesCorrelationTokenOnRequest = ApplyIntegrity || ApplyConfidentiality;
 0147                bool needsCorrelationTokenOnReply = RequireIntegrity || RequireConfidentiality;
 0148                if (!savesCorrelationTokenOnRequest && needsCorrelationTokenOnReply)
 149                {
 0150                    OnPropertySettingsError(nameof(ApplyIntegrity), false);
 151                }
 152            }
 0153        }
 154
 155        public override Task OnOpenAsync(TimeSpan timeout)
 156        {
 0157            base.OnOpenAsync(timeout);
 0158            ProtectionRequirements.MakeReadOnly();
 159
 0160            if (DetectReplays && !RequireIntegrity)
 161            {
 0162                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(RequireIntegrity), SR.ForReplayDetec
 163            }
 164
 0165            if (DoRequestSignatureConfirmation)
 166            {
 0167                if (!SupportsRequestReply)
 168                {
 0169                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.SignatureConfirmationRequiresRequest
 170                }
 171                //TODO fix below
 172                //if (!this.StandardsManager.SecurityVersion.SupportsSignatureConfirmation)
 173                //{
 174                //    throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(SR.Format(SR.SecurityVersionDoesNotSu
 175                //}
 176            }
 177
 0178            WrappedKeySecurityTokenAuthenticator = new List<SecurityTokenAuthenticator>(1);
 0179            SecurityTokenAuthenticator authenticator = new NonValidatingSecurityTokenAuthenticator<WrappedKeySecurityTok
 0180            WrappedKeySecurityTokenAuthenticator.Add(authenticator);
 181
 0182            ValidateCorrelationSecuritySettings();
 0183            return Task.CompletedTask;
 184        }
 185
 186        private static MessagePartSpecification ExtractMessageParts(string action,
 187            ScopedMessagePartSpecification scopedParts, bool isForSignature)
 188        {
 0189            if (scopedParts.TryGetParts(action, out MessagePartSpecification parts))
 190            {
 0191                return parts;
 192            }
 0193            else if (scopedParts.TryGetParts(MessageHeaders.WildcardAction, out parts))
 194            {
 0195                return parts;
 196            }
 197
 198            // send back a fault indication that the action is unknown
 0199            SecurityVersion wss = MessageSecurityVersion.Default.SecurityVersion;
 0200            FaultCode subCode = new FaultCode(wss.InvalidSecurityFaultCode.Value, wss.HeaderNamespace.Value);
 0201            FaultCode senderCode = FaultCode.CreateSenderFaultCode(subCode);
 0202            FaultReason reason = new FaultReason(SR.Format(SR.InvalidOrUnrecognizedAction, action), System.Globalization
 0203            MessageFault fault = MessageFault.CreateFault(senderCode, reason);
 0204            if (isForSignature)
 205            {
 0206                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.NoSi
 207            }
 208            else
 209            {
 0210                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new MessageSecurityException(SR.Format(SR.NoEn
 211            }
 212        }
 213
 214        internal MessagePartSpecification GetIncomingEncryptionParts(string action)
 215        {
 0216            if (RequireConfidentiality)
 217            {
 0218                if (IsDuplexReply)
 219                {
 0220                    return ExtractMessageParts(action, ProtectionRequirements.OutgoingEncryptionParts, false);
 221                }
 222                else
 223                {
 0224                    return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.OutgoingEncryptionParts
 225                }
 226            }
 227            else
 228            {
 0229                return MessagePartSpecification.NoParts;
 230            }
 231        }
 232
 233        internal MessagePartSpecification GetIncomingSignatureParts(string action)
 234        {
 0235            if (RequireIntegrity)
 236            {
 0237                if (IsDuplexReply)
 238                {
 0239                    return ExtractMessageParts(action, ProtectionRequirements.OutgoingSignatureParts, true);
 240                }
 241                else
 242                {
 0243                    return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.OutgoingSignatureParts 
 244                }
 245            }
 246            else
 247            {
 0248                return MessagePartSpecification.NoParts;
 249            }
 250        }
 251
 252        internal MessagePartSpecification GetOutgoingEncryptionParts(string action)
 253        {
 0254            if (ApplyConfidentiality)
 255            {
 0256                if (IsDuplexReply)
 257                {
 0258                    return ExtractMessageParts(action, ProtectionRequirements.OutgoingEncryptionParts, false);
 259                }
 260                else
 261                {
 0262                    return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.IncomingEncryptionParts
 263                }
 264            }
 265            else
 266            {
 0267                return MessagePartSpecification.NoParts;
 268            }
 269        }
 270
 271        internal MessagePartSpecification GetOutgoingSignatureParts(string action)
 272        {
 0273            if (ApplyIntegrity)
 274            {
 0275                if (IsDuplexReply)
 276                {
 0277                    return ExtractMessageParts(action, ProtectionRequirements.OutgoingSignatureParts, true);
 278                }
 279                else
 280                {
 0281                    return ExtractMessageParts(action, (ActAsInitiator) ? ProtectionRequirements.IncomingSignatureParts 
 282                }
 283            }
 284            else
 285            {
 0286                return MessagePartSpecification.NoParts;
 287            }
 288        }
 289    }
 290}