-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '58-trussed-auth-version'
Fixes #80
- Loading branch information
Showing
11 changed files
with
209 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
/target | ||
Cargo.lock | ||
trussed-state.bin | ||
/fuzz/target | ||
/fuzz/corpus | ||
/fuzz/artifacts | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.PHONY: ci setup-ubuntu | ||
|
||
ci: | ||
cargo test --verbose | ||
|
||
setup-ubuntu: | ||
sudo apt install llvm libclang-dev make |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Taken from Opcard-rs implementation | ||
// https://github.com/Nitrokey/opcard-rs/blob/ef8ee3b20958cf605a8d93ee98d28e337da9770f/src/virt.rs | ||
|
||
mod dispatch; | ||
|
||
use trussed::{ | ||
types::Bytes, | ||
virt::{self, Client, Ram, StoreProvider}, | ||
}; | ||
|
||
/// Client type using a dispatcher with the backends required | ||
pub type VirtClient<S> = Client<S, dispatch::Dispatch>; | ||
|
||
/// Run a client using a provided store | ||
pub fn with_client<S, R, F>(store: S, client_id: &str, f: F) -> R | ||
where | ||
F: FnOnce(VirtClient<S>) -> R, | ||
S: StoreProvider, | ||
{ | ||
#[allow(clippy::unwrap_used)] | ||
virt::with_platform(store, |platform| { | ||
platform.run_client_with_backends( | ||
client_id, | ||
dispatch::Dispatch::with_hw_key(Bytes::from_slice(b"some bytes").unwrap()), | ||
dispatch::BACKENDS, | ||
f, | ||
) | ||
}) | ||
} | ||
|
||
/// Run the backend with the extensions required | ||
/// using a RAM file storage | ||
pub fn with_ram_client<R, F>(client_id: &str, f: F) -> R | ||
where | ||
F: FnOnce(VirtClient<Ram>) -> R, | ||
{ | ||
with_client(Ram::default(), client_id, f) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
use trussed::{ | ||
api::{reply, request, Reply, Request}, | ||
backend::{Backend as _, BackendId}, | ||
error::Error, | ||
platform::Platform, | ||
serde_extensions::{ExtensionDispatch, ExtensionId, ExtensionImpl as _}, | ||
service::ServiceResources, | ||
types::{Bytes, Context, Location}, | ||
}; | ||
use trussed_auth::{AuthBackend, AuthContext, AuthExtension, MAX_HW_KEY_LEN}; | ||
|
||
pub const BACKENDS: &[BackendId<Backend>] = | ||
&[BackendId::Custom(Backend::Auth), BackendId::Core]; | ||
|
||
pub enum Backend { | ||
Auth, | ||
} | ||
|
||
pub enum Extension { | ||
Auth, | ||
} | ||
|
||
impl From<Extension> for u8 { | ||
fn from(extension: Extension) -> Self { | ||
match extension { | ||
Extension::Auth => 0, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<u8> for Extension { | ||
type Error = Error; | ||
|
||
fn try_from(id: u8) -> Result<Self, Self::Error> { | ||
match id { | ||
0 => Ok(Extension::Auth), | ||
_ => Err(Error::InternalError), | ||
} | ||
} | ||
} | ||
|
||
pub struct Dispatch { | ||
auth: AuthBackend, | ||
} | ||
|
||
#[derive(Default)] | ||
pub struct DispatchContext { | ||
auth: AuthContext, | ||
} | ||
|
||
impl Dispatch { | ||
pub fn with_hw_key(hw_key: Bytes<MAX_HW_KEY_LEN>) -> Self { | ||
Self { | ||
auth: AuthBackend::with_hw_key(Location::Internal, hw_key), | ||
} | ||
} | ||
} | ||
|
||
impl ExtensionDispatch for Dispatch { | ||
type BackendId = Backend; | ||
type Context = DispatchContext; | ||
type ExtensionId = Extension; | ||
|
||
fn core_request<P: Platform>( | ||
&mut self, | ||
backend: &Self::BackendId, | ||
ctx: &mut Context<Self::Context>, | ||
request: &Request, | ||
resources: &mut ServiceResources<P>, | ||
) -> Result<Reply, Error> { | ||
match backend { | ||
Backend::Auth => { | ||
self.auth | ||
.request(&mut ctx.core, &mut ctx.backends.auth, request, resources) | ||
} | ||
} | ||
} | ||
|
||
fn extension_request<P: Platform>( | ||
&mut self, | ||
backend: &Self::BackendId, | ||
extension: &Self::ExtensionId, | ||
ctx: &mut Context<Self::Context>, | ||
request: &request::SerdeExtension, | ||
resources: &mut ServiceResources<P>, | ||
) -> Result<reply::SerdeExtension, Error> { | ||
match backend { | ||
Backend::Auth => match extension { | ||
Extension::Auth => self.auth.extension_request_serialized( | ||
&mut ctx.core, | ||
&mut ctx.backends.auth, | ||
request, | ||
resources, | ||
), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl ExtensionId<AuthExtension> for Dispatch { | ||
type Id = Extension; | ||
|
||
const ID: Self::Id = Self::Id::Auth; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters