Return NotFound when data object in not avalaible

This commit is contained in:
Sosthène Guédon
2022-12-16 11:34:08 +01:00
parent 43b1a99803
commit 6bc3ca73bb
2 changed files with 44 additions and 32 deletions
+31 -28
View File
@@ -123,8 +123,8 @@ where
Command::ChangeReference(change_reference) => {
self.load()?.change_reference(change_reference)
}
Command::GetData(container) => self.get_data(container, reply),
Command::PutData(put_data) => self.put_data(put_data),
Command::GetData(container) => self.load()?.get_data(container, reply),
Command::PutData(put_data) => self.load()?.put_data(put_data),
Command::Select(_aid) => self.select(reply),
Command::GeneralAuthenticate(authenticate) => {
self.load()?
@@ -203,32 +203,6 @@ where
}
Ok(())
}
fn get_data<const R: usize>(
&mut self,
container: Container,
mut reply: Reply<'_, R>,
) -> Result {
// TODO: check security status, else return Status::SecurityStatusNotSatisfied
use state::ContainerStorage;
reply.expand(&ContainerStorage(container).load(&mut self.trussed)?)
}
fn put_data(&mut self, put_data: PutData<'_>) -> Result {
// TODO: check security status, else return Status::SecurityStatusNotSatisfied
let (container, data) = match put_data {
PutData::Any(container, data) => (container, data),
PutData::BitGroupTemplate(data) => {
(Container::BiometricInformationTemplatesGroupTemplate, data)
}
PutData::DiscoveryObject(data) => (Container::DiscoveryObject, data),
};
use state::ContainerStorage;
ContainerStorage(container).save(&mut self.trussed, data)
}
}
impl<'a, T: trussed::Client + trussed::client::Ed255> LoadedAuthenticator<'a, T> {
@@ -821,4 +795,33 @@ impl<'a, T: trussed::Client + trussed::client::Ed255> LoadedAuthenticator<'a, T>
Ok(())
}
fn get_data<const R: usize>(
&mut self,
container: Container,
mut reply: Reply<'_, R>,
) -> Result {
// TODO: check security status, else return Status::SecurityStatusNotSatisfied
use state::ContainerStorage;
match ContainerStorage(container).load(self.trussed)? {
Some(data) => reply.expand(&data),
None => Err(Status::NotFound),
}
}
fn put_data(&mut self, put_data: PutData<'_>) -> Result {
// TODO: check security status, else return Status::SecurityStatusNotSatisfied
let (container, data) = match put_data {
PutData::Any(container, data) => (container, data),
PutData::BitGroupTemplate(data) => {
(Container::BiometricInformationTemplatesGroupTemplate, data)
}
PutData::DiscoveryObject(data) => (Container::DiscoveryObject, data),
};
use state::ContainerStorage;
ContainerStorage(container).save(self.trussed, data)
}
}
+13 -4
View File
@@ -546,16 +546,25 @@ impl ContainerStorage {
})
}
fn default(self) -> &'static [u8] {
todo!()
fn default(self) -> Option<Vec<u8, MAX_MESSAGE_LENGTH>> {
match self.0 {
Container::CardHolderUniqueIdentifier => panic!("CHUID should alway be set"),
Container::CardCapabilityContainer => Some(
crate::piv_types::CardCapabilityContainer::default()
.to_heapless_vec()
.unwrap(),
),
Container::DiscoveryObject => Some(Vec::from_slice(&DISCOVERY_OBJECT).unwrap()),
_ => None,
}
}
pub fn load(
self,
client: &mut impl trussed::Client,
) -> Result<Bytes<MAX_MESSAGE_LENGTH>, Status> {
) -> Result<Option<Bytes<MAX_MESSAGE_LENGTH>>, Status> {
load_if_exists(client, Location::Internal, &self.path())
.map(|data| data.unwrap_or_else(|| Bytes::from_slice(self.default()).unwrap()))
.map(|data| data.or_else(|| self.default().map(Bytes::from)))
}
pub fn save(self, client: &mut impl trussed::Client, bytes: &[u8]) -> Result<(), Status> {