Beispiel #1
0
        public static ReceivedFault CreateFault11(XmlDictionaryReader reader, int maxBufferSize)
        {
            string str;
            string str2;

            reader.ReadStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            reader.ReadStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);
            XmlUtil.ReadContentAsQName(reader, out str2, out str);
            FaultCode code = new FaultCode(str2, str);

            reader.ReadEndElement();
            string xmlLang = reader.XmlLang;

            reader.MoveToContent();
            FaultReasonText translation = new FaultReasonText(reader.ReadElementContentAsString(XD.Message11Dictionary.FaultString.Value, XD.Message11Dictionary.FaultNamespace.Value), xmlLang);
            string          actor       = "";

            if (reader.IsStartElement(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace))
            {
                actor = reader.ReadElementContentAsString();
            }
            XmlBuffer detail = null;

            if (reader.IsStartElement(XD.Message11Dictionary.FaultDetail, XD.Message11Dictionary.FaultNamespace))
            {
                detail = new XmlBuffer(maxBufferSize);
                detail.OpenSection(reader.Quotas).WriteNode(reader, false);
                detail.CloseSection();
                detail.Close();
            }
            reader.ReadEndElement();
            return(new ReceivedFault(code, new FaultReason(translation), actor, actor, detail, EnvelopeVersion.Soap11));
        }
Beispiel #2
0
        public BufferedHeader(MessageVersion version, XmlBuffer buffer, XmlDictionaryReader reader, XmlAttributeHolder[] envelopeAttributes, XmlAttributeHolder[] headerAttributes)
        {
            _streamed = true;
            _buffer   = buffer;
            _version  = version;
            GetHeaderAttributes(reader, version, out _actor, out _mustUnderstand, out _relay, out _isRefParam);
            _name = reader.LocalName;
            _ns   = reader.NamespaceURI;
            Fx.Assert(_name != null, "");
            Fx.Assert(_ns != null, "");
            _bufferIndex = buffer.SectionCount;
            XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);

            // Write an enclosing Envelope tag
            writer.WriteStartElement(MessageStrings.Envelope);
            if (envelopeAttributes != null)
            {
                XmlAttributeHolder.WriteAttributes(envelopeAttributes, writer);
            }

            // Write and enclosing Header tag
            writer.WriteStartElement(MessageStrings.Header);
            if (headerAttributes != null)
            {
                XmlAttributeHolder.WriteAttributes(headerAttributes, writer);
            }

            writer.WriteNode(reader, false);

            writer.WriteEndElement();
            writer.WriteEndElement();

            buffer.CloseSection();
        }
Beispiel #3
0
        // Saves the element in the reader to the buffer (attributes preserved)
        // Type is populated from type attribute on reader
        // Reader must be positioned at an element
        public XmlSyndicationContent(XmlReader reader)
        {
            if (reader is null)
            {
                throw new ArgumentNullException(nameof(reader));
            }

            SyndicationFeedFormatter.MoveToStartElement(reader);
            if (reader.HasAttributes)
            {
                while (reader.MoveToNextAttribute())
                {
                    string name  = reader.LocalName;
                    string ns    = reader.NamespaceURI;
                    string value = reader.Value;
                    if (name == Atom10Constants.TypeTag && ns == string.Empty)
                    {
                        _type = value;
                    }
                    else if (!FeedUtils.IsXmlns(name, ns))
                    {
                        AttributeExtensions.Add(new XmlQualifiedName(name, ns), value);
                    }
                }
                reader.MoveToElement();
            }
            _type          = string.IsNullOrEmpty(_type) ? Atom10Constants.XmlMediaType : _type;
            _contentBuffer = new XmlBuffer(int.MaxValue);
            using (XmlDictionaryWriter writer = _contentBuffer.OpenSection(XmlDictionaryReaderQuotas.Max))
            {
                writer.WriteNode(reader, false);
            }
            _contentBuffer.CloseSection();
            _contentBuffer.Close();
        }
        internal static async Task <Tuple <XmlBuffer, XmlDictionaryWriter> > CreateBufferIfRequiredAndWriteNodeAsync(XmlBuffer buffer, XmlDictionaryWriter extWriter, XmlReader reader, int maxExtensionSize)
        {
            if (buffer == null)
            {
                buffer    = new XmlBuffer(maxExtensionSize);
                extWriter = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);
                extWriter.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
            }

            XmlDictionaryReader dictionaryReader = reader as XmlDictionaryReader;

            if (dictionaryReader != null)
            {
                // Reimplementing WriteNode for XmlDictionaryWriter asynchronously depends on multiple internal methods
                // so isn't feasible to reimplement here. As the primary scenario will be usage with an XmlReader which
                // isn't an XmlDictionaryReader, deferring to the synchronous implementation is a reasonable fallback.
                extWriter.WriteNode(reader, false);
            }
            else
            {
                await extWriter.WriteNodeAsync(reader, false);
            }

            return(Tuple.Create(buffer, extWriter));
        }
