Skip to content

Commit b999304

Browse files
ionut-armgowthamsk-arm
authored andcommitted
Add function name to errors and logs
Adding the name of the function that lead to a backend error to the log messages it generates and to the error returned to the client. Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent 25c5d2e commit b999304

16 files changed

+103
-74
lines changed

cryptoki/src/context/general_purpose.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::error::{Result, Rv};
77
use cryptoki_sys::{CK_C_INITIALIZE_ARGS, CK_INFO};
88
use paste::paste;
99
use std::convert::TryFrom;
10+
use std::fmt::Display;
1011

1112
// See public docs on stub in parent mod.rs
1213
#[inline(always)]
@@ -18,7 +19,7 @@ pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()>
1819
Rv::from(get_pkcs11!(ctx, C_Initialize)(
1920
init_args_ptr as *mut CK_C_INITIALIZE_ARGS as *mut ::std::ffi::c_void,
2021
))
21-
.into_result()
22+
.into_result(Function::Initialize)
2223
}
2324
}
2425

@@ -27,7 +28,7 @@ pub(super) fn initialize(ctx: &Pkcs11, init_args: CInitializeArgs) -> Result<()>
2728
pub(super) fn get_library_info(ctx: &Pkcs11) -> Result<Info> {
2829
let mut info = CK_INFO::default();
2930
unsafe {
30-
Rv::from(get_pkcs11!(ctx, C_GetInfo)(&mut info)).into_result()?;
31+
Rv::from(get_pkcs11!(ctx, C_GetInfo)(&mut info)).into_result(Function::GetInfo)?;
3132
Info::try_from(info)
3233
}
3334
}
@@ -117,6 +118,12 @@ pub enum Function {
117118
WaitForSlotEvent,
118119
}
119120

121+
impl Display for Function {
122+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
123+
write!(f, "Function::{:?}", self)
124+
}
125+
}
126+
120127
#[inline(always)]
121128
pub(super) fn is_fn_supported(ctx: &Pkcs11, function: Function) -> bool {
122129
match function {

cryptoki/src/context/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ impl Pkcs11Impl {
6060
.ok_or(Error::NullFunctionPointer)?(
6161
ptr::null_mut()
6262
))
63-
.into_result()
63+
.into_result(Function::Finalize)
6464
}
6565
}
6666
}
@@ -91,7 +91,8 @@ impl Pkcs11 {
9191
cryptoki_sys::Pkcs11::new(filename.as_ref()).map_err(Error::LibraryLoading)?;
9292
let mut list = mem::MaybeUninit::uninit();
9393

94-
Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr())).into_result()?;
94+
Rv::from(pkcs11_lib.C_GetFunctionList(list.as_mut_ptr()))
95+
.into_result(Function::GetFunctionList)?;
9596

9697
let list_ptr = *list.as_ptr();
9798

cryptoki/src/context/session_management.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ use crate::error::{Result, Rv};
99
use crate::session::Session;
1010
use crate::slot::Slot;
1111

12+
use super::Function;
13+
1214
impl Pkcs11 {
1315
#[inline(always)]
1416
fn open_session(&self, slot_id: Slot, read_write: bool) -> Result<Session> {
@@ -28,7 +30,7 @@ impl Pkcs11 {
2830
None,
2931
&mut session_handle,
3032
))
31-
.into_result()?;
33+
.into_result(Function::OpenSession)?;
3234
}
3335

3436
Ok(Session::new(session_handle, self.clone()))

cryptoki/src/context/slot_token_management.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ use std::convert::{TryFrom, TryInto};
2020

2121
use crate::error::RvError::BufferTooSmall;
2222

