From 6c5f779dfd4dd5a91678351127acde132f817faf Mon Sep 17 00:00:00 2001 From: Szczepan Zalega Date: Tue, 28 Jul 2020 13:10:59 +0200 Subject: Extract PWS features to separate unit Signed-off-by: Szczepan Zalega --- CMakeLists.txt | 4 +++ NK_C_API.cc | 1 + NitrokeyManager.cc | 68 +-------------------------------------- NitrokeyManagerPWS.cc | 80 ++++++++++++++++++++++++++++++++++++++++++++++ NitrokeyManagerPWS.h | 6 ++++ NitrokeyManagerStorage.cpp | 1 - 6 files changed, 92 insertions(+), 68 deletions(-) create mode 100644 NitrokeyManagerPWS.cc create mode 100644 NitrokeyManagerPWS.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6e0bcb1..c795e22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -77,6 +77,8 @@ set(SOURCE_FILES NitrokeyManagerStorage.h NitrokeyManagerOTP.cc NitrokeyManagerOTP.h + NitrokeyManagerPWS.h + NitrokeyManagerPWS.cc NK_C_API.h NK_C_API.cc NK_C_API_helpers.h @@ -86,6 +88,7 @@ set(SOURCE_FILES set(SOURCE_FILES_storage ${COMMON_FILES} + NitrokeyManager.cc NitrokeyManagerStorage.cpp NitrokeyManagerStorage.h NK_C_API_helpers.h @@ -140,6 +143,7 @@ endif() OPTION(NO_LOG "Compile without logging functionality and its strings (decreases size)" OFF) IF (NO_LOG) SET_TARGET_PROPERTIES(nitrokey PROPERTIES COMPILE_DEFINITIONS "NO_LOG") + SET_TARGET_PROPERTIES(nitrokey-storage PROPERTIES COMPILE_DEFINITIONS "NO_LOG") ENDIF() OPTION(LOG_VOLATILE_DATA "Log volatile data (debug)" OFF) diff --git a/NK_C_API.cc b/NK_C_API.cc index 9dd0837..434fb37 100644 --- a/NK_C_API.cc +++ b/NK_C_API.cc @@ -40,6 +40,7 @@ uint8_t NK_last_command_status = 0; #include "NK_C_API_helpers.h" #include "NitrokeyManagerOTP.h" +#include "NitrokeyManagerPWS.h" #ifdef __cplusplus extern "C" { diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc index cde559b..fa12a5e 100644 --- a/NitrokeyManager.cc +++ b/NitrokeyManager.cc @@ -21,6 +21,7 @@ #include "libnitrokey/NitrokeyManager.h" #include "NitrokeyManagerOTP.h" +#include "NitrokeyManagerPWS.h" #include "libnitrokey/LibraryException.h" #include "libnitrokey/cxx_semantics.h" #include "libnitrokey/misc.h" @@ -465,25 +466,6 @@ using nitrokey::misc::strcpyT; } - void NitrokeyManager::enable_password_safe(const char *user_pin) { - //The following command will cancel enabling PWS if it is not supported - auto a = get_payload(); - strcpyT(a.user_password, user_pin); - IsAESSupported::CommandTransaction::run(device, a); - - auto p = get_payload(); - strcpyT(p.user_password, user_pin); - EnablePasswordSafe::CommandTransaction::run(device, p); - } - - vector NitrokeyManager::get_password_safe_slot_status() { - auto responsePayload = GetPasswordSafeSlotStatus::CommandTransaction::run(device); - vector v = vector(responsePayload.data().password_safe_status, - responsePayload.data().password_safe_status - + sizeof(responsePayload.data().password_safe_status)); - return v; - } - uint8_t NitrokeyManager::get_user_retry_count() { if(device->get_device_model() == DeviceModel::STORAGE){ stick20::GetDeviceStatus::CommandTransaction::run(device); @@ -504,54 +486,6 @@ using nitrokey::misc::strcpyT; LockDevice::CommandTransaction::run(device); } - char * NitrokeyManager::get_password_safe_slot_name(uint8_t slot_number) { - if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); - auto p = get_payload(); - p.slot_number = slot_number; - auto response = GetPasswordSafeSlotName::CommandTransaction::run(device, p); - return strndup((const char *) response.data().slot_name, max_string_field_length); - } - - bool NitrokeyManager::is_valid_password_safe_slot_number(uint8_t slot_number) const { return slot_number < 16; } - - char * NitrokeyManager::get_password_safe_slot_login(uint8_t slot_number) { - if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); - auto p = get_payload(); - p.slot_number = slot_number; - auto response = GetPasswordSafeSlotLogin::CommandTransaction::run(device, p); - return strndup((const char *) response.data().slot_login, max_string_field_length); - } - - char * NitrokeyManager::get_password_safe_slot_password(uint8_t slot_number) { - if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); - auto p = get_payload(); - p.slot_number = slot_number; - auto response = GetPasswordSafeSlotPassword::CommandTransaction::run(device, p); - return strndup((const char *) response.data().slot_password, max_string_field_length); //FIXME use secure way - } - - void NitrokeyManager::write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, - const char *slot_password) { - if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); - auto p = get_payload(); - p.slot_number = slot_number; - strcpyT(p.slot_name, slot_name); - strcpyT(p.slot_password, slot_password); - SetPasswordSafeSlotData::CommandTransaction::run(device, p); - - auto p2 = get_payload(); - p2.slot_number = slot_number; - strcpyT(p2.slot_login_name, slot_login); - SetPasswordSafeSlotData2::CommandTransaction::run(device, p2); - } - - void NitrokeyManager::erase_password_safe_slot(uint8_t slot_number) { - if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); - auto p = get_payload(); - p.slot_number = slot_number; - ErasePasswordSafeSlot::CommandTransaction::run(device, p); - } - void NitrokeyManager::user_authenticate(const char *user_password, const char *temporary_password) { auto p = get_payload(); strcpyT(p.card_password, user_password); diff --git a/NitrokeyManagerPWS.cc b/NitrokeyManagerPWS.cc new file mode 100644 index 0000000..5ccfd25 --- /dev/null +++ b/NitrokeyManagerPWS.cc @@ -0,0 +1,80 @@ +// +// Created by sz on 7/28/20. +// + +#include "NitrokeyManagerPWS.h" +#include "NitrokeyManagerOTP.h" +#include "libnitrokey/LibraryException.h" +#include "libnitrokey/NitrokeyManager.h" +#include "libnitrokey/cxx_semantics.h" +#include "libnitrokey/misc.h" +#include +#include +#include +#include +#include +#include +#include +#include +void nitrokey::NitrokeyManager::enable_password_safe(const char *user_pin) { + //The following command will cancel enabling PWS if it is not supported + auto a = get_payload(); + misc::strcpyT(a.user_password, user_pin); + IsAESSupported::CommandTransaction::run(device, a); + + auto p = get_payload(); + misc::strcpyT(p.user_password, user_pin); + EnablePasswordSafe::CommandTransaction::run(device, p); +} +std::vector nitrokey::NitrokeyManager::get_password_safe_slot_status() { + auto responsePayload = GetPasswordSafeSlotStatus::CommandTransaction::run(device); + vector v = vector(responsePayload.data().password_safe_status, + responsePayload.data().password_safe_status + + sizeof(responsePayload.data().password_safe_status)); + return v; +} +char * nitrokey::NitrokeyManager::get_password_safe_slot_name(uint8_t slot_number) { + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); + auto p = get_payload(); + p.slot_number = slot_number; + auto response = GetPasswordSafeSlotName::CommandTransaction::run(device, p); + return strndup((const char *) response.data().slot_name, max_string_field_length); +} +bool nitrokey::NitrokeyManager::is_valid_password_safe_slot_number(uint8_t slot_number) const { return slot_number < 16; +} +char * +nitrokey::NitrokeyManager::get_password_safe_slot_login(uint8_t slot_number) { + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); + auto p = get_payload(); + p.slot_number = slot_number; + auto response = GetPasswordSafeSlotLogin::CommandTransaction::run(device, p); + return strndup((const char *) response.data().slot_login, max_string_field_length); +} +char * nitrokey::NitrokeyManager::get_password_safe_slot_password(uint8_t slot_number) { + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); + auto p = get_payload(); + p.slot_number = slot_number; + auto response = GetPasswordSafeSlotPassword::CommandTransaction::run(device, p); + return strndup((const char *) response.data().slot_password, max_string_field_length); //FIXME use secure way +} +void nitrokey::NitrokeyManager::write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login, + const char *slot_password) { + if (!is_valid_password_safe_slot_number(slot_number)) + throw InvalidSlotException(slot_number); + auto p = get_payload(); + p.slot_number = slot_number; + misc::strcpyT(p.slot_name, slot_name); + misc::strcpyT(p.slot_password, slot_password); + SetPasswordSafeSlotData::CommandTransaction::run(device, p); + + auto p2 = get_payload(); + p2.slot_number = slot_number; + misc::strcpyT(p2.slot_login_name, slot_login); + SetPasswordSafeSlotData2::CommandTransaction::run(device, p2); +} +void nitrokey::NitrokeyManager::erase_password_safe_slot(uint8_t slot_number) { + if (!is_valid_password_safe_slot_number(slot_number)) throw InvalidSlotException(slot_number); + auto p = get_payload(); + p.slot_number = slot_number; + ErasePasswordSafeSlot::CommandTransaction::run(device, p); +} \ No newline at end of file diff --git a/NitrokeyManagerPWS.h b/NitrokeyManagerPWS.h new file mode 100644 index 0000000..b9f5071 --- /dev/null +++ b/NitrokeyManagerPWS.h @@ -0,0 +1,6 @@ +#ifndef LIBNITROKEY_NITROKEYMANAGERPWS_H +#define LIBNITROKEY_NITROKEYMANAGERPWS_H + +#include "NitrokeyManager.h" + +#endif // LIBNITROKEY_NITROKEYMANAGERPWS_H diff --git a/NitrokeyManagerStorage.cpp b/NitrokeyManagerStorage.cpp index 6814e8b..c78730b 100644 --- a/NitrokeyManagerStorage.cpp +++ b/NitrokeyManagerStorage.cpp @@ -1,6 +1,5 @@ #include "NitrokeyManagerStorage.h" -//using namespace nitrokey; namespace nitrokey{ using nitrokey::misc::strcpyT; -- cgit v1.2.3