Sample code for 30+ languages & platforms
Rust

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

Rust

let socket = chilkat::Socket::new();

// Connect to the server using TLS.
let b_tls = true;
let max_wait_ms = 5000;
if socket.connect("example.com", 5000, b_tls, max_wait_ms).is_err() {
    println!("{}", socket.last_error_text());
    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 = chilkat::Cert::new();
if socket.get_server_cert(&cert).is_err() {
    println!("{}", socket.last_error_text());
    return;
}

println!("Server certificate subject: {}", cert.subject_cn());
println!("Issued by: {}", cert.issuer_cn());