public void FullChain () 
		{
			TransformChain chain = new TransformChain ();

			XmlDsigBase64Transform base64 = new XmlDsigBase64Transform ();
			chain.Add (base64);
			AssertEquals ("XmlDsigBase64Transform", base64, chain[0]);
			AssertEquals ("count 1", 1, chain.Count);

			XmlDsigC14NTransform c14n = new XmlDsigC14NTransform ();
			chain.Add (c14n);
			AssertEquals ("XmlDsigC14NTransform", c14n, chain[1]);
			AssertEquals ("count 2", 2, chain.Count);

			XmlDsigC14NWithCommentsTransform c14nc = new XmlDsigC14NWithCommentsTransform ();
			chain.Add (c14nc);
			AssertEquals ("XmlDsigC14NWithCommentsTransform", c14nc, chain[2]);
			AssertEquals ("count 3", 3, chain.Count);

			XmlDsigEnvelopedSignatureTransform esign = new XmlDsigEnvelopedSignatureTransform ();
			chain.Add (esign);
			AssertEquals ("XmlDsigEnvelopedSignatureTransform", esign, chain[3]);
			AssertEquals ("count 4", 4, chain.Count);

			XmlDsigXPathTransform xpath = new XmlDsigXPathTransform ();
			chain.Add (xpath);
			AssertEquals ("XmlDsigXPathTransform", xpath, chain[4]);
			AssertEquals ("count 5", 5, chain.Count);

			XmlDsigXsltTransform xslt = new XmlDsigXsltTransform ();
			chain.Add (xslt);
			AssertEquals ("XmlDsigXsltTransform", xslt, chain[5]);
			AssertEquals ("count 6", 6, chain.Count);
		}
Example #2
0
		public void FullChain () 
		{
			TransformChain chain = new TransformChain ();

			XmlDsigBase64Transform base64 = new XmlDsigBase64Transform ();
			chain.Add (base64);
			Assert.AreEqual (base64, chain[0], "XmlDsigBase64Transform");
			Assert.AreEqual (1, chain.Count, "count 1");

			XmlDsigC14NTransform c14n = new XmlDsigC14NTransform ();
			chain.Add (c14n);
			Assert.AreEqual (c14n, chain[1], "XmlDsigC14NTransform");
			Assert.AreEqual (2, chain.Count, "count 2");

			XmlDsigC14NWithCommentsTransform c14nc = new XmlDsigC14NWithCommentsTransform ();
			chain.Add (c14nc);
			Assert.AreEqual (c14nc, chain[2], "XmlDsigC14NWithCommentsTransform");
			Assert.AreEqual (3, chain.Count, "count 3");

			XmlDsigEnvelopedSignatureTransform esign = new XmlDsigEnvelopedSignatureTransform ();
			chain.Add (esign);
			Assert.AreEqual (esign, chain[3], "XmlDsigEnvelopedSignatureTransform");
			Assert.AreEqual (4, chain.Count, "count 4");

			XmlDsigXPathTransform xpath = new XmlDsigXPathTransform ();
			chain.Add (xpath);
			Assert.AreEqual (xpath, chain[4], "XmlDsigXPathTransform");
			Assert.AreEqual (5, chain.Count, "count 5");

			XmlDsigXsltTransform xslt = new XmlDsigXsltTransform ();
			chain.Add (xslt);
			Assert.AreEqual (xslt, chain[5], "XmlDsigXsltTransform");
			Assert.AreEqual (6, chain.Count, "count 6");
		}
		public override void LoadXml (XmlElement value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			if ((value.LocalName != XmlEncryption.ElementNames.CipherReference) || (value.NamespaceURI != EncryptedXml.XmlEncNamespaceUrl))
				throw new CryptographicException ("Malformed CipherReference element.");
			else {
				Uri = null;
				TransformChain = new TransformChain ();

				foreach (XmlNode n in value.ChildNodes) {
					if (n is XmlWhitespace)
						continue;

					switch (n.LocalName) {
					case XmlEncryption.ElementNames.Transforms:
						foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
							Transform t = null;
							switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
							case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
								t = new XmlDsigBase64Transform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
								t = new XmlDsigC14NTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
								t = new XmlDsigC14NWithCommentsTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
								t = new XmlDsigEnvelopedSignatureTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
								t = new XmlDsigXPathTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
								t = new XmlDsigXsltTransform ();
								break;
#if NET_2_0
							case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform:
								t = new XmlDecryptionTransform ();
								break;
#endif
							default:
								continue;
							}

							t.LoadInnerXml (((XmlElement) xn).ChildNodes);
							TransformChain.Add (t);
						}
						break;
					}
				}
						
				if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
					Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
			}
		}
		public void FunctionHereObsolete ()
		{
			XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
			XmlDocument xpdoc = new XmlDocument ();
			string ns = "http://www.w3.org/2000/09/xmldsig#";
//			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
			xpdoc.LoadXml (xpath);
			t.LoadInnerXml (xpdoc.ChildNodes);
			XmlDocument doc = new XmlDocument ();

			doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			// FX 2.0 throws at next line!
			XmlNodeList nl = (XmlNodeList) t.GetOutput ();
			AssertEquals (0, nl.Count);

			doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
			AssertEquals (1, nl.Count);

			doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
			AssertEquals (2, nl.Count);

			doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
			AssertEquals (3, nl.Count);

			doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
			AssertEquals (1, nl.Count);
			AssertEquals (XmlNodeType.Attribute, nl [0].NodeType);
		}
		public void TransformSimple ()
		{
			XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
			XmlDocument xpdoc = new XmlDocument ();
			string ns = "http://www.w3.org/2000/09/xmldsig#";
			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>*|@*|namespace::*</XPath>"; // not absolute path.. so @* and namespace::* does not make sense.
			xpdoc.LoadXml (xpath);
			t.LoadInnerXml (xpdoc.ChildNodes);
			XmlDocument doc = new XmlDocument ();
			doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			XmlNodeList nl = (XmlNodeList) t.GetOutput ();
			AssertEquals (XmlNodeType.Document, nl [0].NodeType);
			AssertEquals (XmlNodeType.Element, nl [1].NodeType);
			AssertEquals ("element", nl [1].LocalName);
			AssertEquals (XmlNodeType.Element, nl [2].NodeType);
			AssertEquals ("foo", nl [2].LocalName);
			AssertEquals (XmlNodeType.Element, nl [3].NodeType);
			AssertEquals ("bar", nl [3].LocalName);
			// MS.NET bug - ms.net returns ns node even when the
			// current node is ns node (it is like returning
			// attribute from attribute nodes).
//			AssertEquals (XmlNodeType.Attribute, nl [4].NodeType);
//			AssertEquals ("xmlns", nl [4].LocalName);
		}
