From 2faa8f6782a2e6294ed8849048a281d12d60da1c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 4 Dec 2017 16:29:56 +0100 Subject: Initial support for multiple devices with C++ and test Signed-off-by: Szczepan Zalega --- include/NitrokeyManager.h | 7 ++++++- include/device.h | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) (limited to 'include') diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 0db0856..2a79922 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -30,6 +30,7 @@ #include "stick20_commands.h" #include #include +#include namespace nitrokey { using namespace nitrokey::device; @@ -67,6 +68,8 @@ char * strndup(const char* str, size_t maxlen); bool get_time(uint64_t time = 0); bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); + std::vector list_devices(); + bool connect_with_path (std::string path); bool connect(const char *device_model); bool connect(); bool disconnect(); @@ -198,8 +201,10 @@ char * strndup(const char* str, size_t maxlen); static shared_ptr _instance; std::shared_ptr device; + std::unordered_map > connected_devices; - stick10::ReadSlot::ResponsePayload get_OTP_slot_data(const uint8_t slot_number); + + stick10::ReadSlot::ResponsePayload get_OTP_slot_data(const uint8_t slot_number); bool is_valid_hotp_slot_number(uint8_t slot_number) const; bool is_valid_totp_slot_number(uint8_t slot_number) const; bool is_valid_password_safe_slot_number(uint8_t slot_number) const; diff --git a/include/device.h b/include/device.h index 1bd4773..f6d2380 100644 --- a/include/device.h +++ b/include/device.h @@ -25,6 +25,7 @@ #include "hidapi/hidapi.h" #include #include +#include #define HID_REPORT_SIZE 65 @@ -105,8 +106,10 @@ public: * @return true if visible by OS */ bool could_be_enumerated(); + std::vector enumerate(); - void show_stats(); + + void show_stats(); // ErrorCounters get_stats(){ return m_counters; } int get_retry_receiving_count() const { return m_retry_receiving_count; }; int get_retry_sending_count() const { return m_retry_sending_count; }; @@ -121,8 +124,10 @@ public: void set_retry_delay(std::chrono::milliseconds delay); static void set_default_device_speed(int delay); void setDefaultDelay(); + void set_path(const std::string path); + -private: + private: std::atomic last_command_status; void _reconnect(); bool _connect(); @@ -143,6 +148,7 @@ protected: std::chrono::milliseconds m_retry_timeout; std::chrono::milliseconds m_send_receive_delay; std::atomicmp_devhandle; + std::string m_path; static std::atomic_int instances_count; static std::chrono::milliseconds default_delay ; -- cgit v1.2.3 From 388cf5fcb33f24bc04f79cd1fbea980214518d54 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Mon, 4 Dec 2017 19:21:09 +0100 Subject: List devices by unique SC:SD id Add C API and tests Add mutexes Signed-off-by: Szczepan Zalega --- NK_C_API.cc | 21 ++++++++++ NK_C_API.h | 6 +++ NitrokeyManager.cc | 87 +++++++++++++++++++++++++++++++++++---- include/NitrokeyManager.h | 3 ++ unittest/test_C_API.cpp | 28 +++++++++++++ unittest/test_multiple_devices.cc | 24 ++++++++++- 6 files changed, 160 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/NK_C_API.cc b/NK_C_API.cc index f881caf..59247ba 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -608,6 +608,27 @@ extern "C" { }); } + NK_C_API const char* NK_list_devices_by_cpuID() { + auto nm = NitrokeyManager::instance(); + return get_with_string_result([&]() { + auto v = nm->list_devices_by_cpuID(); + std::string res; + for (const auto a : v){ + res += a+";"; + } + if (res.size()>0) res.pop_back(); // remove last delimeter char + return strndup(res.c_str(), 4096); + }); + } + + NK_C_API int NK_connect_with_ID(const char* id) { + auto m = NitrokeyManager::instance(); + return get_with_result([&]() { + return m->connect_with_ID(id) ? 1 : 0; + }); + } + + #ifdef __cplusplus } diff --git a/NK_C_API.h b/NK_C_API.h index 73022b2..5933c67 100644 --- a/NK_C_API.h +++ b/NK_C_API.h @@ -568,6 +568,12 @@ extern "C" { */ NK_C_API int NK_get_progress_bar_value(); + + NK_C_API const char* NK_list_devices_by_cpuID(); + NK_C_API int NK_connect_with_ID(const char* id); + + + #ifdef __cplusplus } #endif diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index da5f61a..4935076 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -81,6 +81,8 @@ using nitrokey::misc::strcpyT; set_debug(true); } NitrokeyManager::~NitrokeyManager() { + std::lock_guard lock(mex_dev_com_manager); + for (auto d : connected_devices){ if (d.second == nullptr) continue; d.second->disconnect(); @@ -104,27 +106,98 @@ using nitrokey::misc::strcpyT; } std::vector NitrokeyManager::list_devices(){ + std::lock_guard lock(mex_dev_com_manager); + auto p = make_shared(); return p->enumerate(); // make static } - bool NitrokeyManager::connect_with_path(std::string path) { + std::vector NitrokeyManager::list_devices_by_cpuID(){ std::lock_guard lock(mex_dev_com_manager); - if(connected_devices.find(path) != connected_devices.end() - && connected_devices[path] != nullptr) { - device = connected_devices[path]; - return true; + std::vector res; + auto d = make_shared(); + const auto v = d->enumerate(); + for (auto & p: v){ + d = make_shared(); + d->set_path(p); + try{ + if (d->connect()){ + device = d; + const auto status = get_status_storage(); + const auto sc_id = status.ActiveSmartCardID_u32; + const auto sd_id = status.ActiveSD_CardID_u32; + + auto id = std::to_string(sc_id) + ":" + std::to_string(sd_id); + connected_devices_byID[id] = d; + res.push_back(id); + } else{ + std::cout << "Could not connect to: " + p << std::endl; + } + } + catch (const DeviceCommunicationException &e){ + //ignore + std::cout << p << ": " << " Exception encountered" << std::endl; + } } + return res; + } +/** + * Connect to the device using unique smartcard:datacard id. + * Needs list_device_by_cpuID run first + * @param id + * @return + */ + bool NitrokeyManager::connect_with_ID(const std::string id) { + std::lock_guard lock(mex_dev_com_manager); + auto position = connected_devices_byID.find(id); + if (position == connected_devices_byID.end()) return false; + + auto d = connected_devices_byID[id]; + device = d; + + try{ + get_status(); + } + catch (const DeviceCommunicationException &){ + d->disconnect(); + connected_devices_byID[id] = nullptr; + connected_devices_byID.erase(position); + return false; + } + return true; + } + + /** + * Connects device to path. + * Assumes devices are not being disconnected and caches connections (param cache_connections). + * @param path os-dependent device path + * @return false, when could not connect, true otherwise + */ + bool NitrokeyManager::connect_with_path(std::string path) { + const bool cache_connections = false; + + std::lock_guard lock(mex_dev_com_manager); + + if (cache_connections){ + if(connected_devices.find(path) != connected_devices.end() + && connected_devices[path] != nullptr) { + device = connected_devices[path]; + return true; + } + } auto p = make_shared(); p->set_path(path); if(!p->connect()) return false; - connected_devices [path] = p; - device = p; + if(cache_connections){ + connected_devices [path] = p; + } + + device = p; //previous device will be disconnected automatically return true; } diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 2a79922..7ce432f 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -69,6 +69,8 @@ char * strndup(const char* str, size_t maxlen); bool erase_totp_slot(uint8_t slot_number, const char *temporary_password); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); std::vector list_devices(); + std::vector list_devices_by_cpuID(); + bool connect_with_ID(const std::string id); bool connect_with_path (std::string path); bool connect(const char *device_model); bool connect(); @@ -202,6 +204,7 @@ char * strndup(const char* str, size_t maxlen); static shared_ptr _instance; std::shared_ptr device; std::unordered_map > connected_devices; + std::unordered_map > connected_devices_byID; stick10::ReadSlot::ResponsePayload get_OTP_slot_data(const uint8_t slot_number); diff --git a/unittest/test_C_API.cpp b/unittest/test_C_API.cpp index be47f08..2d83ef4 100644 --- a/unittest/test_C_API.cpp +++ b/unittest/test_C_API.cpp @@ -43,6 +43,7 @@ TEST_CASE("C API connect", "[BASIC]") { TEST_CASE("Check retry count", "[BASIC]") { REQUIRE(login != 0); + NK_set_debug_level(3); REQUIRE(NK_get_admin_retry_count() == 3); REQUIRE(NK_get_user_retry_count() == 3); } @@ -56,4 +57,31 @@ TEST_CASE("Check long strings", "[STANDARD]") { result = NK_change_user_PIN(pin, longPin); REQUIRE(result == TOO_LONG_STRING); CAPTURE(result); +} + +#include + +TEST_CASE("multiple devices with ID", "[BASIC]") { + NK_logout(); + NK_set_debug_level(3); + auto s = NK_list_devices_by_cpuID(); + REQUIRE(s!=nullptr); + REQUIRE(strnlen(s, 4096) < 4096); + REQUIRE(strnlen(s, 4096) > 2*4); + std::cout << s << std::endl; + + char *string, *token; + int t; + + string = strndup(s, 4096); + free ( (void*) s); + + while ((token = strsep(&string, ";")) != nullptr){ + if (strnlen(token, 4096) < 3) continue; + std::cout << token << " connecting: "; + std::cout << (t=NK_connect_with_ID(token)) << std::endl; + REQUIRE(t == 1); + } + + free (string); } \ No newline at end of file diff --git a/unittest/test_multiple_devices.cc b/unittest/test_multiple_devices.cc index f5b4d6e..235a24d 100644 --- a/unittest/test_multiple_devices.cc +++ b/unittest/test_multiple_devices.cc @@ -78,15 +78,35 @@ TEST_CASE("Use API", "[BASIC]") { << " " << status_storage.ActiveSD_CardID_u32 << std::endl; - nm->fill_SD_card_with_random_data("12345678"); +// nm->fill_SD_card_with_random_data("12345678"); } catch (const LongOperationInProgressException &e){ std::cout << "long operation in progress on " << a << " " << std::to_string(e.progress_bar_value) << std::endl; - this_thread::sleep_for(1000ms); +// this_thread::sleep_for(1000ms); } } std::cout <<"Iteration: " << i << std::endl; } +} + + +TEST_CASE("Use API ID", "[BASIC]") { + auto nm = NitrokeyManager::instance(); + nm->set_loglevel(2); + + auto v = nm->list_devices_by_cpuID(); + REQUIRE(v.size() > 0); + + for(int i=0; i<1000; i++) { + auto v = nm->list_devices_by_cpuID(); + REQUIRE(v.size() > 0); + for (auto i : v) { + nm->connect_with_ID(i); + auto retry_count = nm->get_admin_retry_count(); + std::cout << i << " " << to_string(retry_count) << std::endl; + } + } + std::cout << "finished" << std::endl; } \ No newline at end of file -- cgit v1.2.3 From 9aa7bb5c6783d28d7d4ffe667fdfbc7400615587 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 8 Dec 2017 10:43:37 +0100 Subject: Add LOGD1 macro Signed-off-by: Szczepan Zalega --- include/log.h | 1 + 1 file changed, 1 insertion(+) (limited to 'include') diff --git a/include/log.h b/include/log.h index 52a6e16..89eda56 100644 --- a/include/log.h +++ b/include/log.h @@ -98,6 +98,7 @@ namespace nitrokey { #define LOGD(string) while(false){} #else #define LOG(string, level) nitrokey::log::Log::instance()((string), (level)) +#define LOGD1(string) nitrokey::log::Log::instance()((string), (nitrokey::log::Loglevel::DEBUG_L1)) #define LOGD(string) nitrokey::log::Log::instance()((string), (nitrokey::log::Loglevel::DEBUG_L2)) #endif -- cgit v1.2.3 From 38c3b4c58e6c6b86d6241183be1814bac2e037d2 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Fri, 16 Feb 2018 15:17:47 +0100 Subject: Log current device ID Allow logger to set global prefix Used to indicate current device Store USB path when used to connection as well Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 16 +++++++++++++++- device.cc | 2 +- include/NitrokeyManager.h | 5 +++++ include/log.h | 5 +++++ log.cc | 17 +++++++++++++++-- 5 files changed, 41 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index 162ad93..f2518b1 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -171,10 +171,14 @@ using nitrokey::misc::strcpyT; std::lock_guard lock(mex_dev_com_manager); auto position = connected_devices_byID.find(id); - if (position == connected_devices_byID.end()) return false; + if (position == connected_devices_byID.end()) { + LOGD1(std::string("Could not find device ")+id); + return false; + } auto d = connected_devices_byID[id]; device = d; + current_device_id = id; //validate connection try{ @@ -185,10 +189,13 @@ using nitrokey::misc::strcpyT; } catch (const DeviceCommunicationException &){ d->disconnect(); + current_device_id = ""; connected_devices_byID[id] = nullptr; connected_devices_byID.erase(position); return false; } + nitrokey::log::Log::setPrefix(id); + LOGD1("Device successfully changed"); return true; } @@ -221,6 +228,9 @@ using nitrokey::misc::strcpyT; } device = p; //previous device will be disconnected automatically + current_device_id = path; + nitrokey::log::Log::setPrefix(path); + LOGD1("Device successfully changed"); return true; } @@ -1105,5 +1115,9 @@ using nitrokey::misc::strcpyT; return data.data().SD_Card_Size_u8; } + const string NitrokeyManager::get_current_device_id() const { + return current_device_id; + } + } diff --git a/device.cc b/device.cc index 9321d2a..da5345b 100644 --- a/device.cc +++ b/device.cc @@ -248,7 +248,7 @@ Stick10::Stick10(): Stick20::Stick20(): - Device(0x20a0, 0x4109, DeviceModel::STORAGE, 40ms, 25, 40ms) + Device(0x20a0, 0x4109, DeviceModel::STORAGE, 40ms, 55, 40ms) { setDefaultDelay(); } diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 7ce432f..5a76616 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -203,6 +203,11 @@ char * strndup(const char* str, size_t maxlen); static shared_ptr _instance; std::shared_ptr device; + std::string current_device_id; + public: + const string get_current_device_id() const; + + private: std::unordered_map > connected_devices; std::unordered_map > connected_devices_byID; diff --git a/include/log.h b/include/log.h index 89eda56..b969b81 100644 --- a/include/log.h +++ b/include/log.h @@ -86,6 +86,11 @@ namespace nitrokey { private: LogHandler *mp_loghandler; Loglevel m_loglevel; + static std::string prefix; + public: + static void setPrefix(std::string prefix = std::string()); + + private: static Log *mp_instance; }; diff --git a/log.cc b/log.cc index 263ddd7..e66feb0 100644 --- a/log.cc +++ b/log.cc @@ -26,6 +26,7 @@ #include "log.h" #include +#include namespace nitrokey { namespace log { @@ -33,6 +34,9 @@ namespace nitrokey { Log *Log::mp_instance = nullptr; StdlogHandler stdlog_handler; + std::string Log::prefix = ""; + + std::string LogHandler::loglevel_to_str(Loglevel lvl) { switch (lvl) { case Loglevel::DEBUG_L1: @@ -52,8 +56,17 @@ namespace nitrokey { } void Log::operator()(const std::string &logstr, Loglevel lvl) { - if (mp_loghandler != nullptr) - if ((int) lvl <= (int) m_loglevel) mp_loghandler->print(logstr, lvl); + if (mp_loghandler != nullptr){ + if ((int) lvl <= (int) m_loglevel) mp_loghandler->print(prefix+logstr, lvl); + } + } + + void Log::setPrefix(const string prefix) { + if (!prefix.empty()){ + Log::prefix = "["+prefix+"]"; + } else { + Log::prefix = ""; + } } void StdlogHandler::print(const std::string &str, Loglevel lvl) { -- cgit v1.2.3 From 01ed5b4302f21a3d77300c2e6f34344fec36d40c Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 1 Mar 2018 11:26:10 +0100 Subject: Clean headers Signed-off-by: Szczepan Zalega --- include/log.h | 2 -- log.cc | 6 ++---- 2 files changed, 2 insertions(+), 6 deletions(-) (limited to 'include') diff --git a/include/log.h b/include/log.h index b969b81..2a64bef 100644 --- a/include/log.h +++ b/include/log.h @@ -23,8 +23,6 @@ #define LOG_H #include -#include - #include namespace nitrokey { diff --git a/log.cc b/log.cc index e66feb0..06acee7 100644 --- a/log.cc +++ b/log.cc @@ -19,14 +19,12 @@ * SPDX-License-Identifier: LGPL-3.0 */ +#include "log.h" #include -#include #include #include -#include "log.h" #include -#include namespace nitrokey { namespace log { @@ -61,7 +59,7 @@ namespace nitrokey { } } - void Log::setPrefix(const string prefix) { + void Log::setPrefix(const std::string prefix) { if (!prefix.empty()){ Log::prefix = "["+prefix+"]"; } else { -- cgit v1.2.3 From f5da7b4cbd508faef38fcdfff659fab5c4649838 Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Thu, 1 Mar 2018 11:29:57 +0100 Subject: Minor cleanup Signed-off-by: Szczepan Zalega --- NitrokeyManager.cc | 12 ++++-------- include/NitrokeyManager.h | 7 +++++++ 2 files changed, 11 insertions(+), 8 deletions(-) (limited to 'include') diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index fa69ac6..e5f10df 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -142,7 +142,8 @@ using nitrokey::misc::strcpyT; const auto status = get_status_storage(); const auto sc_id = toHex(status.ActiveSmartCardID_u32); const auto sd_id = toHex(status.ActiveSD_CardID_u32); - id = sc_id + ":" + sd_id + "_p_" + p; + id += sc_id + ":" + sd_id; + id += "_p_" + p; } catch (const LongOperationInProgressException &e) { LOGD1(std::string("Long operation in progress, setting ID to: ") + p); @@ -162,18 +163,13 @@ using nitrokey::misc::strcpyT; } return res; } -/** - * Connect to the device using unique smartcard:datacard id. - * Needs list_device_by_cpuID run first - * @param id - * @return - */ + bool NitrokeyManager::connect_with_ID(const std::string id) { std::lock_guard lock(mex_dev_com_manager); auto position = connected_devices_byID.find(id); if (position == connected_devices_byID.end()) { - LOGD1(std::string("Could not find device ")+id); + LOGD1(std::string("Could not find device ")+id + ". Refresh devices list with list_devices_by_cpuID()."); return false; } diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h index 5a76616..ca58d24 100644 --- a/include/NitrokeyManager.h +++ b/include/NitrokeyManager.h @@ -70,6 +70,13 @@ char * strndup(const char* str, size_t maxlen); bool erase_hotp_slot(uint8_t slot_number, const char *temporary_password); std::vector list_devices(); std::vector list_devices_by_cpuID(); + + /** + * Connect to the device using unique smartcard:datacard id. + * Needs list_device_by_cpuID() run first + * @param id Current ID of the target device + * @return true on success, false on failure + */ bool connect_with_ID(const std::string id); bool connect_with_path (std::string path); bool connect(const char *device_model); -- cgit v1.2.3