Rust
Rust
ScMinidriver - Import a Certificate to IDPrime MD T=0 Smart Card
See more ScMinidriver Examples
Demonstrates how to import a certificate and its private key to a key container on an ID Prime MD T=0 smartcard.Note: Requires Chilkat v9.5.0.88 or later. This example only runs on Windows because ScMinidriver is a Windows-only class.
Chilkat Rust Downloads
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
let scmd = chilkat::ScMinidriver::new();
// Reader names (smart card readers or USB tokens) can be discovered
// via List Readers or Find Smart Cards
let reader_name = "SCM Microsystems Inc. SCR33x USB Smart Card Reader 0".to_string();
if scmd.acquire_context(&reader_name).is_err() {
println!("{}", scmd.last_error_text());
return;
}
// If successful, the name of the currently inserted smart card is available:
println!("Card name: {}", scmd.card_name());
// The IDPRime MD smart card has 4 different PIN roles:
// "user" -- Primary Card PIN
// "admin" -- Administrator PIN
// "3" -- Digital Signature PIN
// "4" -- Unblock only PIN (PUK)
// To import a certificate to the "IDPrime MD T=0" smart card, we must first PIN authenticate using "user", and then also PIN authenticate using "3" (the Digital Signature PIN)
let pin_id = "user".to_string();
// (Of course, use your PIN which may be different than "0000")
let mut retval = scmd.pin_authenticate(&pin_id, "0000");
if retval != 0 {
println!("PIN Authentication failed.");
let _ = scmd.delete_context();
return;
}
let cert = chilkat::Cert::new();
// Load the cert + private key from a .p12/.pfx
// We got this .p12 from https://badssl.com/download/
let password = "badssl.com".to_string();
if cert.load_pfx_file("qa_data/pfx/badssl.com-client.p12", &password).is_err() {
println!("{}", cert.last_error_text());
let _ = scmd.delete_context();
return;
}
// Also authenticate using "3", the digital signature PIN.
// (Of course, use your PIN which may be different than "12345678")
retval = scmd.pin_authenticate("3", "12345678");
if retval != 0 {
println!("PIN Authentication failed.");
let _ = scmd.delete_context();
return;
}
// Let's import this certificate as the "signature" key/cert in key container #6.
let container_index = 6;
let key_spec = "sig".to_string();
// Note the last argument (the pin ID) is "3". This is the required PIN ID for the IDPrime MD T=0 smart card.
if scmd.import_cert(&cert, container_index, &key_spec, "3").is_err() {
println!("{}", scmd.last_error_text());
} else {
println!("Successfully imported the cert + private key onto the smart card.");
}
// Delete the context when finished with the card.
if scmd.delete_context().is_err() {
println!("{}", scmd.last_error_text());
}