Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parsing NSSDB configuration #128

Merged
merged 3 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions src/storage/nssdb/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ impl Default for NSSConfig {
}
}

const END_VALUE: u8 = b' ';

impl NSSConfig {
fn parse_flags(&mut self, args: &[u8]) -> Result<()> {
let mut idx = 0;
Expand Down Expand Up @@ -113,14 +115,14 @@ impl NSSConfig {
'{' => b'}',
'[' => b']',
'<' => b'>',
_ => b' ',
_ => END_VALUE,
};
let valx = if find != b' ' { idx + 2 } else { idx + 1 };
let valx = if find != END_VALUE { idx + 2 } else { idx + 1 };
idx = valx;

while idx < args.len() {
if let Some(pos) = args[idx..].iter().position(|&x| x == find) {
idx = pos;
idx += pos;

/* backtrack check for escapes */
let mut esc = 0;
Expand All @@ -144,15 +146,20 @@ impl NSSConfig {
if idx >= args.len() {
/* This may be the last parameter, in which case it is ok
* if not trailing space is present otherwise error out */
if idx == args.len() && find != ' ' as u8 {
if idx == args.len() && find != END_VALUE as u8 {
return Err(CKR_ARGUMENTS_BAD)?;
}
}

value = String::from_utf8_lossy(&args[valx..idx]).to_string();

if idx < args.len() {
/* accounting for the space separator */
idx += 1;
if find != END_VALUE {
/* accounting for the closing brace/quote/symbol */
idx += 1;
}
}

match name.as_str() {
Expand Down Expand Up @@ -191,7 +198,7 @@ impl NSSConfig {
let mut idx = 0usize;

while idx < bargs.len() {
idx = config.parse_parameter(&bargs[idx..])?;
idx += config.parse_parameter(&bargs[idx..])?;
}
Ok(config)
}
Expand Down
2 changes: 1 addition & 1 deletion src/storage/nssdb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl NSSStorage {
&mut info.manufacturer,
);
if self.config.password_required {
info.flags = CKF_LOGIN_REQUIRED;
info.flags |= CKF_LOGIN_REQUIRED;
}
Ok(info)
}
Expand Down
45 changes: 45 additions & 0 deletions src/tests/nssdb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,48 @@ fn test_nssdb_init_token() {

testtokn.finalize();
}

#[test]
#[parallel]
fn test_nssdb_init_token_params() {
let name = String::from("test_nssdb_init_token_params");
let datadir = format!("{}/{}", TESTDIR, name);

let dbargs = format!(
"configDir={} \
manufacturerID=<My Kryoptic> \
libraryDescription='My Library' \
cryptoTokenDescription=\"My token description\" \
dbTokenDescription=(db Token Description) \
cryptoSlotDescription=[my slot description] \
flags=passwordRequired",
datadir
);
let dbtype = "nssdb";

let mut testtokn =
TestToken::new_type(String::from(dbtype), String::from(""), name);

/* pre-populate conf so we get the correct slot number assigned */
let mut slot = config::Slot::with_db(dbtype, Some(dbargs.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(dbargs.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);

testtokn.finalize();
}