Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions cryptoki/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1114,18 +1114,25 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
Ok(Attribute::ValidationVersion(Version::new(val[0], val[1])))
}
AttributeType::AllowedMechanisms => {
let val = unsafe {
std::slice::from_raw_parts(
attribute.pValue as *const CK_MECHANISM_TYPE,
attribute.ulValueLen.try_into()?,
)
};
let types: Vec<MechanismType> = val
.iter()
.copied()
.map(|t| t.try_into())
.collect::<Result<Vec<MechanismType>>>()?;
Ok(Attribute::AllowedMechanisms(types))
if attribute.ulValueLen == 0 {
/* For zero-length attributes we are getting pointer to static
* buffer of length zero, which can not be used to create slices.
* Short-circuit here to avoid crash (#324) */
Ok(Attribute::AllowedMechanisms(Vec::<MechanismType>::new()))
} else {
let val = unsafe {
std::slice::from_raw_parts(
attribute.pValue as *const CK_MECHANISM_TYPE,
attribute.ulValueLen.try_into()?,
)
};
let types: Vec<MechanismType> = val
.iter()
.copied()
.map(|t| t.try_into())
.collect::<Result<Vec<MechanismType>>>()?;
Ok(Attribute::AllowedMechanisms(types))
}
}
AttributeType::EndDate => {
if val.is_empty() {
Expand Down
14 changes: 14 additions & 0 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1113,6 +1113,20 @@ fn import_export() -> TestResult {
panic!("Expected the Modulus attribute.");
}

let mut attrs =
session.get_attributes(is_it_the_public_key, &[AttributeType::AllowedMechanisms])?;

if is_softhsm() {
let attr = attrs.remove(0);
if let Attribute::AllowedMechanisms(v) = attr {
assert_eq!(v, Vec::<MechanismType>::new());
} else {
panic!("Expected the AllowedMechanisms attribute.");
}
} else {
assert_eq!(attrs, Vec::<Attribute>::new());
}

// delete key
session.destroy_object(is_it_the_public_key)?;

Expand Down
Loading