protected SecurityToken GetTokenAndEnsureOutgoingIdentity(SecurityTokenProvider provider, bool isEncryptionOn, TimeSpan timeout, SecurityTokenAuthenticator authenticator)
        {
            SecurityToken token = SecurityProtocol.GetToken(provider, base.Target, timeout);

            if (isEncryptionOn)
            {
                this.EnsureOutgoingIdentity(token, authenticator);
            }
            return(token);
        }
        private bool TryGetTokenSynchronouslyForOutgoingSecurity(Message message, SecurityProtocolCorrelationState correlationState, bool isBlockingCall, TimeSpan timeout, out SecurityToken encryptingToken, out SecurityToken signingToken, out IList <SupportingTokenSpecification> supportingTokens, out SecurityProtocolCorrelationState newCorrelationState)
        {
            AsymmetricSecurityProtocolFactory factory = this.Factory;

            encryptingToken     = null;
            signingToken        = null;
            newCorrelationState = null;
            supportingTokens    = null;
            TimeoutHelper helper = new TimeoutHelper(timeout);

            if (factory.ActAsInitiator)
            {
                if (!isBlockingCall || !base.TryGetSupportingTokens(this.Factory, base.Target, base.Via, message, helper.RemainingTime(), isBlockingCall, out supportingTokens))
                {
                    return(false);
                }
                if (factory.ApplyConfidentiality)
                {
                    encryptingToken = base.GetTokenAndEnsureOutgoingIdentity(this.initiatorAsymmetricTokenProvider, true, helper.RemainingTime(), this.initiatorAsymmetricTokenAuthenticator);
                }
                if (factory.ApplyIntegrity)
                {
                    signingToken        = SecurityProtocol.GetToken(this.initiatorCryptoTokenProvider, base.Target, helper.RemainingTime());
                    newCorrelationState = base.GetCorrelationState(signingToken);
                }
            }
            else
            {
                if (factory.ApplyConfidentiality)
                {
                    encryptingToken = base.GetCorrelationToken(correlationState);
                }
                if (factory.ApplyIntegrity)
                {
                    signingToken = SecurityProtocol.GetToken(factory.RecipientAsymmetricTokenProvider, null, helper.RemainingTime());
                }
            }
            return(true);
        }
        protected override SecurityProtocolCorrelationState VerifyIncomingMessageCore(ref Message message, string actor, TimeSpan timeout, SecurityProtocolCorrelationState[] correlationStates)
        {
            IList <SupportingTokenAuthenticatorSpecification> list;
            AsymmetricSecurityProtocolFactory factory = this.Factory;
            TimeoutHelper         helper               = new TimeoutHelper(timeout);
            ReceiveSecurityHeader securityHeader       = base.ConfigureReceiveSecurityHeader(message, string.Empty, correlationStates, out list);
            SecurityToken         requiredSigningToken = null;

            if (factory.ActAsInitiator)
            {
                SecurityTokenAuthenticator initiatorAsymmetricTokenAuthenticator;
                SecurityToken token        = null;
                SecurityToken primaryToken = null;
                if (factory.RequireIntegrity)
                {
                    primaryToken         = SecurityProtocol.GetToken(this.initiatorAsymmetricTokenProvider, null, helper.RemainingTime());
                    requiredSigningToken = primaryToken;
                }
                if (factory.RequireConfidentiality)
                {
                    token = base.GetCorrelationToken(correlationStates);
                    if (!System.ServiceModel.Security.SecurityUtils.HasSymmetricSecurityKey(token))
                    {
                        securityHeader.WrappedKeySecurityTokenAuthenticator = this.Factory.WrappedKeySecurityTokenAuthenticator;
                    }
                }
                if (factory.AllowSerializedSigningTokenOnReply)
                {
                    initiatorAsymmetricTokenAuthenticator = this.initiatorAsymmetricTokenAuthenticator;
                    requiredSigningToken = null;
                }
                else
                {
                    initiatorAsymmetricTokenAuthenticator = null;
                }
                securityHeader.ConfigureAsymmetricBindingClientReceiveHeader(primaryToken, factory.AsymmetricTokenParameters, token, factory.CryptoTokenParameters, initiatorAsymmetricTokenAuthenticator);
            }
            else
            {
                SecurityToken token4;
                if ((this.Factory.RecipientAsymmetricTokenProvider != null) && this.Factory.RequireConfidentiality)
                {
                    token4 = SecurityProtocol.GetToken(factory.RecipientAsymmetricTokenProvider, null, helper.RemainingTime());
                }
                else
                {
                    token4 = null;
                }
                securityHeader.ConfigureAsymmetricBindingServerReceiveHeader(this.Factory.RecipientCryptoTokenAuthenticator, this.Factory.CryptoTokenParameters, token4, this.Factory.AsymmetricTokenParameters, list);
                securityHeader.WrappedKeySecurityTokenAuthenticator = this.Factory.WrappedKeySecurityTokenAuthenticator;
                securityHeader.ConfigureOutOfBandTokenResolver(base.MergeOutOfBandResolvers(list, this.Factory.RecipientOutOfBandTokenResolverList));
            }
            base.ProcessSecurityHeader(securityHeader, ref message, requiredSigningToken, helper.RemainingTime(), correlationStates);
            SecurityToken signatureToken  = securityHeader.SignatureToken;
            SecurityToken encryptionToken = securityHeader.EncryptionToken;

            if (factory.RequireIntegrity)
            {
                if (factory.ActAsInitiator)
                {
                    ReadOnlyCollection <IAuthorizationPolicy> recipientTokenPolicies = this.initiatorAsymmetricTokenAuthenticator.ValidateToken(signatureToken);
                    MessageSecurityProtocol.EnsureNonWrappedToken(signatureToken, message);
                    this.DoIdentityCheckAndAttachInitiatorSecurityProperty(message, encryptionToken, signatureToken, recipientTokenPolicies);
                }
                else
                {
                    MessageSecurityProtocol.EnsureNonWrappedToken(signatureToken, message);
                    this.AttachRecipientSecurityProperty(message, signatureToken, encryptionToken, securityHeader.BasicSupportingTokens, securityHeader.EndorsingSupportingTokens, securityHeader.SignedEndorsingSupportingTokens, securityHeader.SignedSupportingTokens, securityHeader.SecurityTokenAuthorizationPoliciesMapping);
                }
            }
            return(base.GetCorrelationState(signatureToken, securityHeader));
        }