Sample code for 30+ languages & platforms
Delphi ActiveX

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 Delphi ActiveX Downloads

Delphi ActiveX
var
success: Integer;
socket: TChilkatSocket;
bTls: Integer;
maxWaitMs: Integer;
cert: TChilkatCert;

begin
success := 0;

socket := TChilkatSocket.Create(Self);

//  Connect to the server using TLS.
bTls := 1;
maxWaitMs := 5000;
success := socket.Connect('example.com',5000,bTls,maxWaitMs);
if (success = 0) then
  begin
    Memo1.Lines.Add(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 := TChilkatCert.Create(Self);
success := socket.GetServerCert(cert.ControlInterface);
if (success = 0) then
  begin
    Memo1.Lines.Add(socket.LastErrorText);
    Exit;
  end;
Memo1.Lines.Add('Server certificate subject: ' + cert.SubjectCN);
Memo1.Lines.Add('Issued by: ' + cert.IssuerCN);