public static string GetObject2(XmlDocument doc, string id) { var signedXml = new SignedXmlObject(doc) { KeyInfo = new KeyInfo(), SigningKey = (RSA) new RSACryptoServiceProvider() }; var transform1 = new XmlDsigEnvelopedSignatureTransform(); var transform2 = new XmlDsigC14NWithCommentsTransform(); var reference = new Reference { Uri = id }; reference.AddTransform(transform1); reference.AddTransform(transform2); signedXml.AddReference(reference); signedXml.ComputeSignature(); //signedXml. AsymmetricAlgorithm algorithm = new ECDsaCng(); signedXml.CheckSignature(algorithm); var result = new StreamReader((MemoryStream)transform2.GetOutput()).ReadToEnd(); if (!Verify(result, reference.DigestValue)) { throw new Exception("Wrong canonicalization object"); } //using (var file = File.CreateText(id.Replace("#", "canonical2-") + ".txt")) //{ // file.WriteLine(result); //} return(result); }
private string ExecuteXmlDSigC14NTransform(string inputXml, Encoding encoding = null, XmlResolver resolver = null) { XmlDocument doc = new XmlDocument(); doc.XmlResolver = resolver; doc.PreserveWhitespace = true; doc.LoadXml(inputXml); Encoding actualEncoding = encoding ?? Encoding.UTF8; byte[] data = actualEncoding.GetBytes(inputXml); using (Stream stream = new MemoryStream(data)) using (XmlReader reader = XmlReader.Create(stream, new XmlReaderSettings { ValidationType = ValidationType.None, DtdProcessing = DtdProcessing.Parse, XmlResolver = resolver })) { doc.Load(reader); XmlDsigC14NWithCommentsTransform transform = new XmlDsigC14NWithCommentsTransform(); transform.LoadInput(doc); return(TestHelpers.StreamToString((Stream)transform.GetOutput(), actualEncoding)); } }
public void GetOutput_UnsupportedType(Type type) { XmlDsigC14NWithCommentsTransform xmlDsigC14NWithCommentsTransform = new XmlDsigC14NWithCommentsTransform(); AssertExtensions.Throws <ArgumentException>("type", () => xmlDsigC14NWithCommentsTransform.GetOutput(type)); }
/// <summary> /// Takes a document /// </summary> /// <param name="args"></param> /// <returns></returns> public static string AddIRMark(ref XmlDocument originalDoc, string ManifestNameSpace) { try { if ((originalDoc == null) || (originalDoc.ChildNodes.Count == 0)) { return(""); } // Load XML Return originalDoc.PreserveWhitespace = false; // originalDoc.WriteTo(new XmlTextWriter("c:\\OriginalDoc.xml", Encoding.ASCII)); // Step 1. Clone the original document and create a workspace object XmlDocument canonDoc = new XmlDocument(); canonDoc.LoadXml(originalDoc.OuterXml); // We also pick up the name table from the original document XmlNamespaceManager nsMgr = new XmlNamespaceManager(canonDoc.NameTable); // Define namespaces within the document: nsMgr.AddNamespace("ir", ManifestNameSpace); // Define a namespace for the IRheader nsMgr.AddNamespace("gt", "http://www.govtalk.gov.uk/CM/envelope"); // Define a namespace for the GovTalk element XmlNode bodyNode = null; try { // Step 2. Find the Body node and add the GovTalk namespace to it. // Originally was using select single node but this has been replced // with GetElementByTagName as SelectSingleNode has cannot be used // through the COM interface. //bodyNode = canonDoc.SelectSingleNode("//gt:Body", nsMgr); XmlNodeList BodyNodeList = canonDoc.GetElementsByTagName("Body", "http://www.govtalk.gov.uk/CM/envelope"); bodyNode = BodyNodeList[0]; bodyNode.Attributes.Append(canonDoc.CreateAttribute("xmlns")); bodyNode.Attributes["xmlns"].Value = "http://www.govtalk.gov.uk/CM/envelope"; } catch (Exception ex) { throw ex; } // Step 2. Find the IRmark and remove it // Originally was using select single node but this has been replced // with GetElementByTagName as SelectSingleNode has cannot be used // through the COM interface. // XmlNode IRheader = canonDoc.SelectSingleNode("//ir:IRheader", nsMgr); // XmlNode irMarkNode = IRheader.SelectSingleNode("//ir:IRmark", nsMgr); //if (irMarkNode != null) // IRheader.RemoveChild(irMarkNode); XmlNodeList IRHeaderNodeList; // XmlNodeList irMarkNodeList; XmlNode IRheader; XmlNode irMarkNode; IRHeaderNodeList = canonDoc.GetElementsByTagName("IRheader", ManifestNameSpace); IRheader = IRHeaderNodeList[0]; foreach (XmlNode SearchNode in IRheader.ChildNodes) { if (SearchNode.Name == "IRmark") { SearchNode.ParentNode.RemoveChild(SearchNode); } // SearchNode.RemoveAll(); } //Step 4. Generate IRmark canonDoc.PreserveWhitespace = true; // 4a. Extract <Body /> element into an XmlDocument XmlDocument workSpace = new XmlDocument(); workSpace.LoadXml(bodyNode.OuterXml); // 4b. Canonicalise Document XmlDsigC14NWithCommentsTransform transform = new XmlDsigC14NWithCommentsTransform(); transform.LoadInput(workSpace); // 4c. Create SHA1 digest Stream s = (Stream)transform.GetOutput(typeof(Stream)); SHA1 sha1 = SHA1.Create(); // 4d. Compute byte-array hash byte[] hash = sha1.ComputeHash(s); // Step 5. Replace the IRmark in the original document with the computed hash // Originally was using select single node but this has been replced // with GetElementByTagName as SelectSingleNode has cannot be used // through the COM interface. // irMarkNode = originalDoc.SelectSingleNode("//ir:IRmark", nsMgr); XmlNodeList irMarkNodeList2; irMarkNodeList2 = originalDoc.GetElementsByTagName("IRmark", ManifestNameSpace); irMarkNode = irMarkNodeList2[0]; if (irMarkNode == null) { // Add the IRMark at the specified location // XmlNode parentNode = originalDoc.SelectSingleNode("//ir:IRheader"); // XmlNode precedingNode = originalDoc.SelectSingleNode("//ir:Sender"); XmlNodeList parentNodeList; XmlNodeList precedingNodeList; parentNodeList = originalDoc.GetElementsByTagName("IRheader", ManifestNameSpace); precedingNodeList = originalDoc.GetElementsByTagName("Sender", ManifestNameSpace); XmlNode parentNode = parentNodeList[0]; XmlNode precedingNode = precedingNodeList[0]; irMarkNode = originalDoc.CreateElement("IRmark"); parentNode.InsertBefore(irMarkNode, precedingNode); } // 5a. Create the "Type" attribute if it does not exist bool found = false; foreach (XmlAttribute attr in irMarkNode.Attributes) { if (attr.Name == "Type") { found = true; break; } } if (!found) { irMarkNode.Attributes.Prepend(originalDoc.CreateAttribute("Type")); } // 5b. Assign the "Type" attribute the value of "generic" irMarkNode.Attributes["Type"].Value = "generic"; // 5c. Convert the IRmark byte-array to a Base-64 string and set the node with this value: irMarkNode.InnerText = Convert.ToBase64String(hash); // 6. Return the IRmark in Base32. return(IRMark32.ToBase32String(hash)); } catch // (Exception ex) { return("Failed the genration of the IR Mark"); } // The following code is unreachable and causes a compiler warning // return "Failed the genration of the IR Mark"; }