public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("enc", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]);

            XmlElement encryptionMethodElement = value;

            var algorithmUrl = ElementUtils.GetAttribute(encryptionMethodElement, "Algorithm", NS.XmlEncNamespaceUrl);

            _algorithm = XmlNameSpace.Url.FirstOrDefault(x => x.Value == algorithmUrl).Key;

            XmlNode keySizeNode = value.SelectSingleNode("enc:KeySize", nsm);

            if (keySizeNode != null)
            {
                KeySize = Convert.ToInt32(ParserUtils.DiscardWhiteSpaces(keySizeNode.InnerText), null);
            }

            _cachedXml = value;
        }
Пример #2
0
        private int LoadXml2(XmlElement signatureElement, XmlNamespaceManager nsm, int expectedChildNodes)
        {
            XmlNodeList signedInfoNodes = signatureElement.SelectNodes("ds:SignedInfo", nsm);

            if (signedInfoNodes == null || signedInfoNodes.Count == 0 || signedInfoNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo");
            }
            XmlElement signedInfoElement = signedInfoNodes[0] as XmlElement;

            expectedChildNodes += signedInfoNodes.Count;

            XmlNodeList signatureValueNodes = signatureElement.SelectNodes("ds:SignatureValue", nsm);

            if (signatureValueNodes == null || signatureValueNodes.Count == 0 || signatureValueNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignatureValue");
            }
            XmlElement signatureValueElement = signatureValueNodes[0] as XmlElement;

            expectedChildNodes += signatureValueNodes.Count;
            _signatureValue     = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(signatureValueElement.InnerText));
            _signatureValueId   = ElementUtils.GetAttribute(signatureValueElement, "Id", NS.XmlDsigNamespaceUrl);
            if (!ElementUtils.VerifyAttributes(signatureValueElement, "Id"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignatureValue");
            }

            SignedInfo = new SignedInfo();
            SignedInfo.LoadXml(signedInfoElement);

            return(expectedChildNodes);
        }
Пример #3
0
        internal void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("ds", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]);

            XmlNodeList transformNodes = value.SelectNodes("ds:Transform", nsm);

            if (transformNodes.Count == 0)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Transforms");
            }

            _transforms.Clear();
            for (int i = 0; i < transformNodes.Count; ++i)
            {
                XmlElement transformElement = (XmlElement)transformNodes.Item(i);
                string     algorithm        = ElementUtils.GetAttribute(transformElement, "Algorithm", NS.XmlDsigNamespaceUrl);
                Transform  transform        = CryptoHelpers.CreateFromName <Transform>(algorithm);
                if (transform == null)
                {
                    throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
                }
                transform.LoadInnerXml(transformElement.ChildNodes);
                _transforms.Add(transform);
            }
        }
Пример #4
0
 public override void LoadInnerXml(XmlNodeList nodeList)
 {
     if (nodeList != null)
     {
         foreach (XmlNode n in nodeList)
         {
             XmlElement e = n as XmlElement;
             if (e != null)
             {
                 if (e.LocalName.Equals("InclusiveNamespaces") &&
                     e.NamespaceURI.Equals(NS.XmlDsigExcC14NTransformUrl) &&
                     ElementUtils.HasAttribute(e, "PrefixList", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]))
                 {
                     if (!ElementUtils.VerifyAttributes(e, "PrefixList"))
                     {
                         throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
                     }
                     this.InclusiveNamespacesPrefixList = ElementUtils.GetAttribute(e, "PrefixList", NS.XmlDsigNamespaceUrl);
                     return;
                 }
                 else
                 {
                     throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
                 }
             }
         }
     }
 }
        public virtual void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            ReferenceType = value.LocalName;

            string uri = ElementUtils.GetAttribute(value, "URI", NS.XmlEncNamespaceUrl);

            if (uri == null)
            {
                throw new ArgumentNullException(SR.Cryptography_Xml_UriRequired);
            }
            Uri = uri;

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("ds", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]);
            XmlNode transformsNode = value.SelectSingleNode("ds:Transforms", nsm);

            if (transformsNode != null)
            {
                TransformChain.LoadXml(transformsNode as XmlElement);
            }

            _cachedXml = value;
        }
