mls_crypto_provider/
error.rs

1// Wire
2// Copyright (C) 2022 Wire Swiss GmbH
3
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see http://www.gnu.org/licenses/.
16
17#[derive(Debug, thiserror::Error)]
18pub enum MlsProviderError {
19    #[error(transparent)]
20    KeystoreError(#[from] core_crypto_keystore::CryptoKeystoreError),
21    #[error("The provided entropy seed has an incorrect length: expected {expected}, found {actual}")]
22    EntropySeedLengthError { actual: usize, expected: usize },
23    #[error("CSPRNG lock is poisoned")]
24    RngLockPoison,
25    #[error("Unable to collect enough randomness.")]
26    UnsufficientEntropy,
27    #[error("An error occured while generating a X509 certificate")]
28    CertificateGenerationError,
29    #[error("This ciphersuite isn't supported as of now")]
30    UnsupportedSignatureScheme,
31    #[error(transparent)]
32    SignatureError(#[from] signature::Error),
33    #[error("{0}")]
34    StringError(String),
35}
36
37#[allow(clippy::from_over_into)]
38impl Into<String> for MlsProviderError {
39    fn into(self) -> String {
40        self.to_string()
41    }
42}
43
44/// Note: You *will* be losing context when cloning the error, because errors should never be `Clone`able,
45/// but OpenMLS traits require it, so...let's do something that makes no sense.
46impl Clone for MlsProviderError {
47    fn clone(&self) -> Self {
48        Self::StringError(self.to_string())
49    }
50}
51
52/// Note: You should never test errors for equality because stacktraces can be different, yet we're
53/// constrained by OpenMLS to do this kind of things. So once again...
54impl PartialEq for MlsProviderError {
55    fn eq(&self, other: &Self) -> bool {
56        match (self, other) {
57            // (MlsProviderError::KeystoreError(kse), MlsProviderError::KeystoreError(kse2)) => kse == kse2,
58            (
59                MlsProviderError::EntropySeedLengthError { expected, actual },
60                MlsProviderError::EntropySeedLengthError {
61                    expected: expected2,
62                    actual: actual2,
63                },
64            ) => expected == expected2 && actual == actual2,
65            (MlsProviderError::StringError(s), MlsProviderError::StringError(s2)) => s == s2,
66            (MlsProviderError::RngLockPoison, MlsProviderError::RngLockPoison) => true,
67            (MlsProviderError::UnsufficientEntropy, MlsProviderError::UnsufficientEntropy) => true,
68            (MlsProviderError::CertificateGenerationError, MlsProviderError::CertificateGenerationError) => true,
69            (MlsProviderError::UnsupportedSignatureScheme, MlsProviderError::UnsupportedSignatureScheme) => true,
70            _ => false,
71        }
72    }
73}
74
75pub type MlsProviderResult<T> = Result<T, MlsProviderError>;