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

Expose whether a given client contains initialized data #45

Merged
merged 1 commit into from
Apr 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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ hex-literal = "0.4.1"

[patch.crates-io]
littlefs2 = { git = "https://github.com/sosthene-nitrokey/littlefs2.git", rev = "2b45a7559ff44260c6dd693e4cb61f54ae5efc53" }
trussed = { git = "https://github.com/Nitrokey/trussed.git", rev = "be04182e2c74e73599a394e814d353bc4bf79484" }
trussed = { git = "https://github.com/Nitrokey/trussed.git", tag = "v0.1.0-nitrokey.19" }
trussed-manage = { git = "https://github.com/trussed-dev/trussed-staging.git", tag = "manage-v0.1.0" }
apdu-dispatch = { git = "https://github.com/trussed-dev/apdu-dispatch.git", rev = "915fc237103fcecc29d0f0b73391f19abf6576de" }
ctaphid-dispatch = { git = "https://github.com/trussed-dev/ctaphid-dispatch.git", rev = "57cb3317878a8593847595319aa03ef17c29ec5b" }
Expand Down
38 changes: 33 additions & 5 deletions src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ mod data;
use core::fmt;

use hkdf::Hkdf;
use littlefs2::{
path,
path::{Path, PathBuf},
};
use rand_core::{CryptoRng, RngCore};
use sha2::Sha256;
use trussed::{
Expand All @@ -14,9 +18,9 @@ use trussed::{
key::{Kind, Secrecy},
platform::Platform,
serde_extensions::ExtensionImpl,
service::{Keystore, ServiceResources},
store::filestore::Filestore,
types::{CoreContext, Location, PathBuf},
service::{ClientFilestore, Keystore, ServiceResources},
store::{filestore::Filestore, Store},
types::{CoreContext, Location},
Bytes,
};

Expand Down Expand Up @@ -211,6 +215,31 @@ impl AuthBackend {
}
}

impl AuthBackend {
/// Returns whether a client is active (has auth data such as PINs)
pub fn is_client_active(
layout: FilesystemLayout,
location: Location,
client: &Path,
store: impl Store,
) -> Result<bool> {
let backend_path = client.join(BACKEND_DIR);
let mut fs;
match layout {
FilesystemLayout::V0 => {
fs = ClientFilestore::new(backend_path, store);
}
FilesystemLayout::V1 => {
fs = ClientFilestore::new_raw(backend_path, store);
}
}

Ok(fs
.read_dir_first(path!(""), location, &trussed::api::NotBefore::None)?
.is_some())
}
}

/// Per-client context for [`AuthBackend`][]
#[derive(Default, Debug)]
pub struct AuthContext {
Expand All @@ -230,8 +259,7 @@ impl ExtensionImpl<AuthExtension> for AuthBackend {
resources: &mut ServiceResources<P>,
) -> Result<AuthReply> {
// FIXME: Have a real implementation from trussed
let mut backend_path = core_ctx.path.clone();
backend_path.push(&PathBuf::from(BACKEND_DIR));
let backend_path = core_ctx.path.join(BACKEND_DIR);
let mut fs;
let mut global_fs;
match self.layout {
Expand Down
11 changes: 6 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,12 @@ pub mod migrate;

use core::str::FromStr;

use serde::{Deserialize, Serialize};
use trussed::{
config::MAX_SHORT_DATA_LENGTH,
types::{Bytes, PathBuf},
use littlefs2::{
path,
path::{Path, PathBuf},
};
use serde::{Deserialize, Serialize};
use trussed::{config::MAX_SHORT_DATA_LENGTH, types::Bytes};

pub use backend::{AuthBackend, AuthContext, FilesystemLayout, MAX_HW_KEY_LEN};
pub use extension::{
Expand All @@ -84,7 +85,7 @@ pub const MAX_PIN_LENGTH: usize = MAX_SHORT_DATA_LENGTH;
/// A PIN.
pub type Pin = Bytes<MAX_PIN_LENGTH>;

const BACKEND_DIR: &str = "backend-auth";
const BACKEND_DIR: &Path = path!("backend-auth");

/// The ID of a PIN within the namespace of a client.
///
Expand Down