From 5c093e8ff985f3435d45c9fb7172984bf20c202b Mon Sep 17 00:00:00 2001 From: Adriano Pallavicino Date: Thu, 1 Jul 2021 08:41:45 +0100 Subject: [PATCH] Added new methods to fix issue_275 - get slots with initialized token, for PKCS 11 Provider Signed-off-by: Adriano Pallavicino --- .../src/functions/slot_token_management.rs | 19 +++++++++++++++++++ cryptoki/src/types/slot_token.rs | 6 ++++++ 2 files changed, 25 insertions(+) diff --git a/cryptoki/src/functions/slot_token_management.rs b/cryptoki/src/functions/slot_token_management.rs index 2912e5a7..428fa26a 100644 --- a/cryptoki/src/functions/slot_token_management.rs +++ b/cryptoki/src/functions/slot_token_management.rs @@ -47,6 +47,25 @@ impl Pkcs11 { Ok(slots) } + /// Get all slots available with a token + pub fn get_slots_with_initialized_token(&self) -> Result> { + let slots = self.get_slots_with_token()?; + + slots + .into_iter() + .filter_map(|slot| match self.get_token_info(slot) { + Ok(token_info) => { + if token_info.get_flags().token_initialized() { + Some(Ok(slot)) + } else { + None + } + } + Err(e) => Some(Err(e)), + }) + .collect() + } + /// Get all slots pub fn get_all_slots(&self) -> Result> { let mut slot_count = 0; diff --git a/cryptoki/src/types/slot_token.rs b/cryptoki/src/types/slot_token.rs index 739e782b..ac189141 100644 --- a/cryptoki/src/types/slot_token.rs +++ b/cryptoki/src/types/slot_token.rs @@ -6,6 +6,7 @@ //! Slot and token types +use crate::types::Flags; use crate::{Error, Result}; use cryptoki_sys::{CK_SLOT_ID, CK_TOKEN_INFO}; use std::convert::{TryFrom, TryInto}; @@ -68,6 +69,11 @@ impl TokenInfo { .trim_end() .to_string() } + + /// Returns the Token's flags + pub fn get_flags(&self) -> Flags { + self.val.flags.into() + } } impl Deref for TokenInfo {