Skip to content

Commit 8597e0e

Browse files
committed
Remove finalizing Pkcs11 on Drop
As explained in #208, this might not be wanted in contexts where the Pkcs11 structure is initialised/finalised outside of the Rust program.
1 parent ebf7765 commit 8597e0e

File tree

2 files changed

+12
-26
lines changed

2 files changed

+12
-26
lines changed

cryptoki/src/context/general_purpose.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use cryptoki_sys::{CK_C_INITIALIZE_ARGS, CK_INFO};
88
use paste::paste;
99
use std::convert::TryFrom;
1010
use std::fmt::Display;
11+
use std::ptr;
1112

1213
// See public docs on stub in parent mod.rs
1314
#[inline(always)]
@@ -23,6 +24,14 @@ pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()>
2324
}
2425
}
2526

27+
// See public docs on stub in parent mod.rs
28+
#[inline(always)]
29+
pub(super) fn finalize(ctx: Pkcs11) -> Result<()> {
30+
unsafe {
31+
Rv::from(get_pkcs11!(ctx, C_Finalize)(ptr::null_mut())).into_result(Function::Finalize)
32+
}
33+
}
34+
2635
// See public docs on stub in parent mod.rs
2736
#[inline(always)]
2837
pub(super) fn get_library_info(ctx: &Pkcs11) -> Result<Info> {

cryptoki/src/context/mod.rs

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@ pub use locking::*;
3030

3131
use crate::error::{Error, Result, Rv};
3232

33-
use log::error;
3433
use std::fmt;
3534
use std::path::Path;
3635
use std::ptr;
37-
use std::sync::RwLock;
3836

3937
/// Enum for various function lists
4038
/// Each following is super-set of the previous one with overlapping start so we store them
@@ -74,28 +72,6 @@ impl Pkcs11Impl {
7472
FunctionList::V3_2(l) => l,
7573
}
7674
}
77-
78-
// Private finalize call
79-
#[inline(always)]
80-
fn finalize(&self) -> Result<()> {
81-
unsafe {
82-
Rv::from(self
83-
.get_function_list()
84-
.C_Finalize
85-
.ok_or(Error::NullFunctionPointer)?(
86-
ptr::null_mut()
87-
))
88-
.into_result(Function::Finalize)
89-
}
90-
}
91-
}
92-
93-
impl Drop for Pkcs11Impl {
94-
fn drop(&mut self) {
95-
if let Err(err) = self.finalize() {
96-
error!("Failed to finalize: {err}");
97-
}
98-
}
9975
}
10076

10177
/// Main PKCS11 context. Should usually be unique per application.
@@ -202,8 +178,9 @@ impl Pkcs11 {
202178
}
203179

204180
/// Finalize the PKCS11 library. Indicates that the application no longer needs to use PKCS11.
205-
/// The library is also automatically finalized on drop.
206-
pub fn finalize(self) {}
181+
pub fn finalize(self) -> Result<()> {
182+
finalize(self)
183+
}
207184

208185
/// Returns the information about the library
209186
pub fn get_library_info(&self) -> Result<Info> {

0 commit comments

Comments
 (0)