Skip to content

Commit 3d14be8

Browse files
authored
Merge pull request #237 from jrozner/add-vendor-defined-attributes
Add support for vendor defined attributes
2 parents c45a81b + 6921373 commit 3d14be8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

cryptoki/src/object.rs

+18
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ 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+
1719
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
1820
#[non_exhaustive]
1921
/// Type of an attribute
@@ -128,6 +130,8 @@ pub enum AttributeType {
128130
Value,
129131
/// Length in bytes of the value
130132
ValueLen,
133+
/// Vendor defined attribute
134+
VendorDefined(CK_ATTRIBUTE_TYPE),
131135
/// Determines if a key supports verifying
132136
Verify,
133137
/// Determines if a key supports verifying where the data can be recovered from the signature
@@ -254,6 +258,9 @@ impl AttributeType {
254258
CKA_UNWRAP_TEMPLATE => String::from(stringify!(CKA_UNWRAP_TEMPLATE)),
255259
CKA_DERIVE_TEMPLATE => String::from(stringify!(CKA_DERIVE_TEMPLATE)),
256260
CKA_ALLOWED_MECHANISMS => String::from(stringify!(CKA_ALLOWED_MECHANISMS)),
261+
CKA_VENDOR_DEFINED..=MAX_CU_ULONG => {
262+
format!("{}_{}", stringify!(CKA_VENDOR_DEFINED), val)
263+
}
257264
_ => format!("unknown ({val:08x})"),
258265
}
259266
}
@@ -324,6 +331,7 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
324331
AttributeType::Url => CKA_URL,
325332
AttributeType::Value => CKA_VALUE,
326333
AttributeType::ValueLen => CKA_VALUE_LEN,
334+
AttributeType::VendorDefined(val) => val,
327335
AttributeType::Verify => CKA_VERIFY,
328336
AttributeType::VerifyRecover => CKA_VERIFY_RECOVER,
329337
AttributeType::Wrap => CKA_WRAP,
@@ -396,6 +404,7 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
396404
CKA_VERIFY_RECOVER => Ok(AttributeType::VerifyRecover),
397405
CKA_WRAP => Ok(AttributeType::Wrap),
398406
CKA_WRAP_WITH_TRUSTED => Ok(AttributeType::WrapWithTrusted),
407+
CKA_VENDOR_DEFINED..=MAX_CU_ULONG => Ok(AttributeType::VendorDefined(attribute_type)),
399408
attr_type => {
400409
error!("Attribute type {} not supported.", attr_type);
401410
Err(Error::NotSupported)
@@ -518,6 +527,8 @@ pub enum Attribute {
518527
Value(Vec<u8>),
519528
/// Length in bytes of the value
520529
ValueLen(Ulong),
530+
/// Vendor defined value
531+
VendorDefined((AttributeType, Vec<u8>)),
521532
/// Determines if a key supports verifying
522533
Verify(bool),
523534
/// Determines if a key supports verifying where the data can be recovered from the signature
@@ -587,6 +598,7 @@ impl Attribute {
587598
Attribute::Url(_) => AttributeType::Url,
588599
Attribute::Value(_) => AttributeType::Value,
589600
Attribute::ValueLen(_) => AttributeType::ValueLen,
601+
Attribute::VendorDefined((num, _)) => *num,
590602
Attribute::Verify(_) => AttributeType::Verify,
591603
Attribute::VerifyRecover(_) => AttributeType::VerifyRecover,
592604
Attribute::Wrap(_) => AttributeType::Wrap,
@@ -658,6 +670,7 @@ impl Attribute {
658670
Attribute::AllowedMechanisms(mechanisms) => {
659671
size_of::<CK_MECHANISM_TYPE>() * mechanisms.len()
660672
}
673+
Attribute::VendorDefined((_, bytes)) => bytes.len(),
661674
}
662675
}
663676

@@ -730,6 +743,7 @@ impl Attribute {
730743
| Attribute::Subject(bytes)
731744
| Attribute::Url(bytes)
732745
| Attribute::Value(bytes)
746+
| Attribute::VendorDefined((_, bytes))
733747
| Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void,
734748
// Unique types
735749
Attribute::CertificateType(certificate_type) => {
@@ -930,6 +944,10 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
930944
}
931945
}
932946
}
947+
AttributeType::VendorDefined(t) => Ok(Attribute::VendorDefined((
948+
AttributeType::VendorDefined(t),
949+
val.to_vec(),
950+
))),
933951
}
934952
}
935953
}

0 commit comments

Comments
 (0)