summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2016-07-23 15:03:42 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2016-08-01 13:54:56 +0200
commit49ea52c45eee26be51df4969ca2af8c2b7c9ceb6 (patch)
tree91c0eb42eeda7286c8b5e83041b71e763f183d2e
parent7eb15813ffa2c2766e53245311b017c3b9c196fb (diff)
downloadlibnitrokey-49ea52c45eee26be51df4969ca2af8c2b7c9ceb6.tar.gz
libnitrokey-49ea52c45eee26be51df4969ca2af8c2b7c9ceb6.tar.bz2
Handling device errors through exceptions
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--CMakeLists.txt2
-rw-r--r--NK_C_API.cc8
-rw-r--r--NitrokeyManager.cc4
-rw-r--r--build/test.py1
-rw-r--r--include/CommandFailedException.cpp5
-rw-r--r--include/CommandFailedException.h27
-rw-r--r--include/device_proto.h2
7 files changed, 44 insertions, 5 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 62e8f15..37fac0f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -22,6 +22,6 @@ set(SOURCE_FILES
log.cc
misc.cc
NitrokeyManager.cc
- NK_C_API.cc)
+ NK_C_API.cc include/CommandFailedException.cpp include/CommandFailedException.h)
add_executable(libnitrokey ${SOURCE_FILES}) \ No newline at end of file
diff --git a/NK_C_API.cc b/NK_C_API.cc
index 8af0128..1695c92 100644
--- a/NK_C_API.cc
+++ b/NK_C_API.cc
@@ -43,7 +43,13 @@ extern int NK_erase_totp_slot(uint8_t slot_number) {
extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint8_t hotp_counter,
const char *temporary_password) {
auto m = NitrokeyManager::instance();
- return m->write_HOTP_slot(slot_number, slot_name, secret, hotp_counter, temporary_password);
+ try {
+ m->write_HOTP_slot(slot_number, slot_name, secret, hotp_counter, temporary_password);
+ }
+ catch (CommandFailedException & commandFailedException){
+ return commandFailedException.last_command_status;
+ }
+ return 0;
}
extern int NK_write_totp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint16_t time_window,
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc
index 39f1cff..e2dfa9d 100644
--- a/NitrokeyManager.cc
+++ b/NitrokeyManager.cc
@@ -115,11 +115,9 @@ namespace nitrokey{
strcpy((char *) (auth.temporary_password), temporary_password);
auth.crc_to_authorize = WriteToHOTPSlot::CommandTransaction::getCRC(payload);
Authorize::CommandTransaction::run(*device, auth);
- auto auth_successful = device->last_command_sucessfull();
auto resp = WriteToHOTPSlot::CommandTransaction::run(*device, payload);
- auto write_successful = device->last_command_sucessfull();
- return auth_successful && write_successful;
+ return true;
}
enum totp_config{digits8=0, enter=1, tokenID=2};
diff --git a/build/test.py b/build/test.py
index b3d9ea7..4aaf8cd 100644
--- a/build/test.py
+++ b/build/test.py
@@ -25,6 +25,7 @@ if __name__ == "__main__":
# C.NK_set_debug(False)
a = C.NK_write_hotp_slot(1, 'python_test', '12345678901234567890', 0, 'faketemppass')
+ # a = C.NK_write_hotp_slot(1, 'python_test', '12345678901234567890', 0, '123123123')
print(a)
exit()
diff --git a/include/CommandFailedException.cpp b/include/CommandFailedException.cpp
new file mode 100644
index 0000000..5ba4b0b
--- /dev/null
+++ b/include/CommandFailedException.cpp
@@ -0,0 +1,5 @@
+//
+// Created by sz on 23.07.16.
+//
+
+#include "CommandFailedException.h"
diff --git a/include/CommandFailedException.h b/include/CommandFailedException.h
new file mode 100644
index 0000000..3306f7b
--- /dev/null
+++ b/include/CommandFailedException.h
@@ -0,0 +1,27 @@
+//
+// Created by sz on 23.07.16.
+//
+
+#ifndef LIBNITROKEY_COMMANDFAILEDEXCEPTION_H
+#define LIBNITROKEY_COMMANDFAILEDEXCEPTION_H
+
+#include <exception>
+#include <cstdint>
+
+class CommandFailedException : public std::exception {
+public:
+ uint8_t last_command_code;
+ uint8_t last_command_status;
+
+ CommandFailedException(uint8_t last_command_code, uint8_t last_command_status) :
+ last_command_code(last_command_code),
+ last_command_status(last_command_status){}
+
+ virtual const char *what() const throw() {
+ return "Command execution has failed on device";
+ }
+
+};
+
+
+#endif //LIBNITROKEY_COMMANDFAILEDEXCEPTION_H
diff --git a/include/device_proto.h b/include/device_proto.h
index 79632d9..2bbb38a 100644
--- a/include/device_proto.h
+++ b/include/device_proto.h
@@ -14,6 +14,7 @@
#include "log.h"
#include "command_id.h"
#include "dissect.h"
+#include "CommandFailedException.h"
#define STICK20_UPDATE_MODE_VID 0x03EB
#define STICK20_UPDATE_MODE_PID 0x2FF1
@@ -212,6 +213,7 @@ class Transaction : semantics::non_constructible {
Log::instance()((std::string)(resp), Loglevel::DEBUG);
if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet");
+ if (resp.last_command_status!=0) throw CommandFailedException(resp.command_id, resp.last_command_status);
// See: DeviceResponse
return resp.payload;