Skip to content

Commit db324e0

Browse files
committed
Add getters for OTP and PWS properties
This patch adds getters for the number of PWS, TOTP and HOTP slots on the current device and the maximum length of a PWS slot name, login and password as well as a OTP slot name and secret. Fixes #197.
1 parent 3e0b949 commit db324e0

File tree

6 files changed

+222
-1
lines changed

6 files changed

+222
-1
lines changed

NK_C_API.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,38 @@ extern "C" {
336336
});
337337
}
338338

339+
NK_C_API uint8_t NK_get_pws_slot_count() {
340+
return NitrokeyManager::instance()->get_pws_slot_count();
341+
}
342+
343+
NK_C_API size_t NK_get_pws_name_length() {
344+
return NitrokeyManager::instance()->get_pws_name_length();
345+
}
346+
347+
NK_C_API size_t NK_get_pws_login_length() {
348+
return NitrokeyManager::instance()->get_pws_login_length();
349+
}
350+
351+
NK_C_API size_t NK_get_pws_password_length() {
352+
return NitrokeyManager::instance()->get_pws_password_length();
353+
}
354+
355+
NK_C_API uint8_t NK_get_totp_slot_count() {
356+
return NitrokeyManager::instance()->get_totp_slot_count();
357+
}
358+
359+
NK_C_API uint8_t NK_get_hotp_slot_count() {
360+
return NitrokeyManager::instance()->get_hotp_slot_count();
361+
}
362+
363+
NK_C_API size_t NK_get_otp_name_length() {
364+
return NitrokeyManager::instance()->get_otp_name_length();
365+
}
366+
367+
NK_C_API size_t NK_get_otp_secret_length() {
368+
return NitrokeyManager::instance()->get_otp_secret_length();
369+
}
370+
339371
NK_C_API char * NK_get_hotp_code(uint8_t slot_number) {
340372
return NK_get_hotp_code_PIN(slot_number, "");
341373
}

NK_C_API.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#define LIBNITROKEY_NK_C_API_H
2424

2525
#include <stdbool.h>
26+
#include <stddef.h>
2627
#include <stdint.h>
2728

