コード例 #1
0
int EstEID_tokensChanged() {
  LOG_LOCATION;
	CK_SLOT_ID slotID;
	int changed = FALSE;
	while (fl->C_WaitForSlotEvent(CKF_DONT_BLOCK, &slotID, NULL_PTR) == CKR_OK) {
    EstEID_log("C_WaitForSlotEvent() pass cycle 1");
		changed = TRUE;
	}
	if (!changed) {
		while (fl->C_WaitForSlotEvent(CKF_DONT_BLOCK, &slotID, NULL_PTR) == CKR_OK) {
      EstEID_log("C_WaitForSlotEvent() pass cycle 2");
			changed = TRUE;
		}
	}  
  EstEID_log("tokens change %sdetected", changed ? "" : "not ");
	return changed;
}
コード例 #2
0
ファイル: carddata.c プロジェクト: Andhr3y/dcfd-mw-applet
CK_RV WaitForCardEvent(HWND hTextEdit, CK_FUNCTION_LIST_PTR functions, DWORD *pAutoFlags)
{
	CK_RV retVal = CKR_OK; 
	CK_FLAGS flags = 0;
	CK_SLOT_ID slotID;
	CK_SLOT_INFO slotInfo;
	CK_TOKEN_INFO tokenInfo;
	CK_ULONG ulPreviousCount = 0;
	CK_ULONG ulCount = 0;
	CK_ULONG ulCounter = 0;
	CK_SLOT_ID_PTR pSlotList;
	CK_ULONG_PTR pCardPresentList;
	//CK_BYTE_PTR pserialNumberList;
	//CK_ULONG_PTR pserialNumberLenList;
								//CK_ULONG certContextLen = 5;
	PCCERT_CONTEXT**	pCertContextArray;

	retVal = functions->C_GetSlotList(FALSE, NULL_PTR, &ulCount);
	ulPreviousCount = ulCount;
	pSlotList = (CK_SLOT_ID_PTR) malloc(ulCount*sizeof(CK_SLOT_ID));
	pCardPresentList = (CK_ULONG_PTR)malloc(ulCount*sizeof(CK_SLOT_ID));
	//pserialNumberList = (CK_BYTE*)malloc(ulCount*sizeof(CK_BYTE));
	//memset(pserialNumberList,0,ulCount*sizeof(CK_BYTE));
	//pserialNumberLenList = (CK_ULONG*)malloc(ulCount*sizeof(CK_ULONG));
	//allocate space for the array of certificate pointers per slot
	pCertContextArray = malloc(ulCount*sizeof(PCCERT_CONTEXT*));
	//initialize with zero's
	memset(pCertContextArray,0,ulCount*sizeof(PCCERT_CONTEXT*));

	if((pSlotList != NULL) &&(pCardPresentList != NULL) &&(pCertContextArray != NULL))
	{
		//retVal = functions->C_GetSlotList(FALSE, NULL_PTR, &ulCount);
		//if(retVal != CKR_OK){}
		if ((retVal == CKR_OK) && (ulCount > 0)) {
			SendMessage(hTextEdit, EM_REPLACESEL, 0, (LPARAM)"Readers found: \r\n");
			retVal = functions->C_GetSlotList(FALSE, pSlotList, &ulCount);
			if(retVal != CKR_OK){}

			//ulCount readers found, now fill in the certificate context array,
			//and register all certificates found
			while(ulCounter < ulCount)
			{
				/* Get slot information*/
				retVal = functions->C_GetSlotInfo(pSlotList[ulCounter], &slotInfo);
				if(retVal != CKR_OK){}
				else
				{
					slotInfo.slotDescription[63] = 0;
					//do not display the PnP slot
					if(strncmp(slotInfo.slotDescription,"\\\\?PnP?\\Notification",20) != 0)
					{
						SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"- ");
						SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)slotInfo.slotDescription);
						//SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"\r\n");

						/* Get token information*/
						retVal = functions->C_GetTokenInfo(pSlotList[ulCounter], &tokenInfo);
						if (retVal == CKR_TOKEN_NOT_PRESENT) {
							pCardPresentList[ulCounter] = 0;
							SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"  No Card Found \r\n");
						}
						else if (retVal==CKR_OK)
						{
							pCardPresentList[ulCounter] = 1;
							tokenInfo.label[31]=0;
							SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"  Card Found: ");
							SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)tokenInfo.label);
							SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"\r\n");
							//allocate space for 5 certificate context pointers
							pCertContextArray[ulCounter] = malloc (5*sizeof(PCCERT_CONTEXT));
							memset(pCertContextArray[ulCounter],0,5*sizeof(PCCERT_CONTEXT));
							if(pCertContextArray[ulCounter] != NULL)
							{
								memset(pCertContextArray[ulCounter],0,5*sizeof(PCCERT_CONTEXT));
								if(*pAutoFlags & AUTO_REGISTER)
									retVal = HandleNewCardFound(hTextEdit, functions, ulCounter, pSlotList,pCertContextArray[ulCounter], 5);						
							}
							else
							{
								SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"ERROR:  Out of memory\r\n");
							}
						}
					}
				}
				ulCounter++;
			}//end of while
		}

		//as long as the readercount didn't change; keep the current slotlist
		//TODO: match the entire slotList, not just checking its size
		while(ulCount == ulPreviousCount)
		{
			/* Block and wait for a slot event */
			retVal = functions->C_WaitForSlotEvent(flags, &slotID, NULL_PTR);
			if(retVal != CKR_OK)
			{
				SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"WARNING: C_WaitForSlotEvent returned an error \r\n");
				return retVal;

				//	printError()
				//		char errormessage[100];
				//		_snprintf(errormessage,100,"C_GetAttributeValue returned 0x%0.8x\r\n",retval);
				//		SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"\r\n");
			}
			/* Check if we already know that slot */
			retVal = functions->C_GetSlotInfo(slotID, &slotInfo);
			if(retVal != CKR_OK){}

			ulCounter = 0;
			while(ulCounter < ulCount)
			{
				if(pSlotList[ulCounter] == slotID)
				{
					// Get token information
					retVal = functions->C_GetTokenInfo(slotID, &tokenInfo);
					if( (retVal == CKR_TOKEN_NOT_PRESENT) && (pCardPresentList[ulCounter] == 1) )
					{
						SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"Card removed\r\n");
						pCardPresentList[ulCounter] = 0;
						//token removed, so remove its certificates
						if(*pAutoFlags & AUTO_REMOVE)
							retVal = HandleCardRemoved(hTextEdit, functions, pCertContextArray[ulCounter], 5);
						//free the allocated space of the certificate context pointers
						if(pCertContextArray[ulCounter] != NULL)
							free (pCertContextArray[ulCounter]);
					}
					else
					{
						if(pCardPresentList[ulCounter] == 0)
						{
							SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"Card inserted\r\n");
							pCardPresentList[ulCounter] = 1;
							//allocate space for 5 certificate context pointers
							pCertContextArray[ulCounter] = malloc (5*sizeof(PCCERT_CONTEXT));
							if(pCertContextArray[ulCounter] != NULL)
							{
								memset(pCertContextArray[ulCounter],0,5*sizeof(PCCERT_CONTEXT));
								//token added, so add its certificates
								if(*pAutoFlags & AUTO_REGISTER)
									retVal = HandleNewCardFound(hTextEdit, functions, ulCounter, pSlotList,
									pCertContextArray[ulCounter], 5);
							}
							else
							{
								SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"ERROR:  Out of memory\r\n");
							}
						}					
					}			
					break;
				}
				ulCounter++;
			}

			if(ulCounter == ulCount)
			{
				//a new reader is detected
				SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"New reader detected \r\n");
				// Get token information
				//				retVal = functions->C_GetTokenInfo(slotID, &tokenInfo);
				//				if (retVal == CKR_TOKEN_NOT_PRESENT) 
				//				{
				//					SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"  No Card Found \r\n");
				//				}
				//				else
				//				{
				//					SendMessage(hTextEdit, EM_REPLACESEL,0,  (LPARAM)"Card Found: \r\n");
				//				}
			}
			retVal = functions->C_GetSlotList(FALSE, NULL_PTR, &ulCount);
		}// end of while
		if(pSlotList != NULL)
			free(pSlotList);
		if(pCardPresentList != NULL)
			free(pCardPresentList);

		if(pCertContextArray != NULL)
		{
			for(ulCounter = 0;ulCounter < ulPreviousCount;ulCounter++ )
			{
				if(pCertContextArray[ulCounter] != NULL)
				{
					free(pCertContextArray[ulCounter]);
					pCertContextArray[ulCounter] = NULL;
				}
			}
			free(pCertContextArray);
		}
	}
	return retVal;
}
コード例 #3
0
void EstEID_waitForSlotEvent() {
	CK_SLOT_ID slotID = 0;
	CK_RV result = fl->C_WaitForSlotEvent(0, &slotID, NULL_PTR);
	printf("result: %s, slotID: %lu\n", pkcs11_error_message(result), slotID);
}