|
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 bitflags::bitflags; |
| 6 | +use cryptoki_sys::{self, CK_C_INITIALIZE_ARGS, CK_FLAGS}; |
6 | 7 |
|
7 | | -use std::ptr; |
| 8 | +use std::{ |
| 9 | + os::raw::c_void, |
| 10 | + ptr::{self, NonNull}, |
| 11 | +}; |
| 12 | + |
| 13 | +bitflags! { |
| 14 | + /// Flags to set for the initialize function |
| 15 | + #[derive(Debug, Clone, Copy)] |
| 16 | + pub struct CInitializeFlags: CK_FLAGS { |
| 17 | + /// The library can use the native OS library for locking or the custom |
| 18 | + const OS_LOCKING_OK = cryptoki_sys::CKF_OS_LOCKING_OK; |
| 19 | + /// The library may not create its own threads |
| 20 | + const LIBRARY_CANT_CREATE_OS_THREADS = cryptoki_sys::CKF_LIBRARY_CANT_CREATE_OS_THREADS; |
| 21 | + } |
| 22 | +} |
8 | 23 |
|
9 | 24 | /// Argument for the initialize function |
10 | | -#[derive(Copy, Clone, Debug)] |
11 | | -pub enum CInitializeArgs { |
12 | | - /// The library can use the native OS library for locking |
13 | | - OsThreads, |
14 | | - // TODO: add variants for custom mutexes here and no multithreading, safety implications for |
15 | | - // that. |
| 25 | +#[derive(Debug, Clone, Copy)] |
| 26 | +pub struct CInitializeArgs { |
| 27 | + flags: CInitializeFlags, |
| 28 | + p_reserved: Option<NonNull<c_void>>, |
16 | 29 | } |
17 | 30 |
|
18 | | -impl From<CInitializeArgs> for cryptoki_sys::CK_C_INITIALIZE_ARGS { |
| 31 | +impl CInitializeArgs { |
| 32 | + /// Create a new `CInitializeArgs` with the given flags |
| 33 | + /// |
| 34 | + /// # Examples |
| 35 | + /// ``` |
| 36 | + /// use cryptoki::context::{CInitializeArgs, CInitializeFlags}; |
| 37 | + /// |
| 38 | + /// let args = CInitializeArgs::new(CInitializeFlags::OS_LOCKING_OK | CInitializeFlags::LIBRARY_CANT_CREATE_OS_THREADS); |
| 39 | + /// ``` |
| 40 | + pub fn new(flags: CInitializeFlags) -> Self { |
| 41 | + Self { |
| 42 | + flags, |
| 43 | + p_reserved: None, |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + /// Create a new `CInitializeArgs` with the given flags and reserved pointer. |
| 48 | + /// |
| 49 | + /// # Safety |
| 50 | + /// Considered unsafe due to the user's ability to pass any pointer: the |
| 51 | + /// caller must ensure that the provided pointer is valid and points |
| 52 | + /// to a struct that has the same ABI as the one required by cryptoki's |
| 53 | + /// `C_Initialize` |
| 54 | + /// |
| 55 | + /// The user is responsible for managing the memory behind the pointer. |
| 56 | + /// |
| 57 | + /// # Examples |
| 58 | + ///``` |
| 59 | + /// use cryptoki::context::{CInitializeArgs, CInitializeFlags}; |
| 60 | + /// use std::{ptr::NonNull, os::raw::c_void}; |
| 61 | + /// |
| 62 | + /// let flags = CInitializeFlags::OS_LOCKING_OK; |
| 63 | + /// |
| 64 | + /// // Create a box with the reserved data |
| 65 | + /// let boxed_data = Box::new(42); |
| 66 | + /// |
| 67 | + /// // Obtain the raw pointer |
| 68 | + /// let ptr_reserved = NonNull::new(Box::into_raw(boxed_data) as *mut c_void) |
| 69 | + /// .expect("Failed to create NonNull pointer"); |
| 70 | + /// |
| 71 | + /// // SAFETY: since the data was allocated when boxed_data was created, this is safe |
| 72 | + /// let args = unsafe { |
| 73 | + /// CInitializeArgs::new_with_reserved(flags, ptr_reserved) |
| 74 | + /// }; |
| 75 | + /// |
| 76 | + /// // Reassemble back the bax to make sure the data is correctly cleaned |
| 77 | + /// // SAFETY: since ptr_reserved was built with valid data, this is safe |
| 78 | + /// let reserved_data = unsafe { Box::from_raw(ptr_reserved.as_ptr()) }; |
| 79 | + /// ``` |
| 80 | + pub const unsafe fn new_with_reserved( |
| 81 | + flags: CInitializeFlags, |
| 82 | + p_reserved: NonNull<c_void>, |
| 83 | + ) -> Self { |
| 84 | + Self { |
| 85 | + flags, |
| 86 | + p_reserved: Some(p_reserved), |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +impl From<CInitializeArgs> for CK_C_INITIALIZE_ARGS { |
19 | 92 | fn from(c_initialize_args: CInitializeArgs) -> Self { |
20 | | - let mut flags = CK_FLAGS::default(); |
21 | | - match c_initialize_args { |
22 | | - CInitializeArgs::OsThreads => { |
23 | | - flags |= CKF_OS_LOCKING_OK; |
24 | | - Self { |
25 | | - flags, |
26 | | - CreateMutex: None, |
27 | | - DestroyMutex: None, |
28 | | - LockMutex: None, |
29 | | - UnlockMutex: None, |
30 | | - pReserved: ptr::null_mut(), |
31 | | - } |
32 | | - } |
| 93 | + let flags = c_initialize_args.flags.bits(); |
| 94 | + let p_reserved = c_initialize_args |
| 95 | + .p_reserved |
| 96 | + .map(|non_null| non_null.as_ptr()) |
| 97 | + .unwrap_or_else(ptr::null_mut); |
| 98 | + |
| 99 | + Self { |
| 100 | + CreateMutex: None, |
| 101 | + DestroyMutex: None, |
| 102 | + LockMutex: None, |
| 103 | + UnlockMutex: None, |
| 104 | + flags, |
| 105 | + pReserved: p_reserved, |
33 | 106 | } |
34 | 107 | } |
35 | 108 | } |
0 commit comments