protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            switch (_state)
            {
            case BodyState.Created:
                InnerMessage.WriteBodyContents(writer);
                return;

            case BodyState.Signed:
            case BodyState.EncryptedThenSigned:
                XmlDictionaryReader reader = _fullBodyBuffer.GetReader(0);
                reader.ReadStartElement();
                while (reader.NodeType != XmlNodeType.EndElement)
                {
                    writer.WriteNode(reader, false);
                }

                reader.ReadEndElement();
                reader.Close();
                return;

            case BodyState.Encrypted:
            case BodyState.SignedThenEncrypted:
                _encryptedBodyContent.WriteTo(writer, ServiceModelDictionaryManager.Instance);
                break;

            default:
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateBadStateException(nameof(OnWriteBodyContents)));
            }
        }
        public void WriteBodyToEncrypt(EncryptedData encryptedData, SymmetricAlgorithm algorithm)
        {
            encryptedData.Id = _securityHeader.GenerateId();

            BodyContentHelper   helper           = new BodyContentHelper();
            XmlDictionaryWriter encryptingWriter = helper.CreateWriter();

            InnerMessage.WriteBodyContents(encryptingWriter);
            encryptedData.SetUpEncryption(algorithm, helper.ExtractResult());
            _encryptedBodyContent = encryptedData;

            _state = BodyState.Encrypted;
        }
        public void WriteBodyToSignWithFragments(Stream stream, bool includeComments, string[] inclusivePrefixes, XmlDictionaryWriter writer)
        {
            IFragmentCapableXmlDictionaryWriter fragmentingWriter = (IFragmentCapableXmlDictionaryWriter)writer;

            SetBodyId();
            BufferedOutputStream fullBodyFragment = new BufferManagerOutputStream(SR.XmlBufferQuotaExceeded, 1024, int.MaxValue, _securityHeader.StreamBufferManager);

            writer.StartCanonicalization(stream, includeComments, inclusivePrefixes);
            fragmentingWriter.StartFragment(fullBodyFragment, false);
            WriteStartInnerMessageWithId(writer);
            InnerMessage.WriteBodyContents(writer);
            writer.WriteEndElement();
            fragmentingWriter.EndFragment();
            writer.EndCanonicalization();

            _fullBodyFragment = fullBodyFragment.ToArray(out _fullBodyFragmentLength);

            _state = BodyState.Signed;
        }
        protected override void OnWriteBodyContents(XmlDictionaryWriter writer)
        {
            if (_state == BodyState.Created)
            {
                InnerMessage.WriteBodyContents(writer);
                return;
            }

            XmlDictionaryReader reader = CreateFullBodyReader();

            reader.ReadStartElement();
            while (reader.NodeType != XmlNodeType.EndElement)
            {
                writer.WriteNode(reader, false);
            }

            reader.ReadEndElement();
            reader.Close();
        }
        public void WriteBodyToSignThenEncryptWithFragments(
            Stream stream, bool includeComments, string[] inclusivePrefixes,
            EncryptedData encryptedData, SymmetricAlgorithm algorithm, XmlDictionaryWriter writer)
        {
            IFragmentCapableXmlDictionaryWriter fragmentingWriter = (IFragmentCapableXmlDictionaryWriter)writer;

            SetBodyId();
            encryptedData.Id = _securityHeader.GenerateId();

            _startBodyFragment = new MemoryStream();
            BufferedOutputStream bodyContentFragment = new BufferManagerOutputStream(SR.XmlBufferQuotaExceeded, 1024, int.MaxValue, _securityHeader.StreamBufferManager);

            _endBodyFragment = new MemoryStream();

            writer.StartCanonicalization(stream, includeComments, inclusivePrefixes);

            fragmentingWriter.StartFragment(_startBodyFragment, false);
            WriteStartInnerMessageWithId(writer);
            fragmentingWriter.EndFragment();

            fragmentingWriter.StartFragment(bodyContentFragment, true);
            InnerMessage.WriteBodyContents(writer);
            fragmentingWriter.EndFragment();

            fragmentingWriter.StartFragment(_endBodyFragment, false);
            writer.WriteEndElement();
            fragmentingWriter.EndFragment();

            writer.EndCanonicalization();

            byte[] bodyBuffer = bodyContentFragment.ToArray(out int bodyLength);

            encryptedData.SetUpEncryption(algorithm, new ArraySegment <byte>(bodyBuffer, 0, bodyLength));
            _encryptedBodyContent = encryptedData;

            _state = BodyState.SignedThenEncrypted;
        }
        public void WriteBodyToEncryptThenSign(Stream canonicalStream, EncryptedData encryptedData, SymmetricAlgorithm algorithm)
        {
            encryptedData.Id = _securityHeader.GenerateId();
            SetBodyId();

            XmlDictionaryWriter encryptingWriter = XmlDictionaryWriter.CreateTextWriter(Stream.Null);

            // The XmlSerializer body formatter would add a
            // document declaration to the body fragment when a fresh writer
            // is provided. Hence, insert a dummy element here and capture
            // the body contents as a fragment.
            encryptingWriter.WriteStartElement("a");
            MemoryStream ms = new MemoryStream();

            ((IFragmentCapableXmlDictionaryWriter)encryptingWriter).StartFragment(ms, true);

            InnerMessage.WriteBodyContents(encryptingWriter);
            ((IFragmentCapableXmlDictionaryWriter)encryptingWriter).EndFragment();
            encryptingWriter.WriteEndElement();
            ms.Flush();
            encryptedData.SetUpEncryption(algorithm, new ArraySegment <byte>(ms.GetBuffer(), 0, (int)ms.Length));

            _fullBodyBuffer = new XmlBuffer(int.MaxValue);
            XmlDictionaryWriter canonicalWriter = _fullBodyBuffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            canonicalWriter.StartCanonicalization(canonicalStream, false, null);
            WriteStartInnerMessageWithId(canonicalWriter);
            encryptedData.WriteTo(canonicalWriter, ServiceModelDictionaryManager.Instance);
            canonicalWriter.WriteEndElement();
            canonicalWriter.EndCanonicalization();
            canonicalWriter.Flush();

            _fullBodyBuffer.CloseSection();
            _fullBodyBuffer.Close();

            _state = BodyState.EncryptedThenSigned;
        }
 private void WriteInnerMessageWithId(XmlDictionaryWriter writer)
 {
     WriteStartInnerMessageWithId(writer);
     InnerMessage.WriteBodyContents(writer);
     writer.WriteEndElement();
 }