Skip to content

Commit

Permalink
Increase size of file writing
Browse files Browse the repository at this point in the history
The increased size of files is a requirement for  PIV,
1kB certificates are too small.

This commit also removes the `user_attribute` field that was never read anyway.
This avoids increasing the total size of the interchange
  • Loading branch information
sosthene-nitrokey committed Mar 1, 2023
1 parent 67b4ee9 commit 27c1196
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 24 deletions.
9 changes: 4 additions & 5 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,7 @@ pub mod request {
WriteFile:
- location: Location
- path: PathBuf
- data: Message
- user_attribute: Option<UserAttribute>
- data: LargeMessage

UnsafeInjectKey:
- mechanism: Mechanism // -> implies key type
Expand Down Expand Up @@ -339,7 +338,7 @@ pub mod request {

WriteCertificate:
- location: Location
- der: Message
- der: LargeMessage

SerdeExtension:
- id: u8
Expand Down Expand Up @@ -429,7 +428,7 @@ pub mod reply {
- entry: Option<DirEntry>

ReadFile:
- data: Message
- data: LargeMessage

Metadata:
- metadata: Option<crate::types::Metadata>
Expand Down Expand Up @@ -490,7 +489,7 @@ pub mod reply {
DeleteCertificate:

ReadCertificate:
- der: Message
- der: LargeMessage

WriteCertificate:
- id: CertId
Expand Down
6 changes: 2 additions & 4 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ pub trait CertificateClient: PollClient {
location: Location,
der: &[u8],
) -> ClientResult<'_, reply::WriteCertificate, Self> {
let der = Message::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
let der = LargeMessage::from_slice(der).map_err(|_| ClientError::DataTooLarge)?;
self.request(request::WriteCertificate { location, der })
}
}
Expand Down Expand Up @@ -650,14 +650,12 @@ pub trait FilesystemClient: PollClient {
&mut self,
location: Location,
path: PathBuf,
data: Message,
user_attribute: Option<UserAttribute>,
data: LargeMessage,
) -> ClientResult<'_, reply::WriteFile, Self> {
self.request(request::WriteFile {
location,
path,
data,
user_attribute,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use littlefs2::consts;
pub type MAX_APPLICATION_NAME_LENGTH = consts::U256;
pub const MAX_LONG_DATA_LENGTH: usize = 1024;
pub const MAX_MESSAGE_LENGTH: usize = 1024;
pub const MAX_LARGE_MESSAGE_LENGTH: usize = 2048;
pub type MAX_OBJECT_HANDLES = consts::U16;
pub type MAX_LABEL_LENGTH = consts::U256;
pub const MAX_MEDIUM_DATA_LENGTH: usize = 256;
Expand Down
12 changes: 6 additions & 6 deletions src/store/certstore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use littlefs2::path::PathBuf;
use crate::{
error::{Error, Result},
store::{self, Store},
types::{CertId, Location, Message},
types::{CertId, LargeMessage, Location},
};

pub struct ClientCertstore<S>
Expand All @@ -18,11 +18,11 @@ where

pub trait Certstore {
fn delete_certificate(&mut self, id: CertId) -> Result<()>;
fn read_certificate(&mut self, id: CertId) -> Result<Message>;
fn read_certificate(&mut self, id: CertId) -> Result<LargeMessage>;
/// TODO: feels a bit heavy-weight to pass in the ClientCounterstore here
/// just to ensure the next global counter ("counter zero") is used, and
/// not something random.
fn write_certificate(&mut self, location: Location, der: &Message) -> Result<CertId>;
fn write_certificate(&mut self, location: Location, der: &[u8]) -> Result<CertId>;
}

impl<S: Store> Certstore for ClientCertstore<S> {
Expand All @@ -36,7 +36,7 @@ impl<S: Store> Certstore for ClientCertstore<S> {
.ok_or(Error::NoSuchKey)
}

fn read_certificate(&mut self, id: CertId) -> Result<Message> {
fn read_certificate(&mut self, id: CertId) -> Result<LargeMessage> {
let path = self.cert_path(id);
let locations = [Location::Internal, Location::External, Location::Volatile];
locations
Expand All @@ -45,10 +45,10 @@ impl<S: Store> Certstore for ClientCertstore<S> {
.ok_or(Error::NoSuchCertificate)
}

fn write_certificate(&mut self, location: Location, der: &Message) -> Result<CertId> {
fn write_certificate(&mut self, location: Location, der: &[u8]) -> Result<CertId> {
let id = CertId::new(&mut self.rng);
let path = self.cert_path(id);
store::store(self.store, location, &path, der.as_slice())?;
store::store(self.store, location, &path, der)?;
Ok(id)
}
}
Expand Down
7 changes: 1 addition & 6 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,7 @@ fn filesystem() {

let data = Bytes::from_slice(&[0; 20]).unwrap();
block!(client
.write_file(
Location::Internal,
PathBuf::from("test_file"),
data.clone(),
None,
)
.write_file(Location::Internal, PathBuf::from("test_file"), data.clone(),)
.expect("no client error"))
.expect("no errors");

Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,7 @@ pub type ShortData = Bytes<MAX_SHORT_DATA_LENGTH>;

pub type Message = Bytes<MAX_MESSAGE_LENGTH>;
pub type SerializedKey = Bytes<MAX_KEY_MATERIAL_LENGTH>;
pub type LargeMessage = Bytes<MAX_LARGE_MESSAGE_LENGTH>;

#[derive(Copy, Clone, Eq, PartialEq, Debug, Serialize, Deserialize)]
pub enum KeySerialization {
Expand Down
4 changes: 2 additions & 2 deletions tests/backends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use trussed::{
error::Error,
platform,
service::{Service, ServiceResources},
types::{Context, CoreContext, Location, Message, PathBuf},
types::{Context, CoreContext, LargeMessage, Location, PathBuf},
virt::{self, Ram},
ClientImplementation,
};
Expand Down Expand Up @@ -61,7 +61,7 @@ impl backend::Backend for TestBackend {
) -> Result<Reply, Error> {
match request {
Request::ReadFile(_) => {
let mut data = Message::new();
let mut data = LargeMessage::new();
data.push(0xff).unwrap();
Ok(Reply::ReadFile(ReadFile { data }))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/virt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn run_test(data: u8) {

// ensure that no other client is messing with our filesystem
while syscall!(client.uptime()).uptime < Duration::from_secs(1) {
syscall!(client.write_file(location, path.clone(), write_data.clone(), None));
syscall!(client.write_file(location, path.clone(), write_data.clone()));
let read_data = syscall!(client.read_file(location, path.clone())).data;
assert_eq!(write_data, read_data);
}
Expand Down

0 comments on commit 27c1196

Please sign in to comment.