Exemplo n.º 1
0
 public override void LoadInput(object obj)
 {
     _inputStream?.Close();
     _inputStream = new MemoryStream();
     if (obj is Stream)
     {
         _inputStream = (Stream)obj;
     }
     else if (obj is XmlNodeList)
     {
         CanonicalXml xmlDoc = new CanonicalXml((XmlNodeList)obj, null, _includeComments);
         byte[]       buffer = xmlDoc.GetBytes();
         if (buffer == null)
         {
             return;
         }
         _inputStream.Write(buffer, 0, buffer.Length);
         _inputStream.Flush();
         _inputStream.Position = 0;
     }
     else if (obj is XmlDocument)
     {
         CanonicalXml xmlDoc = new CanonicalXml((XmlDocument)obj, null, _includeComments);
         byte[]       buffer = xmlDoc.GetBytes();
         if (buffer == null)
         {
             return;
         }
         _inputStream.Write(buffer, 0, buffer.Length);
         _inputStream.Flush();
         _inputStream.Position = 0;
     }
 }
Exemplo n.º 2
0
 public override void LoadInput(object obj)
 {
     if (this._inputStream != null)
     {
         this._inputStream.Close();
     }
     this._inputStream = new MemoryStream();
     if (obj is Stream)
     {
         this._inputStream = (Stream)obj;
     }
     else if (obj is XmlNodeList)
     {
         byte[] bytes = new CanonicalXml((XmlNodeList)obj, null, this._includeComments).GetBytes();
         if (bytes != null)
         {
             this._inputStream.Write(bytes, 0, bytes.Length);
             this._inputStream.Flush();
             this._inputStream.Position = 0L;
         }
     }
     else if (obj is XmlDocument)
     {
         byte[] buffer = new CanonicalXml((XmlDocument)obj, null, this._includeComments).GetBytes();
         if (buffer != null)
         {
             this._inputStream.Write(buffer, 0, buffer.Length);
             this._inputStream.Flush();
             this._inputStream.Position = 0L;
         }
     }
 }
Exemplo n.º 3
0
        private void LoadStreamInput(Stream stream)
        {
            XmlDocument doc = new XmlDocument();

            doc.PreserveWhitespace = true;
            doc.Load(stream);
            _inputNodeList = CanonicalXml.AllDescendantNodes(doc, true);
        }
Exemplo n.º 4
0
        private void LoadXmlNodeListInput(XmlNodeList nodeList)
        {
            // Use C14N to get a document
            XmlResolver  resolver = (ResolverSet ? _xmlResolver : XmlResolverHelper.GetThrowingResolver());
            CanonicalXml c14n     = new CanonicalXml((XmlNodeList)nodeList, resolver, true);

            using (MemoryStream ms = new MemoryStream(c14n.GetBytes()))
            {
                LoadStreamInput(ms);
            }
        }
        private void LoadXmlNodeListInput(XmlNodeList nodeList)
        {
            // Use C14N to get a document
            XmlResolver  resolver = (ResolverSet ? _xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), BaseURI));
            CanonicalXml c14n     = new CanonicalXml((XmlNodeList)nodeList, resolver, true);

            using (MemoryStream ms = new MemoryStream(c14n.GetBytes()))
            {
                LoadStreamInput(ms);
            }
        }
        private void LoadXmlNodeListInput(XmlNodeList nodeList)
        {
            XmlResolver  resolver = base.ResolverSet ? base.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), base.BaseURI);
            CanonicalXml xml      = new CanonicalXml(nodeList, resolver, true);

            using (MemoryStream stream = new MemoryStream(xml.GetBytes()))
            {
                this._document = new XmlDocument();
                this._document.PreserveWhitespace = true;
                this._document.Load(stream);
            }
        }
