Skip to content

Commit 3340e30

Browse files
jhshannon17jack-shannon-ripple
authored andcommitted
Add pub accessors for object/session handles and unsafe ObjectHandle new_from_raw
Signed-off-by: Jack Shannon <[email protected]>
1 parent f137d48 commit 3340e30

File tree

4 files changed

+77
-4
lines changed

4 files changed

+77
-4
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+
}

cryptoki/tests/common/mod.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub static USER_PIN: &str = "fedcba123456";
1212
// The default SO pin
1313
pub static SO_PIN: &str = "abcdef654321";
1414

15-
fn get_pkcs11_path() -> String {
15+
pub fn get_pkcs11_path() -> String {
1616
env::var("TEST_PKCS11_MODULE")
1717
.unwrap_or_else(|_| "/usr/local/lib/softhsm/libsofthsm2.so".to_string())
1818
}
@@ -41,9 +41,21 @@ pub fn get_pkcs11() -> Pkcs11 {
4141
Pkcs11::new(get_pkcs11_path()).unwrap()
4242
}
4343

44+
#[allow(dead_code)]
45+
pub fn get_pkcs11_from_self() -> Pkcs11 {
46+
Pkcs11::new_from_self().unwrap()
47+
}
48+
49+
#[allow(dead_code)]
4450
pub fn init_pins() -> (Pkcs11, Slot) {
4551
let pkcs11 = get_pkcs11();
4652

53+
let slot = init_pins_from_pkcs11(&pkcs11);
54+
55+
(pkcs11, slot)
56+
}
57+
58+
pub fn init_pins_from_pkcs11(pkcs11: &Pkcs11) -> Slot {
4759
// initialize the library
4860
pkcs11.initialize(CInitializeArgs::OsThreads).unwrap();
4961

@@ -61,7 +73,7 @@ pub fn init_pins() -> (Pkcs11, Slot) {
6173
session.init_pin(&AuthPin::new(USER_PIN.into())).unwrap();
6274
}
6375

64-
(pkcs11, slot)
76+
slot
6577
}
6678

6779
#[allow(dead_code)]

0 commit comments

Comments
 (0)