Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Load Certificate from PFX (PKCS#12)
See more Certificates Examples
Loads a digital certificate (and private key, if available) from a PFX file.(also known as PKCS#12)Chilkat Pascal (Lazarus/Delphi) Downloads
program ChilkatDemo;
// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.
{$IFDEF FPC}
{$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}
uses
{$IFDEF UNIX}
cthreads,
{$ENDIF}
SysUtils,
CkDllLoader,
Chilkat.Cert,
Chilkat.PrivateKey;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
cert: TCert;
pfxFilename: string;
pfxPassword: string;
privKey: TPrivateKey;
pemPassword: string;
pemPath: string;
begin
success := False;
cert := TCert.Create;
// Load from the PFX file
pfxFilename := '/Users/chilkat/testData/pfx/chilkat_ssl_pwd_is_test.pfx';
pfxPassword := 'test';
// A PFX typically contains certificates in the chain of authentication.
// The Chilkat cert object will choose the certificate w/
// private key farthest from the root authority cert.
// To access all the certificates in a PFX, use the
// Chilkat certificate store object instead.
success := cert.LoadPfxFile(pfxFilename,pfxPassword);
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// Get some information about the digital certificate,
// then get the private key...
// DN = "Distinguished Name"
WriteLn('SubjectDN:' + cert.SubjectDN);
WriteLn('Common Name:' + cert.SubjectCN);
WriteLn('Issuer Common Name:' + cert.IssuerCN);
WriteLn('Serial Number:' + cert.SerialNumber);
privKey := TPrivateKey.Create;
success := cert.GetPrivateKey(privKey);
if (success = False) then
begin
WriteLn(cert.LastErrorText);
Exit;
end;
// The private key object may be used in any Chilkat methods
// (in other objects/classes) that expect a private key argument.
// In this case, save the private key to a PKCS8 Encrypted PEM format file:
pemPassword := 'secret';
pemPath := '/Users/chilkat/testData/pem/chilkat_privKey.pem';
success := privKey.SavePkcs8EncryptedPemFile(pemPassword,pemPath);
if (success = False) then
begin
WriteLn(privKey.LastErrorText);
Exit;
end;
WriteLn('Private key saved to PKCS8 Encrypted PEM...');
cert.Free;
privKey.Free;
end;
// ---------------------------------------------------------------------------
begin
try
RunDemo;
except
on E: Exception do
WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
end;
WriteLn;
{$IFDEF MSWINDOWS}
WriteLn('Press Enter to exit...');
ReadLn;
{$ENDIF}
end.