Botan 3.0.0-alpha0
Crypto and TLS for C&
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
15namespace Botan {
16
17/**
18* Different types of errors that might occur
19*/
20enum 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 */
36 /** A key was not set on an object when this is required */
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) */
52 /** An error during an HTTP operation */
54 /** A message with an invalid authentication tag was detected */
56 /** An error during Roughtime validation */
58
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 */
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 */
72 /** An error when interacting with lzma */
74
75};
76
77//! \brief Convert an ErrorType to string
78std::string BOTAN_PUBLIC_API(2,11) to_string(ErrorType type);
79
80/**
81* Base class for all exceptions thrown by the library
82*/
83class 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.
107 */
108 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*/
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 ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; }
142 };
143
144/**
145* An invalid key length was used
146*/
148 {
149 public:
150 Invalid_Key_Length(const std::string& name, size_t length);
151 ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; }
152 };
153
154/**
155* An invalid nonce length was used
156*/
158 {
159 public:
160 Invalid_IV_Length(const std::string& mode, size_t bad_len);
161 ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; }
162 };
163
164/**
165* Invalid_Algorithm_Name Exception
166*/
168 {
169 public:
170 explicit Invalid_Algorithm_Name(const std::string& name);
171 };
172
173/**
174* Encoding_Error Exception
175*/
177 {
178 public:
179 explicit Encoding_Error(const std::string& name);
180
181 ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; }
182 };
183
184/**
185* A decoding error occurred.
186*/
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 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*/
204 {
205 public:
206 explicit Invalid_State(const std::string& err) : Exception(err) {}
207
208 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*/
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*/
225 {
226 public:
227 explicit Key_Not_Set(const std::string& algo);
228
229 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*/
236 {
237 public:
238 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 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*/
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*/
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*/
279 {
280 public:
281 explicit Invalid_Authentication_Tag(const std::string& msg);
282
283 ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; }
284 };
285
286/**
287* For compatability with older versions
288*/
290
291/**
292* An error occurred while operating on an IO stream
293*/
295 {
296 public:
297 explicit Stream_IO_Error(const std::string& err);
298
299 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*/
312 {
313 public:
314 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 ErrorType error_type() const noexcept override { return ErrorType::SystemError; }
319
320 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*/
330 {
331 public:
332 explicit Internal_Error(const std::string& err);
333
334 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*/
344 {
345 public:
346 explicit Not_Implemented(const std::string& err);
347
348 ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; }
349 };
350
351template<typename E, typename... Args>
352inline 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
ErrorType error_type() const noexcept override
Definition: exceptn.h:196
ErrorType error_type() const noexcept override
Definition: exceptn.h:181
const char * what() const noexcept override
Definition: exceptn.h:94
virtual ErrorType error_type() const noexcept
Definition: exceptn.h:99
virtual int error_code() const noexcept
Definition: exceptn.h:108
ErrorType error_type() const noexcept override
Definition: exceptn.h:334
ErrorType error_type() const noexcept override
Definition: exceptn.h:141
ErrorType error_type() const noexcept override
Definition: exceptn.h:283
ErrorType error_type() const noexcept override
Definition: exceptn.h:161
ErrorType error_type() const noexcept override
Definition: exceptn.h:151
ErrorType error_type() const noexcept override
Definition: exceptn.h:208
Invalid_State(const std::string &err)
Definition: exceptn.h:206
ErrorType error_type() const noexcept override
Definition: exceptn.h:229
Lookup_Error(const std::string &err)
Definition: exceptn.h:238
ErrorType error_type() const noexcept override
Definition: exceptn.h:244
ErrorType error_type() const noexcept override
Definition: exceptn.h:348
ErrorType error_type() const noexcept override
Definition: exceptn.h:299
ErrorType error_type() const noexcept override
Definition: exceptn.h:318
System_Error(const std::string &msg)
Definition: exceptn.h:314
int error_code() const noexcept override
Definition: exceptn.h:320
std::string name
int(* final)(unsigned char *, CTX *)
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Definition: alg_id.cpp:13
ErrorType
Definition: exceptn.h:20
void do_throw_error(const char *file, int line, const char *func, Args... args)
Definition: exceptn.h:352
Invalid_Authentication_Tag Integrity_Failure
Definition: exceptn.h:289
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
Definition: bigint.h:1077
MechanismType type