internal static XmlNodeList AllDescendantNodes(XmlNode node, bool includeComments)
        {
            CanonicalXmlNodeList canonicalXmlNodeList1 = new CanonicalXmlNodeList();
            CanonicalXmlNodeList canonicalXmlNodeList2 = new CanonicalXmlNodeList();
            CanonicalXmlNodeList canonicalXmlNodeList3 = new CanonicalXmlNodeList();
            CanonicalXmlNodeList canonicalXmlNodeList4 = new CanonicalXmlNodeList();
            int index = 0;

            canonicalXmlNodeList2.Add((object)node);
            do
            {
                XmlNode     xmlNode1   = canonicalXmlNodeList2[index];
                XmlNodeList childNodes = xmlNode1.ChildNodes;
                if (childNodes != null)
                {
                    foreach (XmlNode xmlNode2 in childNodes)
                    {
                        if (includeComments || !(xmlNode2 is XmlComment))
                        {
                            canonicalXmlNodeList2.Add((object)xmlNode2);
                        }
                    }
                }
                if (xmlNode1.Attributes != null)
                {
                    foreach (XmlNode attribute in (XmlNamedNodeMap)xmlNode1.Attributes)
                    {
                        if (attribute.LocalName == "xmlns" || attribute.Prefix == "xmlns")
                        {
                            canonicalXmlNodeList4.Add((object)attribute);
                        }
                        else
                        {
                            canonicalXmlNodeList3.Add((object)attribute);
                        }
                    }
                }
                ++index;
            }while (index < canonicalXmlNodeList2.Count);
            foreach (XmlNode xmlNode in (XmlNodeList)canonicalXmlNodeList2)
            {
                canonicalXmlNodeList1.Add((object)xmlNode);
            }
            foreach (XmlNode xmlNode in (XmlNodeList)canonicalXmlNodeList3)
            {
                canonicalXmlNodeList1.Add((object)xmlNode);
            }
            foreach (XmlNode xmlNode in (XmlNodeList)canonicalXmlNodeList4)
            {
                canonicalXmlNodeList1.Add((object)xmlNode);
            }
            return((XmlNodeList)canonicalXmlNodeList1);
        }
예제 #2
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.Xml.DataObject" /> class with the specified identification, MIME type, encoding, and data.</summary>
 /// <param name="id">The identification to initialize the new instance of <see cref="T:System.Security.Cryptography.Xml.DataObject" /> with. </param>
 /// <param name="mimeType">The MIME type of the data used to initialize the new instance of <see cref="T:System.Security.Cryptography.Xml.DataObject" />. </param>
 /// <param name="encoding">The encoding of the data used to initialize the new instance of <see cref="T:System.Security.Cryptography.Xml.DataObject" />. </param>
 /// <param name="data">The data to initialize the new instance of <see cref="T:System.Security.Cryptography.Xml.DataObject" /> with. </param>
 /// <exception cref="T:System.ArgumentNullException">The <paramref name="data" /> parameter is <see langword="null" />. </exception>
 public DataObject(string id, string mimeType, string encoding, XmlElement data)
 {
     if (data == null)
     {
         throw new ArgumentNullException(nameof(data));
     }
     m_id       = id;
     m_mimeType = mimeType;
     m_encoding = encoding;
     m_elData   = new CanonicalXmlNodeList();
     m_elData.Add((object)data);
     m_cachedXml = (XmlElement)null;
 }
예제 #3
0
파일: Reference.cs 프로젝트: i-e-b/ADSD
 internal static void AddNamespaces(XmlElement elem, CanonicalXmlNodeList namespaces)
 {
     if (namespaces == null)
     {
         return;
     }
     foreach (XmlNode xmlNode in (XmlNodeList)namespaces)
     {
         string name = xmlNode.Prefix.Length > 0 ? xmlNode.Prefix + ":" + xmlNode.LocalName : xmlNode.LocalName;
         if (!elem.HasAttribute(name) && (!name.Equals("xmlns") || elem.Prefix.Length != 0))
         {
             XmlAttribute attribute = elem.OwnerDocument.CreateAttribute(name);
             attribute.Value = xmlNode.Value;
             elem.SetAttributeNode(attribute);
         }
     }
 }
