Skip to content

Commit ea256c0

Browse files
committed
Fix Date attribute parsing (parallaxsecond#74)
The Date attribute can be returned as empty (i.e., parameter length is 0), however our parsing did not consider this option. A test is also added to ensure that any regressions are caught. Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent 38d7ffb commit ea256c0

File tree

3 files changed

+101
-32
lines changed

3 files changed

+101
-32
lines changed

cryptoki/src/object.rs

+40-32
Original file line numberDiff line numberDiff line change
@@ -884,41 +884,49 @@ impl TryFrom<CK_ATTRIBUTE> for Attribute {
884884
Ok(Attribute::AllowedMechanisms(types))
885885
}
886886
AttributeType::EndDate => {
887-
let date = val.as_ptr() as *const CK_DATE;
888-
unsafe {
889-
let year = String::from_utf8_lossy(Vec::from((*date).year).as_slice())
890-
.trim_end()
891-
.to_string();
892-
let month = String::from_utf8_lossy(Vec::from((*date).month).as_slice())
893-
.trim_end()
894-
.to_string();
895-
let day = String::from_utf8_lossy(Vec::from((*date).day).as_slice())
896-
.trim_end()
897-
.to_string();
898-
Ok(Attribute::EndDate(Date::new_from_str_slice(
899-
year.as_str(),
900-
month.as_str(),
901-
day.as_str(),
902-
)?))
887+
if val.is_empty() {
888+
Ok(Attribute::EndDate(Date::new_empty()))
889+
} else {
890+
let date = val.as_ptr() as *const CK_DATE;
891+
unsafe {
892+
let year = String::from_utf8_lossy(Vec::from((*date).year).as_slice())
893+
.trim_end()
894+
.to_string();
895+
let month = String::from_utf8_lossy(Vec::from((*date).month).as_slice())
896+
.trim_end()
897+
.to_string();
898+
let day = String::from_utf8_lossy(Vec::from((*date).day).as_slice())
899+
.trim_end()
900+
.to_string();
901+
Ok(Attribute::EndDate(Date::new_from_str_slice(
902+
year.as_str(),
903+
month.as_str(),
904+
day.as_str(),
905+
)?))
906+
}
903907
}
904908
}
905909
AttributeType::StartDate => {
906-
let date = val.as_ptr() as *const CK_DATE;
907-
unsafe {
908-
let year = String::from_utf8_lossy(Vec::from((*date).year).as_slice())
909-
.trim_end()
910-
.to_string();
911-
let month = String::from_utf8_lossy(Vec::from((*date).month).as_slice())
912-
.trim_end()
913-
.to_string();
914-
let day = String::from_utf8_lossy(Vec::from((*date).day).as_slice())
915-
.trim_end()
916-
.to_string();
917-
Ok(Attribute::StartDate(Date::new_from_str_slice(
918-
year.as_str(),
919-
month.as_str(),
920-
day.as_str(),
921-
)?))
910+
if val.is_empty() {
911+
Ok(Attribute::EndDate(Date::new_empty()))
912+
} else {
913+
let date = val.as_ptr() as *const CK_DATE;
914+
unsafe {
915+
let year = String::from_utf8_lossy(Vec::from((*date).year).as_slice())
916+
.trim_end()
917+
.to_string();
918+
let month = String::from_utf8_lossy(Vec::from((*date).month).as_slice())
919+
.trim_end()
920+
.to_string();
921+
let day = String::from_utf8_lossy(Vec::from((*date).day).as_slice())
922+
.trim_end()
923+
.to_string();
924+
Ok(Attribute::StartDate(Date::new_from_str_slice(
925+
year.as_str(),
926+
month.as_str(),
927+
day.as_str(),
928+
)?))
929+
}
922930
}
923931
}
924932
}

cryptoki/src/types.rs

+23
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,29 @@ impl Date {
7979
let date = CK_DATE { year, month, day };
8080
Self { date }
8181
}
82+
83+
/// Creates a new, empty `Date` structure
84+
///
85+
/// This represents the default value of the attribute (on
86+
/// newer implementations of `Cryptoki`).
87+
pub fn new_empty() -> Self {
88+
let date = CK_DATE {
89+
year: Default::default(),
90+
month: Default::default(),
91+
day: Default::default(),
92+
};
93+
Self { date }
94+
}
95+
96+
/// Check if `Date` is empty
97+
///
98+
/// *NOTE*: This function is only representative of newer implementations
99+
/// of `Cryptoki`, for which dates are represented as empty object attributes.
100+
pub fn is_empty(&self) -> bool {
101+
self.date.year == <[u8; 4]>::default()
102+
&& self.date.month == <[u8; 2]>::default()
103+
&& self.date.day == <[u8; 2]>::default()
104+
}
82105
}
83106

84107
impl Deref for Date {

cryptoki/tests/basic.rs

+38
Original file line numberDiff line numberDiff line change
@@ -729,3 +729,41 @@ fn is_initialized_test() {
729729
Ok(()) => panic!("Initializing twice should not have been allowed"),
730730
}
731731
}
732+
733+
#[test]
734+
#[serial]
735+
fn aes_key_attributes_test() -> Result<()> {
736+
let (pkcs11, slot) = init_pins();
737+
738+
// set flags
739+
let mut flags = SessionFlags::new();
740+
let _ = flags.set_rw_session(true).set_serial_session(true);
741+
742+
// open a session
743+
let session = pkcs11.open_session_no_callback(slot, flags)?;
744+
745+
// log in the session
746+
session.login(UserType::User, Some(USER_PIN))?;
747+
748+
// get mechanism
749+
let mechanism = Mechanism::AesKeyGen;
750+
751+
// pub key template
752+
let key_template = vec![
753+
Attribute::Class(ObjectClass::SECRET_KEY),
754+
Attribute::Token(true),
755+
Attribute::Sensitive(true),
756+
Attribute::ValueLen(16.into()),
757+
Attribute::KeyType(KeyType::AES),
758+
Attribute::Label(b"testAES".to_vec()),
759+
Attribute::Private(true),
760+
];
761+
762+
// generate a key pair
763+
let key = session.generate_key(&mechanism, &key_template)?;
764+
765+
let _attributes_result =
766+
session.get_attributes(key, &[AttributeType::EndDate, AttributeType::StartDate])?;
767+
768+
Ok(())
769+
}

0 commit comments

Comments
 (0)