0

I tried and read a lot (im new about signin xml documents) So, I try to sing the factura element in the next examle

<?xml version="1.0" encoding="utf-8"?>
<factura id="comprobante" version="1.1.0">
  <data>
  </data>
</factura>

But it doesn't work, I notice that if I put the element into another element it work

<xml>
  <factura>

Something like that, but i need to be only the factura element

This is my code, and if anyone ask why I create another SignedXml, is a solution to sign the KeyInfo. The method "AddXAdEsProperties" only create the DataObject psdt: If something in my code looks like terribly wrong or my english, pls forgive me.

public static XmlElement SignWithXAdES(X509Certificate2 signingCertificate, XmlDocument xmlDocument)
        {
            var signedXml = new SignedXml(xmlDocument.DocumentElement);
            signedXml.Signature.Id = SignatureId;
            signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigCanonicalizationUrl;
            signedXml.SigningKey = signingCertificate.GetRSAPrivateKey();
            var keyInfo = new KeyInfo();
            keyInfo.AddClause(new KeyInfoX509Data(signingCertificate));
            keyInfo.AddClause(new RSAKeyValue(signingCertificate.GetRSAPublicKey()));
            keyInfo.Id = "Cert1439";
            signedXml.Signature.KeyInfo = keyInfo;
            AddXAdESProperties(xmlDocument, signedXml, signingCertificate);
            SignedInfo signedInfo = signedXml.SignedInfo;
            signedInfo.Id = SignatureId + "-SignedInfo";
            signedInfo.CanonicalizationMethod = SignedXml.XmlDsigC14NTransformUrl;
            signedInfo.SignatureMethod = SignedXml.XmlDsigRSASHA1Url;
            var comprobante = new Reference("#comprobante")
            {
                Id = "RefComprobante",
                DigestMethod = SignedXml.XmlDsigSHA1Url
            };
            comprobante.AddTransform(new XmlDsigEnvelopedSignatureTransform()); 
            signedXml.AddReference(comprobante);
            var signedPropertiesId = new Reference($"#{SignatureId}-{SignatureProperties}")
            {
                Id = SignaturePropertiesId,
                Type = XadesSignedXml.XmlDsigSignatureProperties,
                DigestMethod = SignedXml.XmlDsigSHA1Url
            };
            signedXml.AddReference(signedPropertiesId);
            var certificado = new Reference("#" + signedXml.KeyInfo.Id)
            {
                DigestMethod = SignedXml.XmlDsigSHA1Url
            };
            signedXml.AddReference(certificado);
            SignedXml tmp = new SignedXml(xmlDocument)
            {
                SigningKey = signedXml.SigningKey,
                KeyInfo = signedXml.KeyInfo,
            };
            foreach (DataObject obj in signedXml.Signature.ObjectList)
            {
                tmp.AddObject(obj);
            }
            tmp.AddReference(comprobante);
            tmp.ComputeSignature();
            XmlElement elem = tmp.GetXml();
            xmlDocument.DocumentElement.AppendChild(elem);
            signedXml.ComputeSignature();
            xmlDocument.DocumentElement.RemoveChild(elem);
            var firmado = signedXml.GetXml();
            return firmado;
        }

This is my code to check the signature and like I said, if I add a parent node to ir. It works

            var sgn2 = new SignedXml(doc.DocumentElement);
            sgn2.LoadXml(firmado);

            bool bandera = sgn2.CheckSignature(certificado,true);

Tks a lot for the people who help me...

0 Answers0