Skip to content

Commit 40cde2a

Browse files
Add pReserved and other PKCS#11 flags for initialize
Signed-off-by: Alexandru Placinta <[email protected]>
1 parent 2702bba commit 40cde2a

File tree

3 files changed

+103
-14
lines changed

3 files changed

+103
-14
lines changed

cryptoki/src/context/general_purpose.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::fmt::Display;
1111

1212
// See public docs on stub in parent mod.rs
1313
#[inline(always)]
14-
pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()> {
14+
pub(super) fn initialize<T>(ctx: &Pkcs11, init_args: CInitializeArgs<T>) -> Result<()> {
1515
// if no args are specified, library expects NULL
1616
let mut init_args = CK_C_INITIALIZE_ARGS::from(init_args);
1717
let init_args_ptr = &mut init_args;

cryptoki/src/context/locking.rs

Lines changed: 101 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,121 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Locking related type
44
5-
use cryptoki_sys::{CKF_OS_LOCKING_OK, CK_FLAGS};
5+
use cryptoki_sys::{
6+
CKF_LIBRARY_CANT_CREATE_OS_THREADS, CKF_OS_LOCKING_OK, CK_CREATEMUTEX, CK_DESTROYMUTEX,
7+
CK_FLAGS, CK_LOCKMUTEX, CK_UNLOCKMUTEX,
8+
};
69

7-
use std::ptr;
10+
use std::{os::raw::c_void, ptr};
811

9-
/// Argument for the initialize function
12+
/// Provides function pointers for mutex-handling to ensure safe multi-threaded access.
13+
#[derive(Copy, Clone, Debug)]
14+
pub struct CustomMutexHandling {
15+
create_mutex: CK_CREATEMUTEX,
16+
destroy_mutex: CK_DESTROYMUTEX,
17+
lock_mutex: CK_LOCKMUTEX,
18+
unlock_mutex: CK_UNLOCKMUTEX,
19+
}
20+
21+
/// Flags to set for the initialize function
1022
#[derive(Copy, Clone, Debug)]
11-
pub enum CInitializeArgs {
12-
/// The library can use the native OS library for locking
23+
pub enum CInitializeFlags {
24+
/// The library won’t be accessed from multiple threads simultaneously
25+
None,
26+
/// The library may not create its own threads
27+
NoOsThreads,
28+
/// The library can use the native OS library for locking or the custom
1329
OsThreads,
14-
// TODO: add variants for custom mutexes here and no multithreading, safety implications for
15-
// that.
30+
/// The library needs to use the supplied function pointers
31+
/// for mutex-handling to ensure safe multi-threaded access.
32+
CustomMutexHandling(CustomMutexHandling),
33+
/// The library needs to use either the native operating system primitives
34+
/// or the supplied function pointers for mutex-handling to ensure safe
35+
/// multi-threaded access
36+
OsThreadsOrCustomMutexHandling(CustomMutexHandling),
37+
}
38+
39+
#[derive(Debug)]
40+
/// Argument for the initialize function
41+
pub struct CInitializeArgs<T> {
42+
flags: CInitializeFlags,
43+
p_reserved: Option<Box<T>>,
44+
}
45+
46+
impl<T> CInitializeArgs<T> {
47+
/// Create a new `CInitializeArgs` with the given flags
48+
pub fn new(flags: CInitializeFlags) -> Self {
49+
Self {
50+
flags,
51+
p_reserved: None,
52+
}
53+
}
54+
55+
/// Create a new `CInitializeArgs` with the given flags and reserved pointer
56+
pub fn new_with_reserved(flags: CInitializeFlags, p_reserved: Box<T>) -> Self {
57+
Self {
58+
flags,
59+
p_reserved: Some(p_reserved),
60+
}
61+
}
1662
}
1763

18-
impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS {
19-
fn from(c_initialize_args: CInitializeArgs) -> Self {
64+
impl<T> From<CInitializeArgs<T>> for cryptoki_sys::CK_C_INITIALIZE_ARGS {
65+
fn from(c_initialize_args: CInitializeArgs<T>) -> Self {
2066
let mut flags = CK_FLAGS::default();
21-
match c_initialize_args {
22-
CInitializeArgs::OsThreads => {
67+
let p_reserved = c_initialize_args
68+
.p_reserved
69+
.map(Box::into_raw)
70+
.unwrap_or_else(ptr::null_mut) as *mut c_void;
71+
72+
match c_initialize_args.flags {
73+
CInitializeFlags::None => Self {
74+
CreateMutex: None,
75+
DestroyMutex: None,
76+
LockMutex: None,
77+
UnlockMutex: None,
78+
flags,
79+
pReserved: p_reserved,
80+
},
81+
CInitializeFlags::NoOsThreads => {
82+
flags |= CKF_LIBRARY_CANT_CREATE_OS_THREADS;
83+
Self {
84+
flags,
85+
CreateMutex: None,
86+
DestroyMutex: None,
87+
LockMutex: None,
88+
UnlockMutex: None,
89+
pReserved: p_reserved,
90+
}
91+
}
92+
CInitializeFlags::OsThreads => {
2393
flags |= CKF_OS_LOCKING_OK;
2494
Self {
2595
flags,
2696
CreateMutex: None,
2797
DestroyMutex: None,
2898
LockMutex: None,
2999
UnlockMutex: None,
30-
pReserved: ptr::null_mut(),
100+
pReserved: p_reserved,
101+
}
102+
}
103+
CInitializeFlags::CustomMutexHandling(custom_mutex_handling) => Self {
104+
flags,
105+
CreateMutex: custom_mutex_handling.create_mutex,
106+
DestroyMutex: custom_mutex_handling.destroy_mutex,
107+
LockMutex: custom_mutex_handling.lock_mutex,
108+
UnlockMutex: custom_mutex_handling.unlock_mutex,
109+
pReserved: p_reserved,
110+
},
111+
CInitializeFlags::OsThreadsOrCustomMutexHandling(custom_mutex_handling) => {
112+
flags |= CKF_OS_LOCKING_OK;
113+
Self {
114+
flags,
115+
CreateMutex: custom_mutex_handling.create_mutex,
116+
DestroyMutex: custom_mutex_handling.destroy_mutex,
117+
LockMutex: custom_mutex_handling.lock_mutex,
118+
UnlockMutex: custom_mutex_handling.unlock_mutex,
119+
pReserved: p_reserved,
31120
}
32121
}
33122
}

cryptoki/src/context/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl Pkcs11 {
189189
}
190190

191191
/// Initialize the PKCS11 library
192-
pub fn initialize(&self, init_args: CInitializeArgs) -> Result<()> {
192+
pub fn initialize<T>(&self, init_args: CInitializeArgs<T>) -> Result<()> {
193193
let mut init_lock = self
194194
.initialized
195195
.as_ref()

0 commit comments

Comments
 (0)