1 
2 /* ****************************************************************************
3 
4 * eID Middleware Project.
5 * Copyright (C) 2009-2010 FedICT.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License version
9 * 3.0 as published by the Free Software Foundation.
10 *
11 * This software is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this software; if not, see
18 * http://www.gnu.org/licenses/.
19 
20 **************************************************************************** */
21 
22 #include "base.h"
23 
24 CK_ULONG beidsdk_waitcard(void);
25 
main()26 int main() {
27 	CK_ULONG retval = CKR_OK;
28 	retval = beidsdk_waitcard();
29 
30 	_getch();
31 }
32 
beidsdk_waitcard()33 CK_ULONG beidsdk_waitcard()
34 {
35 	void *pkcs11Handle;									//pkcs11Handle to the pkcs11 library
36 	CK_FUNCTION_LIST_PTR pFunctions;		//list of the pkcs11 function pointers
37 	CK_C_GetFunctionList pC_GetFunctionList;
38 	CK_RV retVal = CKR_OK;
39 	CK_BBOOL cardInserted = CK_FALSE;
40 
41 	pkcs11Handle = dlopen(PKCS11_LIB, RTLD_LAZY); // RTLD_NOW is slower
42 	if (pkcs11Handle != NULL)
43 	{
44 		// get function pointer to C_GetFunctionList
45 		pC_GetFunctionList = (CK_C_GetFunctionList) dlsym(pkcs11Handle, "C_GetFunctionList");
46 		if (pC_GetFunctionList != NULL)
47 		{
48 			// invoke C_GetFunctionList to get the list of pkcs11 function pointers
49 			retVal = (*pC_GetFunctionList) (&pFunctions);
50 			if (retVal == CKR_OK)
51 			{
52 				// initialize Cryptoki
53 				retVal = (pFunctions->C_Initialize) (NULL);
54 				if (retVal == CKR_OK)
55 				{
56 					CK_ULONG slot_count;
57 					// retrieve the number of slots (cardreaders)
58 					// to find also the slots without tokens inserted, set the first parameter to CK_FALSE
59 					retVal = (pFunctions->C_GetSlotList) (CK_FALSE, 0, &slot_count);
60 					if ((retVal == CKR_OK)&& (slot_count > 0))
61 					{
62 						CK_SLOT_ID_PTR slotIds = (CK_SLOT_ID_PTR)malloc(slot_count * sizeof(CK_SLOT_INFO));
63 						if(slotIds != NULL)
64 						{
65 							// retrieve the list of slots (cardreaders)
66 							retVal = (pFunctions->C_GetSlotList) (CK_FALSE, slotIds, &slot_count);
67 							if (retVal == CKR_OK)
68 							{
69 								CK_ULONG slotIdx;
70 								CK_SLOT_INFO slotinfo;
71 								CK_UTF8CHAR slotDescription[65];
72 								//check if a card is already present in one of the readers
73 								for (slotIdx = 0; slotIdx < slot_count; slotIdx++)
74 								{
75 									retVal = (pFunctions->C_GetSlotInfo) (slotIds[slotIdx], &slotinfo);
76 									if ( (retVal == CKR_OK) && (slotinfo.flags & CKF_TOKEN_PRESENT) )
77 									{
78 										memcpy(slotDescription,slotinfo.slotDescription,64);
79 										slotDescription[64] = '\0'; //make the string null terminated
80 										printf("Card found in reader %s \n",slotDescription);
81 
82 										//a card is found in the slot
83 										cardInserted = CK_TRUE;
84 									}
85 								}
86 
87 								if(cardInserted == CK_FALSE)
88 								{
89 									CK_FLAGS flags = 0; //use CKF_DONT_BLOCK if you don't want C_WaitForSlotEvent to block
90 									CK_SLOT_ID slotId;  //will receive the ID of the slot that the event occurred in
91 									printf("Please insert a beid card\n");
92 									retVal = (pFunctions->C_WaitForSlotEvent) (flags,&slotId, NULL_PTR);
93 									if(retVal == CKR_OK)
94 									{
95 										printf("Card inserted \n");
96 										for (slotIdx = 0; slotIdx < slot_count; slotIdx++)
97 										{
98 											if(slotId == slotIds[slotIdx])
99 											{
100 												retVal = (pFunctions->C_GetSlotInfo) (slotId, &slotinfo);
101 												if(retVal == CKR_OK)
102 												{
103 													memcpy(slotDescription,slotinfo.slotDescription,64);
104 													slotDescription[64] = '\0'; //make the string null terminated
105 													printf("into reader %s \n",slotDescription);
106 												}
107 											}
108 										}
109 									}
110 								}
111 							}
112 							free(slotIds);
113 						}
114 						else //malloc failed
115 						{
116 							printf("malloc failed\n");
117 							retVal = CKR_GENERAL_ERROR;
118 						}
119 					}//no slots found
120 					else if (slot_count == 0)
121 					{
122 						printf("no slots found\n");
123 					}
124 					if (retVal == CKR_OK)
125 						retVal = (pFunctions->C_Finalize) (NULL_PTR);
126 					else
127 						(pFunctions->C_Finalize) (NULL_PTR);
128 				}
129 			}
130 		}
131 		dlclose(pkcs11Handle);
132 	}
133 	return retVal;
134 }
135 
136