-
Notifications
You must be signed in to change notification settings - Fork 9
/
cackey.c
8600 lines (6582 loc) · 233 KB
/
cackey.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
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_WINTYPES_H
# include <wintypes.h>
#endif
#ifdef HAVE_PCSCLITE_H
# include <pcsclite.h>
#endif
#ifdef HAVE_WINSCARD_H
# include <winscard.h>
#endif
#ifdef HAVE_STDINT_H
# include <stdint.h>
#endif
#ifdef HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_PTHREAD_H
# include <pthread.h>
#endif
#ifdef HAVE_LIMITS_H
# include <limits.h>
#endif
#ifdef HAVE_STDIO_H
# include <stdio.h>
#endif
<<<<<<< HEAD
=======
#define HAVE_ERRNO_H 1
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
>>>>>>> trunk
#ifdef HAVE_ZLIB_H
# ifdef HAVE_LIBZ
# include <zlib.h>
# endif
#else
# ifdef HAVE_LIBZ
# undef HAVE_LIBZ
# endif
#endif
<<<<<<< HEAD
=======
#ifdef CACKEY_DEBUG_SEARCH_SPEEDTEST
# include <sys/time.h>
#endif
>>>>>>> trunk
#define CK_PTR *
#define CK_DEFINE_FUNCTION(returnType, name) returnType name
#define CK_DECLARE_FUNCTION(returnType, name) returnType name
#define CK_DECLARE_FUNCTION_POINTER(returnType, name) returnType (* name)
#define CK_CALLBACK_FUNCTION(returnType, name) returnType (* name)
#ifndef NULL_PTR
# define NULL_PTR 0
#endif
#include "pkcs11.h"
#include "pkcs11n.h"
#include "asn1-x509.h"
#include "sha1.h"
#include "md5.h"
<<<<<<< HEAD
/*
* Include these source files in this translation unit so that we can bind to
* functions and not include any symbols in the output shared object.
*/
#include "asn1-x509.c"
#include "sha1.c"
#include "md5.c"
=======
>>>>>>> trunk
#ifndef CACKEY_CRYPTOKI_VERSION_CODE
# define CACKEY_CRYPTOKI_VERSION_CODE 0x021e00
#endif
/* GSC-IS v2.1 Definitions */
/** Classes **/
#define GSCIS_CLASS_ISO7816 0x00
#define GSCIS_CLASS_GLOBAL_PLATFORM 0x80
/** Instructions **/
#define GSCIS_INSTR_GET_RESPONSE 0xC0
#define GSCIS_INSTR_READ_BINARY 0xB0
#define GSCIS_INSTR_UPDATE_BINARY 0xD6
#define GSCIS_INSTR_SELECT 0xA4
#define GSCIS_INSTR_EXTERNAL_AUTH 0x82
#define GSCIS_INSTR_GET_CHALLENGE 0x84
#define GSCIS_INSTR_INTERNAL_AUTH 0x88
#define GSCIS_INSTR_VERIFY 0x20
<<<<<<< HEAD
=======
#define GSCIS_INSTR_CHANGE_REFERENCE 0x24
>>>>>>> trunk
#define GSCIS_INSTR_SIGN 0x2A
#define GSCIS_INSTR_GET_PROP 0x56
#define GSCIS_INSTR_GET_ACR 0x4C
#define GSCIS_INSTR_READ_BUFFER 0x52
#define GSCIS_INSTR_SIGNDECRYPT 0x42
#define GSCIS_PARAM_SELECT_APPLET 0x04
/** Tags **/
/*** CCC Tags ***/
#define GSCIS_TAG_CARDID 0xF0
#define GSCIS_TAG_CCC_VER 0xF1
#define GSCIS_TAG_CCG_VER 0xF2
#define GSCIS_TAG_CARDURL 0xF3
#define GSCIS_TAG_PKCS15 0xF4
#define GSCIS_TAG_REG_DATA_MODEL 0xF5
#define GSCIS_TAG_ACR_TABLE 0xF6
#define GSCIS_TAG_CARD_APDU 0xF7
#define GSCIS_TAG_REDIRECTION 0xFA
#define GSCIS_TAG_CT 0xFB
#define GSCIS_TAG_ST 0xFC
#define GSCIS_TAG_NEXTCCC 0xFD
/*** General - EF 2200 ***/
#define GSCIS_TAG_FNAME 0x01
#define GSCIS_TAG_MNAME 0x02
#define GSCIS_TAG_LNAME 0x03
#define GSCIS_TAG_SUFFIX 0x04
#define GSCIS_TAG_GOVT_AGENCY 0x05
#define GSCIS_TAG_BUREAU 0x06
#define GSCIS_TAG_BUREAU_CODE 0x07
#define GSCIS_TAG_DEPT_CODE 0x08
#define GSCIS_TAG_TITLE 0x09
#define GSCIS_TAG_BUILDING 0x10
#define GSCIS_TAG_OFFICE_ADDR1 0x11
#define GSCIS_TAG_OFFICE_ADDR2 0x12
#define GSCIS_TAG_OFFICE_CITY 0x13
#define GSCIS_TAG_OFFICE_STATE 0x14
#define GSCIS_TAG_OFFICE_ZIP 0x15
#define GSCIS_TAG_OFFICE_COUNTRY 0x16
#define GSCIS_TAG_OFFICE_PHONE 0x17
#define GSCIS_TAG_OFFICE_PHONE_EXT 0x18
#define GSCIS_TAG_OFFICE_FAX 0x19
#define GSCIS_TAG_OFFICE_EMAIL 0x1A
#define GSCIS_TAG_OFFICE_ROOM 0x1B
#define GSCIS_TAG_NONGOV_AGENCY 0x1C
#define GSCIS_TAG_SSN_DESIGNATOR 0x1D
/*** PII - EF 2100 ***/
#define GSCIS_TAG_SSN 0x20
#define GSCIS_TAG_DOB 0x21
#define GSCIS_TAG_GENDER 0x22
/*** Login Information - EF 4000 ***/
#define GSCIS_TAG_USERID 0x40
#define GSCIS_TAG_DOMAIN 0x41
#define GSCIS_TAG_PASSWORD 0x42
/*** Card Information - EF 5000 ***/
#define GSCIS_TAG_ISSUERID 0x50
#define GSCIS_TAG_SERNO 0x51
#define GSCIS_TAG_ISSUE_DATE 0x52
#define GSCIS_TAG_EXPIRE_DATE 0x53
#define GSCIS_TAG_CARD_TYPE 0x54
#define GSCIS_TAG_SECURITY_CODE 0x57
#define GSCIS_TAG_CARDID_AID 0x58
<<<<<<< HEAD
=======
/*** PIV Codes ***/
#define NISTSP800_73_3_INSTR_GET_DATA 0xCB
#define NISTSP800_73_3_INSTR_GENAUTH 0x87
>>>>>>> trunk
/*** PKI Information - EF 7000 ***/
#define GSCIS_TAG_CERTIFICATE 0x70
#define GSCIS_TAG_CERT_ISSUE_DATE 0x71
#define GSCIS_TAG_CERT_EXPIRE_DATE 0x72
/** Applet IDs **/
#define GSCIS_AID_CCC 0xA0, 0x00, 0x00, 0x01, 0x16, 0xDB, 0x00
<<<<<<< HEAD
=======
#define NISTSP800_73_3_PIV_AID 0xA0, 0x00, 0x00, 0x03, 0x08, 0x00, 0x00, 0x10, 0x00, 0x01, 0x00
/* PIV IDs */
/** Key Identifiers (NIST SP 800-78-3, Table 6-1 **/
#define NISTSP800_78_3_KEY_PIVAUTH 0x9A
#define NISTSP800_78_3_KEY_SIGNATURE 0x9C
#define NISTSP800_78_3_KEY_KEYMGT 0x9D
#define NISTSP800_78_3_KEY_CARDAUTH 0x9E
/** Algorithm Identifiers (NIST SP 800-78-3, Table 6-2 **/
#define NISTSP800_78_3_ALGO_RSA1024 0x06
#define NISTSP800_78_3_ALGO_RSA2048 0x07
/** Object Identifiers (NIST SP 800-73-3 Part 1, Table 2) **/
#define NISTSP800_73_3_OID_PIVAUTH 0x5F, 0xC1, 0x05
#define NISTSP800_73_3_OID_SIGNATURE 0x5F, 0xC1, 0x0A
#define NISTSP800_73_3_OID_KEYMGT 0x5F, 0xC1, 0x0B
#define NISTSP800_73_3_OID_CARDAUTH 0x5F, 0xC1, 0x01
>>>>>>> trunk
/* Maximum size of data portion of APDUs */
/** Do not set this above 250 **/
#define CACKEY_APDU_MTU 250
/* ATR If not available */
#ifndef MAX_ATR_SIZE
#define MAX_ATR_SIZE 1024
#endif
#ifdef CACKEY_DEBUG
# ifdef HAVE_TIME_H
# include <time.h>
static time_t cackey_debug_start_time = 0;
<<<<<<< HEAD
# define CACKEY_DEBUG_PRINTTIME { if (cackey_debug_start_time == 0) { cackey_debug_start_time = time(NULL); }; fprintf(cackey_debug_fd(), "[%lu]: ", (unsigned long) (time(NULL) - cackey_debug_start_time)); }
# else
# define CACKEY_DEBUG_PRINTTIME /**/
# endif
# define CACKEY_DEBUG_PRINTF(x...) { CACKEY_DEBUG_PRINTTIME; fprintf(cackey_debug_fd(), "%s():%i: ", __func__, __LINE__); fprintf(cackey_debug_fd(), x); fprintf(cackey_debug_fd(), "\n"); fflush(cackey_debug_fd()); }
# define CACKEY_DEBUG_PRINTBUF(f, x, y) { unsigned char *TMPBUF; unsigned long idx; TMPBUF = (unsigned char *) (x); CACKEY_DEBUG_PRINTTIME; fprintf(cackey_debug_fd(), "%s():%i: %s (%s/%lu = {%02x", __func__, __LINE__, f, #x, (unsigned long) (y), TMPBUF[0]); for (idx = 1; idx < (y); idx++) { fprintf(cackey_debug_fd(), ", %02x", TMPBUF[idx]); }; fprintf(cackey_debug_fd(), "})\n"); fflush(cackey_debug_fd()); }
# define CACKEY_DEBUG_PERROR(x) { fprintf(cackey_debug_fd(), "%s():%i: ", __func__, __LINE__); CACKEY_DEBUG_PRINTTIME; perror(x); fflush(cackey_debug_fd()); }
# define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", x, #x); free(x); }
=======
static unsigned long CACKEY_DEBUG_GETTIME(void) {
if (cackey_debug_start_time == 0) {
cackey_debug_start_time = time(NULL);
}
return(time(NULL) - cackey_debug_start_time);
}
# else
static unsigned long CACKEY_DEBUG_GETTIME(void) {
return(0);
}
# endif
# define CACKEY_DEBUG_PRINTF(x...) { \
static char buf_user[4096] = {0}; \
snprintf(buf_user, sizeof(buf_user), x); \
buf_user[sizeof(buf_user) - 1] = '\0'; \
fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, buf_user); \
fflush(cackey_debug_fd()); \
}
# define CACKEY_DEBUG_PRINTBUF(f, x, y) { \
static char buf_user[4096] = {0}, *buf_user_p, *buf_user_print; \
unsigned long buf_user_size; \
unsigned char *TMPBUF; \
unsigned long idx; \
int snprintf_ret; \
TMPBUF = (unsigned char *) (x); \
buf_user[0] = 0; \
buf_user[2] = 0; \
buf_user_p = buf_user; \
buf_user_size = sizeof(buf_user); \
for (idx = 0; idx < (y); idx++) { \
if (buf_user_size <= 0) { \
break; \
}; \
snprintf_ret = snprintf(buf_user_p, buf_user_size, ", %02x", TMPBUF[idx]); \
if (snprintf_ret <= 0) { \
break; \
}; \
buf_user_p += snprintf_ret; \
buf_user_size -= snprintf_ret; \
}; \
buf_user[sizeof(buf_user) - 1] = '\0'; \
buf_user_print = buf_user + 2; \
fprintf(cackey_debug_fd(), "[%lu]: %s():%i: %s (%s/%lu = {%s})\n", CACKEY_DEBUG_GETTIME(), __func__, __LINE__, f, #x, (unsigned long) (y), buf_user_print); \
fflush(cackey_debug_fd()); \
}
# define free(x) { CACKEY_DEBUG_PRINTF("FREE(%p) (%s)", (void *) x, #x); free(x); }
>>>>>>> trunk
static FILE *cackey_debug_fd(void) {
static FILE *fd = NULL;
char *logfile;
if (fd != NULL) {
return(fd);
}
/*
* Log to stderr initially so we can use debugging within
* this function without getting into an infinite loop
*/
fd = stderr;
logfile = getenv("CACKEY_DEBUG_LOGFILE");
if (logfile != NULL) {
CACKEY_DEBUG_PRINTF("Found environment variable: %s", logfile);
logfile = strchr(logfile, '=');
if (logfile == NULL) {
logfile = getenv("CACKEY_DEBUG_LOGFILE");
} else {
logfile++;
}
}
<<<<<<< HEAD
=======
#ifdef CACKEY_DEBUG_LOGFILE
if (logfile == NULL) {
logfile = CACKEY_DEBUG_LOGFILE;
}
#endif
>>>>>>> trunk
if (logfile != NULL) {
CACKEY_DEBUG_PRINTF("Found log file: %s", logfile);
fd = fopen(logfile, "a");
}
if (fd == NULL) {
fd = stderr;
}
if (fd == stderr) {
CACKEY_DEBUG_PRINTF("Returning stderr");
} else {
<<<<<<< HEAD
CACKEY_DEBUG_PRINTF("Returning %p", fd);
=======
CACKEY_DEBUG_PRINTF("Returning %p", (void *) fd);
>>>>>>> trunk
}
return(fd);
}
static void *CACKEY_DEBUG_FUNC_MALLOC(size_t size, const char *func, int line) {
void *retval;
retval = malloc(size);
<<<<<<< HEAD
CACKEY_DEBUG_PRINTTIME;
fprintf(cackey_debug_fd(), "%s():%i: ", func, line);
fprintf(cackey_debug_fd(), "MALLOC() = %p", retval);
fprintf(cackey_debug_fd(), "\n");
=======
fprintf(cackey_debug_fd(), "[%lu]: %s():%i: MALLOC() = %p\n", CACKEY_DEBUG_GETTIME(), func, line, retval);
>>>>>>> trunk
fflush(cackey_debug_fd());
return(retval);
}
static void *CACKEY_DEBUG_FUNC_REALLOC(void *ptr, size_t size, const char *func, int line) {
void *retval;
retval = realloc(ptr, size);
if (retval != ptr) {
<<<<<<< HEAD
CACKEY_DEBUG_PRINTTIME;
fprintf(cackey_debug_fd(), "%s():%i: ", func, line);
fprintf(cackey_debug_fd(), "REALLOC(%p) = %p", ptr, retval);
fprintf(cackey_debug_fd(), "\n");
=======
fprintf(cackey_debug_fd(), "[%lu]: %s():%i: REALLOC(%p) = %p\n", CACKEY_DEBUG_GETTIME(), func, line, ptr, retval);
>>>>>>> trunk
fflush(cackey_debug_fd());
}
if (retval == NULL) {
CACKEY_DEBUG_PRINTF(" *** ERROR *** realloc returned NULL (size = %lu)", (unsigned long) size);
}
return(retval);
}
static char *CACKEY_DEBUG_FUNC_STRDUP(const char *ptr, const char *func, int line) {
char *retval;
retval = strdup(ptr);
<<<<<<< HEAD
CACKEY_DEBUG_PRINTTIME;
fprintf(cackey_debug_fd(), "%s():%i: ", func, line);
fprintf(cackey_debug_fd(), "STRDUP_MALLOC() = %p", retval);
fprintf(cackey_debug_fd(), "\n");
=======
fprintf(cackey_debug_fd(), "[%lu]: %s():%i: STRDUP_MALLOC() = %p\n", CACKEY_DEBUG_GETTIME(), func, line, retval);
>>>>>>> trunk
fflush(cackey_debug_fd());
return(retval);
}
static const char *CACKEY_DEBUG_FUNC_TAG_TO_STR(unsigned char tag) {
switch (tag) {
case GSCIS_TAG_CARDID:
return("GSCIS_TAG_CARDID");
case GSCIS_TAG_CCC_VER:
return("GSCIS_TAG_CCC_VER");
case GSCIS_TAG_CCG_VER:
return("GSCIS_TAG_CCG_VER");
case GSCIS_TAG_CARDURL:
return("GSCIS_TAG_CARDURL");
case GSCIS_TAG_PKCS15:
return("GSCIS_TAG_PKCS15");
case GSCIS_TAG_REG_DATA_MODEL:
return("GSCIS_TAG_REG_DATA_MODEL");
case GSCIS_TAG_ACR_TABLE:
return("GSCIS_TAG_ACR_TABLE");
case GSCIS_TAG_CARD_APDU:
return("GSCIS_TAG_CARD_APDU");
case GSCIS_TAG_REDIRECTION:
return("GSCIS_TAG_REDIRECTION");
case GSCIS_TAG_CT:
return("GSCIS_TAG_CT");
case GSCIS_TAG_ST:
return("GSCIS_TAG_ST");
case GSCIS_TAG_NEXTCCC:
return("GSCIS_TAG_NEXTCCC");
case GSCIS_TAG_FNAME:
return("GSCIS_TAG_FNAME");
case GSCIS_TAG_MNAME:
return("GSCIS_TAG_MNAME");
case GSCIS_TAG_LNAME:
return("GSCIS_TAG_LNAME");
case GSCIS_TAG_SUFFIX:
return("GSCIS_TAG_SUFFIX");
case GSCIS_TAG_GOVT_AGENCY:
return("GSCIS_TAG_GOVT_AGENCY");
case GSCIS_TAG_BUREAU:
return("GSCIS_TAG_BUREAU");
case GSCIS_TAG_BUREAU_CODE:
return("GSCIS_TAG_BUREAU_CODE");
case GSCIS_TAG_DEPT_CODE:
return("GSCIS_TAG_DEPT_CODE");
case GSCIS_TAG_TITLE:
return("GSCIS_TAG_TITLE");
case GSCIS_TAG_BUILDING:
return("GSCIS_TAG_BUILDING");
case GSCIS_TAG_OFFICE_ADDR1:
return("GSCIS_TAG_OFFICE_ADDR1");
case GSCIS_TAG_OFFICE_ADDR2:
return("GSCIS_TAG_OFFICE_ADDR2");
case GSCIS_TAG_OFFICE_CITY:
return("GSCIS_TAG_OFFICE_CITY");
case GSCIS_TAG_OFFICE_STATE:
return("GSCIS_TAG_OFFICE_STATE");
case GSCIS_TAG_OFFICE_ZIP:
return("GSCIS_TAG_OFFICE_ZIP");
case GSCIS_TAG_OFFICE_COUNTRY:
return("GSCIS_TAG_OFFICE_COUNTRY");
case GSCIS_TAG_OFFICE_PHONE:
return("GSCIS_TAG_OFFICE_PHONE");
case GSCIS_TAG_OFFICE_PHONE_EXT:
return("GSCIS_TAG_OFFICE_PHONE_EXT");
case GSCIS_TAG_OFFICE_FAX:
return("GSCIS_TAG_OFFICE_FAX");
case GSCIS_TAG_OFFICE_EMAIL:
return("GSCIS_TAG_OFFICE_EMAIL");
case GSCIS_TAG_OFFICE_ROOM:
return("GSCIS_TAG_OFFICE_ROOM");
case GSCIS_TAG_NONGOV_AGENCY:
return("GSCIS_TAG_NONGOV_AGENCY");
case GSCIS_TAG_SSN_DESIGNATOR:
return("GSCIS_TAG_SSN_DESIGNATOR");
case GSCIS_TAG_SSN:
return("GSCIS_TAG_SSN");
case GSCIS_TAG_DOB:
return("GSCIS_TAG_DOB");
case GSCIS_TAG_GENDER:
return("GSCIS_TAG_GENDER");
case GSCIS_TAG_USERID:
return("GSCIS_TAG_USERID");
case GSCIS_TAG_DOMAIN:
return("GSCIS_TAG_DOMAIN");
case GSCIS_TAG_PASSWORD:
return("GSCIS_TAG_PASSWORD");
case GSCIS_TAG_ISSUERID:
return("GSCIS_TAG_ISSUERID");
case GSCIS_TAG_SERNO:
return("GSCIS_TAG_SERNO");
case GSCIS_TAG_ISSUE_DATE:
return("GSCIS_TAG_ISSUE_DATE");
case GSCIS_TAG_EXPIRE_DATE:
return("GSCIS_TAG_EXPIRE_DATE");
case GSCIS_TAG_CARD_TYPE:
return("GSCIS_TAG_CARD_TYPE");
case GSCIS_TAG_SECURITY_CODE:
return("GSCIS_TAG_SECURITY_CODE");
case GSCIS_TAG_CARDID_AID:
return("GSCIS_TAG_CARDID_AID");
case GSCIS_TAG_CERTIFICATE:
return("GSCIS_TAG_CERTIFICATE");
case GSCIS_TAG_CERT_ISSUE_DATE:
return("GSCIS_TAG_CERT_ISSUE_DATE");
case GSCIS_TAG_CERT_EXPIRE_DATE:
return("GSCIS_TAG_CERT_EXPIRE_DATE");
}
return("UNKNOWN");
}
static const char *CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(LONG retcode) {
switch (retcode) {
case SCARD_S_SUCCESS:
return("SCARD_S_SUCCESS");
case SCARD_E_CANCELLED:
return("SCARD_E_CANCELLED");
case SCARD_E_CANT_DISPOSE:
return("SCARD_E_CANT_DISPOSE");
case SCARD_E_INSUFFICIENT_BUFFER:
return("SCARD_E_INSUFFICIENT_BUFFER");
case SCARD_E_INVALID_ATR:
return("SCARD_E_INVALID_ATR");
case SCARD_E_INVALID_HANDLE:
return("SCARD_E_INVALID_HANDLE");
case SCARD_E_INVALID_PARAMETER:
return("SCARD_E_INVALID_PARAMETER");
case SCARD_E_INVALID_TARGET:
return("SCARD_E_INVALID_TARGET");
case SCARD_E_INVALID_VALUE:
return("SCARD_E_INVALID_VALUE");
case SCARD_E_NO_MEMORY:
return("SCARD_E_NO_MEMORY");
case SCARD_E_UNKNOWN_READER:
return("SCARD_E_UNKNOWN_READER");
case SCARD_E_TIMEOUT:
return("SCARD_E_TIMEOUT");
case SCARD_E_SHARING_VIOLATION:
return("SCARD_E_SHARING_VIOLATION");
case SCARD_E_NO_SMARTCARD:
return("SCARD_E_NO_SMARTCARD");
case SCARD_E_UNKNOWN_CARD:
return("SCARD_E_UNKNOWN_CARD");
case SCARD_E_PROTO_MISMATCH:
return("SCARD_E_PROTO_MISMATCH");
case SCARD_E_NOT_READY:
return("SCARD_E_NOT_READY");
case SCARD_E_SYSTEM_CANCELLED:
return("SCARD_E_SYSTEM_CANCELLED");
case SCARD_E_NOT_TRANSACTED:
return("SCARD_E_NOT_TRANSACTED");
case SCARD_E_READER_UNAVAILABLE:
return("SCARD_E_READER_UNAVAILABLE");
case SCARD_W_UNSUPPORTED_CARD:
return("SCARD_W_UNSUPPORTED_CARD");
case SCARD_W_UNRESPONSIVE_CARD:
return("SCARD_W_UNRESPONSIVE_CARD");
case SCARD_W_UNPOWERED_CARD:
return("SCARD_W_UNPOWERED_CARD");
case SCARD_W_RESET_CARD:
return("SCARD_W_RESET_CARD");
case SCARD_W_REMOVED_CARD:
return("SCARD_W_REMOVED_CARD");
case SCARD_E_PCI_TOO_SMALL:
return("SCARD_E_PCI_TOO_SMALL");
case SCARD_E_READER_UNSUPPORTED:
return("SCARD_E_READER_UNSUPPORTED");
case SCARD_E_DUPLICATE_READER:
return("SCARD_E_DUPLICATE_READER");
case SCARD_E_CARD_UNSUPPORTED:
return("SCARD_E_CARD_UNSUPPORTED");
case SCARD_E_NO_SERVICE:
return("SCARD_E_NO_SERVICE");
case SCARD_E_SERVICE_STOPPED:
return("SCARD_E_SERVICE_STOPPED");
case SCARD_E_UNSUPPORTED_FEATURE:
return("SCARD_E_UNSUPPORTED_FEATURE");
#ifdef SCARD_W_INSERTED_CARD
case SCARD_W_INSERTED_CARD:
return("SCARD_W_INSERTED_CARD");
#endif
#ifdef SCARD_E_NO_READERS_AVAILABLE
case SCARD_E_NO_READERS_AVAILABLE:
return("SCARD_E_NO_READERS_AVAILABLE");
#endif
}
return("UNKNOWN");
}
static const char *CACKEY_DEBUG_FUNC_OBJID_TO_STR(uint16_t objid) {
switch (objid) {
case 0x2000:
return("CACKEY_TLV_OBJID_GENERALINFO");
case 0x2100:
return("CACKEY_TLV_OBJID_PROPERSONALINFO");
case 0x3000:
return("CACKEY_TLV_OBJID_ACCESSCONTROL");
case 0x4000:
return("CACKEY_TLV_OBJID_LOGIN");
case 0x5000:
return("CACKEY_TLV_OBJID_CARDINFO");
case 0x6000:
return("CACKEY_TLV_OBJID_BIOMETRICS");
case 0x7000:
return("CACKEY_TLV_OBJID_DIGITALSIGCERT");
case 0x0200:
return("CACKEY_TLV_OBJID_CAC_PERSON");
case 0x0202:
return("CACKEY_TLV_OBJID_CAC_BENEFITS");
case 0x0203:
return("CACKEY_TLV_OBJID_CAC_OTHERBENEFITS");
case 0x0201:
return("CACKEY_TLV_OBJID_CAC_PERSONNEL");
case 0x02FE:
return("CACKEY_TLV_OBJID_CAC_PKICERT");
}
return("UNKNOWN");
}
static const char *CACKEY_DEBUG_FUNC_APPTYPE_TO_STR(uint8_t apptype) {
switch (apptype) {
case 0x00:
return("NONE");
case 0x01:
return("CACKEY_TLV_APP_GENERIC");
case 0x02:
return("CACKEY_TLV_APP_SKI");
case 0x03:
return("CACKEY_TLV_APP_GENERIC | CACKEY_TLV_APP_SKI");
case 0x04:
return("CACKEY_TLV_APP_PKI");
case 0x05:
return("CACKEY_TLV_APP_GENERIC | CACKEY_TLV_APP_PKI");
case 0x06:
return("CACKEY_TLV_APP_SKI | CACKEY_TLV_APP_PKI");
case 0x07:
return("CACKEY_TLV_APP_GENERIC | CACKEY_TLV_APP_SKI | CACKEY_TLV_APP_PKI");
}
return("INVALID");
}
static const char *CACKEY_DEBUG_FUNC_ATTRIBUTE_TO_STR(CK_ATTRIBUTE_TYPE attr) {
switch (attr) {
case CKA_CLASS:
return("CKA_CLASS");
case CKA_TOKEN:
return("CKA_TOKEN");
case CKA_PRIVATE:
return("CKA_PRIVATE");
case CKA_LABEL:
return("CKA_LABEL");
case CKA_APPLICATION:
return("CKA_APPLICATION");
case CKA_VALUE:
return("CKA_VALUE");
case CKA_OBJECT_ID:
return("CKA_OBJECT_ID");
case CKA_CERTIFICATE_TYPE:
return("CKA_CERTIFICATE_TYPE");
case CKA_ISSUER:
return("CKA_ISSUER");
case CKA_SERIAL_NUMBER:
return("CKA_SERIAL_NUMBER");
case CKA_AC_ISSUER:
return("CKA_AC_ISSUER");
case CKA_OWNER:
return("CKA_OWNER");
case CKA_ATTR_TYPES:
return("CKA_ATTR_TYPES");
case CKA_TRUSTED:
return("CKA_TRUSTED");
case CKA_KEY_TYPE:
return("CKA_KEY_TYPE");
case CKA_SUBJECT:
return("CKA_SUBJECT");
case CKA_ID:
return("CKA_ID");
case CKA_SENSITIVE:
return("CKA_SENSITIVE");
case CKA_ENCRYPT:
return("CKA_ENCRYPT");
case CKA_DECRYPT:
return("CKA_DECRYPT");
case CKA_WRAP:
return("CKA_WRAP");
case CKA_UNWRAP:
return("CKA_UNWRAP");
case CKA_SIGN:
return("CKA_SIGN");
case CKA_SIGN_RECOVER:
return("CKA_SIGN_RECOVER");
case CKA_VERIFY:
return("CKA_VERIFY");
case CKA_VERIFY_RECOVER:
return("CKA_VERIFY_RECOVER");
case CKA_DERIVE:
return("CKA_DERIVE");
case CKA_START_DATE:
return("CKA_START_DATE");
case CKA_END_DATE:
return("CKA_END_DATE");
case CKA_MODULUS:
return("CKA_MODULUS");
case CKA_MODULUS_BITS:
return("CKA_MODULUS_BITS");
case CKA_PUBLIC_EXPONENT:
return("CKA_PUBLIC_EXPONENT");
case CKA_PRIVATE_EXPONENT:
return("CKA_PRIVATE_EXPONENT");
case CKA_PRIME_1:
return("CKA_PRIME_1");
case CKA_PRIME_2:
return("CKA_PRIME_2");
case CKA_EXPONENT_1:
return("CKA_EXPONENT_1");
case CKA_EXPONENT_2:
return("CKA_EXPONENT_2");
case CKA_COEFFICIENT:
return("CKA_COEFFICIENT");
case CKA_PRIME:
return("CKA_PRIME");
case CKA_SUBPRIME:
return("CKA_SUBPRIME");
case CKA_BASE:
return("CKA_BASE");
case CKA_PRIME_BITS:
return("CKA_PRIME_BITS");
case CKA_SUB_PRIME_BITS:
return("CKA_SUB_PRIME_BITS");
case CKA_VALUE_BITS:
return("CKA_VALUE_BITS");
case CKA_VALUE_LEN:
return("CKA_VALUE_LEN");
case CKA_EXTRACTABLE:
return("CKA_EXTRACTABLE");
case CKA_LOCAL:
return("CKA_LOCAL");
case CKA_NEVER_EXTRACTABLE:
return("CKA_NEVER_EXTRACTABLE");
case CKA_ALWAYS_SENSITIVE:
return("CKA_ALWAYS_SENSITIVE");
case CKA_KEY_GEN_MECHANISM:
return("CKA_KEY_GEN_MECHANISM");
case CKA_MODIFIABLE:
return("CKA_MODIFIABLE");
case CKA_ECDSA_PARAMS:
return("CKA_ECDSA_PARAMS");
case CKA_EC_POINT:
return("CKA_EC_POINT");
case CKA_SECONDARY_AUTH:
return("CKA_SECONDARY_AUTH");
case CKA_AUTH_PIN_FLAGS:
return("CKA_AUTH_PIN_FLAGS");
case CKA_HW_FEATURE_TYPE:
return("CKA_HW_FEATURE_TYPE");
case CKA_RESET_ON_INIT:
return("CKA_RESET_ON_INIT");
case CKA_HAS_RESET:
return("CKA_HAS_RESET");
case CKA_VENDOR_DEFINED:
return("CKA_VENDOR_DEFINED");
}
return("UNKNOWN");
}
# define malloc(x) CACKEY_DEBUG_FUNC_MALLOC(x, __func__, __LINE__)
# define realloc(x, y) CACKEY_DEBUG_FUNC_REALLOC(x, y, __func__, __LINE__)
# ifdef strdup
# undef strdup
# endif
# define strdup(x) CACKEY_DEBUG_FUNC_STRDUP(x, __func__, __LINE__)
#else
# define CACKEY_DEBUG_PRINTF(x...) /**/
# define CACKEY_DEBUG_PRINTBUF(f, x, y) /**/
<<<<<<< HEAD
# define CACKEY_DEBUG_PERROR(x) /**/
=======
>>>>>>> trunk
# define CACKEY_DEBUG_FUNC_TAG_TO_STR(x) "DEBUG_DISABLED"
# define CACKEY_DEBUG_FUNC_SCARDERR_TO_STR(x) "DEBUG_DISABLED"
# define CACKEY_DEBUG_FUNC_OBJID_TO_STR(x) "DEBUG_DISABLED"
# define CACKEY_DEBUG_FUNC_APPTYPE_TO_STR(x) "DEBUG_DISABLED"
# define CACKEY_DEBUG_FUNC_ATTRIBUTE_TO_STR(x) "DEBUG_DISABLED"
#endif
<<<<<<< HEAD
struct cackey_pcsc_identity {
unsigned char applet[7];
uint16_t file;
=======
/*
* Include these source files in this translation unit so that we can bind to
* functions and not include any symbols in the output shared object.
*/
#include "asn1-x509.c"
#include "sha1.c"
#include "md5.c"
typedef enum {
CACKEY_ID_TYPE_CAC,
CACKEY_ID_TYPE_PIV,
CACKEY_ID_TYPE_CERT_ONLY
} cackey_pcsc_id_type;
struct cackey_pcsc_identity {
cackey_pcsc_id_type id_type;
>>>>>>> trunk
size_t certificate_len;
unsigned char *certificate;
ssize_t keysize;
<<<<<<< HEAD
=======
union {
struct {
unsigned char applet[7];
uint16_t file;
} cac;
struct {
unsigned char key_id;
char label[32];
} piv;
} card;
>>>>>>> trunk
};
struct cackey_identity {
struct cackey_pcsc_identity *pcsc_identity;
CK_ATTRIBUTE *attributes;
CK_ULONG attributes_count;
};
struct cackey_session {
int active;
CK_SLOT_ID slotID;
CK_STATE state;
CK_FLAGS flags;
CK_ULONG ulDeviceError;
CK_VOID_PTR pApplication;
CK_NOTIFY Notify;
struct cackey_identity *identities;
unsigned long identities_count;
int search_active;
CK_ATTRIBUTE_PTR search_query;
CK_ULONG search_query_count;
unsigned long search_curr_id;
int sign_active;
CK_MECHANISM_TYPE sign_mechanism;
CK_BYTE_PTR sign_buf;
unsigned long sign_buflen;
unsigned long sign_bufused;
struct cackey_identity *sign_identity;
int decrypt_active;
CK_MECHANISM_TYPE decrypt_mechanism;
CK_VOID_PTR decrypt_mech_parm;
CK_ULONG decrypt_mech_parmlen;
struct cackey_identity *decrypt_identity;
};
struct cackey_slot {
int active;
<<<<<<< HEAD
=======
int internal;
>>>>>>> trunk
char *pcsc_reader;
int pcsc_card_connected;
SCARDHANDLE pcsc_card;
int transaction_depth;
int transaction_need_hw_lock;
int slot_reset;
CK_FLAGS token_flags;
unsigned char *label;
DWORD protocol;
<<<<<<< HEAD
=======
unsigned int cached_certs_count;
struct cackey_pcsc_identity *cached_certs;
>>>>>>> trunk
};
typedef enum {
CACKEY_TLV_APP_GENERIC = 0x01,
CACKEY_TLV_APP_SKI = 0x02,
CACKEY_TLV_APP_PKI = 0x04
} cackey_tlv_apptype;
typedef enum {
CACKEY_TLV_OBJID_GENERALINFO = 0x2000,
CACKEY_TLV_OBJID_PROPERSONALINFO = 0x2100,
CACKEY_TLV_OBJID_ACCESSCONTROL = 0x3000,
CACKEY_TLV_OBJID_LOGIN = 0x4000,
CACKEY_TLV_OBJID_CARDINFO = 0x5000,
CACKEY_TLV_OBJID_BIOMETRICS = 0x6000,
CACKEY_TLV_OBJID_DIGITALSIGCERT = 0x7000,
CACKEY_TLV_OBJID_CAC_PERSON = 0x0200,
CACKEY_TLV_OBJID_CAC_BENEFITS = 0x0202,
CACKEY_TLV_OBJID_CAC_OTHERBENEFITS = 0x0203,
CACKEY_TLV_OBJID_CAC_PERSONNEL = 0x0201,
CACKEY_TLV_OBJID_CAC_PKICERT = 0x02FE
} cackey_tlv_objectid;
typedef enum {
CACKEY_PCSC_S_TOKENPRESENT = 1,
CACKEY_PCSC_S_OK = 0,
CACKEY_PCSC_E_GENERIC = -1,
CACKEY_PCSC_E_BADPIN = -2,
CACKEY_PCSC_E_LOCKED = -3,
CACKEY_PCSC_E_NEEDLOGIN = -4,
CACKEY_PCSC_E_TOKENABSENT = -6,
<<<<<<< HEAD
CACKEY_PCSC_E_RETRY = -7
=======
CACKEY_PCSC_E_RETRY = -7,
CACKEY_PCSC_E_NODATA = -8
>>>>>>> trunk
} cackey_ret;
struct cackey_tlv_cardurl {
unsigned char rid[5];
cackey_tlv_apptype apptype;
cackey_tlv_objectid objectid;
cackey_tlv_objectid appid;
unsigned char pinid;
};
struct cackey_tlv_entity;
struct cackey_tlv_entity {
uint8_t tag;
size_t length;
union {
void *value;
struct cackey_tlv_cardurl *value_cardurl;
uint8_t value_byte;
};
struct cackey_tlv_entity *_next;
};
/* CACKEY Global Handles */
static void *cackey_biglock = NULL;
static struct cackey_session cackey_sessions[128];
static struct cackey_slot cackey_slots[128];
static int cackey_initialized = 0;
static int cackey_biglock_init = 0;
CK_C_INITIALIZE_ARGS cackey_args;
/** Extra certificates to include in token **/
struct cackey_pcsc_identity extra_certs[] = {
#include "cackey_builtin_certs.h"
};
<<<<<<< HEAD
=======
#define CACKEY_MACRO_DEFAULT_XSTR(str) CACKEY_MACRO_DEFAULT_STR(str)
#define CACKEY_MACRO_DEFAULT_STR(str) #str
/* Protected Authentication Path command */
static char *cackey_pin_command = NULL;
/* Reader Exclusion or Include-only */
static char *cackey_readers_include_only = NULL;
static char *cackey_readers_exclude = NULL;
>>>>>>> trunk
/* PCSC Global Handles */
static LPSCARDCONTEXT cackey_pcsc_handle = NULL;
static unsigned long cackey_getversion(void) {
static unsigned long retval = 255;
unsigned long major = 0;
unsigned long minor = 0;
char *major_str = NULL;
char *minor_str = NULL;
CACKEY_DEBUG_PRINTF("Called.");