Debian Codegraph @

src/device_trezor/trezor/exceptions.hpp

raw symbols SCIP
1 // Copyright (c) 2017-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 //
29
30 #ifndef MONERO_EXCEPTIONS_H
31 #define MONERO_EXCEPTIONS_H
32
33 #include <exception>
34 #include <string>
35 #include <boost/optional.hpp>
36
37 namespace hw {
38 namespace trezor {
39 namespace exc {
40
41 class SecurityException : public std::exception {
42 protected:
43 boost::optional<std::string> reason;
44
45 public:
46 SecurityException(): reason("General Security exception"){}
47 explicit SecurityException(std::string what): reason(what){}
48
49 virtual const char* what() const throw() {
50 return reason.get().c_str();
51 }
52 };
53
54 class Poly1305TagInvalid: public SecurityException {
55 public:
56 using SecurityException::SecurityException;
57 Poly1305TagInvalid(): SecurityException("Poly1305 authentication tag invalid"){}
58 };
59
60 class TrezorException : public std::exception {
61 protected:
62 boost::optional<std::string> reason;
63
64 public:
65 TrezorException(): reason("General Trezor exception"){}
66 explicit TrezorException(std::string what): reason(what){}
67
68 virtual const char* what() const throw() {
69 return reason.get().c_str();
70 }
71 };
72
73 class CommunicationException: public TrezorException {
74 public:
75 using TrezorException::TrezorException;
76 CommunicationException(): TrezorException("Trezor communication error"){}
77 };
78
79 class EncodingException: public CommunicationException {
80 public:
81 using CommunicationException::CommunicationException;
82 EncodingException(): CommunicationException("Trezor message encoding error"){}
83 };
84
85 class NotConnectedException : public CommunicationException {
86 public:
87 using CommunicationException::CommunicationException;
88 NotConnectedException(): CommunicationException("Trezor not connected"){}
89 };
90
91 class DeviceNotResponsiveException : public CommunicationException {
92 public:
93 using CommunicationException::CommunicationException;
94 DeviceNotResponsiveException(): CommunicationException("Trezor does not respond to ping"){}
95 };
96
97 class DeviceAcquireException : public CommunicationException {
98 public:
99 using CommunicationException::CommunicationException;
100 DeviceAcquireException(): CommunicationException("Trezor could not be acquired"){}
101 };
102
103 class SessionException: public CommunicationException {
104 public:
105 using CommunicationException::CommunicationException;
106 SessionException(): CommunicationException("Trezor session expired"){}
107 };
108
109 class TimeoutException: public CommunicationException {
110 public:
111 using CommunicationException::CommunicationException;
112 TimeoutException(): CommunicationException("Trezor communication timeout"){}
113 };
114
115 class ProtocolException: public CommunicationException {
116 public:
117 using CommunicationException::CommunicationException;
118 ProtocolException(): CommunicationException("Trezor protocol error"){}
119 };
120
121 // Communication protocol namespace
122 // Separated to distinguish between client and Trezor side exceptions.
123 namespace proto {
124
125 class SecurityException : public ProtocolException {
126 public:
127 using ProtocolException::ProtocolException;
128 SecurityException(): ProtocolException("Security assertion violated in the protocol"){}
129 };
130
131 class FailureException : public ProtocolException {
132 private:
133 boost::optional<uint32_t> code;
134 boost::optional<std::string> message;
135 public:
136 using ProtocolException::ProtocolException;
137 FailureException(): ProtocolException("Trezor returned failure"){}
138 FailureException(boost::optional<uint32_t> code,
139 boost::optional<std::string> message)
140 : code(code), message(message) {
141 reason = "Trezor returned failure: code="
142 + (code ? std::to_string(code.get()) : "")
143 + ", message=" + (message ? message.get() : "");
144 };
145 };
146
147 class UnexpectedMessageException : public FailureException {
148 public:
149 using FailureException::FailureException;
150 UnexpectedMessageException(): FailureException("Trezor claims unexpected message received"){}
151 };
152
153 class CancelledException : public FailureException {
154 public:
155 using FailureException::FailureException;
156 CancelledException(): FailureException("Trezor returned: cancelled operation"){}
157 };
158
159 class PinExpectedException : public FailureException {
160 public:
161 using FailureException::FailureException;
162 PinExpectedException(): FailureException("Trezor claims PIN is expected"){}
163 };
164
165 class InvalidPinException : public FailureException {
166 public:
167 using FailureException::FailureException;
168 InvalidPinException(): FailureException("Trezor claims PIN is invalid"){}
169 };
170
171 class NotEnoughFundsException : public FailureException {
172 public:
173 using FailureException::FailureException;
174 NotEnoughFundsException(): FailureException("Trezor claims not enough funds"){}
175 };
176
177 class NotInitializedException : public FailureException {
178 public:
179 using FailureException::FailureException;
180 NotInitializedException(): FailureException("Trezor claims not initialized"){}
181 };
182
183 class FirmwareErrorException : public FailureException {
184 public:
185 using FailureException::FailureException;
186 FirmwareErrorException(): FailureException("Trezor returned firmware error"){}
187 };
188
189 }
190 }
191 }
192 }
193 #endif //MONERO_EXCEPTIONS_H