Пример #6
0
 public override void LoadInnerXml(XmlNodeList nodeList)
 {
     if (nodeList == null)
     {
         throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
     }
     ExceptUris.Clear();
     foreach (XmlNode node in nodeList)
     {
         XmlElement elem = node as XmlElement;
         if (elem != null)
         {
             if (elem.LocalName == "Except" && elem.NamespaceURI == XmlNameSpace.Url[NS.XmlDecryptionTransformNamespaceUrl])
             {
                 string uri = ElementUtils.GetAttribute(elem, "URI", NS.XmlDecryptionTransformNamespaceUrl);
                 if (uri == null || uri.Length == 0 || uri[0] != '#')
                 {
                     throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UriRequired);
                 }
                 if (!ElementUtils.VerifyAttributes(elem, "URI"))
                 {
                     throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
                 }
                 string idref = ParserUtils.ExtractIdFromLocalUri(uri);
                 ExceptUris.Add(idref);
             }
             else
             {
                 throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
             }
         }
     }
 }
 public override void LoadXml(XmlElement element)
 {
     if (element == null)
     {
         throw new ArgumentNullException(nameof(element));
     }
     _uri  = ElementUtils.GetAttribute(element, "URI", NS.XmlDsigNamespaceUrl);
     _type = ElementUtils.GetAttribute(element, "Type", NS.XmlDsigNamespaceUrl);
 }
Пример #8
0
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XmlElement keyInfoElement = value;

            _id = ElementUtils.GetAttribute(keyInfoElement, "Id", NS.XmlDsigNamespaceUrl);
            if (!ElementUtils.VerifyAttributes(keyInfoElement, "Id"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "KeyInfo");
            }

            XmlNode child = keyInfoElement.FirstChild;

            while (child != null)
            {
                XmlElement elem = child as XmlElement;
                if (elem != null)
                {
                    string kicString = elem.NamespaceURI + " " + elem.LocalName;
                    if (kicString == "http://www.w3.org/2000/09/xmldsig# KeyValue")
                    {
                        if (!ElementUtils.VerifyAttributes(elem, (string[])null))
                        {
                            throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "KeyInfo/KeyValue");
                        }
                        XmlNodeList nodeList2 = elem.ChildNodes;
                        foreach (XmlNode node2 in nodeList2)
                        {
                            XmlElement elem2 = node2 as XmlElement;
                            if (elem2 != null)
                            {
                                kicString += "/" + elem2.LocalName;
                                break;
                            }
                        }
                    }

                    KeyInfoClause keyInfoClause = CryptoHelpers.CreateFromName <KeyInfoClause>(kicString);
                    if (keyInfoClause == null)
                    {
                        keyInfoClause = new KeyInfoNode();
                    }

                    keyInfoClause.LoadXml(elem);
                    AddClause(keyInfoClause);
                }
                child = child.NextSibling;
            }
        }
Пример #9
0
        private void LoadXml1(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            _id   = ElementUtils.GetAttribute(value, "Id", NS.XmlDsigNamespaceUrl);
            _uri  = ElementUtils.GetAttribute(value, "URI", NS.XmlDsigNamespaceUrl);
            _type = ElementUtils.GetAttribute(value, "Type", NS.XmlDsigNamespaceUrl);
            if (!ElementUtils.VerifyAttributes(value, new string[] { "Id", "URI", "Type" }))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference");
            }
        }
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            if (value.LocalName != "EncryptionProperty" || value.NamespaceURI != XmlNameSpace.Url[NS.XmlEncNamespaceUrl])
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidEncryptionProperty);
            }

            _cachedXml = value;
            _id        = ElementUtils.GetAttribute(value, "Id", NS.XmlEncNamespaceUrl);
            _target    = ElementUtils.GetAttribute(value, "Target", NS.XmlEncNamespaceUrl);
            _elemProp  = value;
        }
