From 733a44e3930ed2d3e5f81a6b438a92a22c5304bf Mon Sep 17 00:00:00 2001 From: Alexandru Placinta Date: Mon, 7 Sep 2026 18:19:29 +0200 Subject: [PATCH] Allow call with null pointer for handle key and template --- cryptoki/src/session/key_management.rs | 55 +++++++++++++++++++------- 1 file changed, 40 insertions(+), 15 deletions(-) diff --git a/cryptoki/src/session/key_management.rs b/cryptoki/src/session/key_management.rs index 0d5dfd13..69b310f3 100644 --- a/cryptoki/src/session/key_management.rs +++ b/cryptoki/src/session/key_management.rs @@ -73,24 +73,49 @@ impl Session { &self, mechanism: &Mechanism, base_key: ObjectHandle, - template: &[Attribute], + template: Option<&[Attribute]>, ) -> Result { let mut mechanism: CK_MECHANISM = mechanism.into(); - let mut template: Vec = 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::>() + }); - 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