From 124d334166848a729229dc1f3bf96ebdcecd4cb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sosth=C3=A8ne=20Gu=C3=A9don?= Date: Thu, 10 Nov 2022 15:28:57 +0100 Subject: [PATCH] Check that command algorithm corresponds with state --- src/commands.rs | 2 +- src/container.rs | 11 +++++++++++ src/lib.rs | 26 ++++++++++++++------------ 3 files changed, 26 insertions(+), 13 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 7aa0bdd..0b8cdde 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -66,7 +66,7 @@ pub enum Command<'l> { #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct GeneralAuthenticate { - algorithm: piv_types::Algorithms, + pub algorithm: piv_types::Algorithms, key_reference: AuthenticateKeyReference, } diff --git a/src/container.rs b/src/container.rs index 8f43553..7206a96 100644 --- a/src/container.rs +++ b/src/container.rs @@ -45,6 +45,17 @@ macro_rules! enum_subset { } } + impl PartialEq<$sup> for $name { + fn eq(&self, other: &$sup) -> bool { + match (self,other) { + $( + | ($name::$var, $sup::$var) + )* => true, + _ => false + } + } + } + impl TryFrom for $name { type Error = ::iso7816::Status; fn try_from(tag: u8) -> ::core::result::Result { diff --git a/src/lib.rs b/src/lib.rs index 88f54ae..e1a3e88 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -474,7 +474,7 @@ impl<'a, T: trussed::Client + trussed::client::Ed255> LoadedAuthenticator<'a, T> pub fn request_for_response( &mut self, - _auth: GeneralAuthenticate, + auth: GeneralAuthenticate, data: derp::Input<'_>, _reply: &mut Data, ) -> Result { @@ -484,6 +484,11 @@ impl<'a, T: trussed::Client + trussed::client::Ed255> LoadedAuthenticator<'a, T> warn!("Bad response length"); return Err(Status::IncorrectDataParameter); } + if alg != auth.algorithm { + warn!("Bad algorithm"); + return Err(Status::IncorrectP1OrP2Parameter); + } + let Some(CommandCache::AuthenticateChallenge(plaintext)) = self.state.runtime.command_cache.take() else { warn!("Request for response without cached challenge"); return Err(Status::ConditionsOfUseNotSatisfied); @@ -518,24 +523,21 @@ impl<'a, T: trussed::Client + trussed::client::Ed255> LoadedAuthenticator<'a, T> pub fn request_for_challenge( &mut self, - _auth: GeneralAuthenticate, + auth: GeneralAuthenticate, data: derp::Input<'_>, reply: &mut Data, ) -> Result { - if data.len() != 0 { + let alg = self.state.persistent.keys.management_key.alg; + if !data.is_empty() { warn!("Request for challenge with non empty data"); return Err(Status::IncorrectDataParameter); } + if alg != auth.algorithm { + warn!("Bad algorithm"); + return Err(Status::IncorrectP1OrP2Parameter); + } info!("Request for challenge "); - let challenge = syscall!(self.trussed.random_bytes( - self.state - .persistent - .keys - .management_key - .alg - .challenge_length() - )) - .bytes; + let challenge = syscall!(self.trussed.random_bytes(alg.challenge_length())).bytes; self.state.runtime.command_cache = Some(CommandCache::AuthenticateChallenge( Bytes::from_slice(&challenge).unwrap(), ));