Check-in [79322b800c]
Overview
Comment:Simplified test code for AFL and added it to the Makefile
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 79322b800c9e73918ca0b071926b98677b443839
User & Date: rkeene on 2015-07-27 15:41:04
Other Links: manifest | tags
Context
2015-08-30
03:21
Updated to use older mechanism for activating gzip support in zlib, to support zlib older than zlib 1.2.4 check-in: ce2ae07c02 user: rkeene tags: trunk
2015-07-27
15:41
Simplified test code for AFL and added it to the Makefile check-in: 79322b800c user: rkeene tags: trunk
2015-07-24
15:11
Better cleanup now that we exclude some bytes in debug printing buffer check-in: 75b2699549 user: rkeene tags: trunk
Changes

Modified .fossil-settings/ignore-glob from [0b3e38b0b6] to [9166d2ec0f].

11
12
13
14
15
16
17


configure
install-sh
libcackey_g.so
libcackey.so
libcackey.syms
build/certs
test









>
>
11
12
13
14
15
16
17
18
19
configure
install-sh
libcackey_g.so
libcackey.so
libcackey.syms
build/certs
test
test-afl
test-afl.data

Modified Makefile.in from [6f86f7629d] to [a9f033d493].

32
33
34
35
36
37
38












39
40
41
42
43
44
45
libcackey_g.@SHOBJEXT@: cackey_g.o
	$(CC) $(SHOBJFLAGS) $(DEBUGCPPFLAGS) $(DEBUGCFLAGS) $(SHOBJLDFLAGS) $(LDFLAGS) -o libcackey_g.@SHOBJEXT@ cackey_g.o $(LIBS)
	-@WEAKENSYMS@ "libcackey_g.@SHOBJEXT@"

test: test.c libcackey_g.@SHOBJEXT@
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o test test.c -Wl,-R,. libcackey_g.@SHOBJEXT@













splint-cackey.txt: cackey.c cackey_builtin_certs.h asn1-x509.c asn1-x509.h config.h
	splint $(DEBUGCPPFLAGS) -DCACKEY_PARANOID=1 -weak +posixlib -I/usr/include/PCSC -Ipkcs11 cackey.c > splint-cackey.txt

install: libcackey.@SHOBJEXT@
	-mkdir -p "$(DESTDIR)$(libdir)"
	rm -f "$(DESTDIR)$(libdir)/libcackey.@SHOBJEXT@"
	rm -f "$(DESTDIR)$(libdir)/libcackey_g.@SHOBJEXT@"







>
>
>
>
>
>
>
>
>
>
>
>







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
libcackey_g.@SHOBJEXT@: cackey_g.o
	$(CC) $(SHOBJFLAGS) $(DEBUGCPPFLAGS) $(DEBUGCFLAGS) $(SHOBJLDFLAGS) $(LDFLAGS) -o libcackey_g.@SHOBJEXT@ cackey_g.o $(LIBS)
	-@WEAKENSYMS@ "libcackey_g.@SHOBJEXT@"

test: test.c libcackey_g.@SHOBJEXT@
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -o test test.c -Wl,-R,. libcackey_g.@SHOBJEXT@

test-afl.data: test
	tmpLogFile='log.$(shell openssl rand -hex 16)'; \
		./test 2> $${tmpLogFile}; \
		echo -ne "$$( \
			grep 'Returned Value:' $${tmpLogFile} | sed 's@^.*/@@;s@ = {@ @;s@})$$@@;s@,@@g;s@ @\\x@g;s@\\@ \\@' | while IFS=' ' read -r count string; do \
				printf '\\x%02x\\x%02x%s' $$[$${count} / 256] $$[$${count} % 256] "$${string}"; \
			done \
		)" > test-afl.data; rm -f $${tmpLogFile}

