Botan  2.12.1
Crypto and TLS for C++11
exceptn.h
Go to the documentation of this file.
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 */
25  /** An operation seems valid, but not supported by the current version */
27  /** Memory allocation failure */
29  /** An internal error occurred */
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 */
40  /** A key with invalid length was provided */
42  /** A nonce with invalid length was provided */
44  /** An object type was requested but cannot be found */
46  /** Encoding a message or datum failed */
48  /** Decoding a message or datum failed */
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 
57  /** An error when calling OpenSSL */
58  OpenSSLError = 200,
59  /** An error when interacting with CommonCrypto API */
61  /** An error when interacting with a PKCS11 device */
63  /** An error when interacting with a TPM device */
64  TPMError,
65  /** An error when interacting with a database */
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  const char* what() const noexcept override { return m_msg.c_str(); }
95 
96  /**
97  * Return the "type" of error which occurred.
98  */
99  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. For error_type() is
107  * OpenSSLError, it will (if nonzero) be an OpenSSL error code from
108  * ERR_get_error.
109  */
110  virtual int error_code() const noexcept { return 0; }
111 
112  /**
113  * Avoid throwing base Exception, use a subclass
114  */
115  explicit Exception(const std::string& msg);
116 
117  /**
118  * Avoid throwing base Exception, use a subclass
119  */
120  Exception(const char* prefix, const std::string& msg);
121 
122  /**
123  * Avoid throwing base Exception, use a subclass
124  */
125  Exception(const std::string& msg, const std::exception& e);
126 
127  private:
128  std::string m_msg;
129  };
130 
131 /**
132 * An invalid argument was provided to an API call.
133 */
135  {
136  public:
137  explicit Invalid_Argument(const std::string& msg);
138 
139  explicit Invalid_Argument(const std::string& msg, const std::string& where);
140 
141  Invalid_Argument(const std::string& msg, const std::exception& e);
142 
143  ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; }
144  };
145 
146 /**
147 * An invalid key length was used
148 */
150  {
151  public:
152  Invalid_Key_Length(const std::string& name, size_t length);
153  ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; }
154  };
155 
156 /**
157 * An invalid nonce length was used
158 */
160  {
161  public:
162  Invalid_IV_Length(const std::string& mode, size_t bad_len);
163  ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; }
164  };
165 
166 /**
167 * Invalid_Algorithm_Name Exception
168 */
170  {
171  public:
172  explicit Invalid_Algorithm_Name(const std::string& name);
173  };
174 
175 /**
176 * Encoding_Error Exception
177 *
178 * This exception derives from Invalid_Argument for historical reasons, and it
179 * does not make any real sense for it to do so. In a future major release this
180 * exception type will derive directly from Exception instead.
181 */
183  {
184  public:
185  explicit Encoding_Error(const std::string& name);
186 
187  ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; }
188  };
189 
190 /**
191 * A decoding error occurred.
192 *
193 * This exception derives from Invalid_Argument for historical reasons, and it
194 * does not make any real sense for it to do so. In a future major release this
195 * exception type will derive directly from Exception instead.
196 */
198  {
199  public:
200  explicit Decoding_Error(const std::string& name);
201 
202  Decoding_Error(const std::string& name, const char* exception_message);
203 
204  Decoding_Error(const std::string& msg, const std::exception& e);
205 
206  ErrorType error_type() const noexcept override { return ErrorType::DecodingFailure; }
207  };
208 
209 /**
210 * Invalid state was encountered. A request was made on an object while the
211 * object was in a state where the operation cannot be performed.
212 */
214  {
215  public:
216  explicit Invalid_State(const std::string& err) : Exception(err) {}
217 
218  ErrorType error_type() const noexcept override { return ErrorType::InvalidObjectState; }
219  };
220 
221 /**
222 * A PRNG was called on to produce output while still unseeded
223 */
225  {
226  public:
227  explicit PRNG_Unseeded(const std::string& algo);
228  };
229 
230 /**
231 * The key was not set on an object. This occurs with symmetric objects where
232 * an operation which requires the key is called prior to set_key being called.
233 */
235  {
236  public:
237  explicit Key_Not_Set(const std::string& algo);
238 
239  ErrorType error_type() const noexcept override { return ErrorType::KeyNotSet; }
240  };
241 
242 /**
243 * A request was made for some kind of object which could not be located
244 */
246  {
247  public:
248  explicit Lookup_Error(const std::string& err) : Exception(err) {}
249 
250  Lookup_Error(const std::string& type,
251  const std::string& algo,
252  const std::string& provider);
253 
254  ErrorType error_type() const noexcept override { return ErrorType::LookupError; }
255  };
256 
257 /**
258 * Algorithm_Not_Found Exception
259 *
260 * @warning This exception type will be removed in the future. Instead
261 * just catch Lookup_Error.
262 */
264  {
265  public:
266  explicit Algorithm_Not_Found(const std::string& name);
267  };
268 
269 /**
270 * Provider_Not_Found is thrown when a specific provider was requested
271 * but that provider is not available.
272 *
273 * @warning This exception type will be removed in the future. Instead
274 * just catch Lookup_Error.
275 */
277  {
278  public:
279  Provider_Not_Found(const std::string& algo, const std::string& provider);
280  };
281 
282 /**
283 * An AEAD or MAC check detected a message modification
284 *
285 * In versions before 2.10, Invalid_Authentication_Tag was named
286 * Integrity_Failure, it was renamed to make its usage more clear.
287 */
289  {
290  public:
291  explicit Invalid_Authentication_Tag(const std::string& msg);
292 
293  ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; }
294  };
295 
296 /**
297 * For compatability with older versions
298 */
300 
301 /**
302 * An error occurred while operating on an IO stream
303 */
305  {
306  public:
307  explicit Stream_IO_Error(const std::string& err);
308 
309  ErrorType error_type() const noexcept override { return ErrorType::IoError; }
310  };
311 
312 /**
313 * System_Error
314 *
315 * This exception is thrown in the event of an error related to interacting
316 * with the operating system.
317 *
318 * This exception type also (optionally) captures an integer error code eg
319 * POSIX errno or Windows GetLastError.
320 */
322  {
323  public:
324  System_Error(const std::string& msg) : Exception(msg), m_error_code(0) {}
325 
326  System_Error(const std::string& msg, int err_code);
327 
328  ErrorType error_type() const noexcept override { return ErrorType::SystemError; }
329 
330  int error_code() const noexcept override { return m_error_code; }
331 
332  private:
333  int m_error_code;
334  };
335 
336 /**
337 * An internal error occurred. If observed, please file a bug.
338 */
340  {
341  public:
342  explicit Internal_Error(const std::string& err);
343 
344  ErrorType error_type() const noexcept override { return ErrorType::InternalError; }
345  };
346 
347 /**
348 * Not Implemented Exception
349 *
350 * This is thrown in the situation where a requested operation is
351 * logically valid but is not implemented by this version of the library.
352 */
354  {
355  public:
356  explicit Not_Implemented(const std::string& err);
357 
358  ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; }
359  };
360 
361 /*
362  The following exception types are still in use for compatability reasons,
363  but are deprecated and will be removed in a future major release.
364  Instead catch the base class.
365 */
366 
367 /**
368 * An invalid OID string was used.
369 *
370 * This exception will be removed in a future major release.
371 */
373  {
374  public:
375  explicit Invalid_OID(const std::string& oid);
376  };
377 
378 /*
379  The following exception types are deprecated, no longer used,
380  and will be removed in a future major release
381 */
382 
383 /**
384 * Self Test Failure Exception
385 *
386 * This exception is no longer used. It will be removed in a future major release.
387 */
389  {
390  public:
391  BOTAN_DEPRECATED("no longer used") explicit Self_Test_Failure(const std::string& err);
392  };
393 
394 /**
395 * No_Provider_Found Exception
396 *
397 * This exception is no longer used. It will be removed in a future major release.
398 */
400  {
401  public:
402  BOTAN_DEPRECATED("no longer used") explicit No_Provider_Found(const std::string& name);
403  };
404 
405 /**
406 * Policy_Violation Exception
407 *
408 * This exception is no longer used. It will be removed in a future major release.
409 */
411  {
412  public:
413  BOTAN_DEPRECATED("no longer used") explicit Policy_Violation(const std::string& err);
414  };
415 
416 /**
417 * Unsupported_Argument Exception
418 *
419 * An argument that is invalid because it is not supported by Botan.
420 * It might or might not be valid in another context like a standard.
421 *
422 * This exception is no longer used, instead Not_Implemented is thrown.
423 * It will be removed in a future major release.
424 */
426  {
427  public:
428  BOTAN_DEPRECATED("no longer used") explicit Unsupported_Argument(const std::string& msg) : Invalid_Argument(msg) {}
429  };
430 
431 template<typename E, typename... Args>
432 inline void do_throw_error(const char* file, int line, const char* func, Args... args)
433  {
434  throw E(file, line, func, args...);
435  }
436 
437 }
438 
439 #endif
ErrorType error_type() const noexcept override
Definition: exceptn.h:143
ErrorType error_type() const noexcept override
Definition: exceptn.h:328
Invalid_State(const std::string &err)
Definition: exceptn.h:216
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:99
void do_throw_error(const char *file, int line, const char *func, Args... args)
Definition: exceptn.h:432
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: bigint.h:1135
ErrorType error_type() const noexcept override
Definition: exceptn.h:218
int error_code() const noexcept override
Definition: exceptn.h:330
MechanismType type
virtual int error_code() const noexcept
Definition: exceptn.h:110
const char * what() const noexcept override
Definition: exceptn.h:94
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
std::string name
ErrorType error_type() const noexcept override
Definition: exceptn.h:163
ErrorType error_type() const noexcept override
Definition: exceptn.h:254
ErrorType
Definition: exceptn.h:20
ErrorType error_type() const noexcept override
Definition: exceptn.h:344
Definition: alg_id.cpp:13
System_Error(const std::string &msg)
Definition: exceptn.h:324
ErrorType error_type() const noexcept override
Definition: exceptn.h:293
Invalid_Authentication_Tag Integrity_Failure
Definition: exceptn.h:299
ErrorType error_type() const noexcept override
Definition: exceptn.h:358
ErrorType error_type() const noexcept override
Definition: exceptn.h:187
ErrorType error_type() const noexcept override
Definition: exceptn.h:309
ErrorType error_type() const noexcept override
Definition: exceptn.h:239
Lookup_Error(const std::string &err)
Definition: exceptn.h:248
ErrorType error_type() const noexcept override
Definition: exceptn.h:206
ErrorType error_type() const noexcept override
Definition: exceptn.h:153