/// <summary> /// Verifies the specified XML text signature. /// </summary> /// <param name="xmlText">The XML text.</param> /// <param name="certificate">The certificate.</param> ///<returns>True if the signature is valid, false otherwise</returns> public static bool VerifyDocument(string xmlText, X509Certificate2 certificate) { var xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.LoadXml(xmlText); XmlElement signatureElement = XmlSignature.GetElementUnderRoot(xmlDoc.DocumentElement, "Signature", "http://www.w3.org/2000/09/xmldsig#") as XmlElement; var result = XmlSignature.CheckSignature(xmlDoc, certificate, signatureElement); return(result); }
private static XmlElement GetOrCreateContextForEmandatesSignatureAdding(XmlDocument xmlDocument, XmlElement xmlElement) { XmlNode mndtAccptncRptNode = XmlSignature.GetElementUnderRoot(xmlElement, "MndtAccptncRpt"); if (mndtAccptncRptNode == null) { return(null); } // the signature should be placed inside the signed element (more specific, inside a <SplmtryData><Envlp>....</Envlp></SplmtryData> container) string elemPrefix = !String.IsNullOrEmpty(xmlDocument.Prefix) ? xmlDocument.Prefix + ":" : ""; var splmtryDataElements = xmlDocument.GetElementsByTagName("SplmtryData"); var envlpDataElements = xmlDocument.GetElementsByTagName("Envlp"); XmlElement splmtryDataContainer; if (splmtryDataElements.Count == 0) { splmtryDataContainer = xmlDocument.CreateElement(elemPrefix + "SplmtryData", xmlElement.NamespaceURI); // append the 'SplmtryData' element at the end of the signed element mndtAccptncRptNode.AppendChild(splmtryDataContainer); } else { splmtryDataContainer = splmtryDataElements[0] as XmlElement; } XmlElement envlpContainer; if (envlpDataElements.Count == 0) { envlpContainer = xmlDocument.CreateElement(elemPrefix + "Envlp", xmlElement.NamespaceURI); // append the 'Envlp' into the 'SplmtryData' element splmtryDataContainer.AppendChild(envlpContainer); } else { envlpContainer = envlpDataElements[0] as XmlElement; } return(envlpContainer); }
private static XmlElement GetEmandatesSignatureElement(XmlElement xmlElement) { XmlNode mndtAccptncRptElement = XmlSignature.GetElementUnderRoot(xmlElement, "MndtAccptncRpt"); if (mndtAccptncRptElement != null) { XmlNode splmtryDataElement = XmlSignature.GetElementUnderRoot(mndtAccptncRptElement as XmlElement, "SplmtryData"); if (splmtryDataElement != null) { XmlNode envlpElement = XmlSignature.GetElementUnderRoot(splmtryDataElement as XmlElement, "Envlp"); if (envlpElement != null) { var sig = XmlSignature.GetElementUnderRoot(envlpElement as XmlElement, "Signature", "http://www.w3.org/2000/09/xmldsig#") as XmlElement; return(sig); } } } return(null); }