Skip to content

Commit e8614b8

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

File tree

1 file changed

+129
-10
lines changed

1 file changed

+129
-10
lines changed

cryptoki/src/context/locking.rs

Lines changed: 129 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,151 @@
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.
1013
#[derive(Copy, Clone, Debug)]
11-
pub enum CInitializeArgs {
12-
/// The library can use the native OS library for locking
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+
impl CustomMutexHandling {
22+
/// Create a new `CustomMutexHandling` with the given function pointers
23+
/// to handle library's thread safety.
24+
///
25+
/// # Safety
26+
/// Considered unsafe due to user's ability to pass any function pointer.
27+
pub unsafe fn new(
28+
create_mutex: CK_CREATEMUTEX,
29+
destroy_mutex: CK_DESTROYMUTEX,
30+
lock_mutex: CK_LOCKMUTEX,
31+
unlock_mutex: CK_UNLOCKMUTEX,
32+
) -> Self {
33+
Self {
34+
create_mutex,
35+
destroy_mutex,
36+
lock_mutex,
37+
unlock_mutex,
38+
}
39+
}
40+
}
41+
42+
/// Flags to set for the initialize function
43+
#[derive(Copy, Clone, Debug)]
44+
pub enum CInitializeFlags {
45+
/// The library won’t be accessed from multiple threads simultaneously
46+
None,
47+
/// The library may not create its own threads
48+
NoOsThreads,
49+
/// The library can use the native OS library for locking or the custom
1350
OsThreads,
14-
// TODO: add variants for custom mutexes here and no multithreading, safety implications for
15-
// that.
51+
/// The library needs to use the supplied function pointers
52+
/// for mutex-handling to ensure safe multi-threaded access.
53+
CustomMutexHandling(CustomMutexHandling),
54+
/// The library needs to use either the native operating system primitives
55+
/// or the supplied function pointers for mutex-handling to ensure safe
56+
/// multi-threaded access
57+
OsThreadsOrCustomMutexHandling(CustomMutexHandling),
58+
}
59+
60+
#[derive(Copy, Clone, Debug)]
61+
/// Argument for the initialize function
62+
pub struct CInitializeArgs {
63+
flags: CInitializeFlags,
64+
p_reserved: Option<*mut c_void>,
65+
}
66+
67+
impl CInitializeArgs {
68+
/// Create a new `CInitializeArgs` with the given flags
69+
///
70+
/// # Examples
71+
/// ```
72+
/// use cryptoki::context::{CInitializeArgs, CInitializeFlags};
73+
///
74+
/// let args = CInitializeArgs::new(CInitializeFlags::OsThreads);
75+
/// ```
76+
pub fn new(flags: CInitializeFlags) -> Self {
77+
Self {
78+
flags,
79+
p_reserved: None,
80+
}
81+
}
82+
83+
/// Create a new `CInitializeArgs` with the given flags and reserved pointer.
84+
///
85+
/// # Safety
86+
/// Considered unsafe due to the user's ability to pass any pointer.
87+
///
88+
/// The user is responsible for managing the memory behind the pointer.
89+
pub unsafe fn new_with_reserved(flags: CInitializeFlags, p_reserved: *mut c_void) -> Self {
90+
Self {
91+
flags,
92+
p_reserved: Some(p_reserved),
93+
}
94+
}
1695
}
1796

1897
impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS {
1998
fn from(c_initialize_args: CInitializeArgs) -> Self {
2099
let mut flags = CK_FLAGS::default();
21-
match c_initialize_args {
22-
CInitializeArgs::OsThreads => {
100+
let p_reserved = c_initialize_args.p_reserved.unwrap_or_else(ptr::null_mut);
101+
102+
match c_initialize_args.flags {
103+
CInitializeFlags::None => Self {
104+
CreateMutex: None,
105+
DestroyMutex: None,
106+
LockMutex: None,
107+
UnlockMutex: None,
108+
flags,
109+
pReserved: p_reserved,
110+
},
111+
CInitializeFlags::NoOsThreads => {
112+
flags |= CKF_LIBRARY_CANT_CREATE_OS_THREADS;
113+
Self {
114+
flags,
115+
CreateMutex: None,
116+
DestroyMutex: None,
117+
LockMutex: None,
118+
UnlockMutex: None,
119+
pReserved: p_reserved,
120+
}
121+
}
122+
CInitializeFlags::OsThreads => {
23123
flags |= CKF_OS_LOCKING_OK;
24124
Self {
25125
flags,
26126
CreateMutex: None,
27127
DestroyMutex: None,
28128
LockMutex: None,
29129
UnlockMutex: None,
30-
pReserved: ptr::null_mut(),
130+
pReserved: p_reserved,
131+
}
132+
}
133+
CInitializeFlags::CustomMutexHandling(custom_mutex_handling) => Self {
134+
flags,
135+
CreateMutex: custom_mutex_handling.create_mutex,
136+
DestroyMutex: custom_mutex_handling.destroy_mutex,
137+
LockMutex: custom_mutex_handling.lock_mutex,
138+
UnlockMutex: custom_mutex_handling.unlock_mutex,
139+
pReserved: p_reserved,
140+
},
141+
CInitializeFlags::OsThreadsOrCustomMutexHandling(custom_mutex_handling) => {
142+
flags |= CKF_OS_LOCKING_OK;
143+
Self {
144+
flags,
145+
CreateMutex: custom_mutex_handling.create_mutex,
146+
DestroyMutex: custom_mutex_handling.destroy_mutex,
147+
LockMutex: custom_mutex_handling.lock_mutex,
148+
UnlockMutex: custom_mutex_handling.unlock_mutex,
149+
pReserved: p_reserved,
31150
}
32151
}
33152
}

0 commit comments

Comments
 (0)