internal static ChannelProtectionRequirements CreateFromContract(ContractDescription contract, ProtectionLevel defaultRequestProtectionLevel, ProtectionLevel defaultResponseProtectionLevel, bool isForClient)
        {
            if (contract == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("contract"));
            }

            ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();

            ProtectionLevel contractScopeDefaultRequestProtectionLevel;
            ProtectionLevel contractScopeDefaultResponseProtectionLevel;

            if (contract.HasProtectionLevel)
            {
                contractScopeDefaultRequestProtectionLevel  = contract.ProtectionLevel;
                contractScopeDefaultResponseProtectionLevel = contract.ProtectionLevel;
            }
            else
            {
                contractScopeDefaultRequestProtectionLevel  = defaultRequestProtectionLevel;
                contractScopeDefaultResponseProtectionLevel = defaultResponseProtectionLevel;
            }

            foreach (OperationDescription operation in contract.Operations)
            {
                ProtectionLevel operationScopeDefaultRequestProtectionLevel;
                ProtectionLevel operationScopeDefaultResponseProtectionLevel;

                operationScopeDefaultRequestProtectionLevel  = contractScopeDefaultRequestProtectionLevel;
                operationScopeDefaultResponseProtectionLevel = contractScopeDefaultResponseProtectionLevel;

                foreach (MessageDescription message in operation.Messages)
                {
                    ProtectionLevel messageScopeDefaultProtectionLevel;
                    if (message.HasProtectionLevel)
                    {
                        messageScopeDefaultProtectionLevel = message.ProtectionLevel;
                    }
                    else if (message.Direction == MessageDirection.Input)
                    {
                        messageScopeDefaultProtectionLevel = operationScopeDefaultRequestProtectionLevel;
                    }
                    else
                    {
                        messageScopeDefaultProtectionLevel = operationScopeDefaultResponseProtectionLevel;
                    }

                    MessagePartSpecification signedParts    = new MessagePartSpecification();
                    MessagePartSpecification encryptedParts = new MessagePartSpecification();

                    // determine header protection requirements for message
                    foreach (MessageHeaderDescription header in message.Headers)
                    {
                        AddHeaderProtectionRequirements(header, signedParts, encryptedParts, messageScopeDefaultProtectionLevel);
                    }

                    // determine body protection requirements for message
                    ProtectionLevel bodyProtectionLevel;
                    if (message.Body.Parts.Count > 0)
                    {
                        // initialize the body protection level to none. all the body parts will be
                        // unioned to get the effective body protection level
                        bodyProtectionLevel = ProtectionLevel.None;
                    }
                    else if (message.Body.ReturnValue != null)
                    {
                        if (!(message.Body.ReturnValue.GetType().Equals(typeof(MessagePartDescription))))
                        {
                            Fx.Assert("Only body return values are supported currently");
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.OnlyBodyReturnValuesSupported));
                        }
                        MessagePartDescription desc = message.Body.ReturnValue;
                        bodyProtectionLevel = desc.HasProtectionLevel ? desc.ProtectionLevel : messageScopeDefaultProtectionLevel;
                    }
                    else
                    {
                        bodyProtectionLevel = messageScopeDefaultProtectionLevel;
                    }

                    // determine body protection requirements for message
                    if (message.Body.Parts.Count > 0)
                    {
                        foreach (MessagePartDescription body in message.Body.Parts)
                        {
                            ProtectionLevel partProtectionLevel = body.HasProtectionLevel ? body.ProtectionLevel : messageScopeDefaultProtectionLevel;
                            bodyProtectionLevel = ProtectionLevelHelper.Max(bodyProtectionLevel, partProtectionLevel);
                            if (bodyProtectionLevel == ProtectionLevel.EncryptAndSign)
                            {
                                break;
                            }
                        }
                    }
                    if (bodyProtectionLevel != ProtectionLevel.None)
                    {
                        signedParts.IsBodyIncluded = true;
                        if (bodyProtectionLevel == ProtectionLevel.EncryptAndSign)
                        {
                            encryptedParts.IsBodyIncluded = true;
                        }
                    }

                    // add requirements for message
                    if (message.Direction == MessageDirection.Input)
                    {
                        requirements.IncomingSignatureParts.AddParts(signedParts, message.Action);
                        requirements.IncomingEncryptionParts.AddParts(encryptedParts, message.Action);
                    }
                    else
                    {
                        requirements.OutgoingSignatureParts.AddParts(signedParts, message.Action);
                        requirements.OutgoingEncryptionParts.AddParts(encryptedParts, message.Action);
                    }
                }
                if (operation.Faults != null)
                {
                    if (operation.IsServerInitiated())
                    {
                        AddFaultProtectionRequirements(operation.Faults, requirements, operationScopeDefaultRequestProtectionLevel, true);
                    }
                    else
                    {
                        AddFaultProtectionRequirements(operation.Faults, requirements, operationScopeDefaultResponseProtectionLevel, false);
                    }
                }
            }

            return(requirements);
        }
