/src/botan/build/include/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 | | { |
85 | | public: |
86 | | /** |
87 | | * Return a descriptive string which is hopefully comprehensible to |
88 | | * a developer. It will likely not be useful for an end user. |
89 | | * |
90 | | * The string has no particular format, and the content of exception |
91 | | * messages may change from release to release. Thus the main use of this |
92 | | * function is for logging or debugging. |
93 | | */ |
94 | 17.3k | const char* what() const noexcept override { return m_msg.c_str(); } |
95 | | |
96 | | /** |
97 | | * Return the "type" of error which occurred. |
98 | | */ |
99 | 0 | virtual ErrorType error_type() const noexcept { return Botan::ErrorType::Unknown; } |
100 | | |
101 | | /** |
102 | | * Return an error code associated with this exception, or otherwise 0. |
103 | | * |
104 | | * The domain of this error varies depending on the source, for example on |
105 | | * POSIX systems it might be errno, while on a Windows system it might be |
106 | | * the result of GetLastError or WSAGetLastError. |
107 | | */ |
108 | 0 | virtual int error_code() const noexcept { return 0; } |
109 | | |
110 | | /** |
111 | | * Avoid throwing base Exception, use a subclass |
112 | | */ |
113 | | explicit Exception(const std::string& msg); |
114 | | |
115 | | /** |
116 | | * Avoid throwing base Exception, use a subclass |
117 | | */ |
118 | | Exception(const char* prefix, const std::string& msg); |
119 | | |
120 | | /** |
121 | | * Avoid throwing base Exception, use a subclass |
122 | | */ |
123 | | Exception(const std::string& msg, const std::exception& e); |
124 | | |
125 | | private: |
126 | | std::string m_msg; |
127 | | }; |
128 | | |
129 | | /** |
130 | | * An invalid argument was provided to an API call. |
131 | | */ |
132 | | class BOTAN_PUBLIC_API(2,0) Invalid_Argument : public Exception |
133 | | { |
134 | | public: |
135 | | explicit Invalid_Argument(const std::string& msg); |
136 | | |
137 | | explicit Invalid_Argument(const std::string& msg, const std::string& where); |
138 | | |
139 | | Invalid_Argument(const std::string& msg, const std::exception& e); |
140 | | |
141 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; } |
142 | | }; |
143 | | |
144 | | /** |
145 | | * An invalid key length was used |
146 | | */ |
147 | | class BOTAN_PUBLIC_API(2,0) Invalid_Key_Length final : public Invalid_Argument |
148 | | { |
149 | | public: |
150 | | Invalid_Key_Length(const std::string& name, size_t length); |
151 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; } |
152 | | }; |
153 | | |
154 | | /** |
155 | | * An invalid nonce length was used |
156 | | */ |
157 | | class BOTAN_PUBLIC_API(2,0) Invalid_IV_Length final : public Invalid_Argument |
158 | | { |
159 | | public: |
160 | | Invalid_IV_Length(const std::string& mode, size_t bad_len); |
161 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; } |
162 | | }; |
163 | | |
164 | | /** |
165 | | * Invalid_Algorithm_Name Exception |
166 | | */ |
167 | | class BOTAN_PUBLIC_API(2,0) Invalid_Algorithm_Name final : public Invalid_Argument |
168 | | { |
169 | | public: |
170 | | explicit Invalid_Algorithm_Name(const std::string& name); |
171 | | }; |
172 | | |
173 | | /** |
174 | | * Encoding_Error Exception |
175 | | */ |
176 | | class BOTAN_PUBLIC_API(2,0) Encoding_Error final : public Exception |
177 | | { |
178 | | public: |
179 | | explicit Encoding_Error(const std::string& name); |
180 | | |
181 | 0 | ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; } |
182 | | }; |
183 | | |
184 | | /** |
185 | | * A decoding error occurred. |
186 | | */ |
187 | | class BOTAN_PUBLIC_API(2,0) Decoding_Error : public Exception |
188 | | { |
189 | | public: |
190 | | explicit Decoding_Error(const std::string& name); |
191 | | |
192 | | Decoding_Error(const std::string& name, const char* exception_message); |
193 | | |
194 | | Decoding_Error(const std::string& msg, const std::exception& e); |
195 | | |
196 | 0 | ErrorType error_type() const noexcept override { return ErrorType::DecodingFailure; } |
197 | | }; |
198 | | |
199 | | /** |
200 | | * Invalid state was encountered. A request was made on an object while the |
201 | | * object was in a state where the operation cannot be performed. |
202 | | */ |
203 | | class BOTAN_PUBLIC_API(2,0) Invalid_State : public Exception |
204 | | { |
205 | | public: |
206 | 1.29k | explicit Invalid_State(const std::string& err) : Exception(err) {} |
207 | | |
208 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidObjectState; } |
209 | | }; |
210 | | |
211 | | /** |
212 | | * A PRNG was called on to produce output while still unseeded |
213 | | */ |
214 | | class BOTAN_PUBLIC_API(2,0) PRNG_Unseeded final : public Invalid_State |
215 | | { |
216 | | public: |
217 | | explicit PRNG_Unseeded(const std::string& algo); |
218 | | }; |
219 | | |
220 | | /** |
221 | | * The key was not set on an object. This occurs with symmetric objects where |
222 | | * an operation which requires the key is called prior to set_key being called. |
223 | | */ |
224 | | class BOTAN_PUBLIC_API(2,4) Key_Not_Set : public Invalid_State |
225 | | { |
226 | | public: |
227 | | explicit Key_Not_Set(const std::string& 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 | | { |
237 | | public: |
238 | 2 | explicit Lookup_Error(const std::string& err) : Exception(err) {} |
239 | | |
240 | | Lookup_Error(const std::string& type, |
241 | | const std::string& algo, |
242 | | const std::string& provider); |
243 | | |
244 | 0 | ErrorType error_type() const noexcept override { return ErrorType::LookupError; } |
245 | | }; |
246 | | |
247 | | /** |
248 | | * Algorithm_Not_Found Exception |
249 | | * |
250 | | * @warning This exception type will be removed in the future. Instead |
251 | | * just catch Lookup_Error. |
252 | | */ |
253 | | class BOTAN_PUBLIC_API(2,0) Algorithm_Not_Found final : public Lookup_Error |
254 | | { |
255 | | public: |
256 | | explicit Algorithm_Not_Found(const std::string& name); |
257 | | }; |
258 | | |
259 | | /** |
260 | | * Provider_Not_Found is thrown when a specific provider was requested |
261 | | * but that provider is not available. |
262 | | * |
263 | | * @warning This exception type will be removed in the future. Instead |
264 | | * just catch Lookup_Error. |
265 | | */ |
266 | | class BOTAN_PUBLIC_API(2,0) Provider_Not_Found final : public Lookup_Error |
267 | | { |
268 | | public: |
269 | | Provider_Not_Found(const std::string& algo, const std::string& provider); |
270 | | }; |
271 | | |
272 | | /** |
273 | | * An AEAD or MAC check detected a message modification |
274 | | * |
275 | | * In versions before 2.10, Invalid_Authentication_Tag was named |
276 | | * Integrity_Failure, it was renamed to make its usage more clear. |
277 | | */ |
278 | | class BOTAN_PUBLIC_API(2,0) Invalid_Authentication_Tag final : public Exception |
279 | | { |
280 | | public: |
281 | | explicit Invalid_Authentication_Tag(const std::string& msg); |
282 | | |
283 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; } |
284 | | }; |
285 | | |
286 | | /** |
287 | | * For compatability with older versions |
288 | | */ |
289 | | typedef Invalid_Authentication_Tag Integrity_Failure; |
290 | | |
291 | | /** |
292 | | * An error occurred while operating on an IO stream |
293 | | */ |
294 | | class BOTAN_PUBLIC_API(2,0) Stream_IO_Error final : public Exception |
295 | | { |
296 | | public: |
297 | | explicit Stream_IO_Error(const std::string& err); |
298 | | |
299 | 0 | ErrorType error_type() const noexcept override { return ErrorType::IoError; } |
300 | | }; |
301 | | |
302 | | /** |
303 | | * System_Error |
304 | | * |
305 | | * This exception is thrown in the event of an error related to interacting |
306 | | * with the operating system. |
307 | | * |
308 | | * This exception type also (optionally) captures an integer error code eg |
309 | | * POSIX errno or Windows GetLastError. |
310 | | */ |
311 | | class BOTAN_PUBLIC_API(2,9) System_Error : public Exception |
312 | | { |
313 | | public: |
314 | 0 | System_Error(const std::string& msg) : Exception(msg), m_error_code(0) {} |
315 | | |
316 | | System_Error(const std::string& msg, int err_code); |
317 | | |
318 | 0 | ErrorType error_type() const noexcept override { return ErrorType::SystemError; } |
319 | | |
320 | 0 | int error_code() const noexcept override { return m_error_code; } |
321 | | |
322 | | private: |
323 | | int m_error_code; |
324 | | }; |
325 | | |
326 | | /** |
327 | | * An internal error occurred. If observed, please file a bug. |
328 | | */ |
329 | | class BOTAN_PUBLIC_API(2,0) Internal_Error : public Exception |
330 | | { |
331 | | public: |
332 | | explicit Internal_Error(const std::string& err); |
333 | | |
334 | 0 | ErrorType error_type() const noexcept override { return ErrorType::InternalError; } |
335 | | }; |
336 | | |
337 | | /** |
338 | | * Not Implemented Exception |
339 | | * |
340 | | * This is thrown in the situation where a requested operation is |
341 | | * logically valid but is not implemented by this version of the library. |
342 | | */ |
343 | | class BOTAN_PUBLIC_API(2,0) Not_Implemented final : public Exception |
344 | | { |
345 | | public: |
346 | | explicit Not_Implemented(const std::string& err); |
347 | | |
348 | 0 | ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; } |
349 | | }; |
350 | | |
351 | | template<typename E, typename... Args> |
352 | | inline void do_throw_error(const char* file, int line, const char* func, Args... args) |
353 | | { |
354 | | throw E(file, line, func, args...); |
355 | | } |
356 | | |
357 | | } |
358 | | |
359 | | #endif |