Beispiel #1
0
        internal void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(value.OwnerDocument.NameTable);

            nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            XmlNodeList xmlNodeList = value.SelectNodes("ds:Transform", nsmgr);

            if (xmlNodeList.Count == 0)
            {
                throw new CryptographicException("Cryptography_Xml_InvalidElement: Transforms");
            }
            this.m_transforms.Clear();
            for (int index = 0; index < xmlNodeList.Count; ++index)
            {
                XmlElement element  = (XmlElement)xmlNodeList.Item(index);
                Transform  fromName = Exml.CreateFromName <Transform>(Exml.GetAttribute(element, "Algorithm", "http://www.w3.org/2000/09/xmldsig#"));
                if (fromName == null)
                {
                    throw new CryptographicException("Cryptography_Xml_UnknownTransform");
                }
                fromName.LoadInnerXml(element.ChildNodes);
                this.m_transforms.Add((object)fromName);
            }
        }
Beispiel #2
0
 /// <summary>Parses the specified <see cref="T:System.Xml.XmlNodeList" /> object as transform-specific content of a <see langword="&lt;Transform&gt;" /> element and configures the internal state of the current <see cref="T:System.Security.Cryptography.Xml.XmlDsigExcC14NTransform" /> object to match the <see langword="&lt;Transform&gt;" /> element.</summary>
 /// <param name="nodeList">An <see cref="T:System.Xml.XmlNodeList" /> object that specifies transform-specific content for the current <see cref="T:System.Security.Cryptography.Xml.XmlDsigExcC14NTransform" /> object.</param>
 public override void LoadInnerXml(XmlNodeList nodeList)
 {
     if (nodeList == null)
     {
         return;
     }
     foreach (XmlNode node in nodeList)
     {
         XmlElement element = node as XmlElement;
         if (element != null)
         {
             if (element.LocalName.Equals("InclusiveNamespaces") &&
                 element.NamespaceURI.Equals("http://www.w3.org/2001/10/xml-exc-c14n#") &&
                 Exml.HasAttribute(element, "PrefixList", "http://www.w3.org/2000/09/xmldsig#"))
             {
                 if (!Exml.VerifyAttributes(element, "PrefixList"))
                 {
                     throw new CryptographicException("Cryptography_Xml_UnknownTransform");
                 }
                 this.InclusiveNamespacesPrefixList = Exml.GetAttribute(element, "PrefixList", "http://www.w3.org/2000/09/xmldsig#");
                 break;
             }
         }
     }
 }
Beispiel #3
0
 public void Write(
     [NotNull] StringBuilder strBuilder,
     DocPosition docPos,
     AncestralNamespaceContextManager anc)
 {
     strBuilder.Append(" " + Name + "=\"");
     strBuilder.Append(Exml.EscapeAttributeValue(Value));
     strBuilder.Append("\"");
 }
Beispiel #4
0
        /// <summary>Loads the trusted issuers from configuration.</summary>
        /// <param name="customConfiguration">The XML that represents the map of trusted issuers that is specified in the configuration file.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="customConfiguration" /> is <see langword="null" />.</exception>
        /// <exception cref="T:System.InvalidOperationException">The configuration contains one or more elements that are not recognized.</exception>
        public void LoadCustomConfiguration(XmlNodeList customConfiguration)
        {
            if (customConfiguration == null)
            {
                throw new ArgumentNullException(nameof(customConfiguration));
            }
            List <XmlElement> xmlElements = Exml.GetXmlElements(customConfiguration);

            if (xmlElements.Count != 1)
            {
                throw new InvalidOperationException("Issuer registry config invalid");
            }

            XmlElement xmlElement1 = xmlElements[0];

            if (!StringComparer.Ordinal.Equals(xmlElement1.LocalName, "trustedIssuers"))
            {
                throw new InvalidOperationException("trustedIssuers");
            }

            foreach (XmlNode childNode in xmlElement1.ChildNodes)
            {
                XmlElement xmlElement2 = childNode as XmlElement;
                if (xmlElement2 != null)
                {
                    if (StringComparer.Ordinal.Equals(xmlElement2.LocalName, "add"))
                    {
                        XmlNode namedItem1 = xmlElement2.Attributes.GetNamedItem("thumbprint");
                        XmlNode namedItem2 = xmlElement2.Attributes.GetNamedItem("name");
                        if (xmlElement2.Attributes.Count > 2 || namedItem1 == null)
                        {
                            throw new Exception("add thumbprint/name does not match");
                        }
                        this._configuredTrustedIssuers.Add(namedItem1.Value.Replace(" ", ""), namedItem2 == null || string.IsNullOrEmpty(namedItem2.Value) ? string.Empty : string.Intern(namedItem2.Value));
                    }
                    else if (StringComparer.Ordinal.Equals(xmlElement2.LocalName, "remove"))
                    {
                        if (xmlElement2.Attributes.Count != 1 || !StringComparer.Ordinal.Equals(xmlElement2.Attributes[0].LocalName, "thumbprint"))
                        {
                            throw new Exception("remove thumbprint deosn't work");
                        }
                        this._configuredTrustedIssuers.Remove(xmlElement2.Attributes.GetNamedItem("thumbprint").Value.Replace(" ", ""));
                    }
                    else if (StringComparer.Ordinal.Equals(xmlElement2.LocalName, "clear"))
                    {
                        this._configuredTrustedIssuers.Clear();
                    }
                    else
                    {
                        throw new Exception("unknown element type");
                    }
                }
            }
        }
        private bool HasNonRedundantInclusivePrefix(XmlAttribute attr)
        {
            string namespacePrefix = Exml.GetNamespacePrefix(attr);

            if (m_inclusivePrefixSet.ContainsKey((object)namespacePrefix))
            {
                int depth;
                return(Exml.IsNonRedundantNamespaceDecl(attr, GetNearestRenderedNamespaceWithMatchingPrefix(namespacePrefix, out depth)));
            }
            return(false);
        }
