-
Notifications
You must be signed in to change notification settings - Fork 81
/
ykpers-json.c
329 lines (294 loc) · 9.95 KB
/
ykpers-json.c
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/* -*- mode:C; c-file-style: "bsd" -*- */
/*
* Copyright (c) 2013 Yubico AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ykpers_lcl.h"
#include "ykpers-json.h"
#include <yubikey.h>
#include <json.h>
#include <string.h>
#ifdef HAVE_JSON_OBJECT_OBJECT_GET_EX
#define yk_json_object_object_get(obj, key, value) json_object_object_get_ex(obj, key, &value)
#else
typedef int json_bool;
#define yk_json_object_object_get(obj, key, value) (value = json_object_object_get(obj, key)) == NULL ? 0 : 1
#endif
static void set_json_value(struct map_st *p, int mode, json_object *options, YKP_CONFIG *cfg) {
if(!p->json_text) {
return;
}
if(p->mode && (mode & p->mode) == mode) {
json_object *joption;
json_bool ret = yk_json_object_object_get(options, p->json_text, joption);
if(ret == 1 && json_object_get_type(joption) == json_type_boolean) {
int value = json_object_get_boolean(joption);
if(value == 1) {
p->setter(cfg, true);
}
}
}
}
int _ykp_json_export_cfg(const YKP_CONFIG *cfg, char *json, size_t len) {
json_object *jobj = json_object_new_object();
json_object *yprod_json = json_object_new_object();
json_object *options_json = json_object_new_object();
if(cfg) {
YK_CONFIG ycfg = cfg->ykcore_config;
int mode = MODE_OTP_YUBICO;
struct map_st *p;
json_object *target_config = NULL;
json_object *prot_obj = NULL;
int protection = ykp_get_acccode_type(cfg);
if((ycfg.tktFlags & TKTFLAG_OATH_HOTP) == TKTFLAG_OATH_HOTP){
if((ycfg.cfgFlags & CFGFLAG_CHAL_HMAC) == CFGFLAG_CHAL_HMAC) {
mode = MODE_CHAL_HMAC;
} else if((ycfg.cfgFlags & CFGFLAG_CHAL_YUBICO) == CFGFLAG_CHAL_YUBICO) {
mode = MODE_CHAL_YUBICO;
} else {
mode = MODE_OATH_HOTP;
}
}
else if((ycfg.cfgFlags & CFGFLAG_STATIC_TICKET) == CFGFLAG_STATIC_TICKET) {
mode = MODE_STATIC_TICKET;
}
for(p = _modes_map; p->flag; p++) {
if(p->flag == mode) {
json_object *jmode = json_object_new_string(p->json_text);
json_object_object_add(yprod_json, "mode", jmode);
break;
}
}
if(cfg->command == SLOT_CONFIG) {
target_config = json_object_new_int(1);
} else if(cfg->command == SLOT_CONFIG2) {
target_config = json_object_new_int(2);
}
if(target_config) {
json_object_object_add(yprod_json, "targetConfig", target_config);
}
if(protection == YKP_ACCCODE_NONE) {
prot_obj = json_object_new_string("none");
} else if(protection == YKP_ACCCODE_RANDOM) {
prot_obj = json_object_new_string("random");
} else if(protection == YKP_ACCCODE_SERIAL) {
prot_obj = json_object_new_string("id");
}
if(prot_obj) {
json_object_object_add(yprod_json, "protection", prot_obj);
}
json_object_object_add(jobj, "yubiProdConfig", yprod_json);
json_object_object_add(yprod_json, "options", options_json);
if(ycfg.fixedSize != 0 && mode != MODE_STATIC_TICKET) {
json_object *jPrefix;
char prefix[5] = {0};
json_object *scope;
if(mode == MODE_OTP_YUBICO &&
ycfg.fixed[0] == 0x00 && ycfg.fixed[1] == 0x00) {
scope = json_object_new_string("yubiCloud");
} else {
scope = json_object_new_string("privatePrefix");
}
json_object_object_add(yprod_json, "scope", scope);
yubikey_modhex_encode(prefix, (const char*)ycfg.fixed, 2);
if(mode == MODE_OATH_HOTP) {
int flag = ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX;
json_object *fixed_modhex = json_object_new_boolean(
flag == CFGFLAG_OATH_FIXED_MODHEX ? 1 : 0);
json_object_object_add(options_json, "fixedModhex", fixed_modhex);
if(flag == 0) {
yubikey_hex_encode(prefix, (const char*)ycfg.fixed, 2);
} else if(flag == CFGFLAG_OATH_FIXED_MODHEX1) {
yubikey_hex_encode(prefix + 2, (const char*)ycfg.fixed + 1, 1);
}
}
jPrefix = json_object_new_string(prefix);
json_object_object_add(yprod_json, "prefix", jPrefix);
} else if(mode != MODE_STATIC_TICKET) {
json_object *scope = json_object_new_string("noPublicId");
json_object_object_add(yprod_json, "scope", scope);
}
if(mode == MODE_OATH_HOTP) {
json_object *oathDigits;
json_object *randomSeed;
if((ycfg.cfgFlags & CFGFLAG_OATH_HOTP8) == CFGFLAG_OATH_HOTP8) {
oathDigits = json_object_new_int(8);
} else {
oathDigits = json_object_new_int(6);
}
json_object_object_add(options_json, "oathDigits", oathDigits);
if((ycfg.uid[5] == 0x01 || ycfg.uid[5] == 0x00) && ycfg.uid[4] == 0x00) {
json_object *fixedSeedvalue = json_object_new_int(ycfg.uid[5] << 4);
json_object_object_add(options_json, "fixedSeedvalue", fixedSeedvalue);
randomSeed = json_object_new_boolean(0);
} else {
randomSeed = json_object_new_boolean(1);
}
json_object_object_add(options_json, "randomSeed", randomSeed);
}
for(p = _ticket_flags_map; p->flag; p++) {
if(!p->json_text) {
continue;
}
if(p->mode && (mode & p->mode) == mode) {
int set = (ycfg.tktFlags & p->flag) == p->flag;
json_object *jsetting = json_object_new_boolean(set);
json_object_object_add(options_json, p->json_text, jsetting);
}
}
for(p = _config_flags_map; p->flag; p++) {
if(!p->json_text) {
continue;
}
if(p->mode && (mode & p->mode) == mode) {
int set = (ycfg.cfgFlags & p->flag) == p->flag;
json_object *jsetting = json_object_new_boolean(set);
json_object_object_add(options_json, p->json_text, jsetting);
}
}
for(p = _extended_flags_map; p->flag; p++) {
if(!p->json_text) {
continue;
}
if(p->mode && (mode & p->mode) == mode) {
int set = (ycfg.extFlags & p->flag) == p->flag;
json_object *jsetting = json_object_new_boolean(set);
json_object_object_add(options_json, p->json_text, jsetting);
}
}
}
#ifdef HAVE_JSON_OBJECT_TO_JSON_STRING_EXT
strncpy(json, json_object_to_json_string_ext(jobj, JSON_C_TO_STRING_PRETTY), len);
#else
strncpy(json, json_object_to_json_string(jobj), len);
#endif
if (len > 0) {
json[len - 1] = '\0';
}
/* free the root object, will free all children */
json_object_put(jobj);
return strlen(json);
}
int _ykp_json_import_cfg(YKP_CONFIG *cfg, const char *json, size_t len) {
int ret_code = 0;
if(cfg) {
json_object *jobj = json_tokener_parse(json);
json_object *yprod_json, *jmode, *options, *jtarget;
const char *raw_mode;
int mode = MODE_OTP_YUBICO;
struct map_st *p;
if(!jobj) {
ykp_errno = YKP_EINVAL;
goto out;
}
if(yk_json_object_object_get(jobj, "yubiProdConfig", yprod_json) == 0) {
ykp_errno = YKP_EINVAL;
goto out;
}
if(yk_json_object_object_get(yprod_json, "mode", jmode) == 0) {
ykp_errno = YKP_EINVAL;
goto out;
}
if(yk_json_object_object_get(yprod_json, "options", options) == 0) {
ykp_errno = YKP_EINVAL;
goto out;
}
if(yk_json_object_object_get(yprod_json, "targetConfig", jtarget) == 1) {
int target_config = json_object_get_int(jtarget);
int command;
if(target_config == 1) {
command = SLOT_CONFIG;
} else if(target_config == 2) {
command = SLOT_CONFIG2;
} else {
ykp_errno = YKP_EINVAL;
goto out;
}
if(ykp_command(cfg) == 0) {
ykp_configure_command(cfg, command);
} else if(ykp_command(cfg) != command) {
ykp_errno = YKP_EINVAL;
goto out;
}
}
raw_mode = json_object_get_string(jmode);
for(p = _modes_map; p->flag; p++) {
if(strcmp(raw_mode, p->json_text) == 0) {
mode = p->flag;
break;
}
}
if(mode == MODE_OATH_HOTP) {
json_object *jdigits, *jrandom;
ykp_set_tktflag_OATH_HOTP(cfg, true);
if(yk_json_object_object_get(options, "oathDigits", jdigits) == 1) {
int digits = json_object_get_int(jdigits);
if(digits == 8) {
ykp_set_cfgflag_OATH_HOTP8(cfg, true);
}
}
if(yk_json_object_object_get(options, "randomSeed", jrandom) == 1) {
int random = json_object_get_boolean(jrandom);
int seed = 0;
if(random == 1) {
/* NOTE: random seed isn't implemented here for now. */
ykp_errno = YKP_ENORANDOM;
goto out;
} else {
json_object *jseed;
if(yk_json_object_object_get(options, "fixedSeedvalue", jseed) == 1) {
seed = json_object_get_int(jseed);
}
}
ykp_set_oath_imf(cfg, (long unsigned int)seed);
}
} else if(mode == MODE_CHAL_HMAC) {
ykp_set_tktflag_CHAL_RESP(cfg, true);
ykp_set_cfgflag_CHAL_HMAC(cfg, true);
} else if(mode == MODE_CHAL_YUBICO) {
ykp_set_tktflag_CHAL_RESP(cfg, true);
ykp_set_cfgflag_CHAL_YUBICO(cfg, true);
} else if(mode == MODE_STATIC_TICKET) {
ykp_set_cfgflag_STATIC_TICKET(cfg, true);
}
for(p = _ticket_flags_map; p->flag; p++) {
set_json_value(p, mode, options, cfg);
}
for(p = _config_flags_map; p->flag; p++) {
set_json_value(p, mode, options, cfg);
}
for(p = _extended_flags_map; p->flag; p++) {
set_json_value(p, mode, options, cfg);
}
ret_code = 1;
out:
if(jobj) {
json_object_put(jobj);
}
}
return ret_code;
}