I want to sign an xml, so it's giving an error: Reference element with incorrect format XMLDSig standard, must also comply with the XAdES-BES standard
i send one exemple
this is my cod
i send one exemple
this is my cod
C#:
static void SignXmlWithCert(XmlDocument doc, X509Certificate2 cert)
{
const string signedPropsIdSuffix = "-signedprops";
var signedXml = new SignedXml(doc)
{
SigningKey = cert.GetRSAPrivateKey()
};
signedXml.SignedInfo.CanonicalizationMethod = "Exclusive XML Canonicalization Version 1.0";
signedXml.SignedInfo.SignatureMethod = "xmldsig-more namespace";
var idKeyInfo = "xmldsig-" + Guid.NewGuid();
var idKeyInfoProps = "#xmldsig-" + Guid.NewGuid() + signedPropsIdSuffix;
var idKeyInfoProps1 = "#xmldsig-" + Guid.NewGuid() + "-keyinfo";
#region keyinfo
var keyInfo = new KeyInfo();
var keydata = new KeyInfoX509Data(cert, X509IncludeOption.None);
keydata.AddIssuerSerial(cert.Issuer, cert.SerialNumber);
keyInfo.AddClause(keydata);
keyInfo.Id = idKeyInfo;
signedXml.KeyInfo = keyInfo;
#endregion keyinfo
#region References
//var transform = new XmlDsigEnvelopedSignatureTransform() { Algorithm = "Exclusive XML Canonicalization Version 1.0" };
var transform = new XmlDsigExcC14NTransform();
var references = new List<Reference>();
// first reference
var keyInfoReference = new Reference();
keyInfoReference.Uri = "#" + keyInfo.Id;
keyInfoReference.DigestMethod = "XML Encryption Syntax and Processing";
keyInfoReference.AddTransform(transform);
references.Add(keyInfoReference);
//second reference
var signaturePropertiesReference = new Reference();
signaturePropertiesReference.Type =
"Assigned ETSI XML URIs";
signaturePropertiesReference.Uri = "#" + idKeyInfoProps;
signaturePropertiesReference.DigestMethod = "XML Encryption Syntax and Processing";
signaturePropertiesReference.AddTransform(transform);
references.Add(signaturePropertiesReference);
//third reference
var documentReference = new Reference();
documentReference.DigestMethod = "XML Encryption Syntax and Processing";
// The code in the question didn't assign Uri, and since no transform did an inherent
// node resolution, signing failed.
documentReference.Uri = "";
documentReference.AddTransform(new XmlDsigEnvelopedSignatureTransform());
documentReference.AddTransform(transform);
references.Add(documentReference);
foreach (Reference reference in references)
{
signedXml.AddReference(reference);
}
#endregion
#region 4. Set up <ds:Object> with <QualifiyingProperties> inside that includes SigningTime
var URI = "Assigned ETSI XML URIs";
XmlElement qualifyingPropertiesRoot = doc.CreateElement("xades", "QualifyingProperties", URI);
XmlElement signaturePropertiesRoot = doc.CreateElement("xades", "SignedProperties", URI);
signaturePropertiesRoot.SetAttribute("Id", idKeyInfoProps);
XmlElement SignedSignatureProperties = doc.CreateElement("xades", "SignedSignatureProperties", URI);
XmlElement timestamp = doc.CreateElement("xades", "SigningTime", URI);
timestamp.InnerText = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:sszzz"); // primero de la lista
signaturePropertiesRoot.AppendChild(SignedSignatureProperties);
SignedSignatureProperties.AppendChild(timestamp);
qualifyingPropertiesRoot.AppendChild(signaturePropertiesRoot);
var qualifyingPropertiesObject = new System.Security.Cryptography.Xml.DataObject
{
Data = qualifyingPropertiesRoot.SelectNodes("."),
//Id = idKeyInfoProps
};
signedXml.AddObject(qualifyingPropertiesObject);
#endregion
SignedXml tmp = new SignedXml(doc)
{
SigningKey = signedXml.SigningKey,
KeyInfo = signedXml.KeyInfo,
};
foreach (System.Security.Cryptography.Xml.DataObject obj in
signedXml.Signature.ObjectList)
{
tmp.AddObject(obj);
}
tmp.AddReference(new Reference(""));
tmp.ComputeSignature();
XmlElement elem = tmp.GetXml();
doc.DocumentElement.AppendChild(elem);
Console.WriteLine("Stage 1 signed");
signedXml.ComputeSignature();
doc.DocumentElement.RemoveChild(elem);
doc.DocumentElement.AppendChild(signedXml.GetXml());
doc.Save("E:\\serialXML.xml");
}
Last edited by a moderator: