예제 #1
0
        public bool VerifyXmlFile(XmlDocument xmlDocument)
        {
            if (xmlDocument == null)
            {
                throw new ArgumentException($"{nameof(xmlDocument)} cannot be null. Please supply a valid xml document");
            }

            var signedXml           = new CustomIdSignedXml(xmlDocument);
            var nodeList            = xmlDocument.GetElementsByTagName(SignatureElementName, NS2);
            var xmlDigitalSignature = (XmlElement)nodeList[0];

            if (xmlDigitalSignature == null)
            {
                return(true);
            }

            signedXml.LoadXml(xmlDigitalSignature);

            // Check the signature and return the result.
            return(signedXml.CheckSignature());
        }
예제 #2
0
파일: Examples.cs 프로젝트: svn2github/ehi
        public void Hack()
        {
            XmlDocument doc = new XmlDocument();
            doc.PreserveWhitespace = true;
            doc.Load(@"C:\TMP\cache.xml");

            SignedXml sig = new CustomIdSignedXml(doc);
            XmlNodeList nodeList = doc.GetElementsByTagName("Signature", "http://www.w3.org/2000/09/xmldsig#");
            foreach (XmlElement e in nodeList)
            {
                sig.LoadXml(e);
                Assert.IsTrue(sig.CheckSignature());
            }
        }
예제 #3
0
        public string SignXmlFile(XmlDocument xmlDocument, X509Certificate2 certificate, string xmlSignatureSyntax)
        {
            if (xmlDocument == null)
            {
                throw new ArgumentException($"{nameof(xmlDocument)} cannot be null. Please supply a valid xml document");
            }
            if (certificate == null)
            {
                throw new ArgumentException($"{nameof(certificate)} cannot be null. Please supply a valid certificate");
            }
            if (string.IsNullOrWhiteSpace(xmlSignatureSyntax))
            {
                throw new ArgumentException($"{nameof(xmlSignatureSyntax)} cannot be null. Please supply a valid XmlSignatureSyntax");
            }

            // Load xml and set signing parameters
            var signedXml = new CustomIdSignedXml(xmlDocument);

            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigC14NTransformUrl;
            signedXml.SignedInfo.SignatureMethod        = SignatureMethod;
            signedXml.SigningKey = certificate.GetRSAPrivateKey();;

            // Set the namespases
            var nsmgr = new XmlNamespaceManager(xmlDocument.NameTable);

            nsmgr.AddNamespace("ns", xmlSignatureSyntax);
            nsmgr.AddNamespace("ns2", NS2);

            // Select message node for signing
            var reference = new Reference();

            reference.DigestMethod = DigestMethod;
            reference.Uri          = "#" + xmlDocument.SelectSingleNode($"//ns:{MessageElementName}", nsmgr)
                                     .Attributes["messageId"].Value;
            signedXml.AddReference(reference);

            // Set signing key and sign xml data
            var keyInfo = new KeyInfo();

            keyInfo.AddClause(new KeyInfoX509Data(certificate));
            signedXml.KeyInfo = keyInfo;
            signedXml.ComputeSignature();

            // Get signature
            var xmlDigitalSignature = signedXml.GetXml();

            // Assign ds prefix
            AssignNameSpacePrefixToElementTree(xmlDigitalSignature, CustomPrefix);

            //load SignedInfo and compute final signature based on correct SignedInfo
            signedXml.LoadXml(xmlDigitalSignature);
            signedXml.SignedInfo.References.Clear();
            signedXml.ComputeSignature();

            // Replace signature
            ReplaceSignature(xmlDigitalSignature, Convert.ToBase64String(signedXml.SignatureValue));

            // Append signature to the xml doc
            xmlDocument.DocumentElement.AppendChild(xmlDocument.ImportNode(xmlDigitalSignature, true));

            using var stringWriter  = new StringWriter();
            using var xmlTextWriter = new XmlTextWriter(stringWriter);
            xmlDocument.WriteTo(xmlTextWriter);
            xmlTextWriter.Flush();
            return(stringWriter.ToString());
        }