Example #6
0
        public virtual void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }
            else
            {
                Uri            = null;
                TransformChain = new TransformChain();

                foreach (XmlNode n in value.ChildNodes)
                {
                    if (n is XmlWhitespace)
                    {
                        continue;
                    }

                    switch (n.LocalName)
                    {
                    case XmlEncryption.ElementNames.Transforms:
                        foreach (XmlNode xn in ((XmlElement)n).GetElementsByTagName(XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI))
                        {
                            Transform t = null;
                            switch (((XmlElement)xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value)
                            {
                            case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
                                t = new XmlDsigBase64Transform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
                                t = new XmlDsigC14NTransform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
                                t = new XmlDsigC14NWithCommentsTransform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
                                t = new XmlDsigEnvelopedSignatureTransform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
                                t = new XmlDsigXPathTransform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
                                t = new XmlDsigXsltTransform();
                                break;

#if NET_2_0
                            case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform:
                                t = new XmlDsigExcC14NTransform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform:
                                t = new XmlDsigExcC14NWithCommentsTransform();
                                break;

                            case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform:
                                t = new XmlDecryptionTransform();
                                break;
#endif
                            default:
                                continue;
                            }

                            t.LoadInnerXml(((XmlElement)xn).ChildNodes);
                            TransformChain.Add(t);
                        }
                        break;
                    }
                }

                if (value.HasAttribute(XmlEncryption.AttributeNames.URI))
                {
                    Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
                }
            }
        }
Example #7
0
		public void LoadXml (XmlElement value) 
		{
			if (value == null)
				throw new ArgumentNullException ("value");

			if ((value.LocalName != XmlSignature.ElementNames.Reference) || (value.NamespaceURI != XmlSignature.NamespaceURI))
				throw new CryptographicException ();

			id = GetAttribute (value, XmlSignature.AttributeNames.Id);
			uri = GetAttribute (value, XmlSignature.AttributeNames.URI);
			type = GetAttribute (value, XmlSignature.AttributeNames.Type);
			// Note: order is important for validations
			XmlNodeList xnl = value.GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI);
			if ((xnl != null) && (xnl.Count > 0)) {
				Transform t = null;
				foreach (XmlNode xn in xnl) {
					string a = GetAttribute ((XmlElement)xn, XmlSignature.AttributeNames.Algorithm);
/*	This code is useful for debugging in VS.NET because using CryptoConfig
	(from MS mscorlib) would throw InvalidCastException because it's 
	Transform would come from MS System.Security.dll not Mono's.*/
					switch (a) {
						case "http://www.w3.org/2000/09/xmldsig#base64":
							t = new XmlDsigBase64Transform ();
							break;
						case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315":
							t = new XmlDsigC14NTransform ();
							break;
						case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments":
							t = new XmlDsigC14NWithCommentsTransform ();
							break;
						case "http://www.w3.org/2000/09/xmldsig#enveloped-signature":
							t = new XmlDsigEnvelopedSignatureTransform ();
							break;
						case "http://www.w3.org/TR/1999/REC-xpath-19991116":
							t = new XmlDsigXPathTransform ();
							break;
						case "http://www.w3.org/TR/1999/REC-xslt-19991116":
							t = new XmlDsigXsltTransform ();
							break;
						case "http://www.w3.org/2002/07/decrypt#XML":
							t = new XmlDecryptionTransform ();
							break;
						case "http://www.w3.org/2001/10/xml-exc-c14n#":
							t = new XmlDsigExcC14NTransform ();
							break;
						default:
							throw new NotSupportedException ();
					}

					if (t == null)
						t = (Transform) CryptoConfig.CreateFromName (a);
					if (t == null)
						throw new CryptographicException ("Unknown transform {0}.", a);

					if (xn.ChildNodes.Count > 0) {
						t.LoadInnerXml (xn.ChildNodes);
					}
					AddTransform (t);
				}
			}
			// get DigestMethod
			DigestMethod = XmlSignature.GetAttributeFromElement (value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.DigestMethod);
			// get DigestValue
			XmlElement dig = XmlSignature.GetChildElement (value, XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);
			if (dig != null)
				DigestValue = Convert.FromBase64String (dig.InnerText);
			element = value;
		}
