Skip to content

Commit a884179

Browse files
authored
Merge pull request #317 from jhshannon17/feature/pub-unsafe-handles
Add pub unsafe accessors for object/session handles and ObjectHandle new
2 parents 49cb1fb + a99ad88 commit a884179

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

cryptoki/src/object.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1193,7 +1193,15 @@ impl ObjectHandle {
11931193
ObjectHandle { handle }
11941194
}
11951195

1196-
pub(crate) fn handle(&self) -> CK_OBJECT_HANDLE {
1196+
/// Create a new object handle from a raw handle.
1197+
/// # Safety
1198+
/// Considered unsafe due to ability for client to arbitrarily create object handles.
1199+
pub unsafe fn new_from_raw(handle: CK_OBJECT_HANDLE) -> Self {
1200+
ObjectHandle { handle }
1201+
}
1202+
1203+
/// Get the raw handle of the object.
1204+
pub fn handle(&self) -> CK_OBJECT_HANDLE {
11971205
self.handle
11981206
}
11991207
}

cryptoki/src/session/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ impl Session {
7878
/// This will be called on drop as well.
7979
pub fn close(self) {}
8080

81-
pub(crate) fn handle(&self) -> CK_SESSION_HANDLE {
81+
/// Get the raw handle of the session.
82+
pub fn handle(&self) -> CK_SESSION_HANDLE {
8283
self.handle
8384
}
8485

cryptoki/tests/basic.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4296,3 +4296,55 @@ fn validation() -> TestResult {
42964296

42974297
Ok(())
42984298
}
4299+
4300+
#[test]
4301+
#[serial]
4302+
fn object_handle_new_from_raw() -> TestResult {
4303+
let (pkcs11, slot) = init_pins();
4304+
4305+
// open a session
4306+
let session = pkcs11.open_rw_session(slot)?;
4307+
4308+
// log in the session
4309+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
4310+
4311+
// get mechanism
4312+
let mechanism = Mechanism::RsaPkcsKeyPairGen;
4313+
4314+
let public_exponent: Vec<u8> = vec![0x01, 0x00, 0x01];
4315+
let modulus_bits = 2048;
4316+
4317+
// pub key template
4318+
let pub_key_template = vec![
4319+
Attribute::Token(true),
4320+
Attribute::Private(false),
4321+
Attribute::PublicExponent(public_exponent),
4322+
Attribute::ModulusBits(modulus_bits.into()),
4323+
Attribute::Verify(true),
4324+
];
4325+
4326+
// priv key template
4327+
let priv_key_template = vec![Attribute::Token(true), Attribute::Sign(true)];
4328+
4329+
// generate a key pair
4330+
let (public, private) =
4331+
session.generate_key_pair(&mechanism, &pub_key_template, &priv_key_template)?;
4332+
4333+
let private_cloned = unsafe { ObjectHandle::new_from_raw(private.handle()) };
4334+
let public_cloned = unsafe { ObjectHandle::new_from_raw(public.handle()) };
4335+
4336+
// data to sign
4337+
let data = [0xFF, 0x55, 0xDD];
4338+
4339+
// sign something with it
4340+
let signature = session.sign(&Mechanism::RsaPkcs, private_cloned, &data)?;
4341+
4342+
// verify the signature
4343+
session.verify(&Mechanism::RsaPkcs, public_cloned, &data, &signature)?;
4344+
4345+
// delete keys
4346+
session.destroy_object(public)?;
4347+
session.destroy_object(private)?;
4348+
4349+
Ok(())
4350+
}

0 commit comments

Comments
 (0)