public override void ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator)
        {
            SecurityAppliedMessage message = this.SecurityAppliedMessage;
            EncryptedData          encryptedData;
            HashStream             hashStream;

            switch (message.BodyProtectionMode)
            {
            case MessagePartProtectionMode.None:
                return;

            case MessagePartProtectionMode.Sign:
                hashStream = TakeHashStream();
                if (CanCanonicalizeAndFragment(writer))
                {
                    message.WriteBodyToSignWithFragments(hashStream, false, null, writer);
                }
                else
                {
                    message.WriteBodyToSign(hashStream);
                }
                this.signedInfo.AddReference(message.BodyId, hashStream.FlushHashAndGetValue());
                return;

            case MessagePartProtectionMode.SignThenEncrypt:
                hashStream    = TakeHashStream();
                encryptedData = CreateEncryptedDataForBody();
                if (CanCanonicalizeAndFragment(writer))
                {
                    message.WriteBodyToSignThenEncryptWithFragments(hashStream, false, null, encryptedData, this.encryptingSymmetricAlgorithm, writer);
                }
                else
                {
                    message.WriteBodyToSignThenEncrypt(hashStream, encryptedData, this.encryptingSymmetricAlgorithm);
                }
                this.signedInfo.AddReference(message.BodyId, hashStream.FlushHashAndGetValue());
                this.referenceList.AddReferredId(encryptedData.Id);
                this.hasSignedEncryptedMessagePart = true;
                return;

            case MessagePartProtectionMode.Encrypt:
                encryptedData = CreateEncryptedDataForBody();
                message.WriteBodyToEncrypt(encryptedData, this.encryptingSymmetricAlgorithm);
                this.referenceList.AddReferredId(encryptedData.Id);
                return;

            case MessagePartProtectionMode.EncryptThenSign:
                hashStream    = TakeHashStream();
                encryptedData = CreateEncryptedDataForBody();
                message.WriteBodyToEncryptThenSign(hashStream, encryptedData, this.encryptingSymmetricAlgorithm);
                this.signedInfo.AddReference(message.BodyId, hashStream.FlushHashAndGetValue());
                this.referenceList.AddReferredId(encryptedData.Id);
                return;

            default:
                Fx.Assert("Invalid MessagePartProtectionMode");
                return;
            }
        }
        public override void ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator)
        {
            EncryptedData          data;
            HashStream             stream;
            SecurityAppliedMessage securityAppliedMessage = base.SecurityAppliedMessage;

            switch (securityAppliedMessage.BodyProtectionMode)
            {
            case MessagePartProtectionMode.Sign:
                stream = this.TakeHashStream();
                if (!CanCanonicalizeAndFragment(writer))
                {
                    securityAppliedMessage.WriteBodyToSign(stream);
                }
                else
                {
                    securityAppliedMessage.WriteBodyToSignWithFragments(stream, false, null, writer);
                }
                this.signedInfo.AddReference(securityAppliedMessage.BodyId, stream.FlushHashAndGetValue());
                return;

            case MessagePartProtectionMode.Encrypt:
                data = this.CreateEncryptedDataForBody();
                securityAppliedMessage.WriteBodyToEncrypt(data, this.encryptingSymmetricAlgorithm);
                this.referenceList.AddReferredId(data.Id);
                return;

            case MessagePartProtectionMode.SignThenEncrypt:
                stream = this.TakeHashStream();
                data   = this.CreateEncryptedDataForBody();
                if (!CanCanonicalizeAndFragment(writer))
                {
                    securityAppliedMessage.WriteBodyToSignThenEncrypt(stream, data, this.encryptingSymmetricAlgorithm);
                }
                else
                {
                    securityAppliedMessage.WriteBodyToSignThenEncryptWithFragments(stream, false, null, data, this.encryptingSymmetricAlgorithm, writer);
                }
                this.signedInfo.AddReference(securityAppliedMessage.BodyId, stream.FlushHashAndGetValue());
                this.referenceList.AddReferredId(data.Id);
                this.hasSignedEncryptedMessagePart = true;
                return;

            case MessagePartProtectionMode.EncryptThenSign:
                stream = this.TakeHashStream();
                data   = this.CreateEncryptedDataForBody();
                securityAppliedMessage.WriteBodyToEncryptThenSign(stream, data, this.encryptingSymmetricAlgorithm);
                this.signedInfo.AddReference(securityAppliedMessage.BodyId, stream.FlushHashAndGetValue());
                this.referenceList.AddReferredId(data.Id);
                return;
            }
        }
        public override void ApplyBodySecurity(XmlDictionaryWriter writer, IPrefixGenerator prefixGenerator)
        {
            SecurityAppliedMessage message = SecurityAppliedMessage;

            switch (message.BodyProtectionMode)
            {
            case MessagePartProtectionMode.None:
                return;

            case MessagePartProtectionMode.Sign:
                var ms = new MemoryStream();
                if (CanCanonicalizeAndFragment(writer))
                {
                    message.WriteBodyToSignWithFragments(ms, false, null, writer);
                }
                else
                {
                    message.WriteBodyToSign(ms);
                }

                ms.Position = 0;
                AddReference("#" + message.BodyId, ms);
                return;

            case MessagePartProtectionMode.SignThenEncrypt:
                throw new PlatformNotSupportedException();

            case MessagePartProtectionMode.Encrypt:
                throw new PlatformNotSupportedException();

            case MessagePartProtectionMode.EncryptThenSign:
                throw new PlatformNotSupportedException();

            default:
                Fx.Assert("Invalid MessagePartProtectionMode");
                return;
            }
        }
Example #4
0
        public Message SetupExecution()
        {
            ThrowIfProcessingStarted();
            SetProcessingStarted();

            bool signBody = false;

            if (ElementContainer.SourceSigningToken != null)
            {
                throw ExceptionHelper.PlatformNotSupported();
            }

            bool encryptBody = false;

            if (ElementContainer.SourceEncryptionToken != null)
            {
                throw ExceptionHelper.PlatformNotSupported();
            }

            SecurityAppliedMessage message = new SecurityAppliedMessage(Message, this, signBody, encryptBody);

            Message = message;
            return(message);
        }
        public Message SetupExecution()
        {
            ThrowIfProcessingStarted();
            SetProcessingStarted();

            bool signBody = false;
            if (this.elementContainer.SourceSigningToken != null)
            {
                if (this.signatureParts == null)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("SignatureParts"), this.Message);
                }
                signBody = this.signatureParts.IsBodyIncluded;
            }

            bool encryptBody = false;
            if (this.elementContainer.SourceEncryptionToken != null)
            {
                if (this.encryptionParts == null)
                {
                    throw TraceUtility.ThrowHelperError(new ArgumentNullException("EncryptionParts"), this.Message);
                }
                encryptBody = this.encryptionParts.IsBodyIncluded;
            }

            SecurityAppliedMessage message = new SecurityAppliedMessage(this.Message, this, signBody, encryptBody);
            this.Message = message;
            return message;
        }