From f6bd34b262b66fde862aa3d95c339f5c587156eb Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 26 Jul 2016 20:53:19 +0200 Subject: Handle writing PW safe slot Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 9 ++++++--- NK_C_API.h | 2 +- NitrokeyManager.cc | 14 ++++++++++++++ include/NitrokeyManager.h | 4 ++++ include/stick10_commands.h | 4 ++-- unittest/test_bindings.py | 22 +++++++++++++++------- 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index c0910a2..542c6ad 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -270,7 +270,7 @@ extern uint8_t NK_get_admin_retry_count(){ extern int NK_lock_device(){ auto m = NitrokeyManager::instance(); return get_without_result([&](){ - return m->lock_device(); + m->lock_device(); }); } @@ -293,9 +293,12 @@ extern const char *NK_get_password_safe_slot_password(uint8_t slot_number, const return m->get_password_safe_slot_password(slot_number, temporary_password); }); } -extern int NK_write_password_safe_slot(){ +extern int NK_write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, + const char *slot_password) { auto m = NitrokeyManager::instance(); - //TODO + return get_without_result([&](){ + m->write_password_safe_slot(slot_number, slot_name, slot_login, slot_password); + }); } diff --git a/NK_C_API.h b/NK_C_API.h index d6bf516..addafc3 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -36,7 +36,7 @@ extern int NK_get_password_safe_slot_status(); extern const char *NK_get_password_safe_slot_name(uint8_t slot_number, const char *temporary_password); extern const char *NK_get_password_safe_slot_login(uint8_t slot_number, const char *temporary_password); extern const char *NK_get_password_safe_slot_password(uint8_t slot_number, const char *temporary_password); -extern int NK_write_password_safe_slot(); +extern int NK_write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, const char *slot_password); } diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index d453f7b..7ffee51 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -263,4 +263,18 @@ namespace nitrokey{ return strdup((const char *) response.slot_password); } + void NitrokeyManager::write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, + const char *slot_password) { + auto p = get_payload(); + p.slot_number = slot_number; + strcpyT(p.slot_name, slot_name); + strcpyT(p.slot_password, slot_password); + SetPasswordSafeSlotData::CommandTransaction::run(*device, p); + + auto p2 = get_payload(); + p2.slot_number = slot_number; + strcpyT(p2.slot_login_name, slot_login); + SetPasswordSafeSlotData2::CommandTransaction::run(*device, p2); + } + } \ No newline at end of file diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 6f65324..541945b 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -53,6 +53,10 @@ namespace nitrokey { const char *get_password_safe_slot_password(uint8_t slot_number, const char *temporary_password); const char *get_password_safe_slot_login(uint8_t slot_number, const char *temporary_password); + void + write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, + const char *slot_password); + private: NitrokeyManager(); ~NitrokeyManager(); diff --git a/include/stick10_commands.h b/include/stick10_commands.h index c3b0a37..16448b0 100644 --- a/include/stick10_commands.h +++ b/include/stick10_commands.h @@ -479,13 +479,13 @@ class SetPasswordSafeSlotData2 : Command { public: struct CommandPayload { uint8_t slot_number; - uint8_t slot_name[PWS_SLOTNAME_LENGTH]; + uint8_t slot_login_name[PWS_LOGINNAME_LENGTH]; bool isValid() const { return !(slot_number & 0xF0); } std::string dissect() const { std::stringstream ss; ss << " slot_number\t" << (int)slot_number << std::endl; - ss << " slot_name\t" << (char*)slot_name << std::endl; + ss << " slot_login_name\t" << (char*)slot_login_name << std::endl; return ss.str(); } } __packed; diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py index 6d9e490..4ac3844 100644 --- a/unittest/test_bindings.py +++ b/unittest/test_bindings.py @@ -7,6 +7,7 @@ gs = ffi.string RFC_SECRET = '12345678901234567890' + class DefaultPasswords(Enum): ADMIN = '12345678' USER = '123456' @@ -18,6 +19,7 @@ class DeviceErrorCode(Enum): WRONG_PASSWORD = 4 STATUS_NOT_AUTHORIZED = 5 + @pytest.fixture(scope="module") def C(request): fp = '../NK_C_API.h' @@ -54,27 +56,33 @@ def test_enable_password_safe(C): assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK +def test_write_password_safe_slot(C): + C.NK_set_debug(True) + assert C.NK_lock_device() == DeviceErrorCode.STATUS_OK + assert C.NK_write_password_safe_slot(0, 'slotname1', 'login1', 'pass1') == DeviceErrorCode.STATUS_NOT_AUTHORIZED + assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK + assert C.NK_write_password_safe_slot(0, 'slotname1', 'login1', 'pass1') == DeviceErrorCode.STATUS_OK + C.NK_set_debug(False) + + def test_get_password_safe_slot_name(C): - # C.NK_set_debug(True) assert C.NK_lock_device() == DeviceErrorCode.STATUS_OK assert gs(C.NK_get_password_safe_slot_name(0, '123123123')) == '' assert C.NK_get_last_command_status() == DeviceErrorCode.STATUS_NOT_AUTHORIZED assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK - assert gs(C.NK_get_password_safe_slot_name(0, '123123123')) == '1' + assert gs(C.NK_get_password_safe_slot_name(0, '123123123')) == 'slotname1' assert C.NK_get_last_command_status() == DeviceErrorCode.STATUS_OK - # C.NK_set_debug(False) + def test_get_password_safe_slot_login_password(C): - C.NK_set_debug(True) assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK slot_login = C.NK_get_password_safe_slot_login(0, '123123123') assert C.NK_get_last_command_status() == DeviceErrorCode.STATUS_OK - assert gs(slot_login) == '1' + assert gs(slot_login) == 'login1' slot_password = gs(C.NK_get_password_safe_slot_password(0, '123123123')) assert C.NK_get_last_command_status() == DeviceErrorCode.STATUS_OK - assert slot_password == '1' - C.NK_set_debug(False) + assert slot_password == 'pass1' def test_password_safe_slot_status(C): -- cgit v1.2.3