I would like to add additional RsaKeyValue KeyInfo
that includes the public key
in the Digital Signature
.
The user then does not have to save the certificate
- instead he can use that public key
to check the validity of the document.
So far here is my signing function:
public static void SignXmlDocumentWithCertificate(XmlDocument xmlDoc, X509Certificate2 cert)
{
SignedXml signedXml = new SignedXml(xmlDoc);
//we will sign it with private key
signedXml.SigningKey = cert.PrivateKey;
Reference reference = new Reference();
//sign the entire doc
reference.Uri = "";
XmlDsigEnvelopedSignatureTransform env = new XmlDsigEnvelopedSignatureTransform();
reference.AddTransform(env);
signedXml.AddReference(reference);
KeyInfo keyInfo = new KeyInfo();
keyInfo.AddClause(new KeyInfoX509Data(cert));
keyInfo.(cert);
signedXml.KeyInfo = keyInfo;
signedXml.ComputeSignature();
// Get the XML representation of the signature and save
// it to an XmlElement object.
XmlElement xmlDigitalSignature = signedXml.GetXml();
// Append the element to the XML document.
xmlDoc.DocumentElement.AppendChild(xmlDoc.ImportNode(xmlDigitalSignature, true));
}
It is written in C++ in this document : https://learn.microsoft.com/en-us/dotnet/api/system.security.cryptography.xml.rsakeyvalue?view=netframework-4.8
Scroll down do the part
// Add an RSAKeyValue KeyInfo (optional; helps recipient find key to validate).
How do I do it in C#?
How do I add this optional keyinfo with the public key?