Skip to content

Commit

Permalink
Merge pull request #35 from Sven-bg/issue_375
Browse files Browse the repository at this point in the history
Added new methods to fix issue 375 - get slots with initialized token…
  • Loading branch information
hug-dev authored Jul 27, 2021
2 parents 2d9ba34 + 5c093e8 commit 2cd54bb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
19 changes: 19 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,25 @@ 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 2cd54bb

Please sign in to comment.