Skip to content

Commit 74a094b

Browse files
committed
Expose vendor defined error code
Fixes #299
1 parent 868a6d8 commit 74a094b

File tree

3 files changed

+38
-7
lines changed

3 files changed

+38
-7
lines changed

cryptoki/src/error/rv.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Function types
44
5-
use crate::context::Function;
5+
use crate::{context::Function, object::MAX_CU_ULONG};
66

77
use super::{Error, Result, RvError};
88
use cryptoki_sys::*;
99
use log::error;
1010

11-
#[derive(Copy, Clone, Debug)]
11+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1212
/// Return value of a PKCS11 function
1313
pub enum Rv {
1414
/// The function exited successfully
@@ -116,7 +116,8 @@ impl From<CK_RV> for Rv {
116116
CKR_PIN_TOO_WEAK => Rv::Error(RvError::PinTooWeak),
117117
CKR_PUBLIC_KEY_INVALID => Rv::Error(RvError::PublicKeyInvalid),
118118
CKR_FUNCTION_REJECTED => Rv::Error(RvError::FunctionRejected),
119-
CKR_VENDOR_DEFINED => Rv::Error(RvError::VendorDefined),
119+
// Section 3.6 of v3.1: "Return values CKR_VENDOR_DEFINED and above are permanently reserved for token vendors."
120+
CKR_VENDOR_DEFINED..=MAX_CU_ULONG => Rv::Error(RvError::VendorDefined(ck_rv)),
120121
other => {
121122
error!(
122123
"Can not find a corresponding error for {}, converting to GeneralError.",
@@ -137,3 +138,32 @@ impl Rv {
137138
}
138139
}
139140
}
141+
142+
#[cfg(test)]
143+
mod test {
144+
use super::{Rv, RvError};
145+
use cryptoki_sys::*;
146+
147+
#[test]
148+
fn vendor_defined_exact() {
149+
let code = CKR_VENDOR_DEFINED;
150+
let actual = Rv::from(code);
151+
let expected = Rv::Error(RvError::VendorDefined(code));
152+
assert_eq!(actual, expected);
153+
}
154+
155+
#[test]
156+
fn vendor_defined_higher() {
157+
let code = CKR_VENDOR_DEFINED + 42;
158+
let actual = Rv::from(code);
159+
let expected = Rv::Error(RvError::VendorDefined(code));
160+
assert_eq!(actual, expected);
161+
}
162+
163+
#[test]
164+
fn unknown_code() {
165+
let actual = Rv::from(CKR_VENDOR_DEFINED - 42);
166+
let expected = Rv::Error(RvError::GeneralError);
167+
assert_eq!(actual, expected);
168+
}
169+
}

cryptoki/src/error/rv_error.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//! Function types
44
5+
use cryptoki_sys::CK_RV;
56
use std::fmt;
67

78
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
@@ -193,8 +194,8 @@ pub enum RvError {
193194
PublicKeyInvalid,
194195
/// The signature request is rejected by the user.
195196
FunctionRejected,
196-
/// CKR_VENDOR_DEFINED
197-
VendorDefined,
197+
/// A vendor defined error code, CKR_VENDOR_DEFINED and above.
198+
VendorDefined(CK_RV),
198199
}
199200

200201
impl fmt::Display for RvError {
@@ -293,7 +294,7 @@ impl fmt::Display for RvError {
293294
RvError::PinTooWeak => write!(f, "The specified PIN is too weak so that it could be easy to guess. If the PIN is too short, CKR_PIN_LEN_RANGE should be returned instead. This return code only applies to functions which attempt to set a PIN."),
294295
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."),
295296
RvError::FunctionRejected => write!(f, "The signature request is rejected by the user."),
296-
RvError::VendorDefined => write!(f, "CKR_VENDOR_DEFINED"),
297+
RvError::VendorDefined(code) => write!(f, "CKR_VENDOR_DEFINED({code:#x})",),
297298
}
298299
}
299300
}

cryptoki/src/object.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::fmt::Formatter;
1414
use std::mem::size_of;
1515
use std::ops::Deref;
1616

17-
const MAX_CU_ULONG: CK_ULONG = !0;
17+
pub(crate) const MAX_CU_ULONG: CK_ULONG = !0;
1818

1919
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
2020
#[non_exhaustive]

0 commit comments

Comments
 (0)