Botan 3.11.0
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#include <string_view>
15
16namespace Botan {
17
18/**
19* Different types of errors that might occur
20*/
21enum class ErrorType : uint16_t {
22 /** Some unknown error */
24 /** An error while calling a system interface */
26 /** An operation seems valid, but not supported by the current version */
28 /** Memory allocation failure */
30 /** An internal error occurred */
32 /** An I/O error occurred */
34
35 /** Invalid object state */
37 /** A key was not set on an object when this is required */
38 KeyNotSet = 101,
39 /** The application provided an argument which is invalid */
41 /** A key with invalid length was provided */
43 /** A nonce with invalid length was provided */
45 /** An object type was requested but cannot be found */
47 /** Encoding a message or datum failed */
49 /** Decoding a message or datum failed */
51 /** A TLS error (error_code will be the alert type) */
52 TLSError = 108,
53 /** An error during an HTTP operation */
54 HttpError = 109,
55 /** A message with an invalid authentication tag was detected */
57 /** An error during Roughtime validation */
59
60 /** An error when interacting with CommonCrypto API */
62 /** An error when interacting with a PKCS11 device */
64 /** An error when interacting with a TPM device */
65 TPMError = 203,
66 /** An error when interacting with a database */
68
69 /** An error when interacting with zlib */
70 ZlibError = 300,
71 /** An error when interacting with bzip2 */
73 /** An error when interacting with lzma */
74 LzmaError = 302,
75
76};
77
78//! \brief Convert an ErrorType to string
79std::string BOTAN_PUBLIC_API(2, 11) to_string(ErrorType type);
80
81/**
82* Base class for all exceptions thrown by the library
83*/
84class BOTAN_PUBLIC_API(2, 0) Exception : public std::exception {
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 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(std::string_view msg);
114
115 /**
116 * Avoid throwing base Exception, use a subclass
117 */
118 Exception(const char* prefix, std::string_view msg);
119
120 /**
121 * Avoid throwing base Exception, use a subclass
122 */
123 Exception(std::string_view 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 public:
134 explicit Invalid_Argument(std::string_view msg);
135
136 explicit Invalid_Argument(std::string_view msg, std::string_view where);
137
138 Invalid_Argument(std::string_view msg, const std::exception& e);
139
140 ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; }
141};
142
143/**
144* An invalid/unknown field name was passed to Public_Key::get_int_field
145*/
147 public:
148 Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name);
149};
150
151/**
152* An invalid key length was used
153*/
155 public:
156 Invalid_Key_Length(std::string_view name, size_t length);
157
158 ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; }
159};
160
161/**
162* An invalid nonce length was used
163*/
165 public:
166 Invalid_IV_Length(std::string_view mode, size_t bad_len);
167
168 ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; }
169};
170
171/**
172* Invalid_Algorithm_Name Exception
173*/
175 public:
176 explicit Invalid_Algorithm_Name(std::string_view name);
177};
178
179/**
180* Encoding_Error Exception
181*/
182class BOTAN_PUBLIC_API(2, 0) Encoding_Error final : public Exception {
183 public:
184 explicit Encoding_Error(std::string_view name);
185
186 ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; }
187};
188
189/**
190* A decoding error occurred.
191*/
193 public:
194 explicit Decoding_Error(std::string_view name);
195
196 Decoding_Error(std::string_view category, std::string_view err);
197
198 Decoding_Error(std::string_view msg, const std::exception& e);
199
200 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*/
208 public:
209 explicit Invalid_State(std::string_view err) : Exception(err) {}
210
211 ErrorType error_type() const noexcept override { return ErrorType::InvalidObjectState; }
212};
213
214/**
215* A PRNG was called on to produce output while still unseeded
216*/
217class BOTAN_PUBLIC_API(2, 0) PRNG_Unseeded final : public Invalid_State {
218 public:
219 explicit PRNG_Unseeded(std::string_view algo);
220};
221
222/**
223* The key was not set on an object. This occurs with symmetric objects where
224* an operation which requires the key is called prior to set_key being called.
225*/
227 public:
228 explicit Key_Not_Set(std::string_view algo);
229
230 ErrorType error_type() const noexcept override { return ErrorType::KeyNotSet; }
231};
232
233/**
234* A request was made for some kind of object which could not be located
235*/
237 public:
238 explicit Lookup_Error(std::string_view err) : Exception(err) {}
239
240 Lookup_Error(std::string_view type, std::string_view algo, std::string_view provider = "");
241
242 ErrorType error_type() const noexcept override { return ErrorType::LookupError; }
243};
244
245/**
246* Algorithm_Not_Found Exception
247*
248* @warning This exception type will be removed in the future. Instead
249* just catch Lookup_Error.
250*/
252 public:
253 explicit Algorithm_Not_Found(std::string_view name);
254};
255
256/**
257* Provider_Not_Found is thrown when a specific provider was requested
258* but that provider is not available.
259*
260* @warning This exception type will be removed in the future. Instead
261* just catch Lookup_Error.
262*/
264 public:
265 Provider_Not_Found(std::string_view algo, std::string_view provider);
266};
267
268/**
269* An AEAD or MAC check detected a message modification
270*
271* In versions before 2.10, Invalid_Authentication_Tag was named
272* Integrity_Failure, it was renamed to make its usage more clear.
273*/
275 public:
276 explicit Invalid_Authentication_Tag(std::string_view msg);
277
278 ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; }
279};
280
281/**
282* For compatibility with older versions
283*/
285
286/**
287* An error occurred while operating on an IO stream
288*/
289class BOTAN_PUBLIC_API(2, 0) Stream_IO_Error final : public Exception {
290 public:
291 explicit Stream_IO_Error(std::string_view err);
292
293 ErrorType error_type() const noexcept override { return ErrorType::IoError; }
294};
295
296/**
297* System_Error
298*
299* This exception is thrown in the event of an error related to interacting
300* with the operating system.
301*
302* This exception type also (optionally) captures an integer error code eg
303* POSIX errno or Windows GetLastError.
304*/
306 public:
307 explicit System_Error(std::string_view msg) : Exception(msg), m_error_code(0) {}
308
309 System_Error(std::string_view msg, int err_code);
310
311 ErrorType error_type() const noexcept override { return ErrorType::SystemError; }
312
313 int error_code() const noexcept override { return m_error_code; }
314
315 private:
316 int m_error_code;
317};
318
319/**
320* An internal error occurred. If observed, please file a bug.
321*/
323 public:
324 explicit Internal_Error(std::string_view err);
325
326 ErrorType error_type() const noexcept override { return ErrorType::InternalError; }
327};
328
329/**
330* Not Implemented Exception
331*
332* This is thrown in the situation where a requested operation is
333* logically valid but is not implemented by this version of the library.
334*/
335class BOTAN_PUBLIC_API(2, 0) Not_Implemented final : public Exception {
336 public:
337 explicit Not_Implemented(std::string_view err);
338
339 ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; }
340};
341
342template <typename E, typename... Args>
343inline void do_throw_error(const char* file, int line, const char* func, Args... args) {
344 throw E(file, line, func, args...);
345}
346
347} // namespace Botan
348
349#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
Algorithm_Not_Found(std::string_view name)
Definition exceptn.cpp:114
ErrorType error_type() const noexcept override
Definition exceptn.h:200
Decoding_Error(std::string_view name)
Definition exceptn.cpp:125
Encoding_Error(std::string_view name)
Definition exceptn.cpp:123
ErrorType error_type() const noexcept override
Definition exceptn.h:186
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
Exception(std::string_view msg)
Definition exceptn.cpp:71
ErrorType error_type() const noexcept override
Definition exceptn.h:326
Internal_Error(std::string_view err)
Definition exceptn.cpp:99
Invalid_Algorithm_Name(std::string_view name)
Definition exceptn.cpp:120
Invalid_Argument(std::string_view msg)
Definition exceptn.cpp:77
ErrorType error_type() const noexcept override
Definition exceptn.h:140
Invalid_Authentication_Tag(std::string_view msg)
Definition exceptn.cpp:132
ErrorType error_type() const noexcept override
Definition exceptn.h:278
ErrorType error_type() const noexcept override
Definition exceptn.h:168
Invalid_IV_Length(std::string_view mode, size_t bad_len)
Definition exceptn.cpp:107
Invalid_Key_Length(std::string_view name, size_t length)
Definition exceptn.cpp:104
ErrorType error_type() const noexcept override
Definition exceptn.h:158
ErrorType error_type() const noexcept override
Definition exceptn.h:211
Invalid_State(std::string_view err)
Definition exceptn.h:209
Key_Not_Set(std::string_view algo)
Definition exceptn.cpp:110
ErrorType error_type() const noexcept override
Definition exceptn.h:230
Lookup_Error(std::string_view err)
Definition exceptn.h:238
ErrorType error_type() const noexcept override
Definition exceptn.h:242
Not_Implemented(std::string_view err)
Definition exceptn.cpp:140
ErrorType error_type() const noexcept override
Definition exceptn.h:339
PRNG_Unseeded(std::string_view algo)
Definition exceptn.cpp:112
Provider_Not_Found(std::string_view algo, std::string_view provider)
Definition exceptn.cpp:117
Stream_IO_Error(std::string_view err)
Definition exceptn.cpp:135
ErrorType error_type() const noexcept override
Definition exceptn.h:293
System_Error(std::string_view msg)
Definition exceptn.h:307
ErrorType error_type() const noexcept override
Definition exceptn.h:311
int error_code() const noexcept override
Definition exceptn.h:313
Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name)
Definition exceptn.cpp:101
void do_throw_error(const char *file, int line, const char *func, Args... args)
Definition exceptn.h:343
Invalid_Authentication_Tag Integrity_Failure
Definition exceptn.h:284
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13
ErrorType
Definition exceptn.h:21