Botan  1.11.34
Crypto and TLS for C++11
exceptn.h
Go to the documentation of this file.
1 /*
2 * Exceptions
3 * (C) 1999-2009 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 <botan/parsing.h>
13 #include <exception>
14 #include <string>
15 
16 namespace Botan {
17 
18 /**
19 * Base class for all exceptions thrown by the library
20 */
21 class BOTAN_DLL Exception : public std::exception
22  {
23  public:
24  explicit Exception(const std::string& msg) : m_msg(msg) {}
25  Exception(const char* prefix, const std::string& msg) : m_msg(std::string(prefix) + " " + msg) {}
26  const char* what() const BOTAN_NOEXCEPT override { return m_msg.c_str(); }
27  private:
28  std::string m_msg;
29  };
30 
31 /**
32 * An invalid argument
33 */
34 class BOTAN_DLL Invalid_Argument : public Exception
35  {
36  public:
37  explicit Invalid_Argument(const std::string& msg) :
38  Exception("Invalid argument", msg) {}
39 
40  explicit Invalid_Argument(const std::string& msg, const std::string& where) :
41  Exception("Invalid argument", msg + " in " + where) {}
42 };
43 
44 #define BOTAN_ARG_CHECK(expr) \
45  do { if(!(expr)) throw Invalid_Argument(#expr, BOTAN_CURRENT_FUNCTION); } while(0)
46 
47 /**
48 * Unsupported_Argument Exception
49 *
50 * An argument that is invalid because it is not supported by Botan.
51 * It might or might not be valid in another context like a standard.
52 */
53 struct BOTAN_DLL Unsupported_Argument : public Invalid_Argument
54  {
55  explicit Unsupported_Argument(const std::string& msg) : Invalid_Argument(msg) {}
56  };
57 
58 /**
59 * Invalid_State Exception
60 */
61 struct BOTAN_DLL Invalid_State : public Exception
62  {
63  explicit Invalid_State(const std::string& err) :
64  Exception(err)
65  {}
66  };
67 
68 /**
69 * Lookup_Error Exception
70 */
71 struct BOTAN_DLL Lookup_Error : public Exception
72  {
73  explicit Lookup_Error(const std::string& err) :
74  Exception(err)
75  {}
76 
77  Lookup_Error(const std::string& type,
78  const std::string& algo,
79  const std::string& provider) :
80  Exception("Unavailable " + type + " " + algo +
81  (provider.empty() ? std::string("") : (" for provider " + provider)))
82  {}
83  };
84 
85 /**
86 * Internal_Error Exception
87 */
88 struct BOTAN_DLL Internal_Error : public Exception
89  {
90  explicit Internal_Error(const std::string& err) :
91  Exception("Internal error: " + err)
92  {}
93  };
94 
95 /**
96 * Invalid_Key_Length Exception
97 */
98 struct BOTAN_DLL Invalid_Key_Length : public Invalid_Argument
99  {
100  Invalid_Key_Length(const std::string& name, size_t length) :
101  Invalid_Argument(name + " cannot accept a key of length " +
102  std::to_string(length))
103  {}
104  };
105 
106 /**
107 * Invalid_IV_Length Exception
108 */
109 struct BOTAN_DLL Invalid_IV_Length : public Invalid_Argument
110  {
111  Invalid_IV_Length(const std::string& mode, size_t bad_len) :
112  Invalid_Argument("IV length " + std::to_string(bad_len) +
113  " is invalid for " + mode)
114  {}
115  };
116 
117 /**
118 * PRNG_Unseeded Exception
119 */
120 struct BOTAN_DLL PRNG_Unseeded : public Invalid_State
121  {
122  explicit PRNG_Unseeded(const std::string& algo) :
123  Invalid_State("PRNG not seeded: " + algo)
124  {}
125  };
126 
127 /**
128 * Policy_Violation Exception
129 */
130 struct BOTAN_DLL Policy_Violation : public Invalid_State
131  {
132  explicit Policy_Violation(const std::string& err) :
133  Invalid_State("Policy violation: " + err)
134  {}
135  };
136 
137 /**
138 * Algorithm_Not_Found Exception
139 */
140 struct BOTAN_DLL Algorithm_Not_Found : public Lookup_Error
141  {
142  explicit Algorithm_Not_Found(const std::string& name) :
143  Lookup_Error("Could not find any algorithm named \"" + name + "\"")
144  {}
145  };
146 
147 /**
148 * No_Provider_Found Exception
149 */
150 struct BOTAN_DLL No_Provider_Found : public Exception
151  {
152  explicit No_Provider_Found(const std::string& name) :
153  Exception("Could not find any provider for algorithm named \"" + name + "\"")
154  {}
155  };
156 
157 /**
158 * Provider_Not_Found is thrown when a specific provider was requested
159 * but that provider is not available.
160 */
161 struct BOTAN_DLL Provider_Not_Found : public Lookup_Error
162  {
163  Provider_Not_Found(const std::string& algo, const std::string& provider) :
164  Lookup_Error("Could not find provider '" + provider + "' for " + algo) {}
165  };
166 
167 /**
168 * Invalid_Algorithm_Name Exception
169 */
170 struct BOTAN_DLL Invalid_Algorithm_Name : public Invalid_Argument
171  {
172  explicit Invalid_Algorithm_Name(const std::string& name):
173  Invalid_Argument("Invalid algorithm name: " + name)
174  {}
175  };
176 
177 /**
178 * Encoding_Error Exception
179 */
180 struct BOTAN_DLL Encoding_Error : public Invalid_Argument
181  {
182  explicit Encoding_Error(const std::string& name) :
183  Invalid_Argument("Encoding error: " + name) {}
184  };
185 
186 /**
187 * Decoding_Error Exception
188 */
189 struct BOTAN_DLL Decoding_Error : public Invalid_Argument
190  {
191  explicit Decoding_Error(const std::string& name) :
192  Invalid_Argument("Decoding error: " + name) {}
193  };
194 
195 /**
196 * Integrity_Failure Exception
197 */
198 struct BOTAN_DLL Integrity_Failure : public Exception
199  {
200  explicit Integrity_Failure(const std::string& msg) :
201  Exception("Integrity failure: " + msg) {}
202  };
203 
204 /**
205 * Invalid_OID Exception
206 */
207 struct BOTAN_DLL Invalid_OID : public Decoding_Error
208  {
209  explicit Invalid_OID(const std::string& oid) :
210  Decoding_Error("Invalid ASN.1 OID: " + oid) {}
211  };
212 
213 /**
214 * Stream_IO_Error Exception
215 */
216 struct BOTAN_DLL Stream_IO_Error : public Exception
217  {
218  explicit Stream_IO_Error(const std::string& err) :
219  Exception("I/O error: " + err)
220  {}
221  };
222 
223 /**
224 * No_Filesystem_Access Exception
225 */
226 struct BOTAN_DLL No_Filesystem_Access : public Exception
227  {
228  No_Filesystem_Access() : Exception("No filesystem access enabled.") {}
229  };
230 
231 /**
232 * Self Test Failure Exception
233 */
234 struct BOTAN_DLL Self_Test_Failure : public Internal_Error
235  {
236  explicit Self_Test_Failure(const std::string& err) :
237  Internal_Error("Self test failed: " + err)
238  {}
239  };
240 
241 /**
242 * Not Implemented Exception
243 */
244 struct BOTAN_DLL Not_Implemented : public Exception
245  {
246  explicit Not_Implemented(const std::string& err) :
247  Exception("Not implemented", err)
248  {}
249  };
250 
251 }
252 
253 #endif
Provider_Not_Found(const std::string &algo, const std::string &provider)
Definition: exceptn.h:163
Invalid_IV_Length(const std::string &mode, size_t bad_len)
Definition: exceptn.h:111
std::string to_string(const secure_vector< byte > &bytes)
Definition: stl_util.h:25
Invalid_Key_Length(const std::string &name, size_t length)
Definition: exceptn.h:100
Exception(const std::string &msg)
Definition: exceptn.h:24
Invalid_Argument(const std::string &msg)
Definition: exceptn.h:37
Algorithm_Not_Found(const std::string &name)
Definition: exceptn.h:142
Lookup_Error(const std::string &type, const std::string &algo, const std::string &provider)
Definition: exceptn.h:77
PRNG_Unseeded(const std::string &algo)
Definition: exceptn.h:122
Stream_IO_Error(const std::string &err)
Definition: exceptn.h:218
Definition: bigint.h:619
Exception(const char *prefix, const std::string &msg)
Definition: exceptn.h:25
MechanismType type
#define BOTAN_NOEXCEPT
Definition: compiler.h:108
No_Provider_Found(const std::string &name)
Definition: exceptn.h:152
Internal_Error(const std::string &err)
Definition: exceptn.h:90
Not_Implemented(const std::string &err)
Definition: exceptn.h:246
Integrity_Failure(const std::string &msg)
Definition: exceptn.h:200
Decoding_Error(const std::string &name)
Definition: exceptn.h:191
Unsupported_Argument(const std::string &msg)
Definition: exceptn.h:55
Invalid_Algorithm_Name(const std::string &name)
Definition: exceptn.h:172
Self_Test_Failure(const std::string &err)
Definition: exceptn.h:236
const char * what() const BOTAN_NOEXCEPT override
Definition: exceptn.h:26
Definition: alg_id.cpp:13
Invalid_State(const std::string &err)
Definition: exceptn.h:63
Invalid_Argument(const std::string &msg, const std::string &where)
Definition: exceptn.h:40
Lookup_Error(const std::string &err)
Definition: exceptn.h:73
Policy_Violation(const std::string &err)
Definition: exceptn.h:132
Encoding_Error(const std::string &name)
Definition: exceptn.h:182
Invalid_OID(const std::string &oid)
Definition: exceptn.h:209