public void ExtensionMethods_EncodeForXmlDocument_WhenInvalidXmlDocument_ExpectException()
        {
            //------------Setup for test--------------------------
            var sb = new StringBuilder("aa");
            //------------Execute Test---------------------------

            sb.EncodeForXmlDocument();
        }
        public void ExtensionMethods_EncodeForXmlDocument_WhenValidUnicodeXmlDocument_ExpectStream()
        {
            //------------Setup for test--------------------------
            byte[] bytes = { (byte)'<', (byte)'x', (byte)'/', (byte)'>' };

            var msg = Encoding.Unicode.GetString(bytes);
            var sb = new StringBuilder(msg);
            //------------Execute Test---------------------------

            using(var result = sb.EncodeForXmlDocument())
            {

                //------------Assert Results-------------------------
                Assert.IsNotNull(result);
                Assert.AreEqual(0, result.Position);
            }
        }
        public void ExtensionMethods_EncodeForXmlDocument_WhenValidUTF8XmlDocument_ExpectStream()
        {
            //------------Setup for test--------------------------
            const string msg = "<x>test message</x>";
            var sb = new StringBuilder(msg);
            //------------Execute Test---------------------------

            using(var result = sb.EncodeForXmlDocument())
            {

                //------------Assert Results-------------------------
                Assert.IsNotNull(result);
                Assert.AreEqual(0, result.Position);
            }
        }
        public StringBuilder SignXml(StringBuilder xml)
        {
           
            if (xml == null || xml.Length == 0)
            {
                throw new ArgumentNullException("xml");
            }

            // remove the signature element here as it does not pick up correctly futher down ;(
            xml = RemoveSignature(xml);

            using(var s = xml.EncodeForXmlDocument())
            {
                var doc = new XmlDocument();
               
                doc.Load(s);

                SetServerID(doc);

                var result = new StringBuilder();
                using (var sw = new StringWriter(result))
                {
                    doc.Save(sw);
                }

                // remove the crapy encoding header
                result = result.CleanEncodingHeaderForXmlSave();

                return result;
            }
        }
Example #5
0
        public bool VerifyXml(StringBuilder xml)
        {
            if (xml == null || xml.Length == 0)
            {
                throw new ArgumentNullException("xml");
            }

            using(Stream s = xml.EncodeForXmlDocument())
            {
                var doc = new XmlDocument();
                doc.Load(s);

                // Validate server ID, this is a check which can be done quickly in order to skip loading the whole file for verification        
                var serverID = GetServerID(doc);

                /*
                 * NOTE : 
                 * 
                 * This magical check is here for shipping resources
                 * It enables the server on first start to resign the resource such that
                 * the end user's install can view and execute them ;)
                 * 
                 * To ship a resource you need to do the following : 
                 * 
                 * 1) Set the type to Unknown
                 * 2) Give the resource a server ID of our InternalServerID
                 * 3) Remove any existing signing data 
                 * 
                 */
                if (serverID != ServerID && serverID != InternalServerID)
                {
                    return false;
                }

                // Find the "Signature" node and add it to the SignedXml object
                var signedXml = new SignedXml(doc);
                var nodeList = doc.GetElementsByTagName("Signature");

                // allow unsigned resources with our internal server ID
                if (nodeList.Count == 0 && serverID == InternalServerID)
                {
                    return true;
                }

                signedXml.LoadXml((XmlElement) nodeList[0]);


                var result = (serverID == ServerID && signedXml.CheckSignature(_serverKey)) ||
                             (serverID != InternalServerID == signedXml.CheckSignature(_systemKey));


                // Check if signed by the server or the system
                return result;
            }
        }
Example #6
0
        public StringBuilder SignXml(StringBuilder xml)
        {
           
            if (xml == null || xml.Length == 0)
            {
                throw new ArgumentNullException("xml");
            }

            // remove the signature element here as it does not pick up correctly futher down ;(
            xml = RemoveSignature(xml);

            using(Stream s = xml.EncodeForXmlDocument())
            {
                var doc = new XmlDocument();
               
                doc.Load(s);

                SetServerID(doc);

                // Create a reference to be signed and add
                // an enveloped transformation to the reference.
                var reference = new Reference
                {
                    Uri = ""
                };

                reference.AddTransform(new XmlDsigEnvelopedSignatureTransform());

                var signedXml = new SignedXml(doc)
                {
                    SigningKey = _serverKey
                };
                signedXml.AddReference(reference);
                signedXml.ComputeSignature();

                // Get the XML representation of the signature and save
                // it to an XmlElement object.
                var xmlDigitalSignature = signedXml.GetXml();

                // Append the element to the XML document.
                if (doc.DocumentElement != null)
                {
                    doc.DocumentElement.AppendChild(doc.ImportNode(xmlDigitalSignature, true));
                }

                StringBuilder result = new StringBuilder();
                using (StringWriter sw = new StringWriter(result))
                {
                    doc.Save(sw);
                }

                // remove the crapy encoding header
                result = result.CleanEncodingHeaderForXmlSave();

                return result;
            }
        }