From f97475e1adae3f258107fa3cb1b65842f1d2cc07 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 19 Dec 2024 15:43:58 +0100 Subject: [PATCH] core: Make more types non-exhaustive MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch marks more types as non-exhaustive that could be extended in the future and that don’t need to be matched exhaustively: - KeySerialization - SignatureSerialization - consent::Error The only exhaustive types that could be realistically be extended in the future are now the request and reply structs. But marking these as non-exhaustive would make it very complex for the backends to implement these syscalls. We can try to find a solution for that when we think about alternative syscall implementations, e. g. using a builder pattern. --- CHANGELOG.md | 2 +- core/src/client.rs | 1 + core/src/types.rs | 5 ++++- src/mechanisms/p256.rs | 6 ++++++ src/mechanisms/p384.rs | 6 ++++++ src/mechanisms/p521.rs | 6 ++++++ 6 files changed, 24 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4373c9e3987..3963fe820db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - As a consequence the type `pipe::TrussedInterchange` becomes a const`pipe::TRUSSED_INTERCHANGE` - Updated `littlefs2` to 0.4.0. - Made `Request`, `Reply`, `Error`, `Context`, `CoreContext`, `Mechanism`, - `ui::Status` non-exhaustive. + `KeySerialization`, `SignatureSerialization`, `consent::Error`, `ui::Status` non-exhaustive. - Made `postcard_deserialize`, `postcard_serialize` and `postcard_serialize_bytes` private. - Changed `&PathBuf` to `&Path` where possible. diff --git a/core/src/client.rs b/core/src/client.rs index 662552818cd..8f8ca132dd4 100644 --- a/core/src/client.rs +++ b/core/src/client.rs @@ -39,6 +39,7 @@ pub use ui::UiClient; // to be fair, this is a programmer error, // and could also just panic #[derive(Copy, Clone, Debug)] +#[non_exhaustive] pub enum ClientError { Full, Pending, diff --git a/core/src/types.rs b/core/src/types.rs index 409eb906522..14071b3a250 100644 --- a/core/src/types.rs +++ b/core/src/types.rs @@ -27,6 +27,7 @@ pub mod consent { } #[derive(Copy, Clone, Eq, PartialEq, Debug)] + #[non_exhaustive] pub enum Error { FailedToInterrupt, Interrupted, @@ -417,8 +418,8 @@ impl NotBefore { /// /// This enum does not provide access to the trait features. It is only intended for backends to /// use in constant assertions to ensure that the correct features are enabled. -#[non_exhaustive] #[derive(Copy, Clone, Debug, Eq, PartialEq)] +#[non_exhaustive] pub enum Client { AttestationClient, CertificateClient, @@ -611,6 +612,7 @@ impl Mechanism { } #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[non_exhaustive] pub enum KeySerialization { // Asn1Der, Cose, @@ -627,6 +629,7 @@ pub enum KeySerialization { } #[derive(Copy, Clone, Eq, PartialEq, Debug)] +#[non_exhaustive] pub enum SignatureSerialization { Asn1Der, // Cose, diff --git a/src/mechanisms/p256.rs b/src/mechanisms/p256.rs index 1a4f667b5b4..b2ea0813fb7 100644 --- a/src/mechanisms/p256.rs +++ b/src/mechanisms/p256.rs @@ -277,6 +277,9 @@ impl Sign for super::P256 { SignatureSerialization::Raw => { Signature::from_slice(&signature.to_untagged_bytes()).unwrap() } + _ => { + return Err(Error::InvalidSerializationFormat); + } }; // return signature @@ -304,6 +307,9 @@ impl Sign for super::P256Prehashed { SignatureSerialization::Raw => { Signature::from_slice(&signature.to_untagged_bytes()).unwrap() } + _ => { + return Err(Error::InvalidSerializationFormat); + } }; // return signature diff --git a/src/mechanisms/p384.rs b/src/mechanisms/p384.rs index 67d2fdda409..775cb3cf35e 100644 --- a/src/mechanisms/p384.rs +++ b/src/mechanisms/p384.rs @@ -209,6 +209,9 @@ impl Sign for P384 { Signature::from_slice(der.as_bytes()).unwrap() } SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(), + _ => { + return Err(Error::InvalidSerializationFormat); + } }; // return signature @@ -235,6 +238,9 @@ impl Sign for P384Prehashed { Signature::from_slice(der.as_bytes()).unwrap() } SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(), + _ => { + return Err(Error::InvalidSerializationFormat); + } }; // return signature diff --git a/src/mechanisms/p521.rs b/src/mechanisms/p521.rs index 224e793d380..9f45fd84bae 100644 --- a/src/mechanisms/p521.rs +++ b/src/mechanisms/p521.rs @@ -212,6 +212,9 @@ impl Sign for P521 { Signature::from_slice(der.as_bytes()).unwrap() } SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(), + _ => { + return Err(Error::InvalidSerializationFormat); + } }; // return signature @@ -238,6 +241,9 @@ impl Sign for P521Prehashed { Signature::from_slice(der.as_bytes()).unwrap() } SignatureSerialization::Raw => Signature::from_slice(&signature.to_bytes()).unwrap(), + _ => { + return Err(Error::InvalidSerializationFormat); + } }; // return signature