Discuss this help topic in SecureBlackbox Forum
Load certificate from stream
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 the certificate from stream use the LoadFromStreamAuto() method:
TElX509Certificate cert = new TElX509Certificate();
int res = cert.LoadFromStreamAuto(stream, "", 0);
You might need to provide a password as the second parameter if the certificate is in encrypted PFX or PEM format.
The third parameter indicates how many bytes from the stream shall be read (0 stands for 'as many as needed').
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 stored in the stream:
TSBCertFileFormat fmt = TElX509Certificate.DetectCertFileFormat(stream);
Important! Rewind the stream back before passing it further to any LoadFromStream() method.
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();
}