2829
#include "deprecated.h"
@@ -527,6 +528,56 @@ extern "C" {
527528
*/
528529
NK_C_API int NK_read_config_struct(struct NK_config* out);
529530

531+
// OTP and PWS properties
532+
533+
/**
534+
* Returns the number of PWS slots provided by the connected device or
535+
* zero if no device is connected.
536+
*/
537+
NK_C_API uint8_t NK_get_pws_slot_count();
538+
539+
/**
540+
* Returns the maximum length of a PWS slot name in bytes for the
541+
* connected device or zero if no device is connected.
542+
*/
543+
NK_C_API size_t NK_get_pws_name_length();
544+
545+
/**
546+
* Returns the maximum length of a PWS login in bytes for the connected
547+
* device or zero if no device is connected.
548+
*/
549+
NK_C_API size_t NK_get_pws_login_length();
550+
551+
/**
552+
* Returns the maximum length of a PWS password in bytes for the
553+
* connected device or zero if no device is connected.
554+
*/
555+
NK_C_API size_t NK_get_pws_password_length();
556+
557+
/**
558+
* Returns the number of TOTP slots provided by the connected device or
559+
* zero if no device is connected.
560+
*/
561+
NK_C_API uint8_t NK_get_totp_slot_count();
562+
563+
/**
564+
* Returns the number of HOTP slots provided by the connected device or
565+
* zero if no device is connected.
566+
*/
567+
NK_C_API uint8_t NK_get_hotp_slot_count();
568+
569+
/**
570+
* Returns the maximum length of an OTP slot name in bytes for the
571+
* connected device or zero if no device is connected.
572+
*/
573+
NK_C_API size_t NK_get_otp_name_length();
574+
575+
/**
576+
* Returns the maximum length of an OTP secret in bytes for the
577+
* connected device or zero if no device is connected.
578+
*/
579+
NK_C_API size_t NK_get_otp_secret_length();
580+
530581
//OTP
531582

532583
/**

NitrokeyManager.cc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,69 @@ using nitrokey::misc::strcpyT;
390390
}
391391
}
392392

393+
uint8_t NitrokeyManager::get_pws_slot_count() {
394+
if (device == nullptr) {
395+
return 0;
396+
} else {
397+
return PWS_SLOT_COUNT;
398+
}
399+
}
400+
401+
size_t NitrokeyManager::get_pws_name_length() {
402+
if (device == nullptr) {
403+
return 0;
404+
} else {
405+
return PWS_SLOTNAME_LENGTH;
406+
}
407+
}
408+
409+
size_t NitrokeyManager::get_pws_login_length() {
410+
if (device == nullptr) {
411+
return 0;
412+
} else {
413+
return PWS_LOGINNAME_LENGTH;
414+
}
415+
}
416+
417+
size_t NitrokeyManager::get_pws_password_length() {
418+
if (device == nullptr) {
419+
return 0;
420+
} else {
421+
return PWS_PASSWORD_LENGTH;
422+
}
423+
}
424+
425+
uint8_t NitrokeyManager::get_totp_slot_count() {
426+
if (device == nullptr) {
427+
return 0;
428+
} else {
429+
return TOTP_SLOT_COUNT;
430+
}
431+
}
432+
433+
uint8_t NitrokeyManager::get_hotp_slot_count() {
434+
if (device == nullptr) {
435+
return 0;
436+
} else {
437+
return HOTP_SLOT_COUNT;
438+
}
439+
}
440+
441+
size_t NitrokeyManager::get_otp_name_length() {
442+
if (device == nullptr) {
443+
return 0;
444+
} else {
445+
return OTP_SLOTNAME_LENGTH;
446+
}
447+
}
448+
449+
size_t NitrokeyManager::get_otp_secret_length() {
450+
if (device == nullptr) {
451+
return 0;
452+
} else {
453+
return OTP_SECRET_LENGTH;
454+
}
455+
}
393456

394457
string NitrokeyManager::get_serial_number() {
395458
try {

libnitrokey/NitrokeyManager.h

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "stick10_commands.h"
2929
#include "stick10_commands_0.8.h"
3030
#include "stick20_commands.h"
31+
#include <cstddef>
3132
#include <vector>
3233
#include <memory>
3334
#include <unordered_map>
@@ -64,6 +65,54 @@ char * strndup(const char* str, size_t maxlen);
6465
stick10::ReadSlot::ResponsePayload get_TOTP_slot_data(const uint8_t slot_number);
6566
stick10::ReadSlot::ResponsePayload get_HOTP_slot_data(const uint8_t slot_number);
6667

68+
/**
69+
* Returns the number of PWS slots provided by the connected device or
70+
* zero if no device is connected.
71+
*/
72+
uint8_t get_pws_slot_count();
73+
74+
/**
75+
* Returns the maximum length of a PWS slot name in bytes for the
76+
* connected device or zero if no device is connected.
77+
*/
78+
size_t get_pws_name_length();
79+
80+
/**
81+
* Returns the maximum length of a PWS login in bytes for the connected
82+
* device or zero if no device is connected.
83+
*/
84+
size_t get_pws_login_length();
85+
86+
/**
87+
* Returns the maximum length of a PWS password in bytes for the
88+
* connected device or zero if no device is connected.
89+
*/
90+
size_t get_pws_password_length();
91+
92+
/**
93+
* Returns the number of TOTP slots provided by the connected device or
94+
* zero if no device is connected.
95+
*/
96+
uint8_t get_totp_slot_count();
97+
98+
/**
99+
* Returns the number of HOTP slots provided by the connected device or
100+
* zero if no device is connected.
101+
*/
102+
uint8_t get_hotp_slot_count();
103+
104+
/**
105+
* Returns the maximum length of an OTP slot name in bytes for the
106+
* connected device or zero if no device is connected.
107+
*/
108+
size_t get_otp_name_length();
109+
110+
/**
111+
* Returns the maximum length of an OTP secret in bytes for the
112+
* connected device or zero if no device is connected.
113+
*/
114+
size_t get_otp_secret_length();
115+
67116
bool set_time(uint64_t time);
68117
/**
69118
* Set the device time used for TOTP to the given time. Contrary to

unittest/test_offline.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,14 @@ def test_offline(C_offline):
3636

3737
# v3.4.1-29-g1f3d
3838
search = re.search(b'v\d\.\d(\.\d)?', libnk_version)
39-
assert search is not None
39+
assert search is not None
40+
41+
assert C_offline.NK_get_pws_slot_count() == 0
42+
assert C_offline.NK_get_pws_name_length() == 0
43+
assert C_offline.NK_get_pws_login_length() == 0
44+
assert C_offline.NK_get_pws_password_length() == 0
45+
46+
assert C_offline.NK_get_hotp_slot_count() == 0
47+
assert C_offline.NK_get_totp_slot_count() == 0
48+
assert C_offline.NK_get_otp_name_length() == 0
49+
assert C_offline.NK_get_otp_secret_length() == 0

unittest/test_pro.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def test_password_safe_slot_status(C):
132132
assert is_slot_programmed[1] == 1
133133

134134

135+
@pytest.mark.PWS
136+
def test_password_safe_properties(C):
137+
assert C.NK_get_pws_slot_count() == 16
138+
assert C.NK_get_pws_name_length() == 11
139+
assert C.NK_get_pws_login_length() == 32
140+
assert C.NK_get_pws_password_length() == 20
141+
142+
135143
@pytest.mark.aes
136144
def test_issue_device_locks_on_second_key_generation_in_sequence(C):
137145
# if is_pro_rtm_07(C) or is_pro_rtm_08(C):
@@ -1070,3 +1078,11 @@ def test_OTP_all_rw(C):
10701078
all_codes.append(this_loop_codes)
10711079
from pprint import pprint
10721080
pprint(all_codes)
1081+
1082+
1083+
@pytest.mark.otp
1084+
def test_otp_properties(C):
1085+
assert C.NK_get_hotp_slot_count() == 3
1086+
assert C.NK_get_totp_slot_count() == 15
1087+
assert C.NK_get_otp_name_length() == 15
1088+
assert C.NK_get_otp_secret_length() == 40

0 commit comments

Comments
 (0)