From 89076a0c44dd12a73060dbfda419c20c4ce5285a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:12:37 +0200 Subject: Migrate commands to new format Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 134 +++++++++++++++++++++------------------------ 1 file changed, 63 insertions(+), 71 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index f4e7500..8e758d4 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -82,68 +82,67 @@ namespace stick20 { CommandTransaction; }; -class EnableEncryptedPartition : semantics::non_constructible { +class EnableEncryptedPartition : Command{ public: struct CommandPayload { uint8_t password[30]; // TODO check w/ firmware }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class DisableEncryptedPartition : semantics::non_constructible { +class DisableEncryptedPartition : Command{ public: - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class EnableHiddenEncryptedPartition : semantics::non_constructible { +class EnableHiddenEncryptedPartition :Command{ public: struct CommandPayload { uint8_t password[30]; // TODO check w/ firmware }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class DisableHiddenEncryptedPartition : semantics::non_constructible { +class DisableHiddenEncryptedPartition :Command{ public: - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class EnableFirmwareUpdate : semantics::non_constructible { +class EnableFirmwareUpdate :Command{ public: struct CommandPayload { uint8_t password[30]; // TODO check w/ firmware }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class UpdatePassword : semantics::non_constructible { +class UpdatePassword :Command{ public: struct CommandPayload { uint8_t old_password[15]; uint8_t new_password[15]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class ExportFirmware : semantics::non_constructible { +class ExportFirmware :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; class CreateNewKeys : Command { @@ -166,50 +165,47 @@ class ExportFirmware : semantics::non_constructible { }; -class FillSDCardWithRandomChars : semantics::non_constructible { +class FillSDCardWithRandomChars :Command{ public: struct CommandPayload { uint8_t volume_flag; uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SetupHiddenVolume : semantics::non_constructible { +class SetupHiddenVolume :Command{ public: - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendPasswordMatrix : semantics::non_constructible { +class SendPasswordMatrix :Command{ public: - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendPasswordMatrixPinData : semantics::non_constructible { +class SendPasswordMatrixPinData :Command{ public: struct CommandPayload { uint8_t pin_data[30]; // TODO how long actually can it be? }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendPasswordMatrixSetup : semantics::non_constructible { +class SendPasswordMatrixSetup :Command{ public: struct CommandPayload { uint8_t setup_data[30]; // TODO how long actually can it be? }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; #define d(x) ss << " "#x":\t" << (int)x << std::endl; @@ -246,95 +242,91 @@ class SendPasswordMatrixSetup : semantics::non_constructible { }; -class SendPassword : semantics::non_constructible { +class SendPassword :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendNewPassword : semantics::non_constructible { +class SendNewPassword :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; // TODO fix original nomenclature -class SendSetReadonlyToUncryptedVolume : semantics::non_constructible { +class SendSetReadonlyToUncryptedVolume :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendSetReadwriteToUncryptedVolume : semantics::non_constructible { +class SendSetReadwriteToUncryptedVolume :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendClearNewSdCardFound : semantics::non_constructible { +class SendClearNewSdCardFound :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendStartup : semantics::non_constructible { +class SendStartup :Command{ public: struct CommandPayload { uint64_t localtime; // POSIX }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class SendHiddenVolumeSetup : semantics::non_constructible { +class SendHiddenVolumeSetup :Command{ public: struct CommandPayload { // TODO HiddenVolumeSetup_tst type }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class LockFirmware : semantics::non_constructible { +class LockFirmware :Command{ public: struct CommandPayload { uint8_t password[30]; }; - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; -class ProductionTest : semantics::non_constructible { +class ProductionTest :Command{ public: - typedef Transaction CommandTransaction; + typedef Transaction + CommandTransaction; }; } } -- cgit v1.2.1 From 3f7aaa09e8f83b68aac29693b536b2d1e6619d83 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:13:03 +0200 Subject: Code reformat Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 634 +++++++++++++++++++++++---------------------- 1 file changed, 322 insertions(+), 312 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 8e758d4..86ac053 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -1,5 +1,6 @@ #ifndef STICK20_COMMANDS_H #define STICK20_COMMANDS_H + #include "inttypes.h" #include "command.h" #include @@ -8,328 +9,337 @@ namespace nitrokey { -namespace proto { + namespace proto { /* * STICK20 protocol command ids * a superset (almost) of STICK10 */ -namespace stick20 { - - enum class PasswordKind : uint8_t { - User = 'P', - Admin = 'A' - }; - - class ChangeAdminUserPin20Current : Command { - public: - struct CommandPayload { - uint8_t kind; - uint8_t old_pin[20]; - std::string dissect() const { - std::stringstream ss; - ss << " old_pin:\t" << old_pin<< std::endl; - return ss.str(); - } - void set_kind(PasswordKind k){ - kind = (uint8_t)k; - } - } __packed; - - typedef Transaction - CommandTransaction; - }; - - - class ChangeAdminUserPin20New : Command { - public: - - struct CommandPayload { - uint8_t kind; - uint8_t new_pin[20]; - std::string dissect() const { - std::stringstream ss; - ss << " new_pin:\t" << new_pin<< std::endl; - return ss.str(); - } - void set_kind(PasswordKind k){ - kind = (uint8_t)k; - } - - } __packed; - - typedef Transaction - CommandTransaction; - }; - - - class UnlockUserPassword : Command { - public: - struct CommandPayload { - uint8_t kind; - uint8_t user_new_password[20]; - std::string dissect() const { - std::stringstream ss; - ss << " user_new_password:\t" << user_new_password<< std::endl; - return ss.str(); - } - void set_kind(PasswordKind k){ - kind = (uint8_t)k; - } - } __packed; - - typedef Transaction - CommandTransaction; - }; - -class EnableEncryptedPartition : Command{ - public: - struct CommandPayload { - uint8_t password[30]; // TODO check w/ firmware - }; - - typedef Transaction - CommandTransaction; -}; - -class DisableEncryptedPartition : Command{ - public: - typedef Transaction - CommandTransaction; -}; - -class EnableHiddenEncryptedPartition :Command{ - public: - struct CommandPayload { - uint8_t password[30]; // TODO check w/ firmware - }; - - typedef Transaction - CommandTransaction; -}; - -class DisableHiddenEncryptedPartition :Command{ - public: - typedef Transaction - CommandTransaction; -}; - -class EnableFirmwareUpdate :Command{ - public: - struct CommandPayload { - uint8_t password[30]; // TODO check w/ firmware - }; - - typedef Transaction - CommandTransaction; -}; - -class UpdatePassword :Command{ - public: - struct CommandPayload { - uint8_t old_password[15]; - uint8_t new_password[15]; - }; - - typedef Transaction - CommandTransaction; -}; - -class ExportFirmware :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - - class CreateNewKeys : Command { - public: - struct CommandPayload { - uint8_t kind; - uint8_t admin_password[30]; //CS20_MAX_PASSWORD_LEN - std::string dissect() const { - std::stringstream ss; - ss << " admin_password:\t" << admin_password<< std::endl; - return ss.str(); - } - void setKindPrefixed(){ - kind = 'P'; - } - } __packed; - - typedef Transaction - CommandTransaction; - }; - - -class FillSDCardWithRandomChars :Command{ - public: - struct CommandPayload { - uint8_t volume_flag; - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - -class SetupHiddenVolume :Command{ - public: - typedef Transaction - CommandTransaction; -}; - -class SendPasswordMatrix :Command{ - public: - typedef Transaction - CommandTransaction; -}; - -class SendPasswordMatrixPinData :Command{ - public: - struct CommandPayload { - uint8_t pin_data[30]; // TODO how long actually can it be? - }; - - typedef Transaction - CommandTransaction; -}; - -class SendPasswordMatrixSetup :Command{ - public: - struct CommandPayload { - uint8_t setup_data[30]; // TODO how long actually can it be? - }; - - typedef Transaction - CommandTransaction; -}; + namespace stick20 { + + enum class PasswordKind : uint8_t { + User = 'P', + Admin = 'A' + }; + + class ChangeAdminUserPin20Current : Command { + public: + struct CommandPayload { + uint8_t kind; + uint8_t old_pin[20]; + + std::string dissect() const { + std::stringstream ss; + ss << " old_pin:\t" << old_pin << std::endl; + return ss.str(); + } + + void set_kind(PasswordKind k) { + kind = (uint8_t) k; + } + } __packed; + + typedef Transaction + CommandTransaction; + }; + + + class ChangeAdminUserPin20New : Command { + public: + + struct CommandPayload { + uint8_t kind; + uint8_t new_pin[20]; + + std::string dissect() const { + std::stringstream ss; + ss << " new_pin:\t" << new_pin << std::endl; + return ss.str(); + } + + void set_kind(PasswordKind k) { + kind = (uint8_t) k; + } + + } __packed; + + typedef Transaction + CommandTransaction; + }; + + + class UnlockUserPassword : Command { + public: + struct CommandPayload { + uint8_t kind; + uint8_t user_new_password[20]; + + std::string dissect() const { + std::stringstream ss; + ss << " user_new_password:\t" << user_new_password << std::endl; + return ss.str(); + } + + void set_kind(PasswordKind k) { + kind = (uint8_t) k; + } + } __packed; + + typedef Transaction + CommandTransaction; + }; + + class EnableEncryptedPartition : Command { + public: + struct CommandPayload { + uint8_t password[30]; // TODO check w/ firmware + }; + + typedef Transaction + CommandTransaction; + }; + + class DisableEncryptedPartition : Command { + public: + typedef Transaction + CommandTransaction; + }; + + class EnableHiddenEncryptedPartition : Command { + public: + struct CommandPayload { + uint8_t password[30]; // TODO check w/ firmware + }; + + typedef Transaction + CommandTransaction; + }; + + class DisableHiddenEncryptedPartition : Command { + public: + typedef Transaction + CommandTransaction; + }; + + class EnableFirmwareUpdate : Command { + public: + struct CommandPayload { + uint8_t password[30]; // TODO check w/ firmware + }; + + typedef Transaction + CommandTransaction; + }; + + class UpdatePassword : Command { + public: + struct CommandPayload { + uint8_t old_password[15]; + uint8_t new_password[15]; + }; + + typedef Transaction + CommandTransaction; + }; + + class ExportFirmware : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class CreateNewKeys : Command { + public: + struct CommandPayload { + uint8_t kind; + uint8_t admin_password[30]; //CS20_MAX_PASSWORD_LEN + std::string dissect() const { + std::stringstream ss; + ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } + + void setKindPrefixed() { + kind = 'P'; + } + } __packed; + + typedef Transaction + CommandTransaction; + }; + + + class FillSDCardWithRandomChars : Command { + public: + struct CommandPayload { + uint8_t volume_flag; + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class SetupHiddenVolume : Command { + public: + typedef Transaction + CommandTransaction; + }; + + class SendPasswordMatrix : Command { + public: + typedef Transaction + CommandTransaction; + }; + + class SendPasswordMatrixPinData : Command { + public: + struct CommandPayload { + uint8_t pin_data[30]; // TODO how long actually can it be? + }; + + typedef Transaction + CommandTransaction; + }; + + class SendPasswordMatrixSetup : Command { + public: + struct CommandPayload { + uint8_t setup_data[30]; // TODO how long actually can it be? + }; + + typedef Transaction + CommandTransaction; + }; #define d(x) ss << " "#x":\t" << (int)x << std::endl; - class GetDeviceStatus : Command { - public: - static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 20 +1; - static const int payload_absolute_begin = 8; - static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; - struct ResponsePayload { - uint8_t _padding[padding_size]; //TODO confirm padding in Storage firmware - //data starts from 21st byte of packet -> 13th byte of payload - uint8_t command_counter; - uint8_t last_command; - uint8_t status; - uint8_t progress_bar_value; - bool isValid() const { return true; } - - std::string dissect() const { - std::stringstream ss; - d(command_counter); - d(last_command); - d(status); - d(progress_bar_value); - ss << "_padding:\t" - << ::nitrokey::misc::hexdump((const char *)(_padding), - sizeof _padding); - return ss.str(); - } - } __packed; - - typedef Transaction - CommandTransaction; - }; - - -class SendPassword :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - -class SendNewPassword :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; + class GetDeviceStatus : Command { + public: + static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 20 + 1; + static const int payload_absolute_begin = 8; + static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; + + struct ResponsePayload { + uint8_t _padding[padding_size]; //TODO confirm padding in Storage firmware + //data starts from 21st byte of packet -> 13th byte of payload + uint8_t command_counter; + uint8_t last_command; + uint8_t status; + uint8_t progress_bar_value; + + bool isValid() const { return true; } + + std::string dissect() const { + std::stringstream ss; + d(command_counter); + d(last_command); + d(status); + d(progress_bar_value); + ss << "_padding:\t" + << ::nitrokey::misc::hexdump((const char *) (_padding), + sizeof _padding); + return ss.str(); + } + } __packed; + + typedef Transaction + CommandTransaction; + }; + + + class SendPassword : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class SendNewPassword : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; // TODO fix original nomenclature -class SendSetReadonlyToUncryptedVolume :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - -class SendSetReadwriteToUncryptedVolume :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - -class SendClearNewSdCardFound :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - -class SendStartup :Command{ - public: - struct CommandPayload { - uint64_t localtime; // POSIX - }; - - typedef Transaction - CommandTransaction; -}; - -class SendHiddenVolumeSetup :Command{ - public: - struct CommandPayload { - // TODO HiddenVolumeSetup_tst type - }; - - typedef Transaction - CommandTransaction; -}; - -class LockFirmware :Command{ - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; -}; - -class ProductionTest :Command{ - public: - typedef Transaction - CommandTransaction; -}; -} -} + class SendSetReadonlyToUncryptedVolume : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class SendSetReadwriteToUncryptedVolume : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class SendClearNewSdCardFound : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class SendStartup : Command { + public: + struct CommandPayload { + uint64_t localtime; // POSIX + }; + + typedef Transaction + CommandTransaction; + }; + + class SendHiddenVolumeSetup : Command { + public: + struct CommandPayload { + // TODO HiddenVolumeSetup_tst type + }; + + typedef Transaction + CommandTransaction; + }; + + class LockFirmware : Command { + public: + struct CommandPayload { + uint8_t password[30]; + }; + + typedef Transaction + CommandTransaction; + }; + + class ProductionTest : Command { + public: + typedef Transaction + CommandTransaction; + }; + } + } } #endif -- cgit v1.2.1 From 323cb033d330129f12ddc19ed96a264e1becf8bb Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:14:24 +0200 Subject: Remove password matrix commands Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 26 -------------------------- 1 file changed, 26 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 86ac053..40352a3 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -190,32 +190,6 @@ namespace nitrokey { CommandTransaction; }; - class SendPasswordMatrix : Command { - public: - typedef Transaction - CommandTransaction; - }; - - class SendPasswordMatrixPinData : Command { - public: - struct CommandPayload { - uint8_t pin_data[30]; // TODO how long actually can it be? - }; - - typedef Transaction - CommandTransaction; - }; - - class SendPasswordMatrixSetup : Command { - public: - struct CommandPayload { - uint8_t setup_data[30]; // TODO how long actually can it be? - }; - - typedef Transaction - CommandTransaction; - }; - #define d(x) ss << " "#x":\t" << (int)x << std::endl; class GetDeviceStatus : Command { -- cgit v1.2.1 From c07390ce32506e009ebf44c2ecc8190d21abd530 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:15:46 +0200 Subject: Remove already implemented commands Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 20 -------------------- 1 file changed, 20 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 40352a3..e57932d 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -226,26 +226,6 @@ namespace nitrokey { }; - class SendPassword : Command { - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; - }; - - class SendNewPassword : Command { - public: - struct CommandPayload { - uint8_t password[30]; - }; - - typedef Transaction - CommandTransaction; - }; - // TODO fix original nomenclature class SendSetReadonlyToUncryptedVolume : Command { public: -- cgit v1.2.1 From 3601f5f97fb64357d9d9221acbd356eec4dbc67a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:20:43 +0200 Subject: Add dissection template to commands Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 60 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index e57932d..b685055 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -93,6 +93,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; // TODO check w/ firmware + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -109,6 +114,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; // TODO check w/ firmware + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -125,6 +135,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; // TODO check w/ firmware + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -136,6 +151,11 @@ namespace nitrokey { struct CommandPayload { uint8_t old_password[15]; uint8_t new_password[15]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -146,6 +166,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -178,6 +203,11 @@ namespace nitrokey { struct CommandPayload { uint8_t volume_flag; uint8_t password[30]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -231,6 +261,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -241,6 +276,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -251,6 +291,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -261,6 +306,11 @@ namespace nitrokey { public: struct CommandPayload { uint64_t localtime; // POSIX + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -271,6 +321,11 @@ namespace nitrokey { public: struct CommandPayload { // TODO HiddenVolumeSetup_tst type + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction @@ -281,6 +336,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t password[30]; + std::string dissect() const { + std::stringstream ss; +// ss << " admin_password:\t" << admin_password << std::endl; + return ss.str(); + } }; typedef Transaction -- cgit v1.2.1 From b0e14aeb25e8d5d799b685bdc844b87617d97c80 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:59:12 +0200 Subject: Add strcpyT to misc.h Signed-off-by: Szczepan Zalega --- include/misc.h | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/misc.h b/include/misc.h index 5fcd16d..48d6a83 100644 --- a/include/misc.h +++ b/include/misc.h @@ -3,11 +3,32 @@ #include #include #include +#include +#include "log.h" +#include "LibraryException.h" namespace nitrokey { namespace misc { -template + template + void strcpyT(T& dest, const char* src){ + + if (src == nullptr) +// throw EmptySourceStringException(slot_number); + return; + const size_t s_dest = sizeof dest; + nitrokey::log::Log::instance()(std::string("strcpyT sizes dest src ") + +std::to_string(s_dest)+ " " + +std::to_string(strlen(src))+ " " + ,nitrokey::log::Loglevel::DEBUG); + if (strlen(src) > s_dest){ + throw TooLongStringException(strlen(src), s_dest, src); + } + strncpy((char*) &dest, src, s_dest); + } + + + template typename T::CommandPayload get_payload(){ //Create, initialize and return by value command payload typename T::CommandPayload st; -- cgit v1.2.1 From 3a856bcfaac6490426f03f0ac149ef5445c5c36f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 18:59:30 +0200 Subject: General password based command Signed-off-by: Szczepan Zalega --- include/command.h | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 42 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/command.h b/include/command.h index 715902d..7f723e5 100644 --- a/include/command.h +++ b/include/command.h @@ -5,19 +5,48 @@ #include "cxx_semantics.h" namespace nitrokey { -namespace proto { - -template -class Command : semantics::non_constructible { - public: - constexpr static CommandID command_id() { return cmd_id; } - - template - static std::string dissect(const T &) { - return std::string("Payload dissection is unavailable"); - } -}; -} + namespace proto { + + template + class Command : semantics::non_constructible { + public: + constexpr static CommandID command_id() { return cmd_id; } + + template + static std::string dissect(const T &) { + return std::string("Payload dissection is unavailable"); + } + }; + +#define print_to_ss(x) ( ss << " #x:\t" << (x) << std::endl ); + + template + class PasswordCommand : public Command { + public: + struct CommandPayload { + uint8_t kind; + uint8_t password[20]; + + std::string dissect() const { + std::stringstream ss; + print_to_ss( kind ); + print_to_ss(password); + return ss.str(); + } + void set_kind_admin() { + kind = (uint8_t) 'A'; + } + void set_kind_user() { + kind = (uint8_t) 'P'; + } + + } __packed; + + typedef Transaction::command_id(), struct CommandPayload, struct EmptyPayload> + CommandTransaction; + + }; + } } #endif -- cgit v1.2.1 From 24f140f34c9c90be54165802edb80fce3408ffa3 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 19:01:13 +0200 Subject: First template based password commands. Initial dissect support for commands Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 49 ++++++++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index b685055..5ee16de 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -15,6 +15,7 @@ namespace nitrokey { * STICK20 protocol command ids * a superset (almost) of STICK10 */ +#define print_to_ss(x) ( ss << " #x:\t" << (x) << std::endl ); namespace stick20 { enum class PasswordKind : uint8_t { @@ -30,6 +31,7 @@ namespace nitrokey { std::string dissect() const { std::stringstream ss; + print_to_ss( kind ); ss << " old_pin:\t" << old_pin << std::endl; return ss.str(); } @@ -53,6 +55,7 @@ namespace nitrokey { std::string dissect() const { std::stringstream ss; + print_to_ss( kind ); ss << " new_pin:\t" << new_pin << std::endl; return ss.str(); } @@ -76,6 +79,7 @@ namespace nitrokey { std::string dissect() const { std::stringstream ss; + print_to_ss( kind ); ss << " user_new_password:\t" << user_new_password << std::endl; return ss.str(); } @@ -89,26 +93,8 @@ namespace nitrokey { CommandTransaction; }; - class EnableEncryptedPartition : Command { - public: - struct CommandPayload { - uint8_t password[30]; // TODO check w/ firmware - std::string dissect() const { - std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; - - class DisableEncryptedPartition : Command { - public: - typedef Transaction - CommandTransaction; - }; + class EnableEncryptedPartition : public PasswordCommand {}; + class DisableEncryptedPartition : public PasswordCommand {}; class EnableHiddenEncryptedPartition : Command { public: @@ -116,7 +102,7 @@ namespace nitrokey { uint8_t password[30]; // TODO check w/ firmware std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( password ); return ss.str(); } }; @@ -137,7 +123,7 @@ namespace nitrokey { uint8_t password[30]; // TODO check w/ firmware std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( password ); return ss.str(); } }; @@ -153,7 +139,8 @@ namespace nitrokey { uint8_t new_password[15]; std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( old_password ); + print_to_ss( new_password ); return ss.str(); } }; @@ -168,7 +155,7 @@ namespace nitrokey { uint8_t password[30]; std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( password ); return ss.str(); } }; @@ -184,6 +171,7 @@ namespace nitrokey { uint8_t admin_password[30]; //CS20_MAX_PASSWORD_LEN std::string dissect() const { std::stringstream ss; + print_to_ss( kind ); ss << " admin_password:\t" << admin_password << std::endl; return ss.str(); } @@ -205,7 +193,8 @@ namespace nitrokey { uint8_t password[30]; std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( (int)volume_flag ); + print_to_ss( password ); return ss.str(); } }; @@ -255,6 +244,8 @@ namespace nitrokey { CommandTransaction; }; +#undef d + // TODO fix original nomenclature class SendSetReadonlyToUncryptedVolume : Command { @@ -263,7 +254,7 @@ namespace nitrokey { uint8_t password[30]; std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( password ); return ss.str(); } }; @@ -278,7 +269,7 @@ namespace nitrokey { uint8_t password[30]; std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( password ); return ss.str(); } }; @@ -293,7 +284,7 @@ namespace nitrokey { uint8_t password[30]; std::string dissect() const { std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; + print_to_ss( password ); return ss.str(); } }; @@ -356,4 +347,6 @@ namespace nitrokey { } } +#undef print_to_ss + #endif -- cgit v1.2.1 From acf8adf7d6920e702cbd2f256ab312f80b781001 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 22 Oct 2016 19:30:42 +0200 Subject: Enable hidden encrypted partition Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 16 +--------------- 1 file changed, 1 insertion(+), 15 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 5ee16de..34dda60 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -95,21 +95,7 @@ namespace nitrokey { class EnableEncryptedPartition : public PasswordCommand {}; class DisableEncryptedPartition : public PasswordCommand {}; - - class EnableHiddenEncryptedPartition : Command { - public: - struct CommandPayload { - uint8_t password[30]; // TODO check w/ firmware - std::string dissect() const { - std::stringstream ss; - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; + class EnableHiddenEncryptedPartition : public PasswordCommand {}; class DisableHiddenEncryptedPartition : Command { public: -- cgit v1.2.1 From 9f0e18f1a91a768717481dbf6b88eedd066a46c9 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 24 Oct 2016 14:42:03 +0200 Subject: Another set of Storage password-only commands Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 118 +++------------------------------------------ 1 file changed, 8 insertions(+), 110 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 34dda60..75bce1c 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -96,27 +96,8 @@ namespace nitrokey { class EnableEncryptedPartition : public PasswordCommand {}; class DisableEncryptedPartition : public PasswordCommand {}; class EnableHiddenEncryptedPartition : public PasswordCommand {}; - - class DisableHiddenEncryptedPartition : Command { - public: - typedef Transaction - CommandTransaction; - }; - - class EnableFirmwareUpdate : Command { - public: - struct CommandPayload { - uint8_t password[30]; // TODO check w/ firmware - std::string dissect() const { - std::stringstream ss; - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; + class DisableHiddenEncryptedPartition : public PasswordCommand {}; + class EnableFirmwareUpdate : public PasswordCommand {}; class UpdatePassword : Command { public: @@ -135,20 +116,7 @@ namespace nitrokey { CommandTransaction; }; - class ExportFirmware : Command { - public: - struct CommandPayload { - uint8_t password[30]; - std::string dissect() const { - std::stringstream ss; - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; + class ExportFirmware : public PasswordCommand {}; class CreateNewKeys : Command { public: @@ -171,23 +139,7 @@ namespace nitrokey { CommandTransaction; }; - - class FillSDCardWithRandomChars : Command { - public: - struct CommandPayload { - uint8_t volume_flag; - uint8_t password[30]; - std::string dissect() const { - std::stringstream ss; - print_to_ss( (int)volume_flag ); - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; + class FillSDCardWithRandomChars : public PasswordCommand {}; class SetupHiddenVolume : Command { public: @@ -234,50 +186,9 @@ namespace nitrokey { // TODO fix original nomenclature - class SendSetReadonlyToUncryptedVolume : Command { - public: - struct CommandPayload { - uint8_t password[30]; - std::string dissect() const { - std::stringstream ss; - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; - - class SendSetReadwriteToUncryptedVolume : Command { - public: - struct CommandPayload { - uint8_t password[30]; - std::string dissect() const { - std::stringstream ss; - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; - - class SendClearNewSdCardFound : Command { - public: - struct CommandPayload { - uint8_t password[30]; - std::string dissect() const { - std::stringstream ss; - print_to_ss( password ); - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; + class SendSetReadonlyToUncryptedVolume : public PasswordCommand {}; + class SendSetReadwriteToUncryptedVolume : public PasswordCommand {}; + class SendClearNewSdCardFound : public PasswordCommand {}; class SendStartup : Command { public: @@ -309,20 +220,7 @@ namespace nitrokey { CommandTransaction; }; - class LockFirmware : Command { - public: - struct CommandPayload { - uint8_t password[30]; - std::string dissect() const { - std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; - return ss.str(); - } - }; - - typedef Transaction - CommandTransaction; - }; + class LockFirmware : public PasswordCommand {}; class ProductionTest : Command { public: -- cgit v1.2.1 From ec8b19e231390e25860218000d77d622d94782c7 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 24 Oct 2016 14:44:38 +0200 Subject: Remove obsolete macro Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 75bce1c..b3dbacf 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -147,7 +147,6 @@ namespace nitrokey { CommandTransaction; }; -#define d(x) ss << " "#x":\t" << (int)x << std::endl; class GetDeviceStatus : Command { public: @@ -167,10 +166,10 @@ namespace nitrokey { std::string dissect() const { std::stringstream ss; - d(command_counter); - d(last_command); - d(status); - d(progress_bar_value); + print_to_ss((int)command_counter); + print_to_ss((int)last_command); + print_to_ss((int)status); + print_to_ss((int)progress_bar_value); ss << "_padding:\t" << ::nitrokey::misc::hexdump((const char *) (_padding), sizeof _padding); @@ -182,8 +181,6 @@ namespace nitrokey { CommandTransaction; }; -#undef d - // TODO fix original nomenclature class SendSetReadonlyToUncryptedVolume : public PasswordCommand {}; -- cgit v1.2.1 From 9a56e6c27104ead60a89c403e77494bfcfea563f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 24 Oct 2016 14:52:10 +0200 Subject: Remove static keyword from CommandID dissection Signed-off-by: Szczepan Zalega --- include/command.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/command.h b/include/command.h index 7f723e5..6e3525e 100644 --- a/include/command.h +++ b/include/command.h @@ -13,7 +13,7 @@ namespace nitrokey { constexpr static CommandID command_id() { return cmd_id; } template - static std::string dissect(const T &) { + std::string dissect(const T &) { return std::string("Payload dissection is unavailable"); } }; -- cgit v1.2.1 From 33a2d790e0a68d243bc1d9e9f75590b141d85c7f Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 24 Oct 2016 16:19:00 +0200 Subject: Fix printing macro Signed-off-by: Szczepan Zalega --- include/command.h | 3 ++- include/stick20_commands.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/command.h b/include/command.h index 6e3525e..5ac1d7f 100644 --- a/include/command.h +++ b/include/command.h @@ -18,7 +18,7 @@ namespace nitrokey { } }; -#define print_to_ss(x) ( ss << " #x:\t" << (x) << std::endl ); +#define print_to_ss(x) ( ss << " " << (#x) <<":\t" << (x) << std::endl ); template class PasswordCommand : public Command { @@ -48,5 +48,6 @@ namespace nitrokey { }; } } +#undef print_to_ss #endif diff --git a/include/stick20_commands.h b/include/stick20_commands.h index b3dbacf..5679681 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -15,7 +15,7 @@ namespace nitrokey { * STICK20 protocol command ids * a superset (almost) of STICK10 */ -#define print_to_ss(x) ( ss << " #x:\t" << (x) << std::endl ); +#define print_to_ss(x) ( ss << " " << (#x) <<":\t" << (x) << std::endl ); namespace stick20 { enum class PasswordKind : uint8_t { -- cgit v1.2.1 From 8f70b382067c932232dbf3966d8859266986004d Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 24 Oct 2016 17:40:50 +0200 Subject: Try to disconnect when destroying device Signed-off-by: Szczepan Zalega --- include/device.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/device.h b/include/device.h index 34b7a5b..3f18921 100644 --- a/include/device.h +++ b/include/device.h @@ -21,6 +21,7 @@ class Device { public: Device(); + virtual ~Device(){disconnect();} // lack of device is not actually an error, // so it doesn't throw -- cgit v1.2.1 From 385d33bb4c8bb7fe604f5e0acc8aeca5f2146fae Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 25 Oct 2016 18:40:30 +0200 Subject: Handle busy_progressbar device status Signed-off-by: Szczepan Zalega --- include/LongOperationInProgressException.h | 28 ++++++++++++++++++++++++++++ include/device_proto.h | 21 +++++++++++++++++++-- 2 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 include/LongOperationInProgressException.h (limited to 'include') diff --git a/include/LongOperationInProgressException.h b/include/LongOperationInProgressException.h new file mode 100644 index 0000000..673a218 --- /dev/null +++ b/include/LongOperationInProgressException.h @@ -0,0 +1,28 @@ +// +// Created by sz on 24.10.16. +// + +#ifndef LIBNITROKEY_LONGOPERATIONINPROGRESSEXCEPTION_H +#define LIBNITROKEY_LONGOPERATIONINPROGRESSEXCEPTION_H + + +class LongOperationInProgressException : public std::exception { + +public: + unsigned char progress_bar_value; + unsigned char command_id; + + LongOperationInProgressException(unsigned char _command_id, unsigned char _progress_bar_value) { + command_id = _command_id; + progress_bar_value = _progress_bar_value; + nitrokey::log::Log::instance()( + std::string("LongOperationInProgressException, progress bar status: ")+ + std::to_string(progress_bar_value), nitrokey::log::Loglevel::DEBUG); + } + virtual const char *what() const throw() { + return "Device returned busy status with long operation in progress"; + } +}; + + +#endif //LIBNITROKEY_LONGOPERATIONINPROGRESSEXCEPTION_H diff --git a/include/device_proto.h b/include/device_proto.h index cde1d51..037451a 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -16,6 +16,7 @@ #include "command_id.h" #include "dissect.h" #include "CommandFailedException.h" +#include "LongOperationInProgressException.h" #define STICK20_UPDATE_MODE_VID 0x03EB #define STICK20_UPDATE_MODE_PID 0x2FF1 @@ -216,6 +217,7 @@ namespace nitrokey { if (!outp.isValid()) throw std::runtime_error("Invalid outgoing packet"); + bool successful_communication = false; int receiving_retry_counter = 0; int sending_retry_counter = dev.get_retry_sending_count(); while (sending_retry_counter-- > 0) { @@ -263,12 +265,21 @@ 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; if (resp.device_status == static_cast(stick10::device_status::ok) && - resp.last_command_crc == outp.crc && resp.isValid()) break; + resp.last_command_crc == outp.crc && resp.isValid()){ + successful_communication = true; + 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); } + if (resp.device_status == static_cast(stick10::device_status::busy) && + static_cast(resp.storage_status.device_status) + == stick20::device_status::busy_progressbar){ + successful_communication = true; + break; + } Log::instance()(std::string("Retry status - dev status, equal crc, correct CRC: ") + std::to_string(resp.device_status) + " " + std::to_string(resp.last_command_crc == outp.crc) + @@ -282,7 +293,7 @@ namespace nitrokey { std::this_thread::sleep_for(dev.get_retry_timeout()); continue; } - if (resp.device_status == 0 && resp.last_command_crc == outp.crc) break; + 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), Loglevel::DEBUG); @@ -297,6 +308,12 @@ namespace nitrokey { std::string("Device error while executing command ") + std::to_string(status)); + if (resp.device_status == static_cast(stick10::device_status::busy) && + static_cast(resp.storage_status.device_status) + == stick20::device_status::busy_progressbar){ + throw LongOperationInProgressException(resp.command_id, resp.storage_status.progress_bar_value); + } + 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), -- cgit v1.2.1 From b0412c73b8ff129183f570a4eed72bdafd9f55d3 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 25 Oct 2016 18:41:59 +0200 Subject: Handle FillSDCardWithRandomChars command and test it Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 5679681..a335809 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -139,7 +139,43 @@ namespace nitrokey { CommandTransaction; }; - class FillSDCardWithRandomChars : public PasswordCommand {}; +// class FillSDCardWithRandomChars : public PasswordCommand {}; + + + class FillSDCardWithRandomChars : Command { + public: + enum class ChosenVolumes : uint8_t { + all_volumes = 0, + encrypted_volume = 1 + }; + + struct CommandPayload { + uint8_t volume_flag; + uint8_t kind; + uint8_t password[20]; + + std::string dissect() const { + std::stringstream ss; + print_to_ss( (int) volume_flag ); + print_to_ss( kind ); + print_to_ss(password); + return ss.str(); + } + void set_kind_user() { + kind = (uint8_t) 'P'; + } + void set_defaults(){ + set_kind_user(); + volume_flag = static_cast(ChosenVolumes::encrypted_volume); + } + + } __packed; + + typedef Transaction::command_id(), + struct CommandPayload, struct EmptyPayload> + CommandTransaction; + }; + class SetupHiddenVolume : Command { public: -- cgit v1.2.1 From e828759986f3c9633fb0177bf446752480a66adf Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 27 Oct 2016 19:40:57 +0200 Subject: Set commands enum values by hand for greater verbosity Signed-off-by: Szczepan Zalega --- include/command_id.h | 52 ++++++++++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 26 deletions(-) (limited to 'include') diff --git a/include/command_id.h b/include/command_id.h index 8148cc1..76c7132 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -70,39 +70,39 @@ enum class CommandID : uint8_t { STICK20_CMD_SEND_NEW_PASSWORD = stick20::CMD_START_VALUE + 19, ENABLE_CRYPTED_PARI = 0x20, - DISABLE_CRYPTED_PARI, - ENABLE_HIDDEN_CRYPTED_PARI, - DISABLE_HIDDEN_CRYPTED_PARI, - ENABLE_FIRMWARE_UPDATE, - EXPORT_FIRMWARE_TO_FILE, - GENERATE_NEW_KEYS, - FILL_SD_CARD_WITH_RANDOM_CHARS, + DISABLE_CRYPTED_PARI = 0x20 + 1, + ENABLE_HIDDEN_CRYPTED_PARI = 0x20 + 2, + DISABLE_HIDDEN_CRYPTED_PARI = 0x20 + 3, + ENABLE_FIRMWARE_UPDATE = 0x20 + 4, + EXPORT_FIRMWARE_TO_FILE = 0x20 + 5, + GENERATE_NEW_KEYS = 0x20 + 6, + FILL_SD_CARD_WITH_RANDOM_CHARS = 0x20 + 7, - WRITE_STATUS_DATA, - ENABLE_READONLY_UNCRYPTED_LUN, - ENABLE_READWRITE_UNCRYPTED_LUN, + WRITE_STATUS_DATA = 0x20 + 8, + ENABLE_READONLY_UNCRYPTED_LUN = 0x20 + 9, + ENABLE_READWRITE_UNCRYPTED_LUN = 0x20 + 10, - SEND_PASSWORD_MATRIX, - SEND_PASSWORD_MATRIX_PINDATA, - SEND_PASSWORD_MATRIX_SETUP, + SEND_PASSWORD_MATRIX = 0x20 + 11, + SEND_PASSWORD_MATRIX_PINDATA = 0x20 + 12, + SEND_PASSWORD_MATRIX_SETUP = 0x20 + 13, - GET_DEVICE_STATUS, - SEND_DEVICE_STATUS, + GET_DEVICE_STATUS = 0x20 + 14, + SEND_DEVICE_STATUS = 0x20 + 15, - SEND_HIDDEN_VOLUME_PASSWORD, - SEND_HIDDEN_VOLUME_SETUP, - SEND_PASSWORD, - SEND_NEW_PASSWORD, - CLEAR_NEW_SD_CARD_FOUND, + SEND_HIDDEN_VOLUME_PASSWORD = 0x20 + 16, + SEND_HIDDEN_VOLUME_SETUP = 0x20 + 17, + SEND_PASSWORD = 0x20 + 18, + SEND_NEW_PASSWORD = 0x20 + 19, + CLEAR_NEW_SD_CARD_FOUND = 0x20 + 20, - SEND_STARTUP, - SEND_CLEAR_STICK_KEYS_NOT_INITIATED, - SEND_LOCK_STICK_HARDWARE, + SEND_STARTUP = 0x20 + 21, + SEND_CLEAR_STICK_KEYS_NOT_INITIATED = 0x20 + 22, + SEND_LOCK_STICK_HARDWARE = 0x20 + 23, - PRODUCTION_TEST, - SEND_DEBUG_DATA, + PRODUCTION_TEST = 0x20 + 24, + SEND_DEBUG_DATA = 0x20 + 25, - CHANGE_UPDATE_PIN, + CHANGE_UPDATE_PIN = 0x20 + 26, GET_PW_SAFE_SLOT_STATUS = 0x60, GET_PW_SAFE_SLOT_NAME = 0x61, -- cgit v1.2.1 From a4d93360a600d5361a825c5270cb313202e1f318 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 27 Oct 2016 19:42:17 +0200 Subject: Fix naming for change password commands ids Signed-off-by: Szczepan Zalega --- include/command_id.h | 3 --- include/stick20_commands.h | 4 ++-- 2 files changed, 2 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/command_id.h b/include/command_id.h index 76c7132..aa65dd3 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -66,9 +66,6 @@ enum class CommandID : uint8_t { CHANGE_USER_PIN = 0x14, CHANGE_ADMIN_PIN = 0x15, - STICK20_CMD_SEND_PASSWORD = stick20::CMD_START_VALUE + 18, - STICK20_CMD_SEND_NEW_PASSWORD = stick20::CMD_START_VALUE + 19, - ENABLE_CRYPTED_PARI = 0x20, DISABLE_CRYPTED_PARI = 0x20 + 1, ENABLE_HIDDEN_CRYPTED_PARI = 0x20 + 2, diff --git a/include/stick20_commands.h b/include/stick20_commands.h index a335809..7b69ec6 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -23,7 +23,7 @@ namespace nitrokey { Admin = 'A' }; - class ChangeAdminUserPin20Current : Command { + class ChangeAdminUserPin20Current : Command { public: struct CommandPayload { uint8_t kind; @@ -46,7 +46,7 @@ namespace nitrokey { }; - class ChangeAdminUserPin20New : Command { + class ChangeAdminUserPin20New : Command { public: struct CommandPayload { -- cgit v1.2.1 From b727d90d4e599762ff3eebff2349ae0fed1e0743 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 27 Oct 2016 20:10:22 +0200 Subject: Support Storage status commands: SendStartup ProductionTest Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 170 +++++++++++++++++++++++++++++++++++---------- 1 file changed, 133 insertions(+), 37 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 7b69ec6..4bbb8c0 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -177,35 +177,78 @@ namespace nitrokey { }; - class SetupHiddenVolume : Command { - public: - typedef Transaction - CommandTransaction; - }; - class GetDeviceStatus : Command { + class SendStartup : Command { public: - static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 20 + 1; + struct CommandPayload { + uint64_t localtime; // POSIX seconds from epoch start, supports until year 2106 + std::string dissect() const { + std::stringstream ss; + print_to_ss( localtime ); + return ss.str(); + } + void set_defaults(){ + localtime = + std::chrono::duration_cast ( + std::chrono::system_clock::now().time_since_epoch()).count(); + } + }__packed; + + static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; static const int payload_absolute_begin = 8; static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; struct ResponsePayload { - uint8_t _padding[padding_size]; //TODO confirm padding in Storage firmware - //data starts from 21st byte of packet -> 13th byte of payload - uint8_t command_counter; - uint8_t last_command; - uint8_t status; - uint8_t progress_bar_value; + uint8_t _padding[padding_size]; + + uint8_t SendCounter_u8; + uint8_t SendDataType_u8; + uint8_t FollowBytesFlag_u8; + uint8_t SendSize_u8; + + uint16_t MagicNumber_StickConfig_u16; + uint8_t ReadWriteFlagUncryptedVolume_u8; + uint8_t ReadWriteFlagCryptedVolume_u8; + uint8_t VersionInfo_au8[4]; + uint8_t ReadWriteFlagHiddenVolume_u8; + uint8_t FirmwareLocked_u8; + uint8_t NewSDCardFound_u8; + uint8_t SDFillWithRandomChars_u8; + uint32_t ActiveSD_CardID_u32; + uint8_t VolumeActiceFlag_u8; + uint8_t NewSmartCardFound_u8; + uint8_t UserPwRetryCount; + uint8_t AdminPwRetryCount; + uint32_t ActiveSmartCardID_u32; + uint8_t StickKeysNotInitiated; bool isValid() const { return true; } std::string dissect() const { std::stringstream ss; - print_to_ss((int)command_counter); - print_to_ss((int)last_command); - print_to_ss((int)status); - print_to_ss((int)progress_bar_value); + + print_to_ss((int) SendCounter_u8 ); + print_to_ss((int) SendDataType_u8 ); + print_to_ss((int) FollowBytesFlag_u8 ); + print_to_ss((int) SendSize_u8 ); + + print_to_ss( MagicNumber_StickConfig_u16 ); + print_to_ss((int) ReadWriteFlagUncryptedVolume_u8 ); + print_to_ss((int) ReadWriteFlagCryptedVolume_u8 ); + print_to_ss((int) VersionInfo_au8[1] ); + print_to_ss((int) VersionInfo_au8[3] ); + print_to_ss((int) ReadWriteFlagHiddenVolume_u8 ); + print_to_ss((int) FirmwareLocked_u8 ); + print_to_ss((int) NewSDCardFound_u8 ); + print_to_ss((int) SDFillWithRandomChars_u8 ); + print_to_ss( ActiveSD_CardID_u32 ); + print_to_ss((int) VolumeActiceFlag_u8 ); + print_to_ss((int) NewSmartCardFound_u8 ); + print_to_ss((int) UserPwRetryCount ); + print_to_ss((int) AdminPwRetryCount ); + print_to_ss( ActiveSmartCardID_u32 ); + print_to_ss((int) StickKeysNotInitiated ); ss << "_padding:\t" << ::nitrokey::misc::hexdump((const char *) (_padding), sizeof _padding); @@ -213,7 +256,7 @@ namespace nitrokey { } } __packed; - typedef Transaction + typedef Transaction CommandTransaction; }; @@ -223,41 +266,94 @@ namespace nitrokey { class SendSetReadwriteToUncryptedVolume : public PasswordCommand {}; class SendClearNewSdCardFound : public PasswordCommand {}; - class SendStartup : Command { + class GetDeviceStatus : Command { public: - struct CommandPayload { - uint64_t localtime; // POSIX - std::string dissect() const { - std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; - return ss.str(); - } - }; - typedef Transaction + typedef Transaction CommandTransaction; }; - class SendHiddenVolumeSetup : Command { + class SendHiddenVolumePassword : public PasswordCommand {}; + + class SetupHiddenVolume : Command { public: + constexpr static int MAX_HIDDEN_VOLUME_PASSOWORD_SIZE = 20; struct CommandPayload { - // TODO HiddenVolumeSetup_tst type - std::string dissect() const { - std::stringstream ss; -// ss << " admin_password:\t" << admin_password << std::endl; - return ss.str(); - } - }; + uint8_t SlotNr_u8; + uint8_t StartBlockPercent_u8; + uint8_t EndBlockPercent_u8; + uint8_t HiddenVolumePassword_au8[MAX_HIDDEN_VOLUME_PASSOWORD_SIZE + 1]; + }__packed; typedef Transaction CommandTransaction; }; + class LockFirmware : public PasswordCommand {}; class ProductionTest : Command { public: - typedef Transaction + static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; + static const int payload_absolute_begin = 8; + static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; + + struct ResponsePayload { + uint8_t _padding[padding_size]; + + uint8_t SendCounter_u8; + uint8_t SendDataType_u8; + uint8_t FollowBytesFlag_u8; + uint8_t SendSize_u8; + + uint8_t FirmwareVersion_au8[2]; // 2 byte // 2 + uint8_t FirmwareVersionInternal_u8; // 1 byte // 3 + uint8_t SD_Card_Size_u8; // 1 byte // 4 + uint32_t CPU_CardID_u32; // 4 byte // 8 + uint32_t SmartCardID_u32; // 4 byte // 12 + uint32_t SD_CardID_u32; // 4 byte // 16 + uint8_t SC_UserPwRetryCount; // User PIN retry count 1 byte // 17 + uint8_t SC_AdminPwRetryCount; // Admin PIN retry count 1 byte // 18 + uint8_t SD_Card_ManufacturingYear_u8; // 1 byte // 19 + uint8_t SD_Card_ManufacturingMonth_u8; // 1 byte // 20 + uint16_t SD_Card_OEM_u16; // 2 byte // 22 + uint16_t SD_WriteSpeed_u16; // in kbyte / sec 2 byte // 24 + uint8_t SD_Card_Manufacturer_u8; // 1 byte // 25 + + bool isValid() const { return true; } + + std::string dissect() const { + std::stringstream ss; + + print_to_ss((int) SendCounter_u8); + print_to_ss((int) SendDataType_u8); + print_to_ss((int) FollowBytesFlag_u8); + print_to_ss((int) SendSize_u8); + + print_to_ss((int) FirmwareVersion_au8[0]); + print_to_ss((int) FirmwareVersion_au8[1]); + print_to_ss((int) FirmwareVersionInternal_u8); + print_to_ss((int) SD_Card_Size_u8); + print_to_ss( CPU_CardID_u32); + print_to_ss( SmartCardID_u32); + print_to_ss( SD_CardID_u32); + print_to_ss((int) SC_UserPwRetryCount); + print_to_ss((int) SC_AdminPwRetryCount); + print_to_ss((int) SD_Card_ManufacturingYear_u8); + print_to_ss((int) SD_Card_ManufacturingMonth_u8); + print_to_ss( SD_Card_OEM_u16); + print_to_ss( SD_WriteSpeed_u16); + print_to_ss((int) SD_Card_Manufacturer_u8); + + ss << "_padding:\t" + << ::nitrokey::misc::hexdump((const char *) (_padding), + sizeof _padding); + return ss.str(); + } + + } __packed; + + typedef Transaction CommandTransaction; }; } -- cgit v1.2.1 From d58f3fd3bb6df5d3f46e3c4c3b719174e31b7201 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 27 Oct 2016 21:14:17 +0200 Subject: Support for setting hidden volume and test for it Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 4bbb8c0..a1e65c2 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -282,8 +282,23 @@ namespace nitrokey { uint8_t SlotNr_u8; uint8_t StartBlockPercent_u8; uint8_t EndBlockPercent_u8; - uint8_t HiddenVolumePassword_au8[MAX_HIDDEN_VOLUME_PASSOWORD_SIZE + 1]; - }__packed; + uint8_t HiddenVolumePassword_au8[MAX_HIDDEN_VOLUME_PASSOWORD_SIZE + 1]; //last char is a null terminator TODO check if it's needed + std::string dissect() const { + std::stringstream ss; + print_to_ss((int) SlotNr_u8); + print_to_ss((int) StartBlockPercent_u8); + print_to_ss((int) EndBlockPercent_u8); + ss << " HiddenVolumePassword_au8:\t" + << HiddenVolumePassword_au8 << std::endl; + return ss.str(); + } + + void set_defaults(){ + SlotNr_u8 = 0; + StartBlockPercent_u8 = 70; + EndBlockPercent_u8 = 90; + } + } __packed; typedef Transaction CommandTransaction; -- cgit v1.2.1 From 3b1926164b2e0eb91cf3a7e9c854cca85f7d4c8e Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 28 Oct 2016 14:33:25 +0200 Subject: Support for changing firmware update password and test Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index a1e65c2..676b3df 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -99,15 +99,17 @@ namespace nitrokey { class DisableHiddenEncryptedPartition : public PasswordCommand {}; class EnableFirmwareUpdate : public PasswordCommand {}; - class UpdatePassword : Command { + class ChangeUpdatePassword : Command { public: struct CommandPayload { - uint8_t old_password[15]; - uint8_t new_password[15]; + uint8_t __gap; + uint8_t current_update_password[20]; + uint8_t __gap2; + uint8_t new_update_password[20]; std::string dissect() const { std::stringstream ss; - print_to_ss( old_password ); - print_to_ss( new_password ); + print_to_ss( current_update_password ); + print_to_ss( new_update_password ); return ss.str(); } }; -- cgit v1.2.1 From e45e2f03cf40b5826632ab95cbc79c487249897a Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 28 Oct 2016 14:34:25 +0200 Subject: Mark unused commands and remove obsolete code Signed-off-by: Szczepan Zalega --- include/command_id.h | 8 ++++---- include/stick20_commands.h | 3 --- 2 files changed, 4 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/command_id.h b/include/command_id.h index aa65dd3..9cace2f 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -79,14 +79,14 @@ enum class CommandID : uint8_t { ENABLE_READONLY_UNCRYPTED_LUN = 0x20 + 9, ENABLE_READWRITE_UNCRYPTED_LUN = 0x20 + 10, - SEND_PASSWORD_MATRIX = 0x20 + 11, - SEND_PASSWORD_MATRIX_PINDATA = 0x20 + 12, - SEND_PASSWORD_MATRIX_SETUP = 0x20 + 13, + SEND_PASSWORD_MATRIX = 0x20 + 11, //@unused + SEND_PASSWORD_MATRIX_PINDATA = 0x20 + 12, //@unused + SEND_PASSWORD_MATRIX_SETUP = 0x20 + 13, //@unused GET_DEVICE_STATUS = 0x20 + 14, SEND_DEVICE_STATUS = 0x20 + 15, - SEND_HIDDEN_VOLUME_PASSWORD = 0x20 + 16, + SEND_HIDDEN_VOLUME_PASSWORD = 0x20 + 16, //@unused SEND_HIDDEN_VOLUME_SETUP = 0x20 + 17, SEND_PASSWORD = 0x20 + 18, SEND_NEW_PASSWORD = 0x20 + 19, diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 676b3df..237118d 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -141,8 +141,6 @@ namespace nitrokey { CommandTransaction; }; -// class FillSDCardWithRandomChars : public PasswordCommand {}; - class FillSDCardWithRandomChars : Command { public: @@ -275,7 +273,6 @@ namespace nitrokey { CommandTransaction; }; - class SendHiddenVolumePassword : public PasswordCommand {}; class SetupHiddenVolume : Command { public: -- cgit v1.2.1 From 6343eea03980627cb763de777af6b316130bdf43 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 28 Oct 2016 14:55:56 +0200 Subject: Support getting SD card occupancy and test Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 237118d..42f46ad 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -273,6 +273,27 @@ namespace nitrokey { CommandTransaction; }; + class GetSDCardOccupancy : Command { + public: + struct ResponsePayload { + uint8_t WriteLevelMin; + uint8_t WriteLevelMax; + uint8_t ReadLevelMin; + uint8_t ReadLevelMax; + std::string dissect() const { + std::stringstream ss; + print_to_ss((int) WriteLevelMin); + print_to_ss((int) WriteLevelMax); + print_to_ss((int) ReadLevelMin); + print_to_ss((int) ReadLevelMax); + return ss.str(); + } + } __packed; + + typedef Transaction + CommandTransaction; + }; + class SetupHiddenVolume : Command { public: -- cgit v1.2.1 From ce30b49b4d50f2eff39a40ddd0c02ad96b78654c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 28 Oct 2016 16:29:55 +0200 Subject: Make packet variables' names more clear General fixes Remove default values from hidden volume setup command rename password -> pin Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 42f46ad..31d7b13 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -71,7 +71,7 @@ namespace nitrokey { }; - class UnlockUserPassword : Command { + class UnlockUserPin : Command { public: struct CommandPayload { uint8_t kind; @@ -124,11 +124,11 @@ namespace nitrokey { public: struct CommandPayload { uint8_t kind; - uint8_t admin_password[30]; //CS20_MAX_PASSWORD_LEN + uint8_t admin_pin[30]; //CS20_MAX_PASSWORD_LEN std::string dissect() const { std::stringstream ss; print_to_ss( kind ); - ss << " admin_password:\t" << admin_password << std::endl; + ss << " admin_pin:\t" << admin_pin << std::endl; return ss.str(); } @@ -152,13 +152,13 @@ namespace nitrokey { struct CommandPayload { uint8_t volume_flag; uint8_t kind; - uint8_t password[20]; + uint8_t admin_pin[20]; std::string dissect() const { std::stringstream ss; print_to_ss( (int) volume_flag ); print_to_ss( kind ); - print_to_ss(password); + print_to_ss(admin_pin); return ss.str(); } void set_kind_user() { @@ -297,27 +297,20 @@ namespace nitrokey { class SetupHiddenVolume : Command { public: - constexpr static int MAX_HIDDEN_VOLUME_PASSOWORD_SIZE = 20; + constexpr static int MAX_HIDDEN_VOLUME_PASSWORD_SIZE = 20; struct CommandPayload { uint8_t SlotNr_u8; uint8_t StartBlockPercent_u8; uint8_t EndBlockPercent_u8; - uint8_t HiddenVolumePassword_au8[MAX_HIDDEN_VOLUME_PASSOWORD_SIZE + 1]; //last char is a null terminator TODO check if it's needed + uint8_t HiddenVolumePassword_au8[MAX_HIDDEN_VOLUME_PASSWORD_SIZE]; std::string dissect() const { std::stringstream ss; print_to_ss((int) SlotNr_u8); print_to_ss((int) StartBlockPercent_u8); print_to_ss((int) EndBlockPercent_u8); - ss << " HiddenVolumePassword_au8:\t" - << HiddenVolumePassword_au8 << std::endl; + print_to_ss(HiddenVolumePassword_au8); return ss.str(); } - - void set_defaults(){ - SlotNr_u8 = 0; - StartBlockPercent_u8 = 70; - EndBlockPercent_u8 = 90; - } } __packed; typedef Transaction @@ -329,9 +322,9 @@ namespace nitrokey { class ProductionTest : Command { public: - static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; - static const int payload_absolute_begin = 8; - static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; + static constexpr int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; + static constexpr int payload_absolute_begin = 8; + static constexpr int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; struct ResponsePayload { uint8_t _padding[padding_size]; @@ -380,7 +373,7 @@ namespace nitrokey { print_to_ss( SD_WriteSpeed_u16); print_to_ss((int) SD_Card_Manufacturer_u8); - ss << "_padding:\t" + ss << "_padding:" << std::endl << ::nitrokey::misc::hexdump((const char *) (_padding), sizeof _padding); return ss.str(); -- cgit v1.2.1 From 90e3e144728114189b5b33b4763b07e871b2e66c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 16:33:40 +0100 Subject: Describe commands and mark unused Signed-off-by: Szczepan Zalega --- include/command_id.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/include/command_id.h b/include/command_id.h index 9cace2f..a3806f0 100644 --- a/include/command_id.h +++ b/include/command_id.h @@ -67,15 +67,15 @@ enum class CommandID : uint8_t { CHANGE_ADMIN_PIN = 0x15, ENABLE_CRYPTED_PARI = 0x20, - DISABLE_CRYPTED_PARI = 0x20 + 1, + DISABLE_CRYPTED_PARI = 0x20 + 1, //@unused ENABLE_HIDDEN_CRYPTED_PARI = 0x20 + 2, - DISABLE_HIDDEN_CRYPTED_PARI = 0x20 + 3, - ENABLE_FIRMWARE_UPDATE = 0x20 + 4, + DISABLE_HIDDEN_CRYPTED_PARI = 0x20 + 3, //@unused + ENABLE_FIRMWARE_UPDATE = 0x20 + 4, //enables update mode EXPORT_FIRMWARE_TO_FILE = 0x20 + 5, GENERATE_NEW_KEYS = 0x20 + 6, FILL_SD_CARD_WITH_RANDOM_CHARS = 0x20 + 7, - WRITE_STATUS_DATA = 0x20 + 8, + WRITE_STATUS_DATA = 0x20 + 8, //@unused ENABLE_READONLY_UNCRYPTED_LUN = 0x20 + 9, ENABLE_READWRITE_UNCRYPTED_LUN = 0x20 + 10, @@ -94,10 +94,10 @@ enum class CommandID : uint8_t { SEND_STARTUP = 0x20 + 21, SEND_CLEAR_STICK_KEYS_NOT_INITIATED = 0x20 + 22, - SEND_LOCK_STICK_HARDWARE = 0x20 + 23, + SEND_LOCK_STICK_HARDWARE = 0x20 + 23, //locks firmware upgrade PRODUCTION_TEST = 0x20 + 24, - SEND_DEBUG_DATA = 0x20 + 25, + SEND_DEBUG_DATA = 0x20 + 25, //@unused CHANGE_UPDATE_PIN = 0x20 + 26, @@ -109,8 +109,8 @@ enum class CommandID : uint8_t { SET_PW_SAFE_SLOT_DATA_2 = 0x65, PW_SAFE_ERASE_SLOT = 0x66, PW_SAFE_ENABLE = 0x67, - PW_SAFE_INIT_KEY = 0x68, - PW_SAFE_SEND_DATA = 0x69, + PW_SAFE_INIT_KEY = 0x68, //@unused + PW_SAFE_SEND_DATA = 0x69, //@unused SD_CARD_HIGH_WATERMARK = 0x70, DETECT_SC_AES = 0x6a, NEW_AES_KEY = 0x6b -- cgit v1.2.1 From 8b49ee9e25efbca9d0d51fcc8be17fd115fc78fd Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 16:56:40 +0100 Subject: Define configuration response packet separately Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 31d7b13..5c2d97e 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -178,23 +178,7 @@ namespace nitrokey { - - class SendStartup : Command { - public: - struct CommandPayload { - uint64_t localtime; // POSIX seconds from epoch start, supports until year 2106 - std::string dissect() const { - std::stringstream ss; - print_to_ss( localtime ); - return ss.str(); - } - void set_defaults(){ - localtime = - std::chrono::duration_cast ( - std::chrono::system_clock::now().time_since_epoch()).count(); - } - }__packed; - + namespace DeviceConfigurationResponsePacket{ static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; static const int payload_absolute_begin = 8; static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; @@ -249,14 +233,33 @@ namespace nitrokey { print_to_ss((int) AdminPwRetryCount ); print_to_ss( ActiveSmartCardID_u32 ); print_to_ss((int) StickKeysNotInitiated ); - ss << "_padding:\t" + ss << "_padding:" << std::endl << ::nitrokey::misc::hexdump((const char *) (_padding), sizeof _padding); return ss.str(); } } __packed; + } + + class SendStartup : Command { + public: + struct CommandPayload { + uint64_t localtime; // POSIX seconds from epoch start, supports until year 2106 + std::string dissect() const { + std::stringstream ss; + print_to_ss( localtime ); + return ss.str(); + } + void set_defaults(){ + localtime = + std::chrono::duration_cast ( + std::chrono::system_clock::now().time_since_epoch()).count(); + } + }__packed; + + using ResponsePayload = DeviceConfigurationResponsePacket::ResponsePayload; - typedef Transaction + typedef Transaction CommandTransaction; }; @@ -268,8 +271,9 @@ namespace nitrokey { class GetDeviceStatus : Command { public: + using ResponsePayload = DeviceConfigurationResponsePacket::ResponsePayload; - typedef Transaction + typedef Transaction CommandTransaction; }; -- cgit v1.2.1 From 9ad4796130a4ce192420563bb601468ffc292df9 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 17:48:18 +0100 Subject: Code refactoring - unify password only commands Signed-off-by: Szczepan Zalega --- include/command.h | 25 +++++++++++++-- include/stick20_commands.h | 78 ++++------------------------------------------ 2 files changed, 29 insertions(+), 74 deletions(-) (limited to 'include') diff --git a/include/command.h b/include/command.h index 5ac1d7f..badf068 100644 --- a/include/command.h +++ b/include/command.h @@ -19,13 +19,18 @@ namespace nitrokey { }; #define print_to_ss(x) ( ss << " " << (#x) <<":\t" << (x) << std::endl ); +namespace stick20{ + enum class PasswordKind : uint8_t { + User = 'P', + Admin = 'A' + }; - template + template class PasswordCommand : public Command { public: struct CommandPayload { uint8_t kind; - uint8_t password[20]; + uint8_t password[password_length]; std::string dissect() const { std::stringstream ss; @@ -40,6 +45,21 @@ namespace nitrokey { kind = (uint8_t) 'P'; } + void set_defaults(){ + set_kind(Tpassword_kind); + } + + void set_kind(PasswordKind password_kind){ + switch (password_kind){ + case PasswordKind::Admin: + set_kind_admin(); + break; + case PasswordKind::User: + set_kind_user(); + break; + } + }; + } __packed; typedef Transaction::command_id(), struct CommandPayload, struct EmptyPayload> @@ -47,6 +67,7 @@ namespace nitrokey { }; } + } } #undef print_to_ss diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 5c2d97e..a51d1ca 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -18,80 +18,14 @@ namespace nitrokey { #define print_to_ss(x) ( ss << " " << (#x) <<":\t" << (x) << std::endl ); namespace stick20 { - enum class PasswordKind : uint8_t { - User = 'P', - Admin = 'A' - }; - - class ChangeAdminUserPin20Current : Command { - public: - struct CommandPayload { - uint8_t kind; - uint8_t old_pin[20]; - - std::string dissect() const { - std::stringstream ss; - print_to_ss( kind ); - ss << " old_pin:\t" << old_pin << std::endl; - return ss.str(); - } - - void set_kind(PasswordKind k) { - kind = (uint8_t) k; - } - } __packed; - - typedef Transaction - CommandTransaction; - }; - - - class ChangeAdminUserPin20New : Command { - public: - - struct CommandPayload { - uint8_t kind; - uint8_t new_pin[20]; - - std::string dissect() const { - std::stringstream ss; - print_to_ss( kind ); - ss << " new_pin:\t" << new_pin << std::endl; - return ss.str(); - } - - void set_kind(PasswordKind k) { - kind = (uint8_t) k; - } - - } __packed; - - typedef Transaction - CommandTransaction; - }; - class UnlockUserPin : Command { - public: - struct CommandPayload { - uint8_t kind; - uint8_t user_new_password[20]; - - std::string dissect() const { - std::stringstream ss; - print_to_ss( kind ); - ss << " user_new_password:\t" << user_new_password << std::endl; - return ss.str(); - } - - void set_kind(PasswordKind k) { - kind = (uint8_t) k; - } - } __packed; - - typedef Transaction - CommandTransaction; - }; + class ChangeAdminUserPin20Current : + public PasswordCommand {}; + class ChangeAdminUserPin20New : + public PasswordCommand {}; + class UnlockUserPin : + public PasswordCommand {}; class EnableEncryptedPartition : public PasswordCommand {}; class DisableEncryptedPartition : public PasswordCommand {}; -- cgit v1.2.1 From 98b27cb58b12f699b5c11f88d55c3e3f70ed2063 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 18:02:55 +0100 Subject: Code refactoring - unify password only commands (2) Signed-off-by: Szczepan Zalega --- include/command.h | 9 ++++++++- include/stick20_commands.h | 24 ++---------------------- 2 files changed, 10 insertions(+), 23 deletions(-) (limited to 'include') diff --git a/include/command.h b/include/command.h index badf068..0a875e4 100644 --- a/include/command.h +++ b/include/command.h @@ -22,7 +22,8 @@ namespace nitrokey { namespace stick20{ enum class PasswordKind : uint8_t { User = 'P', - Admin = 'A' + Admin = 'A', + AdminPrefixed }; template @@ -41,6 +42,9 @@ namespace stick20{ void set_kind_admin() { kind = (uint8_t) 'A'; } + void set_kind_admin_prefixed() { + kind = (uint8_t) 'P'; + } void set_kind_user() { kind = (uint8_t) 'P'; } @@ -57,6 +61,9 @@ namespace stick20{ case PasswordKind::User: set_kind_user(); break; + case PasswordKind::AdminPrefixed: + set_kind_admin_prefixed(); + break; } }; diff --git a/include/stick20_commands.h b/include/stick20_commands.h index a51d1ca..d7d89cf 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -18,8 +18,6 @@ namespace nitrokey { #define print_to_ss(x) ( ss << " " << (#x) <<":\t" << (x) << std::endl ); namespace stick20 { - - class ChangeAdminUserPin20Current : public PasswordCommand {}; class ChangeAdminUserPin20New : @@ -54,26 +52,8 @@ namespace nitrokey { class ExportFirmware : public PasswordCommand {}; - class CreateNewKeys : Command { - public: - struct CommandPayload { - uint8_t kind; - uint8_t admin_pin[30]; //CS20_MAX_PASSWORD_LEN - std::string dissect() const { - std::stringstream ss; - print_to_ss( kind ); - ss << " admin_pin:\t" << admin_pin << std::endl; - return ss.str(); - } - - void setKindPrefixed() { - kind = 'P'; - } - } __packed; - - typedef Transaction - CommandTransaction; - }; + class CreateNewKeys : + public PasswordCommand {}; class FillSDCardWithRandomChars : Command { -- cgit v1.2.1 From b540266c4ce17b640724bc09275e45e15a53d707 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 18:19:54 +0100 Subject: Transmission data for 2.0 commands move to separate struct Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 67 +++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 36 deletions(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index d7d89cf..f816868 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -90,20 +90,37 @@ namespace nitrokey { CommandTransaction; }; - + namespace StorageCommandResponsePayload{ + static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; + static const int payload_absolute_begin = 8; + static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; + struct TransmissionData{ + uint8_t _padding[padding_size]; + + uint8_t SendCounter_u8; + uint8_t SendDataType_u8; + uint8_t FollowBytesFlag_u8; + uint8_t SendSize_u8; + + std::string dissect() const { + std::stringstream ss; + ss << "_padding:" << std::endl + << ::nitrokey::misc::hexdump((const char *) (_padding), + sizeof _padding); + print_to_ss((int) SendCounter_u8); + print_to_ss((int) SendDataType_u8); + print_to_ss((int) FollowBytesFlag_u8); + print_to_ss((int) SendSize_u8); + return ss.str(); + } + + } __packed; + } namespace DeviceConfigurationResponsePacket{ - static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; - static const int payload_absolute_begin = 8; - static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; struct ResponsePayload { - uint8_t _padding[padding_size]; - - uint8_t SendCounter_u8; - uint8_t SendDataType_u8; - uint8_t FollowBytesFlag_u8; - uint8_t SendSize_u8; + StorageCommandResponsePayload::TransmissionData transmission_data; uint16_t MagicNumber_StickConfig_u16; uint8_t ReadWriteFlagUncryptedVolume_u8; @@ -126,11 +143,7 @@ namespace nitrokey { std::string dissect() const { std::stringstream ss; - print_to_ss((int) SendCounter_u8 ); - print_to_ss((int) SendDataType_u8 ); - print_to_ss((int) FollowBytesFlag_u8 ); - print_to_ss((int) SendSize_u8 ); - + print_to_ss(transmission_data.dissect()); print_to_ss( MagicNumber_StickConfig_u16 ); print_to_ss((int) ReadWriteFlagUncryptedVolume_u8 ); print_to_ss((int) ReadWriteFlagCryptedVolume_u8 ); @@ -147,9 +160,7 @@ namespace nitrokey { print_to_ss((int) AdminPwRetryCount ); print_to_ss( ActiveSmartCardID_u32 ); print_to_ss((int) StickKeysNotInitiated ); - ss << "_padding:" << std::endl - << ::nitrokey::misc::hexdump((const char *) (_padding), - sizeof _padding); + return ss.str(); } } __packed; @@ -240,17 +251,9 @@ namespace nitrokey { class ProductionTest : Command { public: - static constexpr int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; - static constexpr int payload_absolute_begin = 8; - static constexpr int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; - struct ResponsePayload { - uint8_t _padding[padding_size]; - uint8_t SendCounter_u8; - uint8_t SendDataType_u8; - uint8_t FollowBytesFlag_u8; - uint8_t SendSize_u8; + StorageCommandResponsePayload::TransmissionData transmission_data; uint8_t FirmwareVersion_au8[2]; // 2 byte // 2 uint8_t FirmwareVersionInternal_u8; // 1 byte // 3 @@ -271,11 +274,7 @@ namespace nitrokey { std::string dissect() const { std::stringstream ss; - print_to_ss((int) SendCounter_u8); - print_to_ss((int) SendDataType_u8); - print_to_ss((int) FollowBytesFlag_u8); - print_to_ss((int) SendSize_u8); - + print_to_ss(transmission_data.dissect()); print_to_ss((int) FirmwareVersion_au8[0]); print_to_ss((int) FirmwareVersion_au8[1]); print_to_ss((int) FirmwareVersionInternal_u8); @@ -290,10 +289,6 @@ namespace nitrokey { print_to_ss( SD_Card_OEM_u16); print_to_ss( SD_WriteSpeed_u16); print_to_ss((int) SD_Card_Manufacturer_u8); - - ss << "_padding:" << std::endl - << ::nitrokey::misc::hexdump((const char *) (_padding), - sizeof _padding); return ss.str(); } -- cgit v1.2.1 From ca130e790ecc4e4502eb39e1ab550348eae9bc54 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 18:40:48 +0100 Subject: Code refactoring - named magic numbers Signed-off-by: Szczepan Zalega --- include/device_proto.h | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 037451a..6bcbcb0 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -92,22 +92,29 @@ namespace nitrokey { */ template struct DeviceResponse { + static constexpr auto storage_status_absolute_address = 21; //magic number from firmware + static constexpr auto header_size = 8; //from _zero to last_command_status inclusive + static constexpr auto footer_size = 4; //crc + static constexpr auto boiler_size = header_size + footer_size; + uint8_t _zero; uint8_t device_status; uint8_t command_id; // originally last_command_type uint32_t last_command_crc; uint8_t last_command_status; + union { - uint8_t _padding[HID_REPORT_SIZE - 12]; + uint8_t _padding[HID_REPORT_SIZE - boiler_size]; ResponsePayload payload; struct { - uint8_t _storage_status_padding[20 - 8 + 1]; //starts on 20th byte minus already 8 used + zero byte + uint8_t _storage_status_padding[storage_status_absolute_address - header_size]; uint8_t command_counter; uint8_t command_id; uint8_t device_status; //@see stick20::device_status uint8_t progress_bar_value; } __packed storage_status; } __packed; + uint32_t crc; void initialize() { bzero(this, sizeof *this); } @@ -120,9 +127,7 @@ namespace nitrokey { } void update_CRC() { crc = calculate_CRC(); } - bool isCRCcorrect() const { return crc == calculate_CRC(); } - bool isValid() const { // return !_zero && payload.isValid() && isCRCcorrect() && // command_id == (uint8_t)(cmd_id); -- cgit v1.2.1 From c346ff180244de682e78f49783eab9b296c79de6 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 19:03:30 +0100 Subject: Code refactoring - named magic numbers (2) Signed-off-by: Szczepan Zalega --- include/device_proto.h | 19 +++++++++++++------ include/stick20_commands.h | 6 +++--- 2 files changed, 16 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/device_proto.h b/include/device_proto.h index 6bcbcb0..187d25c 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -90,12 +90,19 @@ namespace nitrokey { * command_id member in incoming HIDReport structure carries the command * type last used. */ - template - struct DeviceResponse { - static constexpr auto storage_status_absolute_address = 21; //magic number from firmware + namespace DeviceResponseConstants{ + //magic numbers from firmware + static constexpr auto storage_status_absolute_address = 21; + static constexpr auto storage_data_absolute_address = storage_status_absolute_address + 5; static constexpr auto header_size = 8; //from _zero to last_command_status inclusive static constexpr auto footer_size = 4; //crc - static constexpr auto boiler_size = header_size + footer_size; + static constexpr auto wrapping_size = header_size + footer_size; + } + + template + struct DeviceResponse { + static constexpr auto storage_status_padding_size = + DeviceResponseConstants::storage_status_absolute_address - DeviceResponseConstants::header_size; uint8_t _zero; uint8_t device_status; @@ -104,10 +111,10 @@ namespace nitrokey { uint8_t last_command_status; union { - uint8_t _padding[HID_REPORT_SIZE - boiler_size]; + uint8_t _padding[HID_REPORT_SIZE - DeviceResponseConstants::wrapping_size]; ResponsePayload payload; struct { - uint8_t _storage_status_padding[storage_status_absolute_address - header_size]; + uint8_t _storage_status_padding[storage_status_padding_size]; uint8_t command_counter; uint8_t command_id; uint8_t device_status; //@see stick20::device_status diff --git a/include/stick20_commands.h b/include/stick20_commands.h index f816868..139dc63 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -91,9 +91,9 @@ namespace nitrokey { }; namespace StorageCommandResponsePayload{ - static const int OUTPUT_CMD_RESULT_STICK20_STATUS_START = 25 + 1; - static const int payload_absolute_begin = 8; - static const int padding_size = OUTPUT_CMD_RESULT_STICK20_STATUS_START - payload_absolute_begin; + using namespace DeviceResponseConstants; + static constexpr auto padding_size = + storage_data_absolute_address - header_size; struct TransmissionData{ uint8_t _padding[padding_size]; -- cgit v1.2.1 From 266b57dfe7b36799243816b7af22f3dd69b0d197 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 31 Oct 2016 19:07:34 +0100 Subject: Disable locking firmware command for now Signed-off-by: Szczepan Zalega --- include/stick20_commands.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/stick20_commands.h b/include/stick20_commands.h index 139dc63..1af9da3 100644 --- a/include/stick20_commands.h +++ b/include/stick20_commands.h @@ -247,7 +247,8 @@ namespace nitrokey { }; - class LockFirmware : public PasswordCommand {}; +//disable this command for now +// class LockFirmware : public PasswordCommand {}; class ProductionTest : Command { public: -- cgit v1.2.1 From e4cb016284e27c92090868119277091d122114f5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 2 Nov 2016 21:51:21 +0100 Subject: Helper for running simple, password only commands for Storage Signed-off-by: Szczepan Zalega --- include/misc.h | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'include') diff --git a/include/misc.h b/include/misc.h index 48d6a83..5158de0 100644 --- a/include/misc.h +++ b/include/misc.h @@ -36,6 +36,13 @@ typename T::CommandPayload get_payload(){ return st; } + template + void execute_password_command(Tdev &stick, const char *password) { + auto p = get_payload(); + p.set_defaults(); + strcpyT(p.password, password); + CMDTYPE::CommandTransaction::run(stick, p); + } std::string hexdump(const char *p, size_t size, bool print_header=true); uint32_t stm_crc32(const uint8_t *data, size_t size); -- cgit v1.2.1 From 7d943d3e8717ba47af4b53787cc4c29ad6ee2f90 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Wed, 2 Nov 2016 21:52:40 +0100 Subject: Python bindings and tests for most of Storage functionality Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 52c18d7..7a3ffcc 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -82,6 +82,30 @@ namespace nitrokey { bool is_AES_supported(const char *user_password); + void unlock_encrypted_volume(const char *user_password); + + void unlock_hidden_volume(const char *hidden_volume_password); + + void set_unencrypted_read_only(const char *user_pin); + + void set_unencrypted_read_write(const char *user_pin); + + void export_firmware(const char *admin_pin); + + void clear_new_sd_card_warning(const char *admin_pin); + + void fill_SD_card_with_random_data(const char *admin_pin); + + void change_update_password(const char *current_update_password, const char *new_update_password); + + void create_hidden_volume(int slot_nr, int start_percent, int end_percent, const char *hidden_volume_password); + + void send_startup(uint64_t seconds_from_epoch); + + const char * get_status_storage(); + + const char *get_SD_usage_data(); + ~NitrokeyManager(); private: NitrokeyManager(); @@ -101,7 +125,6 @@ namespace nitrokey { template void change_PIN_general(char *current_PIN, char *new_PIN); - }; } -- cgit v1.2.1 From 130a33311567a1f0f6a9a2d8707bff20a558eb20 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 3 Nov 2016 13:45:53 +0100 Subject: Treat LongOperationException as CommandFailed and handle it transparently Signed-off-by: Szczepan Zalega --- include/LongOperationInProgressException.h | 10 +++++----- include/device_proto.h | 15 ++++++++------- 2 files changed, 13 insertions(+), 12 deletions(-) (limited to 'include') diff --git a/include/LongOperationInProgressException.h b/include/LongOperationInProgressException.h index 673a218..7f182b0 100644 --- a/include/LongOperationInProgressException.h +++ b/include/LongOperationInProgressException.h @@ -5,16 +5,16 @@ #ifndef LIBNITROKEY_LONGOPERATIONINPROGRESSEXCEPTION_H #define LIBNITROKEY_LONGOPERATIONINPROGRESSEXCEPTION_H +#include "CommandFailedException.h" -class LongOperationInProgressException : public std::exception { +class LongOperationInProgressException : public CommandFailedException { public: unsigned char progress_bar_value; - unsigned char command_id; - LongOperationInProgressException(unsigned char _command_id, unsigned char _progress_bar_value) { - command_id = _command_id; - progress_bar_value = _progress_bar_value; + 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()( 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 187d25c..0953566 100644 --- a/include/device_proto.h +++ b/include/device_proto.h @@ -316,21 +316,22 @@ namespace nitrokey { clear_packet(outp); if (status <= 0) - throw std::runtime_error( + throw std::runtime_error( //FIXME replace with CriticalErrorException std::string("Device error while executing command ") + std::to_string(status)); - if (resp.device_status == static_cast(stick10::device_status::busy) && - static_cast(resp.storage_status.device_status) - == stick20::device_status::busy_progressbar){ - throw LongOperationInProgressException(resp.command_id, resp.storage_status.progress_bar_value); - } - 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), Loglevel::DEBUG); + if (resp.device_status == static_cast(stick10::device_status::busy) && + static_cast(resp.storage_status.device_status) + == stick20::device_status::busy_progressbar){ + throw LongOperationInProgressException( + resp.command_id, resp.device_status, resp.storage_status.progress_bar_value); + } + if (!resp.isValid()) throw std::runtime_error("Invalid incoming packet"); if (receiving_retry_counter <= 0) throw std::runtime_error( -- cgit v1.2.1 From fe36daa38ab1995c8c7fc6470d06b8595efd2385 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 3 Nov 2016 13:51:59 +0100 Subject: Tests: remake fill SD card test. Get progress bar value when busy. Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 2 ++ 1 file changed, 2 insertions(+) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 7a3ffcc..2d5859f 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -106,6 +106,8 @@ namespace nitrokey { const char *get_SD_usage_data(); + int get_progress_bar_value(); + ~NitrokeyManager(); private: NitrokeyManager(); -- cgit v1.2.1 From 8cb4f03c841e7035af1893f260db5690cf6b4aa5 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 3 Nov 2016 17:02:02 +0100 Subject: Document Storage methods in C API Use uint8_t instead of int where appropriate 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 2d5859f..11c2be4 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -98,7 +98,8 @@ namespace nitrokey { void change_update_password(const char *current_update_password, const char *new_update_password); - void create_hidden_volume(int slot_nr, int start_percent, int end_percent, const char *hidden_volume_password); + void create_hidden_volume(uint8_t slot_nr, uint8_t start_percent, uint8_t end_percent, + const char *hidden_volume_password); void send_startup(uint64_t seconds_from_epoch); -- cgit v1.2.1 From cdd16f3f184b2745094da39de3f815aea6633fdb Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 3 Nov 2016 18:22:02 +0100 Subject: Rename methods in API to make string return more obvious Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 11c2be4..60fa753 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -103,9 +103,9 @@ namespace nitrokey { void send_startup(uint64_t seconds_from_epoch); - const char * get_status_storage(); + const char * get_status_storage_as_string(); - const char *get_SD_usage_data(); + const char *get_SD_usage_data_as_string(); int get_progress_bar_value(); -- cgit v1.2.1