Skip to content

Commit

Permalink
Merge pull request parallaxsecond#119 from baloo/baloo/update-attributes
Browse files Browse the repository at this point in the history
Implement update attributes for objects
  • Loading branch information
wiktor-k authored Jan 25, 2023
2 parents efe141a + e840f46 commit 5c35c82
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
19 changes: 19 additions & 0 deletions cryptoki/src/session/object_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,22 @@ pub(super) fn get_attributes(
// Convert from CK_ATTRIBUTE to Attribute
template.into_iter().map(|attr| attr.try_into()).collect()
}

impl Session {
/// Sets the attributes of an object
pub fn update_attributes(&self, object: ObjectHandle, template: &[Attribute]) -> Result<()> {
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect();

unsafe {
Rv::from(get_pkcs11!(self.client(), C_SetAttributeValue)(
self.handle(),
object.handle(),
template.as_mut_ptr(),
template.len().try_into()?,
))
.into_result()?;
}

Ok(())
}
}
43 changes: 43 additions & 0 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -826,3 +826,46 @@ fn aes_cbc_pad_encrypt() -> TestResult {
assert_eq!(expected_cipher[..], cipher[..]);
Ok(())
}

#[test]
#[serial]
fn update_attributes_key() -> TestResult {
let (pkcs11, slot) = init_pins();
// open a session
let session = pkcs11.open_rw_session(slot)?;

// log in the session
session.login(UserType::User, Some(USER_PIN))?;

// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(true),
Attribute::PublicExponent(vec![0x01, 0x00, 0x01]),
Attribute::ModulusBits(1024.into()),
];

// priv key template
let priv_key_template = vec![Attribute::Token(true), Attribute::Extractable(true)];

let (_public_key, private_key) = session.generate_key_pair(
&Mechanism::RsaPkcsKeyPairGen,
&pub_key_template,
&priv_key_template,
)?;

let updated_attributes = vec![Attribute::Extractable(false)];

session.update_attributes(private_key, &updated_attributes)?;

let mut attributes_result =
session.get_attributes(private_key, &[AttributeType::Extractable])?;

if let Some(Attribute::Extractable(ext)) = attributes_result.pop() {
assert!(!ext);
} else {
panic!("Last attribute was not extractable");
}

Ok(())
}

0 comments on commit 5c35c82

Please sign in to comment.