Swift
Swift
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 Swift Downloads
func chilkatTest() {
var success: Bool = false
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let http = CkoHttp()!
// We're getting the SSL/TLS certificate, so make sure to connect to the SSL/TLS port (443).
let sslCert = CkoCert()!
success = http.getServerCert(domain: "apple.com", port: 443, cert: sslCert)
if success == false {
print("\(http.lastErrorText!)")
return
}
let certChain = CkoCertChain()!
success = sslCert.buildChain(certChain: certChain)
if success == false {
print("\(sslCert.lastErrorText!)")
return
}
let cert = CkoCert()!
var i: Int = 0
var numCerts: Int = certChain.numCerts.intValue
while i < numCerts {
certChain.cert(at: i, cert: cert)
print("SubjectDN \(i): \(cert.subjectDN!)")
print("IssuerDN \(i): \(cert.issuerDN!)")
i = i + 1
}
// If the certificate chain reaches the root CA cert, then the last cert in the chain
// is the root CA cert.
if certChain.reachesRoot == true {
let caCert = CkoCert()!
certChain.cert(at: numCerts - 1, cert: caCert)
print("CA Root Cert: \(caCert.subjectDN!)")
}
}