|
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 | //! Locking related type |
4 | 4 |
|
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 | +}; |
6 | 9 |
|
7 | | -use std::ptr; |
| 10 | +use std::{ |
| 11 | + os::raw::c_void, |
| 12 | + ptr::{self, NonNull}, |
| 13 | +}; |
8 | 14 |
|
9 | | -/// Argument for the initialize function |
| 15 | +/// Provides function pointers for mutex-handling to ensure safe multi-threaded access. |
10 | 16 | #[derive(Copy, Clone, Debug)] |
11 | | -pub enum CInitializeArgs { |
12 | | - /// The library can use the native OS library for locking |
| 17 | +pub struct CustomMutexHandling { |
| 18 | + create_mutex: CK_CREATEMUTEX, |
| 19 | + destroy_mutex: CK_DESTROYMUTEX, |
| 20 | + lock_mutex: CK_LOCKMUTEX, |
| 21 | + unlock_mutex: CK_UNLOCKMUTEX, |
| 22 | +} |
| 23 | + |
| 24 | +impl CustomMutexHandling { |
| 25 | + /// Create a new `CustomMutexHandling` with the given function pointers |
| 26 | + /// to handle library's thread safety. |
| 27 | + /// |
| 28 | + /// # Safety |
| 29 | + /// Considered unsafe due to user's ability to pass any function pointer. |
| 30 | + pub unsafe fn new( |
| 31 | + create_mutex: CK_CREATEMUTEX, |
| 32 | + destroy_mutex: CK_DESTROYMUTEX, |
| 33 | + lock_mutex: CK_LOCKMUTEX, |
| 34 | + unlock_mutex: CK_UNLOCKMUTEX, |
| 35 | + ) -> Self { |
| 36 | + Self { |
| 37 | + create_mutex, |
| 38 | + destroy_mutex, |
| 39 | + lock_mutex, |
| 40 | + unlock_mutex, |
| 41 | + } |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/// Flags to set for the initialize function |
| 46 | +#[derive(Copy, Clone, Debug)] |
| 47 | +pub enum CInitializeFlags { |
| 48 | + /// The library won’t be accessed from multiple threads simultaneously |
| 49 | + None, |
| 50 | + /// The library may not create its own threads |
| 51 | + NoOsThreads, |
| 52 | + /// The library can use the native OS library for locking or the custom |
13 | 53 | OsThreads, |
14 | | - // TODO: add variants for custom mutexes here and no multithreading, safety implications for |
15 | | - // that. |
| 54 | + /// The library needs to use the supplied function pointers |
| 55 | + /// for mutex-handling to ensure safe multi-threaded access. |
| 56 | + CustomMutexHandling(CustomMutexHandling), |
| 57 | + /// The library needs to use either the native operating system primitives |
| 58 | + /// or the supplied function pointers for mutex-handling to ensure safe |
| 59 | + /// multi-threaded access |
| 60 | + OsThreadsOrCustomMutexHandling(CustomMutexHandling), |
| 61 | +} |
| 62 | + |
| 63 | +#[derive(Copy, Clone, Debug)] |
| 64 | +/// Argument for the initialize function |
| 65 | +pub struct CInitializeArgs { |
| 66 | + flags: CInitializeFlags, |
| 67 | + p_reserved: Option<NonNull<c_void>>, |
| 68 | +} |
| 69 | + |
| 70 | +impl CInitializeArgs { |
| 71 | + /// Create a new `CInitializeArgs` with the given flags |
| 72 | + /// |
| 73 | + /// # Examples |
| 74 | + /// ``` |
| 75 | + /// use cryptoki::context::{CInitializeArgs, CInitializeFlags}; |
| 76 | + /// |
| 77 | + /// let args = CInitializeArgs::new(CInitializeFlags::OsThreads); |
| 78 | + /// ``` |
| 79 | + pub fn new(flags: CInitializeFlags) -> Self { |
| 80 | + Self { |
| 81 | + flags, |
| 82 | + p_reserved: None, |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + /// Create a new `CInitializeArgs` with the given flags and reserved pointer. |
| 87 | + /// |
| 88 | + /// # Safety |
| 89 | + /// Considered unsafe due to the user's ability to pass any pointer. |
| 90 | + /// |
| 91 | + /// The user is responsible for managing the memory behind the pointer. |
| 92 | + pub unsafe fn new_with_reserved(flags: CInitializeFlags, p_reserved: *mut c_void) -> Self { |
| 93 | + Self { |
| 94 | + flags, |
| 95 | + p_reserved: NonNull::new(p_reserved), |
| 96 | + } |
| 97 | + } |
16 | 98 | } |
17 | 99 |
|
18 | 100 | impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS { |
19 | 101 | fn from(c_initialize_args: CInitializeArgs) -> Self { |
20 | 102 | let mut flags = CK_FLAGS::default(); |
21 | | - match c_initialize_args { |
22 | | - CInitializeArgs::OsThreads => { |
| 103 | + let p_reserved = c_initialize_args |
| 104 | + .p_reserved |
| 105 | + .map(|non_null| non_null.as_ptr()) |
| 106 | + .unwrap_or_else(ptr::null_mut); |
| 107 | + |
| 108 | + match c_initialize_args.flags { |
| 109 | + CInitializeFlags::None => Self { |
| 110 | + CreateMutex: None, |
| 111 | + DestroyMutex: None, |
| 112 | + LockMutex: None, |
| 113 | + UnlockMutex: None, |
| 114 | + flags, |
| 115 | + pReserved: p_reserved, |
| 116 | + }, |
| 117 | + CInitializeFlags::NoOsThreads => { |
| 118 | + flags |= CKF_LIBRARY_CANT_CREATE_OS_THREADS; |
| 119 | + Self { |
| 120 | + flags, |
| 121 | + CreateMutex: None, |
| 122 | + DestroyMutex: None, |
| 123 | + LockMutex: None, |
| 124 | + UnlockMutex: None, |
| 125 | + pReserved: p_reserved, |
| 126 | + } |
| 127 | + } |
| 128 | + CInitializeFlags::OsThreads => { |
23 | 129 | flags |= CKF_OS_LOCKING_OK; |
24 | 130 | Self { |
25 | 131 | flags, |
26 | 132 | CreateMutex: None, |
27 | 133 | DestroyMutex: None, |
28 | 134 | LockMutex: None, |
29 | 135 | UnlockMutex: None, |
30 | | - pReserved: ptr::null_mut(), |
| 136 | + pReserved: p_reserved, |
| 137 | + } |
| 138 | + } |
| 139 | + CInitializeFlags::CustomMutexHandling(custom_mutex_handling) => Self { |
| 140 | + flags, |
| 141 | + CreateMutex: custom_mutex_handling.create_mutex, |
| 142 | + DestroyMutex: custom_mutex_handling.destroy_mutex, |
| 143 | + LockMutex: custom_mutex_handling.lock_mutex, |
| 144 | + UnlockMutex: custom_mutex_handling.unlock_mutex, |
| 145 | + pReserved: p_reserved, |
| 146 | + }, |
| 147 | + CInitializeFlags::OsThreadsOrCustomMutexHandling(custom_mutex_handling) => { |
| 148 | + flags |= CKF_OS_LOCKING_OK; |
| 149 | + Self { |
| 150 | + flags, |
| 151 | + CreateMutex: custom_mutex_handling.create_mutex, |
| 152 | + DestroyMutex: custom_mutex_handling.destroy_mutex, |
| 153 | + LockMutex: custom_mutex_handling.lock_mutex, |
| 154 | + UnlockMutex: custom_mutex_handling.unlock_mutex, |
| 155 | + pReserved: p_reserved, |
31 | 156 | } |
32 | 157 | } |
33 | 158 | } |
|
0 commit comments