Skip to content

Commit

Permalink
Added new methods to fix issue_275 - get slots with initialized token…
Browse files Browse the repository at this point in the history
…, for PKCS 11 Provider

Signed-off-by: Adriano Pallavicino <[email protected]>
  • Loading branch information
Sven-bg committed Jul 27, 2021
1 parent 2d9ba34 commit 4b9aca4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
18 changes: 18 additions & 0 deletions cryptoki/src/functions/slot_token_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ impl Pkcs11 {
Ok(slots)
}

/// Get all slots available with a token
pub fn get_slots_with_initialized_token(&self) -> Result<Vec<Slot>> {
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<Vec<Slot>> {
let mut slot_count = 0;
Expand Down
6 changes: 6 additions & 0 deletions cryptoki/src/types/slot_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4b9aca4

Please sign in to comment.