fn new(
        key_id_store: Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>,
        backend: Ctx,
        slot_number: usize,
        user_pin: Option<String>,
    ) -> Option<Pkcs11Provider> {
        #[allow(clippy::mutex_atomic)]
        let pkcs11_provider = Pkcs11Provider {
            key_id_store,
            local_ids: RwLock::new(HashSet::new()),
            logged_sessions_counter: Mutex::new(0),
            backend,
            slot_number,
            user_pin,
        };
        {
            // The local scope allows to drop store_handle and local_ids_handle in order to return
            // the pkcs11_provider.
            let mut store_handle = pkcs11_provider
                .key_id_store
                .write()
                .expect("Key store lock poisoned");
            let mut local_ids_handle = pkcs11_provider
                .local_ids
                .write()
                .expect("Local ID lock poisoned");
            let mut to_remove: Vec<KeyTriple> = Vec::new();
            // Go through all PKCS 11 key triple to key ID mappings and check if they are still
            // present.
            // Delete those who are not present and add to the local_store the ones present.
            match store_handle.get_all(ProviderID::Pkcs11) {
                Ok(key_triples) => {
                    let session =
                        Session::new(&pkcs11_provider, ReadWriteSession::ReadOnly).ok()?;

                    for key_triple in key_triples.iter().cloned() {
                        let (key_id, _) = match get_key_id(key_triple, &*store_handle) {
                            Ok(key_id) => key_id,
                            Err(response_status) => {
                                error!("Error getting the Key ID for triple:\n{}\n(error: {}), continuing...", key_triple, response_status);
                                continue;
                            }
                        };
                        match pkcs11_provider.find_key(
                            session.session_handle(),
                            key_id,
                            KeyPairType::Any,
                        ) {
                            Ok(_) => {
                                warn!(
                                    "Key {} found in the PKCS 11 library, adding it.",
                                    key_triple
                                );
                                let _ = local_ids_handle.insert(key_id);
                            }
                            Err(ResponseStatus::PsaErrorDoesNotExist) => {
                                warn!(
                                    "Key {} not found in the PKCS 11 library, deleting it.",
                                    key_triple
                                );
                                to_remove.push(key_triple.clone());
                            }
                            Err(e) => {
                                error!("Error finding key objects: {}.", e);
                                return None;
                            }
                        }
                    }
                }
                Err(string) => {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            };
            for key_triple in to_remove.iter() {
                if let Err(string) = store_handle.remove(key_triple) {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            }
        }

        Some(pkcs11_provider)
    }
/// Creates and initialise a new instance of Pkcs11Provider.
/// Checks if there are not more keys stored in the Key ID Manager than in the PKCS 11 library
/// and if there are, delete them. Adds Key IDs currently in use in the local IDs store.
/// Returns `None` if the initialisation failed.
Creates and initialise a new instance of Pkcs11Provider.
Checks if there are not more keys stored in the Key ID Manager than in the PKCS 11 library
and if there are, delete them. Adds Key IDs currently in use in the local IDs store.
Returns `None` if the initialisation failed.
Creates and initialise a new instance of Pkcs11Provider . Checks if there are not more keys stored in the Key ID Manager than in the PKCS 11 library and if there are delete them . Adds Key IDs currently in use in the local IDs store . Returns ` None ` if the initialisation failed .
fn new(
        key_id_store: Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>,
        backend: Ctx,
        slot_number: usize,
        user_pin: Option<String>,
    ) -> Option<Pkcs11Provider> {
        #[allow(clippy::mutex_atomic)]
        let pkcs11_provider = Pkcs11Provider {
            key_id_store,
            local_ids: RwLock::new(HashSet::new()),
            logged_sessions_counter: Mutex::new(0),
            backend,
            slot_number,
            user_pin,
        };
        {
            // The local scope allows to drop store_handle and local_ids_handle in order to return
            // the pkcs11_provider.
            let mut store_handle = pkcs11_provider
                .key_id_store
                .write()
                .expect("Key store lock poisoned");
            let mut local_ids_handle = pkcs11_provider
                .local_ids
                .write()
                .expect("Local ID lock poisoned");
            let mut to_remove: Vec<KeyTriple> = Vec::new();
            // Go through all PKCS 11 key triple to key ID mappings and check if they are still
            // present.
            // Delete those who are not present and add to the local_store the ones present.
            match store_handle.get_all(ProviderID::Pkcs11) {
                Ok(key_triples) => {
                    let session =
                        Session::new(&pkcs11_provider, ReadWriteSession::ReadOnly).ok()?;
                    for key_triple in key_triples.iter().cloned() {
                        let (key_id, _) = match get_key_id(key_triple, &*store_handle) {
                            Ok(key_id) => key_id,
                            Err(response_status) => {
                                error!("Error getting the Key ID for triple:\n{}\n(error: {}), continuing...", key_triple, response_status);
                                continue;
                            }
                        };
                        match pkcs11_provider.find_key(
                            session.session_handle(),
                            key_id,
                            KeyPairType::Any,
                        ) {
                            Ok(_) => {
                                warn!(
                                    "Key {} found in the PKCS 11 library, adding it.",
                                    key_triple
                                );
                                let _ = local_ids_handle.insert(key_id);
                            }
                            Err(ResponseStatus::PsaErrorDoesNotExist) => {
                                warn!(
                                    "Key {} not found in the PKCS 11 library, deleting it.",
                                    key_triple
                                );
                                to_remove.push(key_triple.clone());
                            }
                            Err(e) => {
                                error!("Error finding key objects: {}.", e);
                                return None;
                            }
                        }
                    }
                }
                Err(string) => {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            };
            for key_triple in to_remove.iter() {
                if let Err(string) = store_handle.remove(key_triple) {
                    error!("Key ID Manager error: {}", string);
                    return None;
                }
            }
        }
        Some(pkcs11_provider)
    }
fn new ( key_id_store : Arc < RwLock < dyn ManageKeyIDs + Send + Sync > > , backend : Ctx , slot_number : usize , user_pin : Option < String > , ) -> Option < Pkcs11Provider > { # [ allow ( clippy mutex_atomic ) ] let pkcs11_provider = Pkcs11Provider { key_id_store , local_ids : RwLock :: new ( HashSet :: new ( ) ) , logged_sessions_counter : Mutex :: new ( 0 ) , backend , slot_number , user_pin , } ; { // The local scope allows to drop store_handle and local_ids_handle in order to return // the pkcs11_provider. let mut store_handle = pkcs11_provider . key_id_store . write ( ) . expect ( " " ) ; let mut local_ids_handle = pkcs11_provider . local_ids . write ( ) . expect ( " " ) ; let mut to_remove : Vec < KeyTriple > = Vec :: new ( ) ; // Go through all PKCS 11 key triple to key ID mappings and check if they are still // present. // Delete those who are not present and add to the local_store the ones present. match store_handle . get_all ( ProviderID :: Pkcs11 ) { Ok ( key_triples ) => { let session = Session :: new ( & pkcs11_provider , ReadWriteSession :: ReadOnly ) . ok ( ) ? ; for key_triple in key_triples . iter ( ) . cloned ( ) { let ( key_id , _ ) = match get_key_id ( key_triple , & * store_handle ) { Ok ( key_id ) => key_id , Err ( response_status ) => { error ! ( " \n \n " key_triple response_status ) ; continue ; } } ; match pkcs11_provider . find_key ( session . session_handle ( ) , key_id , KeyPairType :: Any , ) { Ok ( _ ) => { warn ! ( " " key_triple ) ; let _ = local_ids_handle . insert ( key_id ) ; } Err ( ResponseStatus :: PsaErrorDoesNotExist ) => { warn ! ( " " key_triple ) ; to_remove . push ( key_triple . clone ( ) ) ; } Err ( e ) => { error ! ( " " e ) ; return None ; } } } } Err ( string ) => { error ! ( " " string ) ; return None ; } } ; for key_triple in to_remove . iter ( ) { if let Err ( string ) = store_handle . remove ( key_triple ) { error ! ( " " string ) ; return None ; } } } Some ( pkcs11_provider ) }
Creates and initialise a new instance of Pkcs11Provider.
Creates and initialise a new instance of Pkcs11Provider .
key_id_store
Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>
backend
Ctx
slot_number
usize
user_pin
Option<String>
key_id_store
Arc<RwLock<dyn ManageKeyIDs + Send + Sync>>
backend
Ctx
slot_number
usize
user_pin
Option<String>