Skip to content

Implement update attributes for objects #119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(())
}