예제 #4
0
        private static void MarkInclusionStateForNodes(
            XmlNodeList nodeList,
            XmlDocument inputRoot,
            XmlDocument root)
        {
            CanonicalXmlNodeList canonicalXmlNodeList1 = new CanonicalXmlNodeList();
            CanonicalXmlNodeList canonicalXmlNodeList2 = new CanonicalXmlNodeList();

            canonicalXmlNodeList1.Add(inputRoot);
            canonicalXmlNodeList2.Add(root);
            int index1 = 0;

            do
            {
                XmlNode     xmlNode1    = canonicalXmlNodeList1[index1] ?? throw new InvalidOperationException();
                XmlNode     xmlNode2    = canonicalXmlNodeList2[index1] ?? throw new InvalidOperationException();
                XmlNodeList childNodes1 = xmlNode1.ChildNodes;
                XmlNodeList childNodes2 = xmlNode2.ChildNodes;
                for (int index2 = 0; index2 < childNodes1.Count; ++index2)
                {
                    canonicalXmlNodeList1.Add(childNodes1[index2]);
                    canonicalXmlNodeList2.Add(childNodes2[index2]);
                    if (Exml.NodeInList(childNodes1[index2], nodeList))
                    {
                        MarkNodeAsIncluded(childNodes2[index2]);
                    }
                    XmlAttributeCollection attributes = childNodes1[index2]?.Attributes;
                    if (attributes == null)
                    {
                        continue;
                    }

                    for (int index3 = 0; index3 < attributes.Count; ++index3)
                    {
                        if (Exml.NodeInList(attributes[index3], nodeList))
                        {
                            MarkNodeAsIncluded(childNodes2[index2]?.Attributes?.Item(index3));
                        }
                    }
                }
                ++index1;
            }while (index1 < canonicalXmlNodeList1.Count);
        }
예제 #5
0
        private static void MarkInclusionStateForNodes(
            XmlNodeList nodeList,
            XmlDocument inputRoot,
            XmlDocument root)
        {
            CanonicalXmlNodeList canonicalXmlNodeList1 = new CanonicalXmlNodeList();
            CanonicalXmlNodeList canonicalXmlNodeList2 = new CanonicalXmlNodeList();

            canonicalXmlNodeList1.Add((object)inputRoot);
            canonicalXmlNodeList2.Add((object)root);
            int index1 = 0;

            do
            {
                XmlNode     xmlNode1    = canonicalXmlNodeList1[index1];
                XmlNode     xmlNode2    = canonicalXmlNodeList2[index1];
                XmlNodeList childNodes1 = xmlNode1.ChildNodes;
                XmlNodeList childNodes2 = xmlNode2.ChildNodes;
                for (int index2 = 0; index2 < childNodes1.Count; ++index2)
                {
                    canonicalXmlNodeList1.Add((object)childNodes1[index2]);
                    canonicalXmlNodeList2.Add((object)childNodes2[index2]);
                    if (Exml.NodeInList(childNodes1[index2], nodeList))
                    {
                        ExcCanonicalXml.MarkNodeAsIncluded(childNodes2[index2]);
                    }
                    XmlAttributeCollection attributes = childNodes1[index2].Attributes;
                    if (attributes != null)
                    {
                        for (int index3 = 0; index3 < attributes.Count; ++index3)
                        {
                            if (Exml.NodeInList((XmlNode)attributes[index3], nodeList))
                            {
                                ExcCanonicalXml.MarkNodeAsIncluded(childNodes2[index2].Attributes.Item(index3));
                            }
                        }
                    }
                }
                ++index1;
            }while (index1 < canonicalXmlNodeList1.Count);
        }