Beispiel #6
0
        public void WriteHash(
            [NotNull] HashAlgorithm hash,
            DocPosition docPos,
            AncestralNamespaceContextManager anc)
        {
            UTF8Encoding utF8Encoding = new UTF8Encoding(false);

            byte[] bytes1 = utF8Encoding.GetBytes(" " + Name + "=\"");
            hash.TransformBlock(bytes1, 0, bytes1.Length, bytes1, 0);
            byte[] bytes2 = utF8Encoding.GetBytes(Exml.EscapeAttributeValue(Value));
            hash.TransformBlock(bytes2, 0, bytes2.Length, bytes2, 0);
            byte[] bytes3 = utF8Encoding.GetBytes("\"");
            hash.TransformBlock(bytes3, 0, bytes3.Length, bytes3, 0);
        }
 internal override void TrackNamespaceNode(
     XmlAttribute attr,
     SortedList nsListToRender,
     Hashtable nsLocallyDeclared)
 {
     if (HasNonRedundantInclusivePrefix(attr))
     {
         nsListToRender.Add((object)attr, (object)null);
     }
     else
     {
         nsLocallyDeclared.Add((object)Exml.GetNamespacePrefix(attr), (object)attr);
     }
 }
Beispiel #8
0
 /// <summary>Parses the input <see cref="T:System.Xml.XmlElement" /> and configures the internal state of the <see cref="T:System.Security.Cryptography.Xml.EncryptionProperty" /> object to match.</summary>
 /// <param name="value">An <see cref="T:System.Xml.XmlElement" /> object to parse.</param>
 /// <exception cref="T:System.ArgumentNullException">The <paramref name="value" /> parameter is <see langword="null" />.</exception>
 /// <exception cref="T:System.Security.Cryptography.CryptographicException">The <see cref="P:System.Xml.XmlElement.LocalName" /> property of the <paramref name="value" /> parameter is not "EncryptionProperty". -or-The <see cref="P:System.Xml.XmlElement.NamespaceURI" /> property of the <paramref name="value" /> parameter is not "http://www.w3.org/2001/04/xmlenc#".</exception>
 public void LoadXml(XmlElement value)
 {
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
     if (value.LocalName != nameof(EncryptionProperty) || value.NamespaceURI != "http://www.w3.org/2001/04/xmlenc#")
     {
         throw new CryptographicException("Cryptography_Xml_InvalidEncryptionProperty");
     }
     m_cachedXml = value;
     Id          = Exml.GetAttribute(value, "Id", "http://www.w3.org/2001/04/xmlenc#");
     Target      = Exml.GetAttribute(value, "Target", "http://www.w3.org/2001/04/xmlenc#");
     m_elemProp  = value;
 }
