In this post, I’m going to show how to read details of a digital certificate using Chilkat library from a Base64 string.
The below is my model of the details I want to read from the certificate.
public class CertificateModel
{
public string SerialNumber { get; }
public string AuthorityKeyIdentifier { get; }
public string OcspUrl { get; }
public DateTime ValidFromUtc { get; }
public DateTime ValidToUtc { get; }
public string SubjectName { get; }
public string Email { get; }
public string Abn { get; }
public string IssuingOrganisation { get; }
public CertificateAssuranceLevelEnum? AssuranceLevel { get; }
public bool IsSigningCertificate { get; }
public string Pem { get; }
public CertificateModel(
string serialNumber,
string authorityKeyIdentifier,
string ocspUrl,
DateTime validFromUtc,
DateTime validToUtc,
string subjectName,
string email,
string abn,
string issuingOrganisation,
CertificateAssuranceLevelEnum? assuranceLevel,
bool isSigningCertificate,
string pem)
{
SerialNumber = serialNumber;
AuthorityKeyIdentifier = authorityKeyIdentifier;
OcspUrl = ocspUrl;
ValidFromUtc = validFromUtc;
ValidToUtc = validToUtc;
CommonName = commonName;
Email = email;
Abn = abn;
Pem = pem;
IssuingOrganisation = issuingOrganisation;
AssuranceLevel = assuranceLevel;
IsSigningCertificate = isSigningCertificate;
}
}
Here is the method to read in the certificate
public CertificateModel Decode(string base64Value)
{
var cert = ChilkatHelpers.Cert(base64Value);
var validFrom = cert.ValidFromUtc();
var validTo = cert.ValidToUtc();
var subjectName = cert.SubjectCN;
var email = cert.UserEmail();
var abn = cert.Abn();
var assuranceLevel = cert.AssuranceLevel();
var issuingOrganisation = cert.IssuerO;
var keyUsage = (X509KeyUsageFlags)cert.IntendedKeyUsage;
var isSigningCertificate = keyUsage.HasFlag(X509KeyUsageFlags.DigitalSignature);
var pem = cert.ExportCertPem();
return new CertificateModel(
cert.SerialDecimal,
cert.AuthorityKeyId,
cert.OcspUrl,
validFrom,
validTo,
subjectName,
email,
abn,
issuingOrganisation,
assuranceLevel,
isSigningCertificate,
pem);
}