예제 #6
0
파일: Reference.cs 프로젝트: i-e-b/ADSD
        internal byte[] CalculateHashValue(XmlDocument document, CanonicalXmlNodeList refList)
        {
            this.m_hashAlgorithm = CreateFromName <HashAlgorithm>(this.m_digestMethod);
            if (this.m_hashAlgorithm == null)
            {
                throw new CryptographicException("Cryptography_Xml_CreateHashAlgorithmFailed");
            }
            string      str          = document == null ? Environment.CurrentDirectory + "\\" : document.BaseURI;
            Stream      stream       = (Stream)null;
            WebResponse webResponse  = (WebResponse)null;
            Stream      input        = (Stream)null;
            XmlResolver xmlResolver1 = (XmlResolver)null;

            try
            {
                switch (this.m_refTargetType)
                {
                case ReferenceTargetType.Stream:
                    stream = this.TransformChain.TransformToOctetStream((Stream)this.m_refTarget, this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str), str);
                    break;

                case ReferenceTargetType.XmlElement:
                    XmlResolver xmlResolver2 = this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str);
                    stream = this.TransformChain.TransformToOctetStream(PreProcessElementInput((XmlElement)this.m_refTarget, xmlResolver2, str), xmlResolver2, str);
                    break;

                case ReferenceTargetType.UriReference:
                    if (this.m_uri == null)
                    {
                        stream = this.TransformChain.TransformToOctetStream((Stream)null, this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str), str);
                        break;
                    }
                    if (this.m_uri.Length == 0)
                    {
                        if (document == null)
                        {
                            throw new CryptographicException("Cryptography error: Document is null. XML self-reference requires context (1)");
                        }
                        XmlResolver xmlResolver3 = this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str);
                        stream = this.TransformChain.TransformToOctetStream(DiscardComments(PreProcessDocumentInput(document, xmlResolver3, str)), xmlResolver3, str);
                        break;
                    }
                    if (this.m_uri[0] == '#')
                    {
                        bool   discardComments = true;
                        string idFromLocalUri  = GetIdFromLocalUri(this.m_uri, out discardComments);
                        if (idFromLocalUri == "xpointer(/)")
                        {
                            if (document == null)
                            {
                                throw new CryptographicException("Cryptography error: Document is null. XML self-reference requires context (2)");
                            }
                            XmlResolver xmlResolver3 = this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str);
                            stream = this.TransformChain.TransformToOctetStream(PreProcessDocumentInput(document, xmlResolver3, str), xmlResolver3, str);
                            break;
                        }
                        XmlElement elem = this.SignedXml.GetIdElement(document, idFromLocalUri);
                        if (elem != null)
                        {
                            this.m_namespaces = CanonicalXmlNodeList.GetPropagatedAttributes(elem.ParentNode as XmlElement);
                        }
                        if (elem == null && refList != null)
                        {
                            foreach (XmlNode xmlNode in (XmlNodeList)refList)
                            {
                                XmlElement element = xmlNode as XmlElement;
                                if (element != null && HasAttribute(element, "Id", "http://www.w3.org/2000/09/xmldsig#") && GetAttribute(element, "Id", "http://www.w3.org/2000/09/xmldsig#").Equals(idFromLocalUri))
                                {
                                    elem = element;
                                    if (this.m_signedXml.m_context != null)
                                    {
                                        this.m_namespaces = CanonicalXmlNodeList.GetPropagatedAttributes(this.m_signedXml.m_context);
                                        break;
                                    }
                                    break;
                                }
                            }
                        }
                        if (elem == null)
                        {
                            throw new CryptographicException("Cryptography error: XML reference is invalid");
                        }
                        XmlDocument document1 = PreProcessElementInput(elem, xmlResolver1, str);
                        AddNamespaces(document1.DocumentElement, this.m_namespaces);
                        XmlResolver resolver = this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str);
                        stream = !discardComments?this.TransformChain.TransformToOctetStream(document1, resolver, str) : this.TransformChain.TransformToOctetStream(DiscardComments(document1), resolver, str);

                        break;
                    }
                    if (!GlobalSettings.AllowDetachedSignature)
                    {
                        throw new CryptographicException($"Cryptography error: XML URL {m_uri} not resolved (1)");
                    }
                    System.Uri uri = new System.Uri(this.m_uri, UriKind.RelativeOrAbsolute);
                    if (!uri.IsAbsoluteUri)
                    {
                        uri = new System.Uri(new System.Uri(str), uri);
                    }
                    WebRequest webRequest = WebRequest.Create(uri);
                    if (webRequest != null)
                    {
                        webResponse = webRequest.GetResponse();
                        if (webResponse != null)
                        {
                            input = webResponse.GetResponseStream();
                            if (input != null)
                            {
                                XmlResolver resolver = this.SignedXml.ResolverSet ? this.SignedXml.m_xmlResolver : (XmlResolver) new XmlSecureResolver((XmlResolver) new XmlUrlResolver(), str);
                                stream = this.TransformChain.TransformToOctetStream(input, resolver, this.m_uri);
                                break;
                            }
                            goto default;
                        }
                        else
                        {
                            goto default;
                        }
                    }
                    else
                    {
                        goto default;
                    }

                default:
                    throw new CryptographicException($"Cryptography error: XML URL {m_uri} not resolved (2)");
                }
                return(this.m_hashAlgorithm.ComputeHash(stream));
            }
            finally
            {
                stream?.Close();
                webResponse?.Close();
                input?.Close();
            }
        }
예제 #7
0
파일: Reference.cs 프로젝트: i-e-b/ADSD
 internal void UpdateHashValue(XmlDocument document, CanonicalXmlNodeList refList)
 {
     this.DigestValue = this.CalculateHashValue(document, refList);
 }
