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 --- device.cc | 75 ++++++++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 55 insertions(+), 20 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index bc42965..5f89f2c 100644 --- a/device.cc +++ b/device.cc @@ -45,14 +45,29 @@ const uint16_t nitrokey::device::NITROKEY_VID = 0x20a0; const uint16_t nitrokey::device::NITROKEY_PRO_PID = 0x4108; const uint16_t nitrokey::device::NITROKEY_STORAGE_PID = 0x4109; -Option nitrokey::device::product_id_to_model(uint16_t product_id) { - switch (product_id) { +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 vendor_id, uint16_t product_id) { + switch (vendor_id) { + case NITROKEY_VID: + switch (product_id) { case NITROKEY_PRO_PID: return DeviceModel::PRO; case NITROKEY_STORAGE_PID: return DeviceModel::STORAGE; default: return {}; + } + case PURISM_VID: + switch (product_id) { + case LIBREM_KEY_PID: + return DeviceModel::LIBREM; + default: + return {}; + } + default: + return {}; } } @@ -67,6 +82,9 @@ std::ostream& nitrokey::device::operator<<(std::ostream& stream, DeviceModel mod case DeviceModel::STORAGE: stream << "Storage"; break; + case DeviceModel::LIBREM: + stream << "Librem"; + break; default: stream << "Unknown"; break; @@ -99,7 +117,9 @@ bool Device::disconnect() { } bool Device::_disconnect() { - LOG(std::string(__FUNCTION__) + std::string(m_model == DeviceModel::PRO ? "PRO" : "STORAGE"), Loglevel::DEBUG_L2); + LOG(std::string(__FUNCTION__) + + std::string(m_model == DeviceModel::PRO ? "PRO" : (m_model == DeviceModel::STORAGE ? "STORAGE" : "LIBREM")), + Loglevel::DEBUG_L2); LOG(std::string(__FUNCTION__) + std::string(" *IN* "), Loglevel::DEBUG_L2); if(mp_devhandle == nullptr) { @@ -204,27 +224,33 @@ int Device::recv(void *packet) { return status; } -std::vector Device::enumerate(){ - auto pInfo = hid_enumerate(NITROKEY_VID, 0); - auto pInfo_ = pInfo; - std::vector res; - while (pInfo != nullptr){ - auto deviceModel = product_id_to_model(pInfo->product_id); - if (deviceModel.has_value()) { - std::string path(pInfo->path); - std::wstring serialNumberW(pInfo->serial_number); - std::wstring_convert> converter; - std::string serialNumber = converter.to_bytes(serialNumberW); - DeviceInfo info = { deviceModel.value(), path, serialNumber }; - res.push_back(info); +namespace { + void add_vendor_devices(std::vector& res, uint16_t vendor_id){ + auto pInfo = hid_enumerate(vendor_id, 0); + auto pInfo_ = pInfo; + while (pInfo != nullptr){ + auto deviceModel = product_id_to_model(vendor_id, pInfo->product_id); + if (deviceModel.has_value()) { + std::string path(pInfo->path); + std::wstring serialNumberW(pInfo->serial_number); + std::wstring_convert> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = { deviceModel.value(), path, serialNumber }; + res.push_back(info); + } + pInfo = pInfo->next; } - pInfo = pInfo->next; - } - if (pInfo_ != nullptr){ - hid_free_enumeration(pInfo_); + if (pInfo_ != nullptr){ + hid_free_enumeration(pInfo_); + } } +} +std::vector Device::enumerate(){ + std::vector res; + ::add_vendor_devices(res, NITROKEY_VID); + ::add_vendor_devices(res, PURISM_VID); return res; } @@ -234,6 +260,8 @@ std::shared_ptr Device::create(DeviceModel model) { return std::make_shared(); case DeviceModel::STORAGE: return std::make_shared(); + case DeviceModel::LIBREM: + return std::make_shared(); default: return {}; } @@ -305,6 +333,13 @@ Stick20::Stick20(): setDefaultDelay(); } + +LibremKey::LibremKey(): + Device(PURISM_VID, LIBREM_KEY_PID, DeviceModel::LIBREM, 100ms, 5, 100ms) + { + setDefaultDelay(); + } + #include #define p(x) ss << #x << " " << x << ", "; std::string Device::ErrorCounters::get_as_string() { -- cgit v1.2.3 From 2f1c76bf82619e0809a41a1df4fe0e26c6315270 Mon Sep 17 00:00:00 2001 From: Robin Krahl Date: Mon, 6 Jan 2020 13:16:00 +0100 Subject: device: Check for nullptr in Device::enumerate While the fields of the hid_device_info struct should not be null, it is better to perform an explicit check before trying to construct strings from them. --- device.cc | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index bc42965..5f26d65 100644 --- a/device.cc +++ b/device.cc @@ -209,14 +209,16 @@ std::vector Device::enumerate(){ auto pInfo_ = pInfo; std::vector res; while (pInfo != nullptr){ - auto deviceModel = product_id_to_model(pInfo->product_id); - if (deviceModel.has_value()) { - std::string path(pInfo->path); - std::wstring serialNumberW(pInfo->serial_number); - std::wstring_convert> converter; - std::string serialNumber = converter.to_bytes(serialNumberW); - DeviceInfo info = { deviceModel.value(), path, serialNumber }; - res.push_back(info); + if (pInfo->path != nullptr && pInfo->serial_number != nullptr) { + auto deviceModel = product_id_to_model(pInfo->product_id); + if (deviceModel.has_value()) { + std::string path(pInfo->path); + std::wstring serialNumberW(pInfo->serial_number); + std::wstring_convert> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = { deviceModel.value(), path, serialNumber }; + res.push_back(info); + } } pInfo = pInfo->next; } -- 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 'device.cc') 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 From a3fd24ff87eeae3d320e3ee48f1c6e1b9ba0a783 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Sat, 13 Jun 2020 18:55:34 +0200 Subject: Invert if condition for readability --- device.cc | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'device.cc') diff --git a/device.cc b/device.cc index 5f26d65..1352bc7 100644 --- a/device.cc +++ b/device.cc @@ -209,16 +209,17 @@ std::vector Device::enumerate(){ auto pInfo_ = pInfo; std::vector res; while (pInfo != nullptr){ - if (pInfo->path != nullptr && pInfo->serial_number != nullptr) { - auto deviceModel = product_id_to_model(pInfo->product_id); - if (deviceModel.has_value()) { - std::string path(pInfo->path); - std::wstring serialNumberW(pInfo->serial_number); - std::wstring_convert> converter; - std::string serialNumber = converter.to_bytes(serialNumberW); - DeviceInfo info = { deviceModel.value(), path, serialNumber }; - res.push_back(info); - } + if (pInfo->path == nullptr || pInfo->serial_number == nullptr) { + continue; + } + auto deviceModel = product_id_to_model(pInfo->product_id); + if (deviceModel.has_value()) { + std::string path(pInfo->path); + std::wstring serialNumberW(pInfo->serial_number); + std::wstring_convert> converter; + std::string serialNumber = converter.to_bytes(serialNumberW); + DeviceInfo info = {deviceModel.value(), path, serialNumber}; + res.push_back(info); } pInfo = pInfo->next; } -- cgit v1.2.3