Rust Requires Chilkat v10.1.2+
Rust
SSL Client Certificate
See more Socket/SSL/TLS Examples
Demonstrates how to connect to an SSL server using a client-side certificate, send a simple message, receive a simple response, and disconnect.Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let socket = chilkat::Socket::new();
// Create an instance of a certificate store object, load a PFX file,
// locate the certificate we need, and use it for signing.
// (a PFX file may contain more than one certificate.)
let cert_store = chilkat::CertStore::new();
// The 1st argument is the filename, the 2nd arg is the
// PFX file's password:
if cert_store.load_pfx_file("chilkat_secret.pfx", "secret").is_err() {
println!("{}", cert_store.last_error_text());
return;
}
// Find the certificate by the subject common name:
let json_cn = chilkat::JsonObject::new();
let _ = json_cn.update_string("CN", "cert common name");
let cert = chilkat::Cert::new();
if cert_store.find_cert(&json_cn, &cert).is_err() {
println!("{}", cert_store.last_error_text());
return;
}
let _ = socket.set_ssl_client_cert(&cert).is_ok();
let ssl = true;
let max_wait_millisec = 20000;
// The SSL server hostname may be an IP address, a domain name,
// or "localhost". You'll need to change this:
let ssl_server_host = "123.123.88.88".to_string();
let ssl_server_port = 8123;
// Connect to the SSL server:
if socket.connect(&ssl_server_host, ssl_server_port, ssl, max_wait_millisec).is_err() {
println!("{}", socket.last_error_text());
return;
}
// Set maximum timeouts for reading an writing (in millisec)
socket.set_max_read_idle_ms(20000);
socket.set_max_send_idle_ms(20000);
// Send a "Hello Server! -EOM-" message:
if socket.send_string("Hello Server! -EOM-").is_err() {
println!("{}", socket.last_error_text());
return;
}
// The server (in this example) is going to send a "Hello Client! -EOM-"
// message. Read it:
let Ok(received_msg) = socket.receive_until_match("-EOM-") else {
println!("{}", socket.last_error_text());
return;
};
// Close the connection with the server
// Wait a max of 20 seconds (20000 millsec)
let _ = socket.close(20000).is_ok();
println!("{}", received_msg);