void CheckProperties (XmlDsigXsltTransform transform)
		{
			Assert.AreEqual ("http://www.w3.org/TR/1999/REC-xslt-19991116", transform.Algorithm, "Algorithm");

			Type[] input = transform.InputTypes;
			Assert.IsTrue ((input.Length == 3), "Input #");
			// check presence of every supported input types
			bool istream = false;
			bool ixmldoc = false;
			bool ixmlnl = false;
			foreach (Type t in input) {
				if (t.ToString () == "System.IO.Stream")
					istream = true;
				if (t.ToString () == "System.Xml.XmlDocument")
					ixmldoc = true;
				if (t.ToString () == "System.Xml.XmlNodeList")
					ixmlnl = true;
			}
			Assert.IsTrue (istream, "Input Stream");
			Assert.IsTrue (ixmldoc, "Input XmlDocument");
			Assert.IsTrue (ixmlnl, "Input XmlNodeList");

			Type[] output = transform.OutputTypes;
			Assert.IsTrue ((output.Length == 1), "Output #");
			// check presence of every supported output types
			bool ostream = false;
			foreach (Type t in output) {
				if (t.ToString () == "System.IO.Stream")
					ostream = true;
			}
			Assert.IsTrue (ostream, "Output Stream");
		}
		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);
		}
Exemple #3
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");
		}
        private static XmlDsigXsltTransform CreateDataTransform()
        {
            var dataTransformDocument = new XmlDocument();

            dataTransformDocument.LoadXml(@"
                <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform' xmlns:ds='http://www.w3.org/2000/09/xmldsig#'>
                    <xsl:template match='/'>
                        <xsl:apply-templates />
                    </xsl:template>
                    <xsl:template match='*'>
                        <xsl:copy>
                            <xsl:copy-of select='@*' />
                            <xsl:apply-templates />
                        </xsl:copy>
                    </xsl:template>
                    <xsl:template match='ds:Signature' />
                </xsl:stylesheet>");

            var dataTransform = new XmlDsigXsltTransform();
            dataTransform.LoadInnerXml(dataTransformDocument.ChildNodes);

            return dataTransform;
        }
		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;
			}
		}
Exemple #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;
                }
            }
        }
Exemple #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;
		}
		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;
			}
		}
        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;
        }