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); }
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"); }
/// <remarks>Sólo se implementan las transformadas de canonicalización</remarks> private Stream SignedInfoTransformed() { System.Security.Cryptography.Xml.Transform t = null; switch (signature.SignedInfo.CanonicalizationMethod) { case XmlSignatureConstants.XmlDsigC14NTransformUrl: t = new System.Security.Cryptography.Xml.XmlDsigC14NTransform(); break; case XmlSignatureConstants.XmlDsigC14NWithCommentsTransformUrl: t = new System.Security.Cryptography.Xml.XmlDsigC14NWithCommentsTransform(); break; case XmlSignatureConstants.XmlDsigExcC14NTransformUrl: t = new System.Security.Cryptography.Xml.XmlDsigExcC14NTransform(signature.SignedInfo.InclusiveNamespaces); break; case XmlSignatureConstants.XmlDsigExcC14NWithCommentsTransformUrl: t = new System.Security.Cryptography.Xml.XmlDsigExcC14NWithCommentsTransform(); break; default: t = null; break; } if (t == null) { return(null); } XmlDocument doc = new XmlDocument(); doc.PreserveWhitespace = true; XmlElement signatureNode = (XmlElement)envdoc.SelectSingleNode("//*[@Id='" + signature.Id + "']"); if (signatureNode != null) { XmlNamespaceManager xmlnsManager = new XmlNamespaceManager(envdoc.NameTable); xmlnsManager.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); XmlNode signedInfoNode = signatureNode.SelectSingleNode("ds:SignedInfo", xmlnsManager); doc.LoadXml(signedInfoNode.OuterXml); } else { doc.LoadXml(signature.SignedInfo.GetXml().OuterXml); } var namespaces = GetAllNamespaces(signatureNode); foreach (var item in namespaces) { AddNameSpace(doc, doc.DocumentElement, item.Name, item.Value); } return(ApplyTransform(t, doc)); }
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 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; } } }
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; }