aboutsummaryrefslogtreecommitdiff
path: root/NK_C_API_otp.cpp
blob: cc44ffc31773e36ef986978140d0716cbcc3d989 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
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