This C# sample checks whether a certificate is in its validity period and whether it was revoked, with the CRL (Certificate Revocation List) and the OCSP responder published by the certification authority. It also reads the key usages and whether the certificate declares itself qualified.

How it works

  • DigitalCertificate.VerifyDigitalCertificate(certificate, VerificationType) with LocalTime, CRL, OCSP or LDAP (Windows only).
  • The result is a CertificateStatus: Valid, Revoked, Expired, Unknown (the CRL or the OCSP service is not available) or NotPresent (no CRL or OCSP address in the certificate).
  • GetCertificateRevocationDate, GetKeyUsage and IsQualifiedCertificate (the qcStatements extension) give the details. The methods are static and do not need the serial number of the library.

C# code

/// <summary>
/// Verifies a certificate: validity period, CRL and OCSP, and displays the results.
/// </summary>
static void VerifyCertificate(X509Certificate2 certificate)
{
    Console.WriteLine("Certificate subject: " + certificate.Subject);
    Console.WriteLine("Certificate serial number: " + certificate.SerialNumber);

    // The maximum time of a CRL or OCSP download, in milliseconds (the default value is 20000).
    DigitalCertificate.Timeout = 20000;

    // The validity period of the certificate, compared with the local time of the computer.
    Console.WriteLine("Verify against the local time: " + DigitalCertificate.VerifyDigitalCertificate(certificate, VerificationType.LocalTime));

    // The revocation status, from the CRL published by the certification authority.
    CertificateStatus crlStatus = DigitalCertificate.VerifyDigitalCertificate(certificate, VerificationType.CRL);
    Console.WriteLine("Verify against the CRL: " + crlStatus);

    // The revocation status, from the OCSP responder of the certification authority.
    Console.WriteLine("Verify against the OCSP: " + DigitalCertificate.VerifyDigitalCertificate(certificate, VerificationType.OCSP));

    // The possible results (CertificateStatus):
    //   Valid      - the certificate is valid (in its validity period, or not revoked);
    //   Revoked    - the certificate is revoked;
    //   Expired    - the certificate is expired, or it is not valid yet;
    //   Unknown    - the status cannot be determined (the CRL or the OCSP service is not available);
    //   NotPresent - the certificate does not publish a CRL or an OCSP responder.

    if (crlStatus == CertificateStatus.Revoked)
        Console.WriteLine("Revocation date: " + DigitalCertificate.GetCertificateRevocationDate(certificate));

    // The CRL can also be published on an LDAP server (Windows only):
    // DigitalCertificate.VerifyDigitalCertificate(certificate, VerificationType.LDAP);
}

/// <summary>
/// Displays other information about a certificate: key usages and qualified status.
/// </summary>
static void PrintCertificateDetails(X509Certificate2 certificate)
{
    // GetKeyUsage returns null when the certificate has no key usage extension.
    List<CertificateKeyUsage> keyUsages = DigitalCertificate.GetKeyUsage(certificate);
    Console.WriteLine("Key usages: " + (keyUsages == null ? "(no key usage extension)" : string.Join(", ", keyUsages)));

    // True if the certificate declares itself as a qualified certificate (it has the qcStatements extension).
    // The qualified status itself is given by the EU Trusted Lists.
    Console.WriteLine("Qualified certificate (qcStatements): " + DigitalCertificate.IsQualifiedCertificate(certificate));
}

Run the full sample. The complete Visual Studio project Certificate Validation is included in the library download, in the folder NET Sample Projects (.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

Does IsQualifiedCertificate prove that a certificate is qualified?

No. It shows that the certificate declares itself qualified (qcStatements). The qualified status is given by the EU Trusted Lists.

How long does a CRL or OCSP check take?

It depends on the certification authority. DigitalCertificate.Timeout sets the maximum time of a download (20 seconds by default).