Diff

Differences From Artifact [7dfc56607f]:

To Artifact [b3c16cba8c]:


     1      1   #include <sys/types.h>
     2      2   #include <string.h>
     3      3   #include <unistd.h>
     4      4   #include <stdlib.h>
     5      5   #include <stdio.h>
     6      6   
     7         -#ifndef CACKEY_TEST_AFL
            7  +#ifdef CACKEY_TEST_AFL
            8  +#include <sys/types.h>
            9  +#include <fcntl.h>
           10  +
           11  +static unsigned char *inputData;
           12  +static unsigned long inputDataLen;
           13  +
           14  +/* Include the CACKey source */
           15  +#include "cackey.c"
           16  +
           17  +#undef CACKEY_DEBUG_PRINTF
           18  +#define CACKEY_DEBUG_PRINTF(x...) /**/
           19  +#undef malloc
           20  +#undef realloc
           21  +#undef strdup
           22  +
           23  +/* Fake a smartcard */
           24  +const SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci;
           25  +static int scard_inTransaction = 0;
           26  +static LONG scard_protocol;
           27  +
           28  +
           29  +PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext) {
           30  +	CACKEY_DEBUG_PRINTF("Called");
           31  +
           32  +	*phContext = 42;
           33  +
           34  +	return(SCARD_S_SUCCESS);
           35  +}
           36  +
           37  +PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext) {
           38  +	CACKEY_DEBUG_PRINTF("Called");
           39  +
           40  +	if (hContext != 42) {
           41  +		return(SCARD_E_INVALID_HANDLE);
           42  +	}
           43  +
           44  +	return(SCARD_S_SUCCESS);
           45  +}
           46  +
           47  +PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders) {
           48  +	static char *readers = "READER0";
           49  +
           50  +	CACKEY_DEBUG_PRINTF("Called");
           51  +
           52  +	if (hContext != 42) {
           53  +		return(SCARD_E_INVALID_HANDLE);
           54  +	}
           55  +
           56  +	*pcchReaders = strlen(readers) + 1;
           57  +
           58  +	if (mszReaders == NULL) {
           59  +		return(SCARD_S_SUCCESS);
           60  +	}
           61  +
           62  +	memcpy(mszReaders, readers, *pcchReaders);
           63  +
           64  +	return(SCARD_S_SUCCESS);
           65  +}
           66  +
           67  +PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard) {
           68  +	CACKEY_DEBUG_PRINTF("Called");
           69  +
           70  +	if (hCard != 99) {
           71  +		return(SCARD_E_INVALID_HANDLE);
           72  +	}
           73  +
           74  +	if (scard_inTransaction) {
           75  +		return(SCARD_E_SHARING_VIOLATION);
           76  +	}
           77  +
           78  +	scard_inTransaction = 1;
           79  +
           80  +	return(SCARD_S_SUCCESS);
           81  +}
           82  +PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) {
           83  +	CACKEY_DEBUG_PRINTF("Called");
           84  +
           85  +	if (hCard != 99) {
           86  +		return(SCARD_E_INVALID_HANDLE);
           87  +	}
           88  +
           89  +	scard_inTransaction = 0;
           90  +
           91  +	return(SCARD_S_SUCCESS);
           92  +}
           93  +
           94  +PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen) {
           95  +	LONG scardlistreaders_ret;
           96  +
           97  +	CACKEY_DEBUG_PRINTF("Called");
           98  +
           99  +	if (hCard != 99) {
          100  +		return(SCARD_E_INVALID_HANDLE);
          101  +	}
          102  +
          103  +	*pdwState = 0;
          104  +	scardlistreaders_ret = SCardListReaders(42, NULL, mszReaderName, pcchReaderLen);
          105  +	if (scardlistreaders_ret != SCARD_S_SUCCESS) {
          106  +		return(scardlistreaders_ret);
          107  +	}
          108  +
          109  +	*pdwProtocol = scard_protocol;
          110  +
          111  +	return(SCARD_S_SUCCESS);
          112  +}
          113  +
          114  +PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol) {
          115  +	CACKEY_DEBUG_PRINTF("Called");
          116  +
          117  +	if (hContext != 42) {
          118  +		return(SCARD_E_INVALID_HANDLE);
          119  +	}
          120  +
          121  +	if ((dwPreferredProtocols & SCARD_PROTOCOL_T0) == SCARD_PROTOCOL_T0) {
          122  +		*pdwActiveProtocol = SCARD_PROTOCOL_T0;
          123  +	} else {
          124  +		*pdwActiveProtocol = SCARD_PROTOCOL_T1;
          125  +	}
          126  +
          127  +	scard_protocol = *pdwActiveProtocol;
          128  +
          129  +	*phCard = 99;
          130  +
          131  +	return(SCARD_S_SUCCESS);
          132  +}
          133  +
          134  +PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition) {
          135  +	CACKEY_DEBUG_PRINTF("Called");
          136  +
          137  +	if (hCard != 99) {
          138  +		return(SCARD_E_INVALID_HANDLE);
          139  +	}
          140  +
          141  +	return(SCARD_S_SUCCESS);
          142  +}
          143  +
          144  +PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol) {
          145  +	CACKEY_DEBUG_PRINTF("Called");
          146  +
          147  +	if (hCard != 99) {
          148  +		return(SCARD_E_INVALID_HANDLE);
          149  +	}
          150  +
          151  +	return(SCardConnect(42, NULL, dwShareMode, dwPreferredProtocols, NULL, pdwActiveProtocol));
          152  +}
          153  +
          154  +PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext) {
          155  +	CACKEY_DEBUG_PRINTF("Called");
          156  +
          157  +	if (hContext != 42) {
          158  +		return(SCARD_E_INVALID_HANDLE);
          159  +	}
          160  +
          161  +	return(SCARD_S_SUCCESS);
          162  +}
          163  +
          164  +PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) {
          165  +	CACKEY_DEBUG_PRINTF("Called");
          166  +	unsigned int bytesToRead;
          167  +
          168  +	if (hCard != 99) {
          169  +		return(SCARD_E_INVALID_HANDLE);
          170  +	}
          171  +
          172  +	if (inputDataLen <= 1) {
          173  +		*pcbRecvLength = 0;
          174  +
          175  +		return(SCARD_S_SUCCESS);
          176  +	}
          177  +
          178  +	bytesToRead = (inputData[0] << 8) | inputData[1];
          179  +
          180  +	inputData    += 2;
          181  +	inputDataLen -= 2;
          182  +
          183  +	if (bytesToRead > inputDataLen) {
          184  +		bytesToRead = inputDataLen;
          185  +	}
          186  +
          187  +	if (bytesToRead > *pcbRecvLength) {
          188  +		return(SCARD_E_INSUFFICIENT_BUFFER);
          189  +	}
          190  +
          191  +	*pcbRecvLength = bytesToRead;
          192  +
          193  +	memcpy(pbRecvBuffer, inputData, bytesToRead);
          194  +
          195  +	inputData += bytesToRead;
          196  +	inputDataLen -= bytesToRead;
          197  +
          198  +	return(SCARD_S_SUCCESS);
          199  +}
          200  +
          201  +static void loadTestData(const char *fileName) {
          202  +	ssize_t read_ret;
          203  +	int fd;
          204  +
          205  +	if (fileName == NULL) {
          206  +		goto cleanup;
          207  +	}
          208  +
          209  +	fd = open(fileName, O_RDONLY);
          210  +	if (fd < 0) {
          211  +		goto cleanup;
          212  +	}
          213  +
          214  +	inputDataLen = 16384;
          215  +	inputData = malloc(inputDataLen);
          216  +
          217  +	read_ret = read(fd, inputData, inputDataLen);
          218  +	if (read_ret < 0) {
          219  +		goto cleanup;
          220  +	}
          221  +
          222  +	inputDataLen = read_ret;
          223  +	inputData = realloc(inputData, inputDataLen);
          224  +
          225  +	close(fd);
          226  +
          227  +cleanup:
          228  +
          229  +	return;
          230  +}
          231  +#else
     8    232   #include "mypkcs11.h"
          233  +
          234  +static void loadTestData(const char *filename) {
          235  +	return;
          236  +}
          237  +#endif
     9    238   
    10    239   static char *pkcs11_attribute_to_name(CK_ATTRIBUTE_TYPE attrib) {
    11    240   	static char retbuf[1024];
    12    241   
    13    242   	switch (attrib) {
    14    243   		case 0x00000000: return "CKA_CLASS";
    15    244   		case 0x00000001: return "CKA_TOKEN";
................................................................................
   338    567   		if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
   339    568   			fgets_ret = NULL;
   340    569   
   341    570   			while (fgets_ret == NULL) {
   342    571   				printf("** ENTER PIN: ");
   343    572   				fflush(stdout);
   344    573   
          574  +#ifdef CACKEY_TEST_AFL
          575  +				memcpy(user_pin, "0000000", 8);
          576  +				fgets_ret = (char *) user_pin;
          577  +#else
   345    578   				fgets_ret = fgets((char *) user_pin, sizeof(user_pin), stdin);
          579  +#endif
   346    580   			}
   347    581   
   348    582   			if (strlen((char *) user_pin) >= 1) {
   349    583   				while (user_pin[strlen((char *) user_pin) - 1] < ' ') {
   350    584   					user_pin[strlen((char *) user_pin) - 1] = '\0';
   351    585   				}
   352    586   			}
................................................................................
   620    854   	return(0);
   621    855   }
   622    856   
   623    857   int main(int argc, char **argv) {
   624    858   	int retval = 0, ck_retval;
   625    859   
   626    860   	printf("Testing libcackey...\n");
          861  +
          862  +	if (argc > 1) {
          863  +		loadTestData(argv[1]);
          864  +	}
   627    865   
   628    866   	ck_retval = main_pkcs11();
   629    867   
   630    868   	if (ck_retval != 0) {
   631    869   		retval = ck_retval;
   632    870   	}
   633    871   
   634    872   	printf("Testing libcackey... DONE. Status = %i\n", ck_retval);
   635    873   
   636    874   	return(retval);
   637         -
   638         -	/* UNREACHED: This is just to supress a warning about arguments to main we do not use. */
   639         -	argc = argc;
   640         -	argv = argv;
   641         -}
   642         -#else /* CACKEY_TEST_AFL */
   643         -#include <sys/stat.h>
   644         -#include <sys/types.h>
   645         -#include <fcntl.h>
   646         -
   647         -static unsigned char *inputData;
   648         -static unsigned long inputDataLen;
   649         -
   650         -/* Include the CACKey source */
   651         -#include "cackey.c"
   652         -
   653         -#undef CACKEY_DEBUG_PRINTF
   654         -#define CACKEY_DEBUG_PRINTF(x...) /**/
   655         -#undef malloc
   656         -#undef realloc
   657         -#undef strdup
   658         -
   659         -/* Fake a smartcard */
   660         -const SCARD_IO_REQUEST g_rgSCardT0Pci, g_rgSCardT1Pci;
   661         -static int scard_inTransaction = 0;
   662         -static LONG scard_protocol;
   663         -
   664         -
   665         -PCSC_API LONG SCardEstablishContext(DWORD dwScope, LPCVOID pvReserved1, LPCVOID pvReserved2, LPSCARDCONTEXT phContext) {
   666         -	CACKEY_DEBUG_PRINTF("Called");
   667         -
   668         -	*phContext = 42;
   669         -
   670         -	return(SCARD_S_SUCCESS);
   671         -}
   672         -
   673         -PCSC_API LONG SCardIsValidContext(SCARDCONTEXT hContext) {
   674         -	CACKEY_DEBUG_PRINTF("Called");
   675         -
   676         -	if (hContext != 42) {
   677         -		return(SCARD_E_INVALID_HANDLE);
   678         -	}
   679         -
   680         -	return(SCARD_S_SUCCESS);
   681         -}
   682         -
   683         -PCSC_API LONG SCardListReaders(SCARDCONTEXT hContext, LPCSTR mszGroups, LPSTR mszReaders, LPDWORD pcchReaders) {
   684         -	static char *readers = "READER0";
   685         -
   686         -	CACKEY_DEBUG_PRINTF("Called");
   687         -
   688         -	if (hContext != 42) {
   689         -		return(SCARD_E_INVALID_HANDLE);
   690         -	}
   691         -
   692         -	*pcchReaders = strlen(readers) + 1;
   693         -
   694         -	if (mszReaders == NULL) {
   695         -		return(SCARD_S_SUCCESS);
   696         -	}
   697         -
   698         -	memcpy(mszReaders, readers, *pcchReaders);
   699         -
   700         -	return(SCARD_S_SUCCESS);
   701         -}
   702         -
   703         -PCSC_API LONG SCardBeginTransaction(SCARDHANDLE hCard) {
   704         -	CACKEY_DEBUG_PRINTF("Called");
   705         -
   706         -	if (hCard != 99) {
   707         -		return(SCARD_E_INVALID_HANDLE);
   708         -	}
   709         -
   710         -	if (scard_inTransaction) {
   711         -		return(SCARD_E_SHARING_VIOLATION);
   712         -	}
   713         -
   714         -	scard_inTransaction = 1;
   715         -
   716         -	return(SCARD_S_SUCCESS);
   717         -}
   718         -PCSC_API LONG SCardEndTransaction(SCARDHANDLE hCard, DWORD dwDisposition) {
   719         -	CACKEY_DEBUG_PRINTF("Called");
   720         -
   721         -	if (hCard != 99) {
   722         -		return(SCARD_E_INVALID_HANDLE);
   723         -	}
   724         -
   725         -	scard_inTransaction = 0;
   726         -
   727         -	return(SCARD_S_SUCCESS);
   728         -}
   729         -
   730         -PCSC_API LONG SCardStatus(SCARDHANDLE hCard, LPSTR mszReaderName, LPDWORD pcchReaderLen, LPDWORD pdwState, LPDWORD pdwProtocol, LPBYTE pbAtr, LPDWORD pcbAtrLen) {
   731         -	LONG scardlistreaders_ret;
   732         -
   733         -	CACKEY_DEBUG_PRINTF("Called");
   734         -
   735         -	if (hCard != 99) {
   736         -		return(SCARD_E_INVALID_HANDLE);
   737         -	}
   738         -
   739         -	*pdwState = 0;
   740         -	scardlistreaders_ret = SCardListReaders(42, NULL, mszReaderName, pcchReaderLen);
   741         -	if (scardlistreaders_ret != SCARD_S_SUCCESS) {
   742         -		return(scardlistreaders_ret);
   743         -	}
   744         -
   745         -	*pdwProtocol = scard_protocol;
   746         -
   747         -	return(SCARD_S_SUCCESS);
   748         -}
   749         -
   750         -PCSC_API LONG SCardConnect(SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol) {
   751         -	CACKEY_DEBUG_PRINTF("Called");
   752         -
   753         -	if (hContext != 42) {
   754         -		return(SCARD_E_INVALID_HANDLE);
   755         -	}
   756         -
   757         -	if ((dwPreferredProtocols & SCARD_PROTOCOL_T0) == SCARD_PROTOCOL_T0) {
   758         -		*pdwActiveProtocol = SCARD_PROTOCOL_T0;
   759         -	} else {
   760         -		*pdwActiveProtocol = SCARD_PROTOCOL_T1;
   761         -	}
   762         -
   763         -	scard_protocol = *pdwActiveProtocol;
   764         -
   765         -	*phCard = 99;
   766         -
   767         -	return(SCARD_S_SUCCESS);
   768         -}
   769         -
   770         -PCSC_API LONG SCardDisconnect(SCARDHANDLE hCard, DWORD dwDisposition) {
   771         -	CACKEY_DEBUG_PRINTF("Called");
   772         -
   773         -	if (hCard != 99) {
   774         -		return(SCARD_E_INVALID_HANDLE);
   775         -	}
   776         -
   777         -	return(SCARD_S_SUCCESS);
   778         -}
   779         -
   780         -PCSC_API LONG SCardReconnect(SCARDHANDLE hCard, DWORD dwShareMode, DWORD dwPreferredProtocols, DWORD dwInitialization, LPDWORD pdwActiveProtocol) {
   781         -	CACKEY_DEBUG_PRINTF("Called");
   782         -
   783         -	if (hCard != 99) {
   784         -		return(SCARD_E_INVALID_HANDLE);
   785         -	}
   786         -
   787         -	return(SCardConnect(42, NULL, dwShareMode, dwPreferredProtocols, NULL, pdwActiveProtocol));
   788         -}
   789         -
   790         -PCSC_API LONG SCardReleaseContext(SCARDCONTEXT hContext) {
   791         -	CACKEY_DEBUG_PRINTF("Called");
   792         -
   793         -	if (hContext != 42) {
   794         -		return(SCARD_E_INVALID_HANDLE);
   795         -	}
   796         -
   797         -	return(SCARD_S_SUCCESS);
   798         -}
   799         -
   800         -PCSC_API LONG SCardTransmit(SCARDHANDLE hCard, const SCARD_IO_REQUEST *pioSendPci, LPCBYTE pbSendBuffer, DWORD cbSendLength, SCARD_IO_REQUEST *pioRecvPci, LPBYTE pbRecvBuffer, LPDWORD pcbRecvLength) {
   801         -	CACKEY_DEBUG_PRINTF("Called");
   802         -	unsigned int bytesToRead;
   803         -
   804         -	if (hCard != 99) {
   805         -		return(SCARD_E_INVALID_HANDLE);
   806         -	}
   807         -
   808         -	if (inputDataLen <= 1) {
   809         -		*pcbRecvLength = 0;
   810         -
   811         -		return(SCARD_S_SUCCESS);
   812         -	}
   813         -
   814         -	bytesToRead = (inputData[0] << 8) | inputData[1];
   815         -
   816         -	inputData    += 2;
   817         -	inputDataLen -= 2;
   818         -
   819         -	if (bytesToRead > inputDataLen) {
   820         -		bytesToRead = inputDataLen;
   821         -	}
   822         -
   823         -	if (bytesToRead > *pcbRecvLength) {
   824         -		return(SCARD_E_INSUFFICIENT_BUFFER);
   825         -	}
   826         -
   827         -	*pcbRecvLength = bytesToRead;
   828         -
   829         -	memcpy(pbRecvBuffer, inputData, bytesToRead);
   830         -
   831         -	inputData += bytesToRead;
   832         -	inputDataLen -= bytesToRead;
   833         -
   834         -	return(SCARD_S_SUCCESS);
   835         -}
   836         -
   837         -/* American Fuzzy Lop testing program */
   838         -int main(int argc, char **argv) {
   839         -	CK_FUNCTION_LIST_PTR pFunctionList;
   840         -	CK_RV (*C_CloseSession)(CK_SESSION_HANDLE hSession) = NULL;
   841         -	CK_RV (*C_Finalize)(CK_VOID_PTR pReserved) = NULL;
   842         -	CK_RV (*C_FindObjects)(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE_PTR phObject, CK_ULONG ulMaxObjectCount, CK_ULONG_PTR pulObjectCount) = NULL;
   843         -	CK_RV (*C_FindObjectsFinal)(CK_SESSION_HANDLE hSession) = NULL;
   844         -	CK_RV (*C_FindObjectsInit)(CK_SESSION_HANDLE hSession, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) = NULL;
   845         -	CK_RV (*C_GetAttributeValue)(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount) = NULL;
   846         -	CK_RV (*C_GetSlotList)(CK_BBOOL tokenPresent, CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) = NULL;
   847         -	CK_RV (*C_GetTokenInfo)(CK_SLOT_ID slotID, CK_TOKEN_INFO_PTR pInfo) = NULL;
   848         -	CK_RV (*C_Initialize)(CK_VOID_PTR pInitArgs) = NULL;
   849         -	CK_RV (*C_Login)(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType, CK_UTF8CHAR_PTR pPin, CK_ULONG ulPinLen) = NULL;
   850         -	CK_RV (*C_OpenSession)(CK_SLOT_ID slotID, CK_FLAGS flags, CK_VOID_PTR pApplication, CK_NOTIFY notify, CK_SESSION_HANDLE_PTR phSession) = NULL;
   851         -	CK_RV (*C_Sign)(CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData, CK_ULONG ulDataLen, CK_BYTE_PTR pSignature, CK_ULONG_PTR pulSignatureLen) = NULL;
   852         -	CK_RV (*C_SignInit)(CK_SESSION_HANDLE hSession, CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) = NULL;
   853         -	CK_C_INITIALIZE_ARGS initargs;
   854         -	CK_ULONG numSlots;
   855         -	CK_SLOT_ID_PTR slots = NULL;
   856         -	CK_TOKEN_INFO tokenInfo;
   857         -	CK_SESSION_HANDLE hSession;
   858         -	CK_OBJECT_HANDLE hObject, *privateKeyObjects_root = NULL, *privateKeyObjects, *currPrivKey;
   859         -	CK_ULONG ulObjectCount;
   860         -	CK_ATTRIBUTE template[] = {
   861         -	                           {CKA_CLASS, NULL, 0},
   862         -	                           {CKA_TOKEN, NULL, 0},
   863         -	                           {CKA_LABEL, NULL, 0},
   864         -	                           {CKA_PRIVATE, NULL, 0},
   865         -	                           {CKA_ID, NULL, 0},
   866         -	                           {CKA_SERIAL_NUMBER, NULL, 0},
   867         -	                           {CKA_SUBJECT, NULL, 0},
   868         -	                           {CKA_ISSUER, NULL, 0},
   869         -	                           {CKA_CERTIFICATE_TYPE, NULL, 0},
   870         -	                           {CKA_KEY_TYPE, NULL, 0},
   871         -	                           {CKA_SIGN, NULL, 0},
   872         -	                           {CKA_VALUE, NULL, 0},
   873         -				   {CKA_CERT_MD5_HASH, NULL, 0},
   874         -				   {CKA_CERT_SHA1_HASH, NULL, 0},
   875         -				   {CKA_TRUSTED, NULL, 0},
   876         -				   {CKA_TRUST_CLIENT_AUTH, NULL, 0},
   877         -				   {CKA_TRUST_CODE_SIGNING, NULL, 0},
   878         -				   {CKA_TRUST_CRL_SIGN, NULL, 0},
   879         -				   {CKA_TRUST_DATA_ENCIPHERMENT, NULL, 0},
   880         -				   {CKA_TRUST_DIGITAL_SIGNATURE, NULL, 0},
   881         -				   {CKA_TRUST_EMAIL_PROTECTION, NULL, 0},
   882         -				   {CKA_TRUST_KEY_AGREEMENT, NULL, 0},
   883         -				   {CKA_TRUST_KEY_CERT_SIGN, NULL, 0},
   884         -				   {CKA_TRUST_KEY_ENCIPHERMENT, NULL, 0},
   885         -				   {CKA_TRUST_NON_REPUDIATION, NULL, 0},
   886         -				   {CKA_TRUST_SERVER_AUTH, NULL, 0}
   887         -	                          }, *curr_attr;
   888         -	CK_ULONG curr_attr_idx;
   889         -	CK_ULONG byte_idx;
   890         -	CK_OBJECT_CLASS objectClass;
   891         -	CK_BYTE signature[1024];
   892         -	CK_ULONG signature_len;
   893         -	CK_MECHANISM mechanism = {CKM_RSA_PKCS, NULL, 0};
   894         -	CK_RV chk_rv;
   895         -	ssize_t read_ret;
   896         -	char *fileName = NULL;
   897         -	int fd;
   898         -	int i;
   899         -	int initialized = 0;
   900         -	int retval = 1;
   901         -
   902         -	fileName = argv[1];
   903         -	if (fileName == NULL) {
   904         -		goto cleanup;
   905         -	}
   906         -
   907         -	fd = open(fileName, O_RDONLY);
   908         -	if (fd < 0) {
   909         -		goto cleanup;
   910         -	}
   911         -
   912         -	inputDataLen = 16384;
   913         -	inputData = malloc(inputDataLen);
   914         -
   915         -	read_ret = read(fd, inputData, inputDataLen);
   916         -	if (read_ret < 0) {
   917         -		goto cleanup;
   918         -	}
   919         -
   920         -	inputDataLen = read_ret;
   921         -	inputData = realloc(inputData, inputDataLen);
   922         -
   923         -	close(fd);
   924         -
   925         -	chk_rv = C_GetFunctionList(&pFunctionList);
   926         -	if (chk_rv != CKR_OK) {
   927         -		printf("C_GetFunctionList() failed.");
   928         -
   929         -		goto cleanup;
   930         -	}
   931         -
   932         -	C_CloseSession = pFunctionList->C_CloseSession;
   933         -	C_Finalize = pFunctionList->C_Finalize;
   934         -	C_FindObjects = pFunctionList->C_FindObjects;
   935         -	C_FindObjectsFinal = pFunctionList->C_FindObjectsFinal;
   936         -	C_FindObjectsInit = pFunctionList->C_FindObjectsInit;
   937         -	C_GetAttributeValue = pFunctionList->C_GetAttributeValue;
   938         -	C_GetSlotList = pFunctionList->C_GetSlotList;
   939         -	C_GetTokenInfo = pFunctionList->C_GetTokenInfo;
   940         -	C_Initialize = pFunctionList->C_Initialize;
   941         -	C_Login = pFunctionList->C_Login;
   942         -	C_OpenSession = pFunctionList->C_OpenSession;
   943         -	C_Sign = pFunctionList->C_Sign;
   944         -	C_SignInit = pFunctionList->C_SignInit;
   945         -
   946         -	privateKeyObjects = malloc(sizeof(*privateKeyObjects) * 1024);
   947         -	privateKeyObjects_root = privateKeyObjects;
   948         -	for (i = 0; i < 1024; i++) {
   949         -		privateKeyObjects[i] = CK_INVALID_HANDLE;
   950         -	}
   951         -
   952         -	initargs.CreateMutex = NULL;
   953         -	initargs.DestroyMutex = NULL;
   954         -	initargs.LockMutex = NULL;
   955         -	initargs.UnlockMutex = NULL;
   956         -	initargs.flags = CKF_OS_LOCKING_OK;
   957         -	initargs.pReserved = NULL;
   958         -
   959         -	chk_rv = C_Initialize(&initargs);
   960         -	if (chk_rv != CKR_OK) {
   961         -		initargs.CreateMutex = NULL;
   962         -		initargs.DestroyMutex = NULL;
   963         -		initargs.LockMutex = NULL;
   964         -		initargs.UnlockMutex = NULL;
   965         -		initargs.flags = 0;
   966         -		initargs.pReserved = NULL;
   967         -
   968         -		chk_rv = C_Initialize(&initargs);
   969         -		if (chk_rv != CKR_OK) {
   970         -			printf("C_Initialize() failed.");
   971         -
   972         -			goto cleanup;
   973         -		}
   974         -	}
   975         -
   976         -	initialized = 1;
   977         -
   978         -	chk_rv = C_GetSlotList(FALSE, NULL, &numSlots);
   979         -	if (chk_rv != CKR_OK) {
   980         -		goto cleanup;
   981         -	}
   982         -
   983         -	if (numSlots == 0) {
   984         -		goto cleanup;
   985         -	}
   986         -
   987         -	slots = malloc(sizeof(*slots) * numSlots);
   988         -
   989         -	if (slots == NULL) {
   990         -		goto cleanup;
   991         -	}
   992         -
   993         -	chk_rv = C_GetSlotList(FALSE, slots, &numSlots);
   994         -	if (chk_rv != CKR_OK) {
   995         -		goto cleanup;
   996         -	}
   997         -
   998         -	chk_rv = C_OpenSession(slots[0], CKF_SERIAL_SESSION, NULL, NULL, &hSession);
   999         -	if (chk_rv != CKR_OK) {
  1000         -		goto cleanup;
  1001         -	}
  1002         -
  1003         -	chk_rv = C_GetTokenInfo(slots[0], &tokenInfo);
  1004         -	if (chk_rv != CKR_OK) {
  1005         -		goto cleanup;
  1006         -	}
  1007         -
  1008         -	if ((tokenInfo.flags & CKF_LOGIN_REQUIRED) == CKF_LOGIN_REQUIRED && (tokenInfo.flags & CKF_PROTECTED_AUTHENTICATION_PATH) == 0) {
  1009         -		printf("Unable to login to card.\n");
  1010         -
  1011         -		goto cleanup;
  1012         -	}
  1013         -
  1014         -	chk_rv = C_Login(hSession, CKU_USER, NULL, 0);
  1015         -	if (chk_rv != CKR_OK) {
  1016         -		printf("Login to device failed.\n");
  1017         -
  1018         -		goto cleanup;
  1019         -	}
  1020         -
  1021         -	chk_rv = C_FindObjectsInit(hSession, NULL, 0);
  1022         -	if (chk_rv != CKR_OK) {
  1023         -		goto cleanup;
  1024         -	}
  1025         -
  1026         -	while (1) {
  1027         -		chk_rv = C_FindObjects(hSession, &hObject, 1, &ulObjectCount);
  1028         -		if (chk_rv != CKR_OK) {
  1029         -			printf("FindObjects() failed.\n");
  1030         -			break;
  1031         -		}
  1032         -
  1033         -		if (ulObjectCount == 0) {
  1034         -			break;
  1035         -		}
  1036         -
  1037         -		if (ulObjectCount != 1) {
  1038         -			printf("FindObjects() returned a weird number of objects.  Asked for 1, got %lu.\n", ulObjectCount);
  1039         -			break;
  1040         -		}
  1041         -
  1042         -		for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
  1043         -			curr_attr = &template[curr_attr_idx];
  1044         -			if (curr_attr->pValue) {
  1045         -				free(curr_attr->pValue);
  1046         -			}
  1047         -
  1048         -			curr_attr->pValue = NULL;
  1049         -		}
  1050         -
  1051         -		chk_rv = C_GetAttributeValue(hSession, hObject, &template[0], sizeof(template) / sizeof(template[0]));
  1052         -		if (chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_BUFFER_TOO_SMALL) {
  1053         -			chk_rv = CKR_OK;
  1054         -		}
  1055         -
  1056         -		if (chk_rv != CKR_OK) {
  1057         -			continue;
  1058         -		}
  1059         -
  1060         -		for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
  1061         -			curr_attr = &template[curr_attr_idx];
  1062         -
  1063         -			if (((CK_LONG) curr_attr->ulValueLen) != ((CK_LONG) -1)) {
  1064         -				curr_attr->pValue = malloc(curr_attr->ulValueLen);
  1065         -			}
  1066         -		}
  1067         -
  1068         -		chk_rv = C_GetAttributeValue(hSession, hObject, &template[0], sizeof(template) / sizeof(template[0]));
  1069         -		if (chk_rv == CKR_OK || chk_rv == CKR_ATTRIBUTE_SENSITIVE || chk_rv == CKR_ATTRIBUTE_TYPE_INVALID || chk_rv == CKR_BUFFER_TOO_SMALL) {
  1070         -			for (curr_attr_idx = 0; curr_attr_idx < (sizeof(template) / sizeof(template[0])); curr_attr_idx++) {
  1071         -				curr_attr = &template[curr_attr_idx];
  1072         -
  1073         -				if (curr_attr->pValue) {
  1074         -					switch (curr_attr->type) {
  1075         -						case CKA_CLASS:
  1076         -							objectClass = *((CK_OBJECT_CLASS *) curr_attr->pValue);
  1077         -
  1078         -							if (objectClass == CKO_PRIVATE_KEY) {
  1079         -								*privateKeyObjects = hObject;
  1080         -								privateKeyObjects++;
  1081         -							}
  1082         -					}
  1083         -				} else {
  1084         -					free(curr_attr->pValue);
  1085         -					curr_attr->pValue = NULL;
  1086         -				}
  1087         -			}
  1088         -		} else {
  1089         -			printf("GetAttributeValue(hObject=%lu)/1 failed (rv = %lu).\n", (unsigned long) hObject, (unsigned long) chk_rv);
  1090         -		}
  1091         -
  1092         -	}
  1093         -
  1094         -	chk_rv = C_FindObjectsFinal(hSession);
  1095         -	if (chk_rv != CKR_OK) {
  1096         -		printf("FindObjectsFinal() failed.\n");
  1097         -	}
  1098         -
  1099         -	for (currPrivKey = privateKeyObjects_root; *currPrivKey != CK_INVALID_HANDLE; currPrivKey++) {
  1100         -		chk_rv = C_SignInit(hSession, &mechanism, *currPrivKey);
  1101         -		if (chk_rv == CKR_OK) {
  1102         -			signature_len = sizeof(signature);
  1103         -
  1104         -			chk_rv = C_Sign(hSession, (CK_BYTE_PTR) "Test", 4, (CK_BYTE_PTR) &signature, &signature_len);
  1105         -			if (chk_rv == CKR_OK) {
  1106         -				printf("[%04lu/%02lx] Signature: ", (unsigned long) *currPrivKey, (unsigned long) mechanism.mechanism);
  1107         -
  1108         -				for (byte_idx = 0; byte_idx < signature_len; byte_idx++) {
  1109         -					printf("%02x ", (unsigned int) signature[byte_idx]);
  1110         -				}
  1111         -
  1112         -				printf("\n");
  1113         -			} else {
  1114         -				printf("Sign() failed.\n");
  1115         -			}
  1116         -		} else {
  1117         -			printf("SignInit() failed.\n");
  1118         -		}
  1119         -	}
  1120         -
  1121         -	chk_rv = C_CloseSession(hSession);
  1122         -	if (chk_rv != CKR_OK) {
  1123         -		printf("CloseSession failed.\n");
  1124         -	}
  1125         -
  1126         -	retval = 0;
  1127         -
  1128         -cleanup:
  1129         -	if (initialized) {
  1130         -		C_Finalize(NULL);
  1131         -	}
  1132         -
  1133         -	if (slots) {
  1134         -		free(slots);
  1135         -	}
  1136         -
  1137         -	if (privateKeyObjects_root) {
  1138         -		free(privateKeyObjects_root);
  1139         -	}
  1140         -
  1141         -	return(retval);
  1142         -}
  1143         -#endif
          875  +}