Beispiel #9
0
 /// <summary>Loads a <see cref="T:System.Security.Cryptography.Xml.DataObject" /> state from an XML element.</summary>
 /// <param name="value">The XML element to load the <see cref="T:System.Security.Cryptography.Xml.DataObject" /> state from. </param>
 /// <exception cref="T:System.ArgumentNullException">The value from the XML element is <see langword="null" />.</exception>
 public void LoadXml(XmlElement value)
 {
     if (value == null)
     {
         throw new ArgumentNullException(nameof(value));
     }
     m_id       = Exml.GetAttribute(value, "Id", "http://www.w3.org/2000/09/xmldsig#");
     m_mimeType = Exml.GetAttribute(value, "MimeType", "http://www.w3.org/2000/09/xmldsig#");
     m_encoding = Exml.GetAttribute(value, "Encoding", "http://www.w3.org/2000/09/xmldsig#");
     foreach (XmlNode childNode in value.ChildNodes)
     {
         m_elData.Add((object)childNode);
     }
     m_cachedXml = value;
 }
Beispiel #10
0
 internal ExcCanonicalXml(
     Stream inputStream,
     bool includeComments,
     string inclusiveNamespacesPrefixList,
     XmlResolver resolver,
     string strBaseUri)
 {
     if (inputStream == null)
     {
         throw new ArgumentNullException(nameof(inputStream));
     }
     this.m_c14nDoc             = new CanonicalXmlDocument(true, includeComments);
     this.m_c14nDoc.XmlResolver = resolver;
     this.m_c14nDoc.Load(Exml.PreProcessStreamInput(inputStream, resolver, strBaseUri));
     this.m_ancMgr = new ExcAncestralNamespaceContextManager(inclusiveNamespacesPrefixList);
 }
Beispiel #11
0
        /// <summary>Parses the input <see cref="T:System.Xml.XmlElement" /> object and configures the internal state of the <see cref="T:System.Security.Cryptography.Xml.KeyInfoX509Data" /> object to match.</summary>
        /// <param name="element">The <see cref="T:System.Xml.XmlElement" /> object that specifies the state of the <see cref="T:System.Security.Cryptography.Xml.KeyInfoX509Data" /> object. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="element" /> parameter is <see langword="null" />.</exception>
        /// <exception cref="T:System.Security.Cryptography.CryptographicException">The <paramref name="element" /> parameter does not contain an &lt;<see langword="X509IssuerName" />&gt; node.-or-The <paramref name="element" /> parameter does not contain an &lt;<see langword="X509SerialNumber" />&gt; node.</exception>
        public override void LoadXml(XmlElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException(nameof(element));
            }
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(element.OwnerDocument.NameTable);

            nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#");
            XmlNodeList xmlNodeList1 = element.SelectNodes("ds:X509IssuerSerial", nsmgr);
            XmlNodeList xmlNodeList2 = element.SelectNodes("ds:X509SKI", nsmgr);
            XmlNodeList xmlNodeList3 = element.SelectNodes("ds:X509SubjectName", nsmgr);
            XmlNodeList xmlNodeList4 = element.SelectNodes("ds:X509Certificate", nsmgr);
            XmlNodeList xmlNodeList5 = element.SelectNodes("ds:X509CRL", nsmgr);

            if (xmlNodeList5.Count == 0 && xmlNodeList1.Count == 0 && (xmlNodeList2.Count == 0 && xmlNodeList3.Count == 0) && xmlNodeList4.Count == 0)
            {
                throw new CryptographicException("Invalid XML element: X509Data");
            }
            this.Clear();
            if (xmlNodeList5.Count != 0)
            {
                this.m_CRL = Convert.FromBase64String(Exml.DiscardWhiteSpaces(xmlNodeList5.Item(0).InnerText));
            }
            foreach (XmlNode xmlNode1 in xmlNodeList1)
            {
                XmlNode xmlNode2 = xmlNode1.SelectSingleNode("ds:X509IssuerName", nsmgr);
                XmlNode xmlNode3 = xmlNode1.SelectSingleNode("ds:X509SerialNumber", nsmgr);
                if (xmlNode2 == null || xmlNode3 == null)
                {
                    throw new CryptographicException("Invalid XML element: IssuerSerial");
                }
                this.InternalAddIssuerSerial(xmlNode2.InnerText.Trim(), xmlNode3.InnerText.Trim());
            }
            foreach (XmlNode xmlNode in xmlNodeList2)
            {
                this.AddSubjectKeyId(Convert.FromBase64String(Exml.DiscardWhiteSpaces(xmlNode.InnerText)));
            }
            foreach (XmlNode xmlNode in xmlNodeList3)
            {
                this.AddSubjectName(xmlNode.InnerText.Trim());
            }
            foreach (XmlNode xmlNode in xmlNodeList4)
            {
                this.AddCertificate((X509Certificate) new X509Certificate2(Convert.FromBase64String(Exml.DiscardWhiteSpaces(xmlNode.InnerText))));
            }
        }
