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