Delphi DLL
Delphi DLL
Get the Server Certificate, Certificate Chain, and Root CA Certificate
See more HTTP Examples
Demonstrates how to get the HTTP server certificate, its certificate chain, and the root CA certificate.Chilkat Delphi DLL Downloads
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Http, CertChain, Cert;
...
procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
http: HCkHttp;
sslCert: HCkCert;
certChain: HCkCertChain;
cert: HCkCert;
i: Integer;
numCerts: Integer;
caCert: HCkCert;
begin
success := False;
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
http := CkHttp_Create();
// We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
sslCert := CkCert_Create();
success := CkHttp_GetServerCert(http,'apple.com',443,sslCert);
if (success = False) then
begin
Memo1.Lines.Add(CkHttp__lastErrorText(http));
Exit;
end;
certChain := CkCertChain_Create();
success := CkCert_BuildCertChain(sslCert,certChain);
if (success = False) then
begin
Memo1.Lines.Add(CkCert__lastErrorText(sslCert));
Exit;
end;
cert := CkCert_Create();
i := 0;
numCerts := CkCertChain_getNumCerts(certChain);
while i < numCerts do
begin
CkCertChain_CertAt(certChain,i,cert);
Memo1.Lines.Add('SubjectDN ' + IntToStr(i) + ': ' + CkCert__subjectDN(cert));
Memo1.Lines.Add('IssuerDN ' + IntToStr(i) + ': ' + CkCert__issuerDN(cert));
i := i + 1;
end;
// If the certificate chain reaches the root CA cert, then the last cert in the chain
// is the root CA cert.
if (CkCertChain_getReachesRoot(certChain) = True) then
begin
caCert := CkCert_Create();
CkCertChain_CertAt(certChain,numCerts - 1,caCert);
Memo1.Lines.Add('CA Root Cert: ' + CkCert__subjectDN(caCert));
end;
CkHttp_Dispose(http);
CkCert_Dispose(sslCert);
CkCertChain_Dispose(certChain);
CkCert_Dispose(cert);
CkCert_Dispose(caCert);
end;