Пример #11
0
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            _id       = ElementUtils.GetAttribute(value, "Id", NS.XmlDsigNamespaceUrl);
            _mimeType = ElementUtils.GetAttribute(value, "MimeType", NS.XmlDsigNamespaceUrl);
            _encoding = ElementUtils.GetAttribute(value, "Encoding", NS.XmlDsigNamespaceUrl);

            foreach (XmlNode node in value.ChildNodes)
            {
                _elData.Add(node);
            }

            _cachedXml = value;
        }
Пример #12
0
        public (XmlElement, int) LoadXml2(XmlElement signedInfoElement, int expectedChildNodes, XmlNamespaceManager nsm)
        {
            XmlNodeList canonicalizationMethodNodes = signedInfoElement.SelectNodes("ds:CanonicalizationMethod", nsm);

            if (canonicalizationMethodNodes == null || canonicalizationMethodNodes.Count == 0 || canonicalizationMethodNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/CanonicalizationMethod");
            }
            XmlElement canonicalizationMethodElement = canonicalizationMethodNodes.Item(0) as XmlElement;

            expectedChildNodes     += canonicalizationMethodNodes.Count;
            _canonicalizationMethod = ElementUtils.GetAttribute(canonicalizationMethodElement, "Algorithm", NS.XmlDsigNamespaceUrl);
            if (_canonicalizationMethod == null || !ElementUtils.VerifyAttributes(canonicalizationMethodElement, "Algorithm"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/CanonicalizationMethod");
            }
            _canonicalizationMethodTransform = null;
            if (canonicalizationMethodElement.ChildNodes.Count > 0)
            {
                CanonicalizationMethodObject.LoadInnerXml(canonicalizationMethodElement.ChildNodes);
            }

            XmlNodeList signatureMethodNodes = signedInfoElement.SelectNodes("ds:SignatureMethod", nsm);

            if (signatureMethodNodes == null || signatureMethodNodes.Count == 0 || signatureMethodNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/SignatureMethod");
            }
            XmlElement signatureMethodElement = signatureMethodNodes.Item(0) as XmlElement;

            expectedChildNodes += signatureMethodNodes.Count;
            _signatureMethod    = ElementUtils.GetAttribute(signatureMethodElement, "Algorithm", NS.XmlDsigNamespaceUrl);
            if (_signatureMethod == null || !ElementUtils.VerifyAttributes(signatureMethodElement, "Algorithm"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "SignedInfo/SignatureMethod");
            }

            return(signatureMethodElement.SelectSingleNode("ds:HMACOutputLength", nsm) as XmlElement, expectedChildNodes);
        }
Пример #13
0
        private void LoadXml2(XmlElement value, XmlNamespaceManager nsm, bool hasTransforms)
        {
            XmlNodeList digestMethodNodes = value.SelectNodes("ds:DigestMethod", nsm);

            if (digestMethodNodes == null || digestMethodNodes.Count == 0 || digestMethodNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod");
            }
            XmlElement digestMethodElement = digestMethodNodes[0] as XmlElement;

            _digestMethod = ElementUtils.GetAttribute(digestMethodElement, "Algorithm", NS.XmlDsigNamespaceUrl);
            if (_digestMethod == null || !ElementUtils.VerifyAttributes(digestMethodElement, "Algorithm"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestMethod");
            }


            XmlNodeList digestValueNodes = value.SelectNodes("ds:DigestValue", nsm);

            if (digestValueNodes == null || digestValueNodes.Count == 0 || digestValueNodes.Count > 1)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue");
            }
            XmlElement digestValueElement = digestValueNodes[0] as XmlElement;

            _digestValue = Convert.FromBase64String(ParserUtils.DiscardWhiteSpaces(digestValueElement.InnerText));
            if (!ElementUtils.VerifyAttributes(digestValueElement, (string[])null))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/DigestValue");
            }
            int expectedChildNodeCount = hasTransforms ? 3 : 2;

            if (value.SelectNodes("*").Count != expectedChildNodeCount)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference");
            }

            _cachedXml = value;
        }
