Documents are often signed quickly, at level B-B (without a time-stamp), and archived later. This C# sample raises existing XAdES and ASiC-E signatures to the long-term archival level B-LTA, creates PAdES-LTA and CAdES-LTA signatures, and runs an archive routine that renews the archive time-stamps of PDF, P7M, XML and ASiC-E files before the certificate of the time-stamping authority expires.
What the sample shows
- Raise the level of existing signatures:
XadesSignature.ExtendSignatureandAsicSignature.ExtendSignatureturn a XAdES-B-B signature into XAdES-B-LTA. Only the missing parts are added (the signature time-stamp, the validation data and the archive time-stamp), and the certificate of the signer is not needed. - Create PAdES-LTA and CAdES-LTA signatures directly, with
PdfSignatureStandard.PadesLTAandCadesSignatureStandard.CadesLTA. - Renew an archive: the static class
LongTermArchivereturns the renewal date of every file (GetPdfRenewalDate,GetCadesRenewalDate,GetXadesRenewalDate,GetAsicRenewalDate), andAddArchiveTimestampofPdfSignature,CadesSignature,XadesSignatureandAsicSignatureadds the validation data of the previous time-stamps and a new archive time-stamp.
C# code: raise XAdES and ASiC-E signatures from B-B to B-LTA
/// <summary>
/// Signs an XML document with an enveloped XAdES-B-B signature (no time-stamp, no network), then raises it to XAdES-B-LTA.
/// </summary>
static void CreateAndExtendXades(X509Certificate2 certificate, string signedXmlFile, string archivedXmlFile)
{
XadesSignature xadesSignature = new XadesSignature(LibrarySerialNumber);
xadesSignature.DigitalSignatureCertificate = certificate;
xadesSignature.SignatureStandard = XadesSignatureStandard.XadesB;
xadesSignature.ApplyDigitalSignature(SampleFile("test.xml"), signedXmlFile);
Console.WriteLine(" signed: " + Path.GetFileName(signedXmlFile) + ", level " + XadesLevel(signedXmlFile));
// Later, for example in a nightly batch: the signature time-stamp, the validation data and the archive time-stamp are added.
// The certificate of the signer is not needed. The output can be the input file.
XadesSignature extender = new XadesSignature(LibrarySerialNumber);
extender.TimeStamping.ServerUrl = TimestampServerUrl;
extender.ExtendSignature(signedXmlFile, archivedXmlFile, XadesSignatureStandard.XadesLTA);
Console.WriteLine(" extended: " + Path.GetFileName(archivedXmlFile) + ", level " + XadesLevel(archivedXmlFile));
}
/// <summary>
/// Creates an ASiC-E container with an XAdES-B-B signature, then raises it to XAdES-B-LTA (in memory).
/// </summary>
static void CreateAndExtendAsic(X509Certificate2 certificate, string containerFile, string archivedContainerFile)
{
AsicSignature asicSignature = new AsicSignature(LibrarySerialNumber);
asicSignature.DigitalSignatureCertificate = certificate;
asicSignature.SignatureStandard = XadesSignatureStandard.XadesB;
asicSignature.CreateContainer(new[] { SampleFile("test.txt"), SampleFile("source.pdf") }, containerFile);
Console.WriteLine(" signed: " + Path.GetFileName(containerFile) + ", level " + AsicLevel(File.ReadAllBytes(containerFile)));
AsicSignature extender = new AsicSignature(LibrarySerialNumber);
extender.TimeStamping.ServerUrl = TimestampServerUrl;
byte[] extended = extender.ExtendSignature(File.ReadAllBytes(containerFile), XadesSignatureStandard.XadesLTA);
File.WriteAllBytes(archivedContainerFile, extended);
Console.WriteLine(" extended: " + Path.GetFileName(archivedContainerFile) + ", level " + AsicLevel(extended));
}
The output can be the input file. Extending needs only the time-stamping server (TimeStamping.ServerUrl) and the network for the revocation data. More: raising the level of a XAdES signature and of an ASiC-E container in the user manual.
C# code: the renewal date of an archived file
The renewal date is the first expiry date of the certificate chain of the time-stamping authority of the last archive (or document) time-stamp, or null when the file has no archive time-stamp.
/// <summary>
/// Reads the renewal date of an archived file (UTC): the first expiry date of the certificate chain of the TSA of its last
/// archive (or document) time-stamp, or null when the file has no archive time-stamp.
/// </summary>
static DateTime? GetRenewalDate(string file)
{
switch (Path.GetExtension(file).ToLowerInvariant())
{
case ".pdf":
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.LoadPdfDocument(file);
return LongTermArchive.GetPdfRenewalDate(pdfSignature.DocumentProperties.DigitalSignatures);
case ".p7m":
return LongTermArchive.GetCadesRenewalDate(File.ReadAllBytes(file));
case ".xml":
return LongTermArchive.GetXadesRenewalDate(file);
case ".asice":
return LongTermArchive.GetAsicRenewalDate(File.ReadAllBytes(file));
default:
return null;
}
}
C# code: renew the archive time-stamp
/// <summary>
/// Adds the validation data of the previous time-stamps and a new archive time-stamp to an archived file (the file is replaced).
/// </summary>
static void Renew(string file)
{
switch (Path.GetExtension(file).ToLowerInvariant())
{
case ".pdf":
PdfSignature pdfSignature = new PdfSignature(LibrarySerialNumber);
pdfSignature.LoadPdfDocument(File.ReadAllBytes(file));
pdfSignature.TimeStamping.ServerUrl = TimestampServerUrl;
File.WriteAllBytes(file, pdfSignature.AddArchiveTimestamp());
break;
case ".p7m":
CadesSignature cadesSignature = new CadesSignature(LibrarySerialNumber);
cadesSignature.TimeStamping.ServerUrl = TimestampServerUrl;
// An attached signature. For a detached signature give the signed file as the second parameter.
File.WriteAllBytes(file, cadesSignature.AddArchiveTimestamp(File.ReadAllBytes(file)));
break;
case ".xml":
XadesSignature xadesSignature = new XadesSignature(LibrarySerialNumber);
xadesSignature.TimeStamping.ServerUrl = TimestampServerUrl;
// An enveloped signature (the output can be the input file). A detached signature also needs the signed file.
xadesSignature.AddArchiveTimestamp(file, file);
break;
case ".asice":
AsicSignature asicSignature = new AsicSignature(LibrarySerialNumber);
asicSignature.TimeStamping.ServerUrl = TimestampServerUrl;
File.WriteAllBytes(file, asicSignature.AddArchiveTimestamp(File.ReadAllBytes(file)));
break;
}
}
C# code: the archive routine for a scheduled task
The routine renews only the files whose renewal date is closer than the margin (90 days in the sample). Renew immediately, whatever the date, when an algorithm in use is no longer acceptable. When the revocation data of a certificate cannot be obtained, ValidationDataException is thrown and the file is not changed: retry later.
/// <summary>
/// The archive routine, to run periodically (a scheduled task): every file whose renewal date is closer than the margin is renewed.
/// </summary>
static void RenewArchive(string archiveFolder, TimeSpan margin)
{
foreach (string file in Directory.GetFiles(archiveFolder, "*[lta].*"))
{
string name = Path.GetFileName(file);
try
{
DateTime? renewalDate = GetRenewalDate(file);
if (renewalDate == null)
{
// No archive time-stamp: raise the level of the signatures first (ExtendSignature, AddArchiveTimestamp for PDF).
Console.WriteLine(" " + name + ": no archive time-stamp");
continue;
}
if (renewalDate.Value - DateTime.UtcNow >= margin)
{
Console.WriteLine(" " + name + ": renewal date " + renewalDate.Value.ToShortDateString() + ", nothing to do");
continue;
}
// With the same time-stamping authority, the next renewal date does not change: it moves when the TSA uses
// a new certificate.
Renew(file);
Console.WriteLine(" " + name + ": renewal date " + renewalDate.Value.ToShortDateString() + ", renewed; the next renewal date is "
+ GetRenewalDate(file).Value.ToShortDateString());
}
catch (ValidationDataException ex)
{
// The revocation data of a certificate could not be obtained: the file is not changed. Retry later.
Console.WriteLine(" " + name + ": not renewed, " + ex.Message);
}
}
}
The sample runs the routine twice: with the real margin (usually nothing needs a renewal) and with a very large margin, which simulates a time-stamping certificate that expires soon, to show the renewal of every format. The renewal policy is described in renewing the time-stamps of an archive.
The free server of the sample is for tests. For signatures with legal effect in the European Union, use a qualified time-stamping service, such as the eIDAS qualified timestamps of qtsa.eu.
Run the full sample. The complete Visual Studio project Extend and Renew Long-Term Signatures 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
Do I need the certificate of the signer to extend or renew a signature?
No. ExtendSignature and AddArchiveTimestamp need only a time-stamping server and the revocation data of the certificates, which the library downloads.
Can a PAdES or CAdES signature be raised with ExtendSignature?
No. ExtendSignature exists for XAdES and ASiC-E. A PDF document gets the validation data and a document time-stamp with PdfSignature.AddArchiveTimestamp; CadesSignature.AddArchiveTimestamp renews a signature that is already CAdES-LTA.
When must an archive be renewed?
Before the certificate of the time-stamping authority of the last archive time-stamp expires (LongTermArchive returns this date), with a margin such as 90 days, and immediately when an algorithm in use is no longer considered secure.