aboutsummaryrefslogtreecommitdiff
path: root/NK_C_API.cc
diff options
context:
space:
mode:
Diffstat (limited to 'NK_C_API.cc')
-rw-r--r--NK_C_API.cc147
1 files changed, 112 insertions, 35 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc
index ff612f0..7d0a10e 100644
--- a/NK_C_API.cc
+++ b/NK_C_API.cc
@@ -21,10 +21,13 @@
#include "NK_C_API.h"
#include <iostream>
+#include <tuple>
#include "libnitrokey/NitrokeyManager.h"
#include <cstring>
#include "libnitrokey/LibraryException.h"
#include "libnitrokey/cxx_semantics.h"
+#include "libnitrokey/stick20_commands.h"
+#include "version.h"
#ifdef _MSC_VER
#ifdef _WIN32
@@ -52,11 +55,11 @@ T* duplicate_vector_and_clear(std::vector<T> &v){
return d;
}
-template <typename T>
-uint8_t * get_with_array_result(T func){
+template <typename R, typename T>
+std::tuple<int, R> get_with_status(T func, R fallback) {
NK_last_command_status = 0;
try {
- return func();
+ return std::make_tuple(0, func());
}
catch (CommandFailedException & commandFailedException){
NK_last_command_status = commandFailedException.last_command_status;
@@ -67,43 +70,26 @@ uint8_t * get_with_array_result(T func){
catch (const DeviceCommunicationException &deviceException){
NK_last_command_status = 256-deviceException.getType();
}
- return nullptr;
+ return std::make_tuple(NK_last_command_status, fallback);
+}
+
+template <typename T>
+uint8_t * get_with_array_result(T func){
+ return std::get<1>(get_with_status<uint8_t*>(func, nullptr));
}
template <typename T>
char* get_with_string_result(T func){
- NK_last_command_status = 0;
- try {
- return func();
+ auto result = std::get<1>(get_with_status<char*>(func, nullptr));
+ if (result == nullptr) {
+ return strndup("", MAXIMUM_STR_REPLY_LENGTH);
}
- catch (CommandFailedException & commandFailedException){
- NK_last_command_status = commandFailedException.last_command_status;
- }
- catch (LibraryException & libraryException){
- NK_last_command_status = libraryException.exception_id();
- }
- catch (const DeviceCommunicationException &deviceException){
- NK_last_command_status = 256-deviceException.getType();
- }
- return strndup("", MAXIMUM_STR_REPLY_LENGTH);
+ return result;
}
template <typename T>
auto get_with_result(T func){
- NK_last_command_status = 0;
- try {
- return func();
- }
- catch (CommandFailedException & commandFailedException){
- NK_last_command_status = commandFailedException.last_command_status;
- }
- catch (LibraryException & libraryException){
- NK_last_command_status = libraryException.exception_id();
- }
- catch (const DeviceCommunicationException &deviceException){
- NK_last_command_status = 256-deviceException.getType();
- }
- return static_cast<decltype(func())>(0);
+ return std::get<1>(get_with_status(func, static_cast<decltype(func())>(0)));
}
template <typename T>
@@ -373,6 +359,18 @@ extern "C" {
m->set_loglevel(level);
}
+ NK_C_API unsigned int NK_get_major_library_version() {
+ return get_major_library_version();
+ }
+
+ NK_C_API unsigned int NK_get_minor_library_version() {
+ return get_minor_library_version();
+ }
+
+ NK_C_API const char* NK_get_library_version() {
+ return get_library_version();
+ }
+
NK_C_API int NK_totp_set_time(uint64_t time) {
auto m = NitrokeyManager::instance();
return get_without_result([&]() {
@@ -380,11 +378,15 @@ extern "C" {
});
}
- NK_C_API int NK_totp_get_time() {
+ NK_C_API int NK_totp_set_time_soft(uint64_t time) {
auto m = NitrokeyManager::instance();
return get_without_result([&]() {
- m->get_time(0); // FIXME check how that should work
+ 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) {
@@ -616,7 +618,77 @@ extern "C" {
});
}
- NK_C_API char* NK_get_SD_usage_data_as_string() {
+ NK_C_API int NK_get_status_storage(NK_storage_status* out) {
+ if (out == nullptr) {
+ return -1;
+ }
+ auto m = NitrokeyManager::instance();
+ auto result = get_with_status([&]() {
+ return m->get_status_storage();
+ }, proto::stick20::DeviceConfigurationResponsePacket::ResponsePayload());
+ auto error_code = std::get<0>(result);
+ if (error_code != 0) {
+ return error_code;
+ }
+
+ auto status = std::get<1>(result);
+ out->unencrypted_volume_read_only = status.ReadWriteFlagUncryptedVolume_u8 != 0;
+ out->unencrypted_volume_active = status.VolumeActiceFlag_st.unencrypted;
+ out->encrypted_volume_read_only = status.ReadWriteFlagCryptedVolume_u8 != 0;
+ out->encrypted_volume_active = status.VolumeActiceFlag_st.encrypted;
+ out->hidden_volume_read_only = status.ReadWriteFlagHiddenVolume_u8 != 0;
+ out->hidden_volume_active = status.VolumeActiceFlag_st.hidden;
+ out->firmware_version_major = status.versionInfo.major;
+ out->firmware_version_minor = status.versionInfo.minor;
+ out->firmware_locked = status.FirmwareLocked_u8 != 0;
+ out->serial_number_sd_card = status.ActiveSD_CardID_u32;
+ out->serial_number_smart_card = status.ActiveSmartCardID_u32;
+ out->user_retry_count = status.UserPwRetryCount;
+ out->admin_retry_count = status.AdminPwRetryCount;
+ out->new_sd_card_found = status.NewSDCardFound_st.NewCard;
+ out->filled_with_random = (status.SDFillWithRandomChars_u8 & 0x01) != 0;
+ out->stick_initialized = status.StickKeysNotInitiated == 0;
+ return 0;
+ }
+
+ NK_C_API int NK_get_storage_production_info(NK_storage_ProductionTest * out){
+ if (out == nullptr) {
+ return -1;
+ }
+ auto m = NitrokeyManager::instance();
+ auto result = get_with_status([&]() {
+ return m->production_info();
+ }, proto::stick20::ProductionTest::ResponsePayload());
+
+ auto error_code = std::get<0>(result);
+ if (error_code != 0) {
+ return error_code;
+ }
+
+ stick20::ProductionTest::ResponsePayload status = std::get<1>(result);
+ // Cannot use memcpy without declaring C API struct packed
+ // (which is not parsed by Python's CFFI apparently), hence the manual way.
+#define a(x) out->x = status.x;
+ a(FirmwareVersion_au8[0]);
+ a(FirmwareVersion_au8[1]);
+ a(FirmwareVersionInternal_u8);
+ a(SD_Card_Size_u8);
+ a(CPU_CardID_u32);
+ a(SmartCardID_u32);
+ a(SD_CardID_u32);
+ a(SC_UserPwRetryCount);
+ a(SC_AdminPwRetryCount);
+ a(SD_Card_ManufacturingYear_u8);
+ a(SD_Card_ManufacturingMonth_u8);
+ a(SD_Card_OEM_u16);
+ a(SD_WriteSpeed_u16);
+ a(SD_Card_Manufacturer_u8);
+#undef a
+ return 0;
+ }
+
+
+NK_C_API char* NK_get_SD_usage_data_as_string() {
auto m = NitrokeyManager::instance();
return get_with_string_result([&]() {
return m->get_SD_usage_data_as_string();
@@ -671,7 +743,12 @@ extern "C" {
});
}
-
+ NK_C_API int NK_wink() {
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&]() {
+ return m->wink();
+ });
+ }
#ifdef __cplusplus
}