From 67b14773cf4ab1812af85d3aaf99bdc6119c5a8a Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 2 Apr 2020 15:02:36 +0200 Subject: NitrokeyManager: Also return serial number as u32 This patch adds the get_serial_number_as_u32 method to NitrokeyManager. It returns the serial number as a 32-bit unsigned integer. Previously, we only returned it as a string generated from the integer value, get_serial_number. While get_serial_number returns an empty string if no device is connected and "NA" if an unknown model is connected, the new method throws a DeviceNotConnected exception in the first case and returns zero in the second case as we cannot express the three states in one integer return value. --- NitrokeyManager.cc | 19 +++++++++++++++++++ libnitrokey/NitrokeyManager.h | 1 + unittest/test_offline.cc | 4 ++++ 3 files changed, 24 insertions(+) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 6c26a43..496496e 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -398,6 +398,25 @@ using nitrokey::misc::strcpyT; return "NA"; } + uint32_t NitrokeyManager::get_serial_number_as_u32() { + if (device == nullptr) { throw DeviceNotConnected("device not connected"); } + switch (device->get_device_model()) { + case DeviceModel::PRO: { + auto response = GetStatus::CommandTransaction::run(device); + return response.data().card_serial_u32; + } + break; + + case DeviceModel::STORAGE: + { + auto response = stick20::GetDeviceStatus::CommandTransaction::run(device); + return response.data().ActiveSmartCardID_u32; + } + break; + } + return 0; + } + stick10::GetStatus::ResponsePayload NitrokeyManager::get_status(){ try{ auto response = GetStatus::CommandTransaction::run(device); diff --git a/libnitrokey/NitrokeyManager.h b/libnitrokey/NitrokeyManager.h index 33ede1b..163a799 100644 --- a/libnitrokey/NitrokeyManager.h +++ b/libnitrokey/NitrokeyManager.h @@ -104,6 +104,7 @@ char * strndup(const char* str, size_t maxlen); stick10::GetStatus::ResponsePayload get_status(); string get_status_as_string(); string get_serial_number(); + uint32_t get_serial_number_as_u32(); char * get_totp_slot_name(uint8_t slot_number); char * get_hotp_slot_name(uint8_t slot_number); diff --git a/unittest/test_offline.cc b/unittest/test_offline.cc index 320ad48..3ca3905 100644 --- a/unittest/test_offline.cc +++ b/unittest/test_offline.cc @@ -66,6 +66,10 @@ TEST_CASE("Test C++ side behaviour in offline", "[fast]") { REQUIRE_NOTHROW (serial_number = i->get_serial_number()); REQUIRE(serial_number.empty()); + REQUIRE_THROWS_AS( + i->get_serial_number_as_u32(), DeviceNotConnected + ); + REQUIRE_THROWS_AS( i->get_status(), DeviceNotConnected ); -- cgit v1.2.3 From 02f19713c155457e8cc7d80d581bdac35cb1716d Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 2 Apr 2020 15:14:11 +0200 Subject: NitrokeyManager: Refactor get_serial_number MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit To avoid duplicate code, this patch refactors the get_serial_number method to use get_serial_number_as_u32 internally. It does not change get_serial_number’s behavior. --- NitrokeyManager.cc | 22 ++++++++-------------- 1 file changed, 8 insertions(+), 14 deletions(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 496496e..b6a076a 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -380,22 +380,16 @@ using nitrokey::misc::strcpyT; string NitrokeyManager::get_serial_number() { - if (device == nullptr) { return ""; }; - switch (device->get_device_model()) { - case DeviceModel::PRO: { - auto response = GetStatus::CommandTransaction::run(device); - return nitrokey::misc::toHex(response.data().card_serial_u32); - } - break; - - case DeviceModel::STORAGE: - { - auto response = stick20::GetDeviceStatus::CommandTransaction::run(device); - return nitrokey::misc::toHex(response.data().ActiveSmartCardID_u32); + try { + auto serial_number = this->get_serial_number_as_u32(); + if (serial_number == 0) { + return "NA"; + } else { + return nitrokey::misc::toHex(serial_number); } - break; + } catch (DeviceNotConnected& e) { + return ""; } - return "NA"; } uint32_t NitrokeyManager::get_serial_number_as_u32() { -- cgit v1.2.3 From fedf828e394938fb6f84407b4de7412a3fb6ec40 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Thu, 2 Apr 2020 15:19:00 +0200 Subject: Return serial number as uint32_t from C API This patch adds the function NK_device_serial_number_as_u32 to the C API. It is similar to NK_device_serial_number but returns the raw unsigned integer instead of a formatted string. This patch also adds a simple test case that ensures that the number is not zero. Fixes #172. --- NK_C_API.cc | 7 +++++++ NK_C_API.h | 8 ++++++++ unittest/test_pro.py | 9 ++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/NK_C_API.cc b/NK_C_API.cc index 1d3fa3a..c44e36f 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -299,6 +299,13 @@ extern "C" { }); } + NK_C_API uint32_t NK_device_serial_number_as_u32() { + auto m = NitrokeyManager::instance(); + return get_with_result([&]() { + return m->get_serial_number_as_u32(); + }); + } + NK_C_API char * NK_get_hotp_code(uint8_t slot_number) { return NK_get_hotp_code_PIN(slot_number, ""); } diff --git a/NK_C_API.h b/NK_C_API.h index d5c54a3..df3e992 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -385,6 +385,14 @@ extern "C" { */ NK_C_API char * NK_device_serial_number(); + /** + * Return the device's serial number string as an integer. Use + * NK_last_command_status to check for an error if this function + * returns zero. + * @return device's serial number as an integer + */ + NK_C_API uint32_t NK_device_serial_number_as_u32(); + /** * Get last command processing status. Useful for commands which returns the results of their own and could not return * an error code. diff --git a/unittest/test_pro.py b/unittest/test_pro.py index 99d7b1f..d25a50e 100644 --- a/unittest/test_pro.py +++ b/unittest/test_pro.py @@ -704,6 +704,13 @@ def test_get_serial_number(C): print(('Serial number of the device: ', sn)) +@pytest.mark.status +def test_get_serial_number_as_u32(C): + sn = C.NK_device_serial_number_as_u32() + assert sn > 0 + print(('Serial number of the device (u32): ', sn)) + + @pytest.mark.otp @pytest.mark.parametrize("secret", ['000001', '00'*10+'ff', '00'*19+'ff', '000102', '00'*29+'ff', '00'*39+'ff', '002EF43F51AFA97BA2B46418768123C9E1809A5B' ]) @@ -1038,4 +1045,4 @@ def test_OTP_all_rw(C): this_loop_codes.append(('H', i, code)) all_codes.append(this_loop_codes) from pprint import pprint - pprint(all_codes) \ No newline at end of file + pprint(all_codes) -- cgit v1.2.3