Ejemplo n.º 1
0
CK_RV GetTheReaderCount(int* nrofCardReaders, int cardsInserted) {

    CK_RV retval = CKR_OK;				//return value of last pkcs11 function called
    CK_FUNCTION_LIST_PTR functions=NULL;		// list of the pkcs11 function pointers

    CK_ULONG slot_count = 0;
    CK_BBOOL tokenPresent = CK_FALSE;

    int err = GetLastError();
    printf("err = %d\n",err);

    SetLastError(0);

    retval=loadpkcs11(&functions);
    if( retval != CKR_OK )
        return retval;

    if( (retval=(functions->C_Initialize) (NULL)) == CKR_OK)
    {
        if(cardsInserted > 0)
        {
            tokenPresent = CK_TRUE;
        }

        retval = (functions->C_GetSlotList) (tokenPresent, 0, &slot_count);
        *nrofCardReaders = slot_count;

        (functions->C_Finalize) (NULL_PTR);
    }

    unloadpkcs11();

    return retval;
}
Ejemplo n.º 2
0
CK_RV ReadTheCardData(void) {

	CK_RV retval = CKR_OK;				//return value of last pkcs11 function called
	CK_FUNCTION_LIST_PTR functions=NULL;		// list of the pkcs11 function pointers

	CK_SESSION_HANDLE session_handle;
	CK_ULONG slot_count;
	CK_SLOT_ID_PTR slotIds;
	CK_ULONG slotIdx;

	int err = GetLastError();
	printf("err = %d\n",err);

	SetLastError(0);

	retval=loadpkcs11(&functions);
	if( retval != CKR_OK )
		return retval;

	if( (retval=(functions->C_Initialize) (NULL)) == CKR_OK)
	{		
		if( (retval=(functions->C_GetSlotList) (CK_TRUE, 0, &slot_count)) == CKR_OK)
		{
			if(slot_count == 0)
			{
				retval = CKR_TOKEN_NOT_PRESENT; 				//no card found -> log this
			}
			slotIds = (CK_SLOT_ID_PTR)malloc(slot_count * sizeof(CK_SLOT_INFO));
			if(slotIds != NULL)
			{
				if( (retval = (functions->C_GetSlotList) (CK_TRUE, slotIds, &slot_count)) == CKR_OK)
				{
					if(slot_count == 0)
					{
						//no card found -> log this
						retval = CKR_TOKEN_NOT_PRESENT;
					}
					for (slotIdx = 0; slotIdx < slot_count; slotIdx++) 
					{
						retval = (functions->C_OpenSession)(slotIds[slotIdx], CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session_handle);
						if(retval == CKR_OK)
						{
							if ((retval = FindAndStore(functions, session_handle, "firstnames",&g_firstNames)) == CKR_OK)
							if ((retval = FindAndStore(functions, session_handle, "first_letter_of_third_given_name",&g_firstLetterThirdName)) == CKR_OK)
							if ((retval = FindAndStore(functions, session_handle, "surname",&g_surName)) == CKR_OK)
							if ((retval = FindAndStore(functions, session_handle, "address_street_and_number",&g_address_Street_Number)) == CKR_OK)
							if ((retval = FindAndStore(functions, session_handle, "address_zip",&g_address_Zip)) == CKR_OK)
							if ((retval = FindAndStore(functions, session_handle, "address_municipality",&g_address_Municipality)) == CKR_OK)
							(functions->C_CloseSession) (session_handle);
						}			
					}
				}
				free(slotIds);
			}
			else 
			{
				retval = CKR_HOST_MEMORY;
			}
		}
		(functions->C_Finalize) (NULL_PTR);
	}

	unloadpkcs11();

	return retval;
}