Exemplo n.º 7
0
 /// <include file='doc\Transform.uex' path='docs/doc[@for="XmlDsigC14NTransform.LoadInput"]/*' />
 public override void LoadInput(Object obj)
 {
     if (obj is Stream)
     {
         XmlResolver resolver = (this.ResolverSet ? this.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), this.BaseURI));
         _cXml = new CanonicalXml((Stream)obj, _includeComments, resolver, this.BaseURI);
         return;
     }
     if (obj is XmlDocument)
     {
         _cXml = new CanonicalXml((XmlDocument)obj, _includeComments);
         return;
     }
     if (obj is XmlNodeList)
     {
         _cXml = new CanonicalXml((XmlNodeList)obj, _includeComments);
     }
 }
 public override void LoadInput(object obj)
 {
     XmlResolver resolver = base.ResolverSet ? base.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), base.BaseURI);
     if (obj is Stream)
     {
         this._cXml = new CanonicalXml((Stream) obj, this._includeComments, resolver, base.BaseURI);
     }
     else if (obj is XmlDocument)
     {
         this._cXml = new CanonicalXml((XmlDocument) obj, resolver, this._includeComments);
     }
     else
     {
         if (!(obj is XmlNodeList))
         {
             throw new ArgumentException(SecurityResources.GetResourceString("Cryptography_Xml_IncorrectObjectType"), "obj");
         }
         this._cXml = new CanonicalXml((XmlNodeList) obj, resolver, this._includeComments);
     }
 }
        public override void LoadInput(object obj)
        {
            XmlResolver resolver = base.ResolverSet ? base.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), base.BaseURI);

            if (obj is Stream)
            {
                this._cXml = new CanonicalXml((Stream)obj, this._includeComments, resolver, base.BaseURI);
            }
            else if (obj is XmlDocument)
            {
                this._cXml = new CanonicalXml((XmlDocument)obj, resolver, this._includeComments);
            }
            else
            {
                if (!(obj is XmlNodeList))
                {
                    throw new ArgumentException(SecurityResources.GetResourceString("Cryptography_Xml_IncorrectObjectType"), "obj");
                }
                this._cXml = new CanonicalXml((XmlNodeList)obj, resolver, this._includeComments);
            }
        }
        public override void LoadInput(object obj)
        {
            XmlResolver resolver = (ResolverSet ? _xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), BaseURI));

            if (obj is Stream)
            {
                _cXml = new CanonicalXml((Stream)obj, _includeComments, resolver, BaseURI);
                return;
            }
            if (obj is XmlDocument)
            {
                _cXml = new CanonicalXml((XmlDocument)obj, resolver, _includeComments);
                return;
            }
            if (obj is XmlNodeList)
            {
                _cXml = new CanonicalXml((XmlNodeList)obj, resolver, _includeComments);
            }
            else
            {
                throw new ArgumentException(SR.Cryptography_Xml_IncorrectObjectType, nameof(obj));
            }
        }
Exemplo n.º 11
0
 /// <include file='doc\Transform.uex' path='docs/doc[@for="XmlDsigEnvelopedSignatureTransform.GetOutput1"]/*' />
 public override Object GetOutput(Type type)
 {
     if (type == typeof(XmlNodeList) || type.IsSubclassOf(typeof(XmlNodeList)))
     {
         if (_inputNodeList == null)
         {
             _inputNodeList = CanonicalXml.AllDescendantNodes(_containingDocument, true);
         }
         return((XmlNodeList)GetOutput());
     }
     else if (type == typeof(XmlDocument) || type.IsSubclassOf(typeof(XmlDocument)))
     {
         if (_inputNodeList != null)
         {
             throw new ArgumentException("type");
         }
         return((XmlDocument)GetOutput());
     }
     else
     {
         throw new ArgumentException("type");
     }
 }
 private void LoadXmlNodeListInput(XmlNodeList nodeList)
 {
     XmlResolver resolver = base.ResolverSet ? base.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), base.BaseURI);
     CanonicalXml xml = new CanonicalXml(nodeList, resolver, true);
     using (MemoryStream stream = new MemoryStream(xml.GetBytes()))
     {
         this._document = new XmlDocument();
         this._document.PreserveWhitespace = true;
         this._document.Load(stream);
     }
 }
