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
10 changes: 9 additions & 1 deletion cryptoki/src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,15 @@ impl ObjectHandle {
ObjectHandle { handle }
}

pub(crate) fn handle(&self) -> CK_OBJECT_HANDLE {
/// Create a new object handle from a raw handle.
/// # Safety
/// Considered unsafe due to ability for client to arbitrarily create object handles.
pub unsafe fn new_from_raw(handle: CK_OBJECT_HANDLE) -> Self {
ObjectHandle { handle }
}

/// Get the raw handle of the object.
pub fn handle(&self) -> CK_OBJECT_HANDLE {
self.handle
}
}
Expand Down
3 changes: 2 additions & 1 deletion cryptoki/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ impl Session {
/// This will be called on drop as well.
pub fn close(self) {}

pub(crate) fn handle(&self) -> CK_SESSION_HANDLE {
/// Get the raw handle of the session.
pub fn handle(&self) -> CK_SESSION_HANDLE {
self.handle
}

Expand Down
52 changes: 52 additions & 0 deletions cryptoki/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4296,3 +4296,55 @@ fn validation() -> TestResult {

Ok(())
}

#[test]
#[serial]
fn object_handle_new_from_raw() -> 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(&AuthPin::new(USER_PIN.into())))?;

// get mechanism
let mechanism = Mechanism::RsaPkcsKeyPairGen;

let public_exponent: Vec<u8> = vec![0x01, 0x00, 0x01];
let modulus_bits = 2048;

// pub key template
let pub_key_template = vec![
Attribute::Token(true),
Attribute::Private(false),
Attribute::PublicExponent(public_exponent),
Attribute::ModulusBits(modulus_bits.into()),
Attribute::Verify(true),
];

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

// generate a key pair
let (public, private) =
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;

let private_cloned = unsafe { ObjectHandle::new_from_raw(private.handle()) };
let public_cloned = unsafe { ObjectHandle::new_from_raw(public.handle()) };

// data to sign
let data = [0xFF, 0x55, 0xDD];

// sign something with it
let signature = session.sign(&Mechanism::RsaPkcs, private_cloned, &data)?;

// verify the signature
session.verify(&Mechanism::RsaPkcs, public_cloned, &data, &signature)?;

// delete keys
session.destroy_object(public)?;
session.destroy_object(private)?;

Ok(())
}
Loading