Пример #14
0
        private XmlElement LoadXml1(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XmlElement signatureElement = value;

            if (!signatureElement.LocalName.Equals("Signature"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Signature");
            }

            _id = ElementUtils.GetAttribute(signatureElement, "Id", NS.XmlDsigNamespaceUrl);
            if (!ElementUtils.VerifyAttributes(signatureElement, "Id"))
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Signature");
            }

            return(signatureElement);
        }
Пример #15
0
        public override void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("enc", XmlNameSpace.Url[NS.XmlEncNamespaceUrl]);
            nsm.AddNamespace("ds", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]);

            Id        = ElementUtils.GetAttribute(value, "Id", NS.XmlEncNamespaceUrl);
            Type      = ElementUtils.GetAttribute(value, "Type", NS.XmlEncNamespaceUrl);
            MimeType  = ElementUtils.GetAttribute(value, "MimeType", NS.XmlEncNamespaceUrl);
            Encoding  = ElementUtils.GetAttribute(value, "Encoding", NS.XmlEncNamespaceUrl);
            Recipient = ElementUtils.GetAttribute(value, "Recipient", NS.XmlEncNamespaceUrl);

            XmlNode encryptionMethodNode = value.SelectSingleNode("enc:EncryptionMethod", nsm);

            EncryptionMethod = new EncryptionMethod();
            if (encryptionMethodNode != null)
            {
                EncryptionMethod.LoadXml(encryptionMethodNode as XmlElement);
            }

            KeyInfo = new KeyInfo();
            XmlNode keyInfoNode = value.SelectSingleNode("ds:KeyInfo", nsm);

            if (keyInfoNode != null)
            {
                KeyInfo.LoadXml(keyInfoNode as XmlElement);
            }

            XmlNode cipherDataNode = value.SelectSingleNode("enc:CipherData", nsm);

            if (cipherDataNode == null)
            {
                throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_MissingCipherData);
            }

            CipherData = new CipherData();
            CipherData.LoadXml(cipherDataNode as XmlElement);

            XmlNode encryptionPropertiesNode = value.SelectSingleNode("enc:EncryptionProperties", nsm);

            if (encryptionPropertiesNode != null)
            {
                XmlNodeList encryptionPropertyNodes = encryptionPropertiesNode.SelectNodes("enc:EncryptionProperty", nsm);
                if (encryptionPropertyNodes != null)
                {
                    foreach (XmlNode node in encryptionPropertyNodes)
                    {
                        EncryptionProperty ep = new EncryptionProperty();
                        ep.LoadXml(node as XmlElement);
                        EncryptionProperties.Add(ep);
                    }
                }
            }

            XmlNode carriedKeyNameNode = value.SelectSingleNode("enc:CarriedKeyName", nsm);

            if (carriedKeyNameNode != null)
            {
                CarriedKeyName = carriedKeyNameNode.InnerText;
            }

            XmlNode referenceListNode = value.SelectSingleNode("enc:ReferenceList", nsm);

            if (referenceListNode != null)
            {
                XmlNodeList dataReferenceNodes = referenceListNode.SelectNodes("enc:DataReference", nsm);
                if (dataReferenceNodes != null)
                {
                    foreach (XmlNode node in dataReferenceNodes)
                    {
                        DataReference dr = new DataReference();
                        dr.LoadXml(node as XmlElement);
                        ReferenceList.Add(dr);
                    }
                }
                XmlNodeList keyReferenceNodes = referenceListNode.SelectNodes("enc:KeyReference", nsm);
                if (keyReferenceNodes != null)
                {
                    foreach (XmlNode node in keyReferenceNodes)
                    {
                        KeyReference kr = new KeyReference();
                        kr.LoadXml(node as XmlElement);
                        ReferenceList.Add(kr);
                    }
                }
            }

            _cachedXml = value;
        }