23+
use super::Function;
24+
2325
impl Pkcs11 {
2426
#[inline(always)]
2527
fn get_slots(&self, with_token: CK_BBOOL) -> Result<Vec<Slot>> {
2628
let mut slot_count = 0;
2729
let rval = unsafe {
2830
get_pkcs11!(self, C_GetSlotList)(with_token, std::ptr::null_mut(), &mut slot_count)
2931
};
30-
Rv::from(rval).into_result()?;
32+
Rv::from(rval).into_result(Function::GetSlotList)?;
3133

3234
let mut slots;
3335
loop {
@@ -41,7 +43,7 @@ impl Pkcs11 {
4143
// and we want to loop again with a resized buffer.
4244
if !matches!(Rv::from(rval), Rv::Error(BufferTooSmall)) {
4345
// Account for other possible error types
44-
Rv::from(rval).into_result()?;
46+
Rv::from(rval).into_result(Function::GetSlotList)?;
4547
// Otherwise, we have a valid list to process
4648
break;
4749
}
@@ -92,7 +94,7 @@ impl Pkcs11 {
9294
pin.expose_secret().len().try_into()?,
9395
label.as_ptr() as *mut u8,
9496
))
95-
.into_result()
97+
.into_result(Function::InitToken)
9698
}
9799
}
98100

@@ -104,7 +106,7 @@ impl Pkcs11 {
104106
slot.into(),
105107
&mut slot_info,
106108
))
107-
.into_result()?;
109+
.into_result(Function::GetSlotInfo)?;
108110
Ok(SlotInfo::from(slot_info))
109111
}
110112
}
@@ -117,7 +119,7 @@ impl Pkcs11 {
117119
slot.into(),
118120
&mut token_info,
119121
))
120-
.into_result()?;
122+
.into_result(Function::GetTokenInfo)?;
121123
TokenInfo::try_from(token_info)
122124
}
123125
}
@@ -132,7 +134,7 @@ impl Pkcs11 {
132134
std::ptr::null_mut(),
133135
&mut mechanism_count,
134136
))
135-
.into_result()?;
137+
.into_result(Function::GetMechanismList)?;
136138
}
137139

138140
let mut mechanisms = vec![0; mechanism_count.try_into()?];
@@ -143,7 +145,7 @@ impl Pkcs11 {
143145
mechanisms.as_mut_ptr(),
144146
&mut mechanism_count,
145147
))
146-
.into_result()?;
148+
.into_result(Function::GetMechanismList)?;
147149
}
148150

149151
// Truncate mechanisms if count decreased.
@@ -164,7 +166,7 @@ impl Pkcs11 {
164166
type_.into(),
165167
&mut mechanism_info,
166168
))
167-
.into_result()?;
169+
.into_result(Function::GetMechanismInfo)?;
168170
Ok(MechanismInfo::from(mechanism_info))
169171
}
170172
}
@@ -174,7 +176,7 @@ impl Pkcs11 {
174176
let mut slot: CK_SLOT_ID = 0;
175177
let wait_for_slot_event = get_pkcs11!(self, C_WaitForSlotEvent);
176178
let rv = wait_for_slot_event(flags, &mut slot, std::ptr::null_mut());
177-
Rv::from(rv).into_result()?;
179+
Rv::from(rv).into_result(Function::WaitForSlotEvent)?;
178180
Ok(Slot::new(slot))
179181
}
180182
}
@@ -187,7 +189,7 @@ impl Pkcs11 {
187189
/// Get the latest slot event (insertion or removal of a token)
188190
pub fn get_slot_event(&self) -> Result<Option<Slot>> {
189191
match self.wait_for_slot_event_impl(CKF_DONT_BLOCK) {
190-
Err(Error::Pkcs11(RvError::NoEvent)) => Ok(None),
192+
Err(Error::Pkcs11(RvError::NoEvent, Function::WaitForSlotEvent)) => Ok(None),
191193
Ok(slot) => Ok(Some(slot)),
192194
Err(x) => Err(x),
193195
}

cryptoki/src/error/mod.rs

+5-9
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ pub use rv_error::*;
1010

1111
use std::fmt;
1212

13+
use crate::context::Function;
14+
1315
#[derive(Debug)]
1416
/// Main error type
1517
pub enum Error {
@@ -18,7 +20,7 @@ pub enum Error {
1820
LibraryLoading(libloading::Error),
1921

2022
/// All PKCS#11 functions that return non-zero translate to this error.
21-
Pkcs11(RvError),
23+
Pkcs11(RvError, Function),
2224

2325
/// This error marks a feature that is not yet supported by the PKCS11 Rust abstraction layer.
2426
NotSupported,
@@ -55,7 +57,7 @@ impl fmt::Display for Error {
5557
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5658
match self {
5759
Error::LibraryLoading(e) => write!(f, "libloading error ({e})"),
58-
Error::Pkcs11(e) => write!(f, "PKCS11 error: {e}"),
60+
Error::Pkcs11(e, funct) => write!(f, "{funct}: PKCS11 error: {e}"),
5961
Error::NotSupported => write!(f, "Feature not supported"),
6062
Error::TryFromInt(e) => write!(f, "Conversion between integers failed ({e})"),
6163
Error::TryFromSlice(e) => write!(f, "Error converting slice to array ({e})"),
@@ -79,7 +81,7 @@ impl std::error::Error for Error {
7981
Error::ParseInt(e) => Some(e),
8082
Error::Utf8(e) => Some(e),
8183
Error::NulError(e) => Some(e),
82-
Error::Pkcs11(_)
84+
Error::Pkcs11(_, _)
8385
| Error::NotSupported
8486
| Error::NullFunctionPointer
8587
| Error::PinNotSet
@@ -131,11 +133,5 @@ impl From<std::convert::Infallible> for Error {
131133
}
132134
}
133135

134-
impl From<RvError> for Error {
135-
fn from(rv_error: RvError) -> Self {
136-
Error::Pkcs11(rv_error)
137-
}
138-
}
139-
140136
/// Main Result type
141137
pub type Result<T> = core::result::Result<T, Error>;

cryptoki/src/error/rv.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Function types
44
5+
use crate::context::Function;
6+
57
use super::{Error, Result, RvError};
68
use cryptoki_sys::*;
79
use log::error;
@@ -128,10 +130,10 @@ impl From<CK_RV> for Rv {
128130

129131
impl Rv {
130132
/// Convert the return value into a standard Result type
131-
pub fn into_result(self) -> Result<()> {
133+
pub fn into_result(self, function: Function) -> Result<()> {
132134
match self {
133135
Rv::Ok => Ok(()),
134-
Rv::Error(rv_error) => Err(Error::Pkcs11(rv_error)),
136+
Rv::Error(rv_error) => Err(Error::Pkcs11(rv_error, function)),
135137
}
136138
}
137139
}

cryptoki/src/session/decryption.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Decrypting data
44
5+
use crate::context::Function;
56
use crate::error::{Result, Rv};
67
use crate::mechanism::Mechanism;
78
use crate::object::ObjectHandle;
@@ -26,7 +27,7 @@ impl Session {
2627
&mut mechanism as CK_MECHANISM_PTR,
2728
key.handle(),
2829
))
29-
.into_result()?;
30+
.into_result(Function::DecryptInit)?;
3031
}
3132

3233
// Get the output buffer length
@@ -39,7 +40,7 @@ impl Session {
3940
std::ptr::null_mut(),
4041
&mut data_len,
4142
))
42-
.into_result()?;
43+
.into_result(Function::Decrypt)?;
4344
}
4445

4546
let mut data = vec![0; data_len.try_into()?];
@@ -52,7 +53,7 @@ impl Session {
5253
data.as_mut_ptr(),
5354
&mut data_len,
5455
))
55-
.into_result()?;
56+
.into_result(Function::Decrypt)?;
5657
}
5758

5859
data.resize(data_len.try_into()?, 0);

cryptoki/src/session/digesting.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Digesting functions
44
5+
use crate::context::Function;
56
use crate::error::{Result, Rv};
67
use crate::mechanism::Mechanism;
78
use crate::session::Session;
@@ -19,7 +20,7 @@ impl Session {
1920
self.handle(),
2021
&mut mechanism as CK_MECHANISM_PTR,
2122
))
22-
.into_result()?;
23+
.into_result(Function::DigestInit)?;
2324
}
2425