Beispiel #5
0
        public virtual XmlDictionaryReader GetAddressHeaderReader()
        {
            XmlBuffer           buffer = new XmlBuffer(int.MaxValue);
            XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            WriteAddressHeader(writer);
            buffer.CloseSection();
            buffer.Close();
            return(buffer.GetReader(0));
        }
 internal static void CreateBufferIfRequiredAndWriteNode(ref XmlBuffer buffer, ref XmlDictionaryWriter extWriter, XmlReader reader, int maxExtensionSize)
 {
     if (buffer == null)
     {
         buffer    = new XmlBuffer(maxExtensionSize);
         extWriter = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);
         extWriter.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
     }
     extWriter.WriteNode(reader, false);
 }
Beispiel #7
0
        internal MessageBuffer OnCreateBufferedCopy(int maxBufferSize, XmlDictionaryReaderQuotas quotas)
        {
            XmlBuffer           msgBuffer = new XmlBuffer(maxBufferSize);
            XmlDictionaryWriter writer    = msgBuffer.OpenSection(quotas);

            this.OnWriteMessage(writer);
            msgBuffer.CloseSection();
            msgBuffer.Close();
            return(new DefaultMessageBuffer(this, msgBuffer));
        }
        private XmlDictionaryReader GetComparableReader()
        {
            XmlBuffer           buffer = new XmlBuffer(0x7fffffff);
            XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            ParameterHeader.WriteStartHeader(writer, this, AddressingVersion.WSAddressingAugust2004);
            ParameterHeader.WriteHeaderContents(writer, this);
            writer.WriteEndElement();
            buffer.CloseSection();
            buffer.Close();
            return(buffer.GetReader(0));
        }
Beispiel #9
0
        private static ReceivedFault CreateFault12Driver(XmlDictionaryReader reader, int maxBufferSize, EnvelopeVersion version)
        {
            reader.ReadStartElement(XD.MessageDictionary.Fault, version.DictionaryNamespace);
            reader.ReadStartElement(XD.Message12Dictionary.FaultCode, version.DictionaryNamespace);
            FaultCode code = ReadFaultCode12Driver(reader, version);

            reader.ReadEndElement();
            List <FaultReasonText> translations = new List <FaultReasonText>();

            if (reader.IsEmptyElement)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new FormatException(SR.AtLeastOneFaultReasonMustBeSpecified));
            }
            else
            {
                reader.ReadStartElement(XD.Message12Dictionary.FaultReason, version.DictionaryNamespace);
                while (reader.IsStartElement(XD.Message12Dictionary.FaultText, version.DictionaryNamespace))
                {
                    translations.Add(ReadTranslation12(reader));
                }

                reader.ReadEndElement();
            }

            string actor = "";
            string node  = "";

            if (reader.IsStartElement(XD.Message12Dictionary.FaultNode, version.DictionaryNamespace))
            {
                node = reader.ReadElementContentAsString();
            }

            if (reader.IsStartElement(XD.Message12Dictionary.FaultRole, version.DictionaryNamespace))
            {
                actor = reader.ReadElementContentAsString();
            }

            XmlBuffer detail = null;

            if (reader.IsStartElement(XD.Message12Dictionary.FaultDetail, version.DictionaryNamespace))
            {
                detail = new XmlBuffer(maxBufferSize);
                XmlDictionaryWriter writer = detail.OpenSection(reader.Quotas);
                writer.WriteNode(reader, false);
                detail.CloseSection();
                detail.Close();
            }
            reader.ReadEndElement();
            FaultReason reason = new FaultReason(translations);

            return(new ReceivedFault(code, reason, actor, node, detail, version));
        }
