Beispiel #1
0
        public PhpString C14N(
            bool exclusive       = false,
            bool with_comments   = false,
            PhpArray xpath       = null,
            PhpArray ns_prefixes = null)
        {
            System.Security.Cryptography.Xml.XmlDsigC14NTransform transform = new System.Security.Cryptography.Xml.XmlDsigC14NTransform();

            transform.LoadInput(XmlNode.GetXmlDocument());
            System.IO.MemoryStream stream = (System.IO.MemoryStream)transform.GetOutput(typeof(System.IO.Stream));
            return(new PhpString(stream.ToArray()));
        }
Beispiel #2
0
        public long C14NFile(
            Context ctx,
            string uri,
            bool exclusive       = false,
            bool with_comments   = false,
            PhpArray xpath       = null,
            PhpArray ns_prefixes = null)
        {
            using var stream = PhpStream.Open(ctx, uri, StreamOpenMode.WriteBinary);

            if (stream != null)
            {
                var transform = new System.Security.Cryptography.Xml.XmlDsigC14NTransform();
                transform.LoadInput(XmlNode.GetXmlDocument());
                var output = (System.IO.MemoryStream)transform.GetOutput(typeof(System.IO.Stream));

                output.CopyTo(stream.RawStream);

                return(output.Length);
            }

            return(-1); // FALSE
        }
        /// <summary>
        /// Set the IRmark for a 
        /// </summary>
        /// <param name="XmlFile"></param>
        /// <returns></returns>
        public XmlDocument SetIRmark(XmlDocument XmlFile)
        {
            // Loads XML document into byte array
            byte[] bytes = Encoding.UTF8.GetBytes(XmlFile.OuterXml);

            string vbLf = "\n";
            string vbCrLf = "\r\n";
            string result = String.Empty;

            string text = Encoding.UTF8.GetString(bytes);

            XmlNode root = XmlFile.DocumentElement;

            XmlNamespaceManager nsManager = new XmlNamespaceManager(XmlFile.NameTable);
            nsManager.AddNamespace("govtalkgateway", XmlFile.DocumentElement.NamespaceURI);

            // Create an XML document of just the Body element
            XmlNode bodyNode = XmlFile.SelectSingleNode("//govtalkgateway:Body", nsManager);

            //nsManager.AddNamespace("ir68", bodyNode.FirstChild.NextSibling.NamespaceURI);
            nsManager.AddNamespace("ir68", "http://www.govtalk.gov.uk/taxation/charities/r68/2");

            XmlDocument xmlBody = new XmlDocument();
            xmlBody.PreserveWhitespace = true;
            xmlBody.LoadXml(bodyNode.OuterXml);

            // Remove any existing IRmark
            XmlNode nodeIr = xmlBody.SelectSingleNode("//ir68:IRmark", nsManager);
            XmlNode irMarkPlaceholder = nodeIr;
            if (nodeIr != null)
            {
                irMarkPlaceholder = nodeIr.PreviousSibling;
                nodeIr.ParentNode.RemoveChild(nodeIr);
            }

            // Normalise the document using C14N (Canonicalisation)
            System.Security.Cryptography.Xml.XmlDsigC14NTransform c14n = new System.Security.Cryptography.Xml.XmlDsigC14NTransform();
            c14n.LoadInput(xmlBody);

            using (Stream stream = (Stream)c14n.GetOutput())
            {
                byte[] buffer = new byte[stream.Length];

                // convert to string and normalise line endings
                stream.Read(buffer, 0, (int)stream.Length);
                text = Encoding.UTF8.GetString(buffer);
                text = text.Replace("&#xD;", "");
                text = text.Replace(vbCrLf, vbLf);

                // convert the final document back into a byte array
                byte[] textBytes = Encoding.UTF8.GetBytes(text);

                //create the SHA-1 hash from the final document
                System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
                byte[] hash = sha.ComputeHash(textBytes);

                result = Convert.ToBase64String(hash);
                _loggingService.LogInfo(typeof(GovTalkMessageHelper), string.Format("IRmark created: {0}",result));
            }

            // attempt to re-insert the IRmark

            XmlNode irMarkNode = root.SelectSingleNode("//*[contains(name(),'IRmark')]");

            if (!String.IsNullOrEmpty(irMarkNode.InnerText))
            {
                root.SelectSingleNode("//*[contains(name(),'IRmark')]").LastChild.Value = result;
            }
            else
            {
                if (root.SelectSingleNode("//*[contains(name(),'IRmark')]") != null)
                {
                    irMarkNode.InnerText = result;
                }
                else
                {
                    _loggingService.LogWarning(typeof(GovTalkMessageHelper), "No IRmark");
                }
            }

            _loggingService.LogInfo(typeof(GovTalkMessageHelper), string.Format("IRmark inserted into XML Document."));

            return XmlFile;
        }
