Skip to content

Commit

Permalink
Fix find-credential following migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
sosthene-nitrokey committed Mar 26, 2024
1 parent 5872fc8 commit 81818b0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
18 changes: 12 additions & 6 deletions src/ctap2/credential_management.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,20 @@ where
fn find_credential(&mut self, credential: &PublicKeyCredentialDescriptor) -> Option<PathBuf> {
let credential_id_hash = self.hash(&credential.id[..]);
let mut hex = [b'0'; 16];
super::format_hex(&credential_id_hash[..8], &mut hex);
let hex_str = super::format_hex(&credential_id_hash[..8], &mut hex);
let dir = PathBuf::from(b"rk");
let filename = PathBuf::from(&hex);

syscall!(self
.trussed
.locate_file(Location::Internal, Some(dir), filename,))
.path
let mut maybe_entry =
try_syscall!(self.trussed.read_dir_first(Location::Internal, dir, None))
.ok()?
.entry;
while let Some(entry) = maybe_entry {
if entry.file_name().as_str().ends_with(&hex_str) {
return Some(entry.path().into());
}
maybe_entry = syscall!(self.trussed.read_dir_next()).entry;
}
None
}

pub fn delete_credential(
Expand Down
25 changes: 19 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,16 @@ where
}

// EWW.. this is a bit unsafe isn't it
fn format_hex(data: &[u8], mut buffer: &mut [u8]) {
fn format_hex<'a>(data: &[u8], buffer: &'a mut [u8]) -> &'a str {
const HEX_CHARS: &[u8] = b"0123456789abcdef";
for byte in data.iter() {
buffer[0] = HEX_CHARS[(byte >> 4) as usize];
buffer[1] = HEX_CHARS[(byte & 0xf) as usize];
buffer = &mut buffer[2..];
assert!(data.len() * 2 >= buffer.len());
for (idx, byte) in data.iter().enumerate() {
buffer[idx * 2] = HEX_CHARS[(byte >> 4) as usize];
buffer[idx * 2 + 1] = HEX_CHARS[(byte & 0xf) as usize];
}

// SAFETY: we just added only ascii chars to buffer from 0 to data.len() - 1
unsafe { core::str::from_utf8_unchecked(&buffer[0..data.len() * 2]) }
}

// NB: to actually use this, replace the constant implementation with the inline assembly.
Expand Down Expand Up @@ -290,4 +293,14 @@ where
}

#[cfg(test)]
mod test {}
mod test {
use super::*;

#[test]
fn hex() {
let data = [0x01, 0x02, 0xB1, 0xA1];
let buffer = &mut [0; 8];
assert_eq!(format_hex(&data, buffer), "0102b1a1");
assert_eq!(buffer, b"0102b1a1");
}
}

0 comments on commit 81818b0

Please sign in to comment.