/// Given params to read a HSM, return a `Sender` that uses this HSM to sign
/// requests.
Given params to read a HSM, return a `Sender` that uses this HSM to sign
requests.
Given params to read a HSM return a ` Sender ` that uses this HSM to sign requests .
pub fn make_hsm_sender(hsm_slot: &str, key_id: &str, pin: &str) -> Sender {
    // Set up the agent using an HSM, first perform a simple test for correctness of
    // the PIN.
    UtilityCommand::try_to_attach_hsm();
    let res = UtilityCommand::new(
        "pkcs11-tool".to_string(),
        vec![
            "--read-object",
            "--slot",
            hsm_slot,
            "--type",
            "pubkey",
            "--id",
            key_id,
            "--pin",
            pin,
        ]
        .into_iter()
        .map(|s| s.to_string())
        .collect::<Vec<_>>(),
    )
    .execute();
    if let Err(UtilityCommandError::Failed(err, _status)) = res {
        // The key id is not found.
        if err.contains("object not found") {
            panic!("Cannot find key with id {}", key_id);
        }
        // The pin is incorrect.
        if err.contains("CKR_PIN_INCORRECT") {
            panic!("The PIN that was given is incorrect");
        }
    }
    let pub_key = UtilityCommand::read_public_key(Some(hsm_slot), Some(key_id))
        .execute()
        .unwrap_or_else(|e| {
            panic!(
                "Error while trying to read the public key from the HSM. Underlying error: {}",
                e
            )
        });
    let key_id = key_id.to_string();
    let pin = pin.to_string();
    let hsm_slot = hsm_slot.to_string();
    let sender = Sender::from_external_hsm(
        pub_key,
        std::sync::Arc::new(move |input| {
            UtilityCommand::sign_message(input.to_vec(), Some(&hsm_slot), Some(&pin), Some(&key_id))
                .execute()
                .map_err(|e| Box::new(e) as Box<dyn std::error::Error>)
        }),
    );
    UtilityCommand::try_to_detach_hsm();
    sender
}
pub fn make_hsm_sender ( hsm_slot : & str , key_id : & str , pin : & str ) -> Sender { // Set up the agent using an HSM, first perform a simple test for correctness of // the PIN. UtilityCommand :: try_to_attach_hsm ( ) ; let res = UtilityCommand :: new ( " " . to_string ( ) , vec ! [ " " " " hsm_slot " " " " " " key_id " " pin ] . into_iter ( ) . map ( | s | s . to_string ( ) ) . collect :: < Vec < _ > > ( ) , ) . execute ( ) ; if let Err ( UtilityCommandError :: Failed ( err , _status ) ) = res { // The key id is not found. if err . contains ( " " ) { panic ! ( " " key_id ) ; } // The pin is incorrect. if err . contains ( " " ) { panic ! ( " " ) ; } } let pub_key = UtilityCommand :: read_public_key ( Some ( hsm_slot ) , Some ( key_id ) ) . execute ( ) . unwrap_or_else ( | e | { panic ! ( " " e ) } ) ; let key_id = key_id . to_string ( ) ; let pin = pin . to_string ( ) ; let hsm_slot = hsm_slot . to_string ( ) ; let sender = Sender :: from_external_hsm ( pub_key , std :: sync :: Arc :: new ( move | input | { UtilityCommand :: sign_message ( input . to_vec ( ) , Some ( & hsm_slot ) , Some ( & pin ) , Some ( & key_id ) ) . execute ( ) . map_err ( | e | Box :: new ( e ) as Box < dyn std :: error :: Error > ) } ) , ) ; UtilityCommand :: try_to_detach_hsm ( ) ; sender }
Given params to read a HSM, return a `Sender` that uses this HSM to sign
requests.
Given params to read a HSM return a ` Sender ` that uses this HSM to sign requests .
hsm_slot
&str
key_id
&str
pin
&str