Beispiel #10
0
        protected virtual XmlDictionaryReader OnGetReaderAtDetailContents()
        {
            XmlBuffer           detailBuffer = new XmlBuffer(int.MaxValue);
            XmlDictionaryWriter writer       = detailBuffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            OnWriteDetail(writer, EnvelopeVersion.Soap12);  // Wrap in soap 1.2 by default
            detailBuffer.CloseSection();
            detailBuffer.Close();
            XmlDictionaryReader reader = detailBuffer.GetReader(0);

            reader.Read(); // Skip the detail element
            return(reader);
        }
Beispiel #11
0
 internal BodyWriter OnCreateBufferedCopy(int maxBufferSize, XmlDictionaryReaderQuotas quotas)
 {
     XmlBuffer buffer = new XmlBuffer(maxBufferSize);
     using (XmlDictionaryWriter writer = buffer.OpenSection(quotas))
     {
         writer.WriteStartElement("a");
         OnWriteBodyContents(writer);
         writer.WriteEndElement();
     }
     buffer.CloseSection();
     buffer.Close();
     return new BufferedBodyWriter(buffer);
 }
        protected virtual XmlDictionaryReader OnGetReaderAtDetailContents()
        {
            XmlBuffer           buffer = new XmlBuffer(0x7fffffff);
            XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            this.OnWriteDetail(writer, EnvelopeVersion.Soap12);
            buffer.CloseSection();
            buffer.Close();
            XmlDictionaryReader reader = buffer.GetReader(0);

            reader.Read();
            return(reader);
        }
Beispiel #13
0
        public static XmlDictionaryReader GetReaderAtDetailContentsFeb2005(string detailName, string detailNamespace, XmlDictionaryReader headerReader)
        {
            XmlDictionaryReader reader;

            try
            {
                WsrmFeb2005Dictionary dictionary   = XD.WsrmFeb2005Dictionary;
                XmlDictionaryString   namespaceUri = dictionary.Namespace;
                XmlBuffer             buffer       = null;
                int sectionIndex = 0;
                int depth        = headerReader.Depth;
                headerReader.ReadFullStartElement(dictionary.SequenceFault, namespaceUri);
                while (headerReader.Depth > depth)
                {
                    if (((headerReader.NodeType == XmlNodeType.Element) && (headerReader.NamespaceURI == detailNamespace)) && (headerReader.LocalName == detailName))
                    {
                        if (buffer != null)
                        {
                            return(null);
                        }
                        buffer = new XmlBuffer(0x7fffffff);
                        try
                        {
                            sectionIndex = buffer.SectionCount;
                            buffer.OpenSection(headerReader.Quotas).WriteNode(headerReader, false);
                            continue;
                        }
                        finally
                        {
                            buffer.CloseSection();
                        }
                    }
                    if (headerReader.Depth == depth)
                    {
                        break;
                    }
                    headerReader.Read();
                }
                if (buffer == null)
                {
                    return(null);
                }
                buffer.Close();
                reader = buffer.GetReader(sectionIndex);
            }
            finally
            {
                headerReader.Close();
            }
            return(reader);
        }
Beispiel #14
0
        XmlDictionaryReader GetComparableReader()
        {
            XmlBuffer           buffer = new XmlBuffer(int.MaxValue);
            XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            // WSAddressingAugust2004 does not write the IsReferenceParameter attribute,
            // and that's good for a consistent comparable form
            ParameterHeader.WriteStartHeader(writer, this, AddressingVersion.WSAddressingAugust2004);
            ParameterHeader.WriteHeaderContents(writer, this);
            writer.WriteEndElement();
            buffer.CloseSection();
            buffer.Close();
            return(buffer.GetReader(0));
        }
