From 93803ac591d0e0e0d05d5a552cd2d5fb4a62fb19 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 23 Jul 2016 17:18:20 +0200 Subject: Handle errors with exceptions Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 120 +++++++++++++++++++++++++++++++++++++++++++++++++++++------- NK_C_API.h | 3 +- 2 files changed, 108 insertions(+), 15 deletions(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index 1695c92..440e824 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -3,41 +3,98 @@ using namespace nitrokey; +static uint8_t NK_last_command_status = 0; + extern "C" { +extern uint8_t NK_get_last_command_status(){ + auto _copy = NK_last_command_status; + NK_last_command_status = 0; + return _copy; +} + extern int NK_login(const char *pin, const char *temporary_password) { auto m = NitrokeyManager::instance(); - return m->connect() && m->authorize(pin, temporary_password); + try { + m->connect(); + m->authorize(pin, temporary_password); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } extern int NK_logout() { auto m = NitrokeyManager::instance(); - return m->disconnect(); + try { + m->disconnect(); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } + extern const char * NK_status() { auto m = NitrokeyManager::instance(); - string s = m->get_status(); - return strdup(s.c_str()); + try { + string s = m->get_status(); + return strdup(s.c_str()); //FIXME leak? + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + return ""; } extern uint32_t NK_get_hotp_code(uint8_t slot_number){ auto m = NitrokeyManager::instance(); - return m->get_HOTP_code(slot_number); + try { + return m->get_HOTP_code(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + return 0; } extern uint32_t NK_get_totp_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, uint8_t last_interval){ auto m = NitrokeyManager::instance(); - return m->get_TOTP_code(slot_number, challenge, last_totp_time, last_interval); + try { + return m->get_TOTP_code(slot_number, challenge, last_totp_time, last_interval); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + } + return 0; } extern int NK_erase_hotp_slot(uint8_t slot_number) { auto m = NitrokeyManager::instance(); - return m->erase_hotp_slot(slot_number); + try { + m->erase_hotp_slot(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } + extern int NK_erase_totp_slot(uint8_t slot_number) { auto m = NitrokeyManager::instance(); - return m->erase_totp_slot(slot_number); + try { + m->erase_totp_slot(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint8_t hotp_counter, @@ -47,6 +104,7 @@ extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const m->write_HOTP_slot(slot_number, slot_name, secret, hotp_counter, temporary_password); } catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; return commandFailedException.last_command_status; } return 0; @@ -55,16 +113,35 @@ extern int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const extern int NK_write_totp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint16_t time_window, bool use_8_digits, const char *temporary_password) { auto m = NitrokeyManager::instance(); - return m->write_TOTP_slot(slot_number, slot_name, secret, time_window, use_8_digits, temporary_password); + try { + m->write_TOTP_slot(slot_number, slot_name, secret, time_window, use_8_digits, temporary_password); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } extern const char* NK_get_totp_slot_name(uint8_t slot_number){ auto m = NitrokeyManager::instance(); - return m->get_totp_slot_name(slot_number); + try { + return m->get_totp_slot_name(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return ""; + } } extern const char* NK_get_hotp_slot_name(uint8_t slot_number){ auto m = NitrokeyManager::instance(); - return m->get_hotp_slot_name(slot_number); + try { + return m->get_hotp_slot_name(slot_number); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return ""; + } } extern void NK_set_debug(bool state){ @@ -74,11 +151,26 @@ extern void NK_set_debug(bool state){ extern int NK_totp_set_time(uint64_t time){ auto m = NitrokeyManager::instance(); - return m->set_time(time); + try { + m->set_time(time); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } -extern int NK_totp_mark_time(){ + +extern int NK_totp_get_time(){ auto m = NitrokeyManager::instance(); - return m->get_time(); + try { + m->get_time(); + } + catch (CommandFailedException & commandFailedException){ + NK_last_command_status = commandFailedException.last_command_status; + return commandFailedException.last_command_status; + } + return 0; } } \ No newline at end of file diff --git a/NK_C_API.h b/NK_C_API.h index e0d0ede..0c91095 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -21,7 +21,8 @@ extern int NK_write_totp_slot(uint8_t slot_number, const char *slot_name, const extern uint32_t NK_get_hotp_code(uint8_t slot_number); extern uint32_t NK_get_totp_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, uint8_t last_interval); extern int NK_totp_set_time(uint64_t time); -extern int NK_totp_mark_time(); +extern int NK_totp_get_time(); +extern uint8_t NK_get_last_command_status(); } -- cgit v1.2.3