|
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::{os::raw::c_void, ptr}; |
8 | 11 |
|
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 |
10 | 22 | #[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 |
13 | 29 | 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 | + } |
16 | 62 | } |
17 | 63 |
|
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 { |
20 | 66 | 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 => { |
23 | 93 | flags |= CKF_OS_LOCKING_OK; |
24 | 94 | Self { |
25 | 95 | flags, |
26 | 96 | CreateMutex: None, |
27 | 97 | DestroyMutex: None, |
28 | 98 | LockMutex: None, |
29 | 99 | 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, |
31 | 120 | } |
32 | 121 | } |
33 | 122 | } |
|
0 commit comments