test-afl: test-afl.data test.c cackey.c cackey_builtin_certs.h sha1.c sha1.h md5.c md5.h asn1-x509.c asn1-x509.h config.h
	$(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -DCACKEY_TEST_AFL -o test-afl test.c $(patsubst -lpcsclite,,$(LIBS))

splint-cackey.txt: cackey.c cackey_builtin_certs.h asn1-x509.c asn1-x509.h config.h
	splint $(DEBUGCPPFLAGS) -DCACKEY_PARANOID=1 -weak +posixlib -I/usr/include/PCSC -Ipkcs11 cackey.c > splint-cackey.txt

install: libcackey.@SHOBJEXT@
	-mkdir -p "$(DESTDIR)$(libdir)"
	rm -f "$(DESTDIR)$(libdir)/libcackey.@SHOBJEXT@"
	rm -f "$(DESTDIR)$(libdir)/libcackey_g.@SHOBJEXT@"

Modified test.c from [7dfc56607f] to [b3c16cba8c].

1
2
3
4
5
6
7
































































































































































































































8





9
10
11
12
13
14
15
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#ifndef CACKEY_TEST_AFL
































































































































































































































#include "mypkcs11.h"






static char *pkcs11_attribute_to_name(CK_ATTRIBUTE_TYPE attrib) {
	static char retbuf[1024];

	switch (attrib) {
		case 0x00000000: return "CKA_CLASS";
		case 0x00000001: return "CKA_TOKEN";






|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>

>
>
>
>
>







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
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>

#ifdef CACKEY_TEST_AFL
#include <sys/types.h>
#include <fcntl.h>

static unsigned char *inputData;
static unsigned long inputDataLen;

/* Include the CACKey source */
#include "cackey.c"

#undef CACKEY_DEBUG_PRINTF
#define CACKEY_DEBUG_PRINTF(x...) /**/
#undef malloc
#undef realloc
#undef strdup

/* Fake a smartcard */
const SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci;
static int scard_inTransaction = 0;
static LONG scard_protocol;


PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext) {
	CACKEY_DEBUG_PRINTF("Called");

	*phContext = 42;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders) {
	static char *readers = "READER0";

	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	*pcchReaders = strlen(readers) + 1;

	if (mszReaders == NULL) {
		return(SCARD_S_SUCCESS);
	}

	memcpy(mszReaders, readers, *pcchReaders);

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	if (scard_inTransaction) {
		return(SCARD_E_SHARING_VIOLATION);
	}

	scard_inTransaction = 1;

	return(SCARD_S_SUCCESS);
}
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	scard_inTransaction = 0;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen) {
	LONG scardlistreaders_ret;

	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	*pdwState = 0;
	scardlistreaders_ret = SCardListReaders(42, NULL, mszReaderName, pcchReaderLen);
	if (scardlistreaders_ret != SCARD_S_SUCCESS) {
		return(scardlistreaders_ret);
	}

	*pdwProtocol = scard_protocol;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	if ((dwPreferredProtocols & SCARD_PROTOCOL_T0) == SCARD_PROTOCOL_T0) {
		*pdwActiveProtocol = SCARD_PROTOCOL_T0;
	} else {
		*pdwActiveProtocol = SCARD_PROTOCOL_T1;
	}

	scard_protocol = *pdwActiveProtocol;

	*phCard = 99;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCardConnect(42, NULL, dwShareMode, dwPreferredProtocols, NULL, pdwActiveProtocol));
}

PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) {
	CACKEY_DEBUG_PRINTF("Called");
	unsigned int bytesToRead;

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	if (inputDataLen <= 1) {
		*pcbRecvLength = 0;

		return(SCARD_S_SUCCESS);
	}

	bytesToRead = (inputData[0] << 8) | inputData[1];

	inputData    += 2;
	inputDataLen -= 2;

	if (bytesToRead > inputDataLen) {
		bytesToRead = inputDataLen;
	}

	if (bytesToRead > *pcbRecvLength) {
		return(SCARD_E_INSUFFICIENT_BUFFER);
	}

	*pcbRecvLength = bytesToRead;

	memcpy(pbRecvBuffer, inputData, bytesToRead);

	inputData += bytesToRead;
	inputDataLen -= bytesToRead;

	return(SCARD_S_SUCCESS);
}

static void loadTestData(const char *fileName) {
	ssize_t read_ret;
	int fd;

	if (fileName == NULL) {
		goto cleanup;
	}

	fd = open(fileName, O_RDONLY);
	if (fd < 0) {
		goto cleanup;
	}

	inputDataLen = 16384;
	inputData = malloc(inputDataLen);

	read_ret = read(fd, inputData, inputDataLen);
	if (read_ret < 0) {
		goto cleanup;
	}

	inputDataLen = read_ret;
	inputData = realloc(inputData, inputDataLen);

	close(fd);

cleanup:

	return;
}
#else
#include "mypkcs11.h"

