A long-term signature is only as good as the validation data it contains. This C# sample signs a PDF (PAdES-B-LT), a P7M file (CAdES-B-LT) and an XML document (XAdES-B-LT), then reads the level that every signature really has and whether its validation data is complete. It also shows what happens when the revocation data of a certificate cannot be obtained.
What the sample shows
Level: the effective level of a signature (B,T,LT,LTA), determined from what the signature contains, by the rules of the ETSI baseline profiles. It is inPdfSignatureInfo,CadesSignatureInfo(andCadesVerify.Level, the lowest level of the signers) andXadesSignatureInfo.ValidationDataCompleteandMissingValidationData: whether there is an OCSP response or a CRL for every certificate of the chains of the signer and of the time-stamping authority, and which certificates have none, with the reason.- For PDF documents:
CoversWholeDocument(the signature covers the file up to its end) andSignatureAlgorithmNotSupported(the algorithm is not known, so the signature was not verified). - A signature is created at the level that you ask for, or not at all: when the validation data cannot be obtained,
ValidationDataExceptionlists the certificates and the reasons (MissingData), and no document is written. WithAllowIncompleteValidationData = truethe signature is created with the data that is available, and its level is lower.
C# code: PAdES-B-LT signature and its level
static void SignPdf(X509Certificate2 certificate, string signedPdfFile)
{
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.LoadPdfDocument(SampleFile("source.pdf"));
pdfSignature.DigitalSignatureCertificate = certificate;
pdfSignature.SigningReason = "I approve this document";
pdfSignature.SignaturePosition = SignaturePosition.TopLeft;
// PAdES-B-LT: a time-stamped signature, followed by the validation data in the document security store (DSS).
pdfSignature.SignatureStandard = PdfSignatureStandard.PadesLT;
pdfSignature.PadesLtvLevel = PadesLtvLevel.IncludeOcspOnly;
pdfSignature.TimeStamping.ServerUrl = TimestampServerUrl;
File.WriteAllBytes(signedPdfFile, pdfSignature.ApplyDigitalSignature());
}
static void PrintPdfSignatures(string signedPdfFile)
{
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.LoadPdfDocument(signedPdfFile);
foreach (PdfSignatureInfo signatureInfo in pdfSignature.DocumentProperties.DigitalSignatures)
{
Console.WriteLine(" " + signatureInfo.SignatureName + " (" + signatureInfo.SignatureType + ")");
Console.WriteLine(" signed by: " + signatureInfo.SignatureCertificate.GetNameInfo(X509NameType.SimpleName, false));
Console.WriteLine(" signature is valid: " + signatureInfo.SignatureIsValid);
// true: the library does not know the algorithm, the signature was not verified (it was not found altered).
Console.WriteLine(" signature algorithm not supported: " + signatureInfo.SignatureAlgorithmNotSupported);
// A PAdES-LT signature does not cover the whole file: the document security store was added after it, in a new
// revision of the file. A document is fully protected when one signature or document time-stamp covers the whole file.
Console.WriteLine(" covers the whole document: " + signatureInfo.CoversWholeDocument);
Console.WriteLine(" level: " + signatureInfo.Level);
Console.WriteLine(" validation data complete: " + signatureInfo.ValidationDataComplete);
PrintMissingValidationData(signatureInfo.MissingValidationData);
}
}
A PAdES-LT signature does not cover the whole file: the validation data is added after it, in a new revision of the document. A document is fully protected when a signature or a document time-stamp covers the whole file.
C# code: CAdES-B-LT signature and its level
static byte[] SignCades(X509Certificate2 certificate, bool allowIncompleteValidationData)
{
CadesSignature cadesSignature = new CadesSignature(LibrarySerialNumber);
cadesSignature.DigitalSignatureCertificate = certificate;
cadesSignature.SignatureStandard = CadesSignatureStandard.CadesLT;
cadesSignature.LtvLevel = CadesLtvLevel.IncludeOcspOnly;
cadesSignature.TimeStamping.ServerUrl = TimestampServerUrl;
// false (the default): the signature is not created when the validation data of a certificate is missing.
cadesSignature.AllowIncompleteValidationData = allowIncompleteValidationData;
// An attached signature: the .p7m file contains the signed file.
return cadesSignature.ApplyDigitalSignature(SampleFile("test.txt"));
}
static void PrintCadesSignatures(byte[] signature)
{
CadesVerify cadesVerify = new CadesVerify(signature, LibrarySerialNumber);
// The level of the file is the lowest level of its signers.
Console.WriteLine(" CAdES level of the file: " + cadesVerify.Level);
foreach (CadesSignatureInfo signatureInfo in cadesVerify.Signatures)
{
Console.WriteLine(" signer: " + signatureInfo.SignatureCertificate.GetNameInfo(X509NameType.SimpleName, false));
Console.WriteLine(" signature is valid: " + signatureInfo.SignatureIsValid);
Console.WriteLine(" signature algorithm not supported: " + signatureInfo.SignatureAlgorithmNotSupported);
Console.WriteLine(" level: " + signatureInfo.Level);
Console.WriteLine(" validation data complete: " + signatureInfo.ValidationDataComplete);
PrintMissingValidationData(signatureInfo.MissingValidationData);
}
}
C# code: XAdES-B-LT signature and its level
static void SignXades(X509Certificate2 certificate, string signedXmlFile)
{
XadesSignature xadesSignature = new XadesSignature(LibrarySerialNumber);
xadesSignature.DigitalSignatureCertificate = certificate;
xadesSignature.SignatureStandard = XadesSignatureStandard.XadesLT;
xadesSignature.SignaturePackaging = XadesSignaturePackaging.Enveloped;
xadesSignature.LtvLevel = XadesLtvLevel.IncludeOcspOnly;
xadesSignature.TimeStamping.ServerUrl = TimestampServerUrl;
xadesSignature.ApplyDigitalSignature(SampleFile("test.xml"), signedXmlFile);
}
static void PrintXadesSignatures(string signedXmlFile)
{
XadesSignature xadesSignature = new XadesSignature(LibrarySerialNumber);
// The same information is returned for the signatures of an ASiC-E container by AsicSignature.GetSignatures.
foreach (XadesSignatureInfo signatureInfo in xadesSignature.GetSignatures(signedXmlFile))
{
string signer = signatureInfo.SignatureCertificate != null ? signatureInfo.SignatureCertificate.GetNameInfo(X509NameType.SimpleName, false) : "unknown";
Console.WriteLine(" signer: " + signer);
Console.WriteLine(" signature is valid: " + signatureInfo.SignatureIsValid);
Console.WriteLine(" level: " + signatureInfo.Level);
Console.WriteLine(" validation data complete: " + signatureInfo.ValidationDataComplete);
PrintMissingValidationData(signatureInfo.MissingValidationData);
}
}
AsicSignature.GetSignatures returns the same information for the signatures of an ASiC-E container.
C# code: when the validation data cannot be obtained
The sample creates in memory a test certification authority and a certificate without a CRL address and without an OCSP responder. A CAdES-B-LT signature is refused with ValidationDataException; with AllowIncompleteValidationData = true it is created, time-stamped, at level T.
static void ShowIncompleteValidationData()
{
X509Certificate2 certificate = CreateCertificateWithoutRevocationInformation();
byte[] signature;
try
{
signature = SignCades(certificate, false);
Console.WriteLine(" The signature was created (unexpected for this certificate).");
}
catch (ValidationDataException ex)
{
// No document is produced. Retry later (a server did not answer), or fix the cause.
Console.WriteLine(" ValidationDataException: " + ex.Message);
foreach (MissingValidationData missing in ex.MissingData)
Console.WriteLine(" " + (missing.IsTimestampAuthority ? "(time-stamping authority) " : "") + missing.Subject + " - " + missing.Reason);
// When a lower level is acceptable, the signature is created with the data that is available.
Console.WriteLine(" Signing again with AllowIncompleteValidationData = true.");
signature = SignCades(certificate, true);
}
File.WriteAllBytes(SampleFile("test.txt[incomplete].p7m"), signature);
// The signature is time-stamped, but it is not a B-LT signature: its level is T.
PrintCadesSignatures(signature);
}
More: the level of a signature and incomplete validation data in the user manual.
Run the full sample. The complete Visual Studio project Signature Level and Validation Data is included in the library download, in the folder NET Sample Projects (.NET 8, .NET 9, .NET 10 and .NET 11) and in NET Framework Sample Projects (.NET Framework 4.6.2), 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
Why is my LT signature reported as level T?
The revocation data (OCSP response or CRL) of a certificate of the chain of the signer or of the time-stamping authority is missing. MissingValidationData lists these certificates and the reasons.
What does Level None mean?
The signature is not an AdES signature (for example a PKCS#7 signature without the signing-certificate attribute, an XMLDSig signature without the XAdES properties, a document time-stamp) or it is not valid.
Why does a self-signed test certificate give a complete LT signature?
A root certificate needs no revocation data, so the validation data of its chain is complete. Real certificates issued by a certification authority need an OCSP response or a CRL.