Skip to content

Commit 3a21c37

Browse files
committed
Fix Date attribute parsing (#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 23328a8 commit 3a21c37

File tree

3 files changed

+97
-32
lines changed

3 files changed

+97
-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
@@ -53,6 +53,29 @@ impl Date {
5353
let date = CK_DATE { year, month, day };
5454
Self { date }
5555
}
56+
57+
/// Creates a new, empty `Date` structure
58+
///
59+
/// This represents the default value of the attribute (on
60+
/// newer implementations of `Cryptoki`).
61+
pub fn new_empty() -> Self {
62+
let date = CK_DATE {
63+
year: Default::default(),
64+
month: Default::default(),
65+
day: Default::default(),
66+
};
67+
Self { date }
68+
}
69+
70+
/// Check if `Date` is empty
71+
///
72+
/// *NOTE*: This function is only representative of newer implementations
73+
/// of `Cryptoki`, for which dates are represented as empty object attributes.
74+
pub fn is_empty(&self) -> bool {
75+
self.date.year == <[u8; 4]>::default()
76+
&& self.date.month == <[u8; 2]>::default()
77+
&& self.date.day == <[u8; 2]>::default()
78+
}
5679
}
5780

5881
impl Deref for Date {

cryptoki/tests/basic.rs

+34
Original file line numberDiff line numberDiff line change
@@ -677,3 +677,37 @@ fn is_initialized_test() {
677677
Ok(()) => panic!("Initializing twice should not have been allowed"),
678678
}
679679
}
680+
681+
#[test]
682+
#[serial]
683+
fn aes_key_attributes_test() -> Result<()> {
684+
let (pkcs11, slot) = init_pins();
685+
686+
// open a session
687+
let session = pkcs11.open_session_no_callback(slot, true)?;
688+
689+
// log in the session
690+
session.login(UserType::User, Some(USER_PIN))?;
691+
692+
// get mechanism
693+
let mechanism = Mechanism::AesKeyGen;
694+
695+
// pub key template
696+
let key_template = vec![
697+
Attribute::Class(ObjectClass::SECRET_KEY),
698+
Attribute::Token(true),
699+
Attribute::Sensitive(true),
700+
Attribute::ValueLen(16.into()),
701+
Attribute::KeyType(KeyType::AES),
702+
Attribute::Label(b"testAES".to_vec()),
703+
Attribute::Private(true),
704+
];
705+
706+
// generate a key pair
707+
let key = session.generate_key(&mechanism, &key_template)?;
708+
709+
let _attributes_result =
710+
session.get_attributes(key, &[AttributeType::EndDate, AttributeType::StartDate])?;
711+
712+
Ok(())
713+
}

0 commit comments

Comments
 (0)