0
0
mirror of https://github.com/signalapp/libsignal.git synced 2024-09-20 03:52:17 +02:00

Derive std::error::Error for more types

Also make use of displaydoc::Display and generated From impls in more places.
This commit is contained in:
Alex Konradi 2024-04-17 10:37:19 -04:00 committed by GitHub
parent bbeb0bb85e
commit fcdd48bb54
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
13 changed files with 28 additions and 75 deletions

4
Cargo.lock generated
View File

@ -278,6 +278,7 @@ dependencies = [
"strum",
"subtle",
"test-case",
"thiserror",
"uuid",
"x25519-dalek",
]
@ -3516,6 +3517,7 @@ dependencies = [
"rand_core",
"sha2",
"static_assertions",
"thiserror",
]
[[package]]
@ -4625,6 +4627,7 @@ dependencies = [
"serde",
"sha2",
"subtle",
"thiserror",
]
[[package]]
@ -4653,6 +4656,7 @@ dependencies = [
"signal-crypto",
"subtle",
"test-case",
"thiserror",
"uuid",
"zkcredential",
]

View File

@ -35,6 +35,7 @@ snow = { version = "0.9.5", default-features = false }
static_assertions = "1.1"
strum = { version = "0.26", features = ["derive"] }
subtle = "2.5"
thiserror = "1.0.57"
uuid = "1.1.2"
x25519-dalek = "2.0.0"

View File