Beispiel #12
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);
        }
Beispiel #13
0
        /// <summary>Loads a <see cref="T:System.Security.Cryptography.Xml.KeyInfo" /> state from an XML element.</summary>
        /// <param name="value">The XML element from which to load the <see cref="T:System.Security.Cryptography.Xml.KeyInfo" /> state. </param>
        /// <exception cref="T:System.ArgumentNullException">The <paramref name="value" /> parameter is <see langword="null" />. </exception>
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException(nameof(value));
            }
            XmlElement element1 = value;

            this.m_id = Exml.GetAttribute(element1, "Id", "http://www.w3.org/2000/09/xmldsig#");
            if (!Exml.VerifyAttributes(element1, "Id"))
            {
                throw new CryptographicException("Invalid XML element: KeyInfo");
            }
            for (XmlNode xmlNode = element1.FirstChild; xmlNode != null; xmlNode = xmlNode.NextSibling)
            {
                XmlElement element2 = xmlNode as XmlElement;
                if (element2 != null)
                {
                    string key = element2.NamespaceURI + " " + element2.LocalName;
                    if (key == "http://www.w3.org/2000/09/xmldsig# KeyValue")
                    {
                        if (!Exml.VerifyAttributes(element2, (string[])null))
                        {
                            throw new CryptographicException("Invalid XML element: KeyInfo/KeyValue");
                        }
                        foreach (XmlNode childNode in element2.ChildNodes)
                        {
                            XmlElement xmlElement = childNode as XmlElement;
                            if (xmlElement != null)
                            {
                                key = key + "/" + xmlElement.LocalName;
                                break;
                            }
                        }
                    }
                    KeyInfoClause clause = Exml.CreateFromName <KeyInfoClause>(key) ?? (KeyInfoClause) new KeyInfoNode();
                    clause.LoadXml(element2);
                    this.AddClause(clause);
                }
            }
        }
Beispiel #14
0
        internal ExcCanonicalXml(
            XmlNodeList nodeList,
            bool includeComments,
            string inclusiveNamespacesPrefixList,
            XmlResolver resolver)
        {
            if (nodeList == null)
            {
                throw new ArgumentNullException(nameof(nodeList));
            }
            XmlDocument ownerDocument = Exml.GetOwnerDocument(nodeList);

            if (ownerDocument == null)
            {
                throw new ArgumentException(nameof(nodeList));
            }
            this.m_c14nDoc             = new CanonicalXmlDocument(false, includeComments);
            this.m_c14nDoc.XmlResolver = resolver;
            this.m_c14nDoc.Load((XmlReader) new XmlNodeReader((XmlNode)ownerDocument));
            this.m_ancMgr = new ExcAncestralNamespaceContextManager(inclusiveNamespacesPrefixList);
            ExcCanonicalXml.MarkInclusionStateForNodes(nodeList, ownerDocument, (XmlDocument)this.m_c14nDoc);
        }
        private void GatherNamespaceToRender(
            string nsPrefix,
            SortedList nsListToRender,
            Hashtable nsLocallyDeclared)
        {
            foreach (XmlAttribute key in (IEnumerable)nsListToRender.GetKeyList())
            {
                if (Exml.HasNamespacePrefix(key, nsPrefix))
                {
                    return;
                }
            }
            XmlAttribute a = (XmlAttribute)nsLocallyDeclared[(object)nsPrefix];
            int          depth1;
            XmlAttribute withMatchingPrefix1 = GetNearestRenderedNamespaceWithMatchingPrefix(nsPrefix, out depth1);

            if (a != null)
            {
                if (!Exml.IsNonRedundantNamespaceDecl(a, withMatchingPrefix1))
                {
                    return;
                }
                nsLocallyDeclared.Remove((object)nsPrefix);
                nsListToRender.Add((object)a, (object)null);
            }
            else
            {
                int          depth2;
                XmlAttribute withMatchingPrefix2 = GetNearestUnrenderedNamespaceWithMatchingPrefix(nsPrefix, out depth2);
                if (withMatchingPrefix2 == null || depth2 <= depth1 || !Exml.IsNonRedundantNamespaceDecl(withMatchingPrefix2, withMatchingPrefix1))
                {
                    return;
                }
                nsListToRender.Add((object)withMatchingPrefix2, (object)null);
            }
        }