Skip to content

Commit 16b0532

Browse files
committed
object: Avoid crashing on zero-length AllowedMechanisms attribute
As returned by SoftHSM: softhsm/SoftHSMv2#825 Fixes: #323 Signed-off-by: Jakub Jelen <[email protected]>
1 parent 2702bba commit 16b0532

File tree

1 file changed

+16
-12
lines changed

1 file changed

+16
-12
lines changed

cryptoki/src/object.rs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1114,18 +1114,22 @@ 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+
Ok(Attribute::AllowedMechanisms(Vec::<MechanismType>::new()))
1119+
} else {
1120+
let val = unsafe {
1121+
std::slice::from_raw_parts(
1122+
attribute.pValue as *const CK_MECHANISM_TYPE,
1123+
attribute.ulValueLen.try_into()?,
1124+
)
1125+
};
1126+
let types: Vec<MechanismType> = val
1127+
.iter()
1128+
.copied()
1129+
.map(|t| t.try_into())
1130+
.collect::<Result<Vec<MechanismType>>>()?;
1131+
Ok(Attribute::AllowedMechanisms(types))
1132+
}
11291133
}
11301134
AttributeType::EndDate => {
11311135
if val.is_empty() {

0 commit comments

Comments
 (0)