Beispiel #15
0
 private void EnsureContentBuffer()
 {
     if (_contentBuffer == null)
     {
         XmlBuffer tmp = new XmlBuffer(int.MaxValue);
         using (XmlDictionaryWriter writer = tmp.OpenSection(XmlDictionaryReaderQuotas.Max))
         {
             this.WriteTo(writer, Atom10Constants.ContentTag, Atom10Constants.Atom10Namespace);
         }
         tmp.CloseSection();
         tmp.Close();
         _contentBuffer = tmp;
     }
 }
 private async Task EnsureContentBufferAsync()
 {
     if (_contentBuffer == null)
     {
         XmlBuffer tmp = new XmlBuffer(int.MaxValue);
         using (XmlDictionaryWriter writer = tmp.OpenSection(XmlDictionaryReaderQuotas.Max))
         {
             await WriteToAsync(writer, Atom10Constants.ContentTag, Atom10Constants.Atom10Namespace).ConfigureAwait(false);
         }
         tmp.CloseSection();
         tmp.Close();
         _contentBuffer = tmp;
     }
 }
 private void EnsureContentBuffer()
 {
     if (this.contentBuffer == null)
     {
         XmlBuffer buffer = new XmlBuffer(0x7fffffff);
         using (XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max))
         {
             base.WriteTo(writer, "content", "http://www.w3.org/2005/Atom");
         }
         buffer.CloseSection();
         buffer.Close();
         this.contentBuffer = buffer;
     }
 }
Beispiel #18
0
        internal BodyWriter OnCreateBufferedCopy(int maxBufferSize, XmlDictionaryReaderQuotas quotas)
        {
            XmlBuffer buffer = new XmlBuffer(maxBufferSize);

            using (XmlDictionaryWriter writer = buffer.OpenSection(quotas))
            {
                writer.WriteStartElement("a");
                OnWriteBodyContents(writer);
                writer.WriteEndElement();
            }
            buffer.CloseSection();
            buffer.Close();
            return(new BufferedBodyWriter(buffer));
        }
