Skip to content

Commit

Permalink
Get test working
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Aug 29, 2024
1 parent 53cd729 commit b7d6929
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
71 changes: 68 additions & 3 deletions pkcs11/tests/basic.rs
Original file line number Diff line number Diff line change
@@ -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::<CK_BBOOL>() as _,
},
CK_ATTRIBUTE {
attrType: CKA_TOKEN,
pValue: &CK_FALSE as *const _ as *mut _,
ulValueLen: size_of::<CK_BBOOL>() as _,
},
];

const RSA_PUBLIC_KEY_ATTRIBUTES: &[CK_ATTRIBUTE] = &[
CK_ATTRIBUTE {
attrType: CKA_VERIFY,
pValue: &CK_TRUE as *const _ as *mut _,
ulValueLen: size_of::<CK_BBOOL>() as _,
},
CK_ATTRIBUTE {
attrType: CKA_MODULUS_BITS,
pValue: &(2048 as CK_ULONG) as *const _ as *mut _,
ulValueLen: size_of::<CK_ULONG>() as _,
},
CK_ATTRIBUTE {
attrType: CKA_TOKEN,
pValue: &CK_FALSE as *const _ as *mut _,
ulValueLen: size_of::<CK_BBOOL>() 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 {
Expand All @@ -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")
Expand All @@ -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();
},
)
}
11 changes: 4 additions & 7 deletions pkcs11/tests/tools/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};
Expand Down Expand Up @@ -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(_) => {}
Expand All @@ -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,
Expand All @@ -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();
Expand Down

0 comments on commit b7d6929

Please sign in to comment.