Skip to content

Commit 5b28163

Browse files
committed
Implement update attributes for objects
Signed-off-by: Arthur Gautier <[email protected]>
1 parent efe141a commit 5b28163

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

cryptoki/src/session/object_management.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,3 +194,22 @@ pub(super) fn get_attributes(
194194
// Convert from CK_ATTRIBUTE to Attribute
195195
template.into_iter().map(|attr| attr.try_into()).collect()
196196
}
197+
198+
impl Session {
199+
/// Sets the attributes of an object
200+
pub fn update_attributes(&self, object: ObjectHandle, template: &[Attribute]) -> Result<()> {
201+
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect();
202+
203+
unsafe {
204+
Rv::from(get_pkcs11!(self.client(), C_SetAttributeValue)(
205+
self.handle(),
206+
object.handle(),
207+
template.as_mut_ptr(),
208+
template.len().try_into()?,
209+
))
210+
.into_result()?;
211+
}
212+
213+
Ok(())
214+
}
215+
}

cryptoki/tests/basic.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -826,3 +826,46 @@ fn aes_cbc_pad_encrypt() -> TestResult {
826826
assert_eq!(expected_cipher[..], cipher[..]);
827827
Ok(())
828828
}
829+
830+
#[test]
831+
#[serial]
832+
fn update_attributes_key() -> Result<()> {
833+
let (pkcs11, slot) = init_pins();
834+
// open a session
835+
let session = pkcs11.open_rw_session(slot)?;
836+
837+
// log in the session
838+
session.login(UserType::User, Some(USER_PIN))?;
839+
840+
// pub key template
841+
let pub_key_template = vec![
842+
Attribute::Token(true),
843+
Attribute::Private(true),
844+
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
845+
Attribute::ModulusBits(1024.into()),
846+
];
847+
848+
// priv key template
849+
let priv_key_template = vec![Attribute::Token(true), Attribute::Extractable(true)];
850+
851+
let (_public_key, private_key) = session.generate_key_pair(
852+
&Mechanism::RsaPkcsKeyPairGen,
853+
&pub_key_template,
854+
&priv_key_template,
855+
)?;
856+
857+
let updated_attributes = vec![Attribute::Extractable(false)];
858+
859+
session.update_attributes(private_key, &updated_attributes)?;
860+
861+
let mut attributes_result =
862+
session.get_attributes(private_key, &[AttributeType::Extractable])?;
863+
864+
if let Some(Attribute::Extractable(ext)) = attributes_result.pop() {
865+
assert!(ext == false);
866+
} else {
867+
panic!("Last attribute was not extractable");
868+
}
869+
870+
Ok(())
871+
}

0 commit comments

Comments
 (0)