Rust
Rust
SFTP Authentication using X.509 Certificates
See more SFTP Examples
Demonstrates how to authenticate with an SSH/SFTP server using an certificate's private key.Note: See X.509v3 Certificates for SSH Authentication for more information.
Chilkat Rust Downloads
// This example assumes the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let sftp = chilkat::SFtp::new();
let hostname = "sftp.example.com".to_string();
let port = 22;
if sftp.connect(&hostname, port).is_err() {
println!("{}", sftp.last_error_text());
return;
}
// Load the cert + private key from a .pfx.
// Note: Chilkat provides methods for loading certs and private keys from many sources, including smart cards and USB tokens (HSM's)
let cert = chilkat::Cert::new();
if cert.load_pfx_file("qa_data/pfx/example.pfx", "pfx_password").is_err() {
println!("{}", cert.last_error_text());
return;
}
// Get the cert's private key (as PEM) to be used for SSH authentication.
// (The public key is installed on the server.)
let Ok(priv_key_pem) = cert.get_private_key_pem() else {
println!("{}", cert.last_error_text());
return;
};
let key = chilkat::SshKey::new();
// Load a private key from a PEM string:
if key.from_open_ssh_private_key(&priv_key_pem).is_err() {
println!("{}", key.last_error_text());
return;
}
// Authenticate with the SSH server.
if sftp.authenticate_pk("myLogin", &key).is_err() {
println!("{}", sftp.last_error_text());
return;
}
println!("Public-Key Authentication Successful!");