Example #8
0
		public virtual void LoadXml (XmlElement value)
		{
			if (value == null)
				throw new ArgumentNullException ("value");
			else {
				Uri = null;
				TransformChain = new TransformChain ();

				foreach (XmlNode n in value.ChildNodes) {
					if (n is XmlWhitespace)
						continue;

					switch (n.LocalName) {
					case XmlEncryption.ElementNames.Transforms:
						foreach (XmlNode xn in ((XmlElement) n).GetElementsByTagName (XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI)) {
							Transform t = null;
							switch (((XmlElement) xn).Attributes [XmlSignature.AttributeNames.Algorithm].Value) {
							case XmlSignature.AlgorithmNamespaces.XmlDsigBase64Transform:
								t = new XmlDsigBase64Transform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigC14NTransform:
								t = new XmlDsigC14NTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigC14NWithCommentsTransform:
								t = new XmlDsigC14NWithCommentsTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigEnvelopedSignatureTransform:
								t = new XmlDsigEnvelopedSignatureTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigXPathTransform:
								t = new XmlDsigXPathTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigXsltTransform:
								t = new XmlDsigXsltTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NTransform:
								t = new XmlDsigExcC14NTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDsigExcC14NWithCommentsTransform:
								t = new XmlDsigExcC14NWithCommentsTransform ();
								break;
							case XmlSignature.AlgorithmNamespaces.XmlDecryptionTransform:
								t = new XmlDecryptionTransform ();
								break;
							default:
								continue;
							}

							t.LoadInnerXml (((XmlElement) xn).ChildNodes);
							TransformChain.Add (t);
						}
						break;
					}
				}

				if (value.HasAttribute (XmlEncryption.AttributeNames.URI))
					Uri = value.Attributes [XmlEncryption.AttributeNames.URI].Value;
			}
		}
		// MS.NET looks incorrect, or something incorrect in this test code; It turned out nothing to do with function here()
		public void FunctionHereObsolete ()
		{
			XmlDsigXPathTransform t = new XmlDsigXPathTransform ();
			XmlDocument xpdoc = new XmlDocument ();
			string ns = "http://www.w3.org/2000/09/xmldsig#";
//			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'>here()</XPath>";
			string xpath = "<XPath xmlns='" + ns + "' xmlns:x='urn:foo'></XPath>";
			xpdoc.LoadXml (xpath);
			t.LoadInnerXml (xpdoc.ChildNodes);
			XmlDocument doc = new XmlDocument ();

			doc.LoadXml ("<element a='b'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);

			XmlNodeList nl = (XmlNodeList) t.GetOutput ();
			Assert.AreEqual (0, nl.Count, "0");

			doc.LoadXml ("<element xmlns='urn:foo'><foo><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "1");
#else
			Assert.AreEqual (1, nl.Count, "1");
#endif

			doc.LoadXml ("<element xmlns='urn:foo'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "2");
#else
			Assert.AreEqual (2, nl.Count, "2");
#endif

			doc.LoadXml ("<element xmlns='urn:foo' xmlns:x='urn:x'><foo xmlns='urn:bar'><bar>test</bar></foo></element>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "3");
#else
			Assert.AreEqual (3, nl.Count, "3");
#endif

			doc.LoadXml ("<envelope><Signature xmlns='http://www.w3.org/2000/09/xmldsig#'><XPath>blah</XPath></Signature></envelope>");
			t.LoadInput (doc);
			nl = (XmlNodeList) t.GetOutput ();
#if NET_2_0
			Assert.AreEqual (0, nl.Count, "4");
#else
			Assert.AreEqual (1, nl.Count, "4");
			Assert.AreEqual (XmlNodeType.Attribute, nl [0].NodeType, "NodeType");
#endif
		}
Example #10
0
 private static XmlDsigXPathTransform CreateXPathTransform(string xPathString)
 {
     var doc = new XmlDocument();
     doc.LoadXml("<XPath xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\"></XPath>");
     var xPathElem = (XmlElement) doc.SelectSingleNode("XPath");
     xPathElem.InnerText = xPathString;
     var xForm = new XmlDsigXPathTransform();
     xForm.LoadInnerXml(xPathElem.SelectNodes("."));
     return xForm;
 }
Example #11
0
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((value.LocalName != XmlSignature.ElementNames.Reference) || (value.NamespaceURI != XmlSignature.NamespaceURI))
            {
                throw new CryptographicException();
            }

            id   = GetAttribute(value, XmlSignature.AttributeNames.Id);
            uri  = GetAttribute(value, XmlSignature.AttributeNames.URI);
            type = GetAttribute(value, XmlSignature.AttributeNames.Type);
            // Note: order is important for validations
            XmlNodeList xnl = value.GetElementsByTagName(XmlSignature.ElementNames.Transform, XmlSignature.NamespaceURI);

            if ((xnl != null) && (xnl.Count > 0))
            {
                Transform t = null;
                foreach (XmlNode xn in xnl)
                {
                    string a = GetAttribute((XmlElement)xn, XmlSignature.AttributeNames.Algorithm);

/*	This code is useful for debugging in VS.NET because using CryptoConfig
 *      (from MS mscorlib) would throw InvalidCastException because it's
 *      Transform would come from MS System.Security.dll not Mono's.*/
                    switch (a)
                    {
                    case "http://www.w3.org/2000/09/xmldsig#base64":
                        t = new XmlDsigBase64Transform();
                        break;

                    case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315":
                        t = new XmlDsigC14NTransform();
                        break;

                    case "http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments":
                        t = new XmlDsigC14NWithCommentsTransform();
                        break;

                    case "http://www.w3.org/2000/09/xmldsig#enveloped-signature":
                        t = new XmlDsigEnvelopedSignatureTransform();
                        break;

                    case "http://www.w3.org/TR/1999/REC-xpath-19991116":
                        t = new XmlDsigXPathTransform();
                        break;

                    case "http://www.w3.org/TR/1999/REC-xslt-19991116":
                        t = new XmlDsigXsltTransform();
                        break;

                    case "http://www.w3.org/2002/07/decrypt#XML":
                        t = new XmlDecryptionTransform();
                        break;

                    case "http://www.w3.org/2001/10/xml-exc-c14n#":
                        t = new XmlDsigExcC14NTransform();
                        break;

                    default:
                        throw new NotSupportedException();
                    }

                    if (t == null)
                    {
                        t = (Transform)CryptoConfig.CreateFromName(a);
                    }
                    if (t == null)
                    {
                        throw new CryptographicException("Unknown transform {0}.", a);
                    }

                    if (xn.ChildNodes.Count > 0)
                    {
                        t.LoadInnerXml(xn.ChildNodes);
                    }
                    AddTransform(t);
                }
            }
            // get DigestMethod
            DigestMethod = XmlSignature.GetAttributeFromElement(value, XmlSignature.AttributeNames.Algorithm, XmlSignature.ElementNames.DigestMethod);
            // get DigestValue
            XmlElement dig = XmlSignature.GetChildElement(value, XmlSignature.ElementNames.DigestValue, XmlSignature.NamespaceURI);

            if (dig != null)
            {
                DigestValue = Convert.FromBase64String(dig.InnerText);
            }
            element = value;
        }
Example #12
0
        // Create the XML that represents the transform. 
        private static XmlDsigXPathTransform CreateXPathTransform(string xpath, IDictionary<string, string> namespaces)
        {
            // create the XML that represents the transform
            XmlDocument doc = new XmlDocument();

            XmlElement xpathElem = doc.CreateElement("XPath");
            xpathElem.InnerText = xpath;

            // Add the namespaces that should be in scope for the XPath expression
            if (namespaces != null)
            {
                foreach (string ns in namespaces.Keys)
                {
                    XmlAttribute nsAttr = doc.CreateAttribute("xmlns", ns, "http://www.w3.org/2000/xmlns/");
                    nsAttr.Value = namespaces[ns];
                    xpathElem.Attributes.Append(nsAttr);
                }
            }

            // Build a transform from the inputs
            XmlDsigXPathTransform xpathTransform = new XmlDsigXPathTransform();
            xpathTransform.LoadInnerXml(xpathElem.SelectNodes("."));

            return xpathTransform;
        }