I use XAdESSignedXml class from
http://xadesnet.codeplex.com/[
^]...
i have problem with references when use -> reference2.Uri = "#SignedPropertiesId"; if Uri is some xml data it works well (as my first reference)
try on this XML
http://www.2shared.com/document/DilKQ4Fk/racun_1202017254742.html[^][
^], remove signature from it.
XAdESSignedXml : SignedXml
using System;
using System.Collections.Generic;
using System.Text;
using System.Security.Cryptography.Xml;
using System.Xml;
namespace TestProject1
{
internal sealed class XAdESSignedXml : SignedXml
{
private readonly List<dataobject> _dataObjects = new List<dataobject>();
public const string XadesSignaturePropertiesNamespace = "http://uri.etsi.org/01903/v1.1.1#SignedProperties";
public XAdESSignedXml(XmlDocument document) : base(document) { }
public override XmlElement GetIdElement(XmlDocument doc, string id)
{
if (String.IsNullOrEmpty(id)) return null;
XmlElement xmlElement = base.GetIdElement(doc, id);
if (xmlElement != null) return xmlElement;
foreach (DataObject dataObject in _dataObjects)
{
XmlElement nodeWithSameId = findNodeWithAttributeValueIn(dataObject.Data, "Id", id);
if (nodeWithSameId != null)
return nodeWithSameId;
}
if (KeyInfo != null)
{
XmlElement nodeWithSameId = findNodeWithAttributeValueIn(KeyInfo.GetXml().SelectNodes("."), "Id", id);
if (nodeWithSameId != null)
return nodeWithSameId;
}
return null;
}
public new void AddObject(DataObject dataObject)
{
base.AddObject(dataObject);
_dataObjects.Add(dataObject);
}
public XmlElement findNodeWithAttributeValueIn(XmlNodeList nodeList, string attributeName, string value)
{
if (nodeList.Count == 0) return null;
foreach (XmlNode node in nodeList)
{
XmlElement nodeWithSameId = findNodeWithAttributeValueIn(node, attributeName, value);
if (nodeWithSameId != null) return nodeWithSameId;
}
return null;
}
private XmlElement findNodeWithAttributeValueIn(XmlNode node, string attributeName, string value)
{
string attributeValueInNode = getAttributeValueInNodeOrNull(node, attributeName);
if ((attributeValueInNode != null) && (attributeValueInNode.Equals(value))) return (XmlElement)node;
return findNodeWithAttributeValueIn(node.ChildNodes, attributeName, value);
}
private string getAttributeValueInNodeOrNull(XmlNode node, string attributeName)
{
if (node.Attributes != null)
{
XmlAttribute attribute = node.Attributes[attributeName];
if (attribute != null) return attribute.Value;
}
return null;
}
}
}
</dataobject></dataobject>
AND my method:
private string signEracun(string xml, X509Certificate2 certificate)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.PreserveWhitespace = false;
xmlDoc.LoadXml(xml);
#region signing
TestProject1.XAdESSignedXml signedXml = new TestProject1.XAdESSignedXml(xmlDoc);
signedXml.Signature.Id = "SignatureId";
#region object -> signatureProperties
XmlElement signaturePropertiesRoot;
XmlElement qualifyingPropertiesRoot;
string URI = "http://uri.etsi.org/01903/v1.1.1#";
qualifyingPropertiesRoot = xmlDoc.CreateElement("xds", "QualifyingProperties", URI);
qualifyingPropertiesRoot.SetAttribute("Target", "#SignatureId");
signaturePropertiesRoot = xmlDoc.CreateElement("xds", "SignedProperties", URI);
signaturePropertiesRoot.SetAttribute("Id", "SignedPropertiesId");
XmlElement SignedSignatureProperties = xmlDoc.CreateElement("xds", "SignedSignatureProperties", URI);
XmlElement timestamp = xmlDoc.CreateElement("xds", "SigningTime", URI);
timestamp.InnerText = DateTime.Now.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
SignedSignatureProperties.AppendChild(timestamp);
XmlElement SigningCertificate = xmlDoc.CreateElement("xds", "SigningCertificate", URI);
XmlElement Cert = xmlDoc.CreateElement("xds", "Cert", URI);
XmlElement CertDigest = xmlDoc.CreateElement("xds", "CertDigest", URI);
SHA1 cryptoServiceProvider = new SHA1CryptoServiceProvider();
byte[] sha1 = cryptoServiceProvider.ComputeHash(certificate.RawData);
XmlElement DigestMethod = xmlDoc.CreateElement("xds", "DigestMethod", URI); DigestMethod.SetAttribute("Algorithm", SignedXml.XmlDsigSHA1Url);
XmlElement DigestValue = xmlDoc.CreateElement("xds", "DigestValue", URI); DigestValue.InnerText = Convert.ToBase64String(sha1);
CertDigest.AppendChild(DigestMethod);
CertDigest.AppendChild(DigestValue);
Cert.AppendChild(CertDigest);
XmlElement IssuerSerial = xmlDoc.CreateElement("xds", "IssuerSerial", URI);
XmlElement X509IssuerName = xmlDoc.CreateElement("ds", "X509IssuerName", "http://www.w3.org/2000/09/xmldsig#"); X509IssuerName.InnerText = certificate.IssuerName.Name;
XmlElement X509SerialNumber = xmlDoc.CreateElement("ds", "X509SerialNumber", "http://www.w3.org/2000/09/xmldsig#"); X509SerialNumber.InnerText = certificate.SerialNumber;
IssuerSerial.AppendChild(X509IssuerName);
IssuerSerial.AppendChild(X509SerialNumber);
Cert.AppendChild(IssuerSerial);
SigningCertificate.AppendChild(Cert);
SignedSignatureProperties.AppendChild(SigningCertificate);
signaturePropertiesRoot.AppendChild(SignedSignatureProperties);
qualifyingPropertiesRoot.AppendChild(signaturePropertiesRoot);
DataObject dataObject = new DataObject
{
Data = qualifyingPropertiesRoot.SelectNodes("."),
};
signedXml.AddObject(dataObject);
#endregion
signedXml.SigningKey = certificate.PrivateKey;
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyInfoX509Data = new KeyInfoX509Data(certificate, X509IncludeOption.ExcludeRoot);
keyInfo.AddClause(keyInfoX509Data);
signedXml.KeyInfo = keyInfo;
Reference reference2 = new Reference();
reference2.Type = "http://www.gzs.si/shemas/eslog/racun/1.5#Racun";
reference2.Uri = "#data";
signedXml.AddReference(reference2);
reference2 = new Reference();
reference2.Type = "http://uri.etsi.org/01903/v1.1.1#SignedProperties";
reference2.Uri = "#SignedPropertiesId";
signedXml.AddReference(reference2);
signedXml.ComputeSignature();
XmlElement xmlDigitalSignature = signedXml.GetXml();
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
#endregion
bool checkSign = signedXml.CheckSignature();
return xmlDoc.OuterXml;
}
Björn Ranft thank you for your help.
Regards