Skip to content

Commit 979e631

Browse files
authored
Merge pull request #324 from Jakuje/allowed-mechanism-attribute
Fix handling empty AllowedMechanisms attribute
2 parents cccd9f5 + ad0810a commit 979e631

File tree

2 files changed

+33
-12
lines changed

2 files changed

+33
-12
lines changed

cryptoki/src/object.rs

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,18 +1114,25 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
11141114
Ok(Attribute::ValidationVersion(Version::new(val[0], val[1])))
11151115
}
11161116
AttributeType::AllowedMechanisms => {
1117-
let val = unsafe {
1118-
std::slice::from_raw_parts(
1119-
attribute.pValue as *const CK_MECHANISM_TYPE,
1120-
attribute.ulValueLen.try_into()?,
1121-
)
1122-
};
1123-
let types: Vec<MechanismType> = val
1124-
.iter()
1125-
.copied()
1126-
.map(|t| t.try_into())
1127-
.collect::<Result<Vec<MechanismType>>>()?;
1128-
Ok(Attribute::AllowedMechanisms(types))
1117+
if attribute.ulValueLen == 0 {
1118+
/* For zero-length attributes we are getting pointer to static
1119+
* buffer of length zero, which can not be used to create slices.
1120+
* Short-circuit here to avoid crash (#324) */
1121+
Ok(Attribute::AllowedMechanisms(Vec::new()))
1122+
} else {
1123+
let val = unsafe {
1124+
std::slice::from_raw_parts(
1125+
attribute.pValue as *const CK_MECHANISM_TYPE,
1126+
attribute.ulValueLen.try_into()?,
1127+
)
1128+
};
1129+
let types = val
1130+
.iter()
1131+
.copied()
1132+
.map(|t| t.try_into())
1133+
.collect::<Result<Vec<_>>>()?;
1134+
Ok(Attribute::AllowedMechanisms(types))
1135+
}
11291136
}
11301137
AttributeType::EndDate => {
11311138
if val.is_empty() {

cryptoki/tests/basic.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1117,6 +1117,20 @@ fn import_export() -> TestResult {
11171117
panic!("Expected the Modulus attribute.");
11181118
}
11191119

1120+
let mut attrs =
1121+
session.get_attributes(is_it_the_public_key, &[AttributeType::AllowedMechanisms])?;
1122+
1123+
if is_softhsm() {
1124+
let attr = attrs.remove(0);
1125+
if let Attribute::AllowedMechanisms(v) = attr {
1126+
assert_eq!(v, Vec::<MechanismType>::new());
1127+
} else {
1128+
panic!("Expected the AllowedMechanisms attribute.");
1129+
}
1130+
} else {
1131+
assert_eq!(attrs, Vec::<Attribute>::new());
1132+
}
1133+
11201134
// delete key
11211135
session.destroy_object(is_it_the_public_key)?;
11221136

0 commit comments

Comments
 (0)