Skip to content
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
1 change: 1 addition & 0 deletions cryptoki/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ wycheproof = { version = "0.7.0", features = ["aead"] }
[features]
generate-bindings = ["cryptoki-sys/generate-bindings"]
serde = ["secrecy/serde"]
vendor-specifics = []
45 changes: 45 additions & 0 deletions cryptoki/src/session/key_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use crate::context::Function;
use crate::error::{Result, Rv};
use crate::mechanism::Mechanism;
#[cfg(feature = "vendor-specifics")]
use crate::mechanism::{vendor_defined::VendorDefinedMechanism, MechanismType};
use crate::object::{Attribute, ObjectHandle};
use crate::session::Session;
use cryptoki_sys::{CK_ATTRIBUTE, CK_MECHANISM, CK_MECHANISM_PTR};
Expand Down Expand Up @@ -93,6 +95,49 @@ impl Session {
Ok(ObjectHandle::new(handle))
}

/// Vendore specific call that uses `mechanism` as the output parameter.
Comment thread
wiktor-k marked this conversation as resolved.
///
/// A use case can be found [here](https://thalesdocs.com/gphsm/luna/7/docs/network/Content/sdk/extensions/BIP32.htm)
/// where public and private key handles are stored in the `hPublicKey` and
/// `hPrivateKey`, corresponding to `pParameter` from `CK_MECHANISM`.
#[cfg(feature = "vendor-specifics")]
pub fn derive_key_vendor<'a, T>(
&self,
mechanism: usize,
params: &mut T,
base_key: ObjectHandle,
template: impl Into<Option<&'a [Attribute]>>,
) -> Result<()> {
let mut mechanism = VendorDefinedMechanism::new(
MechanismType::new_vendor_defined(mechanism.try_into()?)?,
Some(params),
);
let mut template = template.into().map(|template: &[Attribute]| {
template
.iter()
.map(|attr| attr.into())
.collect::<Vec<CK_ATTRIBUTE>>()
});
let (template_ptr, template_len) = template
.as_mut()
.map(|template| (template.as_mut_ptr(), template.len()))
.unwrap_or((std::ptr::null_mut(), 0));

unsafe {
Rv::from(get_pkcs11!(self.client(), C_DeriveKey)(
self.handle(),
&mut mechanism.inner as CK_MECHANISM_PTR,
base_key.handle(),
template_ptr,
template_len.try_into()?,
std::ptr::null_mut(),
))
.into_result(Function::DeriveKey)?;
}

Ok(())
}

/// Wrap key
pub fn wrap_key(
&self,
Expand Down
Loading