diff options
| author | Szczepan Zalega <szczepan@nitrokey.com> | 2016-10-22 18:12:37 +0200 | 
|---|---|---|
| committer | Szczepan Zalega <szczepan@nitrokey.com> | 2016-11-26 15:54:05 +0100 | 
| commit | 89076a0c44dd12a73060dbfda419c20c4ce5285a (patch) | |
| tree | c209da073ffb6d379f37f81592f740f0629d02ba | |
| parent | d841239bc9ece1ce969c293783219cceb001fc67 (diff) | |
| download | libnitrokey-89076a0c44dd12a73060dbfda419c20c4ce5285a.tar.gz libnitrokey-89076a0c44dd12a73060dbfda419c20c4ce5285a.tar.bz2 | |
Migrate commands to new format
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
| -rw-r--r-- | include/stick20_commands.h | 134 | ||||
| -rw-r--r-- | unittest/test2.cc | 83 | 
2 files changed, 146 insertions, 71 deletions
| 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<CommandID::ENABLE_CRYPTED_PARI>{   public:    struct CommandPayload {      uint8_t password[30];  // TODO check w/ firmware    }; -  typedef Transaction<CommandID::ENABLE_CRYPTED_PARI, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class DisableEncryptedPartition : semantics::non_constructible { +class DisableEncryptedPartition : Command<CommandID::DISABLE_CRYPTED_PARI>{   public: -  typedef Transaction<CommandID::DISABLE_CRYPTED_PARI, struct EmptyPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class EnableHiddenEncryptedPartition : semantics::non_constructible { +class EnableHiddenEncryptedPartition :Command<CommandID::ENABLE_HIDDEN_CRYPTED_PARI>{   public:    struct CommandPayload {      uint8_t password[30];  // TODO check w/ firmware    }; -  typedef Transaction<CommandID::ENABLE_HIDDEN_CRYPTED_PARI, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class DisableHiddenEncryptedPartition : semantics::non_constructible { +class DisableHiddenEncryptedPartition :Command<CommandID::DISABLE_CRYPTED_PARI>{   public: -  typedef Transaction<CommandID::DISABLE_CRYPTED_PARI, struct EmptyPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class EnableFirmwareUpdate : semantics::non_constructible { +class EnableFirmwareUpdate :Command<CommandID::ENABLE_FIRMWARE_UPDATE>{   public:    struct CommandPayload {      uint8_t password[30];  // TODO check w/ firmware    }; -  typedef Transaction<CommandID::ENABLE_FIRMWARE_UPDATE, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class UpdatePassword : semantics::non_constructible { +class UpdatePassword :Command<CommandID::CHANGE_UPDATE_PIN>{   public:    struct CommandPayload {      uint8_t old_password[15];      uint8_t new_password[15];    }; -  typedef Transaction<CommandID::CHANGE_UPDATE_PIN, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class ExportFirmware : semantics::non_constructible { +class ExportFirmware :Command<CommandID::EXPORT_FIRMWARE_TO_FILE>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::EXPORT_FIRMWARE_TO_FILE, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  };      class CreateNewKeys : Command<CommandID::GENERATE_NEW_KEYS> { @@ -166,50 +165,47 @@ class ExportFirmware : semantics::non_constructible {      }; -class FillSDCardWithRandomChars : semantics::non_constructible { +class FillSDCardWithRandomChars :Command<CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS>{   public:    struct CommandPayload {      uint8_t volume_flag;      uint8_t password[30];    }; -  typedef Transaction<CommandID::FILL_SD_CARD_WITH_RANDOM_CHARS, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SetupHiddenVolume : semantics::non_constructible { +class SetupHiddenVolume :Command<CommandID::SEND_HIDDEN_VOLUME_SETUP>{   public: -  typedef Transaction<CommandID::SEND_HIDDEN_VOLUME_SETUP, struct EmptyPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendPasswordMatrix : semantics::non_constructible { +class SendPasswordMatrix :Command<CommandID::SEND_PASSWORD_MATRIX>{   public: -  typedef Transaction<CommandID::SEND_PASSWORD_MATRIX, struct EmptyPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendPasswordMatrixPinData : semantics::non_constructible { +class SendPasswordMatrixPinData :Command<CommandID::SEND_PASSWORD_MATRIX_PINDATA>{   public:    struct CommandPayload {      uint8_t pin_data[30];  // TODO how long actually can it be?    }; -  typedef Transaction<CommandID::SEND_PASSWORD_MATRIX_PINDATA, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendPasswordMatrixSetup : semantics::non_constructible { +class SendPasswordMatrixSetup :Command<CommandID::SEND_PASSWORD_MATRIX_SETUP>{   public:    struct CommandPayload {      uint8_t setup_data[30];  // TODO how long actually can it be?    }; -  typedef Transaction<CommandID::SEND_PASSWORD_MATRIX_SETUP, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        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<CommandID::SEND_PASSWORD>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::SEND_PASSWORD, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendNewPassword : semantics::non_constructible { +class SendNewPassword :Command<CommandID::SEND_NEW_PASSWORD>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::SEND_NEW_PASSWORD, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  };  // TODO fix original nomenclature -class SendSetReadonlyToUncryptedVolume : semantics::non_constructible { +class SendSetReadonlyToUncryptedVolume :Command<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendSetReadwriteToUncryptedVolume : semantics::non_constructible { +class SendSetReadwriteToUncryptedVolume :Command<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::ENABLE_READWRITE_UNCRYPTED_LUN, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendClearNewSdCardFound : semantics::non_constructible { +class SendClearNewSdCardFound :Command<CommandID::CLEAR_NEW_SD_CARD_FOUND>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::CLEAR_NEW_SD_CARD_FOUND, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendStartup : semantics::non_constructible { +class SendStartup :Command<CommandID::SEND_STARTUP>{   public:    struct CommandPayload {      uint64_t localtime;  // POSIX    }; -  typedef Transaction<CommandID::SEND_STARTUP, struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class SendHiddenVolumeSetup : semantics::non_constructible { +class SendHiddenVolumeSetup :Command<CommandID::SEND_HIDDEN_VOLUME_SETUP>{   public:    struct CommandPayload {      // TODO HiddenVolumeSetup_tst type    }; -  typedef Transaction<CommandID::SEND_HIDDEN_VOLUME_SETUP, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class LockFirmware : semantics::non_constructible { +class LockFirmware :Command<CommandID::SEND_LOCK_STICK_HARDWARE>{   public:    struct CommandPayload {      uint8_t password[30];    }; -  typedef Transaction<CommandID::SEND_LOCK_STICK_HARDWARE, -                      struct CommandPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  }; -class ProductionTest : semantics::non_constructible { +class ProductionTest :Command<CommandID::PRODUCTION_TEST>{   public: -  typedef Transaction<CommandID::PRODUCTION_TEST, struct EmptyPayload, -                      struct EmptyPayload> CommandTransaction; +    typedef Transaction<command_id(), struct CommandPayload, struct EmptyPayload> +        CommandTransaction;  };  }  } diff --git a/unittest/test2.cc b/unittest/test2.cc new file mode 100644 index 0000000..6744b45 --- /dev/null +++ b/unittest/test2.cc @@ -0,0 +1,83 @@ +#define CATCH_CONFIG_MAIN  // This tells Catch to provide a main() +#include "catch.hpp" + +#include <iostream> +#include <string.h> +#include "device_proto.h" +#include "log.h" +#include "stick10_commands.h" + +using namespace std; +using namespace nitrokey::device; +using namespace nitrokey::proto::stick10; +using namespace nitrokey::log; +using namespace nitrokey::misc; + + +std::string getSlotName(Stick10 &stick, int slotNo) { +  auto slot_req = get_payload<ReadSlot>(); +  slot_req.slot_number = slotNo; +  auto slot = ReadSlot::CommandTransaction::run(stick, slot_req); +  std::string sName(reinterpret_cast<char *>(slot.data().slot_name)); +  return sName; +} + +TEST_CASE("Slot names are correct", "[slotNames]") { +  Stick10 stick; +  bool connected = stick.connect(); +  REQUIRE(connected == true); + +  Log::instance().set_loglevel(Loglevel::DEBUG); + +  auto resp = GetStatus::CommandTransaction::run(stick); + +  auto authreq = get_payload<FirstAuthenticate>(); +  strcpy((char *)(authreq.card_password), "12345678"); +  FirstAuthenticate::CommandTransaction::run(stick, authreq); + +  { +    auto authreq = get_payload<EnablePasswordSafe>(); +    strcpy((char *)(authreq.user_password), "123456"); +    EnablePasswordSafe::CommandTransaction::run(stick, authreq); +  } + +  //assuming these values were set earlier, thus failing on normal use +  REQUIRE(getSlotName(stick, 0x20) == std::string("1")); +  REQUIRE(getSlotName(stick, 0x21) == std::string("slot2")); + +  { +    auto resp = GetPasswordRetryCount::CommandTransaction::run(stick); +    REQUIRE(resp.data().password_retry_count == 3); +  } +  { +    auto resp = GetUserPasswordRetryCount::CommandTransaction::run(stick); +    REQUIRE(resp.data().password_retry_count == 3); +  } + +  { +    auto slot = get_payload<GetPasswordSafeSlotName>(); +    slot.slot_number = 0; +    auto resp2 = GetPasswordSafeSlotName::CommandTransaction::run(stick, slot); +    std::string sName(reinterpret_cast<char *>(resp2.data().slot_name)); +    REQUIRE(sName == std::string("web1")); +  } + +  { +    auto slot = get_payload<GetPasswordSafeSlotPassword>(); +    slot.slot_number = 0; +    auto resp2 = +        GetPasswordSafeSlotPassword::CommandTransaction::run(stick, slot); +    std::string sName(reinterpret_cast<char *>(resp2.data().slot_password)); +    REQUIRE(sName == std::string("pass1")); +  } + +  { +    auto slot = get_payload<GetPasswordSafeSlotLogin>(); +    slot.slot_number = 0; +    auto resp2 = GetPasswordSafeSlotLogin::CommandTransaction::run(stick, slot); +    std::string sName(reinterpret_cast<char *>(resp2.data().slot_login)); +    REQUIRE(sName == std::string("login1")); +  } + +  stick.disconnect(); +} | 
