1use alloc::format;
2use alloc::string::{String, ToString};
3
4use sentc_crypto_core::Error;
5
6#[derive(Debug)]
7pub enum SdkUtilError
8{
9 Base(Error),
10 JsonToStringFailed,
11 JsonParseFailed(serde_json::Error),
12 JsonParse,
13
14 ServerErr(u32, String),
15
16 ExportingPublicKeyFailed,
17 DecodeSaltFailed,
18 ImportingPrivateKeyFailed,
19 ImportPublicKeyFailed,
20 DecodePublicKeyFailed,
21 AlgNotFound,
22 ImportingKeyFromPemFailed,
23 DecodeRandomValueFailed,
24 ImportSymmetricKeyFailed,
25 ImportingSignKeyFailed,
26 ImportVerifyKeyFailed,
27 ImportAuthMasterKeyFailed,
28 ImportKeyFailed,
29
30 DerivedKeyWrongFormat,
31 InvalidJwt,
32 InvalidJwtFormat,
33
34 DecryptingLoginChallengeFailed,
35
36 #[cfg(any(feature = "rustls", feature = "wasm"))]
37 RequestErr(String),
38 #[cfg(any(feature = "rustls", feature = "wasm"))]
39 ResponseErrText,
40 #[cfg(any(feature = "rustls", feature = "wasm"))]
41 ResponseErrBytes,
42
43 #[cfg(feature = "encryption")]
44 SigFoundNotKey,
45 #[cfg(feature = "encryption")]
46 VerifyFailed,
47 #[cfg(feature = "encryption")]
48 DecodeEncryptedDataFailed,
49 #[cfg(feature = "encryption")]
50 SearchableEncryptionDataNotFound,
51 #[cfg(feature = "encryption")]
52 SearchableEncryptionDataTooLong,
53}
54
55impl From<Error> for SdkUtilError
59{
60 fn from(e: Error) -> Self
61 {
62 SdkUtilError::Base(e)
63 }
64}
65
66impl From<serde_json::Error> for SdkUtilError
67{
68 fn from(value: serde_json::Error) -> Self
69 {
70 Self::JsonParseFailed(value)
71 }
72}
73
74impl From<SdkUtilError> for String
75{
76 fn from(e: SdkUtilError) -> Self
77 {
78 err_to_msg(e)
79 }
80}
81
82pub fn err_to_msg(error: SdkUtilError) -> String
83{
84 match error {
85 SdkUtilError::Base(base_error) => {
86 match base_error {
87 Error::AlgNotFound => out_error("client_1", "The algorithms for this action was not found."),
88
89 Error::DecodePrivateKeyFailed => out_error("client_3", "The private key has a wrong format."),
90 Error::DecryptionFailedCiphertextShort => out_error("client_10", "cipher is too short."),
91 Error::KeyCreationFailed => {
92 out_error(
93 "client_11",
94 "Can't create a key. This normally happened when the used system has no mechanisms to create random numbers",
95 )
96 },
97 Error::EncryptionFailed => out_error("client_12", "Can't encrypt symmetrically."),
98 Error::EncryptionFailedRng => {
99 out_error(
100 "client_13",
101 "Can't create random numbers. This normally happened when the used system has no mechanisms to create random numbers",
102 )
103 },
104 Error::DecryptionFailed => {
105 out_error(
106 "client_14",
107 "Can't decrypt the cipher. This happened when using a wrong key to decrypt",
108 )
109 },
110
111 Error::PwHashFailed => {
112 out_error(
113 "client_20",
114 "Can't hash the password. This happened when using a wrong algorithm or the output is wrong.",
115 )
116 },
117 Error::PwSplitFailedLeft => out_error("client_21", "Can't hash the password. The input is too short"),
118 Error::PwSplitFailedRight => out_error("client_21", "Can't hash the password. The input is too short"),
119 Error::HashAuthKeyFailed => out_error("client_22", "Can't hash the password"),
120
121 Error::KeyDecryptFailed => {
122 out_error(
123 "client_30",
124 "Can't decrypt the key. Maybe a wrong master key was used.",
125 )
126 },
127 Error::KeyDecryptionVerifyFailed => {
128 out_error(
129 "client_31",
130 "Can't verify the key. Maybe the signature or the key a corrupted.",
131 )
132 },
133 Error::SignKeyCreateFailed => out_error("client_40", "Can't create a sign key from given bytes"),
134 Error::InitSignFailed => out_error("client_41", "Can't create a sign"),
135 Error::DataToSignTooShort => out_error("client_42", "This data doesn't contains a sign"),
136 Error::InitVerifyFailed => out_error("client_43", "Can't verify the data"),
137
138 Error::HmacAuthFailedLength => out_error("client_50", "Can't auth the hmac"),
139
140 Error::OpeRangeError => out_error("client_60", "Invalid input range"),
141 Error::OpeStringToLarge => out_error("client_61", "String is too large to process"),
142 Error::OpeHdgInvalidInputs => out_error("client_62", "Invalid inputs"),
143 }
144 },
145 SdkUtilError::AlgNotFound => out_error("client_1", "The algorithms for this action was not found."),
146 SdkUtilError::JsonToStringFailed => out_error("client_100", "Cannot create a string from this object"),
147 SdkUtilError::JsonParse => out_error("client_102", "Cannot create an object from the input string"),
148 SdkUtilError::JsonParseFailed(err) => {
149 format!("{{\"status\": {}, \"error_message\": \"{}\"}}", "client_101", err)
150 },
151 SdkUtilError::ServerErr(code, msg) => {
152 out_error(
153 (String::from("server_") + code.to_string().as_str()).as_str(),
154 msg.as_str(),
155 )
156 },
157
158 SdkUtilError::DerivedKeyWrongFormat => out_error("client_2", "The encrypted key has a wrong format."),
160 SdkUtilError::DecodeRandomValueFailed => out_error("client_5", "Can't decode the client random value from registration"),
161 SdkUtilError::DecodeSaltFailed => out_error("client_4", "The salt has a wrong format"),
163 SdkUtilError::DecodePublicKeyFailed => out_error("client_8", "Can't decode the public key. Maybe the format is wrong"),
164
165 SdkUtilError::ImportingSignKeyFailed => out_error("client_110", "Can't import the sign key"),
167 SdkUtilError::ImportingPrivateKeyFailed => out_error("client_111", "Can't import the private key"),
168 SdkUtilError::ImportSymmetricKeyFailed => out_error("client_112", "Can't import symmetric key"),
169 SdkUtilError::ImportPublicKeyFailed => out_error("client_113", "Can't import public key"),
170 SdkUtilError::ImportVerifyKeyFailed => out_error("client_114", "Can't import verify key"),
171 SdkUtilError::ImportingKeyFromPemFailed => out_error("client_115", "Can't import this key. It has a wrong format"),
172 SdkUtilError::ImportAuthMasterKeyFailed => out_error("client_116", "Can't import auth master key"),
173 SdkUtilError::ImportKeyFailed => out_error("client_109", "Can't import the key"),
174
175 SdkUtilError::ExportingPublicKeyFailed => {
177 out_error(
178 "client_120",
179 "Can't export the public key. It doesn't fit in a pem format",
180 )
181 },
182
183 SdkUtilError::InvalidJwt => out_error("client_1100", "Jwt is invalid"),
184 SdkUtilError::InvalidJwtFormat => out_error("client_1101", "Jwt has a wrong format"),
185
186 SdkUtilError::DecryptingLoginChallengeFailed => out_error("client_1102", "Can't verify login."),
187
188 #[cfg(any(feature = "rustls", feature = "wasm"))]
189 SdkUtilError::RequestErr(e) => out_error("client_1000", format!("Can't send the request: {}", e).as_str()),
190 #[cfg(any(feature = "rustls", feature = "wasm"))]
191 SdkUtilError::ResponseErrText => out_error("client_1002", "Can't decode the response to text"),
192 #[cfg(any(feature = "rustls", feature = "wasm"))]
193 SdkUtilError::ResponseErrBytes => out_error("client_1003", "Can't get bytes from response"),
194
195 #[cfg(feature = "encryption")]
196 SdkUtilError::SigFoundNotKey => {
197 out_error(
198 "client_20",
199 "The verification key can't verify this signature. The signature was signed by another key pair.",
200 )
201 },
202 #[cfg(feature = "encryption")]
203 SdkUtilError::VerifyFailed => out_error("client_22", "The verification failed. A wrong verify key was used"),
204 #[cfg(feature = "encryption")]
205 SdkUtilError::DecodeEncryptedDataFailed => out_error("client_10", "Can't decode the encrypted data"),
206 #[cfg(feature = "encryption")]
207 SdkUtilError::SearchableEncryptionDataTooLong => {
208 out_error(
209 "client_300",
210 "The input data is too long to hash. The maximal length is 200 characters.",
211 )
212 },
213 #[cfg(feature = "encryption")]
214 SdkUtilError::SearchableEncryptionDataNotFound => out_error("client_301", "No data found to hash. Empty Strings are not allowed."),
215 }
216}
217
218pub fn out_error(code: &str, message: &str) -> String
219{
220 format!("{{\"status\": \"{}\", \"error_message\": \"{}\"}}", code, message)
223}