Skip to content

Commit 697a4bf

Browse files
committed
Add support for vendor defined attributes
Signed-off-by: Joe Rozner <[email protected]>
1 parent c45a81b commit 697a4bf

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

cryptoki/src/object.rs

+12-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use std::mem::size_of;
1515
use std::ops::Deref;
1616

1717
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)]
18-
#[non_exhaustive]
1918
/// Type of an attribute
2019
pub enum AttributeType {
2120
/// DER-encoding of the attribute certificate's issuer
@@ -128,6 +127,8 @@ pub enum AttributeType {
128127
Value,
129128
/// Length in bytes of the value
130129
ValueLen,
130+
/// Vendor defined attribute
131+
VendorDefined(CK_ATTRIBUTE_TYPE),
131132
/// Determines if a key supports verifying
132133
Verify,
133134
/// Determines if a key supports verifying where the data can be recovered from the signature
@@ -254,6 +255,7 @@ impl AttributeType {
254255
CKA_UNWRAP_TEMPLATE => String::from(stringify!(CKA_UNWRAP_TEMPLATE)),
255256
CKA_DERIVE_TEMPLATE => String::from(stringify!(CKA_DERIVE_TEMPLATE)),
256257
CKA_ALLOWED_MECHANISMS => String::from(stringify!(CKA_ALLOWED_MECHANISMS)),
258+
CKA_VENDOR_DEFINED => String::from(stringify!(CKA_VENDOR_DEFINED)),
257259
_ => format!("unknown ({val:08x})"),
258260
}
259261
}
@@ -324,6 +326,7 @@ impl From<AttributeType> for CK_ATTRIBUTE_TYPE {
324326
AttributeType::Url => CKA_URL,
325327
AttributeType::Value => CKA_VALUE,
326328
AttributeType::ValueLen => CKA_VALUE_LEN,
329+
AttributeType::VendorDefined(val) => val,
327330
AttributeType::Verify => CKA_VERIFY,
328331
AttributeType::VerifyRecover => CKA_VERIFY_RECOVER,
329332
AttributeType::Wrap => CKA_WRAP,
@@ -396,6 +399,7 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
396399
CKA_VERIFY_RECOVER => Ok(AttributeType::VerifyRecover),
397400
CKA_WRAP => Ok(AttributeType::Wrap),
398401
CKA_WRAP_WITH_TRUSTED => Ok(AttributeType::WrapWithTrusted),
402+
0x8000_0000..=0xffff_ffff => Ok(AttributeType::VendorDefined(attribute_type)),
399403
attr_type => {
400404
error!("Attribute type {} not supported.", attr_type);
401405
Err(Error::NotSupported)
@@ -405,7 +409,6 @@ impl TryFrom<CK_ATTRIBUTE_TYPE> for AttributeType {
405409
}
406410

407411
#[derive(Debug, Clone, PartialEq, Eq)]
408-
#[non_exhaustive]
409412
/// Attribute value
410413
pub enum Attribute {
411414
/// DER-encoding of the attribute certificate's issuer
@@ -518,6 +521,8 @@ pub enum Attribute {
518521
Value(Vec<u8>),
519522
/// Length in bytes of the value
520523
ValueLen(Ulong),
524+
/// Vendor defined value
525+
VendorDefined((CK_ATTRIBUTE_TYPE, Vec<u8>)),
521526
/// Determines if a key supports verifying
522527
Verify(bool),
523528
/// Determines if a key supports verifying where the data can be recovered from the signature
@@ -587,6 +592,7 @@ impl Attribute {
587592
Attribute::Url(_) => AttributeType::Url,
588593
Attribute::Value(_) => AttributeType::Value,
589594
Attribute::ValueLen(_) => AttributeType::ValueLen,
595+
Attribute::VendorDefined((num, _)) => AttributeType::VendorDefined(*num),
590596
Attribute::Verify(_) => AttributeType::Verify,
591597
Attribute::VerifyRecover(_) => AttributeType::VerifyRecover,
592598
Attribute::Wrap(_) => AttributeType::Wrap,
@@ -658,6 +664,7 @@ impl Attribute {
658664
Attribute::AllowedMechanisms(mechanisms) => {
659665
size_of::<CK_MECHANISM_TYPE>() * mechanisms.len()
660666
}
667+
Attribute::VendorDefined((_, bytes)) => bytes.len(),
661668
}
662669
}
663670

@@ -730,6 +737,7 @@ impl Attribute {
730737
| Attribute::Subject(bytes)
731738
| Attribute::Url(bytes)
732739
| Attribute::Value(bytes)
740+
| Attribute::VendorDefined((_, bytes))
733741
| Attribute::Id(bytes) => bytes.as_ptr() as *mut c_void,
734742
// Unique types
735743
Attribute::CertificateType(certificate_type) => {
@@ -929,7 +937,8 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
929937
)?))
930938
}
931939
}
932-
}
940+
},
941+
AttributeType::VendorDefined(t) => Ok(Attribute::VendorDefined((t, val.to_vec()))),
933942
}
934943
}
935944
}

0 commit comments

Comments
 (0)