Skip to content

Commit 596a2e3

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

17 files changed

+44
-55
lines changed

cryptoki/src/context/mod.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use log::error;
3434
use std::fmt;
3535
use std::path::Path;
3636
use std::ptr;
37-
use std::sync::Arc;
3837
use std::sync::RwLock;
3938

4039
/// Enum for various function lists
@@ -100,10 +99,10 @@ impl Drop for Pkcs11Impl {
10099
}
101100

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

109108
impl Pkcs11 {
@@ -154,21 +153,21 @@ impl Pkcs11 {
154153
let list32_ptr: *mut cryptoki_sys::CK_FUNCTION_LIST_3_2 =
155154
ifce.pFunctionList as *mut cryptoki_sys::CK_FUNCTION_LIST_3_2;
156155
return Ok(Pkcs11 {
157-
impl_: Arc::new(Pkcs11Impl {
156+
impl_: Pkcs11Impl {
158157
_pkcs11_lib: pkcs11_lib,
159158
function_list: FunctionList::V3_2(*list32_ptr),
160-
}),
161-
initialized: Arc::new(RwLock::new(false)),
159+
},
160+
initialized: RwLock::new(false),
162161
});
163162
}
164163
let list30_ptr: *mut cryptoki_sys::CK_FUNCTION_LIST_3_0 =
165164
ifce.pFunctionList as *mut cryptoki_sys::CK_FUNCTION_LIST_3_0;
166165
return Ok(Pkcs11 {
167-
impl_: Arc::new(Pkcs11Impl {
166+
impl_: Pkcs11Impl {
168167
_pkcs11_lib: pkcs11_lib,
169168
function_list: FunctionList::V3_0(v30tov32(*list30_ptr)),
170-
}),
171-
initialized: Arc::new(RwLock::new(false)),
169+
},
170+
initialized: RwLock::new(false),
172171
});
173172
}
174173
/* fall back to the 2.* API */
@@ -180,21 +179,17 @@ impl Pkcs11 {
180179
.into_result(Function::GetFunctionList)?;
181180

182181
Ok(Pkcs11 {
183-
impl_: Arc::new(Pkcs11Impl {
182+
impl_: Pkcs11Impl {
184183
_pkcs11_lib: pkcs11_lib,
185184
function_list: FunctionList::V2(v2tov3(*list_ptr)),
186-
}),
187-
initialized: Arc::new(RwLock::new(false)),
185+
},
186+
initialized: RwLock::new(false),
188187
})
189188
}
190189

191190
/// Initialize the PKCS11 library
192191
pub fn initialize(&self, init_args: CInitializeArgs) -> Result<()> {
193-
let mut init_lock = self
194-
.initialized
195-
.as_ref()
196-
.write()
197-
.expect("lock not to be poisoned");
192+
let mut init_lock = self.initialized.write().expect("lock not to be poisoned");
198193
if *init_lock {
199194
Err(Error::AlreadyInitialized)?
200195
}
@@ -203,11 +198,7 @@ impl Pkcs11 {
203198

204199
/// Check whether the PKCS11 library has been initialized
205200
pub fn is_initialized(&self) -> bool {
206-
*self
207-
.initialized
208-
.as_ref()
209-
.read()
210-
.expect("lock not to be poisoned")
201+
*self.initialized.read().expect("lock not to be poisoned")
211202
}
212203

213204
/// 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/encapsulation.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
/// Encapsulate key
1515
pub fn encapsulate_key(
1616
&self,

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
@@ -34,37 +34,33 @@ pub use validation::ValidationFlagsType;
3434
/// threads. A Session needs to be created in its own thread or to be passed by ownership to
3535
/// another thread.
3636
#[derive(Debug)]
37-
pub struct Session {
37+
pub struct Session<'a> {
3838
handle: CK_SESSION_HANDLE,
39-
client: Pkcs11,
39+
client: &'a Pkcs11,
4040
// This is not used but to prevent Session to automatically implement Send and Sync
4141
_guard: PhantomData<*mut u32>,
4242
}
4343

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

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

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

62-
// Session does not implement Sync to prevent the same Session instance to be used from multiple
63-
// threads.
64-
unsafe impl Send for Session {}
65-
66-
impl Session {
67-
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: Pkcs11) -> Self {
62+
impl<'a> Session<'a> {
63+
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: &'a Pkcs11) -> Self {
6864
Session {
6965
handle,
7066
client,
@@ -73,7 +69,7 @@ impl Session {
7369
}
7470
}
7571

76-
impl Session {
72+
impl<'a> Session<'a> {
7773
/// Close a session
7874
/// This will be called on drop as well.
7975
pub fn close(self) {}

0 commit comments

Comments
 (0)