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