Discuss this help topic in SecureBlackbox Forum

Load certificate from file

Certificates may come in a number of formats. Different methods should be used to load the certificate depending on the exact format in which it is stored .

In SecureBlackbox, an X.509 certificate is represented by TElX509Certificate component. Each instance keeps one certificate, hence you will need one TElX509Certificate object for each certificate.

To load a certificate from a file, use the TElX509Certificate.LoadFromFileAuto() method: TElX509Certificate cert = new TElX509Certificate();
int res = cert.LoadFromFileAuto("certificate.csr", "");
You might need to provide a password as the second parameter if you are loading an encrypted certificate from a PFX or PEM file.

Please pay attention to the result of this call. The return value of 0 indicates that the certificate has been successfully loaded.

The DetectCertFileFormat() method allows to detect the format of the certificate file: TSBCertFileFormat fmt = TElX509Certificate.DetectCertFileFormat("certificate.csr");

In addition to your certificate, some certification authorities (CAs) provide you with a complete chain of CA certificates stored in a single PFX file. The above method won't work in such case, the TElX509Certificate is designed to only load the first certificate from such PFX stream. Multi-certificate PFX files can be loaded into a certificate storage component such as TElMemoryCertStorage. You can use this component to access individual certificates:


TElMemoryCertStorage certStorage = new TElMemoryCertStorage();

FileStream f = new FileStream("certs.pfx", FileMode.Open);
try {
  int r = certStorage.LoadFromStreamPFX(f, "password");
  if (r == 0) {
    Console.WriteLine(certStorage.Count.ToString() + " certificates have been loaded successfully");
  } else {
    Console.WriteLine("Failed to load certificate(s). The error code is " + r.ToString());
  }
} finally {
  f.Close();
}
	

Certificate-related How To articles

Discuss this help topic in SecureBlackbox Forum