/src/Botan-3.4.0/build/include/public/botan/exceptn.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Exceptions |
3 | | * (C) 1999-2009,2018 Jack Lloyd |
4 | | * |
5 | | * Botan is released under the Simplified BSD License (see license.txt) |
6 | | */ |
7 | | |
8 | | #ifndef BOTAN_EXCEPTION_H_ |
9 | | #define BOTAN_EXCEPTION_H_ |
10 | | |
11 | | #include <botan/types.h> |
12 | | #include <exception> |
13 | | #include <string> |
14 | | |
15 | | namespace Botan { |
16 | | |
17 | | /** |
18 | | * Different types of errors that might occur |
19 | | */ |
20 | | enum class ErrorType { |
21 | | /** Some unknown error */ |
22 | | Unknown = 1, |
23 | | /** An error while calling a system interface */ |
24 | | SystemError, |
25 | | /** An operation seems valid, but not supported by the current version */ |
26 | | NotImplemented, |
27 | | /** Memory allocation failure */ |
28 | | OutOfMemory, |
29 | | /** An internal error occurred */ |
30 | | InternalError, |
31 | | /** An I/O error occurred */ |
32 | | IoError, |
33 | | |
34 | | /** Invalid object state */ |
35 | | InvalidObjectState = 100, |
36 | | /** A key was not set on an object when this is required */ |
37 | | KeyNotSet, |
38 | | /** The application provided an argument which is invalid */ |
39 | | InvalidArgument, |
40 | | /** A key with invalid length was provided */ |
41 | | InvalidKeyLength, |
42 | | /** A nonce with invalid length was provided */ |
43 | | InvalidNonceLength, |
44 | | /** An object type was requested but cannot be found */ |
45 | | LookupError, |
46 | | /** Encoding a message or datum failed */ |
47 | | EncodingFailure, |
48 | | /** Decoding a message or datum failed */ |
49 | | DecodingFailure, |
50 | | /** A TLS error (error_code will be the alert type) */ |
51 | | TLSError, |
52 | | /** An error during an HTTP operation */ |
53 | | HttpError, |
54 | | /** A message with an invalid authentication tag was detected */ |
55 | | InvalidTag, |
56 | | /** An error during Roughtime validation */ |
57 | | RoughtimeError, |
58 | | |
59 | | /** An error when interacting with CommonCrypto API */ |
60 | | CommonCryptoError = 201, |
61 | | /** An error when interacting with a PKCS11 device */ |
62 | | Pkcs11Error, |
63 | | /** An error when interacting with a TPM device */ |
64 | | TPMError, |
65 | | /** An error when interacting with a database */ |
66 | | DatabaseError, |
67 | | |
68 | | /** An error when interacting with zlib */ |
69 | | ZlibError = 300, |
70 | | /** An error when interacting with bzip2 */ |
71 | | Bzip2Error, |
72 | | /** An error when interacting with lzma */ |
73 | | LzmaError, |
74 | | |
75 | | }; |
76 | | |
77 | | //! \brief Convert an ErrorType to string |
78 | | std::string BOTAN_PUBLIC_API(2, 11) to_string(ErrorType type); |
79 | | |
80 | | /** |
81 | | * Base class for all exceptions thrown by the library |
82 | | */ |
83 | | class BOTAN_PUBLIC_API(2, 0) Exception : public std::exception { |
84 | | public: |
85 | | /** |
86 | | * Return a descriptive string which is hopefully comprehensible to |
87 | | * a developer. It will likely not be useful for an end user. |
88 | | * |
89 | | * The string has no particular format, and the content of exception |
90 | | * messages may change from release to release. Thus the main use of this |
91 | | * function is for logging or debugging. |
92 | | */ |
93 | 3.25k | const char* what() const noexcept override { return m_msg.c_str(); } |
94 | | |
95 | | /** |
96 | | * Return the "type" of error which occurred. |
97 | | */ |
98 | 0 | virtual ErrorType error_type() const noexcept { return ErrorType::Unknown; } |
99 | | |
100 | | /** |
101 | | * Return an error code associated with this exception, or otherwise 0. |
102 | | * |
103 | | * The domain of this error varies depending on the source, for example on |
104 | | * POSIX systems it might be errno, while on a Windows system it might be |
105 | | * the result of GetLastError or WSAGetLastError. |
106 | | */ |
107 | 0 | virtual int error_code() const noexcept { return 0; } |
108 | | |
109 | | /** |
110 | | * Avoid throwing base Exception, use a subclass |
111 | | */ |
112 | | explicit Exception(std::string_view msg); |
113 | | |
114 | | /** |
115 | | * Avoid throwing base Exception, use a subclass |
116 | | */ |
117 | | Exception(const char* prefix, std::string_view msg); |
118 | | |
119 | | /** |
120 | | * Avoid throwing base Exception, use a subclass |
121 | | */ |
122 | | Exception(std::string_view msg, const std::exception& e); |
123 | | |
124 | | private: |
125 | | std::string m_msg; |
126 | | }; |
127 | | |
128 | | /** |
129 | | * An invalid argument was provided to an API call. |
130 | | */ |
131 | | class BOTAN_PUBLIC_API(2, 0) Invalid_Argument : public Exception { |
132 | | public: |
133 | | explicit Invalid_Argument(std::string_view msg); |
134 | | |
135 | | explicit Invalid_Argument(std::string_view msg, std::string_view where); |
136 | | |
137 | | Invalid_Argument(std::string_view msg, const std::exception& e); |
138 | | |
139 | 173 | ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; } |
140 | | }; |
141 | | |
142 | | /** |
143 | | * An invalid/unknown field name was passed to Public_Key::get_int_field |
144 | | */ |
145 | | class BOTAN_PUBLIC_API(3, 0) Unknown_PK_Field_Name final : public Invalid_Argument { |
146 | | public: |
147 | | Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name); |
148 | | }; |
149 | | |
150 | | /** |
151 | | * An invalid key length was used |
152 | | */ |
153 | | class BOTAN_PUBLIC_API(2, 0) Invalid_Key_Length final : public Invalid_Argument { |
154 | | public: |
155 | | Invalid_Key_Length(std::string_view name, size_t length); |
156 | | |
157 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; } |
158 | | }; |
159 | | |
160 | | /** |
161 | | * An invalid nonce length was used |
162 | | */ |
163 | | class BOTAN_PUBLIC_API(2, 0) Invalid_IV_Length final : public Invalid_Argument { |
164 | | public: |
165 | | Invalid_IV_Length(std::string_view mode, size_t bad_len); |
166 | | |
167 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; } |
168 | | }; |
169 | | |
170 | | /** |
171 | | * Invalid_Algorithm_Name Exception |
172 | | */ |
173 | | class BOTAN_PUBLIC_API(2, 0) Invalid_Algorithm_Name final : public Invalid_Argument { |
174 | | public: |
175 | | explicit Invalid_Algorithm_Name(std::string_view name); |
176 | | }; |
177 | | |
178 | | /** |
179 | | * Encoding_Error Exception |
180 | | */ |
181 | | class BOTAN_PUBLIC_API(2, 0) Encoding_Error final : public Exception { |
182 | | public: |
183 | | explicit Encoding_Error(std::string_view name); |
184 | | |
185 | 0 | ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; } |
186 | | }; |
187 | | |
188 | | /** |
189 | | * A decoding error occurred. |
190 | | */ |
191 | | class BOTAN_PUBLIC_API(2, 0) Decoding_Error : public Exception { |
192 | | public: |
193 | | explicit Decoding_Error(std::string_view name); |
194 | | |
195 | | Decoding_Error(std::string_view category, std::string_view err); |
196 | | |
197 | | Decoding_Error(std::string_view msg, const std::exception& e); |
198 | | |
199 | 3.08k | ErrorType error_type() const noexcept override { return ErrorType::DecodingFailure; } |
200 | | }; |
201 | | |
202 | | /** |
203 | | * Invalid state was encountered. A request was made on an object while the |
204 | | * object was in a state where the operation cannot be performed. |
205 | | */ |
206 | | class BOTAN_PUBLIC_API(2, 0) Invalid_State : public Exception { |
207 | | public: |
208 | 0 | explicit Invalid_State(std::string_view err) : Exception(err) {} |
209 | | |
210 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidObjectState; } |
211 | | }; |
212 | | |
213 | | /** |
214 | | * A PRNG was called on to produce output while still unseeded |
215 | | */ |
216 | | class BOTAN_PUBLIC_API(2, 0) PRNG_Unseeded final : public Invalid_State { |
217 | | public: |
218 | | explicit PRNG_Unseeded(std::string_view algo); |
219 | | }; |
220 | | |
221 | | /** |
222 | | * The key was not set on an object. This occurs with symmetric objects where |
223 | | * an operation which requires the key is called prior to set_key being called. |
224 | | */ |
225 | | class BOTAN_PUBLIC_API(2, 4) Key_Not_Set : public Invalid_State { |
226 | | public: |
227 | | explicit Key_Not_Set(std::string_view algo); |
228 | | |
229 | 0 | ErrorType error_type() const noexcept override { return ErrorType::KeyNotSet; } |
230 | | }; |
231 | | |
232 | | /** |
233 | | * A request was made for some kind of object which could not be located |
234 | | */ |
235 | | class BOTAN_PUBLIC_API(2, 0) Lookup_Error : public Exception { |
236 | | public: |
237 | 0 | explicit Lookup_Error(std::string_view err) : Exception(err) {} |
238 | | |
239 | | Lookup_Error(std::string_view type, std::string_view algo, std::string_view provider = ""); |
240 | | |
241 | 0 | ErrorType error_type() const noexcept override { return ErrorType::LookupError; } |
242 | | }; |
243 | | |
244 | | /** |
245 | | * Algorithm_Not_Found Exception |
246 | | * |
247 | | * @warning This exception type will be removed in the future. Instead |
248 | | * just catch Lookup_Error. |
249 | | */ |
250 | | class BOTAN_PUBLIC_API(2, 0) Algorithm_Not_Found final : public Lookup_Error { |
251 | | public: |
252 | | explicit Algorithm_Not_Found(std::string_view name); |
253 | | }; |
254 | | |
255 | | /** |
256 | | * Provider_Not_Found is thrown when a specific provider was requested |
257 | | * but that provider is not available. |
258 | | * |
259 | | * @warning This exception type will be removed in the future. Instead |
260 | | * just catch Lookup_Error. |
261 | | */ |
262 | | class BOTAN_PUBLIC_API(2, 0) Provider_Not_Found final : public Lookup_Error { |
263 | | public: |
264 | | Provider_Not_Found(std::string_view algo, std::string_view provider); |
265 | | }; |
266 | | |
267 | | /** |
268 | | * An AEAD or MAC check detected a message modification |
269 | | * |
270 | | * In versions before 2.10, Invalid_Authentication_Tag was named |
271 | | * Integrity_Failure, it was renamed to make its usage more clear. |
272 | | */ |
273 | | class BOTAN_PUBLIC_API(2, 0) Invalid_Authentication_Tag final : public Exception { |
274 | | public: |
275 | | explicit Invalid_Authentication_Tag(std::string_view msg); |
276 | | |
277 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; } |
278 | | }; |
279 | | |
280 | | /** |
281 | | * For compatability with older versions |
282 | | */ |
283 | | typedef Invalid_Authentication_Tag Integrity_Failure; |
284 | | |
285 | | /** |
286 | | * An error occurred while operating on an IO stream |
287 | | */ |
288 | | class BOTAN_PUBLIC_API(2, 0) Stream_IO_Error final : public Exception { |
289 | | public: |
290 | | explicit Stream_IO_Error(std::string_view err); |
291 | | |
292 | 0 | ErrorType error_type() const noexcept override { return ErrorType::IoError; } |
293 | | }; |
294 | | |
295 | | /** |
296 | | * System_Error |
297 | | * |
298 | | * This exception is thrown in the event of an error related to interacting |
299 | | * with the operating system. |
300 | | * |
301 | | * This exception type also (optionally) captures an integer error code eg |
302 | | * POSIX errno or Windows GetLastError. |
303 | | */ |
304 | | class BOTAN_PUBLIC_API(2, 9) System_Error : public Exception { |
305 | | public: |
306 | 0 | System_Error(std::string_view msg) : Exception(msg), m_error_code(0) {} |
307 | | |
308 | | System_Error(std::string_view msg, int err_code); |
309 | | |
310 | 0 | ErrorType error_type() const noexcept override { return ErrorType::SystemError; } |
311 | | |
312 | 0 | int error_code() const noexcept override { return m_error_code; } |
313 | | |
314 | | private: |
315 | | int m_error_code; |
316 | | }; |
317 | | |
318 | | /** |
319 | | * An internal error occurred. If observed, please file a bug. |
320 | | */ |
321 | | class BOTAN_PUBLIC_API(2, 0) Internal_Error : public Exception { |
322 | | public: |
323 | | explicit Internal_Error(std::string_view err); |
324 | | |
325 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InternalError; } |
326 | | }; |
327 | | |
328 | | /** |
329 | | * Not Implemented Exception |
330 | | * |
331 | | * This is thrown in the situation where a requested operation is |
332 | | * logically valid but is not implemented by this version of the library. |
333 | | */ |
334 | | class BOTAN_PUBLIC_API(2, 0) Not_Implemented final : public Exception { |
335 | | public: |
336 | | explicit Not_Implemented(std::string_view err); |
337 | | |
338 | 0 | ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; } |
339 | | }; |
340 | | |
341 | | template <typename E, typename... Args> |
342 | | inline void do_throw_error(const char* file, int line, const char* func, Args... args) { |
343 | | throw E(file, line, func, args...); |
344 | | } |
345 | | |
346 | | } // namespace Botan |
347 | | |
348 | | #endif |