1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use crypto_box as lib_crypto_box;

/// Keystore Error Type.
#[derive(Debug, thiserror::Error)]
pub enum LairError {
    /// An error generated from the GhostActor system.
    #[error("LairError: {0}")]
    GhostError(#[from] ghost_actor::GhostError),

    /// Trying to start up Lair process, but a pidfile/process already exists
    #[error("Lair pidfile/process already exists")]
    ProcessAlreadyExists,

    /// Failure to establish client connection to Lair IPC.
    #[error("IpcClientConnectError: {0} {1}")]
    IpcClientConnectError(String, Box<dyn std::error::Error + Send + Sync>),

    /// A public key was provided (e.g. for signing) that cannot be found in the keystore
    #[error("Public key not found")]
    PubKeyNotFound,

    /// Error during aead encryption, likely bad data.
    #[error("Aead error: {0}")]
    Aead(String),

    /// Error adding padding to encrypt data.
    #[error("Block pad error: {0}")]
    BlockPad(String),

    /// Error removing padding from decrypted data.
    #[error("Block unpad error: {0}")]
    BlockUnpad(String),

    /// Nonce byte lengths did not line up internally. Always very bad.
    #[error("CryptoBox nonce bad length")]
    CryptoBoxNonceLength,

    /// X25519 pub key lengths did not line up internally. Always very bad.
    #[error("X25519 pub key bad length")]
    X25519PubKeyLength,

    /// X25519 priv key lengths did not line up internally. Always very bad.
    #[error("X25519 priv key bad length")]
    X25519PrivKeyLength,

    /// Unspecified Internal error.
    #[error(transparent)]
    Other(Box<dyn std::error::Error + Send + Sync>),
}

impl From<lib_crypto_box::aead::Error> for LairError {
    fn from(aead_error: lib_crypto_box::aead::Error) -> Self {
        Self::Aead(aead_error.to_string())
    }
}

impl From<block_padding::PadError> for LairError {
    fn from(error: block_padding::PadError) -> Self {
        Self::BlockPad(format!("{:?}", error))
    }
}

impl From<block_padding::UnpadError> for LairError {
    fn from(error: block_padding::UnpadError) -> Self {
        Self::BlockUnpad(format!("{:?}", error))
    }
}

impl From<std::io::Error> for LairError {
    fn from(error: std::io::Error) -> Self {
        Self::other(error)
    }
}

impl From<sodoken::SodokenError> for LairError {
    fn from(error: sodoken::SodokenError) -> Self {
        Self::other(error)
    }
}

impl LairError {
    /// Build an "Other" type LairError.
    pub fn other(
        e: impl Into<Box<dyn std::error::Error + Send + Sync>>,
    ) -> Self {
        LairError::Other(e.into())
    }
}

impl From<String> for LairError {
    fn from(s: String) -> Self {
        #[derive(Debug, thiserror::Error)]
        struct OtherError(String);
        impl std::fmt::Display for OtherError {
            fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                write!(f, "{}", self.0)
            }
        }

        LairError::other(OtherError(s))
    }
}

impl From<&str> for LairError {
    fn from(s: &str) -> Self {
        s.to_string().into()
    }
}

impl From<LairError> for () {
    fn from(_: LairError) {}
}

/// Lair Result Type.
pub type LairResult<T> = Result<T, LairError>;