Skip to content

Commit 9f19f12

Browse files
committed
Make Session take a reference to Pkcs11 and remove inner Arc
Signed-off-by: Wiktor Kwapisiewicz <wiktor@metacode.biz>
1 parent 52daf5f commit 9f19f12

15 files changed

+42
-53
lines changed

cryptoki/src/context/mod.rs

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use std::fmt;
3535
use std::mem;
3636
use std::path::Path;
3737
use std::ptr;
38-
use std::sync::Arc;
3938
use std::sync::RwLock;
4039

4140
/// Enum for various function lists
@@ -101,10 +100,10 @@ impl Drop for Pkcs11Impl {
101100
}
102101

103102
/// Main PKCS11 context. Should usually be unique per application.
104-
#[derive(Clone, Debug)]
103+
#[derive(Debug)]
105104
pub struct Pkcs11 {
106-
pub(crate) impl_: Arc<Pkcs11Impl>,
107-
initialized: Arc<RwLock<bool>>,
105+
pub(crate) impl_: Pkcs11Impl,
106+
initialized: RwLock<bool>,
108107
}
109108

110109
impl Pkcs11 {
@@ -155,11 +154,11 @@ impl Pkcs11 {
155154
let list30_ptr: *mut cryptoki_sys::CK_FUNCTION_LIST_3_0 =
156155
ifce.pFunctionList as *mut cryptoki_sys::CK_FUNCTION_LIST_3_0;
157156
return Ok(Pkcs11 {
158-
impl_: Arc::new(Pkcs11Impl {
157+
impl_: Pkcs11Impl {
159158
_pkcs11_lib: pkcs11_lib,
160159
function_list: FunctionList::V3_0(*list30_ptr),
161-
}),
162-
initialized: Arc::new(RwLock::new(false)),
160+
},
161+
initialized: RwLock::new(false),
163162
});
164163
}
165164
/* fall back to the 2.* API */
@@ -174,21 +173,17 @@ impl Pkcs11 {
174173
let list_ptr = *list.as_ptr();
175174

176175
Ok(Pkcs11 {
177-
impl_: Arc::new(Pkcs11Impl {
176+
impl_: Pkcs11Impl {
178177
_pkcs11_lib: pkcs11_lib,
179178
function_list: FunctionList::V2(v2tov3(*list_ptr)),
180-
}),
181-
initialized: Arc::new(RwLock::new(false)),
179+
},
180+
initialized: RwLock::new(false),
182181
})
183182
}
184183

185184
/// Initialize the PKCS11 library
186185
pub fn initialize(&self, init_args: CInitializeArgs) -> Result<()> {
187-
let mut init_lock = self
188-
.initialized
189-
.as_ref()
190-
.write()
191-
.expect("lock not to be poisoned");
186+
let mut init_lock = self.initialized.write().expect("lock not to be poisoned");
192187
if *init_lock {
193188
Err(Error::AlreadyInitialized)?
194189
}
@@ -197,11 +192,7 @@ impl Pkcs11 {
197192

198193
/// Check whether the PKCS11 library has been initialized
199194
pub fn is_initialized(&self) -> bool {
200-
*self
201-
.initialized
202-
.as_ref()
203-
.read()
204-
.expect("lock not to be poisoned")
195+
*self.initialized.read().expect("lock not to be poisoned")
205196
}
206197

207198
/// Finalize the PKCS11 library. Indicates that the application no longer needs to use PKCS11.

cryptoki/src/context/session_management.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::Function;
1313

1414
impl Pkcs11 {
1515
#[inline(always)]
16-
fn open_session(&self, slot_id: Slot, read_write: bool) -> Result<Session> {
16+
fn open_session(&self, slot_id: Slot, read_write: bool) -> Result<Session<'_>> {
1717
let mut session_handle = 0;
1818

1919
let flags = if read_write {
@@ -33,7 +33,7 @@ impl Pkcs11 {
3333
.into_result(Function::OpenSession)?;
3434
}
3535

36-
Ok(Session::new(session_handle, self.clone()))
36+
Ok(Session::new(session_handle, self))
3737
}
3838

3939
/// Open a new Read-Only session
@@ -60,14 +60,14 @@ impl Pkcs11 {
6060
/// let session = client.open_ro_session(slot)?;
6161
/// # let _ = session; Ok(()) }
6262
/// ```
63-
pub fn open_ro_session(&self, slot_id: Slot) -> Result<Session> {
63+
pub fn open_ro_session(&self, slot_id: Slot) -> Result<Session<'_>> {
6464
self.open_session(slot_id, false)
6565
}
6666

6767
/// Open a new Read/Write session
6868
///
6969
/// Note: No callback is set when opening the session.
70-
pub fn open_rw_session(&self, slot_id: Slot) -> Result<Session> {
70+
pub fn open_rw_session(&self, slot_id: Slot) -> Result<Session<'_>> {
7171
self.open_session(slot_id, true)
7272
}
7373
}

cryptoki/src/session/decryption.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session::Session;
1010
use cryptoki_sys::*;
1111
use std::convert::TryInto;
1212

13-
impl Session {
13+
impl Session<'_> {
1414
/// Single-part decryption operation
1515
pub fn decrypt(
1616
&self,

cryptoki/src/session/digesting.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session::Session;
1010
use cryptoki_sys::*;
1111
use std::convert::TryInto;
1212

13-
impl Session {
13+
impl Session<'_> {
1414
/// Single-part digesting operation
1515
pub fn digest(&self, m: &Mechanism, data: &[u8]) -> Result<Vec<u8>> {
1616
let mut mechanism: CK_MECHANISM = m.into();

cryptoki/src/session/encryption.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session::Session;
1010
use cryptoki_sys::*;
1111
use std::convert::TryInto;
1212

13-
impl Session {
13+
impl Session<'_> {
1414
/// Single-part encryption operation
1515
pub fn encrypt(
1616
&self,

cryptoki/src/session/key_management.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session::Session;
1010
use cryptoki_sys::{CK_ATTRIBUTE, CK_MECHANISM, CK_MECHANISM_PTR};
1111
use std::convert::TryInto;
1212

13-
impl Session {
13+
impl Session<'_> {
1414
/// Generate a secret key
1515
pub fn generate_key(
1616
&self,

cryptoki/src/session/message_decryption.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session::Session;
1010
use cryptoki_sys::*;
1111
use std::convert::TryInto;
1212

13-
impl Session {
13+
impl Session<'_> {
1414
/// Prepare a session for one or more Message-based decryption using the same mechanism and key
1515
pub fn message_decrypt_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
1616
let mut mechanism: CK_MECHANISM = mechanism.into();

cryptoki/src/session/message_encryption.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::session::Session;
1010
use cryptoki_sys::*;
1111
use std::convert::TryInto;
1212

13-
impl Session {
13+
impl Session<'_> {
1414
/// Prepare a session for one or more Message-based encryption using the same mechanism and key
1515
pub fn message_encrypt_init(&self, mechanism: &Mechanism, key: ObjectHandle) -> Result<()> {
1616
let mut mechanism: CK_MECHANISM = mechanism.into();

cryptoki/src/session/mod.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,37 +31,33 @@ pub use session_info::{SessionInfo, SessionState};
3131
/// threads. A Session needs to be created in its own thread or to be passed by ownership to
3232
/// another thread.
3333
#[derive(Debug)]
34-
pub struct Session {
34+
pub struct Session<'a> {
3535
handle: CK_SESSION_HANDLE,
36-
client: Pkcs11,
36+
client: &'a Pkcs11,
3737
// This is not used but to prevent Session to automatically implement Send and Sync
3838
_guard: PhantomData<*mut u32>,
3939
}
4040

41-
impl std::fmt::Display for Session {
41+
impl<'a> std::fmt::Display for Session<'a> {
4242
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4343
write!(f, "{}", self.handle)
4444
}
4545
}
4646

47-
impl std::fmt::LowerHex for Session {
47+
impl<'a> std::fmt::LowerHex for Session<'a> {
4848
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
4949
write!(f, "{:08x}", self.handle)
5050
}
5151
}
5252

53-
impl std::fmt::UpperHex for Session {
53+
impl<'a> std::fmt::UpperHex for Session<'a> {
5454
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
5555
write!(f, "{:08X}", self.handle)
5656
}
5757
}
5858

59-
// Session does not implement Sync to prevent the same Session instance to be used from multiple
60-
// threads.
61-
unsafe impl Send for Session {}
62-
63-
impl Session {
64-
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: Pkcs11) -> Self {
59+
impl<'a> Session<'a> {
60+
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: &'a Pkcs11) -> Self {
6561
Session {
6662
handle,
6763
client,
@@ -70,7 +66,7 @@ impl Session {
7066
}
7167
}
7268

73-
impl Session {
69+
impl<'a> Session<'a> {
7470
/// Close a session
7571
/// This will be called on drop as well.
7672
pub fn close(self) {}

cryptoki/src/session/object_management.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ const MAX_OBJECT_COUNT: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(10)
7878
/// ```
7979
#[derive(Debug)]
8080
pub struct ObjectHandleIterator<'a> {
81-
session: &'a Session,
81+
session: &'a Session<'a>,
8282
object_count: usize,
8383
index: usize,
8484
cache: Vec<CK_OBJECT_HANDLE>,
@@ -207,7 +207,7 @@ impl Drop for ObjectHandleIterator<'_> {
207207
}
208208
}
209209

210-
impl Session {
210+
impl Session<'_> {
211211
/// Iterate over session objects matching a template.
212212
///
213213
/// # Arguments
@@ -224,7 +224,7 @@ impl Session {
224224
/// * [`ObjectHandleIterator`] for more information on how to use the iterator
225225
/// * [`Session::iter_objects_with_cache_size`] for a way to specify the cache size
226226
#[inline(always)]
227-
pub fn iter_objects(&self, template: &[Attribute]) -> Result<ObjectHandleIterator> {
227+
pub fn iter_objects(&self, template: &[Attribute]) -> Result<ObjectHandleIterator<'_>> {
228228
self.iter_objects_with_cache_size(template, MAX_OBJECT_COUNT)
229229
}
230230

@@ -248,7 +248,7 @@ impl Session {
248248
&self,
249249
template: &[Attribute],
250250
cache_size: NonZeroUsize,
251-
) -> Result<ObjectHandleIterator> {
251+
) -> Result<ObjectHandleIterator<'_>> {
252252
let template: Vec<CK_ATTRIBUTE> = template.iter().map(Into::into).collect();
253253
ObjectHandleIterator::new(self, template, cache_size)
254254
}

0 commit comments

Comments
 (0)