This C# sample signs an XML document with an enveloped XMLDSig signature (W3C XML Signature): the Signature element is added inside the document. It adds a second signature and verifies all the signatures of the document.
How to sign an XML document in C#
- Create an
XmlSignatureobject, setDigitalSignatureCertificateand callApplyDigitalSignature(input, output). HashAlgorithm: SHA-256 (the default), SHA-384 or SHA-512. SHA-1 signatures can be verified, but not created.SignatureType: the canonicalization method, Canonical XML 1.0 by default; exclusive canonicalization is recommended when the signed fragment is moved to another document.IncludeKeyInfoandIncludeSignatureCertificateput the key and the certificate of the signer in the signature, so the verifier can identify the signer.
C# code: sign and add a second signature
/// <summary>
/// Signs an XML document with an enveloped XMLDSig signature.
/// </summary>
static void SignXmlDocument(string unsignedXmlFile, string signedXmlFile)
{
XmlSignature xmlSignature = new XmlSignature(LibrarySerialNumber);
xmlSignature.DigitalSignatureCertificate = LoadSigningCertificate();
// The hash algorithm of the signature: SHA256 (the default), SHA384 or SHA512.
// SHA-1 signatures can be verified, but new SHA-1 signatures cannot be created.
xmlSignature.HashAlgorithm = SignLib.HashAlgorithm.SHA256;
// The canonicalization method of the signature. Default is the Canonical XML 1.0 (c14n).
// Exclusive is recommended when the signed fragment is moved to another XML document.
// The "WithComments" values keep the comments of the document in the signed data.
xmlSignature.SignatureType = XmlSignatureType.Default;
// The signature includes the key of the signer and the signer certificate (the defaults). With
// the certificate in the signature, the signer can be identified by the one that verifies the document.
xmlSignature.IncludeKeyInfo = true;
xmlSignature.IncludeSignatureCertificate = true;
// The white space of the document is preserved (the default): the signed document is saved as it is.
xmlSignature.PreserveWhitespace = true;
xmlSignature.ApplyDigitalSignature(unsignedXmlFile, signedXmlFile);
Console.WriteLine("The XML signature was created." + Environment.NewLine);
}
/// <summary>
/// Adds a second signature to an already signed XML document. The existing signature remains valid.
/// </summary>
static void AddSecondSignature(string signedXmlFile)
{
XmlSignature xmlSignature = new XmlSignature(LibrarySerialNumber);
// In a real application the second signature is created by another signer, with his own certificate.
xmlSignature.DigitalSignatureCertificate = LoadSigningCertificate();
// The output file can be the same as the input file.
xmlSignature.ApplyDigitalSignature(signedXmlFile, signedXmlFile);
Console.WriteLine("The second XML signature was added." + Environment.NewLine);
}
C# code: verify the XML signatures
/// <summary>
/// Verifies the signatures of a signed XML document and displays their information.
/// </summary>
static void VerifyXmlSignatures(string signedXmlFile)
{
XmlSignature xmlSignature = new XmlSignature(LibrarySerialNumber);
int numberOfSignatures = xmlSignature.GetNumberOfSignatures(signedXmlFile);
Console.WriteLine("Number of signatures: " + numberOfSignatures);
// The algorithm of the first signature, for example RSA-SHA256.
Console.WriteLine("Signature algorithm: " + xmlSignature.GetSignatureAlgorithm(signedXmlFile));
// Verifies every signature of the document. It returns true if all of them are valid.
Console.WriteLine("All the signatures are valid: " + xmlSignature.VerifyDigitalSignature(signedXmlFile));
// Every signature can also be verified separately. The signature index starts at 0.
for (int signatureIndex = 0; signatureIndex < numberOfSignatures; signatureIndex++)
{
// The signer certificate is read from the signature (when it is included in the signature).
X509Certificate2 signerCertificate = xmlSignature.GetDigitalSignatureCertificate(signedXmlFile, signatureIndex);
Console.WriteLine("Signature " + (signatureIndex + 1) + ": signed by " + signerCertificate.Subject
+ ", valid: " + xmlSignature.VerifyDigitalSignature(signedXmlFile, signatureIndex));
}
// Verifies that the document has a valid signature created with a specific certificate.
// The certificate used here is the public part (.cer) of the expected signer.
X509Certificate2 expectedSigner = xmlSignature.GetDigitalSignatureCertificate(signedXmlFile);
Console.WriteLine("The document is signed by the expected certificate: " + xmlSignature.VerifyDigitalSignature(signedXmlFile, expectedSigner));
// These methods verify the integrity of the signatures. They do not verify the certificate chain or the
// revocation status of the signing certificate (see the sample "Certificate Validation").
Console.WriteLine("Done XML signature verification." + Environment.NewLine);
}
XMLDSig is the basic W3C format. For the eIDAS advanced and qualified XML signatures, with the signing time, the signing certificate and long-term validation, use XAdES and XAdES-LTA; to sign several files together, use an ASiC-E container.
Run the full sample. The complete Visual Studio project XML Digital Signature - XMLDSig is included in the library download, in the folder NET Sample Projects (.NET 8, .NET 9, .NET 10 and .NET 11), with the test files and a test certificate. Without a serial number the library runs in demo mode. Download the library and the samples · User manual
Frequently asked questions
What is the difference between XMLDSig and XAdES?
XAdES (ETSI EN 319 132) is XMLDSig with signed properties: the signing time, the signing certificate, the commitment type and, at the higher levels, time-stamps and validation data. The eIDAS advanced and qualified XML signatures are XAdES signatures.
Do the verification methods check the certificate?
No. They verify the integrity of the signatures. Check the certificate with the certificate validation methods.