예제 #8
0
        public static CanonicalXmlNodeList GetPropagatedAttributes(XmlElement elem)
        {
            if (elem == null)
            {
                return((CanonicalXmlNodeList)null);
            }
            CanonicalXmlNodeList canonicalXmlNodeList = new CanonicalXmlNodeList();
            XmlNode xmlNode = (XmlNode)elem;

            if (xmlNode == null)
            {
                return((CanonicalXmlNodeList)null);
            }
            bool flag = true;

            while (xmlNode != null)
            {
                XmlElement element = xmlNode as XmlElement;
                if (element == null)
                {
                    xmlNode = xmlNode.ParentNode;
                }
                else
                {
                    if (!IsCommittedNamespace(element, element.Prefix, element.NamespaceURI) && !IsRedundantNamespace(element, element.Prefix, element.NamespaceURI))
                    {
                        string       name      = element.Prefix.Length > 0 ? "xmlns:" + element.Prefix : "xmlns";
                        XmlAttribute attribute = elem.OwnerDocument.CreateAttribute(name);
                        attribute.Value = element.NamespaceURI;
                        canonicalXmlNodeList.Add((object)attribute);
                    }
                    if (element.HasAttributes)
                    {
                        foreach (XmlAttribute attribute1 in (XmlNamedNodeMap)element.Attributes)
                        {
                            if (flag && attribute1.LocalName == "xmlns")
                            {
                                XmlAttribute attribute2 = elem.OwnerDocument.CreateAttribute("xmlns");
                                attribute2.Value = attribute1.Value;
                                canonicalXmlNodeList.Add((object)attribute2);
                                flag = false;
                            }
                            else if (attribute1.Prefix == "xmlns" || attribute1.Prefix == "xml")
                            {
                                canonicalXmlNodeList.Add((object)attribute1);
                            }
                            else if (attribute1.NamespaceURI.Length > 0 &&
                                     !IsCommittedNamespace(element, attribute1.Prefix, attribute1.NamespaceURI) &&
                                     !IsRedundantNamespace(element, attribute1.Prefix, attribute1.NamespaceURI))
                            {
                                string       name       = attribute1.Prefix.Length > 0 ? "xmlns:" + attribute1.Prefix : "xmlns";
                                XmlAttribute attribute2 = elem.OwnerDocument.CreateAttribute(name);
                                attribute2.Value = attribute1.NamespaceURI;
                                canonicalXmlNodeList.Add((object)attribute2);
                            }
                        }
                    }
                    xmlNode = xmlNode.ParentNode;
                }
            }
            return(canonicalXmlNodeList);
        }
