This C# sample validates a certificate: its chain up to a trusted root, its validity period and its revocation status, with the CRL (Certificate Revocation List) and the OCSP responder published by the certification authority. It also reads the key usages and the qualified statements that the certificate declares.
How it works
CertificateValidator.Validate(certificate, settings)is the complete validation: the chain, the validity period, the revocation status (OCSP then CRL) and the key usage. The result is aCertificateTrustStatus:Trusted, or the most serious problem found (InvalidChain,NotTimeValid,Revoked,InvalidKeyUsage,UntrustedRoot,RevocationUnknown).CertificateValidationSettingssets the trusted roots and the revocation check.DigitalCertificate.VerifyDigitalCertificate(certificate, VerificationType)is a quick check of one certificate, without its chain and without a trust decision, withLocalTime,CRL,OCSPorLDAP(Windows only). The result is aCertificateStatus:Valid,Revoked,Expired,Unknown(the CRL or the OCSP service is not available) orNotPresent(no CRL or OCSP address in the certificate).GetCertificateRevocationDate,GetKeyUsageandGetQualifiedCertificateInfo(the qualified statements, qcStatements) give the details. The methods are static and do not need the serial number of the library.
C# code
/// <summary>
/// Validates a certificate with CertificateValidator: the chain, the validity period, the revocation status and the key usage.
/// </summary>
static void ValidateCertificate(X509Certificate2 certificate)
{
Console.WriteLine("Certificate subject: " + certificate.Subject);
Console.WriteLine("Certificate serial number: " + certificate.SerialNumber);
// The trust settings of the application. By default: the trusted roots of Windows, OCSP then CRL, the CA certificates of the
// chain checked too, online revocation checks.
CertificateValidationSettings settings = new CertificateValidationSettings();
// A root (or a CA certificate) trusted by the application, in addition to the trusted roots of Windows (a public .cer file):
// settings.TrustedRoots.Add(new X509Certificate2("my_root_ca.cer"));
// Trust only TrustedRoots: the same result on every computer, whatever certificates are installed on it.
// settings.UseWindowsTrustedRoots = false;
// The revocation check: None, Ocsp, Crl or OcspThenCrl (the default).
settings.RevocationCheck = RevocationCheckMode.OcspThenCrl;
// The validation now. The overload Validate(certificate, validationTime, settings) validates the certificate at another
// time (for a signature: the time of its time-stamp).
CertificateValidationResult result = CertificateValidator.Validate(certificate, settings);
// The status: Trusted, or the most serious problem found (InvalidChain, NotTimeValid, Revoked, InvalidKeyUsage,
// UntrustedRoot, RevocationUnknown).
Console.WriteLine("Validation status: " + result.Status + " (trusted: " + result.IsTrusted + ") at " + result.ValidationTime.ToLocalTime());
foreach (string problem in result.Problems)
Console.WriteLine(" problem: " + problem);
foreach (string note in result.Notes)
Console.WriteLine(" note: " + note);
// The chain: the certificate, then its issuers up to the root.
foreach (X509Certificate2 chainCertificate in result.Chain)
Console.WriteLine(" chain: " + chainCertificate.GetNameInfo(X509NameType.SimpleName, false));
// Where the revocation status of every checked certificate came from: Settings, Signature, Online or None.
foreach (KeyValuePair<string, RevocationDataSource> source in result.RevocationSources)
Console.WriteLine(" revocation status of " + source.Key + ": " + source.Value);
}
/// <summary>
/// Checks one certificate quickly, without its chain and without a trust decision: validity period, CRL and OCSP.
/// </summary>
static void VerifyCertificate(X509Certificate2 certificate)
{
// 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 statements.
/// </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)));
// The qualified statements (qcStatements, ETSI EN 319 412-5) declared by the issuer of the certificate, or null when the
// certificate has none. They are declarations: the qualified status itself is given by the EU trusted lists.
// Details: https://www.signfiles.com/manuals/signature-library/#eidas-qualified
QualifiedCertificateInfo qualifiedInfo = DigitalCertificate.GetQualifiedCertificateInfo(certificate);
if (qualifiedInfo == null || !qualifiedInfo.IsQcCompliant)
{
Console.WriteLine("Qualified certificate (declared): no");
}
else
{
// A qualified certificate without QcType is for electronic signatures.
bool forSignatures = qualifiedInfo.Types.Count == 0 || qualifiedInfo.Types.Contains(QualifiedCertificateType.ESign);
bool forSeals = qualifiedInfo.Types.Contains(QualifiedCertificateType.ESeal);
Console.WriteLine("Qualified certificate (declared): signature " + forSignatures + ", seal " + forSeals
+ ", key on a qualified device (QSCD) " + qualifiedInfo.IsKeyOnQualifiedDevice
+ (qualifiedInfo.RetentionPeriodYears.HasValue ? ", retention " + qualifiedInfo.RetentionPeriodYears.Value + " years" : string.Empty));
}
}
Run the full sample. The complete Visual Studio project Certificate Validation 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
Does GetQualifiedCertificateInfo prove that a certificate is qualified?
No. It shows what the certificate declares (qcStatements). The qualified status is given by the EU Trusted Lists: check it with the free .NET eIDAS Certificate Validator.
How long does a CRL or OCSP check take?
It depends on the certification authority. NetworkSettings.RevocationTimeout (the same setting as DigitalCertificate.Timeout) sets the maximum time of an OCSP request or a CRL download (20 seconds by default).