Пример #16
0
        public void LoadXml(XmlElement value)
        {
            LoadXml1(value);

            XmlNamespaceManager nsm = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsm.AddNamespace("ds", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl]);

            bool hasTransforms = false;

            TransformChain = new TransformChain();
            XmlNodeList transformsNodes = value.SelectNodes("ds:Transforms", nsm);

            if (transformsNodes != null && transformsNodes.Count != 0)
            {
                if (transformsNodes.Count > 1)
                {
                    throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms");
                }
                hasTransforms = true;
                XmlElement transformsElement = transformsNodes[0] as XmlElement;
                if (!ElementUtils.VerifyAttributes(transformsElement, (string[])null))
                {
                    throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms");
                }

                XmlNodeList transformNodes = transformsElement.SelectNodes("ds:Transform", nsm);
                if (transformNodes != null)
                {
                    if (transformNodes.Count != transformsElement.SelectNodes("*").Count)
                    {
                        throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms");
                    }
                    if (transformNodes.Count > CryptoUtils.MaxTransformsPerReference)
                    {
                        throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidElement, "Reference/Transforms");
                    }
                    foreach (XmlNode transformNode in transformNodes)
                    {
                        XmlElement transformElement = transformNode as XmlElement;
                        string     algorithm        = ElementUtils.GetAttribute(transformElement, "Algorithm", NS.XmlDsigNamespaceUrl);
                        if (algorithm == null || !ElementUtils.VerifyAttributes(transformElement, "Algorithm"))
                        {
                            throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
                        }
                        Transform transform = CryptoHelpers.CreateFromName <Transform>(algorithm);
                        if (transform == null)
                        {
                            throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UnknownTransform);
                        }
                        AddTransform(transform);
                        transform.LoadInnerXml(transformElement.ChildNodes);
                        if (transform is XmlDsigEnvelopedSignatureTransform)
                        {
                            XmlNode     signatureTag  = transformElement.SelectSingleNode("ancestor::ds:Signature[1]", nsm);
                            XmlNodeList signatureList = transformElement.SelectNodes("//ds:Signature", nsm);
                            if (signatureList != null)
                            {
                                int position = 0;
                                foreach (XmlNode node in signatureList)
                                {
                                    position++;
                                    if (node == signatureTag)
                                    {
                                        ((XmlDsigEnvelopedSignatureTransform)transform).SignaturePosition = position;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            LoadXml2(value, nsm, hasTransforms);
        }
Пример #17
0
        internal byte[] CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList)
        {
            IDigest digest = CryptoHelpers.CreateFromName <IDigest>(_digestMethod);

            if (digest == null)
            {
                IMac mac = CryptoHelpers.CreateFromName <IMac>(_digestMethod);
                if (mac == null)
                {
                    throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_CreateHashAlgorithmFailed);
                }

                byte[] randomKey = CryptoUtils.GenerateRandomBlock(mac.GetMacSize());
                mac.Init(new KeyParameter(randomKey));

                _hashAlgorithm = new MacHashWrapper(mac);
            }
            else
            {
                _hashAlgorithm = new DigestHashWrapper(digest);
            }

            string      baseUri         = document == null ? Environment.CurrentDirectory + "\\" : document.BaseURI;
            Stream      hashInputStream = null;
            WebResponse response        = null;
            Stream      inputStream     = null;
            XmlResolver resolver        = null;

            _hashval = null;

            try
            {
                switch (_refTargetType)
                {
                case ReferenceTargetType.Stream:
                    resolver        = (GetSignedXml().ResolverSet ? GetSignedXml()._xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), baseUri));
                    hashInputStream = TransformChain.TransformToOctetStream((Stream)_refTarget, resolver, baseUri);
                    break;

                case ReferenceTargetType.UriReference:
                    if (_uri == null)
                    {
                        resolver        = (GetSignedXml().ResolverSet ? GetSignedXml()._xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), baseUri));
                        hashInputStream = TransformChain.TransformToOctetStream((Stream)null, resolver, baseUri);
                    }
                    else if (_uri.Length == 0)
                    {
                        if (document == null)
                        {
                            throw new System.Security.Cryptography.CryptographicException(string.Format(CultureInfo.CurrentCulture, SR.Cryptography_Xml_SelfReferenceRequiresContext, _uri));
                        }

                        resolver = (GetSignedXml().ResolverSet ? GetSignedXml()._xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), baseUri));
                        XmlDocument docWithNoComments = StreamUtils.DiscardComments(StreamUtils.PreProcessDocumentInput(document, resolver, baseUri));
                        hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri);
                    }
                    else if (_uri[0] == '#')
                    {
                        bool   discardComments = true;
                        string idref           = ParserUtils.GetIdFromLocalUri(_uri, out discardComments);
                        if (idref == "xpointer(/)")
                        {
                            if (document == null)
                            {
                                throw new System.Security.Cryptography.CryptographicException(string.Format(CultureInfo.CurrentCulture, SR.Cryptography_Xml_SelfReferenceRequiresContext, _uri));
                            }

                            resolver        = (GetSignedXml().ResolverSet ? GetSignedXml()._xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), baseUri));
                            hashInputStream = TransformChain.TransformToOctetStream(StreamUtils.PreProcessDocumentInput(document, resolver, baseUri), resolver, baseUri);
                            break;
                        }

                        XmlElement elem = GetSignedXml().GetIdElement(document, idref);
                        if (elem != null)
                        {
                            _namespaces = ElementUtils.GetPropagatedAttributes(elem.ParentNode as XmlElement);
                        }

                        if (elem == null && refList != null)
                        {
                            foreach (XmlNode node in refList)
                            {
                                XmlElement tempElem = node as XmlElement;
                                if ((tempElem != null) && (ElementUtils.HasAttribute(tempElem, "Id", XmlNameSpace.Url[NS.XmlDsigNamespaceUrl])) &&
                                    (ElementUtils.GetAttribute(tempElem, "Id", NS.XmlDsigNamespaceUrl).Equals(idref)))
                                {
                                    elem = tempElem;
                                    if (_signedXml._context != null)
                                    {
                                        _namespaces = ElementUtils.GetPropagatedAttributes(_signedXml._context);
                                    }
                                    break;
                                }
                            }
                        }

                        if (elem == null)
                        {
                            throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_InvalidReference);
                        }

                        XmlDocument normDocument = StreamUtils.PreProcessElementInput(elem, resolver, baseUri);
                        ElementUtils.AddNamespaces(normDocument.DocumentElement, _namespaces);

                        resolver = (GetSignedXml().ResolverSet ? GetSignedXml()._xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), baseUri));
                        if (discardComments)
                        {
                            XmlDocument docWithNoComments = StreamUtils.DiscardComments(normDocument);
                            hashInputStream = TransformChain.TransformToOctetStream(docWithNoComments, resolver, baseUri);
                        }
                        else
                        {
                            hashInputStream = TransformChain.TransformToOctetStream(normDocument, resolver, baseUri);
                        }
                    }
                    else
                    {
                        throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UriNotResolved, _uri);
                    }
                    break;

                case ReferenceTargetType.XmlElement:
                    resolver        = (GetSignedXml().ResolverSet ? GetSignedXml()._xmlResolver : new XmlSecureResolver(new XmlUrlResolver(), baseUri));
                    hashInputStream = TransformChain.TransformToOctetStream(StreamUtils.PreProcessElementInput((XmlElement)_refTarget, resolver, baseUri), resolver, baseUri);
                    break;

                default:
                    throw new System.Security.Cryptography.CryptographicException(SR.Cryptography_Xml_UriNotResolved, _uri);
                }

                hashInputStream = SignedXmlDebugLog.LogReferenceData(this, hashInputStream);
                byte[] buffer = new byte[4096];
                int    bytesRead;
                _hashAlgorithm.Reset();
                while ((bytesRead = hashInputStream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    _hashAlgorithm.BlockUpdate(buffer, 0, bytesRead);
                }
                _hashval = new byte[_hashAlgorithm.GetHashSize()];
                _hashAlgorithm.DoFinal(_hashval, 0);
            }
            finally
            {
                if (hashInputStream != null)
                {
                    hashInputStream.Close();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (inputStream != null)
                {
                    inputStream.Close();
                }
            }

            return(_hashval);
        }