Botan  2.10.0
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 
66  /** An error when interacting with zlib */
67  ZlibError = 300,
68  /** An error when interacting with bzip2 */
69  Bzip2Error,
70  /** An error when interacting with lzma */
71  LzmaError,
72 
73 };
74 
75 /**
76 * Base class for all exceptions thrown by the library
77 */
78 class BOTAN_PUBLIC_API(2,0) Exception : public std::exception
79  {
80  public:
81  /**
82  * Return a descriptive string which is hopefully comprehensible to
83  * a developer. It will likely not be useful for an end user.
84  *
85  * The string has no particular format, and the content of exception
86  * messages may change from release to release. Thus the main use of this
87  * function is for logging or debugging.
88  */
89  const char* what() const noexcept override { return m_msg.c_str(); }
90 
91  /**
92  * Return the "type" of error which occurred.
93  */
94  virtual ErrorType error_type() const noexcept { return Botan::ErrorType::Unknown; }
95 
96  /**
97  * Return an error code associated with this exception, or otherwise 0.
98  *
99  * The domain of this error varies depending on the source, for example on
100  * POSIX systems it might be errno, while on a Windows system it might be
101  * the result of GetLastError or WSAGetLastError. For error_type() is
102  * OpenSSLError, it will (if nonzero) be an OpenSSL error code from
103  * ERR_get_error.
104  */
105  virtual int error_code() const noexcept { return 0; }
106 
107  protected:
108  explicit Exception(const std::string& msg);
109  Exception(const char* prefix, const std::string& msg);
110  Exception(const std::string& msg, const std::exception& e);
111 
112  private:
113  std::string m_msg;
114  };
115 
116 /**
117 * An invalid argument was provided to an API call.
118 */
119 class BOTAN_PUBLIC_API(2,0) Invalid_Argument : public Exception
120  {
121  public:
122  explicit Invalid_Argument(const std::string& msg);
123 
124  explicit Invalid_Argument(const std::string& msg, const std::string& where);
125 
126  Invalid_Argument(const std::string& msg, const std::exception& e);
127 
128  ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; }
129  };
130 
131 /**
132 * An invalid key length was used
133 */
134 class BOTAN_PUBLIC_API(2,0) Invalid_Key_Length final : public Invalid_Argument
135  {
136  public:
137  Invalid_Key_Length(const std::string& name, size_t length);
138  ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; }
139  };
140 
141 /**
142 * An invalid nonce length was used
143 */
144 class BOTAN_PUBLIC_API(2,0) Invalid_IV_Length final : public Invalid_Argument
145  {
146  public:
147  Invalid_IV_Length(const std::string& mode, size_t bad_len);
148  ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; }
149  };
150 
151 /**
152 * Invalid_Algorithm_Name Exception
153 */
154 class BOTAN_PUBLIC_API(2,0) Invalid_Algorithm_Name final : public Invalid_Argument
155  {
156  public:
157  explicit Invalid_Algorithm_Name(const std::string& name);
158  };
159 
160 /**
161 * Encoding_Error Exception
162 *
163 * This exception derives from Invalid_Argument for historical reasons, and it
164 * does not make any real sense for it to do so. In a future major release this
165 * exception type will derive directly from Exception instead.
166 */
167 class BOTAN_PUBLIC_API(2,0) Encoding_Error final : public Invalid_Argument
168  {
169  public:
170  explicit Encoding_Error(const std::string& name);
171 
172  ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; }
173  };
174 
175 /**
176 * A decoding error occurred.
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 */
182 class BOTAN_PUBLIC_API(2,0) Decoding_Error : public Invalid_Argument
183  {
184  public:
185  explicit Decoding_Error(const std::string& name);
186 
187  Decoding_Error(const std::string& name, const char* exception_message);
188 
189  Decoding_Error(const std::string& msg, const std::exception& e);
190 
191  ErrorType error_type() const noexcept override { return ErrorType::DecodingFailure; }
192  };
193 
194 /**
195 * Invalid state was encountered. A request was made on an object while the
196 * object was in a state where the operation cannot be performed.
197 */
198 class BOTAN_PUBLIC_API(2,0) Invalid_State : public Exception
199  {
200  public:
201  explicit Invalid_State(const std::string& err) : Exception(err) {}
202 
203  ErrorType error_type() const noexcept override { return ErrorType::InvalidObjectState; }
204  };
205 
206 /**
207 * A PRNG was called on to produce output while still unseeded
208 */
209 class BOTAN_PUBLIC_API(2,0) PRNG_Unseeded final : public Invalid_State
210  {
211  public:
212  explicit PRNG_Unseeded(const std::string& algo);
213  };
214 
215 /**
216 * The key was not set on an object. This occurs with symmetric objects where
217 * an operation which requires the key is called prior to set_key being called.
218 */
219 class BOTAN_PUBLIC_API(2,4) Key_Not_Set : public Invalid_State
220  {
221  public:
222  explicit Key_Not_Set(const std::string& algo);
223 
224  ErrorType error_type() const noexcept override { return ErrorType::KeyNotSet; }
225  };
226 
227 /**
228 * A request was made for some kind of object which could not be located
229 */
230 class BOTAN_PUBLIC_API(2,0) Lookup_Error : public Exception
231  {
232  public:
233  explicit Lookup_Error(const std::string& err) : Exception(err) {}
234 
235  Lookup_Error(const std::string& type,
236  const std::string& algo,
237  const std::string& provider);
238 
239  ErrorType error_type() const noexcept override { return ErrorType::LookupError; }
240  };
241 
242 /**
243 * Algorithm_Not_Found Exception
244 *
245 * @warning This exception type will be removed in the future. Instead
246 * just catch Lookup_Error.
247 */
248 class BOTAN_PUBLIC_API(2,0) Algorithm_Not_Found final : public Lookup_Error
249  {
250  public:
251  explicit Algorithm_Not_Found(const std::string& name);
252  };
253 
254 /**
255 * Provider_Not_Found is thrown when a specific provider was requested
256 * but that provider is not available.
257 *
258 * @warning This exception type will be removed in the future. Instead
259 * just catch Lookup_Error.
260 */
261 class BOTAN_PUBLIC_API(2,0) Provider_Not_Found final : public Lookup_Error
262  {
263  public:
264  Provider_Not_Found(const std::string& algo, const std::string& 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  {
275  public:
276  explicit Invalid_Authentication_Tag(const std::string& msg);
277 
278  ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; }
279  };
280 
281 /**
282 * For compatability with older versions
283 */
284 typedef Invalid_Authentication_Tag Integrity_Failure;
285 
286 /**
287 * An error occurred while operating on an IO stream
288 */
289 class BOTAN_PUBLIC_API(2,0) Stream_IO_Error final : public Exception
290  {
291  public:
292  explicit Stream_IO_Error(const std::string& err);
293 
294  ErrorType error_type() const noexcept override { return ErrorType::IoError; }
295  };
296 
297 /**
298 * System_Error
299 *
300 * This exception is thrown in the event of an error related to interacting
301 * with the operating system.
302 *
303 * This exception type also (optionally) captures an integer error code eg
304 * POSIX errno or Windows GetLastError.
305 */
306 class BOTAN_PUBLIC_API(2,9) System_Error : public Exception
307  {
308  public:
309  System_Error(const std::string& msg) : Exception(msg), m_error_code(0) {}
310 
311  System_Error(const std::string& msg, int err_code);
312 
313  ErrorType error_type() const noexcept override { return ErrorType::SystemError; }
314 
315  int error_code() const noexcept override { return m_error_code; }
316 
317  private:
318  int m_error_code;
319  };
320 
321 /**
322 * An internal error occurred. If observed, please file a bug.
323 */
324 class BOTAN_PUBLIC_API(2,0) Internal_Error : public Exception
325  {
326  public:
327  explicit Internal_Error(const std::string& err);
328 
329  ErrorType error_type() const noexcept override { return ErrorType::InternalError; }
330  };
331 
332 /**
333 * Not Implemented Exception
334 *
335 * This is thrown in the situation where a requested operation is
336 * logically valid but is not implemented by this version of the library.
337 */
338 class BOTAN_PUBLIC_API(2,0) Not_Implemented final : public Exception
339  {
340  public:
341  explicit Not_Implemented(const std::string& err);
342 
343  ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; }
344  };
345 
346 /*
347  The following exception types are still in use for compatability reasons,
348  but are deprecated and will be removed in a future major release
349 */
350 
351 /**
352 * An invalid OID string was used.
353 *
354 * This exception will be removed in a future major release.
355 */
356 class BOTAN_PUBLIC_API(2,0) Invalid_OID final : public Decoding_Error
357  {
358  public:
359  explicit Invalid_OID(const std::string& oid);
360  };
361 
362 /*
363  The following exception types are deprecated, no longer used,
364  and will be removed in a future major release
365 */
366 
367 /**
368 * Self Test Failure Exception
369 *
370 * This exception is no longer used. It will be removed in a future major release.
371 */
372 class BOTAN_PUBLIC_API(2,0) Self_Test_Failure final : public Internal_Error
373  {
374  public:
375  BOTAN_DEPRECATED("no longer used") explicit Self_Test_Failure(const std::string& err);
376  };
377 
378 /**
379 * No_Provider_Found Exception
380 *
381 * This exception is no longer used. It will be removed in a future major release.
382 */
383 class BOTAN_PUBLIC_API(2,0) No_Provider_Found final : public Exception
384  {
385  public:
386  BOTAN_DEPRECATED("no longer used") explicit No_Provider_Found(const std::string& name);
387  };
388 
389 /**
390 * Policy_Violation Exception
391 *
392 * This exception is no longer used. It will be removed in a future major release.
393 */
394 class BOTAN_PUBLIC_API(2,0) Policy_Violation final : public Invalid_State
395  {
396  public:
397  BOTAN_DEPRECATED("no longer used") explicit Policy_Violation(const std::string& err);
398  };
399 
400 /**
401 * Unsupported_Argument Exception
402 *
403 * An argument that is invalid because it is not supported by Botan.
404 * It might or might not be valid in another context like a standard.
405 *
406 * This exception is no longer used, instead Not_Implemented is thrown.
407 * It will be removed in a future major release.
408 */
409 class BOTAN_PUBLIC_API(2,0) Unsupported_Argument final : public Invalid_Argument
410  {
411  public:
412  BOTAN_DEPRECATED("no longer used") explicit Unsupported_Argument(const std::string& msg) : Invalid_Argument(msg) {}
413  };
414 
415 template<typename E, typename... Args>
416 inline void do_throw_error(const char* file, int line, const char* func, Args... args)
417  {
418  throw E(file, line, func, args...);
419  }
420 
421 }
422 
423 #endif
int(* final)(unsigned char *, CTX *)
Definition: bigint.h:1136
char * name
Definition: ffi.h:327
MechanismType type
ErrorType
Definition: exceptn.h:20
void uint8_t const uint8_t RandomNumberGenerator Newhope_Mode mode
Definition: newhope.h:75
BOTAN_NORETURN void const char const char const char * file
Definition: assert.h:23
BOTAN_NORETURN void const char const char const char int line
Definition: assert.h:23
Definition: alg_id.cpp:13
class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final bool BOTAN_PUBLIC_API(2, 0) operator
Name Constraints.
Definition: asn1_obj.h:65
class BOTAN_PUBLIC_API(2, 0) Stream_IO_Error final class BOTAN_PUBLIC_API(2, 9) System_Error class BOTAN_PUBLIC_API(2, 0) Internal_Error class BOTAN_PUBLIC_API(2, 0) Not_Implemented final class BOTAN_PUBLIC_API(2, 0) Invalid_OID final class BOTAN_PUBLIC_API(2, 0) Self_Test_Failure final class BOTAN_PUBLIC_API(2, 0) No_Provider_Found final class BOTAN_PUBLIC_API(2, 0) Policy_Violation final class BOTAN_PUBLIC_API(2, 0) Unsupported_Argument final void do_throw_error(const char *file, int line, const char *func, Args... args)
Definition: exceptn.h:416
botan_mp_t botan_mp_t botan_mp_t e
Definition: ffi.h:1098
BOTAN_NORETURN void const char const char * func
Definition: assert.h:23
class BOTAN_PUBLIC_API(2, 0) Exception class BOTAN_PUBLIC_API(2, 0) Invalid_Argument class BOTAN_PUBLIC_API(2, 0) Invalid_Key_Length final class BOTAN_PUBLIC_API(2, 0) Invalid_IV_Length final class BOTAN_PUBLIC_API(2, 0) Invalid_Algorithm_Name final class BOTAN_PUBLIC_API(2, 0) Encoding_Error final class BOTAN_PUBLIC_API(2, 0) Decoding_Error class BOTAN_PUBLIC_API(2, 0) Invalid_State class BOTAN_PUBLIC_API(2, 0) PRNG_Unseeded final class BOTAN_PUBLIC_API(2, 4) Key_Not_Set class BOTAN_PUBLIC_API(2, 0) Lookup_Error class BOTAN_PUBLIC_API(2, 0) Algorithm_Not_Found final class BOTAN_PUBLIC_API(2, 0) Provider_Not_Found final class BOTAN_PUBLIC_API(2, 0) Invalid_Authentication_Tag final typedef Invalid_Authentication_Tag Integrity_Failure
Definition: exceptn.h:273