aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--NK_C_API.cc21
-rw-r--r--NK_C_API.h1
-rw-r--r--NitrokeyManager.cc4
-rw-r--r--include/NitrokeyManager.h2
-rw-r--r--unittest/test_bindings.py2
5 files changed, 28 insertions, 2 deletions
diff --git a/NK_C_API.cc b/NK_C_API.cc
index 7de5bbd..c2e7df0 100644
--- a/NK_C_API.cc
+++ b/NK_C_API.cc
@@ -1,6 +1,6 @@
#include <cstring>
#include "NK_C_API.h"
-
+#include <functional>
using namespace nitrokey;
static uint8_t NK_last_command_status = 0;
@@ -16,6 +16,18 @@ auto get_with_result(T func){
}
}
+template <typename T>
+uint8_t get_without_result(T func){
+ try {
+ func();
+ return 0;
+ }
+ catch (CommandFailedException & commandFailedException){
+ NK_last_command_status = commandFailedException.last_command_status;
+ return commandFailedException.last_command_status;
+ }
+}
+
extern "C"
{
extern uint8_t NK_get_last_command_status(){
@@ -245,6 +257,13 @@ extern uint8_t NK_get_admin_retry_count(){
});
}
+extern int NK_lock_device(){
+ auto m = NitrokeyManager::instance();
+ return get_without_result([&](){
+ return m->lock_device();
+ });
+}
+
}
diff --git a/NK_C_API.h b/NK_C_API.h
index 75702cc..dbbacae 100644
--- a/NK_C_API.h
+++ b/NK_C_API.h
@@ -29,6 +29,7 @@ extern uint8_t NK_get_user_retry_count();
extern uint8_t NK_get_admin_retry_count();
extern int NK_enable_password_safe(const char *user_pin);
extern int NK_get_password_safe_slot_status();
+extern int NK_lock_device();
}
diff --git a/NitrokeyManager.cc b/NitrokeyManager.cc
index d684853..15f09b8 100644
--- a/NitrokeyManager.cc
+++ b/NitrokeyManager.cc
@@ -232,5 +232,9 @@ namespace nitrokey{
return response.password_retry_count;
}
+ void NitrokeyManager::lock_device() {
+ LockDevice::CommandTransaction::run(*device);
+ }
+
} \ No newline at end of file
diff --git a/include/NitrokeyManager.h b/include/NitrokeyManager.h
index 6588711..ecc397d 100644
--- a/include/NitrokeyManager.h
+++ b/include/NitrokeyManager.h
@@ -47,6 +47,8 @@ namespace nitrokey {
uint8_t get_admin_retry_count();
uint8_t get_user_retry_count();
+ void lock_device();
+
private:
NitrokeyManager();
~NitrokeyManager();
diff --git a/unittest/test_bindings.py b/unittest/test_bindings.py
index 51e1233..ec7608e 100644
--- a/unittest/test_bindings.py
+++ b/unittest/test_bindings.py
@@ -50,7 +50,7 @@ def C(request):
def test_enable_password_safe(C):
- # lock device
+ assert C.NK_lock_device() == DeviceErrorCode.STATUS_OK
assert C.NK_enable_password_safe('wrong_password') == DeviceErrorCode.WRONG_PASSWORD
assert C.NK_enable_password_safe(DefaultPasswords.USER) == DeviceErrorCode.STATUS_OK