Skip to content

Commit 81aac81

Browse files
authored
Merge pull request #337 from nwalfield/revert-session
Don't tie Session's lifetime to the Pkcs11 object.
2 parents 36fb3c3 + 16ae62f commit 81aac81

17 files changed

+40
-41
lines changed

cryptoki/src/context/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ use crate::error::{Error, Result, Rv};
3333
use std::fmt;
3434
use std::path::Path;
3535
use std::ptr;
36+
use std::sync::Arc;
3637

3738
/// Enum for various function lists
3839
/// Each following is super-set of the previous one with overlapping start so we store them
@@ -75,9 +76,9 @@ impl Pkcs11Impl {
7576
}
7677

7778
/// Main PKCS11 context. Should usually be unique per application.
78-
#[derive(Debug)]
79+
#[derive(Clone, Debug)]
7980
pub struct Pkcs11 {
80-
pub(crate) impl_: Pkcs11Impl,
81+
pub(crate) impl_: Arc<Pkcs11Impl>,
8182
}
8283

8384
impl Pkcs11 {
@@ -128,19 +129,19 @@ impl Pkcs11 {
128129
let list32_ptr: *mut cryptoki_sys::CK_FUNCTION_LIST_3_2 =
129130
ifce.pFunctionList as *mut cryptoki_sys::CK_FUNCTION_LIST_3_2;
130131
return Ok(Pkcs11 {
131-
impl_: Pkcs11Impl {
132+
impl_: Arc::new(Pkcs11Impl {
132133
_pkcs11_lib: pkcs11_lib,
133134
function_list: FunctionList::V3_2(*list32_ptr),
134-
},
135+
}),
135136
});
136137
}
137138
let list30_ptr: *mut cryptoki_sys::CK_FUNCTION_LIST_3_0 =
138139
ifce.pFunctionList as *mut cryptoki_sys::CK_FUNCTION_LIST_3_0;
139140
return Ok(Pkcs11 {
140-
impl_: Pkcs11Impl {
141+
impl_: Arc::new(Pkcs11Impl {
141142
_pkcs11_lib: pkcs11_lib,
142143
function_list: FunctionList::V3_0(v30tov32(*list30_ptr)),
143-
},
144+
}),
144145
});
145146
}
146147
/* fall back to the 2.* API */
@@ -152,10 +153,10 @@ impl Pkcs11 {
152153
.into_result(Function::GetFunctionList)?;
153154

154155
Ok(Pkcs11 {
155-
impl_: Pkcs11Impl {
156+
impl_: Arc::new(Pkcs11Impl {
156157
_pkcs11_lib: pkcs11_lib,
157158
function_list: FunctionList::V2(v2tov3(*list_ptr)),
158-
},
159+
}),
159160
})
160161
}
161162

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))
36+
Ok(Session::new(session_handle, self.clone()))
3737
}
3838

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

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

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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,33 @@ pub use validation::ValidationFlagsType;
3535
/// threads. A Session needs to be created in its own thread or to be passed by ownership to
3636
/// another thread.
3737
#[derive(Debug)]
38-
pub struct Session<'a> {
38+
pub struct Session {
3939
handle: CK_SESSION_HANDLE,
40-
client: &'a Pkcs11,
40+
client: Pkcs11,
4141
// This is not used but to prevent Session to automatically implement Send and Sync
4242
_guard: PhantomData<*mut u32>,
4343
}
4444

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

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

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

63-
impl<'a> Session<'a> {
64-
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: &'a Pkcs11) -> Self {
63+
impl Session {
64+
pub(crate) fn new(handle: CK_SESSION_HANDLE, client: Pkcs11) -> Self {
6565
Session {
6666
handle,
6767
client,
@@ -70,7 +70,7 @@ impl<'a> Session<'a> {
7070
}
7171
}
7272

73-
impl<'a> Session<'a> {
73+
impl Session {
7474
/// Close a session
7575
/// This will be called on drop as well.
7676
pub fn close(self) -> Result<()> {
@@ -83,7 +83,7 @@ impl<'a> Session<'a> {
8383
}
8484

8585
pub(crate) fn client(&self) -> &Pkcs11 {
86-
self.client
86+
&self.client
8787
}
8888
}
8989

0 commit comments

Comments
 (0)