Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Load PKCS12 / PFX and Access Contents
See more PFX/P12 Examples
Loads a PKCS12 / PFX file and iterates over the contents which include private keys and certificates.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.Pfx,
Chilkat.PrivateKey,
Chilkat.Cert;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
pfx: TPfx;
numPrivateKeys: Integer;
privKey: TPrivateKey;
i: Integer;
cert: TCert;
numCerts: Integer;
begin
success := False;
pfx := TPfx.Create;
// Load the PKCS12 from a file
success := pfx.LoadPfxFile('/someDir/my.p12','pfxFilePassword');
if (success = False) then
begin
WriteLn(pfx.LastErrorText);
Exit;
end;
numPrivateKeys := pfx.NumPrivateKeys;
privKey := TPrivateKey.Create;
WriteLn('Private Keys:');
i := 0;
while i < numPrivateKeys do
begin
pfx.PrivateKeyAt(i,privKey);
// Do something with the private key ...
i := i + 1;
end;
cert := TCert.Create;
numCerts := pfx.NumCerts;
WriteLn('Certs:');
i := 0;
while i < numCerts do
begin
pfx.CertAt(i,cert);
WriteLn(cert.SubjectDN);
// If the certificate has a private key (one of the private keys within the PFX)
// then it can also be obtained via the certificate object:
if (cert.HasPrivateKey() = True) then
begin
WriteLn('Has private key!');
success := cert.GetPrivateKey(privKey);
// ...
end;
i := i + 1;
end;
pfx.Free;
privKey.Free;
cert.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.