Sample code for 30+ languages & platforms
Rust Requires Chilkat v11.0.0+

Examine Client Certificates for an Accepted TLS Connection

See more Socket/SSL/TLS Examples

Demonstrates how to access the client certificates for a TLS connection accepted by your application acting as the server.

Chilkat Rust Downloads

Rust

// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.

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

// An SSL/TLS server needs a digital certificate.  This example loads it from a PFX file.
// This is the server's certificate.

let cert = chilkat::Cert::new();
if cert.load_pfx_file("qa_data/serverCert/myServerCert.pfx", "pfx_password").is_err() {
    println!("{}", cert.last_error_text());
    return;
}

// To accept client client certificates in the TLS handshake,
// we must indicate a list of acceptable client certificate root CA DN's
// that are allowed.  (DN is an acronym for Distinguished Name.)
// Call AddSslAcceptableClientCaDn once for each acceptable CA DN.
// Here are a few examples so you can see the general format of a DN.
let _ = listen_ssl_socket.add_ssl_acceptable_client_ca_dn("C=SE, O=AddTrust AB, OU=AddTrust External TTP Network, CN=AddTrust External CA Root");
let _ = listen_ssl_socket.add_ssl_acceptable_client_ca_dn("O=Digital Signature Trust Co., CN=DST Root CA X3");

// Initialize with our server's TLS certificate.
if listen_ssl_socket.init_ssl_server(&cert).is_err() {
    println!("{}", listen_ssl_socket.last_error_text());
    return;
}

// Bind and listen on a port:
let my_port = 8123;
// Allow for a max of 5 queued connect requests.
let back_log = 5;
if listen_ssl_socket.bind_and_listen(my_port, back_log).is_err() {
    println!("{}", listen_ssl_socket.last_error_text());
    return;
}

// Accept the next incoming connection.
let max_wait_millisec = 20000;

let client_sock = chilkat::Socket::new();
if listen_ssl_socket.accept_next(max_wait_millisec, &client_sock).is_err() {
    println!("{}", listen_ssl_socket.last_error_text());
    return;
}

// Examine the client certs chain.  The 1st cert will be the client certificate, and
// the subsequent certs will be the certs in the chain of authentication.
let num_client_certs = client_sock.num_received_client_certs();
println!("numClientCerts = {}", num_client_certs);

let client_cert = chilkat::Cert::new();
let mut i = 0;
while i < num_client_certs {
    let _ = client_sock.get_rcvd_client_cert(i, &client_cert);
    println!("{}", client_cert.subject_dn());
    i = i + 1;
}

// Close the connection with the client
let _ = client_sock.close(1000).is_ok();