From 4200af146a17398dc7050c92e1f861f2066debec Mon Sep 17 00:00:00 2001 From: Amit Aronovitch Date: Wed, 2 Oct 2019 00:01:48 +0300 Subject: Identify Librem Key, behaving like Nitrokey Pro device --- libnitrokey/device.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'libnitrokey') diff --git a/libnitrokey/device.h b/libnitrokey/device.h index d50080d..d39310a 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -50,7 +50,8 @@ namespace device { enum class DeviceModel{ PRO, - STORAGE + STORAGE, + LIBREM }; std::ostream& operator<<(std::ostream& stream, DeviceModel model); @@ -67,12 +68,20 @@ extern const uint16_t NITROKEY_PRO_PID; * The USB product ID for the Nitrokey Storage. */ extern const uint16_t NITROKEY_STORAGE_PID; +/** + * The USB vendor ID for Purism devices. + */ +extern const uint16_t PURISM_VID; +/** + * The USB product ID for the Librem Key. + */ +extern const uint16_t LIBREM_KEY_PID; /** * Convert the given USB product ID to a Nitrokey model. If there is no model * with that ID, return an absent value. */ -misc::Option product_id_to_model(uint16_t product_id); +misc::Option product_id_to_model(uint16_t vendor_id, uint16_t product_id); /** * Information about a connected device. @@ -219,6 +228,12 @@ class Stick20 : public Device { public: Stick20(); }; + +class LibremKey : public Device { + public: + LibremKey(); +}; + } } #endif -- cgit v1.2.3 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(+) (limited to 'libnitrokey') 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 2e38681cd0b34e1ce36a6417445f3a7ca75f246c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 16 Apr 2020 11:23:10 +0200 Subject: Fix build warning - possibly uninitialized value --- libnitrokey/device_proto.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libnitrokey') diff --git a/libnitrokey/device_proto.h b/libnitrokey/device_proto.h index 45a6c16..6ffe5fb 100644 --- a/libnitrokey/device_proto.h +++ b/libnitrokey/device_proto.h @@ -249,7 +249,7 @@ namespace nitrokey { } dev->m_counters.total_comm_runs++; - int status; + int status = 0; OutgoingPacket outp; ResponsePacket resp; -- cgit v1.2.3 From 444a6cb764fbcea3c91ae936b1c76a190f935b10 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 13 Jun 2020 12:21:44 +0200 Subject: Revert API change Remove the change to keep binary compatibility. Use the vendor_id field from the dev description. --- NitrokeyManager.cc | 2 +- device.cc | 4 ++++ libnitrokey/device.h | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) (limited to 'libnitrokey') diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index d874dca..71d156f 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -230,7 +230,7 @@ using nitrokey::misc::strcpyT; misc::Option model; while (info_ptr && !model.has_value()) { if (path == std::string(info_ptr->path)) { - model = product_id_to_model(vendor_id, info_ptr->product_id); + model = product_id_to_model(info_ptr->vendor_id, info_ptr->product_id); } info_ptr = info_ptr->next; } diff --git a/device.cc b/device.cc index 5f89f2c..9cf4dc3 100644 --- a/device.cc +++ b/device.cc @@ -48,6 +48,10 @@ const uint16_t nitrokey::device::NITROKEY_STORAGE_PID = 0x4109; const uint16_t nitrokey::device::PURISM_VID = 0x316d; const uint16_t nitrokey::device::LIBREM_KEY_PID = 0x4c4b; +Option nitrokey::device::product_id_to_model(uint16_t product_id) { + return product_id_to_model(NITROKEY_VID, product_id); +} + Option nitrokey::device::product_id_to_model(uint16_t vendor_id, uint16_t product_id) { switch (vendor_id) { case NITROKEY_VID: diff --git a/libnitrokey/device.h b/libnitrokey/device.h index d39310a..917e0d0 100644 --- a/libnitrokey/device.h +++ b/libnitrokey/device.h @@ -81,6 +81,7 @@ extern const uint16_t LIBREM_KEY_PID; * Convert the given USB product ID to a Nitrokey model. If there is no model * with that ID, return an absent value. */ +misc::Option product_id_to_model(uint16_t product_id); misc::Option product_id_to_model(uint16_t vendor_id, uint16_t product_id); /** -- cgit v1.2.3