Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: SlotInfo refactor and reimplement flags types #63

Merged
merged 20 commits into from
Nov 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cryptoki-sys/pkcs11.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ struct ck_token_info
#define CKF_SO_PIN_FINAL_TRY (1UL << 21)
#define CKF_SO_PIN_LOCKED (1UL << 22)
#define CKF_SO_PIN_TO_BE_CHANGED (1UL << 23)
#define CKF_ERROR_STATE (1UL << 24)

#define CK_UNAVAILABLE_INFORMATION ((unsigned long)-1L)
#define CK_EFFECTIVELY_INFINITE (0UL)
Expand Down Expand Up @@ -1044,6 +1045,8 @@ struct ck_aes_cbc_encrypt_data_params {
#define CKF_EXTENSION ((unsigned long) (1UL << 31))

#define CKF_EC_F_P (1UL << 20)
#define CKF_EC_F_2M (1UL << 21)
#define CKF_EC_ECPARAMETERS (1UL << 22)
#define CKF_EC_NAMEDCURVE (1UL << 23)
#define CKF_EC_UNCOMPRESS (1UL << 24)
#define CKF_EC_COMPRESS (1UL << 25)
Expand Down
3 changes: 3 additions & 0 deletions cryptoki-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ pub const CKF_SO_PIN_COUNT_LOW: CK_FLAGS = 0x00100000;
pub const CKF_SO_PIN_FINAL_TRY: CK_FLAGS = 0x00200000;
pub const CKF_SO_PIN_LOCKED: CK_FLAGS = 0x00400000;
pub const CKF_SO_PIN_TO_BE_CHANGED: CK_FLAGS = 0x00800000;
pub const CKF_ERROR_STATE: CK_FLAGS = 0x01000000;
pub const CK_UNAVAILABLE_INFORMATION: CK_ULONG = !0;
pub const CK_EFFECTIVELY_INFINITE: CK_ULONG = 0;
pub const CK_INVALID_HANDLE: CK_ULONG = 0;
Expand Down Expand Up @@ -689,6 +690,8 @@ pub const CKF_UNWRAP: CK_FLAGS = 0x00040000;
pub const CKF_DERIVE: CK_FLAGS = 0x00080000;
pub const CKF_EXTENSION: CK_FLAGS = 0x80000000;
pub const CKF_EC_F_P: CK_FLAGS = 0x00100000;
pub const CKF_EC_F_2M: CK_FLAGS = 0x00200000;
pub const CKF_EC_ECPARAMETERS: CK_FLAGS = 0x00400000;
pub const CKF_EC_NAMEDCURVE: CK_FLAGS = 0x00800000;
pub const CKF_EC_UNCOMPRESS: CK_FLAGS = 0x01000000;
pub const CKF_EC_COMPRESS: CK_FLAGS = 0x02000000;
Expand Down
5 changes: 3 additions & 2 deletions cryptoki/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mod locking;
mod session_management;
mod slot_token_management;

use cryptoki_sys::{CK_FALSE, CK_TRUE};
pub use flags::*;
pub use info::*;
pub use locking::*;
Expand Down Expand Up @@ -82,7 +83,7 @@ impl Pkcs11 {

/// Get all slots available with a token
pub fn get_slots_with_token(&self) -> Result<Vec<Slot>> {
slot_token_management::get_slots_with_token(self)
slot_token_management::get_slots(self, CK_TRUE)
}

/// Get all slots available with a token
Expand All @@ -92,7 +93,7 @@ impl Pkcs11 {

/// Get all slots
pub fn get_all_slots(&self) -> Result<Vec<Slot>> {
slot_token_management::get_all_slots(self)
slot_token_management::get_slots(self, CK_FALSE)
}

/// Initialize a token
Expand Down
97 changes: 32 additions & 65 deletions cryptoki/src/context/slot_token_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,40 @@ use crate::error::{Result, Rv};
use crate::label_from_str;
use crate::mechanism::{MechanismInfo, MechanismType};
use crate::slot::{Slot, SlotInfo, TokenInfo};
use cryptoki_sys::{CK_MECHANISM_INFO, CK_SLOT_INFO, CK_TOKEN_INFO};
use cryptoki_sys::{CK_BBOOL, CK_MECHANISM_INFO, CK_SLOT_INFO, CK_TOKEN_INFO};
use std::convert::TryInto;

use crate::error::RvError::BufferTooSmall;

// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn get_slots_with_token(ctx: &Pkcs11) -> Result<Vec<Slot>> {
pub(super) fn get_slots(ctx: &Pkcs11, with_token: CK_BBOOL) -> Result<Vec<Slot>> {
let mut slot_count = 0;

unsafe {
Rv::from(get_pkcs11!(ctx, C_GetSlotList)(
cryptoki_sys::CK_TRUE,
std::ptr::null_mut(),
&mut slot_count,
))
.into_result()?;
}

let mut slots = vec![0; slot_count.try_into()?];

unsafe {
Rv::from(get_pkcs11!(ctx, C_GetSlotList)(
cryptoki_sys::CK_TRUE,
slots.as_mut_ptr(),
&mut slot_count,
))
.into_result()?;
let rval = unsafe {
get_pkcs11!(ctx, C_GetSlotList)(with_token, std::ptr::null_mut(), &mut slot_count)
};
Rv::from(rval).into_result()?;

let mut slots;
loop {
slots = vec![0; slot_count as usize];
let rval = unsafe {
get_pkcs11!(ctx, C_GetSlotList)(with_token, slots.as_mut_ptr(), &mut slot_count)
};
// Account for a race condition between the call to get the
// slot_count and the last call in which the number of slots grew.
// In this case, slot_count will have been updated to the larger amount
// and we want to loop again with a resized buffer.
if !matches!(Rv::from(rval), Rv::Error(BufferTooSmall)) {
// Account for other possible error types
Rv::from(rval).into_result()?;
// Otherwise, we have a valid list to process
break;
}
}

let mut slots: Vec<Slot> = slots.into_iter().map(Slot::new).collect();

// This should always truncate slots.
slots.resize(slot_count.try_into()?, Slot::new(0));

Ok(slots)
// Account for the same race condition, but with a shrinking slot_count
slots.truncate(slot_count as usize);
Ok(slots.into_iter().map(Slot::new).collect())
}

// See public docs on stub in parent mod.rs
Expand All @@ -52,7 +52,7 @@ pub(super) fn get_slots_with_initialized_token(ctx: &Pkcs11) -> Result<Vec<Slot>
.into_iter()
.filter_map(|slot| match ctx.get_token_info(slot) {
Ok(token_info) => {
if token_info.flags().token_initialized() {
if token_info.token_initialized() {
Some(Ok(slot))
} else {
None
Expand All @@ -63,39 +63,6 @@ pub(super) fn get_slots_with_initialized_token(ctx: &Pkcs11) -> Result<Vec<Slot>
.collect()
}

// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn get_all_slots(ctx: &Pkcs11) -> Result<Vec<Slot>> {
let mut slot_count = 0;

unsafe {
Rv::from(get_pkcs11!(ctx, C_GetSlotList)(
cryptoki_sys::CK_FALSE,
std::ptr::null_mut(),
&mut slot_count,
))
.into_result()?;
}

let mut slots = vec![0; slot_count.try_into()?];

unsafe {
Rv::from(get_pkcs11!(ctx, C_GetSlotList)(
cryptoki_sys::CK_FALSE,
slots.as_mut_ptr(),
&mut slot_count,
))
.into_result()?;
}

let mut slots: Vec<Slot> = slots.into_iter().map(Slot::new).collect();

// This should always truncate slots.
slots.resize(slot_count.try_into()?, Slot::new(0));

Ok(slots)
}

// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn init_token(ctx: &Pkcs11, slot: Slot, pin: &str, label: &str) -> Result<()> {
Expand All @@ -121,7 +88,7 @@ pub(super) fn get_slot_info(ctx: &Pkcs11, slot: Slot) -> Result<SlotInfo> {
&mut slot_info,
))
.into_result()?;
Ok(SlotInfo::new(slot_info))
Ok(SlotInfo::from(slot_info))
}
}

Expand All @@ -135,7 +102,7 @@ pub(super) fn get_token_info(ctx: &Pkcs11, slot: Slot) -> Result<TokenInfo> {
&mut token_info,
))
.into_result()?;
Ok(TokenInfo::new(token_info))
Ok(TokenInfo::from(token_info))
}
}

Expand Down Expand Up @@ -188,6 +155,6 @@ pub(super) fn get_mechanism_info(
&mut mechanism_info,
))
.into_result()?;
Ok(MechanismInfo::new(mechanism_info))
Ok(MechanismInfo::from(mechanism_info))
}
}
Loading