Skip to content

Commit a3b5e75

Browse files
authored
Merge pull request #300 from thgoebel/vendor-error-codes
Expose vendor defined error code
2 parents 868a6d8 + fabf938 commit a3b5e75

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

cryptoki/src/error/rv.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ 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,13 +116,14 @@ 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..=CK_ULONG::MAX => Rv::Error(RvError::VendorDefined(ck_rv)),
120121
other => {
121122
error!(
122-
"Can not find a corresponding error for {}, converting to GeneralError.",
123+
"Can not find a corresponding error for {}, converting to UnknownErrorCode.",
123124
other
124125
);
125-
Rv::Error(RvError::GeneralError)
126+
Rv::Error(RvError::UnknownErrorCode(other))
126127
}
127128
}
128129
}
@@ -137,3 +138,33 @@ 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 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+
}
170+
}

cryptoki/src/error/rv_error.rs

Lines changed: 7 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,10 @@ 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),
199+
/// An unknown error code
200+
UnknownErrorCode(CK_RV),
198201
}
199202

200203
impl fmt::Display for RvError {
@@ -293,7 +296,8 @@ impl fmt::Display for RvError {
293296
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."),
294297
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."),
295298
RvError::FunctionRejected => write!(f, "The signature request is rejected by the user."),
296-
RvError::VendorDefined => write!(f, "CKR_VENDOR_DEFINED"),
299+
RvError::VendorDefined(code) => write!(f, "CKR_VENDOR_DEFINED({code:#x})"),
300+
RvError::UnknownErrorCode(code) => write!(f, "Unknown error code: {code:#x}"),
297301
}
298302
}
299303
}

cryptoki/src/object.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ use std::fmt::Formatter;
1414
use std::mem::size_of;
1515
use std::ops::Deref;
1616

17-
const MAX_CU_ULONG: CK_ULONG = !0;
18-
1917
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
2018
#[non_exhaustive]
2119
/// Type of an attribute
@@ -261,7 +259,7 @@ impl AttributeType {
261259
CKA_DERIVE_TEMPLATE => String::from(stringify!(CKA_DERIVE_TEMPLATE)),
262260
CKA_ALLOWED_MECHANISMS => String::from(stringify!(CKA_ALLOWED_MECHANISMS)),
263261
CKA_UNIQUE_ID => String::from(stringify!(CKA_UNIQUE_ID)),
264-
CKA_VENDOR_DEFINED..=MAX_CU_ULONG => {
262+
CKA_VENDOR_DEFINED..=CK_ULONG::MAX => {
265263
format!("{}_{}", stringify!(CKA_VENDOR_DEFINED), val)
266264
}
267265
_ => format!("unknown ({val:08x})"),
@@ -409,7 +407,7 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
409407
CKA_VERIFY_RECOVER => Ok(AttributeType::VerifyRecover),
410408
CKA_WRAP => Ok(AttributeType::Wrap),
411409
CKA_WRAP_WITH_TRUSTED => Ok(AttributeType::WrapWithTrusted),
412-
CKA_VENDOR_DEFINED..=MAX_CU_ULONG => Ok(AttributeType::VendorDefined(attribute_type)),
410+
CKA_VENDOR_DEFINED..=CK_ULONG::MAX => Ok(AttributeType::VendorDefined(attribute_type)),
413411
attr_type => {
414412
error!("Attribute type {} not supported.", attr_type);
415413
Err(Error::NotSupported)
@@ -1296,7 +1294,7 @@ impl KeyType {
12961294
CKK_EC_EDWARDS => String::from(stringify!(CKK_EC_EDWARDS)),
12971295
CKK_EC_MONTGOMERY => String::from(stringify!(CKK_EC_MONTGOMERY)),
12981296
CKK_HKDF => String::from(stringify!(CKK_HKDF)),
1299-
CKK_VENDOR_DEFINED..=MAX_CU_ULONG => String::from(stringify!(key_type)),
1297+
CKK_VENDOR_DEFINED..=CK_ULONG::MAX => String::from(stringify!(key_type)),
13001298
_ => format!("unknown ({key_type:08x})"),
13011299
}
13021300
}
@@ -1371,7 +1369,7 @@ impl TryFrom<CK_KEY_TYPE> for KeyType {
13711369
CKK_EC_EDWARDS => Ok(KeyType::EC_EDWARDS),
13721370
CKK_EC_MONTGOMERY => Ok(KeyType::EC_MONTGOMERY),
13731371
CKK_HKDF => Ok(KeyType::HKDF),
1374-
CKK_VENDOR_DEFINED..=MAX_CU_ULONG => KeyType::new_vendor_defined(key_type),
1372+
CKK_VENDOR_DEFINED..=CK_ULONG::MAX => KeyType::new_vendor_defined(key_type),
13751373
_ => {
13761374
error!("Key type {} is not supported.", key_type);
13771375
Err(Error::NotSupported)

0 commit comments

Comments
 (0)