static void loadTestData(const char *filename) {
	return;
}
#endif

static char *pkcs11_attribute_to_name(CK_ATTRIBUTE_TYPE attrib) {
	static char retbuf[1024];

	switch (attrib) {
		case 0x00000000: return "CKA_CLASS";
		case 0x00000001: return "CKA_TOKEN";
338
339
340
341
342
343
344




345

346
347
348
349
350
351
352
		if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
			fgets_ret = NULL;

			while (fgets_ret == NULL) {
				printf("** ENTER PIN: ");
				fflush(stdout);





				fgets_ret = fgets((char *) user_pin, sizeof(user_pin), stdin);

			}

			if (strlen((char *) user_pin) >= 1) {
				while (user_pin[strlen((char *) user_pin) - 1] < ' ') {
					user_pin[strlen((char *) user_pin) - 1] = '\0';
				}
			}







>
>
>
>

>







567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
		if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
			fgets_ret = NULL;

			while (fgets_ret == NULL) {
				printf("** ENTER PIN: ");
				fflush(stdout);

#ifdef CACKEY_TEST_AFL
				memcpy(user_pin, "0000000", 8);
				fgets_ret = (char *) user_pin;
#else
				fgets_ret = fgets((char *) user_pin, sizeof(user_pin), stdin);
#endif
			}

			if (strlen((char *) user_pin) >= 1) {
				while (user_pin[strlen((char *) user_pin) - 1] < ' ') {
					user_pin[strlen((char *) user_pin) - 1] = '\0';
				}
			}
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
	return(0);
}

int main(int argc, char **argv) {
	int retval = 0, ck_retval;

	printf("Testing libcackey...\n");





	ck_retval = main_pkcs11();

	if (ck_retval != 0) {
		retval = ck_retval;
	}

	printf("Testing libcackey... DONE. Status = %i\n", ck_retval);

	return(retval);

	/* UNREACHED: This is just to supress a warning about arguments to main we do not use. */
	argc = argc;
	argv = argv;
}
#else /* CACKEY_TEST_AFL */
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>

static unsigned char *inputData;
static unsigned long inputDataLen;

/* Include the CACKey source */
#include "cackey.c"

#undef CACKEY_DEBUG_PRINTF
#define CACKEY_DEBUG_PRINTF(x...) /**/
#undef malloc
#undef realloc
#undef strdup

/* Fake a smartcard */
const SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci;
static int scard_inTransaction = 0;
static LONG scard_protocol;


PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext) {
	CACKEY_DEBUG_PRINTF("Called");

	*phContext = 42;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders) {
	static char *readers = "READER0";

	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	*pcchReaders = strlen(readers) + 1;

	if (mszReaders == NULL) {
		return(SCARD_S_SUCCESS);
	}

	memcpy(mszReaders, readers, *pcchReaders);

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	if (scard_inTransaction) {
		return(SCARD_E_SHARING_VIOLATION);
	}

	scard_inTransaction = 1;

	return(SCARD_S_SUCCESS);
}
PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	scard_inTransaction = 0;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen) {
	LONG scardlistreaders_ret;

	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	*pdwState = 0;
	scardlistreaders_ret = SCardListReaders(42, NULL, mszReaderName, pcchReaderLen);
	if (scardlistreaders_ret != SCARD_S_SUCCESS) {
		return(scardlistreaders_ret);
	}

	*pdwProtocol = scard_protocol;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	if ((dwPreferredProtocols & SCARD_PROTOCOL_T0) == SCARD_PROTOCOL_T0) {
		*pdwActiveProtocol = SCARD_PROTOCOL_T0;
	} else {
		*pdwActiveProtocol = SCARD_PROTOCOL_T1;
	}

	scard_protocol = *pdwActiveProtocol;

	*phCard = 99;

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCardConnect(42, NULL, dwShareMode, dwPreferredProtocols, NULL, pdwActiveProtocol));
}

PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext) {
	CACKEY_DEBUG_PRINTF("Called");

	if (hContext != 42) {
		return(SCARD_E_INVALID_HANDLE);
	}

	return(SCARD_S_SUCCESS);
}

PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) {
	CACKEY_DEBUG_PRINTF("Called");
	unsigned int bytesToRead;

	if (hCard != 99) {
		return(SCARD_E_INVALID_HANDLE);
	}

	if (inputDataLen <= 1) {
		*pcbRecvLength = 0;

		return(SCARD_S_SUCCESS);
	}

	bytesToRead = (inputData[0] << 8) | inputData[1];

	inputData    += 2;
	inputDataLen -= 2;

	if (bytesToRead > inputDataLen) {
		bytesToRead = inputDataLen;
	}

	if (bytesToRead > *pcbRecvLength) {
		return(SCARD_E_INSUFFICIENT_BUFFER);
	}

	*pcbRecvLength = bytesToRead;

	memcpy(pbRecvBuffer, inputData, bytesToRead);

	inputData += bytesToRead;
	inputDataLen -= bytesToRead;

	return(SCARD_S_SUCCESS);
}

/* American Fuzzy Lop testing program */
int main(int argc, char **argv) {
	CK_FUNCTION_LIST_PTR pFunctionList;
	CK_RV (*C_CloseSession)(CK_SESSION_HANDLE hSession) = NULL;
	CK_RV (*C_Finalize)(CK_VOID_PTR pReserved) = NULL;
	CK_RV (*C_FindObjects)(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount) = NULL;
	CK_RV (*C_FindObjectsFinal)(CK_SESSION_HANDLE hSession) = NULL;
	CK_RV (*C_FindObjectsInit)(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) = NULL;
	CK_RV (*C_GetAttributeValue)(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) = NULL;
	CK_RV (*C_GetSlotList)(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) = NULL;
	CK_RV (*C_GetTokenInfo)(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) = NULL;
	CK_RV (*C_Initialize)(CK_VOID_PTR pInitArgs) = NULL;
	CK_RV (*C_Login)(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen) = NULL;
	CK_RV (*C_OpenSession)(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_SESSION_HANDLE_PTR phSession) = NULL;
	CK_RV (*C_Sign)(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) = NULL;
	CK_RV (*C_SignInit)(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) = NULL;
	CK_C_INITIALIZE_ARGS initargs;
	CK_ULONG numSlots;
	CK_SLOT_ID_PTR slots = NULL;
	CK_TOKEN_INFO tokenInfo;
	CK_SESSION_HANDLE hSession;
	CK_OBJECT_HANDLE hObject, *privateKeyObjects_root = NULL, *privateKeyObjects, *currPrivKey;
	CK_ULONG ulObjectCount;
	CK_ATTRIBUTE template[] = {
	                           {CKA_CLASS, NULL, 0},
	                           {CKA_TOKEN, NULL, 0},
	                           {CKA_LABEL, NULL, 0},
	                           {CKA_PRIVATE, NULL, 0},
	                           {CKA_ID, NULL, 0},
	                           {CKA_SERIAL_NUMBER, NULL, 0},
	                           {CKA_SUBJECT, NULL, 0},
	                           {CKA_ISSUER, NULL, 0},
	                           {CKA_CERTIFICATE_TYPE, NULL, 0},
	                           {CKA_KEY_TYPE, NULL, 0},
	                           {CKA_SIGN, NULL, 0},
	                           {CKA_VALUE, NULL, 0},
				   {CKA_CERT_MD5_HASH, NULL, 0},
				   {CKA_CERT_SHA1_HASH, NULL, 0},
				   {CKA_TRUSTED, NULL, 0},
				   {CKA_TRUST_CLIENT_AUTH, NULL, 0},
				   {CKA_TRUST_CODE_SIGNING, NULL, 0},
				   {CKA_TRUST_CRL_SIGN, NULL, 0},
				   {CKA_TRUST_DATA_ENCIPHERMENT, NULL, 0},
				   {CKA_TRUST_DIGITAL_SIGNATURE, NULL, 0},
				   {CKA_TRUST_EMAIL_PROTECTION, NULL, 0},
				   {CKA_TRUST_KEY_AGREEMENT, NULL, 0},
				   {CKA_TRUST_KEY_CERT_SIGN, NULL, 0},
				   {CKA_TRUST_KEY_ENCIPHERMENT, NULL, 0},
				   {CKA_TRUST_NON_REPUDIATION, NULL, 0},
				   {CKA_TRUST_SERVER_AUTH, NULL, 0}
	                          }, *curr_attr;
	CK_ULONG curr_attr_idx;
	CK_ULONG byte_idx;
	CK_OBJECT_CLASS objectClass;
	CK_BYTE signature[1024];
	CK_ULONG signature_len;
	CK_MECHANISM mechanism = {CKM_RSA_PKCS, NULL, 0};
	CK_RV chk_rv;
	ssize_t read_ret;
	char *fileName = NULL;
	int fd;
	int i;
	int initialized = 0;
	int retval = 1;

	fileName = argv[1];
	if (fileName == NULL) {
		goto cleanup;
	}

	fd = open(fileName, O_RDONLY);
	if (fd < 0) {
		goto cleanup;
	}

	inputDataLen = 16384;
	inputData = malloc(inputDataLen);

	read_ret = read(fd, inputData, inputDataLen);
	if (read_ret < 0) {
		goto cleanup;
	}

	inputDataLen = read_ret;
	inputData = realloc(inputData, inputDataLen);

	close(fd);

	chk_rv = C_GetFunctionList(&pFunctionList);
	if (chk_rv != CKR_OK) {
		printf("C_GetFunctionList() failed.");

		goto cleanup;
	}

	C_CloseSession = pFunctionList->C_CloseSession;
	C_Finalize = pFunctionList->C_Finalize;
	C_FindObjects = pFunctionList->C_FindObjects;
	C_FindObjectsFinal = pFunctionList->C_FindObjectsFinal;
	C_FindObjectsInit = pFunctionList->C_FindObjectsInit;
	C_GetAttributeValue = pFunctionList->C_GetAttributeValue;
	C_GetSlotList = pFunctionList->C_GetSlotList;
	C_GetTokenInfo = pFunctionList->C_GetTokenInfo;
	C_Initialize = pFunctionList->C_Initialize;
	C_Login = pFunctionList->C_Login;
	C_OpenSession = pFunctionList->C_OpenSession;
	C_Sign = pFunctionList->C_Sign;
	C_SignInit = pFunctionList->C_SignInit;

	privateKeyObjects = malloc(sizeof(*privateKeyObjects) * 1024);
	privateKeyObjects_root = privateKeyObjects;
	for (i = 0; i < 1024; i++) {
		privateKeyObjects[i] = CK_INVALID_HANDLE;
	}

	initargs.CreateMutex = NULL;
	initargs.DestroyMutex = NULL;
	initargs.LockMutex = NULL;
	initargs.UnlockMutex = NULL;
	initargs.flags = CKF_OS_LOCKING_OK;
	initargs.pReserved = NULL;

	chk_rv = C_Initialize(&initargs);
	if (chk_rv != CKR_OK) {
		initargs.CreateMutex = NULL;
		initargs.DestroyMutex = NULL;
		initargs.LockMutex = NULL;
		initargs.UnlockMutex = NULL;
		initargs.flags = 0;
		initargs.pReserved = NULL;

		chk_rv = C_Initialize(&initargs);
		if (chk_rv != CKR_OK) {
			printf("C_Initialize() failed.");

			goto cleanup;
		}
	}

	initialized = 1;

	chk_rv = C_GetSlotList(FALSE, NULL, &numSlots);
	if (chk_rv != CKR_OK) {
		goto cleanup;
	}

	if (numSlots == 0) {
		goto cleanup;
	}

	slots = malloc(sizeof(*slots) * numSlots);

	if (slots == NULL) {
		goto cleanup;
	}

	chk_rv = C_GetSlotList(FALSE, slots, &numSlots);
	if (chk_rv != CKR_OK) {
		goto cleanup;
	}

	chk_rv = C_OpenSession(slots[0], CKF_SERIAL_SESSION, NULL, NULL, &hSession);
	if (chk_rv != CKR_OK) {
		goto cleanup;
	}

	chk_rv = C_GetTokenInfo(slots[0], &tokenInfo);
	if (chk_rv != CKR_OK) {
		goto cleanup;
	}

	if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
		printf("Unable to login to card.\n");

		goto cleanup;
	}

	chk_rv = C_Login(hSession, CKU_USER, NULL, 0);
	if (chk_rv != CKR_OK) {
		printf("Login to device failed.\n");

		goto cleanup;
	}

	chk_rv = C_FindObjectsInit(hSession, NULL, 0);
	if (chk_rv != CKR_OK) {
		goto cleanup;
	}

	while (1) {
		chk_rv = C_FindObjects(hSession, &hObject, 1, &ulObjectCount);
		if (chk_rv != CKR_OK) {
			printf("FindObjects() failed.\n");
			break;
		}

		if (ulObjectCount == 0) {
			break;
		}

		if (ulObjectCount != 1) {
			printf("FindObjects() returned a weird number of objects.  Asked for 1, got %lu.\n", ulObjectCount);
			break;
		}

		for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
			curr_attr = &template[curr_attr_idx];
			if (curr_attr->pValue) {
				free(curr_attr->pValue);
			}

			curr_attr->pValue = NULL;
		}

		chk_rv = C_GetAttributeValue(hSession, hObject, &template[0], sizeof(template) / sizeof(template[0]));
		if (chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_BUFFER_TOO_SMALL) {
			chk_rv = CKR_OK;
		}

		if (chk_rv != CKR_OK) {
			continue;
		}

		for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
			curr_attr = &template[curr_attr_idx];

			if (((CK_LONG) curr_attr->ulValueLen) != ((CK_LONG) -1)) {
				curr_attr->pValue = malloc(curr_attr->ulValueLen);
			}
		}

		chk_rv = C_GetAttributeValue(hSession, hObject, &template[0], sizeof(template) / sizeof(template[0]));
		if (chk_rv == CKR_OK || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_BUFFER_TOO_SMALL) {
			for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
				curr_attr = &template[curr_attr_idx];

				if (curr_attr->pValue) {
					switch (curr_attr->type) {
						case CKA_CLASS:
							objectClass = *((CK_OBJECT_CLASS *) curr_attr->pValue);

							if (objectClass == CKO_PRIVATE_KEY) {
								*privateKeyObjects = hObject;
								privateKeyObjects++;
							}
					}
				} else {
					free(curr_attr->pValue);
					curr_attr->pValue = NULL;
				}
			}
		} else {
			printf("GetAttributeValue(hObject=%lu)/1 failed (rv = %lu).\n", (unsigned long) hObject, (unsigned long) chk_rv);
		}

	}

	chk_rv = C_FindObjectsFinal(hSession);
	if (chk_rv != CKR_OK) {
		printf("FindObjectsFinal() failed.\n");
	}

	for (currPrivKey = privateKeyObjects_root; *currPrivKey != CK_INVALID_HANDLE; currPrivKey++) {
		chk_rv = C_SignInit(hSession, &mechanism, *currPrivKey);
		if (chk_rv == CKR_OK) {
			signature_len = sizeof(signature);

			chk_rv = C_Sign(hSession, (CK_BYTE_PTR) "Test", 4, (CK_BYTE_PTR) &signature, &signature_len);
			if (chk_rv == CKR_OK) {
				printf("[%04lu/%02lx] Signature: ", (unsigned long) *currPrivKey, (unsigned long) mechanism.mechanism);

				for (byte_idx = 0; byte_idx < signature_len; byte_idx++) {
					printf("%02x ", (unsigned int) signature[byte_idx]);
				}

				printf("\n");
			} else {
				printf("Sign() failed.\n");
			}
		} else {
			printf("SignInit() failed.\n");
		}
	}

	chk_rv = C_CloseSession(hSession);
	if (chk_rv != CKR_OK) {
		printf("CloseSession failed.\n");
	}

	retval = 0;

cleanup:
	if (initialized) {
		C_Finalize(NULL);
	}

	if (slots) {
		free(slots);
	}

	if (privateKeyObjects_root) {
		free(privateKeyObjects_root);
	}

	return(retval);
}
#endif







>
>
>
>










|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875


























































































































































































































































































































































































































































































































	return(0);
}

int main(int argc, char **argv) {
	int retval = 0, ck_retval;

	printf("Testing libcackey...\n");

	if (argc > 1) {
		loadTestData(argv[1]);
	}

	ck_retval = main_pkcs11();

	if (ck_retval != 0) {
		retval = ck_retval;
	}

	printf("Testing libcackey... DONE. Status = %i\n", ck_retval);

	return(retval);
}