From b7d69291ce46416fb677a0ff7925ae10ece1cb05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Thu, 29 Aug 2024 17:29:59 +0200 Subject: [PATCH] Get test working --- pkcs11/tests/basic.rs | 71 +++++++++++++++++++++++++++++++++++++-- pkcs11/tests/tools/mod.rs | 11 +++--- 2 files changed, 72 insertions(+), 10 deletions(-) diff --git a/pkcs11/tests/basic.rs b/pkcs11/tests/basic.rs index 4b08c817..f8141813 100644 --- a/pkcs11/tests/basic.rs +++ b/pkcs11/tests/basic.rs @@ -1,11 +1,60 @@ #![cfg(feature = "pkcs11-full-tests")] +use core::ptr; + use config_file::{InstanceConfig, P11Config, SlotConfig, UserConfig}; +use pkcs11::types::{ + CKA_MODULUS_BITS, CKA_PUBLIC_EXPONENT, CKA_SIGN, CKA_TOKEN, CKA_VERIFY, CKM_RSA_PKCS, + CK_ATTRIBUTE, CK_BBOOL, CK_FALSE, CK_MECHANISM, CK_TRUE, CK_ULONG, +}; mod tools; +const RSA_PRIVATE_KEY_ATTRIBUTES: &[CK_ATTRIBUTE] = &[ + CK_ATTRIBUTE { + attrType: CKA_SIGN, + pValue: &CK_TRUE as *const _ as *mut _, + ulValueLen: size_of::() as _, + }, + CK_ATTRIBUTE { + attrType: CKA_TOKEN, + pValue: &CK_FALSE as *const _ as *mut _, + ulValueLen: size_of::() as _, + }, +]; + +const RSA_PUBLIC_KEY_ATTRIBUTES: &[CK_ATTRIBUTE] = &[ + CK_ATTRIBUTE { + attrType: CKA_VERIFY, + pValue: &CK_TRUE as *const _ as *mut _, + ulValueLen: size_of::() as _, + }, + CK_ATTRIBUTE { + attrType: CKA_MODULUS_BITS, + pValue: &(2048 as CK_ULONG) as *const _ as *mut _, + ulValueLen: size_of::() as _, + }, + CK_ATTRIBUTE { + attrType: CKA_TOKEN, + pValue: &CK_FALSE as *const _ as *mut _, + ulValueLen: size_of::() as _, + }, + CK_ATTRIBUTE { + attrType: CKA_PUBLIC_EXPONENT, + pValue: [0x01, 0x00, 0x01].as_ptr() as *mut _, + ulValueLen: 3 as _, + }, +]; + +const RSA_MECHANISM: CK_MECHANISM = CK_MECHANISM { + mechanism: CKM_RSA_PKCS, + pParameter: ptr::null_mut(), + ulParameterLen: 0, +}; + #[test] fn basic() { + env_logger::init(); tools::run_tests( P11Config { slots: vec![SlotConfig { @@ -14,7 +63,10 @@ fn basic() { username: "operator".into(), password: Some("opPassphrase".into()), }), - administrator: None, + administrator: Some(UserConfig { + username: "admin".into(), + password: Some("Administrator".into()), + }), description: Some("Test slot".into()), instances: vec![InstanceConfig { url: option_env!("TEST_NETHSM_INSTANCE") @@ -30,8 +82,21 @@ fn basic() { ..Default::default() }, |ctx| { - let session = 0; - ctx.open_session(session, 0x04, None, None).unwrap(); + let slot = 0; + let session = ctx.open_session(slot, 0x04, None, None).unwrap(); + let (_public_key, private_key) = ctx + .generate_key_pair( + session, + &RSA_MECHANISM, + RSA_PUBLIC_KEY_ATTRIBUTES, + RSA_PRIVATE_KEY_ATTRIBUTES, + ) + .unwrap(); + let data = [0x42; 32]; + ctx.sign_init(session, &RSA_MECHANISM, private_key).unwrap(); + + // Verifying signatures is not supported + let _signature = ctx.sign(session, &data).unwrap(); }, ) } diff --git a/pkcs11/tests/tools/mod.rs b/pkcs11/tests/tools/mod.rs index 8ae04bdd..0e350501 100644 --- a/pkcs11/tests/tools/mod.rs +++ b/pkcs11/tests/tools/mod.rs @@ -10,10 +10,7 @@ pub use config_file::P11Config; use nethsm_sdk_rs::{ apis::{ configuration::Configuration, - default_api::{ - provision_post, system_factory_reset_post, users_post, SystemFactoryResetPostError, - }, - ResponseContent, + default_api::{provision_post, users_user_id_put}, }, models::{ProvisionRequestData, UserPostData, UserRole}, }; @@ -151,7 +148,7 @@ pub fn run_tests(config: P11Config, f: impl FnOnce(&mut Ctx)) { ..Default::default() }; - sleep(Duration::from_secs(1)); + sleep(Duration::from_secs(2)); // match system_factory_reset_post(&sdk_config) { // Ok(_) => {} @@ -176,8 +173,9 @@ pub fn run_tests(config: P11Config, f: impl FnOnce(&mut Ctx)) { }, ) .unwrap(); - users_post( + users_user_id_put( &sdk_config, + "operator", UserPostData { real_name: "Operator".into(), role: UserRole::Operator, @@ -191,7 +189,6 @@ pub fn run_tests(config: P11Config, f: impl FnOnce(&mut Ctx)) { serde_yaml::to_writer(BufWriter::new(tmpfile.as_file_mut()), &config).unwrap(); let path = tmpfile.path(); set_var(config_file::ENV_VAR_CONFIG_FILE, path); - dbg!("loading"); let mut ctx = Ctx::new_and_initialize("../target/release/libnethsm_pkcs11.so").unwrap(); f(&mut ctx); ctx.close_all_sessions(0).unwrap();