I am quite new to C# and especially new to Signing with X509. I have an xml template in which I have to add the certificate used (done) and sign the timestamp (TS-1), the binary security token and the body (id-1). Furthermore, I need to write (e.g. replace placeholders) with the digest value of these 3 elements and add the signature value.
However, I do not really understand the concept, e.g. how to do this. I read a couple of websites, e.g. signing a xml document with x509 certificate but I cannot adapt the code to my problem.
Here is what I tried:
public static string SignXml(string template)
{
XmlDocument document = new XmlDocument();
document.LoadXml(template);
// define elements that will be signed
XmlNode securityToken = null;
XmlNode validityPeriod = null;
XmlNode body = null;
XmlNode signedInfo = null;
XmlNode signatureValue = null;
XmlNodeList digestTags = null;
XmlNamespaceManager namespaces = new XmlNamespaceManager(document.NameTable);
namespaces.AddNamespace("ns", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
namespaces.AddNamespace("nu", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd");
namespaces.AddNamespace("bo", "http://schemas.xmlsoap.org/soap/envelope/");
namespaces.AddNamespace("si", "http://www.w3.org/2000/09/xmldsig#");
namespaces.AddNamespace("sinfo", "soapenv xd xe");
document.LoadXml(template);
//XmlNode idNode = document.SelectSingleNode("/My_RootNode/ns:id", namespaces);
securityToken = document.SelectSingleNode("descendant::ns:BinarySecurityToken", namespaces);
validityPeriod = document.SelectSingleNode("descendant::nu:Timestamp", namespaces);
body = document.SelectSingleNode("descendant::bo:Body", namespaces);
signedInfo = document.SelectSingleNode("descendant::si:SignedInfo", namespaces);
signatureValue = document.SelectSingleNode("descendant::si::sinfo:SignatureValue", namespaces);
digestTags = document.SelectNodes("descendant::si:DigestValue", namespaces);
// add the digests (to know where to write the digests)
String nodeName = null;
for (int counter = 0; counter < digestTags.Count; counter++)
{
nodeName = digestTags[counter].FirstChild.InnerText;
if (WebserviceConstants.PLACEHOLDER_AUTHNREQUEST_DIGEST.Equals(nodeName))
{
generateDigest(body, digestTags[counter]);
}
else if (WebserviceConstants.PLACEHOLDER_CERTIFICATE_DIGEST.Equals(nodeName))
{
generateDigest(securityToken, digestTags[counter]);
}
else if (WebserviceConstants.PLACEHOLDER_TIMESTAMP_TAG_DIGEST.Equals(nodeName))
{
generateDigest(validityPeriod, digestTags[counter]);
}
}
SignedXml signedXml = new SignedXml(document);
X509Certificate2 cert = new X509Certificate2();
cert = getbase();
signedXml.SigningKey = cert.PrivateKey;
// Create a reference to be signed.
Reference reference = new Reference();
reference.Uri = "#TS-1";
// Add an enveloped transformation to the reference.
XmlDsigEnvelopedSignatureTransform env =
new XmlDsigEnvelopedSignatureTransform(true);
reference.AddTransform(env);
//canonicalize
XmlDsigC14NTransform c14t = new XmlDsigC14NTransform();
reference.AddTransform(c14t);
KeyInfo keyInfo = new KeyInfo();
KeyInfoX509Data keyInfoData = new KeyInfoX509Data(cert);
KeyInfoName kin = new KeyInfoName();
kin.Value = "Public key of certificate";
RSACryptoServiceProvider rsaprovider = (RSACryptoServiceProvider)cert.PublicKey.Key;
RSAKeyValue rkv = new RSAKeyValue(rsaprovider);
keyInfo.AddClause(kin);
keyInfo.AddClause(rkv);
keyInfo.AddClause(keyInfoData);
signedXml.KeyInfo = keyInfo;
// 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();
document.DocumentElement.AppendChild(
document.ImportNode(xmlDigitalSignature, true));
document.DocumentElement.AppendChild(document.ImportNode(xmlDigitalSignature, true));
return document.OuterXml;
}
}
I am questioning myself:
- How do I get the digest value and how to write it to the corresponding xml node
- How to calculate the signature value as it "contains" the signed info of all 3 references???
As you can see, I am missing some general background and understanding. Would be really cool if you could help me out!
Thank you