Exemplo n.º 13
0
        // The goal behind this method is to pump the input stream through the transforms and get back something that
        // can be hashed
        internal Stream TransformToOctetStream(object inputObject, Type inputType, XmlResolver resolver, string baseUri)
        {
            object currentInput = inputObject;

            foreach (Transform transform in _transforms)
            {
                if (currentInput == null || transform.AcceptsType(currentInput.GetType()))
                {
                    //in this case, no translation necessary, pump it through
                    transform.Resolver = resolver;
                    transform.BaseURI  = baseUri;
                    transform.LoadInput(currentInput);
                    currentInput = transform.GetOutput();
                }
                else
                {
                    // We need translation
                    // For now, we just know about Stream->{XmlNodeList,XmlDocument} and {XmlNodeList,XmlDocument}->Stream
                    if (currentInput is Stream)
                    {
                        if (transform.AcceptsType(typeof(XmlDocument)))
                        {
                            Stream      currentInputStream = currentInput as Stream;
                            XmlDocument doc = new XmlDocument();
                            doc.PreserveWhitespace = true;
                            XmlReader valReader = Utils.PreProcessStreamInput(currentInputStream, resolver, baseUri);
                            doc.Load(valReader);
                            transform.LoadInput(doc);
                            currentInputStream.Close();
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType);
                        }
                    }
                    if (currentInput is XmlNodeList)
                    {
                        if (transform.AcceptsType(typeof(Stream)))
                        {
                            CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, resolver, false);
                            MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput(ms);
                            currentInput = transform.GetOutput();
                            ms.Close();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType);
                        }
                    }
                    if (currentInput is XmlDocument)
                    {
                        if (transform.AcceptsType(typeof(Stream)))
                        {
                            CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput, resolver);
                            MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput(ms);
                            currentInput = transform.GetOutput();
                            ms.Close();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType);
                        }
                    }
                    throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType);
                }
            }

            // Final processing, either we already have a stream or have to canonicalize
            if (currentInput is Stream)
            {
                return(currentInput as Stream);
            }
            if (currentInput is XmlNodeList)
            {
                CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, resolver, false);
                MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                return(ms);
            }
            if (currentInput is XmlDocument)
            {
                CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput, resolver);
                MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                return(ms);
            }
            throw new CryptographicException(SR.Cryptography_Xml_TransformIncorrectInputType);
        }
Exemplo n.º 14
0
 private void LoadXmlDocumentInput(XmlDocument doc)
 {
     _inputNodeList = CanonicalXml.AllDescendantNodes(doc, true);
 }
Exemplo n.º 15
0
 public override void LoadInput(Object obj) {
     if (_inputStream != null)
         _inputStream.Close();
     _inputStream = new MemoryStream();
     if (obj is Stream) {
         _inputStream = (Stream) obj;
     }
     else if (obj is XmlNodeList) {
         CanonicalXml xmlDoc = new CanonicalXml((XmlNodeList) obj, null, _includeComments);
         byte[] buffer = xmlDoc.GetBytes();
         if (buffer == null) return;
         _inputStream.Write(buffer, 0, buffer.Length);
         _inputStream.Flush();
         _inputStream.Position = 0;
     }
     else if (obj is XmlDocument) {
         CanonicalXml xmlDoc = new CanonicalXml((XmlDocument) obj, null, _includeComments);
         byte[] buffer = xmlDoc.GetBytes();
         if (buffer == null) return;
         _inputStream.Write(buffer, 0, buffer.Length);
         _inputStream.Flush();
         _inputStream.Position = 0;
     }
 }
Exemplo n.º 16
0
 private void LoadXmlNodeListInput(XmlNodeList nodeList) {
     // Use C14N to get a document
     XmlResolver resolver = (this.ResolverSet ? this.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), this.BaseURI));
     CanonicalXml c14n = new CanonicalXml((XmlNodeList) nodeList, resolver, true);
     using (MemoryStream ms = new MemoryStream(c14n.GetBytes())) {
         LoadStreamInput(ms);
     }
 }
