aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSzczepan Zalega <szczepan@nitrokey.com>2020-07-31 13:02:15 +0200
committerSzczepan Zalega <szczepan@nitrokey.com>2020-07-31 13:02:15 +0200
commit4b698519fc125ee03f47560402e2c35c495e0f7c (patch)
tree80d703803cec1b09d80e2aa0ebf6bb3ce2cd5601
parent2281f33430f3edb874f7fd350ed173e5476a30ed (diff)
downloadlibnitrokey-4b698519fc125ee03f47560402e2c35c495e0f7c.tar.gz
libnitrokey-4b698519fc125ee03f47560402e2c35c495e0f7c.tar.bz2
Make C API modularized - extract PWS and OTP
Signed-off-by: Szczepan Zalega <szczepan@nitrokey.com>
-rw-r--r--CMakeLists.txt6
-rw-r--r--NK_C_API.cc225
-rw-r--r--NK_C_API_helpers.cpp5
-rw-r--r--NK_C_API_helpers.h1
-rw-r--r--NK_C_API_otp.cpp196
-rw-r--r--NK_C_API_pws.cpp77
6 files changed, 285 insertions, 225 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c795e22..c592a9f 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -81,6 +81,9 @@ set(SOURCE_FILES
NitrokeyManagerPWS.cc
NK_C_API.h
NK_C_API.cc
+ NK_C_API_pws.cpp
+ NK_C_API_otp.cpp
+ NK_C_API_helpers.cpp
NK_C_API_helpers.h
NK_C_API_storage.h
NK_C_API_storage.cpp
@@ -91,6 +94,9 @@ set(SOURCE_FILES_storage
NitrokeyManager.cc
NitrokeyManagerStorage.cpp
NitrokeyManagerStorage.h
+ NK_C_API.h
+ NK_C_API.cc
+ NK_C_API_helpers.cpp
NK_C_API_helpers.h
NK_C_API_storage.h
NK_C_API_storage.cpp
diff --git a/NK_C_API.cc b/NK_C_API.cc
index 434fb37..5460a92 100644
--- a/NK_C_API.cc
+++ b/NK_C_API.cc
@@ -35,7 +35,6 @@
using namespace nitrokey;
-const uint8_t NK_PWS_SLOT_COUNT = PWS_SLOT_COUNT;
uint8_t NK_last_command_status = 0;
#include "NK_C_API_helpers.h"
@@ -136,50 +135,6 @@ extern "C" {
});
}
- NK_C_API int NK_write_config(uint8_t numlock, uint8_t capslock, uint8_t scrolllock, bool enable_user_password,
- bool delete_user_password,
- const char *admin_temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- return m->write_config(numlock, capslock, scrolllock, enable_user_password, delete_user_password, admin_temporary_password);
- });
- }
-
- NK_C_API int NK_write_config_struct(struct NK_config config,
- const char *admin_temporary_password) {
- return NK_write_config(config.numlock, config.capslock, config.scrolllock, config.enable_user_password,
- config.disable_user_password, admin_temporary_password);
- }
-
-
- NK_C_API uint8_t* NK_read_config() {
- auto m = NitrokeyManager::instance();
- return get_with_array_result([&]() {
- auto v = m->read_config();
- return duplicate_vector_and_clear(v);
- });
- }
-
- NK_C_API void NK_free_config(uint8_t* config) {
- delete[] config;
- }
-
- NK_C_API int NK_read_config_struct(struct NK_config* out) {
- if (out == nullptr) {
- return -1;
- }
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- auto v = m->read_config();
- out->numlock = v[0];
- out->capslock = v[1];
- out->scrolllock = v[2];
- out->enable_user_password = v[3];
- out->disable_user_password = v[4];
- });
- }
-
-
NK_C_API enum NK_device_model NK_get_device_model() {
auto m = NitrokeyManager::instance();
try {
@@ -200,12 +155,6 @@ extern "C" {
}
}
-
- void clear_string(std::string &s) {
- std::fill(s.begin(), s.end(), ' ');
- }
-
-
NK_C_API char * NK_status() {
return NK_get_status_as_string();
}
@@ -261,91 +210,11 @@ extern "C" {
});
}
- NK_C_API char * NK_get_hotp_code(uint8_t slot_number) {
- return NK_get_hotp_code_PIN(slot_number, "");
- }
-
- NK_C_API char * NK_get_hotp_code_PIN(uint8_t slot_number, const char *user_temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- string && s = m->get_HOTP_code(slot_number, user_temporary_password);
- char * rs = strndup(s.c_str(), max_string_field_length);
- clear_string(s);
- return rs;
- });
- }
-
- NK_C_API char * NK_get_totp_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time,
- uint8_t last_interval) {
- return NK_get_totp_code_PIN(slot_number, challenge, last_totp_time, last_interval, "");
- }
-
- NK_C_API char * NK_get_totp_code_PIN(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time,
- uint8_t last_interval, const char *user_temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- string && s = m->get_TOTP_code(slot_number, challenge, last_totp_time, last_interval, user_temporary_password);
- char * rs = strndup(s.c_str(), max_string_field_length);
- clear_string(s);
- return rs;
- });
- }
-
- NK_C_API int NK_erase_hotp_slot(uint8_t slot_number, const char *temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&] {
- m->erase_hotp_slot(slot_number, temporary_password);
- });
- }
-
- NK_C_API int NK_erase_totp_slot(uint8_t slot_number, const char *temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&] {
- m->erase_totp_slot(slot_number, temporary_password);
- });
- }
-
- NK_C_API int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint64_t hotp_counter,
- bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID,
- const char *temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&] {
- m->write_HOTP_slot(slot_number, slot_name, secret, hotp_counter, use_8_digits, use_enter, use_tokenID, token_ID,
- temporary_password);
- });
- }
-
- NK_C_API int NK_write_totp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint16_t time_window,
- bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID,
- const char *temporary_password) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&] {
- m->write_TOTP_slot(slot_number, slot_name, secret, time_window, use_8_digits, use_enter, use_tokenID, token_ID,
- temporary_password);
- });
- }
-
- NK_C_API char* NK_get_totp_slot_name(uint8_t slot_number) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- const auto slot_name = m->get_totp_slot_name(slot_number);
- return slot_name;
- });
- }
- NK_C_API char* NK_get_hotp_slot_name(uint8_t slot_number) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- const auto slot_name = m->get_hotp_slot_name(slot_number);
- return slot_name;
- });
- }
-
NK_C_API void NK_set_debug(bool state) {
auto m = NitrokeyManager::instance();
m->set_debug(state);
}
-
NK_C_API void NK_set_debug_level(const int level) {
auto m = NitrokeyManager::instance();
m->set_loglevel(level);
@@ -363,24 +232,6 @@ extern "C" {
return get_library_version();
}
- NK_C_API int NK_totp_set_time(uint64_t time) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- m->set_time(time);
- });
- }
-
- NK_C_API int NK_totp_set_time_soft(uint64_t time) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- m->set_time_soft(time);
- });
- }
-
- NK_C_API int NK_totp_get_time() {
- return 0;
- }
-
NK_C_API int NK_change_admin_PIN(const char *current_PIN, const char *new_PIN) {
auto m = NitrokeyManager::instance();
return get_without_result([&]() {
@@ -395,25 +246,6 @@ extern "C" {
});
}
- NK_C_API int NK_enable_password_safe(const char *user_pin) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- m->enable_password_safe(user_pin);
- });
- }
- NK_C_API uint8_t * NK_get_password_safe_slot_status() {
- auto m = NitrokeyManager::instance();
- return get_with_array_result([&]() {
- auto slot_status = m->get_password_safe_slot_status();
- return duplicate_vector_and_clear(slot_status);
- });
-
- }
-
- NK_C_API void NK_free_password_safe_slot_status(uint8_t* status) {
- delete[] status;
- }
-
NK_C_API uint8_t NK_get_user_retry_count() {
auto m = NitrokeyManager::instance();
return get_with_result([&]() {
@@ -435,40 +267,6 @@ extern "C" {
});
}
- NK_C_API char *NK_get_password_safe_slot_name(uint8_t slot_number) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- return m->get_password_safe_slot_name(slot_number);
- });
- }
-
- NK_C_API char *NK_get_password_safe_slot_login(uint8_t slot_number) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- return m->get_password_safe_slot_login(slot_number);
- });
- }
- NK_C_API char *NK_get_password_safe_slot_password(uint8_t slot_number) {
- auto m = NitrokeyManager::instance();
- return get_with_string_result([&]() {
- return m->get_password_safe_slot_password(slot_number);
- });
- }
- NK_C_API int NK_write_password_safe_slot(uint8_t slot_number, const char *slot_name, const char *slot_login,
- const char *slot_password) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- m->write_password_safe_slot(slot_number, slot_name, slot_login, slot_password);
- });
- }
-
- NK_C_API int NK_erase_password_safe_slot(uint8_t slot_number) {
- auto m = NitrokeyManager::instance();
- return get_without_result([&]() {
- m->erase_password_safe_slot(slot_number);
- });
- }
-
NK_C_API int NK_is_AES_supported(const char *user_password) {
auto m = NitrokeyManager::instance();
return get_with_result([&]() {
@@ -612,29 +410,6 @@ extern "C" {
}
- NK_C_API int NK_read_HOTP_slot(const uint8_t slot_num, struct ReadSlot_t* out){
- if (out == nullptr)
- return -1;
- auto m = NitrokeyManager::instance();
- auto result = get_with_status([&]() {
- return m->get_HOTP_slot_data(slot_num);
- }, stick10::ReadSlot::ResponsePayload() );
- auto error_code = std::get<0>(result);
- if (error_code != 0) {
- return error_code;
- }
-#define a(x) out->x = read_slot.x
- stick10::ReadSlot::ResponsePayload read_slot = std::get<1>(result);
- a(_slot_config);
- a(slot_counter);
-#undef a
-#define m(x) memmove(out->x, read_slot.x, sizeof(read_slot.x))
- m(slot_name);
- m(slot_token_id);
-#undef m
- return 0;
-}
-
#ifdef __cplusplus
}
diff --git a/NK_C_API_helpers.cpp b/NK_C_API_helpers.cpp
new file mode 100644
index 0000000..f0353aa
--- /dev/null
+++ b/NK_C_API_helpers.cpp
@@ -0,0 +1,5 @@
+#include <string>
+
+void clear_string(std::string &s) {
+ std::fill(s.begin(), s.end(), ' ');
+} \ No newline at end of file
diff --git a/NK_C_API_helpers.h b/NK_C_API_helpers.h
index e9ae4d1..c5241be 100644
--- a/NK_C_API_helpers.h
+++ b/NK_C_API_helpers.h
@@ -13,6 +13,7 @@
#include "libnitrokey/device_proto.h"
#include "libnitrokey/version.h"
+void clear_string(std::string &s);
extern uint8_t NK_last_command_status;
diff --git a/NK_C_API_otp.cpp b/NK_C_API_otp.cpp
new file mode 100644
index 0000000..cc44ffc
--- /dev/null
+++ b/NK_C_API_otp.cpp
@@ -0,0 +1,196 @@
+
+#include "NK_C_API.h"
+#include "NK_C_API_helpers.h"
+#include "NitrokeyManagerOTP.h"
+#include "NitrokeyManagerPWS.h"
+#include "libnitrokey/LibraryException.h"
+#include "libnitrokey/NitrokeyManager.h"
+#include "libnitrokey/cxx_semantics.h"
+#include "libnitrokey/device_proto.h"
+#include "libnitrokey/stick20_commands.h"
+#include "libnitrokey/version.h"
+#include <cstring>
+#include <iostream>
+#include <tuple>
+
+#include "nk_strndup.h"
+
+using namespace nitrokey;
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+NK_C_API int NK_totp_set_time(uint64_t time) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() {
+ m->set_time(time);
+ });
+}
+
+NK_C_API int NK_totp_set_time_soft(uint64_t time) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() {
+ m->set_time_soft(time);
+ });
+}
+
+NK_C_API int NK_totp_get_time() {
+ return 0;
+}
+
+
+NK_C_API char * NK_get_hotp_code(uint8_t slot_number) {
+ return NK_get_hotp_code_PIN(slot_number, "");
+}
+
+NK_C_API char * NK_get_hotp_code_PIN(uint8_t slot_number, const char *user_temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result([&]() {
+ string && s = m->get_HOTP_code(slot_number, user_temporary_password);
+ char * rs = strndup(s.c_str(), max_string_field_length);
+ clear_string(s);
+ return rs;
+ });
+}
+
+NK_C_API char * NK_get_totp_code(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time,
+ uint8_t last_interval) {
+ return NK_get_totp_code_PIN(slot_number, challenge, last_totp_time, last_interval, "");
+}
+
+NK_C_API char * NK_get_totp_code_PIN(uint8_t slot_number, uint64_t challenge, uint64_t last_totp_time,
+ uint8_t last_interval, const char *user_temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result([&]() {
+ string && s = m->get_TOTP_code(slot_number, challenge, last_totp_time, last_interval, user_temporary_password);
+ char * rs = strndup(s.c_str(), max_string_field_length);
+ clear_string(s);
+ return rs;
+ });
+}
+
+NK_C_API int NK_erase_hotp_slot(uint8_t slot_number, const char *temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&] {
+ m->erase_hotp_slot(slot_number, temporary_password);
+ });
+}
+
+NK_C_API int NK_erase_totp_slot(uint8_t slot_number, const char *temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&] {
+ m->erase_totp_slot(slot_number, temporary_password);
+ });
+}
+
+NK_C_API int NK_write_hotp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint64_t hotp_counter,
+ bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID,
+ const char *temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&] {
+ m->write_HOTP_slot(slot_number, slot_name, secret, hotp_counter, use_8_digits, use_enter, use_tokenID, token_ID,
+ temporary_password);
+ });
+}
+
+NK_C_API int NK_write_totp_slot(uint8_t slot_number, const char *slot_name, const char *secret, uint16_t time_window,
+ bool use_8_digits, bool use_enter, bool use_tokenID, const char *token_ID,
+ const char *temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&] {
+ m->write_TOTP_slot(slot_number, slot_name, secret, time_window, use_8_digits, use_enter, use_tokenID, token_ID,
+ temporary_password);
+ });
+}
+
+NK_C_API char* NK_get_totp_slot_name(uint8_t slot_number) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result([&]() {
+ const auto slot_name = m->get_totp_slot_name(slot_number);
+ return slot_name;
+ });
+}
+NK_C_API char* NK_get_hotp_slot_name(uint8_t slot_number) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result([&]() {
+ const auto slot_name = m->get_hotp_slot_name(slot_number);
+ return slot_name;
+ });
+}
+
+
+NK_C_API int NK_read_HOTP_slot(const uint8_t slot_num, struct ReadSlot_t* out){
+ if (out == nullptr)
+ return -1;
+ auto m = NitrokeyManager::instance();
+ auto result = get_with_status([&]() {
+ return m->get_HOTP_slot_data(slot_num);
+ }, stick10::ReadSlot::ResponsePayload() );
+ auto error_code = std::get<0>(result);
+ if (error_code != 0) {
+ return error_code;
+ }
+#define a(x) out->x = read_slot.x
+ stick10::ReadSlot::ResponsePayload read_slot = std::get<1>(result);
+ a(_slot_config);
+ a(slot_counter);
+#undef a
+#define m(x) memmove(out->x, read_slot.x, sizeof(read_slot.x))
+ m(slot_name);
+ m(slot_token_id);
+#undef m
+ return 0;
+}
+
+
+NK_C_API int NK_write_config(uint8_t numlock, uint8_t capslock, uint8_t scrolllock, bool enable_user_password,
+ bool delete_user_password,
+ const char *admin_temporary_password) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() {
+ return m->write_config(numlock, capslock, scrolllock, enable_user_password, delete_user_password, admin_temporary_password);
+ });
+}
+
+NK_C_API int NK_write_config_struct(struct NK_config config,
+ const char *admin_temporary_password) {
+ return NK_write_config(config.numlock, config.capslock, config.scrolllock, config.enable_user_password,
+ config.disable_user_password, admin_temporary_password);
+}
+
+
+NK_C_API uint8_t* NK_read_config() {
+ auto m = NitrokeyManager::instance();
+ return get_with_array_result([&]() {
+ auto v = m->read_config();
+ return duplicate_vector_and_clear(v);
+ });
+}
+
+NK_C_API void NK_free_config(uint8_t* config) {
+ delete[] config;
+}
+
+NK_C_API int NK_read_config_struct(struct NK_config* out) {
+ if (out == nullptr) {
+ return -1;
+ }
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() {
+ auto v = m->read_config();
+ out->numlock = v[0];
+ out->capslock = v[1];
+ out->scrolllock = v[2];
+ out->enable_user_password = v[3];
+ out->disable_user_password = v[4];
+ });
+}
+
+
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/NK_C_API_pws.cpp b/NK_C_API_pws.cpp
new file mode 100644
index 0000000..27ca406
--- /dev/null
+++ b/NK_C_API_pws.cpp
@@ -0,0 +1,77 @@
+
+#include "NK_C_API.h"
+#include "NK_C_API_helpers.h"
+#include "NitrokeyManagerOTP.h"
+#include "NitrokeyManagerPWS.h"
+#include "libnitrokey/LibraryException.h"
+#include "libnitrokey/NitrokeyManager.h"
+#include "libnitrokey/cxx_semantics.h"
+#include "libnitrokey/device_proto.h"
+#include "libnitrokey/stick20_commands.h"
+#include "libnitrokey/version.h"
+#include <cstring>
+#include <iostream>
+#include <tuple>
+
+#include "nk_strndup.h"
+
+using namespace nitrokey;
+const uint8_t NK_PWS_SLOT_COUNT = PWS_SLOT_COUNT;
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+NK_C_API int NK_enable_password_safe(const char *user_pin) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() { m->enable_password_safe(user_pin); });
+}
+NK_C_API uint8_t *NK_get_password_safe_slot_status() {
+ auto m = NitrokeyManager::instance();
+ return get_with_array_result([&]() {
+ auto slot_status = m->get_password_safe_slot_status();
+ return duplicate_vector_and_clear(slot_status);
+ });
+}
+
+NK_C_API void NK_free_password_safe_slot_status(uint8_t *status) {
+ delete[] status;
+}
+
+NK_C_API char *NK_get_password_safe_slot_name(uint8_t slot_number) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result(
+ [&]() { return m->get_password_safe_slot_name(slot_number); });
+}
+
+NK_C_API char *NK_get_password_safe_slot_login(uint8_t slot_number) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result(
+ [&]() { return m->get_password_safe_slot_login(slot_number); });
+}
+NK_C_API char *NK_get_password_safe_slot_password(uint8_t slot_number) {
+ auto m = NitrokeyManager::instance();
+ return get_with_string_result(
+ [&]() { return m->get_password_safe_slot_password(slot_number); });
+}
+NK_C_API int NK_write_password_safe_slot(uint8_t slot_number,
+ const char *slot_name,
+ const char *slot_login,
+ const char *slot_password) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() {
+ m->write_password_safe_slot(slot_number, slot_name, slot_login,
+ slot_password);
+ });
+}
+
+NK_C_API int NK_erase_password_safe_slot(uint8_t slot_number) {
+ auto m = NitrokeyManager::instance();
+ return get_without_result(
+ [&]() { m->erase_password_safe_slot(slot_number); });
+}
+
+#ifdef __cplusplus
+}
+#endif