Sample code for 30+ languages & platforms
Swift

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 Swift Downloads

Swift

func chilkatTest() {
    var success: Bool = false

    let socket = CkoSocket()!

    //  Connect to the server using TLS.
    var bTls: Bool = true
    var maxWaitMs: Int = 5000
    success = socket.connect(hostname: "example.com", port: 5000, ssl: bTls, maxWaitMs: maxWaitMs)
    if success == false {
        print("\(socket.lastErrorText!)")
        return
    }

    //  Copy the certificate presented by the remote TLS server, then inspect it.  This can be used to
    //  validate the server's identity.
    let cert = CkoCert()!
    success = socket.getServerCert(cert: cert)
    if success == false {
        print("\(socket.lastErrorText!)")
        return
    }

    print("Server certificate subject: \(cert.subjectCN!)")
    print("Issued by: \(cert.issuerCN!)")

}