Exemplo n.º 17
0
        // The goal behind this method is to pump the input stream through the transforms and get back something that
        // can be hashed
        internal Stream TransformToOctetStream(Object inputObject, Type inputType, XmlResolver resolver, string baseUri) {
            Object currentInput = inputObject;
            foreach (Transform transform in m_transforms) {
                if (currentInput == null || transform.AcceptsType(currentInput.GetType())) {
                    //in this case, no translation necessary, pump it through
                    transform.Resolver = resolver;
                    transform.BaseURI = baseUri;
                    transform.LoadInput(currentInput);
                    currentInput = transform.GetOutput();
                } else {
                    // We need translation 
                    // For now, we just know about Stream->{XmlNodeList,XmlDocument} and {XmlNodeList,XmlDocument}->Stream
                    if (currentInput is Stream) {
                        if (transform.AcceptsType(typeof(XmlDocument))) {
                            Stream currentInputStream = currentInput as Stream;
                            XmlDocument doc = new XmlDocument();
                            doc.PreserveWhitespace = true;
                            XmlReader valReader = Utils.PreProcessStreamInput(currentInputStream, resolver, baseUri);
                            doc.Load(valReader);
                            transform.LoadInput(doc);
                            currentInputStream.Close();
                            currentInput = transform.GetOutput();
                            continue;
                        } else {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    } 
                    if (currentInput is XmlNodeList) {
                        if (transform.AcceptsType(typeof(Stream))) {
                            CanonicalXml c14n = new CanonicalXml((XmlNodeList) currentInput, resolver, false);
                            MemoryStream ms = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput(ms);
                            currentInput = transform.GetOutput();
                            ms.Close();
                            continue;
                        } else {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    if (currentInput is XmlDocument) {
                        if (transform.AcceptsType(typeof(Stream))) {
                            CanonicalXml c14n = new CanonicalXml((XmlDocument) currentInput, resolver);
                            MemoryStream ms = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput(ms);
                            currentInput = transform.GetOutput();
                            ms.Close();
                            continue;
                        } else {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                }
            }

            // Final processing, either we already have a stream or have to canonicalize
            if (currentInput is Stream) {
                return currentInput as Stream;
            }
            if (currentInput is XmlNodeList) {
                CanonicalXml c14n = new CanonicalXml((XmlNodeList) currentInput, resolver, false);
                MemoryStream ms = new MemoryStream(c14n.GetBytes());
                return ms;
            }
            if (currentInput is XmlDocument) {
                CanonicalXml c14n = new CanonicalXml((XmlDocument) currentInput, resolver);
                MemoryStream ms = new MemoryStream(c14n.GetBytes());
                return ms;
            }
            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
        }
Exemplo n.º 18
0
        // The goal behind this method is to pump the input stream through the transforms and get back something that
        // can be hashed
        internal Stream TransformToOctetStream(Object inputObject, Type inputType, XmlResolver resolver, string strBaseUri)
        {
            Object currentInput = inputObject;

            foreach (Object obj in m_transforms)
            {
                Transform transform = obj as Transform;
                if (transform.AcceptsType(currentInput.GetType()))
                {
                    //in this case, no translation necessary, pump it through
                    transform.Resolver = resolver;
                    transform.BaseURI  = strBaseUri;
                    transform.LoadInput(currentInput);
                    currentInput = transform.GetOutput();
                }
                else
                {
                    // We need translation
                    // For now, we just know about Stream->{XmlNodeList,XmlDocument} and {XmlNodeList,XmlDocument}->Stream
                    if (currentInput is Stream)
                    {
                        if (transform.AcceptsType(typeof(XmlDocument)))
                        {
                            XmlDocument doc = new XmlDocument();
                            doc.PreserveWhitespace = true;
                            XmlValidatingReader valReader = SignedXml.PreProcessStreamInput((Stream)currentInput, resolver, strBaseUri);
                            doc.Load(valReader);
                            transform.LoadInput(doc);
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    if (currentInput is XmlNodeList)
                    {
                        if (transform.AcceptsType(typeof(Stream)))
                        {
                            CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, false);
                            MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput((Stream)ms);
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    if (currentInput is XmlDocument)
                    {
                        if (transform.AcceptsType(typeof(Stream)))
                        {
                            CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput);
                            MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                            transform.LoadInput((Stream)ms);
                            currentInput = transform.GetOutput();
                            continue;
                        }
                        else
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                        }
                    }
                    throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                }
            }

            // Final processing, either we already have a stream or have to canonicalize
            if (currentInput is Stream)
            {
                return(currentInput as Stream);
            }
            if (currentInput is XmlNodeList)
            {
                CanonicalXml c14n = new CanonicalXml((XmlNodeList)currentInput, false);
                MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                return(ms);
            }
            if (currentInput is XmlDocument)
            {
                CanonicalXml c14n = new CanonicalXml((XmlDocument)currentInput);
                MemoryStream ms   = new MemoryStream(c14n.GetBytes());
                return(ms);
            }
            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
        }
 internal Stream TransformToOctetStream(object inputObject, Type inputType, XmlResolver resolver, string baseUri)
 {
     object output = inputObject;
     foreach (Transform transform in this.m_transforms)
     {
         if ((output == null) || transform.AcceptsType(output.GetType()))
         {
             transform.Resolver = resolver;
             transform.BaseURI = baseUri;
             transform.LoadInput(output);
             output = transform.GetOutput();
         }
         else if (output is Stream)
         {
             if (!transform.AcceptsType(typeof(XmlDocument)))
             {
                 throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
             }
             Stream inputStream = output as Stream;
             XmlDocument document = new XmlDocument {
                 PreserveWhitespace = true
             };
             XmlReader reader = System.Security.Cryptography.Xml.Utils.PreProcessStreamInput(inputStream, resolver, baseUri);
             document.Load(reader);
             transform.LoadInput(document);
             inputStream.Close();
             output = transform.GetOutput();
         }
         else if (output is XmlNodeList)
         {
             if (!transform.AcceptsType(typeof(Stream)))
             {
                 throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
             }
             CanonicalXml xml = new CanonicalXml((XmlNodeList) output, resolver, false);
             MemoryStream stream2 = new MemoryStream(xml.GetBytes());
             transform.LoadInput(stream2);
             output = transform.GetOutput();
             stream2.Close();
         }
         else
         {
             if (!(output is XmlDocument))
             {
                 throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
             }
             if (!transform.AcceptsType(typeof(Stream)))
             {
                 throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
             }
             CanonicalXml xml2 = new CanonicalXml((XmlDocument) output, resolver);
             MemoryStream stream3 = new MemoryStream(xml2.GetBytes());
             transform.LoadInput(stream3);
             output = transform.GetOutput();
             stream3.Close();
         }
     }
     if (output is Stream)
     {
         return (output as Stream);
     }
     if (output is XmlNodeList)
     {
         CanonicalXml xml3 = new CanonicalXml((XmlNodeList) output, resolver, false);
         return new MemoryStream(xml3.GetBytes());
     }
     if (!(output is XmlDocument))
     {
         throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
     }
     CanonicalXml xml4 = new CanonicalXml((XmlDocument) output, resolver);
     return new MemoryStream(xml4.GetBytes());
 }
 public override void LoadInput(object obj)
 {
     if (this._inputStream != null)
     {
         this._inputStream.Close();
     }
     this._inputStream = new MemoryStream();
     if (obj is Stream)
     {
         this._inputStream = (Stream) obj;
     }
     else if (obj is XmlNodeList)
     {
         byte[] bytes = new CanonicalXml((XmlNodeList) obj, null, this._includeComments).GetBytes();
         if (bytes != null)
         {
             this._inputStream.Write(bytes, 0, bytes.Length);
             this._inputStream.Flush();
             this._inputStream.Position = 0L;
         }
     }
     else if (obj is XmlDocument)
     {
         byte[] buffer = new CanonicalXml((XmlDocument) obj, null, this._includeComments).GetBytes();
         if (buffer != null)
         {
             this._inputStream.Write(buffer, 0, buffer.Length);
             this._inputStream.Flush();
             this._inputStream.Position = 0L;
         }
     }
 }
Exemplo n.º 21
0
        internal Stream TransformToOctetStream(object inputObject, Type inputType, XmlResolver resolver, string baseUri)
        {
            object output = inputObject;

            foreach (Transform transform in this.m_transforms)
            {
                if ((output == null) || transform.AcceptsType(output.GetType()))
                {
                    transform.Resolver = resolver;
                    transform.BaseURI  = baseUri;
                    transform.LoadInput(output);
                    output = transform.GetOutput();
                }
                else if (output is Stream)
                {
                    if (!transform.AcceptsType(typeof(XmlDocument)))
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                    }
                    Stream      inputStream = output as Stream;
                    XmlDocument document    = new XmlDocument {
                        PreserveWhitespace = true
                    };
                    XmlReader reader = System.Security.Cryptography.Xml.Utils.PreProcessStreamInput(inputStream, resolver, baseUri);
                    document.Load(reader);
                    transform.LoadInput(document);
                    inputStream.Close();
                    output = transform.GetOutput();
                }
                else if (output is XmlNodeList)
                {
                    if (!transform.AcceptsType(typeof(Stream)))
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                    }
                    CanonicalXml xml     = new CanonicalXml((XmlNodeList)output, resolver, false);
                    MemoryStream stream2 = new MemoryStream(xml.GetBytes());
                    transform.LoadInput(stream2);
                    output = transform.GetOutput();
                    stream2.Close();
                }
                else
                {
                    if (!(output is XmlDocument))
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                    }
                    if (!transform.AcceptsType(typeof(Stream)))
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
                    }
                    CanonicalXml xml2    = new CanonicalXml((XmlDocument)output, resolver);
                    MemoryStream stream3 = new MemoryStream(xml2.GetBytes());
                    transform.LoadInput(stream3);
                    output = transform.GetOutput();
                    stream3.Close();
                }
            }
            if (output is Stream)
            {
                return(output as Stream);
            }
            if (output is XmlNodeList)
            {
                CanonicalXml xml3 = new CanonicalXml((XmlNodeList)output, resolver, false);
                return(new MemoryStream(xml3.GetBytes()));
            }
            if (!(output is XmlDocument))
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_TransformIncorrectInputType"));
            }
            CanonicalXml xml4 = new CanonicalXml((XmlDocument)output, resolver);

            return(new MemoryStream(xml4.GetBytes()));
        }
Exemplo n.º 22
0
        // What we want to do is pump the input throug the TransformChain and then
        // hash the output of the chain
        // document is the document context for resolving relative references
        internal byte[] CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList)
        {
            // refList is a list of elements that might be targets of references
            // Now's the time to create our hashing algorithm
            m_hashAlgorithm = (HashAlgorithm)CryptoConfig.CreateFromName(DigestMethod);
            if (m_hashAlgorithm == null)
            {
                throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_CreateHashAlgorithmFailed"));
            }

            string strBaseUri = (document == null ? null : document.BaseURI);
            // For interop w/ Petteri & IBM -- may not be required by the spec
            //if (m_transforms.Count == 0) {
            //if (DataObject != null && DataObject.Data != null) {
            //AddTransform(new W3cCanonicalization());
            //}
            //}

            // Let's go get the target.
            Stream      hashInputStream;
            WebRequest  theRequest  = null;
            WebResponse theResponse = null;
            Stream      inputStream = null;
            XmlResolver resolver    = null;

            switch (m_refTargetType)
            {
            case ReferenceTargetType.Stream:
                // This is the easiest case.  We already have a stream, so just pump it through
                // the TransformChain
                resolver        = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri));
                hashInputStream = m_transformChain.TransformToOctetStream((Stream)m_refTarget, resolver, strBaseUri);
                break;

            case ReferenceTargetType.UriReference:
                // Second-easiest case -- dereference the URI & pump through the TransformChain
                // handle the special cases where the URI is null (meaning whole doc)
                // or the URI is just a fragment (meaning a reference to an embedded Object)
                if (m_strUri == "")
                {
                    // This is the self-referential case.
                    // The Enveloped Signature does not discard comments as per spec; those will be omitted during the transform chain process
                    // First, check that we have a document context.
                    if (document == null)
                    {
                        throw new CryptographicException(String.Format(SecurityResources.GetResourceString("Cryptography_Xml_SelfReferenceRequiresContext"), m_strUri));
                    }

                    // Normalize the containing document
                    resolver = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri));
                    XmlDocument docWithNoComments = CanonicalXml.DiscardComments(SignedXml.PreProcessDocumentInput(document, resolver, strBaseUri));
                    hashInputStream = m_transformChain.TransformToOctetStream(docWithNoComments, resolver, strBaseUri);
                }
                else if (m_strUri[0] == '#')
                {
                    // If we get here, then we are constructing a Reference to an embedded DataObject
                    // referenced by an Id= attribute.  Go find the relevant object
                    String idref            = m_strUri.Substring(1);
                    bool   bDiscardComments = true;

                    // Deal with XPointer of types #xpointer(/) and #xpointer(id("ID")). Other XPointer support isn't handled here and is anyway optional
                    if (idref == "xpointer(/)")
                    {
                        // This is a self referencial case
                        if (document == null)
                        {
                            throw new CryptographicException(String.Format(SecurityResources.GetResourceString("Cryptography_Xml_SelfReferenceRequiresContext"), m_strUri));
                        }

                        // We should not discard comments here!!!
                        resolver        = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri));
                        hashInputStream = m_transformChain.TransformToOctetStream(SignedXml.PreProcessDocumentInput(document, resolver, strBaseUri), resolver, strBaseUri);
                        goto end;
                    }
                    else if (idref.StartsWith("xpointer(id("))
                    {
                        int startId = idref.IndexOf("id(");
                        int endId   = idref.IndexOf(")");
                        if (endId < 0 || endId < startId + 3)
                        {
                            throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidReference"));
                        }
                        idref            = idref.Substring(startId + 3, endId - startId - 3);
                        idref            = idref.Replace("\'", "");
                        idref            = idref.Replace("\"", "");
                        bDiscardComments = false;
                    }

                    XmlElement elem = m_signedXml.GetIdElement(document, idref);

                    if (elem == null)
                    {
                        // Go throw the referenced items passed in
                        if (refList != null)
                        {
                            foreach (XmlNode node in refList)
                            {
                                XmlElement tempElem = node as XmlElement;
                                if ((tempElem != null) && (tempElem.HasAttribute("Id")) && (tempElem.GetAttribute("Id").Equals(idref)))
                                {
                                    elem = tempElem;
                                    break;
                                }
                            }
                        }
                    }

                    if (elem == null)
                    {
                        throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidReference"));
                    }

                    // Add the propagated attributes, clone the element first
                    XmlElement elemClone = elem.Clone() as XmlElement;
                    if (m_namespaces != null)
                    {
                        foreach (XmlNode attrib in m_namespaces)
                        {
                            string name = ((attrib.Prefix != String.Empty) ? attrib.Prefix + ":" + attrib.LocalName : attrib.LocalName);
                            // Skip the attribute if one with the same qualified name already exists
                            if (elemClone.HasAttribute(name) || (name.Equals("xmlns") && elemClone.NamespaceURI != String.Empty))
                            {
                                continue;
                            }
                            XmlAttribute nsattrib = (XmlAttribute)elemClone.OwnerDocument.CreateAttribute(name);
                            nsattrib.Value = attrib.Value;
                            elemClone.SetAttributeNode(nsattrib);
                        }
                    }

                    if (bDiscardComments)
                    {
                        // We should discard comments before going into the transform chain
                        resolver = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri));
                        XmlDocument docWithNoComments = CanonicalXml.DiscardComments(SignedXml.PreProcessElementInput(elemClone, resolver, strBaseUri));
                        hashInputStream = m_transformChain.TransformToOctetStream(docWithNoComments, resolver, strBaseUri);
                    }
                    else
                    {
                        // This is an XPointer reference, do not discard comments!!!
                        resolver        = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri));
                        hashInputStream = m_transformChain.TransformToOctetStream(SignedXml.PreProcessElementInput(elemClone, resolver, strBaseUri), resolver, strBaseUri);
                    }
                }
                else
                {
                    theRequest = WebRequest.Create(m_strUri);
                    if (theRequest == null)
                    {
                        goto default;
                    }
                    theResponse = theRequest.GetResponse();
                    if (theResponse == null)
                    {
                        goto default;
                    }
                    inputStream = theResponse.GetResponseStream();
                    if (inputStream == null)
                    {
                        goto default;
                    }
                    resolver        = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), m_strUri));
                    hashInputStream = m_transformChain.TransformToOctetStream(inputStream, resolver, m_strUri);
                }
                break;

            case ReferenceTargetType.XmlElement:
                // We need to create a DocumentNavigator out of the XmlElement
                resolver        = (m_signedXml.ResolverSet ? m_signedXml.m_xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), strBaseUri));
                hashInputStream = m_transformChain.TransformToOctetStream(SignedXml.PreProcessElementInput((XmlElement)m_refTarget, resolver, strBaseUri), resolver, strBaseUri);
                break;

            default:
                throw new CryptographicException();
            }

end:
            // Compute the new hash value
            byte[] hashval = m_hashAlgorithm.ComputeHash(hashInputStream);

            // Close the response to free resources, before returning
            if (theResponse != null)
            {
                theResponse.Close();
            }
            if (inputStream != null)
            {
                inputStream.Close();
            }

            return(hashval);
        }
 public override void LoadInput(XmlDocument obj)
 {
     _cXml = new CanonicalXml(obj, _includeComments);
 }