Beispiel #19
0
        public BufferedAddressHeader(XmlDictionaryReader reader)
        {
            buffer = new XmlBuffer(int.MaxValue);
            XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);

            Fx.Assert(reader.NodeType == XmlNodeType.Element, "");
            name = reader.LocalName;
            ns   = reader.NamespaceURI;
            Fx.Assert(name != null, "");
            Fx.Assert(ns != null, "");
            writer.WriteNode(reader, false);
            buffer.CloseSection();
            buffer.Close();
            isReferenceProperty = false;
        }
 private void EnsureBuffer()
 {
     if (_buffer == null)
     {
         _buffer = new XmlBuffer(int.MaxValue);
         using (XmlDictionaryWriter writer = _buffer.OpenSection(XmlDictionaryReaderQuotas.Max))
         {
             writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
             WriteTo(writer);
             writer.WriteEndElement();
         }
         _buffer.CloseSection();
         _buffer.Close();
         _bufferElementIndex = 0;
     }
 }
 static XmlBuffer CreateXmlBuffer(XmlDictionaryReader unparsedExtensionsReader, int maxExtensionSize)
 {
     XmlBuffer buffer = new XmlBuffer(maxExtensionSize);
     using (XmlDictionaryWriter writer = buffer.OpenSection(unparsedExtensionsReader.Quotas))
     {
         writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
         while (unparsedExtensionsReader.IsStartElement())
         {
             writer.WriteNode(unparsedExtensionsReader, false);
         }
         writer.WriteEndElement();
     }
     buffer.CloseSection();
     buffer.Close();
     return buffer;
 }
        public void WriteBodyToSign(Stream canonicalStream)
        {
            SetBodyId();

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

            canonicalWriter.StartCanonicalization(canonicalStream, false, null);
            WriteInnerMessageWithId(canonicalWriter);
            canonicalWriter.EndCanonicalization();
            canonicalWriter.Flush();
            _fullBodyBuffer.CloseSection();
            _fullBodyBuffer.Close();

            _state = BodyState.Signed;
        }
        static XmlBuffer CreateXmlBuffer(XmlDictionaryReader unparsedExtensionsReader, int maxExtensionSize)
        {
            XmlBuffer buffer = new XmlBuffer(maxExtensionSize);

            using (XmlDictionaryWriter writer = buffer.OpenSection(unparsedExtensionsReader.Quotas))
            {
                writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
                while (unparsedExtensionsReader.IsStartElement())
                {
                    writer.WriteNode(unparsedExtensionsReader, false);
                }
                writer.WriteEndElement();
            }
            buffer.CloseSection();
            buffer.Close();
            return(buffer);
        }
        private string GetSignatureStream(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, XmlDictionaryWriter writer, out Stream stream)
        {
            stream = new MemoryStream();
            XmlDictionaryWriter effectiveWriter;
            XmlBuffer           canonicalBuffer = null;

            if (writer.CanCanonicalize)
            {
                effectiveWriter = writer;
            }
            else
            {
                canonicalBuffer = new XmlBuffer(int.MaxValue);
                effectiveWriter = canonicalBuffer.OpenSection(XmlDictionaryReaderQuotas.Max);
            }

            effectiveWriter.StartCanonicalization(stream, false, null);

            header.WriteStartHeader(effectiveWriter, Version);
            if (headerId == null)
            {
                headerId = GenerateId();
                StandardsManager.IdManager.WriteIdAttribute(effectiveWriter, headerId);
            }
            header.WriteHeaderContents(effectiveWriter, Version);
            effectiveWriter.WriteEndElement();
            effectiveWriter.EndCanonicalization();
            effectiveWriter.Flush();

            if (!ReferenceEquals(effectiveWriter, writer))
            {
                Fx.Assert(canonicalBuffer != null, "Canonical buffer cannot be null.");
                canonicalBuffer.CloseSection();
                canonicalBuffer.Close();
                XmlDictionaryReader dicReader = canonicalBuffer.GetReader(0);
                writer.WriteNode(dicReader, false);
                dicReader.Close();
            }

            stream.Position = 0;

            return(headerId);
        }
 public SyndicationElementExtension(XmlReader xmlReader)
 {
     if (xmlReader == null)
     {
         throw new ArgumentNullException(nameof(xmlReader));
     }
     SyndicationFeedFormatter.MoveToStartElement(xmlReader);
     _outerName      = xmlReader.LocalName;
     _outerNamespace = xmlReader.NamespaceURI;
     _buffer         = new XmlBuffer(int.MaxValue);
     using (XmlDictionaryWriter writer = _buffer.OpenSection(XmlDictionaryReaderQuotas.Max))
     {
         writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
         writer.WriteNode(xmlReader, false);
         writer.WriteEndElement();
     }
     _buffer.CloseSection();
     _buffer.Close();
     _bufferElementIndex = 0;
 }
Beispiel #26
0
        public static ReceivedFault CreateFault11(XmlDictionaryReader reader, int maxBufferSize)
        {
            reader.ReadStartElement(XD.MessageDictionary.Fault, XD.Message11Dictionary.Namespace);
            string ns;
            string name;

            reader.ReadStartElement(XD.Message11Dictionary.FaultCode, XD.Message11Dictionary.FaultNamespace);
            XmlUtil.ReadContentAsQName(reader, out name, out ns);
            FaultCode code = new FaultCode(name, ns);

            reader.ReadEndElement();

            string xmlLang = reader.XmlLang;

            reader.MoveToContent();  // Don't do IsStartElement.  FaultString is required, so let the reader throw.
            string          text        = reader.ReadElementContentAsString(XD.Message11Dictionary.FaultString.Value, XD.Message11Dictionary.FaultNamespace.Value);
            FaultReasonText translation = new FaultReasonText(text, xmlLang);

            string actor = "";

            if (reader.IsStartElement(XD.Message11Dictionary.FaultActor, XD.Message11Dictionary.FaultNamespace))
            {
                actor = reader.ReadElementContentAsString();
            }

            XmlBuffer detail = null;

            if (reader.IsStartElement(XD.Message11Dictionary.FaultDetail, XD.Message11Dictionary.FaultNamespace))
            {
                detail = new XmlBuffer(maxBufferSize);
                XmlDictionaryWriter writer = detail.OpenSection(reader.Quotas);
                writer.WriteNode(reader, false);
                detail.CloseSection();
                detail.Close();
            }
            reader.ReadEndElement();
            FaultReason reason = new FaultReason(translation);

            return(new ReceivedFault(code, reason, actor, actor, detail, EnvelopeVersion.Soap11));
        }
