Coverage Report

Created: 2026-12-01 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Botan-3.13.0/build/include/public/botan/exceptn.h
Line
Count
Source
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
16
namespace Botan {
17
18
/**
19
* Different types of errors that might occur
20
*/
21
enum class ErrorType : uint16_t {
22
   /** Some unknown error */
23
   Unknown = 1,
24
   /** An error while calling a system interface */
25
   SystemError = 2,
26
   /** An operation seems valid, but not supported by the current version */
27
   NotImplemented = 3,
28
   /** Memory allocation failure */
29
   OutOfMemory = 4,
30
   /** An internal error occurred */
31
   InternalError = 5,
32
   /** An I/O error occurred */
33
   IoError = 6,
34
35
   /** Invalid object state */
36
   InvalidObjectState = 100,
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 */
40
   InvalidArgument = 102,
41
   /** A key with invalid length was provided */
42
   InvalidKeyLength = 103,
43
   /** A nonce with invalid length was provided */
44
   InvalidNonceLength = 104,
45
   /** An object type was requested but cannot be found */
46
   LookupError = 105,
47
   /** Encoding a message or datum failed */
48
   EncodingFailure = 106,
49
   /** Decoding a message or datum failed */
50
   DecodingFailure = 107,
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 */
56
   InvalidTag = 110,
57
   /** An error during Roughtime validation */
58
   RoughtimeError = 111,
59
60
   /** An error when interacting with CommonCrypto API */
61
   CommonCryptoError = 201,
62
   /** An error when interacting with a PKCS11 device */
63
   Pkcs11Error = 202,
64
   /** An error when interacting with a TPM device */
65
   TPMError = 203,
66
   /** An error when interacting with a database */
67
   DatabaseError = 204,
68
69
   /** An error when interacting with zlib */
70
   ZlibError = 300,
71
   /** An error when interacting with bzip2 */
72
   Bzip2Error = 301,
73
   /** An error when interacting with lzma */
74
   LzmaError = 302,
75
76
};
77
78
//! \brief Convert an ErrorType to string
79
std::string BOTAN_PUBLIC_API(2, 11) to_string(ErrorType type);
80
81
/**
82
* Base class for all exceptions thrown by the library
83
*/
84
class 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
17.5k
      const char* what() const noexcept override { return m_msg.c_str(); }
95
96
      /**
97
      * Return the "type" of error which occurred.
98
      */
99
0
      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
0
      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
*/
132
class BOTAN_PUBLIC_API(2, 0) Invalid_Argument : public Exception {
133
   public:
134
      /**
135
      * Create a Invalid_Argument exception
136
      * @param msg a description of the problem
137
      */
138
      explicit Invalid_Argument(std::string_view msg);
139
140
      /**
141
      * Create a Invalid_Argument exception
142
      * @param msg a description of the problem
143
      * @param where the API call which received the invalid argument
144
      */
145
      explicit Invalid_Argument(std::string_view msg, std::string_view where);
146
147
      /**
148
      * Create a Invalid_Argument exception
149
      * @param msg a description of the problem
150
      * @param e the exception which caused this one
151
      */
152
      Invalid_Argument(std::string_view msg, const std::exception& e);
153
154
      /**
155
      * Return the error type of this exception
156
      * @return the error type of this exception
157
      */
158
2.88k
      ErrorType error_type() const noexcept override { return ErrorType::InvalidArgument; }
159
};
160
161
/**
162
* An invalid/unknown field name was passed to Public_Key::get_int_field
163
*/
164
class BOTAN_PUBLIC_API(3, 0) Unknown_PK_Field_Name final : public Invalid_Argument {
165
   public:
166
      /**
167
      * Create a Unknown_PK_Field_Name exception
168
      * @param algo_name the name of the key algorithm
169
      * @param field_name the unknown field which was requested
170
      */
171
      Unknown_PK_Field_Name(std::string_view algo_name, std::string_view field_name);
172
};
173
174
/**
175
* An invalid key length was used
176
*/
177
class BOTAN_PUBLIC_API(2, 0) Invalid_Key_Length final : public Invalid_Argument {
178
   public:
179
      /**
180
      * Create a Invalid_Key_Length exception
181
      * @param name the name of the algorithm which rejected the key
182
      * @param length the invalid key length in bytes
183
      */
184
      Invalid_Key_Length(std::string_view name, size_t length);
185
186
      /**
187
      * Return the error type of this exception
188
      * @return the error type of this exception
189
      */
190
0
      ErrorType error_type() const noexcept override { return ErrorType::InvalidKeyLength; }
191
};
192
193
/**
194
* An invalid nonce length was used
195
*/
196
class BOTAN_PUBLIC_API(2, 0) Invalid_IV_Length final : public Invalid_Argument {
197
   public:
198
      /**
199
      * Create a Invalid_IV_Length exception
200
      * @param mode the name of the mode which rejected the nonce
201
      * @param bad_len the invalid nonce length in bytes
202
      */
203
      Invalid_IV_Length(std::string_view mode, size_t bad_len);
204
205
      /**
206
      * Return the error type of this exception
207
      * @return the error type of this exception
208
      */
209
0
      ErrorType error_type() const noexcept override { return ErrorType::InvalidNonceLength; }
210
};
211
212
/**
213
* Invalid_Algorithm_Name Exception
214
*/
215
class BOTAN_PUBLIC_API(2, 0) Invalid_Algorithm_Name final : public Invalid_Argument {
216
   public:
217
      /**
218
      * Create a Invalid_Algorithm_Name exception
219
      * @param name the algorithm name which could not be parsed
220
      */
221
      explicit Invalid_Algorithm_Name(std::string_view name);
222
};
223
224
/**
225
* Encoding_Error Exception
226
*/
227
class BOTAN_PUBLIC_API(2, 0) Encoding_Error final : public Exception {
228
   public:
229
      /**
230
      * Create a Encoding_Error exception
231
      * @param name a description of the encoding which failed
232
      */
233
      explicit Encoding_Error(std::string_view name);
234
235
      /**
236
      * Return the error type of this exception
237
      * @return the error type of this exception
238
      */
239
0
      ErrorType error_type() const noexcept override { return ErrorType::EncodingFailure; }
240
};
241
242
/**
243
* A decoding error occurred.
244
*/
245
class BOTAN_PUBLIC_API(2, 0) Decoding_Error : public Exception {
246
   public:
247
      /**
248
      * Create a Decoding_Error exception
249
      * @param name a description of the decoding which failed
250
      */
251
      explicit Decoding_Error(std::string_view name);
252
253
      /**
254
      * Create a Decoding_Error exception
255
      * @param category the kind of object being decoded
256
      * @param err a description of the problem
257
      */
258
      Decoding_Error(std::string_view category, std::string_view err);
259
260
      /**
261
      * Create a Decoding_Error exception
262
      * @param msg a description of the problem
263
      * @param e the exception which caused this one
264
      */
265
      Decoding_Error(std::string_view msg, const std::exception& e);
266
267
      /**
268
      * Return the error type of this exception
269
      * @return the error type of this exception
270
      */
271
14.6k
      ErrorType error_type() const noexcept override { return ErrorType::DecodingFailure; }
272
};
273
274
/**
275
* Invalid state was encountered. A request was made on an object while the
276
* object was in a state where the operation cannot be performed.
277
*/
278
class BOTAN_PUBLIC_API(2, 0) Invalid_State : public Exception {
279
   public:
280
      /**
281
      * Create a Invalid_State exception
282
      * @param err a description of the invalid state
283
      */
284
0
      explicit Invalid_State(std::string_view err) : Exception(err) {}
285
286
      /**
287
      * Return the error type of this exception
288
      * @return the error type of this exception
289
      */
290
0
      ErrorType error_type() const noexcept override { return ErrorType::InvalidObjectState; }
291
};
292
293
/**
294
* A PRNG was called on to produce output while still unseeded
295
*/
296
class BOTAN_PUBLIC_API(2, 0) PRNG_Unseeded final : public Invalid_State {
297
   public:
298
      /**
299
      * Create a PRNG_Unseeded exception
300
      * @param algo the name of the unseeded PRNG
301
      */
302
      explicit PRNG_Unseeded(std::string_view algo);
303
};
304
305
/**
306
* The key was not set on an object. This occurs with symmetric objects where
307
* an operation which requires the key is called prior to set_key being called.
308
*/
309
class BOTAN_PUBLIC_API(2, 4) Key_Not_Set : public Invalid_State {
310
   public:
311
      /**
312
      * Create a Key_Not_Set exception
313
      * @param algo the name of the algorithm whose key was not set
314
      */
315
      explicit Key_Not_Set(std::string_view algo);
316
317
      /**
318
      * Return the error type of this exception
319
      * @return the error type of this exception
320
      */
321
0
      ErrorType error_type() const noexcept override { return ErrorType::KeyNotSet; }
322
};
323
324
/**
325
* A request was made for some kind of object which could not be located
326
*/
327
class BOTAN_PUBLIC_API(2, 0) Lookup_Error : public Exception {
328
   public:
329
      /**
330
      * Create a Lookup_Error exception
331
      * @param err a description of the object which was not found
332
      */
333
0
      explicit Lookup_Error(std::string_view err) : Exception(err) {}
334
335
      /**
336
      * Create a Lookup_Error exception
337
      * @param type the kind of object which was requested
338
      * @param algo the algorithm name which was requested
339
      * @param provider the provider which was requested, if any
340
      */
341
      Lookup_Error(std::string_view type, std::string_view algo, std::string_view provider = "");
342
343
      /**
344
      * Return the error type of this exception
345
      * @return the error type of this exception
346
      */
347
0
      ErrorType error_type() const noexcept override { return ErrorType::LookupError; }
348
};
349
350
/**
351
* Algorithm_Not_Found Exception
352
*
353
* @warning This exception type will be removed in the future. Instead
354
* just catch Lookup_Error.
355
*/
356
class BOTAN_PUBLIC_API(2, 0) Algorithm_Not_Found final : public Lookup_Error {
357
   public:
358
      /**
359
      * Create a Algorithm_Not_Found exception
360
      * @param name the algorithm which was not found
361
      */
362
      explicit Algorithm_Not_Found(std::string_view name);
363
};
364
365
/**
366
* Provider_Not_Found is thrown when a specific provider was requested
367
* but that provider is not available.
368
*
369
* @warning This exception type will be removed in the future. Instead
370
* just catch Lookup_Error.
371
*/
372
class BOTAN_PUBLIC_API(2, 0) Provider_Not_Found final : public Lookup_Error {
373
   public:
374
      /**
375
      * Create a Provider_Not_Found exception
376
      * @param algo the algorithm which was requested
377
      * @param provider the provider which was not available
378
      */
379
      Provider_Not_Found(std::string_view algo, std::string_view provider);
380
};
381
382
/**
383
* An AEAD or MAC check detected a message modification
384
*
385
* In versions before 2.10, Invalid_Authentication_Tag was named
386
* Integrity_Failure, it was renamed to make its usage more clear.
387
*/
388
class BOTAN_PUBLIC_API(2, 0) Invalid_Authentication_Tag final : public Exception {
389
   public:
390
      /**
391
      * Create a Invalid_Authentication_Tag exception
392
      * @param msg a description of the failure
393
      */
394
      explicit Invalid_Authentication_Tag(std::string_view msg);
395
396
      /**
397
      * Return the error type of this exception
398
      * @return the error type of this exception
399
      */
400
0
      ErrorType error_type() const noexcept override { return ErrorType::InvalidTag; }
401
};
402
403
/**
404
* For compatibility with older versions
405
*/
406
typedef Invalid_Authentication_Tag Integrity_Failure;
407
408
/**
409
* An error occurred while operating on an IO stream
410
*/
411
class BOTAN_PUBLIC_API(2, 0) Stream_IO_Error final : public Exception {
412
   public:
413
      /**
414
      * Create a Stream_IO_Error exception
415
      * @param err a description of the IO failure
416
      */
417
      explicit Stream_IO_Error(std::string_view err);
418
419
      /**
420
      * Return the error type of this exception
421
      * @return the error type of this exception
422
      */
423
0
      ErrorType error_type() const noexcept override { return ErrorType::IoError; }
424
};
425
426
/**
427
* System_Error
428
*
429
* This exception is thrown in the event of an error related to interacting
430
* with the operating system.
431
*
432
* This exception type also (optionally) captures an integer error code eg
433
* POSIX errno or Windows GetLastError.
434
*/
435
class BOTAN_PUBLIC_API(2, 9) System_Error : public Exception {
436
   public:
437
      /**
438
      * Create a System_Error exception
439
      * @param msg a description of the problem
440
      */
441
0
      explicit System_Error(std::string_view msg) : Exception(msg), m_error_code(0) {}
442
443
      /**
444
      * Create a System_Error exception
445
      * @param msg a description of the problem
446
      * @param err_code the operating system error code
447
      */
448
      System_Error(std::string_view msg, int err_code);
449
450
      /**
451
      * Return the error type of this exception
452
      * @return the error type of this exception
453
      */
454
0
      ErrorType error_type() const noexcept override { return ErrorType::SystemError; }
455
456
      /**
457
      * Return the operating system error code associated with this exception
458
      * @return the operating system error code captured at construction
459
      */
460
0
      int error_code() const noexcept override { return m_error_code; }
461
462
   private:
463
      int m_error_code;
464
};
465
466
/**
467
* An internal error occurred. If observed, please file a bug.
468
*/
469
class BOTAN_PUBLIC_API(2, 0) Internal_Error : public Exception {
470
   public:
471
      /**
472
      * Create a Internal_Error exception
473
      * @param err a description of the internal error
474
      */
475
      explicit Internal_Error(std::string_view err);
476
477
      /**
478
      * Return the error type of this exception
479
      * @return the error type of this exception
480
      */
481
0
      ErrorType error_type() const noexcept override { return ErrorType::InternalError; }
482
};
483
484
/**
485
* Not Implemented Exception
486
*
487
* This is thrown in the situation where a requested operation is
488
* logically valid but is not implemented by this version of the library.
489
*/
490
class BOTAN_PUBLIC_API(2, 0) Not_Implemented final : public Exception {
491
   public:
492
      /**
493
      * Create a Not_Implemented exception
494
      * @param err a description of the unimplemented operation
495
      */
496
      explicit Not_Implemented(std::string_view err);
497
498
      /**
499
      * Return the error type of this exception
500
      * @return the error type of this exception
501
      */
502
0
      ErrorType error_type() const noexcept override { return ErrorType::NotImplemented; }
503
};
504
505
/**
506
* Throw an exception of type E, prefixing the message with the source location
507
*
508
* @param file the source file name
509
* @param line the source line number
510
* @param func the enclosing function name
511
* @param args the remaining arguments forwarded to E's constructor
512
*/
513
template <typename E, typename... Args>
514
inline void do_throw_error(const char* file, int line, const char* func, Args... args) {
515
   throw E(file, line, func, args...);
516
}
517
518
}  // namespace Botan
519
520
#endif