From 1ba9938837a726580728c64bdd77d9676c98a020 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 6 Jan 2020 00:36:07 +0100 Subject: [PATCH 1/2] test_pro: Extend test_get_status test case This patch extends the test_get_status test case to check the content of the returned status struct. The firmware version, serial number and config should match the values returned by the NK_*_firmware_version, NK_device_serial_number and NK_read_config functions. This does not work for the Nitrokey Storage, which will be fixed in the next patch. --- unittest/test_pro.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/unittest/test_pro.py b/unittest/test_pro.py index a8df7cdf..203c8b1b 100644 --- a/unittest/test_pro.py +++ b/unittest/test_pro.py @@ -723,8 +723,27 @@ def test_get_status(C): raise Exception("Could not allocate status") err = C.NK_get_status(status_st) assert err == 0 + + # check firmware version + firmware_version_major = C.NK_get_major_firmware_version() + firmware_version_minor = C.NK_get_minor_firmware_version() + assert status_st.firmware_version_major == 0 assert status_st.firmware_version_minor != 0 + assert status_st.firmware_version_major == firmware_version_major + assert status_st.firmware_version_minor == firmware_version_minor + + # check serial number + serial_number = gs(C.NK_device_serial_number()) + assert status_st.serial_number_smart_card != 0 + assert '{:08x}'.format(status_st.serial_number_smart_card) == serial_number.decode('ascii') + + # check config + config = C.NK_read_config() + assert status_st.config_numlock == config[0] + assert status_st.config_capslock == config[1] + assert status_st.config_scrolllock == config[2] + assert status_st.otp_user_password == config[3] @pytest.mark.status @@ -1094,4 +1113,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) From dd0cdcd2130699be8dde5dbadcf18cd9c00ef78f Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 6 Jan 2020 00:47:13 +0100 Subject: [PATCH 2/2] Also execute GET_DEVICE_STATUS in get_status for Storage As described in this issue [0], the GET_STATUS command does not work for the Nitrokey Storage. Therefore, NitrokeyManager::get_status does not work either. To fix this, we additionally execute the GET_DEVICE_STATUS command if a Nitrokey Storage is detected to determine the serial number and the firmware version. This also fixes the failing test test_get_status that we introduced in the last patch. Fixes #166. [0] https://github.com/Nitrokey/nitrokey-storage-firmware/issues/96 --- NitrokeyManager.cc | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 6c26a434..f345692f 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -401,7 +401,19 @@ using nitrokey::misc::strcpyT; stick10::GetStatus::ResponsePayload NitrokeyManager::get_status(){ try{ auto response = GetStatus::CommandTransaction::run(device); - return response.data(); + auto data = response.data(); + if (device != nullptr && device->get_device_model() == DeviceModel::STORAGE) { + // The GET_STATUS command does not work for the Nitrokey Storage, + // see https://github.com/Nitrokey/nitrokey-storage-firmware/issues/96 + // Therefore, we have to use the GET_DEVICE_STATUS command to query + // the correct serial number and firmware version. + auto response2 = stick20::GetDeviceStatus::CommandTransaction::run(device); + auto data2 = response2.data(); + data.firmware_version_st.major = data2.versionInfo.major; + data.firmware_version_st.minor = data2.versionInfo.minor; + data.card_serial_u32 = data2.ActiveSmartCardID_u32; + } + return data; } catch (DeviceSendingFailure &e){ // disconnect();