I develop an application where I digitally sign an XML document, but I don't know how to add the Prefix (xs) to the nodes that the signature generates.
<xs:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
My code:
private string FirmaXadesEPES(XmlDocument xmlDoc, X509Certificate2 certificate,string path)
{
error = string.Empty;
idFirma = IdSignature();
SignedXml signedXml = new SignedXml(xmlDoc);
string URI = "http://uri.etsi.org/01903/v1.3.2#";
try
{
signedXml.Signature.Id = "Id-" + idFirma;
signedXml.SignedInfo.CanonicalizationMethod = SignedXml.XmlDsigExcC14NTransformUrl;
signedXml.SignedInfo.SignatureMethod = "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256";
signedXml.SigningKey = certificate.PrivateKey;
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyInfoX509Data = new KeyInfoX509Data(certificate, X509IncludeOption.ExcludeRoot);
keyInfo.AddClause(keyInfoX509Data);
signedXml.KeyInfo = keyInfo;
//Reference 1
Reference reference = new Reference();
reference.Id = "r-id-" + idFirma;
reference.Type = "";
reference.Uri = "";
XmlDsigXPathTransform XPathTransform = CreateXPathTransform("//. | //@* | //namespace::*", xmlDoc);
reference.AddTransform(XPathTransform);
reference.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
reference.AddTransform(new XmlDsigExcC14NTransform());
signedXml.AddReference(reference);
//Reference 2
Reference reference2 = new Reference();
reference2.Type = "http://uri.etsi.org/01903#SignedProperties";
reference2.Uri = "";
reference2.DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256";
reference2.AddTransform(new XmlDsigExcC14NTransform());
signedXml.AddReference(reference2);
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
bool checkSign = signedXml.CheckSignature();
} catch (Exception ex) { error = ex.ToString(); }
xmlDoc.Save(path);
return error;
}
Generated Signature:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#" Id="Id-37d3fd17-b0f9-48f0-add9-75a52bb9e83c-59880">
<SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
<SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/>
<Reference Id="r-id-37d3fd17-b0f9-48f0-add9-75a52bb9e83c-59880" URI="">
<Transforms>
<Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
<XPath>//. | //@* | //namespace::*</XPath>
</Transform>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>HiOIkatoMGOfHDeUaCFmeWGrYOhHtkItSfRwvc/z5Vo=</DigestValue>
</Reference>
<Reference URI="" Type="http://uri.etsi.org/01903#SignedProperties">
<Transforms>
<Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2001/04/xmlenc#sha256"/>
<DigestValue>HiOIkatoMGOfHDeUaCFmeWGrYOhHtkItSfRwvc/z5Vo=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>P3ElvXAHH1gXbcmiYCyl2OERHeeKOwOER7MFcBHRaYc47rhPmJQl99kiCo4kvXTEEonRVcNYe6jLSFy8bmXxlrLmQ4O5ju/7IOtZqPIcJpOvGQjKlCFw/o3lczynSuSR1sIGWiKe33wV4rahfc5N5dO8PVMSTAz2GgCrDFrVSAg7i4VmAyWoS45qrtgLwh+tIaX8Oc6zSwsiMbJUZ+k9GYFiO9FERoB5ww3AJenylL8wDo4wmGvaBuBjDU0+mgZyeutkgHA7Xge3MQ0QuuGm4FRtYR4itqGq1rnilHmhOxtvqyeNdipg5WKKhuQM55m2eQbdjVFPzC9nJTpIoW7X5g==</SignatureValue>
<KeyInfo>
<X509Data>
<X509Certificate>MIIFajCCA1KgAwIBAgIGAV66vzIOMA0GCSqGSIb3DQEBCwUAMG4xCzAJ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GGMA0GCSqGSIb3DQEBDQUAA4ICAQBrSW828HiLsXmxShXL29gmmOQ0LPwTlqQuD5HRD6ay2SjqxixIdLiImqddfRzSt6SXU0RsQzgEnX2/iREfIdDz0jAH2qgDNu/Z5dCqRGQ3ap/czvrpGMdC3ungvU5keRRRNRTkhhnwsRKo/Kz4FoBrPitJ0INxqy0zLLRo4/AKVQOUa2N0GmhV5t5l9RbJlnlhaEvlbI/swqRlYahgIDooKh2IYhhpX0G0YQsOZ4ZCSlfAomJUnvryVonziOvdHSDFCuVzqokg/EUMJk7RMrZ1M9OTc/6t7Io3n9Se5OWgH1H8k5bcwdG3xeJTRDtEyJQahyk6fXzo3YikhhqubngzNGzrD65t4H/g58stYUJf5mNPoJdgJIjPzbF00l2hkjNneB+gGnWRCfQnFOYDU5WmCjrepX+RIG1BwEuVa113lc3OFPBcNFOY5z2BSJKGQanAJPsFLLGU4foXXdv+cVAN7gKvXFl9kKGShdI07Dfmz2gY0f+L7795ZB8SAhyXgCd/jZH3Fz4ciLrxXnV4/hg7jjZ5dBwH7mFH/6ZD44sMtCC1SVL1mPvWagXlc+DWADdTaHfJCWITWCcQGQAgXeoqqVe7sCwGen07sKvhnt+VOusTmGhg2eOp/wZ37R4ILeGsvhSOB5kHRZnGqUrfPXlkFMgmgbsDSU5frxz49FtvYA==</X509Certificate>
</X509Data>
</KeyInfo>
</Signature>
Add this AddPrefix(XmlNodet n) Method which receives the signature node as a parameter:
and before saving the document in the FirmaXadesEPES() method I add these lines to Invoke it
when you mean prefixing nodes is creating a schema file.
I don't know if this is what you mean but I add this in my xml tags for example.