Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
        // Sign an XML file.
        // This document cannot be verified unless the verifying
        // code has the key with which it was signed.
        public static void SignXml(System.Xml.XmlDocument Doc, RSA Key)
        {
            // Check arguments.
            if (Doc == null)
            {
                throw new ArgumentException("Doc");
            }
            if (Key == null)
            {
                throw new ArgumentException("Key");
            }

            // Create a SignedXml object.
            var signedXml = new System.Security.Cryptography.Xml.SignedXml(Doc);

            // Add the key to the SignedXml document.
            signedXml.SigningKey = Key;

            // Create a reference to be signed.
            var reference = new System.Security.Cryptography.Xml.Reference();

            reference.Uri = "";

            // Add an enveloped transformation to the reference.
            var env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform();

            reference.AddTransform(env);

            // Add the reference to the SignedXml object.
            signedXml.AddReference(reference);

            // Compute the signature.
            signedXml.ComputeSignature();

            // Get the XML representation of the signature and save
            // it to an XmlElement object.
            XmlElement xmlDigitalSignature = signedXml.GetXml();

            // Append the element to the XML document.
            Doc.DocumentElement.AppendChild(Doc.ImportNode(xmlDigitalSignature, true));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Carga y parsea un objeto SignedInfo desde un elemento proporcionado
        /// </summary>
        /// <param name="value">Elemento SignedInfo a parsear.</param>
        public void LoadXml(XmlElement value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            if ((value.LocalName != XmlSignatureConstants.ElementNames.SignedInfo) || (value.NamespaceURI != XmlSignatureConstants.NamespaceURI))
            {
                throw new CryptographicException();
            }

            id         = GetAttribute(value, XmlSignatureConstants.AttributeNames.Id);
            c14nMethod = GetAttributeFromElement(value, XmlSignatureConstants.AttributeNames.Algorithm, XmlSignatureConstants.ElementNames.CanonicalizationMethod, XmlSignatureConstants.NamespaceURI);

            XmlNodeList xnlCn = value.GetElementsByTagName(XmlSignatureConstants.ElementNames.CanonicalizationMethod, XmlSignatureConstants.NamespaceURI);

            if (xnlCn.Count > 0)
            {
                XmlNode xn = xnlCn[0];

                var items = ((XmlElement)xn).GetElementsByTagName("InclusiveNamespaces");

                if (items != null && items.Count > 0)
                {
                    inclusiveNamespaces = GetAttribute((XmlElement)items[0], "PrefixList");
                }
            }

            signatureMethod = GetAttributeFromElement(value, XmlSignatureConstants.AttributeNames.Algorithm, XmlSignatureConstants.ElementNames.SignatureMethod, XmlSignatureConstants.NamespaceURI);
            XmlNodeList xnl = value.GetElementsByTagName(XmlSignatureConstants.ElementNames.Reference, XmlSignatureConstants.NamespaceURI);

            foreach (XmlNode xn in xnl)
            {
                System.Security.Cryptography.Xml.Reference r = new System.Security.Cryptography.Xml.Reference();
                r.LoadXml((XmlElement)xn);
                AddReference(r);
            }
        }
Ejemplo n.º 4
0
            /// <summary>
            /// Create a signature xml element for the specified xml document and private key
            /// </summary>
            /// <param name="xmlToSign"></param>
            /// <param name="keyPubPri">Private+public key</param>
            /// <returns></returns>
            public static System.Xml.XmlElement CreateSignature(System.Xml.XmlDocument xmlToSign, string keyPubPri)
            {
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
                rsa.FromXmlString(keyPubPri);

                System.Security.Cryptography.Xml.SignedXml sx = new System.Security.Cryptography.Xml.SignedXml(xmlToSign);
                sx.SigningKey = rsa;

                // Create a reference to be signed
                System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference("");

                // Set the canonicalization method for the document.
                sx.SignedInfo.CanonicalizationMethod = System.Security.Cryptography.Xml.SignedXml.XmlDsigCanonicalizationUrl; // No comments.

                // Add an enveloped transformation to the reference.
                System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform(false);
                reference.AddTransform(env);

                sx.AddReference(reference);

                sx.ComputeSignature();

                return(sx.GetXml());
            }
Ejemplo n.º 5
0
 public void AddReference(System.Security.Cryptography.Xml.Reference reference)
 {
 }
Ejemplo n.º 6
0
            /// <summary>
            /// Create a signature xml element for the specified xml document and private key
            /// </summary>
            /// <param name="xmlToSign"></param>
            /// <param name="keyPubPri">Private+public key</param>
            /// <returns></returns>
            public static System.Xml.XmlElement CreateSignature(System.Xml.XmlDocument xmlToSign, string keyPubPri)
            {
                System.Security.Cryptography.RSACryptoServiceProvider rsa = new System.Security.Cryptography.RSACryptoServiceProvider();
                rsa.FromXmlString(keyPubPri);

                System.Security.Cryptography.Xml.SignedXml sx = new System.Security.Cryptography.Xml.SignedXml(xmlToSign);
                sx.SigningKey = rsa;

                // Create a reference to be signed
                System.Security.Cryptography.Xml.Reference reference = new System.Security.Cryptography.Xml.Reference("");

                // Set the canonicalization method for the document.
                sx.SignedInfo.CanonicalizationMethod = System.Security.Cryptography.Xml.SignedXml.XmlDsigCanonicalizationUrl; // No comments.

                // Add an enveloped transformation to the reference.
                System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform env = new System.Security.Cryptography.Xml.XmlDsigEnvelopedSignatureTransform(false);
                reference.AddTransform(env);

                sx.AddReference(reference);

                sx.ComputeSignature();

                return sx.GetXml();
            }