From c6ba90ba1ca606b63373caaba16cb4fcc65d00f9 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 9 Jan 2017 18:30:29 +0100 Subject: Remove unused inttypes Signed-off-by: Szczepan Zalega --- include/CommandFailedException.h | 2 +- include/command_id.h | 2 +- include/device.h | 2 +- include/device_proto.h | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/CommandFailedException.h b/include/CommandFailedException.h index 9b0c59e..8bcdcae 100644 --- a/include/CommandFailedException.h +++ b/include/CommandFailedException.h @@ -7,7 +7,7 @@ #include #include -#include +#include "log.h" class CommandFailedException : public std::exception { public: diff --git a/include/command_id.h b/include/command_id.h index 346b750..9d12f93 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -1,6 +1,6 @@ #ifndef COMMAND_ID_H #define COMMAND_ID_H -#include "inttypes.h" +#include namespace nitrokey { namespace proto { diff --git a/include/device.h b/include/device.h index 62c4073..f686fbd 100644 --- a/include/device.h +++ b/include/device.h @@ -2,7 +2,7 @@ #define DEVICE_H #include #include -#include "inttypes.h" +#include #define HID_REPORT_SIZE 65 diff --git a/include/device_proto.h b/include/device_proto.h index 0953566..667b8db 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -8,7 +8,7 @@ #include #include // a local version for compatibility with Windows -#include "inttypes.h" +#include #include "cxx_semantics.h" #include "device.h" #include "misc.h" -- cgit v1.2.1 From 0c6f3234acea5888dd6c3c3aeee8cebcce59ba06 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 15:57:45 +0100 Subject: Use stdint instead of inttypes Signed-off-by: Szczepan Zalega --- include/stick10_commands.h | 2 +- include/stick10_commands_0.8.h | 2 +- include/stick20_commands.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/stick10_commands.h b/include/stick10_commands.h index f02fd70..9b72e92 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -4,7 +4,7 @@ #include #include #include -#include "inttypes.h" +#include #include "command.h" #include "device_proto.h" diff --git a/include/stick10_commands_0.8.h b/include/stick10_commands_0.8.h index 9594d1e..ead5add 100644 --- a/include/stick10_commands_0.8.h +++ b/include/stick10_commands_0.8.h @@ -9,7 +9,7 @@ #include #include #include -#include "inttypes.h" +#include #include "command.h" #include "device_proto.h" #include "stick10_commands.h" diff --git a/include/stick20_commands.h b/include/stick20_commands.h index e6df770..386cbda 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -1,7 +1,7 @@ #ifndef STICK20_COMMANDS_H #define STICK20_COMMANDS_H -#include "inttypes.h" +#include #include "command.h" #include #include -- cgit v1.2.1 From c2d3de8820cc2ad3f394b6672853af257d32e6f6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:04:52 +0100 Subject: Helper functions for getting device state get status for Pro and Storage check is device connected use make_shared for keeping instance reference fixed accessing active volume flag Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 11 +++++++---- include/stick20_commands.h | 9 ++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index fd39445..d6b70a4 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -39,8 +39,11 @@ namespace nitrokey { bool connect(const char *device_model); bool connect(); bool disconnect(); - void set_debug(bool state); - string get_status(); + bool is_connected(); + DeviceModel get_connected_device_model(); + void set_debug(bool state); + stick10::GetStatus::ResponsePayload get_status(); + string get_status_as_string(); string get_serial_number(); const char * get_totp_slot_name(uint8_t slot_number); @@ -105,6 +108,7 @@ namespace nitrokey { void send_startup(uint64_t seconds_from_epoch); const char * get_status_storage_as_string(); + stick20::DeviceConfigurationResponsePacket::ResponsePayload get_status_storage(); const char *get_SD_usage_data_as_string(); @@ -117,11 +121,10 @@ namespace nitrokey { void authorize_packet(T &package, const char *admin_temporary_password, shared_ptr device); int get_major_firmware_version(); + explicit NitrokeyManager(); private: - NitrokeyManager(); static shared_ptr _instance; - bool connected; std::shared_ptr device; bool is_valid_hotp_slot_number(uint8_t slot_number) const; diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 386cbda..8080117 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -141,7 +141,14 @@ namespace nitrokey { uint8_t NewSDCardFound_u8; uint8_t SDFillWithRandomChars_u8; uint32_t ActiveSD_CardID_u32; - uint8_t VolumeActiceFlag_u8; + union{ + uint8_t VolumeActiceFlag_u8; + struct { + bool unencrypted :1; + bool encrypted :1; + bool hidden :1; + } __packed VolumeActiceFlag_st; + } __packed; uint8_t NewSmartCardFound_u8; uint8_t UserPwRetryCount; uint8_t AdminPwRetryCount; -- cgit v1.2.1 From 774f3a0d93bac42aa50d4d30753e00f769b17881 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:09:08 +0100 Subject: Make device configuration const, protect non-const with ::atomic Signed-off-by: Szczepan Zalega --- include/device.h | 42 +++++++++++++++++++++++++----------------- 1 file changed, 25 insertions(+), 17 deletions(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index f686fbd..938fc75 100644 --- a/include/device.h +++ b/include/device.h @@ -8,9 +8,12 @@ // TODO !! SEMAPHORE +#include + namespace nitrokey { namespace device { using namespace std::chrono_literals; + using std::chrono::milliseconds; struct EnumClassHash { @@ -22,6 +25,7 @@ namespace device { }; enum class DeviceModel{ + UNKNOWN, PRO, STORAGE }; @@ -29,8 +33,11 @@ enum class DeviceModel{ class Device { public: - Device(); - virtual ~Device(){disconnect();} + Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, + const milliseconds send_receive_delay, const int retry_receiving_count, + const milliseconds retry_timeout); + + virtual ~Device(){disconnect();} // lack of device is not actually an error, // so it doesn't throw @@ -51,31 +58,32 @@ public: int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; - std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;} + std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;} - int get_last_command_status() {auto a = last_command_status; last_command_status = 0; return a;}; - void set_last_command_status(uint8_t _err) { last_command_status = _err;} ; - bool last_command_sucessfull() const {return last_command_status == 0;}; - DeviceModel get_device_model() const {return m_model;} + int get_last_command_status() {int a = last_command_status; last_command_status = 0; return a;}; + void set_last_command_status(uint8_t _err) { last_command_status = _err;} ; + bool last_command_sucessfull() const {return last_command_status == 0;}; + DeviceModel get_device_model() const {return m_model;} private: - uint8_t last_command_status; + std::atomic last_command_status; - protected: - uint16_t m_vid; - uint16_t m_pid; - DeviceModel m_model; +protected: + const uint16_t m_vid; + const uint16_t m_pid; + const DeviceModel m_model; /* * While the project uses Signal11 portable HIDAPI * library, there's no way of doing it asynchronously, * hence polling. */ - int m_retry_sending_count; - int m_retry_receiving_count; - std::chrono::milliseconds m_retry_timeout; - std::chrono::milliseconds m_send_receive_delay; + const int m_retry_sending_count; + const int m_retry_receiving_count; + const std::chrono::milliseconds m_retry_timeout; + const std::chrono::milliseconds m_send_receive_delay; + + std::atomicmp_devhandle; - hid_device *mp_devhandle; }; class Stick10 : public Device { -- cgit v1.2.1 From fc986f87fd256e2cfdd5edcc5b0cf14faf694441 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 16:52:33 +0100 Subject: Use atomic_exchange for atomic operations Signed-off-by: Szczepan Zalega --- include/device.h | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index 938fc75..42865ee 100644 --- a/include/device.h +++ b/include/device.h @@ -6,8 +6,6 @@ #define HID_REPORT_SIZE 65 -// TODO !! SEMAPHORE - #include namespace nitrokey { @@ -60,7 +58,7 @@ public: std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; std::chrono::milliseconds get_send_receive_delay() const {return m_send_receive_delay;} - int get_last_command_status() {int a = last_command_status; last_command_status = 0; return a;}; + int get_last_command_status() {int a = std::atomic_exchange(&last_command_status, static_cast(0)); return a;}; void set_last_command_status(uint8_t _err) { last_command_status = _err;} ; bool last_command_sucessfull() const {return last_command_status == 0;}; DeviceModel get_device_model() const {return m_model;} -- cgit v1.2.1 From 02189413ce463116694478fcdcb418fed7e42027 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 20:09:53 +0100 Subject: Remove UNKNOWN type of the device Signed-off-by: Szczepan Zalega --- include/device.h | 1 - 1 file changed, 1 deletion(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index 42865ee..4001d45 100644 --- a/include/device.h +++ b/include/device.h @@ -23,7 +23,6 @@ namespace device { }; enum class DeviceModel{ - UNKNOWN, PRO, STORAGE }; -- cgit v1.2.1 From 73eac5050abad1b8f0ddbc7e94a11170a640e130 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 11 Jan 2017 20:11:01 +0100 Subject: Protect concurrent use with lock guard Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index d6b70a4..6551c1a 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -39,7 +39,7 @@ namespace nitrokey { bool connect(const char *device_model); bool connect(); bool disconnect(); - bool is_connected(); + bool is_connected() const throw() ; DeviceModel get_connected_device_model(); void set_debug(bool state); stick10::GetStatus::ResponsePayload get_status(); -- cgit v1.2.1 From 90e626b52c75e9e117494f15f57177aaf912468c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 10:43:15 +0100 Subject: Add lock_guard for complete send-receive cycle Signed-off-by: Szczepan Zalega --- include/device_proto.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 667b8db..31bfe11 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -32,6 +32,8 @@ #define PWS_SEND_TAB 2 #define PWS_SEND_CR 3 +#include + namespace nitrokey { namespace proto { /* @@ -204,13 +206,15 @@ namespace nitrokey { bzero(&st, sizeof(st)); } - static ClearingProxy run(device::Device &dev, const command_payload &payload) { using namespace ::nitrokey::device; using namespace ::nitrokey::log; using namespace std::chrono_literals; + static std::mutex send_receive_mtx; + std::lock_guard guard(send_receive_mtx); + Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); int status; -- cgit v1.2.1 From daf51e7e6a6f6569472f2a5fae5a376f105f858a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 10:51:14 +0100 Subject: Add const qualifier to functions Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 6551c1a..e96ac22 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -40,7 +40,7 @@ namespace nitrokey { bool connect(); bool disconnect(); bool is_connected() const throw() ; - DeviceModel get_connected_device_model(); + DeviceModel get_connected_device_model() const; void set_debug(bool state); stick10::GetStatus::ResponsePayload get_status(); string get_status_as_string(); -- cgit v1.2.1 From deed3e733e7158859fd48ef51b64d924ea4e8184 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 12:04:52 +0100 Subject: Decrease retry count to 20 Update log message to be more readable Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 31bfe11..2105f30 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -296,7 +296,7 @@ namespace nitrokey { successful_communication = true; break; } - Log::instance()(std::string("Retry status - dev status, equal crc, correct CRC: ") + Log::instance()(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + std::to_string(resp.device_status) + " " + std::to_string(resp.last_command_crc == outp.crc) + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); -- cgit v1.2.1 From ffcb53e4cb3419ea31bf7b22e5f0c42fd54041da Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 12:11:18 +0100 Subject: Name fix for firmware version getter Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index e96ac22..c7d7704 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -119,7 +119,7 @@ namespace nitrokey { template void authorize_packet(T &package, const char *admin_temporary_password, shared_ptr device); - int get_major_firmware_version(); + int get_minor_firmware_version(); explicit NitrokeyManager(); private: -- cgit v1.2.1 From 185b318b9134da163bbfb160d2d737c835927f30 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 13 Jan 2017 12:55:08 +0100 Subject: Fix firmware version in device status Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 8080117..fd72f1e 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -130,9 +130,9 @@ namespace nitrokey { uint8_t VersionInfo_au8[4]; struct { uint8_t __unused; - uint8_t major; - uint8_t __unused2; uint8_t minor; + uint8_t __unused2; + uint8_t major; } __packed versionInfo; }; -- cgit v1.2.1 From c551b27792774b87a1be7fd0dcfd9e209eaef5ec Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 14 Jan 2017 13:37:29 +0100 Subject: Method to get commands failure cause Signed-off-by: Szczepan Zalega --- include/CommandFailedException.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/CommandFailedException.h b/include/CommandFailedException.h index 8bcdcae..3753ad4 100644 --- a/include/CommandFailedException.h +++ b/include/CommandFailedException.h @@ -11,8 +11,8 @@ class CommandFailedException : public std::exception { public: - uint8_t last_command_code; - uint8_t last_command_status; + const uint8_t last_command_code; + const uint8_t last_command_status; CommandFailedException(uint8_t last_command_code, uint8_t last_command_status) : last_command_code(last_command_code), @@ -24,6 +24,10 @@ public: return "Command execution has failed on device"; } + bool reason_slot_not_programmed() const throw(){ + return last_command_status == 3; //FIXME use enum status codes + } + }; -- cgit v1.2.1 From 56bd3e2c4353cfc1b902c6dfb55df0ef563c5372 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 17 Jan 2017 15:35:44 +0100 Subject: Check command's fail reason within exception Signed-off-by: Szczepan Zalega --- include/CommandFailedException.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/CommandFailedException.h b/include/CommandFailedException.h index 3753ad4..190eabc 100644 --- a/include/CommandFailedException.h +++ b/include/CommandFailedException.h @@ -8,6 +8,9 @@ #include #include #include "log.h" +#include "command_id.h" + +using cs = nitrokey::proto::stick10::command_status; class CommandFailedException : public std::exception { public: @@ -25,7 +28,11 @@ public: } bool reason_slot_not_programmed() const throw(){ - return last_command_status == 3; //FIXME use enum status codes + return last_command_status == static_cast(cs::slot_not_programmed); + } + + bool reason_wrong_password() const throw(){ + return last_command_status == static_cast(cs::wrong_password); } }; -- cgit v1.2.1 From 3d54b4ea0c96a2420973dc840c1b32545bf2b05d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 18 Jan 2017 00:11:25 +0100 Subject: Fix getting card serial todo: remove whitespace at the string end Signed-off-by: Szczepan Zalega --- include/misc.h | 3 ++- include/stick10_commands.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/misc.h b/include/misc.h index 5158de0..9e4659d 100644 --- a/include/misc.h +++ b/include/misc.h @@ -44,7 +44,8 @@ typename T::CommandPayload get_payload(){ CMDTYPE::CommandTransaction::run(stick, p); } - std::string hexdump(const char *p, size_t size, bool print_header=true); + std::string hexdump(const char *p, size_t size, bool print_header=true, bool print_ascii=true, + bool print_empty=true); uint32_t stm_crc32(const uint8_t *data, size_t size); std::vector hex_string_to_byte(const char* hexString); } diff --git a/include/stick10_commands.h b/include/stick10_commands.h index 9b72e92..fb362fb 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -332,7 +332,7 @@ class GetStatus : Command { std::string get_card_serial_hex() const { return ::nitrokey::misc::hexdump((const char *)(card_serial), - sizeof card_serial, false); + sizeof card_serial, false, false, false); } std::string dissect() const { -- cgit v1.2.1 From 2543e09fa25fa8ed54920c519de32d4b4da074d4 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 24 Jan 2017 17:33:49 +0100 Subject: Read slot command support Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 7 ++++++- include/stick10_commands.h | 38 ++++++++++++++++++++++++++++++++------ 2 files changed, 38 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index c7d7704..03f1a86 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -32,6 +32,10 @@ namespace nitrokey { uint32_t get_HOTP_code(uint8_t slot_number, const char *user_temporary_password); uint32_t get_TOTP_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time, uint8_t last_interval, const char *user_temporary_password); + uint32_t get_TOTP_code(uint8_t slot_number, const char *user_temporary_password); + stick10::ReadSlot::ResponsePayload get_TOTP_slot_data(const uint8_t slot_number); + stick10::ReadSlot::ResponsePayload get_HOTP_slot_data(const uint8_t slot_number); + bool set_time(uint64_t time); bool get_time(); bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); @@ -127,7 +131,8 @@ namespace nitrokey { static shared_ptr _instance; std::shared_ptr device; - bool is_valid_hotp_slot_number(uint8_t slot_number) const; + stick10::ReadSlot::ResponsePayload get_OTP_slot_data(const uint8_t slot_number); + bool is_valid_hotp_slot_number(uint8_t slot_number) const; bool is_valid_totp_slot_number(uint8_t slot_number) const; bool is_valid_password_safe_slot_number(uint8_t slot_number) const; uint8_t get_internal_slot_number_for_hotp(uint8_t slot_number) const; diff --git a/include/stick10_commands.h b/include/stick10_commands.h index fb362fb..b66a9b4 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -293,18 +293,44 @@ class ReadSlot : Command { struct ResponsePayload { uint8_t slot_name[15]; - uint8_t config; - uint8_t token_id[13]; - uint64_t counter; + union{ + uint8_t _slot_config; + struct{ + bool use_8_digits : 1; + bool use_enter : 1; + bool use_tokenID : 1; + }; + }; + union{ + uint8_t slot_token_id[13]; /** OATH Token Identifier */ + struct{ /** @see https://openauthentication.org/token-specs/ */ + uint8_t omp[2]; + uint8_t tt[2]; + uint8_t mui[8]; + uint8_t keyboard_layout; //disabled feature in nitroapp as of 20160805 + } slot_token_fields; + }; + union{ + uint64_t slot_counter; + uint8_t slot_counter_s[8]; + } __packed; bool isValid() const { return true; } std::string dissect() const { std::stringstream ss; ss << "slot_name:\t" << slot_name << std::endl; - ss << "config:\t" << config << std::endl; - ss << "token_id:\t" << token_id << std::endl; - ss << "counter:\t" << counter << std::endl; + ss << "slot_config:\t" << std::bitset<8>((int)_slot_config) << std::endl; + ss << "\tuse_8_digits(0):\t" << use_8_digits << std::endl; + ss << "\tuse_enter(1):\t" << use_enter << std::endl; + ss << "\tuse_tokenID(2):\t" << use_tokenID << std::endl; + + ss << "slot_token_id:\t"; + for (auto i : slot_token_id) + ss << std::hex << std::setw(2) << std::setfill('0')<< (int) i << " " ; + ss << std::endl; + ss << "slot_counter:\t[" << (int)slot_counter << "]\t" + << ::nitrokey::misc::hexdump((const char *)(&slot_counter), sizeof slot_counter, false); return ss.str(); } } __packed; -- cgit v1.2.1 From a721ca6391d1f6494d5493fb0e56c868bcd2b60c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 26 Jan 2017 10:28:12 +0100 Subject: Use const char pointers for C strings Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 03f1a86..f0cab68 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -53,8 +53,8 @@ namespace nitrokey { const char * get_totp_slot_name(uint8_t slot_number); const char * get_hotp_slot_name(uint8_t slot_number); - void change_user_PIN(char *current_PIN, char *new_PIN); - void change_admin_PIN(char *current_PIN, char *new_PIN); + void change_user_PIN(const char *current_PIN, const char *new_PIN); + void change_admin_PIN(const char *current_PIN, const char *new_PIN); void enable_password_safe(const char *user_pin); @@ -141,7 +141,7 @@ namespace nitrokey { const char * get_slot_name(uint8_t slot_number); template - void change_PIN_general(char *current_PIN, char *new_PIN); + void change_PIN_general(const char *current_PIN, const char *new_PIN); void write_HOTP_slot_authorize(uint8_t slot_number, const char *slot_name, const char *secret, uint64_t hotp_counter, bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID, -- cgit v1.2.1 From 84a98c04c6c79455b04ba300ebfa5ec752abb721 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 30 Jan 2017 17:29:53 +0100 Subject: Send current time when checking time synchronization Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index f0cab68..3e38cc3 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -37,7 +37,7 @@ namespace nitrokey { stick10::ReadSlot::ResponsePayload get_HOTP_slot_data(const uint8_t slot_number); bool set_time(uint64_t time); - bool get_time(); + bool get_time(uint64_t time = 0); bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); bool connect(const char *device_model); -- cgit v1.2.1 From 48ec48d2c680b9f40d2b038fda9555bfd024bb97 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 30 Jan 2017 20:31:18 +0100 Subject: Use local hexdumping function for getting serial number Signed-off-by: Szczepan Zalega --- include/stick10_commands.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/stick10_commands.h b/include/stick10_commands.h index b66a9b4..8d37dbd 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -357,8 +357,14 @@ class GetStatus : Command { bool isValid() const { return enable_user_password!=delete_user_password; } std::string get_card_serial_hex() const { - return ::nitrokey::misc::hexdump((const char *)(card_serial), - sizeof card_serial, false, false, false); +// return ::nitrokey::misc::hexdump((const char *)(card_serial), +// sizeof card_serial, false, false, false); + std::stringstream ss; + ss << std::hex << std::setfill('0'); + for (int i = 0; i < sizeof(card_serial); ++i) { + ss << std::setw(2) << static_cast(card_serial[i]); + } + return ss.str(); } std::string dissect() const { -- cgit v1.2.1 From aa668f74e95617fd0544327a2b57bf654a6f9a2d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:07:55 +0100 Subject: Be tread-safe on initializing instance Signed-off-by: Szczepan Zalega --- include/DeviceCommunicationExceptions.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 include/DeviceCommunicationExceptions.h (limited to 'include') diff --git a/include/DeviceCommunicationExceptions.h b/include/DeviceCommunicationExceptions.h new file mode 100644 index 0000000..78fc625 --- /dev/null +++ b/include/DeviceCommunicationExceptions.h @@ -0,0 +1,31 @@ +#ifndef LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H +#define LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H + +#include +#include +//class DeviceCommunicationException: public std::exception { +class DeviceCommunicationException: public std::runtime_error{ + std::string message; +public: + DeviceCommunicationException(std::string _msg): runtime_error(_msg), message(_msg){} +// virtual const char* what() const throw() override { +// return message.c_str(); +// } +}; + +class DeviceNotConnected: public DeviceCommunicationException { +public: + DeviceNotConnected(std::string msg) : DeviceCommunicationException(msg){} +}; + +class DeviceSendingFailure: public DeviceCommunicationException { +public: + DeviceSendingFailure(std::string msg) : DeviceCommunicationException(msg){} +}; + +class DeviceReceivingFailure: public DeviceCommunicationException { +public: + DeviceReceivingFailure(std::string msg) : DeviceCommunicationException(msg){} +}; + +#endif //LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H -- cgit v1.2.1 From cb6b2dd65e1f0132353159b83ae05c944d8e62f0 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:10:59 +0100 Subject: Make disconnect thread safe. Check is device actually connected by invoking its checking method Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 3e38cc3..4a98e94 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -43,7 +43,7 @@ namespace nitrokey { bool connect(const char *device_model); bool connect(); bool disconnect(); - bool is_connected() const throw() ; + bool is_connected() throw() ; DeviceModel get_connected_device_model() const; void set_debug(bool state); stick10::GetStatus::ResponsePayload get_status(); @@ -156,6 +156,7 @@ namespace nitrokey { bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID, const char *temporary_password) const; + bool _disconnect_no_lock(); }; } -- cgit v1.2.1 From 0503db5b47f247568b78504fa781e083e108eab9 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:12:31 +0100 Subject: Pass devices shared pointer to methods instead of ref Signed-off-by: Szczepan Zalega --- include/device_proto.h | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 2105f30..ba314f4 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -206,7 +206,7 @@ namespace nitrokey { bzero(&st, sizeof(st)); } - static ClearingProxy run(device::Device &dev, + static ClearingProxy run(std::shared_ptr dev, const command_payload &payload) { using namespace ::nitrokey::device; using namespace ::nitrokey::log; @@ -235,22 +235,22 @@ namespace nitrokey { bool successful_communication = false; int receiving_retry_counter = 0; - int sending_retry_counter = dev.get_retry_sending_count(); + int sending_retry_counter = dev->get_retry_sending_count(); while (sending_retry_counter-- > 0) { - status = dev.send(&outp); + status = dev->send(&outp); if (status <= 0) - throw std::runtime_error( + throw DeviceSendingFailure( std::string("Device error while sending command ") + std::to_string(status)); - std::this_thread::sleep_for(dev.get_send_receive_delay()); + std::this_thread::sleep_for(dev->get_send_receive_delay()); // FIXME make checks done in device:recv here - receiving_retry_counter = dev.get_retry_receiving_count(); + receiving_retry_counter = dev->get_retry_receiving_count(); while (receiving_retry_counter-- > 0) { - status = dev.recv(&resp); + status = dev->recv(&resp); - if (dev.get_device_model() == DeviceModel::STORAGE && + if (dev->get_device_model() == DeviceModel::STORAGE && resp.command_id >= stick20::CMD_START_VALUE && resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + @@ -306,7 +306,7 @@ namespace nitrokey { Loglevel::DEBUG); Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); - std::this_thread::sleep_for(dev.get_retry_timeout()); + std::this_thread::sleep_for(dev->get_retry_timeout()); continue; } if (successful_communication) break; @@ -315,7 +315,7 @@ namespace nitrokey { Loglevel::DEBUG); } - dev.set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv + dev->set_last_command_status(resp.last_command_status); // FIXME should be handled on device.recv clear_packet(outp); @@ -348,7 +348,7 @@ namespace nitrokey { return resp; } - static ClearingProxy run(device::Device &dev) { + static ClearingProxy run(std::shared_ptr dev) { command_payload empty_payload; return run(dev, empty_payload); } -- cgit v1.2.1 From d69cf0b866fa3cc5afda2bb1a321a900520fbcc1 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:15:48 +0100 Subject: Add method for checking is the device listed as connected in OS Signed-off-by: Szczepan Zalega --- include/device.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/device.h b/include/device.h index 4001d45..5965f99 100644 --- a/include/device.h +++ b/include/device.h @@ -52,6 +52,8 @@ public: */ virtual int recv(void *packet); + bool is_connected(); + int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; -- cgit v1.2.1 From 03f444905d3a7af3091c2401280e83146f08443a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:17:23 +0100 Subject: Add more shortcuts for checking devices error code Signed-off-by: Szczepan Zalega --- include/CommandFailedException.h | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/CommandFailedException.h b/include/CommandFailedException.h index 190eabc..6ff9a2d 100644 --- a/include/CommandFailedException.h +++ b/include/CommandFailedException.h @@ -14,11 +14,11 @@ using cs = nitrokey::proto::stick10::command_status; class CommandFailedException : public std::exception { public: - const uint8_t last_command_code; + const uint8_t last_command_id; const uint8_t last_command_status; - CommandFailedException(uint8_t last_command_code, uint8_t last_command_status) : - last_command_code(last_command_code), + CommandFailedException(uint8_t last_command_id, uint8_t last_command_status) : + last_command_id(last_command_id), last_command_status(last_command_status){ nitrokey::log::Log::instance()(std::string("CommandFailedException, status: ")+ std::to_string(last_command_status), nitrokey::log::Loglevel::DEBUG); } @@ -27,6 +27,19 @@ public: return "Command execution has failed on device"; } + + bool reason_timestamp_warning() const throw(){ + return last_command_status == static_cast(cs::timestamp_warning); + } + + bool reason_AES_not_initialized() const throw(){ + return last_command_status == static_cast(cs::AES_dec_failed); + } + + bool reason_not_authorized() const throw(){ + return last_command_status == static_cast(cs::not_authorized); + } + bool reason_slot_not_programmed() const throw(){ return last_command_status == static_cast(cs::slot_not_programmed); } -- cgit v1.2.1 From 9bc6b85e12d73a43b8d85ba109acff8778f4c08a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 31 Jan 2017 18:17:58 +0100 Subject: Throw before communicating with device if it is not initialized Signed-off-by: Szczepan Zalega --- include/LibraryException.h | 2 -- include/device_proto.h | 7 ++++++- 2 files changed, 6 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/LibraryException.h b/include/LibraryException.h index 3c3fab4..e62788d 100644 --- a/include/LibraryException.h +++ b/include/LibraryException.h @@ -11,8 +11,6 @@ public: virtual uint8_t exception_id()= 0; }; - - class TargetBufferSmallerThanSource: public LibraryException { public: virtual uint8_t exception_id() override { diff --git a/include/device_proto.h b/include/device_proto.h index ba314f4..9401428 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -33,6 +33,7 @@ #define PWS_SEND_CR 3 #include +#include "DeviceCommunicationExceptions.h" namespace nitrokey { namespace proto { @@ -217,6 +218,10 @@ namespace nitrokey { Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + if (dev == nullptr){ + throw DeviceNotConnected("Device not initialized"); + } + int status; OutgoingPacket outp; ResponsePacket resp; @@ -320,7 +325,7 @@ namespace nitrokey { clear_packet(outp); if (status <= 0) - throw std::runtime_error( //FIXME replace with CriticalErrorException + throw DeviceReceivingFailure( //FIXME replace with CriticalErrorException std::string("Device error while executing command ") + std::to_string(status)); -- cgit v1.2.1 From 69fedd655a87c32c0c54eac46a1d6df7450873d6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 2 Feb 2017 18:22:40 +0100 Subject: Add description for some Storage variables Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index fd72f1e..a3f1609 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -123,6 +123,9 @@ namespace nitrokey { StorageCommandResponsePayload::TransmissionData transmission_data; uint16_t MagicNumber_StickConfig_u16; + /** + * READ_WRITE_ACTIVE = ReadWriteFlagUncryptedVolume_u8 == 0; + */ uint8_t ReadWriteFlagUncryptedVolume_u8; uint8_t ReadWriteFlagCryptedVolume_u8; @@ -134,11 +137,22 @@ namespace nitrokey { uint8_t __unused2; uint8_t major; } __packed versionInfo; - }; + } __packed; uint8_t ReadWriteFlagHiddenVolume_u8; uint8_t FirmwareLocked_u8; - uint8_t NewSDCardFound_u8; + + union{ + uint8_t NewSDCardFound_u8; + struct { + bool NewCard :1; + uint8_t Counter :7; + } __packed NewSDCardFound_st; + } __packed; + + /** + * SD card FILLED with random chars + */ uint8_t SDFillWithRandomChars_u8; uint32_t ActiveSD_CardID_u32; union{ -- cgit v1.2.1 From 767e24572db2bbc4b9837c32ffc0bab4e1ad0b81 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 3 Feb 2017 17:07:32 +0100 Subject: Disconnect device as soon as the communication issue appears Signed-off-by: Szczepan Zalega --- include/device_proto.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 9401428..1e07277 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -243,10 +243,13 @@ namespace nitrokey { int sending_retry_counter = dev->get_retry_sending_count(); while (sending_retry_counter-- > 0) { status = dev->send(&outp); - if (status <= 0) + if (status <= 0){ + Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); + dev->disconnect(); throw DeviceSendingFailure( std::string("Device error while sending command ") + std::to_string(status)); + } std::this_thread::sleep_for(dev->get_send_receive_delay()); -- cgit v1.2.1 From db76ae5299f3650385f66e4c596b18fd54250d38 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 3 Feb 2017 17:23:44 +0100 Subject: Allow users to lock encrypted volumes specifically Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 ++ include/stick20_commands.h | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 4a98e94..b89db63 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -91,8 +91,10 @@ namespace nitrokey { bool is_AES_supported(const char *user_password); void unlock_encrypted_volume(const char *user_password); + void lock_encrypted_volume(); void unlock_hidden_volume(const char *hidden_volume_password); + void lock_hidden_volume(); void set_unencrypted_read_only(const char *user_pin); diff --git a/include/stick20_commands.h b/include/stick20_commands.h index a3f1609..b887636 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -26,9 +26,12 @@ namespace nitrokey { public PasswordCommand {}; class EnableEncryptedPartition : public PasswordCommand {}; - class DisableEncryptedPartition : public PasswordCommand {}; class EnableHiddenEncryptedPartition : public PasswordCommand {}; + + //FIXME the volume disabling commands do not need password + class DisableEncryptedPartition : public PasswordCommand {}; class DisableHiddenEncryptedPartition : public PasswordCommand {}; + class EnableFirmwareUpdate : public PasswordCommand {}; class ChangeUpdatePassword : Command { -- cgit v1.2.1 From 3fcbb90176fb02816cc47ad7172b7b89bf6cb67d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 4 Feb 2017 16:13:02 +0100 Subject: Make statistics about device's connection Signed-off-by: Szczepan Zalega --- include/device.h | 22 ++++++++++++++++++++-- include/device_proto.h | 18 +++++++++++++++--- 2 files changed, 35 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index 5965f99..a23e1b3 100644 --- a/include/device.h +++ b/include/device.h @@ -2,7 +2,8 @@ #define DEVICE_H #include #include -#include +#include +#include #define HID_REPORT_SIZE 65 @@ -30,11 +31,26 @@ enum class DeviceModel{ class Device { public: + + struct ErrorCounters{ + int wrong_CRC; + int CRC_other_than_awaited; + int busy; + int total_retries; + int sending_error; + int receiving_error; + int total_comm_runs; + int storage_commands; + int successful; + std::string get_as_string(); + } m_counters = {}; + + Device(const uint16_t vid, const uint16_t pid, const DeviceModel model, const milliseconds send_receive_delay, const int retry_receiving_count, const milliseconds retry_timeout); - virtual ~Device(){disconnect();} + virtual ~Device(){show_stats(); disconnect();} // lack of device is not actually an error, // so it doesn't throw @@ -54,6 +70,8 @@ public: bool is_connected(); + void show_stats(); + ErrorCounters get_stats(){ return m_counters; } int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; diff --git a/include/device_proto.h b/include/device_proto.h index 1e07277..e3a217d 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -221,6 +221,7 @@ namespace nitrokey { if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); } + dev->m_counters.total_comm_runs++; int status; OutgoingPacket outp; @@ -246,6 +247,7 @@ namespace nitrokey { if (status <= 0){ Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); dev->disconnect(); + dev->m_counters.sending_error++; throw DeviceSendingFailure( std::string("Device error while sending command ") + std::to_string(status)); @@ -263,6 +265,7 @@ namespace nitrokey { resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); + dev->m_counters.storage_commands++; resp.last_command_status = static_cast(stick10::command_status::ok); switch (static_cast(resp.storage_status.device_status)) { @@ -288,8 +291,9 @@ namespace nitrokey { //SENDPASSWORD gives wrong CRC , for now rely on !=0 (TODO report) // if (resp.device_status == 0 && resp.last_command_crc == outp.crc && resp.isCRCcorrect()) break; + auto CRC_equal_awaited = resp.last_command_crc == outp.crc; if (resp.device_status == static_cast(stick10::device_status::ok) && - resp.last_command_crc == outp.crc && resp.isValid()){ + CRC_equal_awaited && resp.isValid()){ successful_communication = true; break; } @@ -306,14 +310,19 @@ namespace nitrokey { } Log::instance()(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + std::to_string(resp.device_status) + " " + - std::to_string(resp.last_command_crc == outp.crc) + + std::to_string(CRC_equal_awaited) + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); + if (!resp.isCRCcorrect()) dev->m_counters.wrong_CRC++; + if (!CRC_equal_awaited) dev->m_counters.CRC_other_than_awaited++; + + Log::instance()( "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", Loglevel::DEBUG); Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); + dev->m_counters.total_retries++; std::this_thread::sleep_for(dev->get_retry_timeout()); continue; } @@ -327,10 +336,12 @@ namespace nitrokey { clear_packet(outp); - if (status <= 0) + if (status <= 0) { + dev->m_counters.receiving_error++; throw DeviceReceivingFailure( //FIXME replace with CriticalErrorException std::string("Device error while executing command ") + std::to_string(status)); + } Log::instance()("Incoming HID packet:", Loglevel::DEBUG); Log::instance()(static_cast(resp), Loglevel::DEBUG); @@ -351,6 +362,7 @@ namespace nitrokey { if (resp.last_command_status != static_cast(stick10::command_status::ok)) throw CommandFailedException(resp.command_id, resp.last_command_status); + dev->m_counters.successful++; // See: DeviceResponse return resp; -- cgit v1.2.1 From 832ae0720e7ce2a9666baaf9b86c1c9b9c150697 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 4 Feb 2017 16:14:55 +0100 Subject: Allow device to reply 10 times with busy status in a try Signed-off-by: Szczepan Zalega --- include/device_proto.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index e3a217d..ea740b3 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -298,9 +298,17 @@ namespace nitrokey { break; } if (resp.device_status == static_cast(stick10::device_status::busy)) { - receiving_retry_counter++; - Log::instance()("Status busy, not decresing receiving_retry_counter counter: " + - std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); + static int busy_counter = 0; + if (busy_counter++<10) { + receiving_retry_counter++; + dev->m_counters.busy++; + Log::instance()("Status busy, not decreasing receiving_retry_counter counter: " + + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); + } else { + busy_counter = 0; + Log::instance()("Status busy, decreasing receiving_retry_counter counter: " + + std::to_string(receiving_retry_counter), Loglevel::DEBUG); + } } if (resp.device_status == static_cast(stick10::device_status::busy) && static_cast(resp.storage_status.device_status) -- cgit v1.2.1 From b16667ba57ef301ef961801676de66cce30d4c52 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 4 Feb 2017 16:16:03 +0100 Subject: Disable early device disconnection on communication error Signed-off-by: Szczepan Zalega --- include/device_proto.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index ea740b3..cd91952 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -245,8 +245,9 @@ namespace nitrokey { while (sending_retry_counter-- > 0) { status = dev->send(&outp); if (status <= 0){ - Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); - dev->disconnect(); + //FIXME early disconnection not yet working properly +// Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); +// dev->disconnect(); dev->m_counters.sending_error++; throw DeviceSendingFailure( std::string("Device error while sending command ") + -- cgit v1.2.1 From c3aec5d004968c103a65398a1fd66f0d5ced63af Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 10 Feb 2017 12:39:25 +0100 Subject: Count all busy status Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index cd91952..cbb74fb 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -300,9 +300,9 @@ namespace nitrokey { } if (resp.device_status == static_cast(stick10::device_status::busy)) { static int busy_counter = 0; + dev->m_counters.busy++; if (busy_counter++<10) { receiving_retry_counter++; - dev->m_counters.busy++; Log::instance()("Status busy, not decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } else { -- cgit v1.2.1 From c69604e8ba099b1421af86c34d904b0b380f996c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 18:49:02 +0100 Subject: Test compilation under windows --- include/hidapi/hidapi.h | 391 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 391 insertions(+) create mode 100644 include/hidapi/hidapi.h (limited to 'include') diff --git a/include/hidapi/hidapi.h b/include/hidapi/hidapi.h new file mode 100644 index 0000000..e5bc2dc --- /dev/null +++ b/include/hidapi/hidapi.h @@ -0,0 +1,391 @@ +/******************************************************* + HIDAPI - Multi-Platform library for + communication with HID devices. + + Alan Ott + Signal 11 Software + + 8/22/2009 + + Copyright 2009, All Rights Reserved. + + At the discretion of the user of this library, + this software may be licensed under the terms of the + GNU General Public License v3, a BSD-Style license, or the + original HIDAPI license as outlined in the LICENSE.txt, + LICENSE-gpl3.txt, LICENSE-bsd.txt, and LICENSE-orig.txt + files located at the root of the source distribution. + These files may also be found in the public source + code repository located at: + http://github.com/signal11/hidapi . +********************************************************/ + +/** @file + * @defgroup API hidapi API + */ + +#ifndef HIDAPI_H__ +#define HIDAPI_H__ + +#include + +#ifdef _WIN32 + #define HID_API_EXPORT __declspec(dllexport) + #define HID_API_CALL +#else + #define HID_API_EXPORT /**< API export macro */ + #define HID_API_CALL /**< API call macro */ +#endif + +#define HID_API_EXPORT_CALL HID_API_EXPORT HID_API_CALL /**< API export and call macro*/ + +#ifdef __cplusplus +extern "C" { +#endif + struct hid_device_; + typedef struct hid_device_ hid_device; /**< opaque hidapi structure */ + + /** hidapi info structure */ + struct hid_device_info { + /** Platform-specific device path */ + char *path; + /** Device Vendor ID */ + unsigned short vendor_id; + /** Device Product ID */ + unsigned short product_id; + /** Serial Number */ + wchar_t *serial_number; + /** Device Release Number in binary-coded decimal, + also known as Device Version Number */ + unsigned short release_number; + /** Manufacturer String */ + wchar_t *manufacturer_string; + /** Product string */ + wchar_t *product_string; + /** Usage Page for this Device/Interface + (Windows/Mac only). */ + unsigned short usage_page; + /** Usage for this Device/Interface + (Windows/Mac only).*/ + unsigned short usage; + /** The USB interface which this logical device + represents. Valid on both Linux implementations + in all cases, and valid on the Windows implementation + only if the device contains more than one interface. */ + int interface_number; + + /** Pointer to the next device */ + struct hid_device_info *next; + }; + + + /** @brief Initialize the HIDAPI library. + + This function initializes the HIDAPI library. Calling it is not + strictly necessary, as it will be called automatically by + hid_enumerate() and any of the hid_open_*() functions if it is + needed. This function should be called at the beginning of + execution however, if there is a chance of HIDAPI handles + being opened by different threads simultaneously. + + @ingroup API + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_init(void); + + /** @brief Finalize the HIDAPI library. + + This function frees all of the static data associated with + HIDAPI. It should be called at the end of execution to avoid + memory leaks. + + @ingroup API + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_exit(void); + + /** @brief Enumerate the HID Devices. + + This function returns a linked list of all the HID devices + attached to the system which match vendor_id and product_id. + If @p vendor_id is set to 0 then any vendor matches. + If @p product_id is set to 0 then any product matches. + If @p vendor_id and @p product_id are both set to 0, then + all HID devices will be returned. + + @ingroup API + @param vendor_id The Vendor ID (VID) of the types of device + to open. + @param product_id The Product ID (PID) of the types of + device to open. + + @returns + This function returns a pointer to a linked list of type + struct #hid_device, containing information about the HID devices + attached to the system, or NULL in the case of failure. Free + this linked list by calling hid_free_enumeration(). + */ + struct hid_device_info HID_API_EXPORT * HID_API_CALL hid_enumerate(unsigned short vendor_id, unsigned short product_id); + + /** @brief Free an enumeration Linked List + + This function frees a linked list created by hid_enumerate(). + + @ingroup API + @param devs Pointer to a list of struct_device returned from + hid_enumerate(). + */ + void HID_API_EXPORT HID_API_CALL hid_free_enumeration(struct hid_device_info *devs); + + /** @brief Open a HID device using a Vendor ID (VID), Product ID + (PID) and optionally a serial number. + + If @p serial_number is NULL, the first device with the + specified VID and PID is opened. + + @ingroup API + @param vendor_id The Vendor ID (VID) of the device to open. + @param product_id The Product ID (PID) of the device to open. + @param serial_number The Serial Number of the device to open + (Optionally NULL). + + @returns + This function returns a pointer to a #hid_device object on + success or NULL on failure. + */ + HID_API_EXPORT hid_device * HID_API_CALL hid_open(unsigned short vendor_id, unsigned short product_id, const wchar_t *serial_number); + + /** @brief Open a HID device by its path name. + + The path name be determined by calling hid_enumerate(), or a + platform-specific path name can be used (eg: /dev/hidraw0 on + Linux). + + @ingroup API + @param path The path name of the device to open + + @returns + This function returns a pointer to a #hid_device object on + success or NULL on failure. + */ + HID_API_EXPORT hid_device * HID_API_CALL hid_open_path(const char *path); + + /** @brief Write an Output report to a HID device. + + The first byte of @p data[] must contain the Report ID. For + devices which only support a single report, this must be set + to 0x0. The remaining bytes contain the report data. Since + the Report ID is mandatory, calls to hid_write() will always + contain one more byte than the report contains. For example, + if a hid report is 16 bytes long, 17 bytes must be passed to + hid_write(), the Report ID (or 0x0, for devices with a + single report), followed by the report data (16 bytes). In + this example, the length passed in would be 17. + + hid_write() will send the data on the first OUT endpoint, if + one exists. If it does not, it will send the data through + the Control Endpoint (Endpoint 0). + + @ingroup API + @param device A device handle returned from hid_open(). + @param data The data to send, including the report number as + the first byte. + @param length The length in bytes of the data to send. + + @returns + This function returns the actual number of bytes written and + -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_write(hid_device *device, const unsigned char *data, size_t length); + + /** @brief Read an Input report from a HID device with timeout. + + Input reports are returned + to the host through the INTERRUPT IN endpoint. The first byte will + contain the Report number if the device uses numbered reports. + + @ingroup API + @param device A device handle returned from hid_open(). + @param data A buffer to put the read data into. + @param length The number of bytes to read. For devices with + multiple reports, make sure to read an extra byte for + the report number. + @param milliseconds timeout in milliseconds or -1 for blocking wait. + + @returns + This function returns the actual number of bytes read and + -1 on error. If no packet was available to be read within + the timeout period, this function returns 0. + */ + int HID_API_EXPORT HID_API_CALL hid_read_timeout(hid_device *dev, unsigned char *data, size_t length, int milliseconds); + + /** @brief Read an Input report from a HID device. + + Input reports are returned + to the host through the INTERRUPT IN endpoint. The first byte will + contain the Report number if the device uses numbered reports. + + @ingroup API + @param device A device handle returned from hid_open(). + @param data A buffer to put the read data into. + @param length The number of bytes to read. For devices with + multiple reports, make sure to read an extra byte for + the report number. + + @returns + This function returns the actual number of bytes read and + -1 on error. If no packet was available to be read and + the handle is in non-blocking mode, this function returns 0. + */ + int HID_API_EXPORT HID_API_CALL hid_read(hid_device *device, unsigned char *data, size_t length); + + /** @brief Set the device handle to be non-blocking. + + In non-blocking mode calls to hid_read() will return + immediately with a value of 0 if there is no data to be + read. In blocking mode, hid_read() will wait (block) until + there is data to read before returning. + + Nonblocking can be turned on and off at any time. + + @ingroup API + @param device A device handle returned from hid_open(). + @param nonblock enable or not the nonblocking reads + - 1 to enable nonblocking + - 0 to disable nonblocking. + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_set_nonblocking(hid_device *device, int nonblock); + + /** @brief Send a Feature report to the device. + + Feature reports are sent over the Control endpoint as a + Set_Report transfer. The first byte of @p data[] must + contain the Report ID. For devices which only support a + single report, this must be set to 0x0. The remaining bytes + contain the report data. Since the Report ID is mandatory, + calls to hid_send_feature_report() will always contain one + more byte than the report contains. For example, if a hid + report is 16 bytes long, 17 bytes must be passed to + hid_send_feature_report(): the Report ID (or 0x0, for + devices which do not use numbered reports), followed by the + report data (16 bytes). In this example, the length passed + in would be 17. + + @ingroup API + @param device A device handle returned from hid_open(). + @param data The data to send, including the report number as + the first byte. + @param length The length in bytes of the data to send, including + the report number. + + @returns + This function returns the actual number of bytes written and + -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_send_feature_report(hid_device *device, const unsigned char *data, size_t length); + + /** @brief Get a feature report from a HID device. + + Set the first byte of @p data[] to the Report ID of the + report to be read. Make sure to allow space for this + extra byte in @p data[]. Upon return, the first byte will + still contain the Report ID, and the report data will + start in data[1]. + + @ingroup API + @param device A device handle returned from hid_open(). + @param data A buffer to put the read data into, including + the Report ID. Set the first byte of @p data[] to the + Report ID of the report to be read, or set it to zero + if your device does not use numbered reports. + @param length The number of bytes to read, including an + extra byte for the report ID. The buffer can be longer + than the actual report. + + @returns + This function returns the number of bytes read plus + one for the report ID (which is still in the first + byte), or -1 on error. + */ + int HID_API_EXPORT HID_API_CALL hid_get_feature_report(hid_device *device, unsigned char *data, size_t length); + + /** @brief Close a HID device. + + @ingroup API + @param device A device handle returned from hid_open(). + */ + void HID_API_EXPORT HID_API_CALL hid_close(hid_device *device); + + /** @brief Get The Manufacturer String from a HID device. + + @ingroup API + @param device A device handle returned from hid_open(). + @param string A wide string buffer to put the data into. + @param maxlen The length of the buffer in multiples of wchar_t. + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *device, wchar_t *string, size_t maxlen); + + /** @brief Get The Product String from a HID device. + + @ingroup API + @param device A device handle returned from hid_open(). + @param string A wide string buffer to put the data into. + @param maxlen The length of the buffer in multiples of wchar_t. + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT_CALL hid_get_product_string(hid_device *device, wchar_t *string, size_t maxlen); + + /** @brief Get The Serial Number String from a HID device. + + @ingroup API + @param device A device handle returned from hid_open(). + @param string A wide string buffer to put the data into. + @param maxlen The length of the buffer in multiples of wchar_t. + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *device, wchar_t *string, size_t maxlen); + + /** @brief Get a string from a HID device, based on its string index. + + @ingroup API + @param device A device handle returned from hid_open(). + @param string_index The index of the string to get. + @param string A wide string buffer to put the data into. + @param maxlen The length of the buffer in multiples of wchar_t. + + @returns + This function returns 0 on success and -1 on error. + */ + int HID_API_EXPORT_CALL hid_get_indexed_string(hid_device *device, int string_index, wchar_t *string, size_t maxlen); + + /** @brief Get a string describing the last error which occurred. + + @ingroup API + @param device A device handle returned from hid_open(). + + @returns + This function returns a string containing the last error + which occurred or NULL if none has occurred. + */ + HID_API_EXPORT const wchar_t* HID_API_CALL hid_error(hid_device *device); + +#ifdef __cplusplus +} +#endif + +#endif + -- cgit v1.2.1 From adbc664125142c434294bfa795666c90c7608429 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 18:49:24 +0100 Subject: Adjust for compilation on Visual Studio 2017 Building works however tests are not. Possibly linking with original hidapi solution would work. --- include/LibraryException.h | 2 +- include/command.h | 7 +++++-- include/cxx_semantics.h | 4 ++++ include/device_proto.h | 8 +++++--- include/misc.h | 2 +- include/stick10_commands.h | 8 ++++++++ include/stick20_commands.h | 5 +++++ 7 files changed, 29 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/LibraryException.h b/include/LibraryException.h index e62788d..daf0155 100644 --- a/include/LibraryException.h +++ b/include/LibraryException.h @@ -27,7 +27,7 @@ public: virtual const char *what() const throw() override { std::string s = " "; - auto ts = [](int x){ return std::to_string(x); }; + auto ts = [](size_t x){ return std::to_string(x); }; std::string msg = std::string("Target buffer size is smaller than source: [source size, buffer size]") +s+ ts(source_size) +s+ ts(target_size); return msg.c_str(); diff --git a/include/command.h b/include/command.h index 0a875e4..fc374f7 100644 --- a/include/command.h +++ b/include/command.h @@ -28,6 +28,7 @@ namespace stick20{ template class PasswordCommand : public Command { + constexpr static CommandID _command_id() { return cmd_id; } public: struct CommandPayload { uint8_t kind; @@ -69,8 +70,10 @@ namespace stick20{ } __packed; - typedef Transaction::command_id(), struct CommandPayload, struct EmptyPayload> - CommandTransaction; + //typedef Transaction::command_id(), struct CommandPayload, struct EmptyPayload> + // CommandTransaction; + using CommandTransaction = Transaction; + //using CommandTransaction = Transaction<_command_id(), CommandPayload, EmptyPayload>; }; } diff --git a/include/cxx_semantics.h b/include/cxx_semantics.h index b846317..29e51c3 100644 --- a/include/cxx_semantics.h +++ b/include/cxx_semantics.h @@ -1,7 +1,11 @@ #ifndef CXX_SEMANTICS_H #define CXX_SEMANTICS_H +#ifndef _WINDOWS #define __packed __attribute__((__packed__)) +#else +#define __packed +#endif /* * There's no need to include Boost for a simple subset this project needs. diff --git a/include/device_proto.h b/include/device_proto.h index cbb74fb..1e381dd 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -6,7 +6,6 @@ #include #include #include -#include // a local version for compatibility with Windows #include #include "cxx_semantics.h" @@ -34,6 +33,7 @@ #include #include "DeviceCommunicationExceptions.h" +#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) namespace nitrokey { namespace proto { @@ -43,7 +43,7 @@ namespace nitrokey { * * TODO (future) support for Big Endian */ - +#pragma pack (push,1) /* * Every packet is a USB HID report (check USB spec) */ @@ -181,8 +181,10 @@ namespace nitrokey { typedef command_payload CommandPayload; typedef response_payload ResponsePayload; + typedef struct HIDReport OutgoingPacket; typedef struct DeviceResponse ResponsePacket; +#pragma pack (pop) static_assert(std::is_pod::value, "outgoingpacket must be a pod type"); @@ -216,7 +218,7 @@ namespace nitrokey { static std::mutex send_receive_mtx; std::lock_guard guard(send_receive_mtx); - Log::instance()(__PRETTY_FUNCTION__, Loglevel::DEBUG_L2); + Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); diff --git a/include/misc.h b/include/misc.h index 9e4659d..330654a 100644 --- a/include/misc.h +++ b/include/misc.h @@ -27,7 +27,7 @@ namespace misc { strncpy((char*) &dest, src, s_dest); } - +#define bzero(b,len) (memset((b), '\0', (len)), (void) 0) template typename T::CommandPayload get_payload(){ //Create, initialize and return by value command payload diff --git a/include/stick10_commands.h b/include/stick10_commands.h index 8d37dbd..3d9e234 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -1,5 +1,6 @@ #ifndef STICK10_COMMANDS_H #define STICK10_COMMANDS_H + #include #include #include @@ -8,9 +9,13 @@ #include "command.h" #include "device_proto.h" +#pragma pack (push,1) + namespace nitrokey { namespace proto { + + /* * Stick10 protocol definition */ @@ -844,8 +849,11 @@ class BuildAESKey : Command { typedef Transaction CommandTransaction; + }; + } } } +#pragma pack (pop) #endif diff --git a/include/stick20_commands.h b/include/stick20_commands.h index b887636..e3bea3f 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -1,12 +1,15 @@ #ifndef STICK20_COMMANDS_H #define STICK20_COMMANDS_H + + #include #include "command.h" #include #include #include "device_proto.h" +#pragma pack (push,1) namespace nitrokey { namespace proto { @@ -332,10 +335,12 @@ namespace nitrokey { typedef Transaction CommandTransaction; }; + } } } #undef print_to_ss +#pragma pack (pop) #endif -- cgit v1.2.1 From 6ee68fa294d1d9ab8fa8e61a009845dc31a9b771 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 14 Feb 2017 11:53:25 +0100 Subject: Compiles on MXE, but not working on Windows Signed-off-by: Szczepan Zalega --- include/device.h | 2 +- include/log.h | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index a23e1b3..281c4d9 100644 --- a/include/device.h +++ b/include/device.h @@ -1,7 +1,7 @@ #ifndef DEVICE_H #define DEVICE_H #include -#include +#include "hidapi/hidapi.h" #include #include diff --git a/include/log.h b/include/log.h index 8eda4fb..0b0df8c 100644 --- a/include/log.h +++ b/include/log.h @@ -6,6 +6,10 @@ namespace nitrokey { namespace log { +#ifdef ERROR +#undef ERROR +#endif + enum class Loglevel : int { DEBUG_L2, DEBUG, INFO, WARNING, ERROR }; class LogHandler { -- cgit v1.2.1 From a0df25c10bfc21574d474547bf2f25372bdbb417 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Feb 2017 12:29:28 +0100 Subject: Rename strdup to _strdup under MSVC Signed-off-by: Szczepan Zalega --- include/cxx_semantics.h | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/cxx_semantics.h b/include/cxx_semantics.h index 29e51c3..f358e8f 100644 --- a/include/cxx_semantics.h +++ b/include/cxx_semantics.h @@ -1,12 +1,16 @@ #ifndef CXX_SEMANTICS_H #define CXX_SEMANTICS_H -#ifndef _WINDOWS +#ifndef _MSC_VER #define __packed __attribute__((__packed__)) #else #define __packed #endif +#ifdef _MSC_VER +#define strdup _strdup +#endif + /* * There's no need to include Boost for a simple subset this project needs. */ -- cgit v1.2.1 From 3d96df28fe95deb096a19e3886381ef360b978d8 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Feb 2017 17:48:13 +0100 Subject: Correct device counters Signed-off-by: Szczepan Zalega --- include/device.h | 29 +++++++++++++++++++---------- include/device_proto.h | 18 +++++++++++++++--- 2 files changed, 34 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index 281c4d9..40eb376 100644 --- a/include/device.h +++ b/include/device.h @@ -28,21 +28,30 @@ enum class DeviceModel{ STORAGE }; +#include + class Device { public: struct ErrorCounters{ - int wrong_CRC; - int CRC_other_than_awaited; - int busy; - int total_retries; - int sending_error; - int receiving_error; - int total_comm_runs; - int storage_commands; - int successful; + using cnt = std::atomic_int; + cnt wrong_CRC; + cnt CRC_other_than_awaited; + cnt busy; + cnt total_retries; + cnt sending_error; + cnt receiving_error; + cnt total_comm_runs; + cnt successful_storage_commands; + cnt command_successful_recv; + cnt recv_executed; + cnt sends_executed; + cnt busy_progressbar; + cnt command_result_not_equal_0_recv; + cnt communication_successful; std::string get_as_string(); + } m_counters = {}; @@ -71,7 +80,7 @@ public: bool is_connected(); void show_stats(); - ErrorCounters get_stats(){ return m_counters; } +// ErrorCounters get_stats(){ return m_counters; } int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; std::chrono::milliseconds get_retry_timeout() const { return m_retry_timeout; }; diff --git a/include/device_proto.h b/include/device_proto.h index 1e381dd..3519123 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -245,6 +245,7 @@ namespace nitrokey { int receiving_retry_counter = 0; int sending_retry_counter = dev->get_retry_sending_count(); while (sending_retry_counter-- > 0) { + dev->m_counters.sends_executed++; status = dev->send(&outp); if (status <= 0){ //FIXME early disconnection not yet working properly @@ -261,6 +262,7 @@ namespace nitrokey { // FIXME make checks done in device:recv here receiving_retry_counter = dev->get_retry_receiving_count(); while (receiving_retry_counter-- > 0) { + dev->m_counters.recv_executed++; status = dev->recv(&resp); if (dev->get_device_model() == DeviceModel::STORAGE && @@ -268,7 +270,6 @@ namespace nitrokey { resp.command_id < stick20::CMD_END_VALUE ) { Log::instance()(std::string("Detected storage device cmd, status: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); - dev->m_counters.storage_commands++; resp.last_command_status = static_cast(stick10::command_status::ok); switch (static_cast(resp.storage_status.device_status)) { @@ -362,6 +363,7 @@ namespace nitrokey { if (resp.device_status == static_cast(stick10::device_status::busy) && static_cast(resp.storage_status.device_status) == stick20::device_status::busy_progressbar){ + dev->m_counters.busy_progressbar++; throw LongOperationInProgressException( resp.command_id, resp.device_status, resp.storage_status.progress_bar_value); } @@ -370,10 +372,20 @@ namespace nitrokey { if (receiving_retry_counter <= 0) throw std::runtime_error( "Maximum receiving_retry_counter count reached for receiving response from the device!"); - if (resp.last_command_status != static_cast(stick10::command_status::ok)) + dev->m_counters.communication_successful++; + + if (resp.last_command_status != static_cast(stick10::command_status::ok)){ + dev->m_counters.command_result_not_equal_0_recv++; throw CommandFailedException(resp.command_id, resp.last_command_status); + } - dev->m_counters.successful++; + dev->m_counters.command_successful_recv++; + + if (dev->get_device_model() == DeviceModel::STORAGE && + resp.command_id >= stick20::CMD_START_VALUE && + resp.command_id < stick20::CMD_END_VALUE ) { + dev->m_counters.successful_storage_commands++; + } // See: DeviceResponse return resp; -- cgit v1.2.1 From 8617a13371d087b1eb67bd066926038d289ab331 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 17 Feb 2017 09:27:38 +0100 Subject: Handle SD card related functions Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index b89db63..2200955 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -106,6 +106,8 @@ namespace nitrokey { void fill_SD_card_with_random_data(const char *admin_pin); + int get_SD_card_size(); + void change_update_password(const char *current_update_password, const char *new_update_password); void create_hidden_volume(uint8_t slot_nr, uint8_t start_percent, uint8_t end_percent, @@ -117,8 +119,10 @@ namespace nitrokey { stick20::DeviceConfigurationResponsePacket::ResponsePayload get_status_storage(); const char *get_SD_usage_data_as_string(); + std::pair get_SD_usage_data(); + - int get_progress_bar_value(); + int get_progress_bar_value(); ~NitrokeyManager(); bool is_authorization_command_supported(); @@ -159,6 +163,7 @@ namespace nitrokey { const char *temporary_password) const; bool _disconnect_no_lock(); + }; } -- cgit v1.2.1 From 4e26fdee0c1435016d6642cf8c1f88c3dd5495fa Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 17 Feb 2017 11:16:48 +0100 Subject: Return SD card size as get from the device Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 2200955..7cf55c7 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -106,7 +106,7 @@ namespace nitrokey { void fill_SD_card_with_random_data(const char *admin_pin); - int get_SD_card_size(); + uint8_t get_SD_card_size(); void change_update_password(const char *current_update_password, const char *new_update_password); -- cgit v1.2.1 From 5650e48b114529075d89dbdde0330901351b8460 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 17 Feb 2017 16:29:21 +0100 Subject: Get proper card serial for Storage. Get serial as one number. Signed-off-by: Szczepan Zalega --- include/misc.h | 11 +++++++++++ include/stick10_commands.h | 19 ++++++++----------- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'include') diff --git a/include/misc.h b/include/misc.h index 330654a..c39c741 100644 --- a/include/misc.h +++ b/include/misc.h @@ -6,10 +6,21 @@ #include #include "log.h" #include "LibraryException.h" +#include +#include + namespace nitrokey { namespace misc { + template + std::string toHex(T value){ + using namespace std; + std::ostringstream oss; + oss << std::hex << std::setw(sizeof(value)*2) << std::setfill('0') << value; + return oss.str(); + } + template void strcpyT(T& dest, const char* src){ diff --git a/include/stick10_commands.h b/include/stick10_commands.h index 3d9e234..843d8bd 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -348,7 +348,10 @@ class GetStatus : Command { public: struct ResponsePayload { uint16_t firmware_version; - uint8_t card_serial[4]; + union{ + uint8_t card_serial[4]; + uint32_t card_serial_i; + } __packed; union { uint8_t general_config[5]; struct{ @@ -357,19 +360,12 @@ class GetStatus : Command { uint8_t scrolllock; /** same as numlock */ uint8_t enable_user_password; uint8_t delete_user_password; - }; - }; + } __packed; + } __packed; bool isValid() const { return enable_user_password!=delete_user_password; } std::string get_card_serial_hex() const { -// return ::nitrokey::misc::hexdump((const char *)(card_serial), -// sizeof card_serial, false, false, false); - std::stringstream ss; - ss << std::hex << std::setfill('0'); - for (int i = 0; i < sizeof(card_serial); ++i) { - ss << std::setw(2) << static_cast(card_serial[i]); - } - return ss.str(); + return nitrokey::misc::toHex(card_serial_i); } std::string dissect() const { @@ -378,6 +374,7 @@ class GetStatus : Command { << "[" << firmware_version << "]" << "\t" << ::nitrokey::misc::hexdump( (const char *)(&firmware_version), sizeof firmware_version, false); + ss << "card_serial_i:\t" << std::hex << card_serial_i << std::endl; ss << "card_serial:\t" << ::nitrokey::misc::hexdump((const char *)(card_serial), sizeof card_serial, false); -- cgit v1.2.1 From 6061ee1af573147e41a0834d1c6628eda2fa2f7c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 17 Feb 2017 17:49:20 +0100 Subject: Rename is_connected to be more specific Signed-off-by: Szczepan Zalega --- include/device.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index 40eb376..5d7ee12 100644 --- a/include/device.h +++ b/include/device.h @@ -77,7 +77,12 @@ public: */ virtual int recv(void *packet); - bool is_connected(); + /*** + * Returns true if some device is visible by OS with given VID and PID + * whether the device is connected through HID API or not. + * @return true if visible by OS + */ + bool could_be_enumerated(); void show_stats(); // ErrorCounters get_stats(){ return m_counters; } -- cgit v1.2.1 From 0fb8c08704a338bb9f1b7d3ead4b488bf65cf51e Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 17 Feb 2017 17:52:54 +0100 Subject: Make names more consistent Signed-off-by: Szczepan Zalega --- include/stick10_commands.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/stick10_commands.h b/include/stick10_commands.h index 843d8bd..676aabc 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -350,7 +350,7 @@ class GetStatus : Command { uint16_t firmware_version; union{ uint8_t card_serial[4]; - uint32_t card_serial_i; + uint32_t card_serial_u32; } __packed; union { uint8_t general_config[5]; @@ -365,7 +365,7 @@ class GetStatus : Command { bool isValid() const { return enable_user_password!=delete_user_password; } std::string get_card_serial_hex() const { - return nitrokey::misc::toHex(card_serial_i); + return nitrokey::misc::toHex(card_serial_u32); } std::string dissect() const { @@ -374,7 +374,7 @@ class GetStatus : Command { << "[" << firmware_version << "]" << "\t" << ::nitrokey::misc::hexdump( (const char *)(&firmware_version), sizeof firmware_version, false); - ss << "card_serial_i:\t" << std::hex << card_serial_i << std::endl; + ss << "card_serial_u32:\t" << std::hex << card_serial_u32 << std::endl; ss << "card_serial:\t" << ::nitrokey::misc::hexdump((const char *)(card_serial), sizeof card_serial, false); -- cgit v1.2.1 From e8f6df836522250b9a9d4052722fb9780683058b Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 18 Feb 2017 17:05:02 +0100 Subject: Use proper command code for disabling hidden volume Signed-off-by: Szczepan Zalega --- include/command_id.h | 4 ++-- include/stick20_commands.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/command_id.h b/include/command_id.h index 9d12f93..d0b1454 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -69,9 +69,9 @@ enum class CommandID : uint8_t { SEND_OTP_DATA = 0x17, ENABLE_CRYPTED_PARI = 0x20, - DISABLE_CRYPTED_PARI = 0x20 + 1, //@unused + DISABLE_CRYPTED_PARI = 0x20 + 1, ENABLE_HIDDEN_CRYPTED_PARI = 0x20 + 2, - DISABLE_HIDDEN_CRYPTED_PARI = 0x20 + 3, //@unused + DISABLE_HIDDEN_CRYPTED_PARI = 0x20 + 3, ENABLE_FIRMWARE_UPDATE = 0x20 + 4, //enables update mode EXPORT_FIRMWARE_TO_FILE = 0x20 + 5, GENERATE_NEW_KEYS = 0x20 + 6, diff --git a/include/stick20_commands.h b/include/stick20_commands.h index e3bea3f..d88919b 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -33,7 +33,7 @@ namespace nitrokey { //FIXME the volume disabling commands do not need password class DisableEncryptedPartition : public PasswordCommand {}; - class DisableHiddenEncryptedPartition : public PasswordCommand {}; + class DisableHiddenEncryptedPartition : public PasswordCommand {}; class EnableFirmwareUpdate : public PasswordCommand {}; -- cgit v1.2.1 From e3d88377463f4706372ae7fd6c85937f6035b5ef Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 18 Feb 2017 17:06:10 +0100 Subject: Show Storage status only on matching command code Should not show on commands where it is not supplied Signed-off-by: Szczepan Zalega --- include/dissect.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/dissect.h b/include/dissect.h index 8c975c5..8992c56 100644 --- a/include/dissect.h +++ b/include/dissect.h @@ -98,7 +98,8 @@ class ResponseDissector : semantics::non_constructible { out << "CRC:\t" << std::hex << std::setw(2) << std::setfill('0') << pod.crc << std::endl; - out << "Storage stick status:" << std::endl; + if((int)pod.command_id == pod.storage_status.command_id){ + out << "Storage stick status (where applicable):" << std::endl; #define d(x) out << " "#x": \t"<< std::hex << std::setw(2) \ << std::setfill('0')<< static_cast(x) << std::endl; d(pod.storage_status.command_counter); @@ -106,6 +107,7 @@ class ResponseDissector : semantics::non_constructible { d(pod.storage_status.device_status); d(pod.storage_status.progress_bar_value); #undef d + } out << "Payload:" << std::endl; out << pod.payload.dissect(); -- cgit v1.2.1 From ad286a23ba8a542afe0095b97caf52320778c5e6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 21 Feb 2017 13:12:12 +0100 Subject: Feature check for 320 bits OTP secret Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 6 ++++-- include/log.h | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 7cf55c7..d49941e 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -122,12 +122,14 @@ namespace nitrokey { std::pair get_SD_usage_data(); - int get_progress_bar_value(); + int get_progress_bar_value(); ~NitrokeyManager(); bool is_authorization_command_supported(); + bool is_320_OTP_secret_supported(); - template + + template void authorize_packet(T &package, const char *admin_temporary_password, shared_ptr device); int get_minor_firmware_version(); diff --git a/include/log.h b/include/log.h index 0b0df8c..30fc7fa 100644 --- a/include/log.h +++ b/include/log.h @@ -5,7 +5,7 @@ namespace nitrokey { namespace log { - + #ifdef ERROR #undef ERROR #endif -- cgit v1.2.1 From c13c7fda5b9f69cd46ba40ac5e6cf1cc4bc7e71d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 21 Feb 2017 14:52:36 +0100 Subject: Comments Signed-off-by: Szczepan Zalega --- include/log.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/log.h b/include/log.h index 30fc7fa..695884b 100644 --- a/include/log.h +++ b/include/log.h @@ -5,11 +5,13 @@ namespace nitrokey { namespace log { - + +//for MSVC #ifdef ERROR #undef ERROR #endif + enum class Loglevel : int { DEBUG_L2, DEBUG, INFO, WARNING, ERROR }; class LogHandler { -- cgit v1.2.1 From 29fc4839b7aaf76c3587cf0d268546fd1d1390c4 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 21 Feb 2017 14:56:07 +0100 Subject: Build debug-log-free library for increased security Signed-off-by: Szczepan Zalega --- include/CommandFailedException.h | 2 +- include/LibraryException.h | 2 +- include/LongOperationInProgressException.h | 2 +- include/device_proto.h | 34 +++++++++++++++--------------- include/log.h | 7 ++++++ include/misc.h | 2 +- 6 files changed, 28 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/CommandFailedException.h b/include/CommandFailedException.h index 6ff9a2d..417e850 100644 --- a/include/CommandFailedException.h +++ b/include/CommandFailedException.h @@ -20,7 +20,7 @@ public: CommandFailedException(uint8_t last_command_id, uint8_t last_command_status) : last_command_id(last_command_id), last_command_status(last_command_status){ - nitrokey::log::Log::instance()(std::string("CommandFailedException, status: ")+ std::to_string(last_command_status), nitrokey::log::Loglevel::DEBUG); + LOG(std::string("CommandFailedException, status: ")+ std::to_string(last_command_status), nitrokey::log::Loglevel::DEBUG); } virtual const char *what() const throw() { diff --git a/include/LibraryException.h b/include/LibraryException.h index daf0155..b9303ad 100644 --- a/include/LibraryException.h +++ b/include/LibraryException.h @@ -83,7 +83,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) { - nitrokey::log::Log::instance()(std::string("TooLongStringException, size diff: ")+ std::to_string(size_source-size_destination), nitrokey::log::Loglevel::DEBUG); + LOG(std::string("TooLongStringException, size diff: ")+ std::to_string(size_source-size_destination), nitrokey::log::Loglevel::DEBUG); } diff --git a/include/LongOperationInProgressException.h b/include/LongOperationInProgressException.h index 7f182b0..5b441c0 100644 --- a/include/LongOperationInProgressException.h +++ b/include/LongOperationInProgressException.h @@ -15,7 +15,7 @@ public: LongOperationInProgressException( unsigned char _command_id, uint8_t last_command_status, unsigned char _progress_bar_value) : CommandFailedException(_command_id, last_command_status), progress_bar_value(_progress_bar_value){ - nitrokey::log::Log::instance()( + LOG( std::string("LongOperationInProgressException, progress bar status: ")+ std::to_string(progress_bar_value), nitrokey::log::Loglevel::DEBUG); } diff --git a/include/device_proto.h b/include/device_proto.h index 3519123..ea8f136 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -218,7 +218,7 @@ namespace nitrokey { static std::mutex send_receive_mtx; std::lock_guard guard(send_receive_mtx); - Log::instance()(__FUNCTION__, Loglevel::DEBUG_L2); + LOG(__FUNCTION__, Loglevel::DEBUG_L2); if (dev == nullptr){ throw DeviceNotConnected("Device not initialized"); @@ -236,8 +236,8 @@ namespace nitrokey { outp.payload = payload; outp.update_CRC(); - Log::instance()("Outgoing HID packet:", Loglevel::DEBUG); - Log::instance()(static_cast(outp), Loglevel::DEBUG); + LOG("Outgoing HID packet:", Loglevel::DEBUG); + LOG(static_cast(outp), Loglevel::DEBUG); if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); @@ -249,7 +249,7 @@ namespace nitrokey { status = dev->send(&outp); if (status <= 0){ //FIXME early disconnection not yet working properly -// Log::instance()("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); +// LOG("Encountered communication error, disconnecting device", Loglevel::DEBUG_L2); // dev->disconnect(); dev->m_counters.sending_error++; throw DeviceSendingFailure( @@ -268,7 +268,7 @@ namespace nitrokey { if (dev->get_device_model() == DeviceModel::STORAGE && resp.command_id >= stick20::CMD_START_VALUE && resp.command_id < stick20::CMD_END_VALUE ) { - Log::instance()(std::string("Detected storage device cmd, status: ") + + LOG(std::string("Detected storage device cmd, status: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG_L2); resp.last_command_status = static_cast(stick10::command_status::ok); @@ -286,7 +286,7 @@ namespace nitrokey { resp.device_status = static_cast(stick10::device_status::ok); break; default: - Log::instance()(std::string("Unknown storage device status, cannot translate: ") + + LOG(std::string("Unknown storage device status, cannot translate: ") + std::to_string(resp.storage_status.device_status), Loglevel::DEBUG); resp.device_status = resp.storage_status.device_status; break; @@ -306,11 +306,11 @@ namespace nitrokey { dev->m_counters.busy++; if (busy_counter++<10) { receiving_retry_counter++; - Log::instance()("Status busy, not decreasing receiving_retry_counter counter: " + + LOG("Status busy, not decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } else { busy_counter = 0; - Log::instance()("Status busy, decreasing receiving_retry_counter counter: " + + LOG("Status busy, decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG); } } @@ -320,7 +320,7 @@ namespace nitrokey { successful_communication = true; break; } - Log::instance()(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + LOG(std::string("Retry status - dev status, awaited cmd crc, correct packet CRC: ") + std::to_string(resp.device_status) + " " + std::to_string(CRC_equal_awaited) + " " + std::to_string(resp.isCRCcorrect()), Loglevel::DEBUG_L2); @@ -329,18 +329,18 @@ namespace nitrokey { if (!CRC_equal_awaited) dev->m_counters.CRC_other_than_awaited++; - Log::instance()( + LOG( "Device is not ready or received packet's last CRC is not equal to sent CRC packet, retrying...", Loglevel::DEBUG); - Log::instance()("Invalid incoming HID packet:", Loglevel::DEBUG_L2); - Log::instance()(static_cast(resp), Loglevel::DEBUG_L2); + LOG("Invalid incoming HID packet:", Loglevel::DEBUG_L2); + LOG(static_cast(resp), Loglevel::DEBUG_L2); dev->m_counters.total_retries++; std::this_thread::sleep_for(dev->get_retry_timeout()); continue; } if (successful_communication) break; - Log::instance()(std::string("Resending (outer loop) "), Loglevel::DEBUG_L2); - Log::instance()(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), + LOG(std::string("Resending (outer loop) "), Loglevel::DEBUG_L2); + LOG(std::string("sending_retry_counter count: ") + std::to_string(sending_retry_counter), Loglevel::DEBUG); } @@ -355,9 +355,9 @@ namespace nitrokey { std::to_string(status)); } - Log::instance()("Incoming HID packet:", Loglevel::DEBUG); - Log::instance()(static_cast(resp), Loglevel::DEBUG); - Log::instance()(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), + LOG("Incoming HID packet:", Loglevel::DEBUG); + LOG(static_cast(resp), Loglevel::DEBUG); + LOG(std::string("receiving_retry_counter count: ") + std::to_string(receiving_retry_counter), Loglevel::DEBUG); if (resp.device_status == static_cast(stick10::device_status::busy) && diff --git a/include/log.h b/include/log.h index 695884b..a3e8281 100644 --- a/include/log.h +++ b/include/log.h @@ -53,4 +53,11 @@ class Log { } } + +#ifdef NO_LOG +#define LOG(string, level) while(false){} +#else +#define LOG(string, level) nitrokey::log::Log::instance()((string), (level)) +#endif + #endif diff --git a/include/misc.h b/include/misc.h index c39c741..111d772 100644 --- a/include/misc.h +++ b/include/misc.h @@ -28,7 +28,7 @@ namespace misc { // throw EmptySourceStringException(slot_number); return; const size_t s_dest = sizeof dest; - nitrokey::log::Log::instance()(std::string("strcpyT sizes dest src ") + LOG(std::string("strcpyT sizes dest src ") +std::to_string(s_dest)+ " " +std::to_string(strlen(src))+ " " ,nitrokey::log::Loglevel::DEBUG); -- cgit v1.2.1 From 49e45d532f80c6ffc60c2fbd754030a11dfc8893 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 21 Feb 2017 15:58:47 +0100 Subject: Use nullptr instead of NULL Signed-off-by: Szczepan Zalega --- include/log.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/log.h b/include/log.h index a3e8281..7c25918 100644 --- a/include/log.h +++ b/include/log.h @@ -34,7 +34,7 @@ class Log { Log() : mp_loghandler(&stdlog_handler), m_loglevel(Loglevel::WARNING) {} static Log &instance() { - if (mp_instance == NULL) mp_instance = new Log; + if (mp_instance == nullptr) mp_instance = new Log; return *mp_instance; } -- cgit v1.2.1 From 894cef6e839785700250e7ffaf5c8892afecd394 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 22 Feb 2017 18:06:39 +0100 Subject: Comments Signed-off-by: Szczepan Zalega --- include/command_id.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/command_id.h b/include/command_id.h index d0b1454..d1246dd 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -12,7 +12,7 @@ namespace proto { wrong_password, busy_progressbar, password_matrix_ready, - no_user_password_unlock, + no_user_password_unlock, // FIXME: translate on receive to command status error (fix in firmware?) smartcard_error, security_bit_active }; -- cgit v1.2.1 From 34daa03a2be13f9f38fc29336350236a3e9f541e Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 17:32:47 +0100 Subject: Count lifetime instances of device communication exception Signed-off-by: Szczepan Zalega --- include/DeviceCommunicationExceptions.h | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/DeviceCommunicationExceptions.h b/include/DeviceCommunicationExceptions.h index 78fc625..6d21561 100644 --- a/include/DeviceCommunicationExceptions.h +++ b/include/DeviceCommunicationExceptions.h @@ -1,16 +1,24 @@ #ifndef LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H #define LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H +#include #include +#include #include -//class DeviceCommunicationException: public std::exception { -class DeviceCommunicationException: public std::runtime_error{ + +class DeviceCommunicationException: public std::runtime_error +{ std::string message; + static std::atomic_int occurred; public: - DeviceCommunicationException(std::string _msg): runtime_error(_msg), message(_msg){} + DeviceCommunicationException(std::string _msg): std::runtime_error(_msg), message(_msg){ + ++occurred; + } // virtual const char* what() const throw() override { // return message.c_str(); // } + static bool has_occurred(){ return occurred > 0; }; + static void reset_occurred_flag(){ occurred = 0; }; }; class DeviceNotConnected: public DeviceCommunicationException { @@ -28,4 +36,5 @@ public: DeviceReceivingFailure(std::string msg) : DeviceCommunicationException(msg){} }; + #endif //LIBNITROKEY_DEVICECOMMUNICATIONEXCEPTIONS_H -- cgit v1.2.1 From f12b6a9c29f8d236ca969f45d3be1cd9ee5a749f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 19:13:01 +0100 Subject: Reorder includes Signed-off-by: Szczepan Zalega --- include/stick10_commands.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/stick10_commands.h b/include/stick10_commands.h index 676aabc..a50d0ae 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -6,8 +6,8 @@ #include #include #include -#include "command.h" #include "device_proto.h" +#include "command.h" #pragma pack (push,1) -- cgit v1.2.1 From 802dd4543b7634f498bd44909461eae6d7975abb Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Feb 2017 21:02:30 +0100 Subject: Make device-level reconnect on problem with sending Make it 3 times before throwing exception Call hid_exit on last device disconnection Signed-off-by: Szczepan Zalega --- include/device.h | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/device.h b/include/device.h index 5d7ee12..7b300e5 100644 --- a/include/device.h +++ b/include/device.h @@ -50,6 +50,7 @@ public: cnt busy_progressbar; cnt command_result_not_equal_0_recv; cnt communication_successful; + cnt low_level_reconnect; std::string get_as_string(); } m_counters = {}; @@ -59,7 +60,7 @@ public: const milliseconds send_receive_delay, const int retry_receiving_count, const milliseconds retry_timeout); - virtual ~Device(){show_stats(); disconnect();} + virtual ~Device(); // lack of device is not actually an error, // so it doesn't throw @@ -97,6 +98,9 @@ public: DeviceModel get_device_model() const {return m_model;} private: std::atomic last_command_status; + void _reconnect(); + bool _connect(); + bool _disconnect(); protected: const uint16_t m_vid; @@ -115,6 +119,8 @@ protected: std::atomicmp_devhandle; + + static std::atomic_int instances_count; }; class Stick10 : public Device { -- cgit v1.2.1 From 7132b01a499568c21a7ec64b9c58672541bbb7f6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 7 Mar 2017 16:57:47 +0100 Subject: Handle enabling update mode on Storage device Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index d49941e..71ac6fa 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -101,6 +101,7 @@ namespace nitrokey { void set_unencrypted_read_write(const char *user_pin); void export_firmware(const char *admin_pin); + void enable_firmware_update(const char *firmware_pin); void clear_new_sd_card_warning(const char *admin_pin); -- cgit v1.2.1 From 019d86a056ecb036da243a3304a3220b15e0bfd5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 7 Mar 2017 17:31:46 +0100 Subject: Add TODO comment Signed-off-by: Szczepan Zalega --- include/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index ea8f136..1eb637f 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -370,7 +370,7 @@ namespace nitrokey { if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); if (receiving_retry_counter <= 0) - throw std::runtime_error( + throw std::runtime_error( //TODO change to other kind to handle correctly by caller, communication exception? "Maximum receiving_retry_counter count reached for receiving response from the device!"); dev->m_counters.communication_successful++; -- cgit v1.2.1 From a9c42dea301329136f663ebc9482a1d38feada29 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 17:33:56 +0100 Subject: Allow to check is current device visible to the OS Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 71ac6fa..4f11314 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -44,7 +44,9 @@ namespace nitrokey { bool connect(); bool disconnect(); bool is_connected() throw() ; - DeviceModel get_connected_device_model() const; + bool could_current_device_be_enumerated(); + + DeviceModel get_connected_device_model() const; void set_debug(bool state); stick10::GetStatus::ResponsePayload get_status(); string get_status_as_string(); -- cgit v1.2.1 From 6c2e2c8177ff6bf5731ea25f3211fc6d889628d6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 17:35:39 +0100 Subject: Use own exception types instead of general runtime_error Signed-off-by: Szczepan Zalega --- include/device_proto.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 1eb637f..f1f52d6 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -239,7 +239,7 @@ namespace nitrokey { LOG("Outgoing HID packet:", Loglevel::DEBUG); LOG(static_cast(outp), Loglevel::DEBUG); - if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); + if (!outp.isValid()) throw DeviceSendingFailure("Invalid outgoing packet"); bool successful_communication = false; int receiving_retry_counter = 0; @@ -368,9 +368,9 @@ namespace nitrokey { resp.command_id, resp.device_status, resp.storage_status.progress_bar_value); } - if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); + if (!resp.isValid()) throw DeviceReceivingFailure("Invalid incoming packet"); if (receiving_retry_counter <= 0) - throw std::runtime_error( //TODO change to other kind to handle correctly by caller, communication exception? + throw DeviceReceivingFailure( "Maximum receiving_retry_counter count reached for receiving response from the device!"); dev->m_counters.communication_successful++; -- cgit v1.2.1 From 077aa887ba94e93006feadc28a06c13c829d318a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 9 Mar 2017 17:36:39 +0100 Subject: Dynamically increase delay between retries on busy status Signed-off-by: Szczepan Zalega --- include/device_proto.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index f1f52d6..b137689 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -261,6 +261,8 @@ namespace nitrokey { // FIXME make checks done in device:recv here receiving_retry_counter = dev->get_retry_receiving_count(); + int busy_counter = 0; + auto retry_timeout = dev->get_retry_timeout(); while (receiving_retry_counter-- > 0) { dev->m_counters.recv_executed++; status = dev->recv(&resp); @@ -302,16 +304,17 @@ namespace nitrokey { break; } if (resp.device_status == static_cast(stick10::device_status::busy)) { - static int busy_counter = 0; dev->m_counters.busy++; if (busy_counter++<10) { receiving_retry_counter++; LOG("Status busy, not decreasing receiving_retry_counter counter: " + std::to_string(receiving_retry_counter), Loglevel::DEBUG_L2); } else { + retry_timeout *= 2; busy_counter = 0; LOG("Status busy, decreasing receiving_retry_counter counter: " + - std::to_string(receiving_retry_counter), Loglevel::DEBUG); + std::to_string(receiving_retry_counter) + ", current delay:" + + std::to_string(retry_timeout.count()), Loglevel::DEBUG); } } if (resp.device_status == static_cast(stick10::device_status::busy) && @@ -335,7 +338,7 @@ namespace nitrokey { LOG("Invalid incoming HID packet:", Loglevel::DEBUG_L2); LOG(static_cast(resp), Loglevel::DEBUG_L2); dev->m_counters.total_retries++; - std::this_thread::sleep_for(dev->get_retry_timeout()); + std::this_thread::sleep_for(retry_timeout); continue; } if (successful_communication) break; -- cgit v1.2.1