From 49ea52c45eee26be51df4969ca2af8c2b7c9ceb6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 23 Jul 2016 15:03:42 +0200 Subject: Handling device errors through exceptions Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 2 +- NK_C_API.cc | 8 +++++++- NitrokeyManager.cc | 4 +--- build/test.py | 1 + include/CommandFailedException.cpp | 5 +++++ include/CommandFailedException.h | 27 +++++++++++++++++++++++++++ include/device_proto.h | 2 ++ 7 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 include/CommandFailedException.cpp create mode 100644 include/CommandFailedException.h 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 +#include + +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; -- cgit v1.2.1