@ -10,8 +10,6 @@
//! with [ClientConnection::send]. Likewise a single received message consisting internally
//! of one or more noise transport messages can be decrypted with [ClientConnection::recv]
use std::fmt;
pub const NOISE_PATTERN: &str = "Noise_NK_25519_ChaChaPoly_SHA256";
pub(crate) const NOISE_HANDSHAKE_OVERHEAD: usize = 64;
@ -29,24 +27,10 @@ pub struct ClientConnection {
/// Result type for client connection.
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub enum Error {
/// Error in noise protocol.
NoiseError(snow::Error),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::NoiseError(n) => write!(f, "Noise error ({})", n),
}
}
}
impl From<snow::Error> for Error {
fn from(e: snow::Error) -> Self {
Error::NoiseError(e)
}
/// Noise error ({0})
NoiseError(#[from] snow::Error),
}
fn ceil_div(total: usize, chunk_size: usize) -> usize {

View File

@ -15,19 +15,13 @@ use prost::Message;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
/// Failure to attest remote enclave.
#[error("{message}")]
pub struct AttestationError {
message: String,
}
impl std::fmt::Display for AttestationError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.message.fmt(f)
}
}
impl std::error::Error for AttestationError {}
impl From<dcap::Error> for AttestationError {
fn from(e: dcap::Error) -> Self {
Self {
@ -37,38 +31,20 @@ impl From<dcap::Error> for AttestationError {
}
/// Error types for an enclave noise session.
#[derive(Display, Debug)]
#[derive(Display, Debug, thiserror::Error)]
pub enum Error {
/// failure to attest remote enclave: {0:?}
AttestationError(AttestationError),
AttestationError(#[from] AttestationError),
/// failure to communicate on established Noise channel to the enclave: {0}
NoiseError(client_connection::Error),
NoiseError(#[from] client_connection::Error),
/// failure to complete Noise handshake to the enclave: {0}
NoiseHandshakeError(snow::Error),
NoiseHandshakeError(#[from] snow::Error),
/// attestation data invalid: {reason}
AttestationDataError { reason: String },
/// invalid bridge state
InvalidBridgeStateError,
}
impl From<snow::Error> for Error {
fn from(e: snow::Error) -> Self {
Error::NoiseHandshakeError(e)
}
}
impl From<AttestationError> for Error {
fn from(err: AttestationError) -> Error {
Error::AttestationError(err)
}
}
impl From<client_connection::Error> for Error {
fn from(err: client_connection::Error) -> Self {
Error::NoiseError(err)
}
}
impl From<prost::DecodeError> for Error {
fn from(err: prost::DecodeError) -> Self {
Error::AttestationDataError {

View File

@ -9,18 +9,17 @@
#![warn(missing_docs)]
use log::*;
use std::convert::From;
use std::fmt;
use crate::{client_connection, snow_resolver};
/// Error types for HSM enclave.
#[derive(Debug)]
#[derive(Debug, thiserror::Error)]
pub enum Error {
/// Failure to connect to a trusted HSM.
HSMCommunicationError(client_connection::Error),
HSMCommunicationError(#[from] client_connection::Error),
/// Failure to handshake to trusted HSM.
HSMHandshakeError(snow::Error),
HSMHandshakeError(#[from] snow::Error),
/// Failure to connect to trusted code on the given HSM.
TrustedCodeError,
/// Invalid public key provided (used in bridging)
@ -59,18 +58,6 @@ impl fmt::Display for Error {
}
}
impl From<client_connection::Error> for Error {
fn from(e: client_connection::Error) -> Self {
Error::HSMCommunicationError(e)
}
}
impl From<snow::Error> for Error {
fn from(e: snow::Error) -> Self {
Error::HSMHandshakeError(e)
}
}
/// Wraps a connection handshake to an HSM-resident enclave.
///
/// ```pseudocode

View File

@ -68,7 +68,7 @@ pub fn new_handshake(
Ok(handshake)
}
#[derive(Debug, displaydoc::Display, PartialEq, Eq)]
#[derive(Debug, displaydoc::Display, thiserror::Error, PartialEq, Eq)]
pub enum NitroError {
/// Invalid CBOR
InvalidCbor,
@ -90,8 +90,6 @@ pub enum NitroError {
InvalidUserData,
}
impl std::error::Error for NitroError {}
impl From<ciborium::de::Error<std::io::Error>> for NitroError {
fn from(_err: ciborium::de::Error<std::io::Error>) -> NitroError {
NitroError::InvalidCbor

View File

@ -18,6 +18,7 @@ hkdf = "0.12"
rand_core = { version = "0.6", features = ["getrandom"] }
sha2 = "0.10"
static_assertions = "1.1"
thiserror = "1.0.57"
[dev-dependencies]
hex-literal = "0.4.1"

View File

@ -4,7 +4,7 @@
//
/// Error types for pin operations
#[derive(displaydoc::Display, Debug, Clone, Eq, PartialEq)]
#[derive(displaydoc::Display, thiserror::Error, Debug, Clone, Eq, PartialEq)]
pub enum Error {
/// Argon2 hashing error: {0}
Argon2Error(argon2::Error),

View File

@ -31,11 +31,11 @@ pub enum UsernameError {
DiscriminatorTooLarge,
}
#[derive(displaydoc::Display, Debug)]
#[derive(displaydoc::Display, Debug, thiserror::Error)]
/// Username could not be verified
pub struct ProofVerificationFailure;
#[derive(displaydoc::Display, Debug)]
#[derive(displaydoc::Display, Debug, thiserror::Error)]
pub enum UsernameLinkError {
/// The combined length of all input data is too long
InputDataTooLong,

View File

@ -22,6 +22,7 @@ partial-default = { version = "0.1.0", features = ["derive"] }
serde = { version = "1.0.106", features = ["derive"] }
sha2 = "0.10"
subtle = "2.3"
thiserror = "1.0.57"
# If rayon is enabled, certain operations will use rayon's thread pool.
rayon = { version = "1.8.0", optional = true }

View File

@ -39,7 +39,7 @@
#![warn(missing_docs)]
/// A zkcredential operation failed to verify.
#[derive(Debug, displaydoc::Display)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
pub struct VerificationFailure;
/// A reasonable size of entropy to request for operations.

View File

@ -33,6 +33,7 @@ rayon = "1.8.0"
serde = { version = "1.0.106", features = ["derive"] }
sha2 = "0.10.0"
subtle = "2.3"
thiserror = "1.0.57"
uuid = "1.1.2"
# For generation

View File

@ -5,11 +5,11 @@
use zkcredential::VerificationFailure;
#[derive(Debug, displaydoc::Display)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
/// Verification failure in zkgroup
pub struct ZkGroupVerificationFailure;
#[derive(Debug, displaydoc::Display)]
#[derive(Debug, thiserror::Error, displaydoc::Display)]
/// Deserialization failure in zkgroup
pub struct ZkGroupDeserializationFailure;