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