public static XmlDocument AssinarXML(this XmlDocument xmlDocument, string tagAssinatura) { var certificado = Config.Certificado; var reference = new System.Security.Cryptography.Xml.Reference { Uri = "" }; var signedXml = new System.Security.Cryptography.Xml.SignedXml(xmlDocument) { SigningKey = certificado.PrivateKey }; reference.AddTransform(new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform()); reference.AddTransform(new System.Security.Cryptography.Xml.XmlDsigC14NTransform()); signedXml.AddReference(reference); var keyInfo = new System.Security.Cryptography.Xml.KeyInfo(); keyInfo.AddClause(new System.Security.Cryptography.Xml.KeyInfoX509Data(certificado)); signedXml.KeyInfo = keyInfo; signedXml.ComputeSignature(); var xmlDigitalSignature = signedXml.GetXml(); xmlDocument.GetElementsByTagName(tagAssinatura)[0].AppendChild(xmlDocument.ImportNode(xmlDigitalSignature, true)); return(xmlDocument); }
/// <summary> /// Creates the metadata document. /// </summary> /// <param name="context">The context.</param> /// <param name="sign">if set to <c>true</c> sign the document.</param> public string CreateMetadataDocument(Encoding encoding, bool sign) { logger.Debug(TraceMessages.MetadataDocumentBeingCreated); var keyinfo = new System.Security.Cryptography.Xml.KeyInfo(); var keyClause = new System.Security.Cryptography.Xml.KeyInfoX509Data(configuration.ServiceProvider.SigningCertificate, X509IncludeOption.EndCertOnly); keyinfo.AddClause(keyClause); var doc = new Saml20MetadataDocument(configuration, keyinfo, sign); logger.Debug(TraceMessages.MetadataDocumentCreated); return doc.ToXml(encoding, configuration.ServiceProvider.SigningCertificate); }
/// <summary> /// Creates the metadata document. /// </summary> /// <param name="context">The context.</param> /// <param name="sign">if set to <c>true</c> sign the document.</param> public string CreateMetadataDocument(Encoding encoding, bool sign) { logger.Debug(TraceMessages.MetadataDocumentBeingCreated); var keyinfo = new System.Security.Cryptography.Xml.KeyInfo(); var keyClause = new System.Security.Cryptography.Xml.KeyInfoX509Data(configuration.ServiceProvider.SigningCertificate, X509IncludeOption.EndCertOnly); keyinfo.AddClause(keyClause); var doc = new Saml20MetadataDocument(configuration, keyinfo, sign); logger.Debug(TraceMessages.MetadataDocumentCreated); return(doc.ToXml(encoding, configuration.ServiceProvider.SigningCertificate)); }
public void LoadXml(XmlElement value) { if (value == null) { throw new ArgumentNullException("value"); } XmlElement element = value; if (!element.LocalName.Equals("Signature")) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "Signature"); } this.m_id = System.Security.Cryptography.Xml.Utils.GetAttribute(element, "Id", "http://www.w3.org/2000/09/xmldsig#"); XmlNamespaceManager nsmgr = new XmlNamespaceManager(value.OwnerDocument.NameTable); nsmgr.AddNamespace("ds", "http://www.w3.org/2000/09/xmldsig#"); XmlElement element2 = element.SelectSingleNode("ds:SignedInfo", nsmgr) as XmlElement; if (element2 == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "SignedInfo"); } this.SignedInfo = new System.Security.Cryptography.Xml.SignedInfo(); this.SignedInfo.LoadXml(element2); XmlElement element3 = element.SelectSingleNode("ds:SignatureValue", nsmgr) as XmlElement; if (element3 == null) { throw new CryptographicException(SecurityResources.GetResourceString("Cryptography_Xml_InvalidElement"), "SignedInfo/SignatureValue"); } this.m_signatureValue = Convert.FromBase64String(System.Security.Cryptography.Xml.Utils.DiscardWhiteSpaces(element3.InnerText)); this.m_signatureValueId = System.Security.Cryptography.Xml.Utils.GetAttribute(element3, "Id", "http://www.w3.org/2000/09/xmldsig#"); XmlNodeList list = element.SelectNodes("ds:KeyInfo", nsmgr); this.m_keyInfo = new System.Security.Cryptography.Xml.KeyInfo(); if (list != null) { foreach (XmlNode node in list) { XmlElement element4 = node as XmlElement; if (element4 != null) { this.m_keyInfo.LoadXml(element4); } } } XmlNodeList list2 = element.SelectNodes("ds:Object", nsmgr); this.m_embeddedObjects.Clear(); if (list2 != null) { foreach (XmlNode node2 in list2) { XmlElement element5 = node2 as XmlElement; if (element5 != null) { DataObject obj2 = new DataObject(); obj2.LoadXml(element5); this.m_embeddedObjects.Add(obj2); } } } XmlNodeList list3 = element.SelectNodes("//*[@Id]", nsmgr); if (list3 != null) { foreach (XmlNode node3 in list3) { this.m_referencedItems.Add(node3); } } }