Skip to content

Commit 40a4111

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

File tree

1 file changed

+144
-10
lines changed

1 file changed

+144
-10
lines changed

cryptoki/src/context/locking.rs

Lines changed: 144 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,166 @@
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::{CKF_LIBRARY_CANT_CREATE_OS_THREADS, CKF_OS_LOCKING_OK, CK_FLAGS, CK_RV};
66

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

9-
/// Argument for the initialize function
12+
/// Function pointer that creates a mutex
13+
pub type CreateMutexFn = unsafe extern "C" fn(*mut *mut ::std::os::raw::c_void) -> CK_RV;
14+
15+
/// Function pointer that destroys a mutex
16+
pub type DestroyMutexFn = unsafe extern "C" fn(*mut ::std::os::raw::c_void) -> CK_RV;
17+
18+
/// Function pointer that locks a mutex
19+
pub type LockMutexFn = unsafe extern "C" fn(*mut ::std::os::raw::c_void) -> CK_RV;
20+
21+
/// Function pointer that unlocks a mutex
22+
pub type UnlockMutexFn = unsafe extern "C" fn(*mut ::std::os::raw::c_void) -> CK_RV;
23+
24+
/// Provides function pointers for mutex-handling to ensure safe multi-threaded access.
1025
#[derive(Copy, Clone, Debug)]
11-
pub enum CInitializeArgs {
12-
/// The library can use the native OS library for locking
26+
pub struct CustomMutexHandling {
27+
create_mutex: CreateMutexFn,
28+
destroy_mutex: DestroyMutexFn,
29+
lock_mutex: LockMutexFn,
30+
unlock_mutex: UnlockMutexFn,
31+
}
32+
33+
impl CustomMutexHandling {
34+
/// Create a new `CustomMutexHandling` with the given function pointers
35+
/// to handle library's thread safety.
36+
///
37+
/// # Safety
38+
/// Considered unsafe due to user's ability to pass any function pointer.
39+
pub unsafe fn new(
40+
create_mutex: CreateMutexFn,
41+
destroy_mutex: DestroyMutexFn,
42+
lock_mutex: LockMutexFn,
43+
unlock_mutex: UnlockMutexFn,
44+
) -> Self {
45+
Self {
46+
create_mutex: create_mutex,
47+
destroy_mutex: destroy_mutex,
48+
lock_mutex: lock_mutex,
49+
unlock_mutex: unlock_mutex,
50+
}
51+
}
52+
}
53+
54+
/// Flags to set for the initialize function
55+
#[derive(Copy, Clone, Debug)]
56+
pub enum CInitializeFlags {
57+
/// The library won’t be accessed from multiple threads simultaneously
58+
None,
59+
/// The library may not create its own threads
60+
NoOsThreads,
61+
/// The library can use the native OS library for locking or the custom
1362
OsThreads,
14-
// TODO: add variants for custom mutexes here and no multithreading, safety implications for
15-
// that.
63+
/// The library needs to use the supplied function pointers
64+
/// for mutex-handling to ensure safe multi-threaded access.
65+
CustomMutexHandling(CustomMutexHandling),
66+
/// The library needs to use either the native operating system primitives
67+
/// or the supplied function pointers for mutex-handling to ensure safe
68+
/// multi-threaded access
69+
OsThreadsOrCustomMutexHandling(CustomMutexHandling),
70+
}
71+
72+
#[derive(Copy, Clone, Debug)]
73+
/// Argument for the initialize function
74+
pub struct CInitializeArgs {
75+
flags: CInitializeFlags,
76+
p_reserved: Option<NonNull<c_void>>,
77+
}
78+
79+
impl CInitializeArgs {
80+
/// Create a new `CInitializeArgs` with the given flags
81+
///
82+
/// # Examples
83+
/// ```
84+
/// use cryptoki::context::{CInitializeArgs, CInitializeFlags};
85+
///
86+
/// let args = CInitializeArgs::new(CInitializeFlags::OsThreads);
87+
/// ```
88+
pub fn new(flags: CInitializeFlags) -> Self {
89+
Self {
90+
flags,
91+
p_reserved: None,
92+
}
93+
}
94+
95+
/// Create a new `CInitializeArgs` with the given flags and reserved pointer.
96+
///
97+
/// # Safety
98+
/// Considered unsafe due to the user's ability to pass any pointer.
99+
///
100+
/// The user is responsible for managing the memory behind the pointer.
101+
pub unsafe fn new_with_reserved(flags: CInitializeFlags, p_reserved: NonNull<c_void>) -> Self {
102+
Self {
103+
flags,
104+
p_reserved: Some(p_reserved),
105+
}
106+
}
16107
}
17108

18109
impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS {
19110
fn from(c_initialize_args: CInitializeArgs) -> Self {
20111
let mut flags = CK_FLAGS::default();
21-
match c_initialize_args {
22-
CInitializeArgs::OsThreads => {
112+
let p_reserved = c_initialize_args
113+
.p_reserved
114+
.map(|non_null| non_null.as_ptr())
115+
.unwrap_or_else(ptr::null_mut);
116+
117+
match c_initialize_args.flags {
118+
CInitializeFlags::None => Self {
119+
CreateMutex: None,
120+
DestroyMutex: None,
121+
LockMutex: None,
122+
UnlockMutex: None,
123+
flags,
124+
pReserved: p_reserved,
125+
},
126+
CInitializeFlags::NoOsThreads => {
127+
flags |= CKF_LIBRARY_CANT_CREATE_OS_THREADS;
128+
Self {
129+
flags,
130+
CreateMutex: None,
131+
DestroyMutex: None,
132+
LockMutex: None,
133+
UnlockMutex: None,
134+
pReserved: p_reserved,
135+
}
136+
}
137+
CInitializeFlags::OsThreads => {
23138
flags |= CKF_OS_LOCKING_OK;
24139
Self {
25140
flags,
26141
CreateMutex: None,
27142
DestroyMutex: None,
28143
LockMutex: None,
29144
UnlockMutex: None,
30-
pReserved: ptr::null_mut(),
145+
pReserved: p_reserved,
146+
}
147+
}
148+
CInitializeFlags::CustomMutexHandling(custom_mutex_handling) => Self {
149+
flags,
150+
CreateMutex: Some(custom_mutex_handling.create_mutex),
151+
DestroyMutex: Some(custom_mutex_handling.destroy_mutex),
152+
LockMutex: Some(custom_mutex_handling.lock_mutex),
153+
UnlockMutex: Some(custom_mutex_handling.unlock_mutex),
154+
pReserved: p_reserved,
155+
},
156+
CInitializeFlags::OsThreadsOrCustomMutexHandling(custom_mutex_handling) => {
157+
flags |= CKF_OS_LOCKING_OK;
158+
Self {
159+
flags,
160+
CreateMutex: Some(custom_mutex_handling.create_mutex),
161+
DestroyMutex: Some(custom_mutex_handling.destroy_mutex),
162+
LockMutex: Some(custom_mutex_handling.lock_mutex),
163+
UnlockMutex: Some(custom_mutex_handling.unlock_mutex),
164+
pReserved: p_reserved,
31165
}
32166
}
33167
}

0 commit comments

Comments
 (0)