Skip to content

Commit

Permalink
Add more tests for NSSDB
Browse files Browse the repository at this point in the history
Signed-off-by: Simo Sorce <[email protected]>
  • Loading branch information
simo5 committed Nov 8, 2024
1 parent ea8e15b commit 2197fb2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
62 changes: 62 additions & 0 deletions src/tests/nssdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,65 @@ fn test_nssdb_token() {

testtokn.finalize();
}

#[test]
#[parallel]
fn test_nssdb_init_token() {
let datadir = format!("{}/{}", TESTDIR, "test_nssdb_init_token");

let dbpath = format!("configDir={}", datadir);
let dbtype = "nssdb";
let dbname = format!("{}:{}", dbtype, dbpath);

let mut testtokn = TestToken::new(dbname);

/* pre-populate conf so we get the correct slot number assigned */
let mut slot = config::Slot::with_db(dbtype, Some(dbpath.clone()));
slot.slot = u32::try_from(testtokn.get_slot()).unwrap();
let ret = add_slot(slot);

assert_eq!(ret, CKR_OK);
let mut args = TestToken::make_init_args(Some(dbpath.clone()));
let args_ptr = &mut args as *mut CK_C_INITIALIZE_ARGS;
let ret = fn_initialize(args_ptr as *mut std::ffi::c_void);
assert_eq!(ret, CKR_OK);

/* init once (NSSDB ignores SO pin) */
let pin_value = "Unused";
let ret = fn_init_token(
testtokn.get_slot(),
CString::new(pin_value).unwrap().into_raw() as *mut u8,
pin_value.len() as CK_ULONG,
std::ptr::null_mut(),
);
assert_eq!(ret, CKR_OK);

let session = testtokn.get_session(true);

/* NSS allows SO login w/o PIN only to set the initial User PIN */
let ret = fn_login(session, CKU_SO, &mut [] as *mut u8, 0);
assert_eq!(ret, CKR_OK);

/* set user pin */
let user_pin = "User PIN Value";
let ret = fn_init_pin(
session,
CString::new(user_pin).unwrap().into_raw() as *mut u8,
user_pin.len() as CK_ULONG,
);
assert_eq!(ret, CKR_OK);

let ret = fn_logout(session);
assert_eq!(ret, CKR_OK);

/* try to login as user */
let ret = fn_login(
session,
CKU_USER,
CString::new(user_pin).unwrap().into_raw() as *mut u8,
user_pin.len() as CK_ULONG,
);
assert_eq!(ret, CKR_OK);

testtokn.finalize();
}
16 changes: 10 additions & 6 deletions src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,15 @@ impl Token {
let mut info = self.storage.reinit(&self.facilities)?;

/* Add SO PIN */
self.set_pin(CKU_SO, pin, &[])?;
match self.set_pin(CKU_SO, pin, &[]) {
Ok(()) => (),
Err(e) => {
/* not all storage dbs support setting a CKU_SO Pin */
if e.rv() != CKR_USER_TYPE_INVALID {
return Err(e);
}
}
}

/* copy Label */
copy_sized_string(label, &mut info.label);
Expand Down Expand Up @@ -214,10 +222,6 @@ impl Token {
}

pub fn is_logged_in(&self, user_type: CK_USER_TYPE) -> bool {
if user_type != CKU_SO && self.info.flags & CKF_LOGIN_REQUIRED == 0 {
return true;
}

match user_type {
KRY_UNSPEC => self.logged == CKU_SO || self.logged == CKU_USER,
CKU_SO => self.logged == CKU_SO,
Expand Down Expand Up @@ -459,7 +463,7 @@ impl Token {
let is_logged = self.is_logged_in(KRY_UNSPEC);

/* value does not matter, only type does */
let dnm: CK_BBOOL = 0;
let dnm: CK_BBOOL = CK_FALSE;
let mut attrs = CkAttrs::from(template);
if !is_logged {
attrs.add_bool(CKA_TOKEN, &dnm);
Expand Down

0 comments on commit 2197fb2

Please sign in to comment.