예제 #9
0
        public void WriteHash(
            HashAlgorithm hash,
            DocPosition docPos,
            AncestralNamespaceContextManager anc)
        {
            Hashtable              nsLocallyDeclared = new Hashtable();
            SortedList             sortedList        = new SortedList((IComparer) new NamespaceSortOrder());
            SortedList             attrListToRender  = new SortedList((IComparer) new AttributeSortOrder());
            UTF8Encoding           utF8Encoding      = new UTF8Encoding(false);
            XmlAttributeCollection attributes        = this.Attributes;

            if (attributes != null)
            {
                foreach (XmlAttribute attr in (XmlNamedNodeMap)attributes)
                {
                    if (((CanonicalXmlAttribute)attr).IsInNodeSet || Exml.IsNamespaceNode((XmlNode)attr) || Exml.IsXmlNamespaceNode((XmlNode)attr))
                    {
                        if (Exml.IsNamespaceNode((XmlNode)attr))
                        {
                            anc.TrackNamespaceNode(attr, sortedList, nsLocallyDeclared);
                        }
                        else if (Exml.IsXmlNamespaceNode((XmlNode)attr))
                        {
                            anc.TrackXmlNamespaceNode(attr, sortedList, attrListToRender, nsLocallyDeclared);
                        }
                        else if (this.IsInNodeSet)
                        {
                            attrListToRender.Add((object)attr, (object)null);
                        }
                    }
                }
            }
            if (!CanonicalXmlNodeList.IsCommittedNamespace((XmlElement)this, this.Prefix, this.NamespaceURI))
            {
                XmlAttribute attribute = this.OwnerDocument.CreateAttribute(this.Prefix.Length > 0 ? "xmlns:" + this.Prefix : "xmlns");
                attribute.Value = this.NamespaceURI;
                anc.TrackNamespaceNode(attribute, sortedList, nsLocallyDeclared);
            }
            if (this.IsInNodeSet)
            {
                anc.GetNamespacesToRender((XmlElement)this, attrListToRender, sortedList, nsLocallyDeclared);
                byte[] bytes1 = utF8Encoding.GetBytes("<" + this.Name);
                hash.TransformBlock(bytes1, 0, bytes1.Length, bytes1, 0);
                foreach (object key in (IEnumerable)sortedList.GetKeyList())
                {
                    (key as CanonicalXmlAttribute).WriteHash(hash, docPos, anc);
                }
                foreach (object key in (IEnumerable)attrListToRender.GetKeyList())
                {
                    (key as CanonicalXmlAttribute).WriteHash(hash, docPos, anc);
                }
                byte[] bytes2 = utF8Encoding.GetBytes(">");
                hash.TransformBlock(bytes2, 0, bytes2.Length, bytes2, 0);
            }
            anc.EnterElementContext();
            anc.LoadUnrenderedNamespaces(nsLocallyDeclared);
            anc.LoadRenderedNamespaces(sortedList);
            foreach (XmlNode childNode in this.ChildNodes)
            {
                CanonicalizationDispatcher.WriteHash(childNode, hash, docPos, anc);
            }
            anc.ExitElementContext();
            if (!this.IsInNodeSet)
            {
                return;
            }
            byte[] bytes = utF8Encoding.GetBytes("</" + this.Name + ">");
            hash.TransformBlock(bytes, 0, bytes.Length, bytes, 0);
        }
        /// <summary>Returns the output of the current <see cref="T:System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform" /> object.</summary>
        /// <returns>The output of the current <see cref="T:System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform" /> object.</returns>
        /// <exception cref="T:System.Security.Cryptography.CryptographicException">The containing XML document is <see langword="null" />.</exception>
        public override object GetOutput()
        {
            if (_containingDocument == null)
            {
                throw new CryptographicException("Cryptography error: XML enveloped signature requires context");
            }
            if (_inputNodeList != null)
            {
                if (_signaturePosition == 0)
                {
                    return((object)_inputNodeList);
                }
                XmlNodeList xmlNodeList = _containingDocument.SelectNodes("//dsig:Signature", _nsm);
                if (xmlNodeList == null)
                {
                    return((object)_inputNodeList);
                }
                CanonicalXmlNodeList canonicalXmlNodeList = new CanonicalXmlNodeList();
                foreach (XmlNode inputNode in _inputNodeList)
                {
                    if (inputNode != null)
                    {
                        if (!IsXmlNamespaceNode(inputNode))
                        {
                            if (!IsNamespaceNode(inputNode))
                            {
                                try
                                {
                                    XmlNode xmlNode1 = inputNode.SelectSingleNode("ancestor-or-self::dsig:Signature[1]", _nsm);
                                    int     num      = 0;
                                    foreach (XmlNode xmlNode2 in xmlNodeList)
                                    {
                                        ++num;
                                        if (xmlNode2 == xmlNode1)
                                        {
                                            break;
                                        }
                                    }
                                    if (xmlNode1 != null)
                                    {
                                        if (xmlNode1 != null)
                                        {
                                            if (num == _signaturePosition)
                                            {
                                                continue;
                                            }
                                        }
                                        else
                                        {
                                            continue;
                                        }
                                    }
                                    canonicalXmlNodeList.Add((object)inputNode);
                                    continue;
                                }
                                catch
                                {
                                    continue;
                                }
                            }
                        }
                        canonicalXmlNodeList.Add((object)inputNode);
                    }
                }
                return((object)canonicalXmlNodeList);
            }
            XmlNodeList xmlNodeList1 = _containingDocument.SelectNodes("//dsig:Signature", _nsm);

            if (xmlNodeList1 == null)
            {
                return((object)_containingDocument);
            }
            if (xmlNodeList1.Count < _signaturePosition || _signaturePosition <= 0)
            {
                return((object)_containingDocument);
            }
            xmlNodeList1[_signaturePosition - 1].ParentNode.RemoveChild(xmlNodeList1[_signaturePosition - 1]);
            return((object)_containingDocument);
        }
예제 #11
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.Xml.DataObject" /> class.</summary>
 public DataObject()
 {
     m_cachedXml = (XmlElement)null;
     m_elData    = new CanonicalXmlNodeList();
 }
예제 #12
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.Xml.Signature" /> class.</summary>
 public Signature()
 {
     this.m_embeddedObjects = (IList) new ArrayList();
     this.m_referencedItems = new CanonicalXmlNodeList();
 }