Exemplo n.º 2
0
        internal static ChannelProtectionRequirements CreateFromContract(ContractDescription contract, ProtectionLevel defaultRequestProtectionLevel, ProtectionLevel defaultResponseProtectionLevel, bool isForClient)
        {
            ProtectionLevel protectionLevel;
            ProtectionLevel level2;

            if (contract == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("contract"));
            }
            ChannelProtectionRequirements requirements = new ChannelProtectionRequirements();

            if (contract.HasProtectionLevel)
            {
                protectionLevel = contract.ProtectionLevel;
                level2          = contract.ProtectionLevel;
            }
            else
            {
                protectionLevel = defaultRequestProtectionLevel;
                level2          = defaultResponseProtectionLevel;
            }
            foreach (OperationDescription description in contract.Operations)
            {
                ProtectionLevel level3;
                ProtectionLevel level4;
                if (description.HasProtectionLevel)
                {
                    level3 = description.ProtectionLevel;
                    level4 = description.ProtectionLevel;
                }
                else
                {
                    level3 = protectionLevel;
                    level4 = level2;
                }
                foreach (MessageDescription description2 in description.Messages)
                {
                    ProtectionLevel level5;
                    ProtectionLevel none;
                    if (description2.HasProtectionLevel)
                    {
                        level5 = description2.ProtectionLevel;
                    }
                    else if (description2.Direction == MessageDirection.Input)
                    {
                        level5 = level3;
                    }
                    else
                    {
                        level5 = level4;
                    }
                    MessagePartSpecification signedParts    = new MessagePartSpecification();
                    MessagePartSpecification encryptedParts = new MessagePartSpecification();
                    foreach (MessageHeaderDescription description3 in description2.Headers)
                    {
                        AddHeaderProtectionRequirements(description3, signedParts, encryptedParts, level5);
                    }
                    if (description2.Body.Parts.Count > 0)
                    {
                        none = ProtectionLevel.None;
                    }
                    else if (description2.Body.ReturnValue != null)
                    {
                        if (!description2.Body.ReturnValue.GetType().Equals(typeof(MessagePartDescription)))
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("OnlyBodyReturnValuesSupported")));
                        }
                        MessagePartDescription returnValue = description2.Body.ReturnValue;
                        none = returnValue.HasProtectionLevel ? returnValue.ProtectionLevel : level5;
                    }
                    else
                    {
                        none = level5;
                    }
                    if (description2.Body.Parts.Count > 0)
                    {
                        foreach (MessagePartDescription description5 in description2.Body.Parts)
                        {
                            ProtectionLevel level7 = description5.HasProtectionLevel ? description5.ProtectionLevel : level5;
                            none = ProtectionLevelHelper.Max(none, level7);
                            if (none == ProtectionLevel.EncryptAndSign)
                            {
                                break;
                            }
                        }
                    }
                    if (none != ProtectionLevel.None)
                    {
                        signedParts.IsBodyIncluded = true;
                        if (none == ProtectionLevel.EncryptAndSign)
                        {
                            encryptedParts.IsBodyIncluded = true;
                        }
                    }
                    if (description2.Direction == MessageDirection.Input)
                    {
                        requirements.IncomingSignatureParts.AddParts(signedParts, description2.Action);
                        requirements.IncomingEncryptionParts.AddParts(encryptedParts, description2.Action);
                    }
                    else
                    {
                        requirements.OutgoingSignatureParts.AddParts(signedParts, description2.Action);
                        requirements.OutgoingEncryptionParts.AddParts(encryptedParts, description2.Action);
                    }
                }
                if (description.Faults != null)
                {
                    if (description.IsServerInitiated())
                    {
                        AddFaultProtectionRequirements(description.Faults, requirements, level3, true);
                    }
                    else
                    {
                        AddFaultProtectionRequirements(description.Faults, requirements, level4, false);
                    }
                }
            }
            return(requirements);
        }