Skip to content
Open
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
55 changes: 40 additions & 15 deletions cryptoki/src/session/key_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,24 +73,49 @@ impl Session {
&self,
mechanism: &Mechanism,
base_key: ObjectHandle,
template: &[Attribute],
template: Option<&[Attribute]>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this could be:

Suggested change
template: Option<&[Attribute]>,
template: impl Into<Option<&[Attribute]>>,

and then somewhere below:

let template = template.into();

This would allow existing clients to use normal syntax and also allow passing None.

Example in the Rust Playground: https://play.rust-lang.org/?version=stable&mode=debug&edition=2024&gist=5e16ba050b6c7d7736801d3b552e3dbd

(it seems it requires explicit lifetime there)

) -> Result<ObjectHandle> {
let mut mechanism: CK_MECHANISM = mechanism.into();
let mut template: Vec<CK_ATTRIBUTE> = template.iter().map(|attr| attr.into()).collect();
let mut handle = 0;
unsafe {
Rv::from(get_pkcs11!(self.client(), C_DeriveKey)(
self.handle(),
&mut mechanism as CK_MECHANISM_PTR,
base_key.handle(),
template.as_mut_ptr(),
template.len().try_into()?,
&mut handle,
))
.into_result(Function::DeriveKey)?;
}
let template = template.map(|template| {
template
.iter()
.map(|attr| attr.into())
.collect::<Vec<CK_ATTRIBUTE>>()
});

Ok(ObjectHandle::new(handle))
match template {
Some(template) => {
let mut template = template;
let mut handle = 0;

unsafe {
Rv::from(get_pkcs11!(self.client(), C_DeriveKey)(
self.handle(),
&mut mechanism as CK_MECHANISM_PTR,
base_key.handle(),
template.as_mut_ptr(),
template.len().try_into()?,
&mut handle,
))
.into_result(Function::DeriveKey)?;
}

Ok(ObjectHandle::new(handle))
}
None => unsafe {
Rv::from(get_pkcs11!(self.client(), C_DeriveKey)(
self.handle(),
&mut mechanism as CK_MECHANISM_PTR,
base_key.handle(),
std::ptr::null_mut(),
0,
std::ptr::null_mut(),
))
.into_result(Function::DeriveKey)?;

Ok(ObjectHandle::new(0))
},
}
}

/// Wrap key
Expand Down
Loading