Beispiel #27
0
        internal virtual XmlDictionaryReader GetReaderAtHeader()
        {
            XmlBuffer           buffer = new XmlBuffer(0x7fffffff);
            XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            this.WriteStartEnvelope(writer);
            MessageHeaders headers = this.Headers;

            for (int i = 0; i < headers.Count; i++)
            {
                headers.WriteHeader(i, writer);
            }
            writer.WriteEndElement();
            writer.WriteEndElement();
            buffer.CloseSection();
            buffer.Close();
            XmlDictionaryReader reader = buffer.GetReader(0);

            reader.ReadStartElement();
            reader.MoveToStartElement();
            return(reader);
        }
        private XmlBuffer GetOrCreateBufferOverExtensions()
        {
            if (_buffer != null)
            {
                return(_buffer);
            }
            XmlBuffer newBuffer = new XmlBuffer(int.MaxValue);

            using (XmlWriter writer = newBuffer.OpenSection(XmlDictionaryReaderQuotas.Max))
            {
                writer.WriteStartElement(Rss20Constants.ExtensionWrapperTag);
                for (int i = 0; i < this.Count; ++i)
                {
                    this[i].WriteTo(writer);
                }
                writer.WriteEndElement();
            }
            newBuffer.CloseSection();
            newBuffer.Close();
            _buffer = newBuffer;
            return(newBuffer);
        }
        private XmlBuffer GetOrCreateBufferOverExtensions()
        {
            if (this.buffer != null)
            {
                return(this.buffer);
            }
            XmlBuffer buffer = new XmlBuffer(0x7fffffff);

            using (XmlWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max))
            {
                writer.WriteStartElement("extensionWrapper");
                for (int i = 0; i < base.Count; i++)
                {
                    base[i].WriteTo(writer);
                }
                writer.WriteEndElement();
            }
            buffer.CloseSection();
            buffer.Close();
            this.buffer = buffer;
            return(buffer);
        }