Beispiel #4
0
        /// <summary>
        /// Set the IRmark for a
        /// </summary>
        /// <param name="XmlFile"></param>
        /// <returns></returns>
        public XmlDocument SetIRmark(XmlDocument XmlFile)
        {
            // Loads XML document into byte array
            byte[] bytes = Encoding.UTF8.GetBytes(XmlFile.OuterXml);

            string vbLf   = "\n";
            string vbCrLf = "\r\n";
            string result = String.Empty;

            string text = Encoding.UTF8.GetString(bytes);

            XmlNode root = XmlFile.DocumentElement;

            XmlNamespaceManager nsManager = new XmlNamespaceManager(XmlFile.NameTable);

            nsManager.AddNamespace("govtalkgateway", XmlFile.DocumentElement.NamespaceURI);

            // Create an XML document of just the Body element
            XmlNode bodyNode = XmlFile.SelectSingleNode("//govtalkgateway:Body", nsManager);

            //nsManager.AddNamespace("ir68", bodyNode.FirstChild.NextSibling.NamespaceURI);
            nsManager.AddNamespace("ir68", "http://www.govtalk.gov.uk/taxation/charities/r68/2");

            XmlDocument xmlBody = new XmlDocument();

            xmlBody.PreserveWhitespace = true;
            xmlBody.LoadXml(bodyNode.OuterXml);

            // Remove any existing IRmark
            XmlNode nodeIr            = xmlBody.SelectSingleNode("//ir68:IRmark", nsManager);
            XmlNode irMarkPlaceholder = nodeIr;

            if (nodeIr != null)
            {
                irMarkPlaceholder = nodeIr.PreviousSibling;
                nodeIr.ParentNode.RemoveChild(nodeIr);
            }

            // Normalise the document using C14N (Canonicalisation)
            System.Security.Cryptography.Xml.XmlDsigC14NTransform c14n = new System.Security.Cryptography.Xml.XmlDsigC14NTransform();
            c14n.LoadInput(xmlBody);

            using (Stream stream = (Stream)c14n.GetOutput())
            {
                byte[] buffer = new byte[stream.Length];

                // convert to string and normalise line endings
                stream.Read(buffer, 0, (int)stream.Length);
                text = Encoding.UTF8.GetString(buffer);
                text = text.Replace("&#xD;", "");
                text = text.Replace(vbCrLf, vbLf);

                // convert the final document back into a byte array
                byte[] textBytes = Encoding.UTF8.GetBytes(text);

                //create the SHA-1 hash from the final document
                System.Security.Cryptography.SHA1 sha = System.Security.Cryptography.SHA1.Create();
                byte[] hash = sha.ComputeHash(textBytes);

                result = Convert.ToBase64String(hash);
                _loggingService.LogInfo(typeof(GovTalkMessageHelper), string.Format("IRmark created: {0}", result));
            }

            // attempt to re-insert the IRmark

            XmlNode irMarkNode = root.SelectSingleNode("//*[contains(name(),'IRmark')]");

            if (!String.IsNullOrEmpty(irMarkNode.InnerText))
            {
                root.SelectSingleNode("//*[contains(name(),'IRmark')]").LastChild.Value = result;
            }
            else
            {
                if (root.SelectSingleNode("//*[contains(name(),'IRmark')]") != null)
                {
                    irMarkNode.InnerText = result;
                }
                else
                {
                    _loggingService.LogWarning(typeof(GovTalkMessageHelper), "No IRmark");
                }
            }

            _loggingService.LogInfo(typeof(GovTalkMessageHelper), string.Format("IRmark inserted into XML Document."));

            return(XmlFile);
        }
Beispiel #5
0
        private byte[] GetXDocumentHash(XDocument xdocument)
        {
            MemoryStream memoryStream = new MemoryStream();
            using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream))
            {
                xdocument.WriteTo(xmlWriter);
            }
            memoryStream.Position = 0;

            var xfrm = new System.Security.Cryptography.Xml.XmlDsigC14NTransform(false);
            xfrm.LoadInput(memoryStream);
            byte[] result = xfrm.GetDigestedOutput(new System.Security.Cryptography.SHA1Managed());
            return result;
        }