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
use alloc::string::String;
use core::fmt::Debug;
#[cfg(not(any(feature = "std", test)))]
use core::fmt::{self, Display, Formatter};
#[cfg(feature = "datasize")]
use datasize::DataSize;
use ed25519_dalek::ed25519::Error as SignatureError;
#[cfg(any(feature = "std", test))]
use pem::PemError;
#[cfg(any(feature = "std", test))]
use thiserror::Error;
#[cfg(any(feature = "std", test))]
use crate::file_utils::{ReadFileError, WriteFileError};
#[derive(Debug)]
#[cfg_attr(feature = "datasize", derive(DataSize))]
#[cfg_attr(any(feature = "std", test), derive(Error))]
pub enum Error {
#[cfg_attr(any(feature = "std", test), error("asymmetric key error: {0}"))]
AsymmetricKey(String),
#[cfg_attr(feature = "datasize", data_size(skip))]
#[cfg_attr(any(feature = "std", test), error("parsing from hex: {0}"))]
FromHex(base16::DecodeError),
#[cfg_attr(feature = "datasize", data_size(skip))]
#[cfg_attr(any(feature = "std", test), error("decoding error: {0}"))]
FromBase64(base64::DecodeError),
#[cfg_attr(feature = "datasize", data_size(skip))]
#[cfg_attr(any(feature = "std", test), error("error in signature"))]
SignatureError(SignatureError),
#[cfg_attr(
any(feature = "std", test),
error("invalid operation on system key: {0}")
)]
System(String),
}
#[cfg(not(any(feature = "std", test)))]
impl Display for Error {
fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
Debug::fmt(self, formatter)
}
}
impl From<base16::DecodeError> for Error {
fn from(error: base16::DecodeError) -> Self {
Error::FromHex(error)
}
}
impl From<SignatureError> for Error {
fn from(error: SignatureError) -> Self {
Error::SignatureError(error)
}
}
#[cfg(any(feature = "std", test))]
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum ErrorExt {
#[error("crypto error: {0:?}")]
CryptoError(#[from] Error),
#[error("secret key load failed: {0}")]
SecretKeyLoad(ReadFileError),
#[error("public key load failed: {0}")]
PublicKeyLoad(ReadFileError),
#[error("secret key save failed: {0}")]
SecretKeySave(WriteFileError),
#[error("public key save failed: {0}")]
PublicKeySave(WriteFileError),
#[error("pem error: {0}")]
FromPem(String),
#[error("der error: {0}")]
FromDer(#[from] derp::Error),
#[error("failed to get random bytes: {0}")]
GetRandomBytes(#[from] getrandom::Error),
}
#[cfg(any(feature = "std", test))]
impl From<PemError> for ErrorExt {
fn from(error: PemError) -> Self {
ErrorExt::FromPem(error.to_string())
}
}