Beispiel #30
0
        public void WriteBodyToSignThenEncrypt(Stream canonicalStream, EncryptedData encryptedData, SymmetricAlgorithm algorithm)
        {
            XmlBuffer           buffer = new XmlBuffer(0x7fffffff);
            XmlDictionaryWriter writer = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);

            this.WriteBodyToSignThenEncryptWithFragments(canonicalStream, false, null, encryptedData, algorithm, writer);
            ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(this.startBodyFragment.GetBuffer(), 0, (int)this.startBodyFragment.Length);
            ((IFragmentCapableXmlDictionaryWriter)writer).WriteFragment(this.endBodyFragment.GetBuffer(), 0, (int)this.endBodyFragment.Length);
            buffer.CloseSection();
            buffer.Close();
            this.startBodyFragment = null;
            this.endBodyFragment   = null;
            XmlDictionaryReader reader = buffer.GetReader(0);

            reader.MoveToContent();
            this.bodyPrefix = reader.Prefix;
            if (reader.HasAttributes)
            {
                this.bodyAttributes = System.IdentityModel.XmlAttributeHolder.ReadAttributes(reader);
            }
            reader.Close();
        }
        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 string GetSignatureHash(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, XmlDictionaryWriter writer, out byte[] hash)
        {
            XmlDictionaryWriter writer2;
            HashStream          stream = this.TakeHashStream();
            XmlBuffer           buffer = null;

            if (writer.CanCanonicalize)
            {
                writer2 = writer;
            }
            else
            {
                buffer  = new XmlBuffer(0x7fffffff);
                writer2 = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);
            }
            writer2.StartCanonicalization(stream, false, null);
            header.WriteStartHeader(writer2, base.Version);
            if (headerId == null)
            {
                headerId = base.GenerateId();
                base.StandardsManager.IdManager.WriteIdAttribute(writer2, headerId);
            }
            header.WriteHeaderContents(writer2, base.Version);
            writer2.WriteEndElement();
            writer2.EndCanonicalization();
            writer2.Flush();
            if (!object.ReferenceEquals(writer2, writer))
            {
                buffer.CloseSection();
                buffer.Close();
                XmlDictionaryReader reader = buffer.GetReader(0);
                writer.WriteNode(reader, false);
                reader.Close();
            }
            hash = stream.FlushHashAndGetValue();
            return(headerId);
        }
        public BufferedHeader(MessageVersion version, XmlBuffer buffer, XmlDictionaryReader reader, XmlAttributeHolder[] envelopeAttributes, XmlAttributeHolder[] headerAttributes)
        {
            _streamed = true;
            _buffer = buffer;
            _version = version;
            GetHeaderAttributes(reader, version, out _actor, out _mustUnderstand, out _relay, out _isRefParam);
            _name = reader.LocalName;
            _ns = reader.NamespaceURI;
            Fx.Assert(_name != null, "");
            Fx.Assert(_ns != null, "");
            _bufferIndex = buffer.SectionCount;
            XmlDictionaryWriter writer = buffer.OpenSection(reader.Quotas);

            // Write an enclosing Envelope tag
            writer.WriteStartElement(MessageStrings.Envelope);
            if (envelopeAttributes != null)
                XmlAttributeHolder.WriteAttributes(envelopeAttributes, writer);

            // Write and enclosing Header tag
            writer.WriteStartElement(MessageStrings.Header);
            if (headerAttributes != null)
                XmlAttributeHolder.WriteAttributes(headerAttributes, writer);

            writer.WriteNode(reader, false);

            writer.WriteEndElement();
            writer.WriteEndElement();

            buffer.CloseSection();
        }
        public void WriteBodyToSignThenEncrypt(Stream canonicalStream, EncryptedData encryptedData, SymmetricAlgorithm algorithm)
        {
            XmlBuffer buffer = new XmlBuffer(int.MaxValue);
            XmlDictionaryWriter fragmentingWriter = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);
            WriteBodyToSignThenEncryptWithFragments(canonicalStream, false, null, encryptedData, algorithm, fragmentingWriter);
            ((IFragmentCapableXmlDictionaryWriter)fragmentingWriter).WriteFragment(this.startBodyFragment.GetBuffer(), 0, (int)this.startBodyFragment.Length);
            ((IFragmentCapableXmlDictionaryWriter)fragmentingWriter).WriteFragment(this.endBodyFragment.GetBuffer(), 0, (int)this.endBodyFragment.Length);
            buffer.CloseSection();
            buffer.Close();

            this.startBodyFragment = null;
            this.endBodyFragment = null;

            XmlDictionaryReader reader = buffer.GetReader(0);
            reader.MoveToContent();
            this.bodyPrefix = reader.Prefix;
            if (reader.HasAttributes)
            {
                this.bodyAttributes = XmlAttributeHolder.ReadAttributes(reader);
            }
            reader.Close();
        }
 private string GetSignatureHash(MessageHeader header, string headerId, IPrefixGenerator prefixGenerator, XmlDictionaryWriter writer, out byte[] hash)
 {
     XmlDictionaryWriter writer2;
     HashStream stream = this.TakeHashStream();
     XmlBuffer buffer = null;
     if (writer.CanCanonicalize)
     {
         writer2 = writer;
     }
     else
     {
         buffer = new XmlBuffer(0x7fffffff);
         writer2 = buffer.OpenSection(XmlDictionaryReaderQuotas.Max);
     }
     writer2.StartCanonicalization(stream, false, null);
     header.WriteStartHeader(writer2, base.Version);
     if (headerId == null)
     {
         headerId = base.GenerateId();
         base.StandardsManager.IdManager.WriteIdAttribute(writer2, headerId);
     }
     header.WriteHeaderContents(writer2, base.Version);
     writer2.WriteEndElement();
     writer2.EndCanonicalization();
     writer2.Flush();
     if (!object.ReferenceEquals(writer2, writer))
     {
         buffer.CloseSection();
         buffer.Close();
         XmlDictionaryReader reader = buffer.GetReader(0);
         writer.WriteNode(reader, false);
         reader.Close();
     }
     hash = stream.FlushHashAndGetValue();
     return headerId;
 }