Skip to content

Commit

Permalink
Use transparent encrypted storage
Browse files Browse the repository at this point in the history
  • Loading branch information
szszszsz committed Sep 30, 2022
1 parent cda9f88 commit a14a49b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 40 deletions.
29 changes: 5 additions & 24 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 9 additions & 2 deletions examples/udp_sim/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ extern crate delog;

use delog::log;
use heapless_bytes::{Bytes, Bytes32};
use std::path::PathBuf;

use webcrypt::{RequestDetails, RequestSource, Webcrypt};
use webcrypt::{RequestDetails, RequestSource, Webcrypt, DEFAULT_ENCRYPTION_PIN};

use crate::udp_server::UDPServer;
use trussed::types::ClientContext;

generate_macros!();

Expand All @@ -24,7 +26,12 @@ fn main() -> std::io::Result<()> {
log::info!("Initializing Trussed");
let trussed_platform = platform::init_platform("state_file");
let mut trussed_service = trussed::service::Service::new(trussed_platform);
let trussed_client = trussed_service.try_as_new_client("webcrypt").unwrap();
let trussed_client = trussed_service
.try_as_new_client_ctx(ClientContext::new(
littlefs2::path::PathBuf::from("webcrypt"),
Some(DEFAULT_ENCRYPTION_PIN),
))
.unwrap();
log::info!("Initializing Webcrypt {}", webcrypt::GIT_VERSION);
let mut w = Webcrypt::new(trussed_client);
let mut server = UDPServer::new();
Expand Down
56 changes: 43 additions & 13 deletions src/lib/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ use crate::types::ERROR_ID;
use crate::helpers::hash;
use crate::openpgp::OpenPGPData;
use crate::types::ERROR_ID::{
ERR_BAD_FORMAT, ERR_BAD_ORIGIN, ERR_FAILED_LOADING_DATA, ERR_NOT_FOUND,
ERR_BAD_FORMAT, ERR_BAD_ORIGIN, ERR_FAILED_LOADING_DATA, ERR_INVALID_PIN, ERR_NOT_FOUND,
};
use crate::{Message, RequestSource};
use crate::{Message, RequestSource, DEFAULT_ENCRYPTION_PIN};

type CommandResult = Result<(), ERROR_ID>;

Expand Down Expand Up @@ -919,16 +919,28 @@ where
}
};

let tp = w
.session
.login(req.pin, &mut w.trussed, &rpid, &mut w.state)?;
try_syscall!(w
.trussed
.set_client_context_pin(Bytes::from_slice(req.pin.as_slice()).unwrap()))
.map_err(|_| ERR_INTERNAL_ERROR)?;

// ignore loading errors for now
if !w.state.initialized() {
w.state
.load(&mut w.trussed)
.map_err(|_| ERR_FAILED_LOADING_DATA)?
log::debug!("WC loading state");
let res = w
.state
.load(&mut w.trussed)
// the cause might be in the corrupted storage as well (ERR_FAILED_LOADING_DATA),
// but we can't differentiate at this point
.map_err(|_| ERR_INVALID_PIN);
if res.is_err() {
w.state.pin.decrease_counter()?;
res?
}

let tp = w
.session
.login(req.pin.clone(), &mut w.trussed, &rpid, &mut w.state)?;

w.send_to_output(CommandLoginResponse { tp });

Ok(())
Expand All @@ -953,6 +965,10 @@ where
// Clear session
w.session.logout();
w.state.logout();
try_syscall!(w
.trussed
.set_client_context_pin(Bytes::from_slice(b"invalid pin").unwrap()))
.map_err(|_| ERR_INTERNAL_ERROR)?;

Ok(())
}
Expand All @@ -974,6 +990,9 @@ where
.trussed
.remove_dir_all(Location::Internal, PathBuf::from("wcrk"),));

let default_pin = Bytes::from_slice(DEFAULT_ENCRYPTION_PIN.as_ref()).unwrap();
try_syscall!(w.trussed.reset_pin(default_pin)).map_err(|_| ERR_INTERNAL_ERROR)?;

// delete persistent state
// reset PIN
w.state.reset(&mut w.trussed);
Expand Down Expand Up @@ -1033,15 +1052,28 @@ where
let req: CommandSetPINRequest = w
.get_input_deserialized()
.map_err(|_| ERROR_ID::ERR_BAD_FORMAT)?;
w.state.pin.set_pin(req.pin)?;
w.state.pin.set_pin(req.pin.clone())?;

try_syscall!(w.trussed.set_client_context_pin(
Bytes::from_slice(DEFAULT_ENCRYPTION_PIN.as_ref()).unwrap()
))
.map_err(|_| ERR_INTERNAL_ERROR)?;
try_syscall!(w.trussed.change_pin(req.pin.to_bytes().unwrap()))
.map_err(|_| ERR_INTERNAL_ERROR)?;

w.state.initialize(&mut w.trussed);
Ok(())
}
CHANGE_PIN => {
let req: CommandChangePINRequest = w
.get_input_deserialized()
.map_err(|_| ERROR_ID::ERR_BAD_FORMAT)?;
w.state.pin.change_pin(req.pin, req.newpin)?;
w.state.pin.change_pin(req.pin, req.newpin.clone())?;
try_syscall!(w
.trussed
.change_pin(Bytes::from_slice(req.newpin.as_slice()).unwrap()))
.map_err(|_| ERROR_ID::ERR_INTERNAL_ERROR)?;

Ok(())
}
_ => Err(ERROR_ID::ERR_INVALID_COMMAND),
Expand Down Expand Up @@ -1133,8 +1165,6 @@ where
.check_token_res(req.tp.unwrap())
.map_err(|_| ERROR_ID::ERR_REQ_AUTH)?;

use trussed::key::Kind;

let private_key = try_syscall!(w.trussed.unsafe_inject_shared_key(
// &k.serialize(),
req.raw_key_data.as_slice(),
Expand Down
1 change: 1 addition & 0 deletions src/lib/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod types;
mod wcstate;

pub const MAX_MESSAGE_LENGTH: usize = 1024;
pub const DEFAULT_ENCRYPTION_PIN: &'static str = "1234";

pub type Message = Bytes<MAX_MESSAGE_LENGTH>;

Expand Down
12 changes: 11 additions & 1 deletion src/lib/wcstate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,15 @@ impl WebcryptPIN {
self.counter
}

pub fn decrease_counter(&mut self) -> Result<(), ERROR_ID> {
if self.counter == 0 {
log::info!("Counter PIN used up");
return Err(ERR_NOT_ALLOWED);
}
self.counter -= 1;
Ok(())
}

pub fn check_pin(&mut self, pin: Bytes64) -> Result<bool, ERROR_ID> {
if self.pin.is_none() {
log::info!("PIN not set");
Expand All @@ -54,7 +63,8 @@ impl WebcryptPIN {
log::info!("Counter PIN used up");
return Err(ERR_NOT_ALLOWED);
}
self.counter -= 1;
self.decrease_counter()?;

log::info!("Counter PIN value: {:?}", self.counter);

// TODO use side-channels safe comparison library, e.g. subtle
Expand Down

0 comments on commit a14a49b

Please sign in to comment.