Skip to content

Commit

Permalink
Split the implementation of session and pkcs11
Browse files Browse the repository at this point in the history
I know this is uncommon, and I don't know if that's recent or not, but
Rust allows for an implementation of an object to happen in submodules.

I believe this was the intention with the various inline implementations
so I'm submitting this alternative.

Signed-off-by: Arthur Gautier <[email protected]>
  • Loading branch information
baloo committed Feb 13, 2023
1 parent 66d83e8 commit 7d55987
Show file tree
Hide file tree
Showing 12 changed files with 829 additions and 1,056 deletions.
62 changes: 0 additions & 62 deletions cryptoki/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@ mod locking;
mod session_management;
mod slot_token_management;

use cryptoki_sys::{CK_FALSE, CK_TRUE};
pub use general_purpose::*;
pub use info::*;
pub use locking::*;

use crate::error::{Error, Result, Rv};
use crate::mechanism::{MechanismInfo, MechanismType};
use crate::session::Session;
use crate::slot::{Slot, SlotInfo, TokenInfo};

use derivative::Derivative;
use log::error;
Expand Down Expand Up @@ -126,64 +122,6 @@ impl Pkcs11 {
get_library_info(self)
}

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

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

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

/// Initialize a token
///
/// Currently will use an empty label for all tokens.
pub fn init_token(&self, slot: Slot, pin: &str, label: &str) -> Result<()> {
slot_token_management::init_token(self, slot, pin, label)
}

/// Returns the slot info
pub fn get_slot_info(&self, slot: Slot) -> Result<SlotInfo> {
slot_token_management::get_slot_info(self, slot)
}

/// Returns information about a specific token
pub fn get_token_info(&self, slot: Slot) -> Result<TokenInfo> {
slot_token_management::get_token_info(self, slot)
}

/// Get all mechanisms support by a slot
pub fn get_mechanism_list(&self, slot: Slot) -> Result<Vec<MechanismType>> {
slot_token_management::get_mechanism_list(self, slot)
}

/// Get detailed information about a mechanism for a slot
pub fn get_mechanism_info(&self, slot: Slot, type_: MechanismType) -> Result<MechanismInfo> {
slot_token_management::get_mechanism_info(self, slot, type_)
}

/// Open a new Read-Only session
///
/// For a Read-Write session, use `open_rw_session`
///
/// Note: No callback is set when opening the session.
pub fn open_ro_session(&self, slot_id: Slot) -> Result<Session> {
session_management::open_session(self, slot_id, false)
}

/// Open a new Read/Write session
///
/// Note: No callback is set when opening the session.
pub fn open_rw_session(&self, slot_id: Slot) -> Result<Session> {
session_management::open_session(self, slot_id, true)
}

/// Check whether a given PKCS11 spec-defined function is supported by this implementation
pub fn is_fn_supported(&self, function: Function) -> bool {
is_fn_supported(self, function)
Expand Down
58 changes: 38 additions & 20 deletions cryptoki/src/context/session_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,45 @@ use crate::error::{Result, Rv};
use crate::session::Session;
use crate::slot::Slot;
use std::convert::TryInto;
// See public docs on stub in parent mod.rs
#[inline(always)]
pub(super) fn open_session(ctx: &Pkcs11, slot_id: Slot, read_write: bool) -> Result<Session> {
let mut session_handle = 0;

let flags = if read_write {
CKF_SERIAL_SESSION | CKF_RW_SESSION
} else {
CKF_SERIAL_SESSION
};
unsafe {
Rv::from(get_pkcs11!(ctx, C_OpenSession)(
slot_id.try_into()?,
flags,
// TODO: abstract those types or create new functions for callbacks
std::ptr::null_mut(),
None,
&mut session_handle,
))
.into_result()?;
impl Pkcs11 {
#[inline(always)]
fn open_session(&self, slot_id: Slot, read_write: bool) -> Result<Session> {
let mut session_handle = 0;

let flags = if read_write {
CKF_SERIAL_SESSION | CKF_RW_SESSION
} else {
CKF_SERIAL_SESSION
};
unsafe {
Rv::from(get_pkcs11!(self, C_OpenSession)(
slot_id.try_into()?,
flags,
// TODO: abstract those types or create new functions for callbacks
std::ptr::null_mut(),
None,
&mut session_handle,
))
.into_result()?;
}

Ok(Session::new(session_handle, self.clone()))
}

Ok(Session::new(session_handle, ctx.clone()))
/// Open a new Read-Only session
///
/// For a Read-Write session, use `open_rw_session`
///
/// Note: No callback is set when opening the session.
pub fn open_ro_session(&self, slot_id: Slot) -> Result<Session> {
self.open_session(slot_id, false)
}

/// Open a new Read/Write session
///
/// Note: No callback is set when opening the session.
pub fn open_rw_session(&self, slot_id: Slot) -> Result<Session> {
self.open_session(slot_id, true)
}
}
Loading

0 comments on commit 7d55987

Please sign in to comment.