Skip to content

Commit fabf938

Browse files
committed
Expose error code for unknown errors
This makes debugging easier, since the error code is readily available. Signed-off-by: Thore Goebel <[email protected]>
1 parent 42fb2ac commit fabf938

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

cryptoki/src/error/rv.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ impl From<CK_RV> for Rv {
120120
CKR_VENDOR_DEFINED..=CK_ULONG::MAX => Rv::Error(RvError::VendorDefined(ck_rv)),
121121
other => {
122122
error!(
123-
"Can not find a corresponding error for {}, converting to GeneralError.",
123+
"Can not find a corresponding error for {}, converting to UnknownErrorCode.",
124124
other
125125
);
126-
Rv::Error(RvError::GeneralError)
126+
Rv::Error(RvError::UnknownErrorCode(other))
127127
}
128128
}
129129
}
@@ -159,4 +159,12 @@ mod test {
159159
let expected = Rv::Error(RvError::VendorDefined(code));
160160
assert_eq!(actual, expected);
161161
}
162+
163+
#[test]
164+
fn unknown_code() {
165+
let code = CKR_VENDOR_DEFINED - 42;
166+
let actual = Rv::from(code);
167+
let expected = Rv::Error(RvError::UnknownErrorCode(code));
168+
assert_eq!(actual, expected);
169+
}
162170
}

cryptoki/src/error/rv_error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ pub enum RvError {
196196
FunctionRejected,
197197
/// A vendor defined error code, CKR_VENDOR_DEFINED and above.
198198
VendorDefined(CK_RV),
199+
/// An unknown error code
200+
UnknownErrorCode(CK_RV),
199201
}
200202

201203
impl fmt::Display for RvError {
@@ -295,6 +297,7 @@ impl fmt::Display for RvError {
295297
RvError::PublicKeyInvalid => write!(f, "The public key fails a public key validation. For example, an EC public key fails the public key validation specified in Section 5.2.2 of ANSI X9.62. This error code may be returned by C_CreateObject, when the public key is created, or by C_VerifyInit or C_VerifyRecoverInit, when the public key is used. It may also be returned by C_DeriveKey, in preference to CKR_MECHANISM_PARAM_INVALID, if the other party's public key specified in the mechanism's parameters is invalid."),
296298
RvError::FunctionRejected => write!(f, "The signature request is rejected by the user."),
297299
RvError::VendorDefined(code) => write!(f, "CKR_VENDOR_DEFINED({code:#x})"),
300+
RvError::UnknownErrorCode(code) => write!(f, "Unknown error code: {code:#x}"),
298301
}
299302
}
300303
}

0 commit comments

Comments
 (0)