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 --- NitrokeyManager.cc | 12 +++---- include/command.h | 25 +++++++++++++-- include/stick20_commands.h | 78 ++++------------------------------------------ 3 files changed, 35 insertions(+), 80 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 8bc0e38..eb34ef2 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -304,10 +304,10 @@ namespace nitrokey{ case DeviceModel::STORAGE: { auto p = get_payload(); - strcpyT(p.old_pin, current_PIN); + strcpyT(p.password, current_PIN); p.set_kind(StoKind); auto p2 = get_payload(); - strcpyT(p2.new_pin, new_PIN); + strcpyT(p2.password, new_PIN); p2.set_kind(StoKind); ChangeAdminUserPin20Current::CommandTransaction::run(*device, p); ChangeAdminUserPin20New::CommandTransaction::run(*device, p2); @@ -445,12 +445,12 @@ namespace nitrokey{ } case DeviceModel::STORAGE : { auto p2 = get_payload(); - p2.set_kind(PasswordKind::Admin); - strcpyT(p2.old_pin, admin_password); + p2.set_defaults(); + strcpyT(p2.password, admin_password); ChangeAdminUserPin20Current::CommandTransaction::run(*device, p2); auto p3 = get_payload(); - p3.set_kind(PasswordKind::Admin); - strcpyT(p3.user_new_password, new_user_password); + p3.set_defaults(); + strcpyT(p3.password, new_user_password); stick20::UnlockUserPin::CommandTransaction::run(*device, p3); break; } 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.3