Pascal (Lazarus/Delphi)
Pascal (Lazarus/Delphi)
Get the Server Certificate from a TLS Connection
See more Socket/SSL/TLS Examples
Demonstrates Socket.GetServerCert, which copies the certificate presented by the remote TLS server into a Cert object for inspection.
Background. The method returns false when the socket is not connected, is not using TLS, or no server certificate is available. Inspecting the certificate lets an application validate the server's identity.
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.Socket;
// ---------------------------------------------------------------------------
procedure RunDemo;
var
success: Boolean;
socket: TSocket;
bTls: Boolean;
maxWaitMs: Integer;
cert: TCert;
begin
success := False;
socket := TSocket.Create;
// Connect to the server using TLS.
bTls := True;
maxWaitMs := 5000;
success := socket.Connect('example.com',5000,bTls,maxWaitMs);
if (success = False) then
begin
WriteLn(socket.LastErrorText);
Exit;
end;
// Copy the certificate presented by the remote TLS server, then inspect it. This can be used to
// validate the server's identity.
cert := TCert.Create;
success := socket.GetServerCert(cert);
if (success = False) then
begin
WriteLn(socket.LastErrorText);
Exit;
end;
WriteLn('Server certificate subject: ' + cert.SubjectCN);
WriteLn('Issued by: ' + cert.IssuerCN);
socket.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.