forked from Yubico/yubikey-personalization
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ykpers.c
1237 lines (1117 loc) · 31.1 KB
/
ykpers.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode:C; c-file-style: "bsd" -*- */
/*
* Copyright (c) 2008-2014 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 "ykpbkdf2.h"
#include "yktsd.h"
#include "ykpers-json.h"
#include <ykpers.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <yubikey.h>
static const YK_CONFIG default_config1 = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* fixed */
{ 0, 0, 0, 0, 0, 0 }, /* uid */
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* key */
{ 0, 0, 0, 0, 0, 0 }, /* accCode */
0, /* fixedSize */
0, /* extFlags */
TKTFLAG_APPEND_CR, /* tktFlags */
0, /* cfgFlags */
{0}, /* ctrOffs */
0 /* crc */
};
static const YK_CONFIG default_config2 = {
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* fixed */
{ 0, 0, 0, 0, 0, 0 }, /* uid */
{ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, /* key */
{ 0, 0, 0, 0, 0, 0 }, /* accCode */
0, /* fixedSize */
0, /* extFlags */
TKTFLAG_APPEND_CR, /* tktFlags */
/* cfgFlags */
CFGFLAG_STATIC_TICKET | CFGFLAG_STRONG_PW1 | CFGFLAG_STRONG_PW2 | CFGFLAG_MAN_UPDATE,
{0}, /* ctrOffs */
0 /* crc */
};
/* From nfcforum-ts-rtd-uri-1.0.pdf */
static const char *ndef_identifiers[] = {
"http://www.",
"https://www.",
"http://",
"https://",
"tel:",
"mailto:",
"ftp://anonymous:anonymous@",
"ftp://ftp.",
"ftps://",
"sftp://",
"smb://",
"nfs://",
"ftp://",
"dav://",
"news:",
"telnet://",
"imap:",
"rtsp://",
"urn:",
"pop:",
"sip:",
"sips:",
"tftp:",
"btspp://",
"btl2cap://",
"btgoep://",
"tcpobex://",
"irdaobex://",
"file://",
"urn:epc:id:",
"urn:epc:tag:",
"urn:epc:pat:",
"urn:epc:raw:",
"urn:epc:",
"urn:nfc:"
};
YKP_CONFIG *ykp_create_config(void)
{
YKP_CONFIG *cfg = malloc(sizeof(YKP_CONFIG));
if (cfg) {
memcpy(&cfg->ykcore_config, &default_config1,
sizeof(default_config1));
cfg->yk_major_version = 1;
cfg->yk_minor_version = 3;
cfg->yk_build_version = 0;
cfg->command = SLOT_CONFIG;
return cfg;
}
return 0;
}
YKP_CONFIG *ykp_alloc(void)
{
YKP_CONFIG *cfg = malloc(sizeof(YKP_CONFIG));
if(cfg) {
memset(cfg, 0, sizeof(YKP_CONFIG));
return cfg;
}
return 0;
}
int ykp_free_config(YKP_CONFIG *cfg)
{
if (cfg) {
free(cfg);
return 1;
}
return 0;
}
int ykp_clear_config(YKP_CONFIG *cfg)
{
if(cfg) {
cfg->ykcore_config.tktFlags = 0;
cfg->ykcore_config.cfgFlags = 0;
cfg->ykcore_config.extFlags = 0;
return 1;
}
return 0;
}
void ykp_configure_version(YKP_CONFIG *cfg, YK_STATUS *st)
{
cfg->yk_major_version = st->versionMajor;
cfg->yk_minor_version = st->versionMinor;
cfg->yk_build_version = st->versionBuild;
}
int ykp_configure_command(YKP_CONFIG *cfg, uint8_t command)
{
switch(command) {
case SLOT_CONFIG:
break;
case SLOT_CONFIG2:
if (!(cfg->yk_major_version >= 2)) {
ykp_errno = YKP_EOLDYUBIKEY;
return 0;
}
/* The NEO Beta key is versioned from 2.1.4 but doesn't support slot2 */
else if( cfg->yk_major_version == 2 && cfg->yk_minor_version == 1 &&
cfg->yk_build_version >= 4) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
}
break;
case SLOT_UPDATE1:
case SLOT_UPDATE2:
case SLOT_SWAP:
if (!((cfg->yk_major_version == 2 && cfg->yk_minor_version >= 3)
|| cfg->yk_major_version > 2)) {
ykp_errno = YKP_EOLDYUBIKEY;
return 0;
}
break;
case SLOT_DEVICE_CONFIG:
if(!(cfg->yk_major_version <= 5)) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
} /* we have an intentional fall-through to the next case here */
case SLOT_SCAN_MAP:
if(!(cfg->yk_major_version >= 3)) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
}
break;
case SLOT_YK4_SET_DEVICE_INFO:
if(!(cfg->yk_major_version >= 5)) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
}
break;
case SLOT_NDEF2:
if(cfg->yk_major_version != 3 && cfg->yk_major_version != 5) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
}
break;
case SLOT_NDEF:
/* NDEF is available for neo, thus within 2.1 from build 4 */
if (!((cfg->yk_major_version == 2 && cfg->yk_minor_version == 1 &&
cfg->yk_build_version >= 4) || cfg->yk_major_version == 3 || cfg->yk_major_version >= 5)) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
}
break;
default:
ykp_errno = YKP_EINVCONFNUM;
return 0;
}
cfg->command = command;
return 1;
}
int ykp_configure_for(YKP_CONFIG *cfg, int confnum, YK_STATUS *st)
{
ykp_configure_version(cfg, st);
switch(confnum) {
case 1:
memcpy(&cfg->ykcore_config, &default_config1,
sizeof(default_config1));
return ykp_configure_command(cfg, SLOT_CONFIG);
case 2:
memcpy(&cfg->ykcore_config, &default_config2,
sizeof(default_config2));
return ykp_configure_command(cfg, SLOT_CONFIG2);
default:
ykp_errno = YKP_EINVCONFNUM;
return 0;
}
}
/* Return number of bytes of key data for this configuration.
* 20 bytes is 160 bits, 16 bytes is 128.
*/
int ykp_get_supported_key_length(const YKP_CONFIG *cfg)
{
/* OATH-HOTP and HMAC-SHA1 challenge response support 20 byte (160 bits)
* keys, holding the last four bytes in the uid field.
*/
if((ykp_get_tktflag_OATH_HOTP(cfg) &&
!ykp_get_cfgflag_CHAL_YUBICO(cfg)) ||
(ykp_get_tktflag_CHAL_RESP(cfg) &&
ykp_get_cfgflag_CHAL_HMAC(cfg))) {
return 20;
}
return 16;
}
/* Decode 128 bit AES key into cfg->ykcore_config.key */
int ykp_AES_key_from_hex(YKP_CONFIG *cfg, const char *hexkey) {
char aesbin[256];
/* Make sure that the hexkey is exactly 32 characters */
if (strlen(hexkey) != 32) {
ykp_errno = YKP_EINVAL;
return 1; /* Bad AES key */
}
/* Make sure that the hexkey is made up of only [0-9a-f] */
if (! yubikey_hex_p(hexkey)) {
ykp_errno = YKP_EINVAL;
return 1;
}
yubikey_hex_decode(aesbin, hexkey, sizeof(aesbin));
memcpy(cfg->ykcore_config.key, aesbin, sizeof(cfg->ykcore_config.key));
return 0;
}
/* Store a 16 byte AES key.
*
* copy 16 bytes from key to cfg->ykcore_config.key
*/
int ykp_AES_key_from_raw(YKP_CONFIG *cfg, const char *key) {
memcpy(cfg->ykcore_config.key, key, sizeof(cfg->ykcore_config.key));
return 0;
}
/* Store a 20 byte HMAC key.
*
* store the first 16 bytes of key in cfg->ykcore_config.key
* and the remaining 4 bytes in cfg->ykcore_config.uid
*/
int ykp_HMAC_key_from_raw(YKP_CONFIG *cfg, const char *key) {
size_t size = sizeof(cfg->ykcore_config.key);
memcpy(cfg->ykcore_config.key, key, size);
memcpy(cfg->ykcore_config.uid, key + size, 20 - size);
return 0;
}
/* Decode 160 bits HMAC key, used with OATH and HMAC challenge-response.
*
* The first 128 bits of the HMAC go key into cfg->ykcore_config.key,
* and 32 bits into the first four bytes of cfg->ykcore_config.uid.
*/
int ykp_HMAC_key_from_hex(YKP_CONFIG *cfg, const char *hexkey) {
char aesbin[256];
size_t i;
/* Make sure that the hexkey is exactly 40 characters */
if (strlen(hexkey) != 40) {
ykp_errno = YKP_EINVAL;
return 1; /* Bad HMAC key */
}
/* Make sure that the hexkey is made up of only [0-9a-f] */
if (! yubikey_hex_p(hexkey)) {
ykp_errno = YKP_EINVAL;
return 1;
}
yubikey_hex_decode(aesbin, hexkey, sizeof(aesbin));
i = sizeof(cfg->ykcore_config.key);
memcpy(cfg->ykcore_config.key, aesbin, i);
memcpy(cfg->ykcore_config.uid, aesbin + i, 20 - i);
return 0;
}
/* Generate an AES (128 bits) or HMAC (despite the function name) (160 bits)
* key from user entered input.
*
* Use user provided salt, or use salt from an available random device.
* If no random device is available we return with an error.
*/
int ykp_AES_key_from_passphrase(YKP_CONFIG *cfg, const char *passphrase,
const char *salt)
{
if (cfg) {
const char *random_places[] = {
"/dev/srandom",
"/dev/urandom",
"/dev/random",
0
};
const char **random_place;
uint8_t _salt[8];
size_t _salt_len = 0;
unsigned char buf[sizeof(cfg->ykcore_config.key) + 4];
int rc;
int key_bytes = ykp_get_supported_key_length(cfg);
YK_PRF_METHOD prf_method = {20, yk_hmac_sha1};
assert (key_bytes <= sizeof(buf));
if (salt) {
_salt_len = strlen(salt);
if (_salt_len > 8)
_salt_len = 8;
memcpy(_salt, salt, _salt_len);
} else {
for (random_place = random_places;
*random_place;
random_place++) {
FILE *random_file = fopen(*random_place, "r");
if (random_file) {
size_t read_bytes = 0;
while (read_bytes < sizeof(_salt)) {
size_t n = fread(&_salt[read_bytes],
1, sizeof (_salt) - read_bytes,
random_file);
read_bytes += n;
}
fclose(random_file);
_salt_len = sizeof(_salt);
break; /* from for loop */
}
}
}
if (_salt_len == 0) {
/* There was no randomness files, so don't do
* anything */
ykp_errno = YKP_ENORANDOM;
return 0;
}
rc = yk_pbkdf2(passphrase,
_salt, _salt_len,
1024,
buf, key_bytes,
&prf_method);
if (rc) {
memcpy(cfg->ykcore_config.key, buf, sizeof(cfg->ykcore_config.key));
if (key_bytes == 20) {
memcpy(cfg->ykcore_config.uid, buf + sizeof(cfg->ykcore_config.key), 4);
}
}
memset (buf, 0, sizeof(buf));
return rc;
}
return 0;
}
YK_NDEF *ykp_alloc_ndef(void)
{
YK_NDEF *ndef = malloc(sizeof(YK_NDEF));
if(ndef) {
memset(ndef, 0, sizeof(YK_NDEF));
return ndef;
}
return 0;
}
int ykp_free_ndef(YK_NDEF *ndef)
{
if(ndef)
{
free(ndef);
return 1;
}
return 0;
}
/* Fill in the data and len parts of the YK_NDEF struct based on supplied uri. */
int ykp_construct_ndef_uri(YK_NDEF *ndef, const char *uri)
{
int num_identifiers = sizeof(ndef_identifiers) / sizeof(char*);
size_t data_length;
int indx = 0;
for(; indx < num_identifiers; indx++) {
size_t len = strlen(ndef_identifiers[indx]);
if(strncmp(uri, ndef_identifiers[indx], len) == 0) {
uri += len;
break;
}
}
data_length = strlen(uri);
if(data_length + 1 > NDEF_DATA_SIZE) {
ykp_errno = YKP_EINVAL;
return 0;
}
if(indx == num_identifiers) {
ndef->data[0] = 0;
} else {
ndef->data[0] = indx + 1;
}
memcpy(ndef->data + 1, uri, data_length);
ndef->len = data_length + 1;
ndef->type = 'U';
return 1;
}
/* Fill in the data and len parts of the YK_NDEF struct based on supplied text. */
int ykp_construct_ndef_text(YK_NDEF *ndef, const char *text, const char *lang, bool isutf16)
{
size_t data_length = strlen(text);
size_t lang_length = strlen(lang);
unsigned char status = lang_length;
if(isutf16) {
status &= 0x80;
}
if((data_length + lang_length + 1) > NDEF_DATA_SIZE) {
ykp_errno = YKP_EINVAL;
return 0;
}
ndef->data[0] = status;
memcpy(ndef->data + 1, lang, lang_length);
memcpy(ndef->data + lang_length + 1, text, data_length);
ndef->len = data_length + lang_length + 1;
ndef->type = 'T';
return 1;
}
int ykp_ndef_as_text(YK_NDEF *ndef, char *text, size_t len)
{
if(ndef->type == 'U') {
const char *part = NULL;
size_t offset = 0;
if(ndef->data[0] > 0) {
part = ndef_identifiers[ndef->data[0] - 1];
offset = strlen(part);
}
if(offset + ndef->len - 1 > len) {
ykp_errno = YKP_EINVAL;
return 0;
}
if(part) {
memcpy(text, part, offset);
}
memcpy(text + offset, ndef->data + 1, ndef->len - 1);
text[ndef->len + offset] = 0;
return 1;
}
else if(ndef->type == 'T') {
unsigned char status = ndef->data[0];
if(status & 0x80)
status ^= 0x80;
if(ndef->len - status - 1 > len) {
ykp_errno = YKP_EINVAL;
return 0;
}
memcpy(text, ndef->data + status + 1, ndef->len - status - 1);
text[ndef->len - status] = 0;
return 1;
}
else {
return 0;
}
}
int ykp_set_ndef_access_code(YK_NDEF *ndef, unsigned char *access_code)
{
if(ndef)
{
memcpy(ndef->curAccCode, access_code, ACC_CODE_SIZE);
return 0;
}
return 1;
}
YK_DEVICE_CONFIG *ykp_alloc_device_config(void)
{
YK_DEVICE_CONFIG *cfg = malloc(sizeof(YK_DEVICE_CONFIG));
if(cfg) {
memset(cfg, 0, sizeof(YK_DEVICE_CONFIG));
return cfg;
}
return 0;
}
int ykp_free_device_config(YK_DEVICE_CONFIG *device_config)
{
if(device_config) {
free(device_config);
return 1;
}
return 0;
}
int ykp_set_device_mode(YK_DEVICE_CONFIG *device_config, unsigned char mode)
{
if(device_config) {
device_config->mode = mode;
return 1;
}
ykp_errno = YKP_EINVAL;
return 0;
}
int ykp_set_device_chalresp_timeout(YK_DEVICE_CONFIG *device_config, unsigned char timeout)
{
if(device_config) {
device_config->crTimeout = timeout;
return 1;
}
ykp_errno = YKP_EINVAL;
return 0;
}
int ykp_set_device_autoeject_time(YK_DEVICE_CONFIG *device_config, unsigned short eject_time)
{
if(device_config) {
device_config->autoEjectTime = eject_time;
return 1;
}
ykp_errno = YKP_EINVAL;
return 0;
}
static bool vcheck_all(const YKP_CONFIG *cfg)
{
return true;
}
static bool vcheck_v1(const YKP_CONFIG *cfg)
{
return cfg->yk_major_version == 1;
}
static bool vcheck_no_v1(const YKP_CONFIG *cfg)
{
return cfg->yk_major_version > 1;
}
static bool vcheck_v21_or_greater(const YKP_CONFIG *cfg)
{
/* the NEO Beta is versioned from 2.1.4 but shouldn't be identified as a 2.1 above key */
return (cfg->yk_major_version == 2 && cfg->yk_minor_version > 1) ||
(cfg->yk_major_version == 2 && cfg->yk_minor_version == 1 && cfg->yk_build_version <= 3)
|| cfg->yk_major_version > 2;
}
static bool vcheck_v22_or_greater(const YKP_CONFIG *cfg)
{
return (cfg->yk_major_version == 2 &&
cfg->yk_minor_version >= 2) ||
cfg->yk_major_version > 2;
}
static bool vcheck_v23_or_greater(const YKP_CONFIG *cfg)
{
return (cfg->yk_major_version == 2 &&
cfg->yk_minor_version >= 3) ||
cfg->yk_major_version > 2;
}
static bool vcheck_v24_or_greater(const YKP_CONFIG *cfg)
{
return (cfg->yk_major_version == 2 &&
cfg->yk_minor_version >= 4) ||
cfg->yk_major_version > 2;
}
static bool vcheck_v30(const YKP_CONFIG *cfg)
{
return (cfg->yk_major_version == 3 &&
cfg->yk_minor_version == 0);
}
static bool vcheck_neo(const YKP_CONFIG *cfg)
{
return (cfg->yk_major_version == 2 &&
cfg->yk_minor_version == 1 &&
cfg->yk_build_version >= 4);
}
static bool vcheck_neo_before_5(const YKP_CONFIG *cfg)
{
return vcheck_neo(cfg) && cfg->yk_build_version < 5;
}
static bool vcheck_neo_after_6(const YKP_CONFIG *cfg)
{
return vcheck_neo(cfg) && cfg->yk_build_version > 6;
}
bool capability_has_hidtrig(const YKP_CONFIG *cfg)
{
return vcheck_v1(cfg);
}
bool capability_has_ticket_first(const YKP_CONFIG *cfg)
{
return vcheck_v1(cfg);
}
bool capability_has_static(const YKP_CONFIG *cfg)
{
return vcheck_all(cfg) && !vcheck_neo_before_5(cfg);
}
bool capability_has_static_extras(const YKP_CONFIG *cfg)
{
return vcheck_no_v1(cfg) && !vcheck_neo_before_5(cfg);
}
bool capability_has_slot_two(const YKP_CONFIG *cfg)
{
return vcheck_no_v1(cfg) && !vcheck_neo(cfg);
}
bool capability_has_chal_resp(const YKP_CONFIG *cfg)
{
return vcheck_v22_or_greater(cfg);
}
bool capability_has_oath_imf(const YKP_CONFIG *cfg)
{
return vcheck_v22_or_greater(cfg) || vcheck_neo_after_6(cfg);
}
bool capability_has_serial_api(const YKP_CONFIG *cfg)
{
return vcheck_v22_or_greater(cfg) || vcheck_neo(cfg);
}
bool capability_has_serial(const YKP_CONFIG *cfg)
{
return vcheck_v22_or_greater(cfg);
}
bool capability_has_oath(const YKP_CONFIG *cfg)
{
return vcheck_v21_or_greater(cfg) || vcheck_neo(cfg);
}
bool capability_has_ticket_mods(const YKP_CONFIG *cfg)
{
return vcheck_all(cfg);
}
bool capability_has_update(const YKP_CONFIG *cfg)
{
return vcheck_v23_or_greater(cfg);
}
bool capability_has_fast(const YKP_CONFIG *cfg)
{
return vcheck_v23_or_greater(cfg);
}
bool capability_has_numeric(const YKP_CONFIG *cfg)
{
return vcheck_v23_or_greater(cfg);
}
bool capability_has_dormant(const YKP_CONFIG *cfg)
{
return vcheck_v23_or_greater(cfg);
}
bool capability_has_led_inv(const YKP_CONFIG *cfg)
{
return (vcheck_v24_or_greater(cfg) && !vcheck_v30(cfg));
}
int ykp_set_oath_imf(YKP_CONFIG *cfg, unsigned long imf)
{
if (!capability_has_oath_imf(cfg)) {
ykp_errno = YKP_EYUBIKEYVER;
return 0;
}
if (imf > 65535*16) {
ykp_errno = YKP_EINVAL;
return 0;
}
if (imf % 16 != 0) {
ykp_errno = YKP_EINVAL;
return 0;
}
/* IMF/16 is 16 bits stored big-endian in uid[4] */
imf /= 16;
cfg->ykcore_config.uid[4] = imf >> 8;
cfg->ykcore_config.uid[5] = imf;
return 1;
}
unsigned long ykp_get_oath_imf(const YKP_CONFIG *cfg)
{
if (!capability_has_oath_imf(cfg)) {
return 0;
}
/* we can't do a simple cast due to alignment issues */
return ((cfg->ykcore_config.uid[4] << 8)
| cfg->ykcore_config.uid[5]) << 4;
}
#define def_set_charfield(fnname,fieldname,size,extra,capability) \
int ykp_set_ ## fnname(YKP_CONFIG *cfg, unsigned char *input, size_t len) \
{ \
if (cfg) { \
size_t max_chars = len; \
\
if (!capability(cfg)) { \
ykp_errno = YKP_EYUBIKEYVER; \
return 0; \
} \
if (max_chars > (size)) \
max_chars = (size); \
\
memcpy(cfg->ykcore_config.fieldname, (input), max_chars); \
memset(cfg->ykcore_config.fieldname + max_chars, 0, \
(size) - max_chars); \
extra; \
\
return 1; \
} \
ykp_errno = YKP_ENOCFG; \
return 0; \
}
def_set_charfield(access_code,accCode,ACC_CODE_SIZE,,vcheck_all)
def_set_charfield(fixed,fixed,FIXED_SIZE,cfg->ykcore_config.fixedSize = max_chars,vcheck_all)
def_set_charfield(uid,uid,UID_SIZE,,vcheck_all)
#define def_set_tktflag(type,capability) \
int ykp_set_tktflag_ ## type(YKP_CONFIG *cfg, bool state) \
{ \
if (cfg) { \
if (!capability(cfg)) { \
ykp_errno = YKP_EYUBIKEYVER; \
return 0; \
} \
if (state) \
cfg->ykcore_config.tktFlags |= TKTFLAG_ ## type; \
else \
cfg->ykcore_config.tktFlags &= ~TKTFLAG_ ## type; \
return 1; \
} \
ykp_errno = YKP_ENOCFG; \
return 0; \
} \
bool ykp_get_tktflag_ ## type(const YKP_CONFIG *cfg) \
{ \
if (cfg) { \
if((cfg->ykcore_config.tktFlags & TKTFLAG_ ## type) == TKTFLAG_ ## type) \
return true; \
else \
return false; \
} \
return false; \
}
#define def_set_cfgflag(type,capability) \
int ykp_set_cfgflag_ ## type(YKP_CONFIG *cfg, bool state) \
{ \
if (cfg) { \
if (!capability(cfg)) { \
ykp_errno = YKP_EYUBIKEYVER; \
return 0; \
} \
if (state) \
cfg->ykcore_config.cfgFlags |= CFGFLAG_ ## type; \
else \
cfg->ykcore_config.cfgFlags &= ~CFGFLAG_ ## type; \
return 1; \
} \
ykp_errno = YKP_ENOCFG; \
return 0; \
} \
bool ykp_get_cfgflag_ ## type(const YKP_CONFIG *cfg) \
{ \
if (cfg) { \
if((cfg->ykcore_config.cfgFlags & CFGFLAG_ ## type) == CFGFLAG_ ## type) \
return true; \
else \
return false; \
} \
return false; \
}
#define def_set_extflag(type,capability) \
int ykp_set_extflag_ ## type(YKP_CONFIG *cfg, bool state) \
{ \
if (cfg) { \
if (!capability(cfg)) { \
ykp_errno = YKP_EYUBIKEYVER; \
return 0; \
} \
if (state) \
cfg->ykcore_config.extFlags |= EXTFLAG_ ## type; \
else \
cfg->ykcore_config.extFlags &= ~EXTFLAG_ ## type; \
return 1; \
} \
ykp_errno = YKP_ENOCFG; \
return 0; \
} \
bool ykp_get_extflag_ ## type(const YKP_CONFIG *cfg) \
{ \
if (cfg) { \
if((cfg->ykcore_config.extFlags & EXTFLAG_ ## type) == EXTFLAG_ ## type) \
return true; \
else \
return false; \
} \
return false; \
}
def_set_tktflag(TAB_FIRST,capability_has_ticket_mods)
def_set_tktflag(APPEND_TAB1,capability_has_ticket_mods)
def_set_tktflag(APPEND_TAB2,capability_has_ticket_mods)
def_set_tktflag(APPEND_DELAY1,capability_has_ticket_mods)
def_set_tktflag(APPEND_DELAY2,capability_has_ticket_mods)
def_set_tktflag(APPEND_CR,capability_has_ticket_mods)
def_set_tktflag(PROTECT_CFG2,capability_has_slot_two)
def_set_tktflag(OATH_HOTP,capability_has_oath)
def_set_tktflag(CHAL_RESP,capability_has_chal_resp)
def_set_cfgflag(SEND_REF,capability_has_ticket_mods)
def_set_cfgflag(TICKET_FIRST,capability_has_ticket_first)
def_set_cfgflag(PACING_10MS,capability_has_ticket_mods)
def_set_cfgflag(PACING_20MS,capability_has_ticket_mods)
def_set_cfgflag(ALLOW_HIDTRIG,capability_has_hidtrig)
def_set_cfgflag(STATIC_TICKET,capability_has_static)
def_set_cfgflag(SHORT_TICKET,capability_has_static_extras)
def_set_cfgflag(STRONG_PW1,capability_has_static_extras)
def_set_cfgflag(STRONG_PW2,capability_has_static_extras)
def_set_cfgflag(MAN_UPDATE,capability_has_static_extras)
def_set_cfgflag(OATH_HOTP8,capability_has_oath)
def_set_cfgflag(OATH_FIXED_MODHEX1,capability_has_oath)
def_set_cfgflag(OATH_FIXED_MODHEX2,capability_has_oath)
def_set_cfgflag(OATH_FIXED_MODHEX,capability_has_oath)
def_set_cfgflag(CHAL_YUBICO,capability_has_chal_resp)
def_set_cfgflag(CHAL_HMAC,capability_has_chal_resp)
def_set_cfgflag(HMAC_LT64,capability_has_chal_resp)
def_set_cfgflag(CHAL_BTN_TRIG,capability_has_chal_resp)
def_set_extflag(SERIAL_BTN_VISIBLE,capability_has_serial)
def_set_extflag(SERIAL_USB_VISIBLE,capability_has_serial)
def_set_extflag(SERIAL_API_VISIBLE,capability_has_serial_api)
def_set_extflag(USE_NUMERIC_KEYPAD,capability_has_numeric)
def_set_extflag(FAST_TRIG,capability_has_fast)
def_set_extflag(ALLOW_UPDATE,capability_has_update)
def_set_extflag(DORMANT,capability_has_dormant)
def_set_extflag(LED_INV,capability_has_led_inv)
static const char str_key_value_separator[] = ": ";
static const char str_hex_prefix[] = "h:";
static const char str_modhex_prefix[] = "m:";
static const char str_fixed[] = "fixed";
static const char str_oath_id[] = "OATH id";
static const char str_uid[] = "uid";
static const char str_key[] = "key";
static const char str_acc_code[] = "acc_code";
static const char str_oath_imf[] = "OATH IMF";
static const char str_flags_separator[] = "|";
static const char str_ticket_flags[] = "ticket_flags";
static const char str_config_flags[] = "config_flags";
static const char str_extended_flags[] = "extended_flags";
static int _ykp_legacy_export_config(const YKP_CONFIG *cfg, char *buf, size_t len) {
if (cfg) {
char buffer[256];
struct map_st *p;
unsigned char t_flags;
bool key_bits_in_uid = false;
YK_CONFIG ycfg = cfg->ykcore_config;
int mode = MODE_OTP_YUBICO;
int pos = 0;
int written;
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 OATH-HOTP and HMAC-SHA1 challenge response, there is four bytes
* additional key data in the uid field
*/
key_bits_in_uid = (ykp_get_supported_key_length(cfg) == 20);
/* fixed: or OATH id: */
if ((ycfg.tktFlags & TKTFLAG_OATH_HOTP) == TKTFLAG_OATH_HOTP &&
ycfg.fixedSize) {
/* First byte (vendor id) */
if ((ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX1) == CFGFLAG_OATH_FIXED_MODHEX1 ||
(ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX2) == CFGFLAG_OATH_FIXED_MODHEX2 ||
(ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX) == CFGFLAG_OATH_FIXED_MODHEX) {
yubikey_modhex_encode(buffer, (const char *)ycfg.fixed, 1);
} else {
yubikey_hex_encode(buffer, (const char *)ycfg.fixed, 1);
}
/* Second byte (token type) */
if ((ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX2) == CFGFLAG_OATH_FIXED_MODHEX2 ||
(ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX) == CFGFLAG_OATH_FIXED_MODHEX) {
yubikey_modhex_encode(buffer + 2, (const char *)ycfg.fixed + 1, 1);
} else {
yubikey_hex_encode(buffer + 2, (const char *)ycfg.fixed + 1, 1);
}
/* bytes 3-12 - MUI */
if ((ycfg.cfgFlags & CFGFLAG_OATH_FIXED_MODHEX) == CFGFLAG_OATH_FIXED_MODHEX) {
yubikey_modhex_encode(buffer + 4, (const char *)ycfg.fixed + 2, 8);
} else {
yubikey_hex_encode(buffer + 4, (const char *)ycfg.fixed + 2, 8);
}
buffer[12] = 0;
written = snprintf(buf, len - (size_t)pos, "%s%s%s\n", str_oath_id, str_key_value_separator, buffer);
if (written < 0 || pos + written > len) {
return -1;
}
pos += written;
} else {
yubikey_modhex_encode(buffer, (const char *)ycfg.fixed, ycfg.fixedSize);
written = snprintf(buf, len - (size_t)pos, "%s%s%s%s\n", str_fixed, str_key_value_separator, str_modhex_prefix, buffer);
if (written < 0 || pos + written > len) {
return -1;
}
pos += written;
}
/* uid: */