2526
// Get the output buffer length
@@ -31,7 +32,7 @@ impl Session {
3132
std::ptr::null_mut(),
3233
&mut digest_len,
3334
))
34-
.into_result()?;
35+
.into_result(Function::Digest)?;
3536
}
3637

3738
let mut digest = vec![0; digest_len.try_into()?];
@@ -44,7 +45,7 @@ impl Session {
4445
digest.as_mut_ptr(),
4546
&mut digest_len,
4647
))
47-
.into_result()?;
48+
.into_result(Function::Digest)?;
4849
}
4950

5051
digest.resize(digest_len.try_into()?, 0);

cryptoki/src/session/encryption.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Encrypting data
44
5+
use crate::context::Function;
56
use crate::error::{Result, Rv};
67
use crate::mechanism::Mechanism;
78
use crate::object::ObjectHandle;
@@ -26,7 +27,7 @@ impl Session {
2627
&mut mechanism as CK_MECHANISM_PTR,
2728
key.handle(),
2829
))
29-
.into_result()?;
30+
.into_result(Function::EncryptInit)?;
3031
}
3132

3233
// Get the output buffer length
@@ -38,7 +39,7 @@ impl Session {
3839
std::ptr::null_mut(),
3940
&mut encrypted_data_len,
4041
))
41-
.into_result()?;
42+
.into_result(Function::Encrypt)?;
4243
}
4344

4445
let mut encrypted_data = vec![0; encrypted_data_len.try_into()?];
@@ -51,7 +52,7 @@ impl Session {
5152
encrypted_data.as_mut_ptr(),
5253
&mut encrypted_data_len,
5354
))
54-
.into_result()?;
55+
.into_result(Function::Encrypt)?;
5556
}
5657

5758
encrypted_data.resize(encrypted_data_len.try_into()?, 0);

0 commit comments

Comments
 (0)