From ba4d36c38c0017415129a5674c0f7133e6d76f9d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 9 Aug 2016 15:33:26 +0200 Subject: Return error on invalid slot Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 2 +- InvalidSlotException.h | 27 --------------------------- NK_C_API.cc | 19 +++++++++---------- NitrokeyManager.cc | 32 +++++++++++++++++--------------- include/InvalidSlotException.h | 31 +++++++++++++++++++++++++++++++ include/LibraryException.h | 17 +++++++++++++++++ include/TooLongStringException.h | 10 ++++++---- unittest/test_bindings.py | 16 +++++++++++++--- 8 files changed, 94 insertions(+), 60 deletions(-) delete mode 100644 InvalidSlotException.h create mode 100644 include/InvalidSlotException.h create mode 100644 include/LibraryException.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 3abcb0e..265e737 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,6 +22,6 @@ set(SOURCE_FILES log.cc misc.cc NitrokeyManager.cc - NK_C_API.cc CommandFailedException.cpp include/CommandFailedException.h TooLongStringException.cpp include/TooLongStringException.h InvalidSlotException.h) + NK_C_API.cc CommandFailedException.cpp include/CommandFailedException.h TooLongStringException.cpp include/TooLongStringException.h include/InvalidSlotException.h include/LibraryException.h) add_executable(libnitrokey ${SOURCE_FILES}) \ No newline at end of file diff --git a/InvalidSlotException.h b/InvalidSlotException.h deleted file mode 100644 index 741d53e..0000000 --- a/InvalidSlotException.h +++ /dev/null @@ -1,27 +0,0 @@ -// -// Created by sz on 09.08.16. -// - -#ifndef LIBNITROKEY_INVALIDSLOTEXCEPTION_H -#define LIBNITROKEY_INVALIDSLOTEXCEPTION_H - - -#include -#include -#include - -class InvalidSlotException : public std::exception { -public: - static const std::uint8_t exception_id = 201; - - uint8_t slot_selected; - - InvalidSlotException(uint8_t slot_selected) : slot_selected(slot_selected) {} - - virtual const char *what() const throw() { - return "Wrong slot selected"; - } - -}; - -#endif //LIBNITROKEY_INVALIDSLOTEXCEPTION_H diff --git a/NK_C_API.cc b/NK_C_API.cc index 4ba29ab..ea63f36 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -23,8 +23,8 @@ uint8_t * get_with_array_result(T func){ catch (CommandFailedException & commandFailedException){ NK_last_command_status = commandFailedException.last_command_status; } - catch (TooLongStringException & longStringException){ - NK_last_command_status = TooLongStringException::exception_id; + catch (LibraryException & libraryException){ + NK_last_command_status = libraryException.exception_id(); } return nullptr; } @@ -38,8 +38,8 @@ const char* get_with_string_result(T func){ catch (CommandFailedException & commandFailedException){ NK_last_command_status = commandFailedException.last_command_status; } - catch (TooLongStringException & longStringException){ - NK_last_command_status = TooLongStringException::exception_id; + catch (LibraryException & libraryException){ + NK_last_command_status = libraryException.exception_id(); } return ""; } @@ -53,8 +53,8 @@ auto get_with_result(T func){ catch (CommandFailedException & commandFailedException){ NK_last_command_status = commandFailedException.last_command_status; } - catch (TooLongStringException & longStringException){ - NK_last_command_status = TooLongStringException::exception_id; + catch (LibraryException & libraryException){ + NK_last_command_status = libraryException.exception_id(); } return static_cast(0); } @@ -68,12 +68,11 @@ uint8_t get_without_result(T func){ } catch (CommandFailedException & commandFailedException){ NK_last_command_status = commandFailedException.last_command_status; - return commandFailedException.last_command_status; } - catch (TooLongStringException & longStringException){ - NK_last_command_status = TooLongStringException::exception_id; - return NK_last_command_status; + catch (LibraryException & libraryException){ + NK_last_command_status = libraryException.exception_id(); } + return NK_last_command_status; } extern "C" diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index c27de2b..81d27aa 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -1,14 +1,16 @@ -#include #include #include #include "include/NitrokeyManager.h" #include "include/TooLongStringException.h" +#include "include/InvalidSlotException.h" namespace nitrokey{ template void strcpyT(T& dest, const char* src){ - assert(src != nullptr); + if (src == nullptr) +// throw EmptySourceStringException(slot_number); + return; const size_t s_dest = sizeof dest; if (strlen(src) > s_dest){ throw TooLongStringException(strlen(src), s_dest, src); @@ -93,7 +95,7 @@ namespace nitrokey{ } uint32_t NitrokeyManager::get_HOTP_code(uint8_t slot_number, const char *user_temporary_password) { - assert(is_valid_hotp_slot_number(slot_number)); + if (!is_valid_hotp_slot_number(slot_number)) throw InvalidSlotException(slot_number); auto gh = get_payload(); gh.slot_number = get_internal_slot_number_for_hotp(slot_number); @@ -114,7 +116,7 @@ namespace nitrokey{ uint32_t NitrokeyManager::get_TOTP_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, uint8_t last_interval, const char *user_temporary_password) { - assert(is_valid_totp_slot_number(slot_number)); + if(!is_valid_totp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_totp(slot_number); auto gt = get_payload(); gt.slot_number = slot_number; @@ -140,13 +142,13 @@ namespace nitrokey{ } bool NitrokeyManager::erase_hotp_slot(uint8_t slot_number, const char *temporary_password) { - assert(is_valid_hotp_slot_number(slot_number)); + if (!is_valid_hotp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_hotp(slot_number); return erase_slot(slot_number, temporary_password); } bool NitrokeyManager::erase_totp_slot(uint8_t slot_number, const char *temporary_password) { - assert(is_valid_totp_slot_number(slot_number)); + if (!is_valid_totp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_totp(slot_number); return erase_slot(slot_number, temporary_password); } @@ -155,7 +157,7 @@ namespace nitrokey{ bool NitrokeyManager::write_HOTP_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint8_t hotp_counter, bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID, const char *temporary_password) { - assert(is_valid_hotp_slot_number(slot_number)); + if (!is_valid_hotp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_hotp(slot_number); auto payload = get_payload(); @@ -178,7 +180,7 @@ namespace nitrokey{ bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID, const char *temporary_password) { auto payload = get_payload(); - assert(is_valid_totp_slot_number(slot_number)); + if (!is_valid_totp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_totp(slot_number); payload.slot_number = slot_number; @@ -197,12 +199,12 @@ namespace nitrokey{ } const char * NitrokeyManager::get_totp_slot_name(uint8_t slot_number) { - assert(is_valid_totp_slot_number(slot_number)); + if (!is_valid_totp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_totp(slot_number); return get_slot_name(slot_number); } const char * NitrokeyManager::get_hotp_slot_name(uint8_t slot_number) { - assert(is_valid_hotp_slot_number(slot_number)); + if (!is_valid_hotp_slot_number(slot_number)) throw InvalidSlotException(slot_number); slot_number = get_internal_slot_number_for_hotp(slot_number); return get_slot_name(slot_number); } @@ -307,7 +309,7 @@ namespace nitrokey{ } const char *NitrokeyManager::get_password_safe_slot_name(uint8_t slot_number) { - assert (is_valid_password_safe_slot_number(slot_number)); + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); auto p = get_payload(); p.slot_number = slot_number; auto response = GetPasswordSafeSlotName::CommandTransaction::run(*device, p); @@ -317,7 +319,7 @@ namespace nitrokey{ bool NitrokeyManager::is_valid_password_safe_slot_number(uint8_t slot_number) const { return slot_number < 16; } const char *NitrokeyManager::get_password_safe_slot_login(uint8_t slot_number) { - assert (is_valid_password_safe_slot_number(slot_number)); + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); auto p = get_payload(); p.slot_number = slot_number; auto response = GetPasswordSafeSlotLogin::CommandTransaction::run(*device, p); @@ -325,7 +327,7 @@ namespace nitrokey{ } const char *NitrokeyManager::get_password_safe_slot_password(uint8_t slot_number) { - assert (is_valid_password_safe_slot_number(slot_number)); + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); auto p = get_payload(); p.slot_number = slot_number; auto response = GetPasswordSafeSlotPassword::CommandTransaction::run(*device, p); @@ -334,7 +336,7 @@ namespace nitrokey{ void NitrokeyManager::write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, const char *slot_password) { - assert (is_valid_password_safe_slot_number(slot_number)); + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); auto p = get_payload(); p.slot_number = slot_number; strcpyT(p.slot_name, slot_name); @@ -348,7 +350,7 @@ namespace nitrokey{ } void NitrokeyManager::erase_password_safe_slot(uint8_t slot_number) { - assert (is_valid_password_safe_slot_number(slot_number)); + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); auto p = get_payload(); p.slot_number = slot_number; ErasePasswordSafeSlot::CommandTransaction::run(*device, p); diff --git a/include/InvalidSlotException.h b/include/InvalidSlotException.h new file mode 100644 index 0000000..e97d993 --- /dev/null +++ b/include/InvalidSlotException.h @@ -0,0 +1,31 @@ +// +// Created by sz on 09.08.16. +// + +#ifndef LIBNITROKEY_INVALIDSLOTEXCEPTION_H +#define LIBNITROKEY_INVALIDSLOTEXCEPTION_H + + +#include +#include +#include "LibraryException.h" + + +class InvalidSlotException : public LibraryException { +public: + virtual uint8_t exception_id() override { + return 201; + } + +public: + uint8_t slot_selected; + + InvalidSlotException(uint8_t slot_selected) : slot_selected(slot_selected) {} + + virtual const char *what() const throw() override { + return "Wrong slot selected"; + } + +}; + +#endif //LIBNITROKEY_INVALIDSLOTEXCEPTION_H diff --git a/include/LibraryException.h b/include/LibraryException.h new file mode 100644 index 0000000..ef81624 --- /dev/null +++ b/include/LibraryException.h @@ -0,0 +1,17 @@ +// +// Created by sz on 09.08.16. +// + +#ifndef LIBNITROKEY_LIBRARYEXCEPTION_H +#define LIBNITROKEY_LIBRARYEXCEPTION_H + +#include +#include + +class LibraryException: std::exception { +public: + virtual uint8_t exception_id()= 0; +}; + + +#endif //LIBNITROKEY_LIBRARYEXCEPTION_H diff --git a/include/TooLongStringException.h b/include/TooLongStringException.h index 3a9244b..58f5801 100644 --- a/include/TooLongStringException.h +++ b/include/TooLongStringException.h @@ -8,11 +8,13 @@ #include #include -#include +#include "LibraryException.h" -class TooLongStringException : public std::exception { +class TooLongStringException : public LibraryException { public: - static const std::uint8_t exception_id = 200; + virtual uint8_t exception_id() override { + return 200; + } std::size_t size_source; std::size_t size_destination; @@ -21,7 +23,7 @@ public: TooLongStringException(size_t size_source, size_t size_destination, const std::string &message = "") : size_source( size_source), size_destination(size_destination), message(message) {} - virtual const char *what() const throw() { + virtual const char *what() const throw() override { //TODO add sizes and message data to final message return "Too long string has been supplied as an argument"; } diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py index 9eebcb3..7b16a46 100644 --- a/unittest/test_bindings.py +++ b/unittest/test_bindings.py @@ -210,9 +210,19 @@ def test_too_long_strings(C): assert C.NK_get_last_command_status() == LibraryErrors.TOO_LONG_STRING -# def test_invalid_slot(C): -# invalid_slot = 255 -# assert C.NK_erase_totp_slot(invalid_slot, 'some password') == LibraryErrors.INVALID_SLOT +def test_invalid_slot(C): + invalid_slot = 255 + assert C.NK_erase_totp_slot(invalid_slot, 'some password') == LibraryErrors.INVALID_SLOT + assert C.NK_write_hotp_slot(invalid_slot, 'long_test', RFC_SECRET, 0, False, False, False, "", + 'aaa') == LibraryErrors.INVALID_SLOT + assert C.NK_get_hotp_code_PIN(invalid_slot, 'some password') == 0 + assert C.NK_get_last_command_status() == LibraryErrors.INVALID_SLOT + assert C.NK_erase_password_safe_slot(invalid_slot) == LibraryErrors.INVALID_SLOT + assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK + assert gs(C.NK_get_password_safe_slot_name(invalid_slot)) == '' + assert C.NK_get_last_command_status() == LibraryErrors.INVALID_SLOT + assert gs(C.NK_get_password_safe_slot_login(invalid_slot)) == '' + assert C.NK_get_last_command_status() == LibraryErrors.INVALID_SLOT def test_admin_retry_counts(C): -- cgit v1.2.1