CardProperty::CardProperty(const char* propertyName) : isPresent(false), propertyLabel(propertyName) { char* pValue = NULL; CK_ULONG valueLength = 0; CardReader* reader = CardReader::GetInstance(); if (!reader) return; int retVal = Beidsdk_GetObjectValue(reader->GetReaderFunctions(), reader->GetSessionHandle(), (CK_CHAR_PTR)propertyName, (CK_VOID_PTR*)&pValue, &valueLength); if (retVal == CKR_OK) { data.resize(valueLength); char* pDataDest = (char*)data.data(); memcpy(pDataDest, pValue, valueLength); isPresent = true; } if (pValue != NULL) free(pValue); }
CK_ULONG beidsdk_GetData() { void *pkcs11Handle; //handle to the pkcs11 library CK_FUNCTION_LIST_PTR pFunctions; //list of the pkcs11 function pointers CK_C_GetFunctionList pC_GetFunctionList; CK_RV retVal = CKR_OK; //open the pkcs11 library pkcs11Handle = dlopen(PKCS11_LIB, RTLD_LAZY); // RTLD_NOW is slower if (pkcs11Handle == NULL) { printf("%s not found\n",PKCS11_LIB); #ifdef WIN32 DWORD err; err = GetLastError(); printf("err is 0x%.8x\n",err); //14001 is "MSVCR80.DLL not found" #else printf("err is %s", dlerror()); #endif return CKR_GENERAL_ERROR; } // get function pointer to C_GetFunctionList pC_GetFunctionList = (CK_C_GetFunctionList)dlsym(pkcs11Handle, "C_GetFunctionList"); if (pC_GetFunctionList == NULL) { retVal = CKR_GENERAL_ERROR; goto out; } // invoke C_GetFunctionList to get the list of pkcs11 function pointers retVal = (*pC_GetFunctionList) (&pFunctions); if (retVal != CKR_OK) { goto out; } // initialize Cryptoki retVal = (pFunctions->C_Initialize) (NULL); if (retVal != CKR_OK) { goto out; } CK_ULONG slot_count = 0; // retrieve the number of slots (cardreaders) found //set first parameter to CK_FALSE if you also want to find the slots without a card inserted retVal = (pFunctions->C_GetSlotList) (CK_TRUE, 0, &slot_count); if (slot_count == 0) { retVal = CKR_GENERAL_ERROR; printf("no slots found\n"); } if (retVal != CKR_OK) { goto finalize; } CK_SLOT_ID_PTR slotIds = (CK_SLOT_ID_PTR)malloc(slot_count * sizeof(CK_SLOT_ID)); if(slotIds == NULL) { printf("malloc failed\n"); retVal = CKR_GENERAL_ERROR; goto finalize; } // Now retrieve the list of slots (cardreaders). retVal = (pFunctions->C_GetSlotList) (CK_TRUE, slotIds, &slot_count); if (retVal != CKR_OK) { goto freeslots; } /* Note: the above should ideally be done in a loop, since the * number of slots reported by C_GetSlotList might increase if * the user inserts a card (or card reader) at exactly the right * moment. See PKCS#11 (pkcs-11v2-11r1.pdf) for details. */ /* Loop over the reported slots and read data from any eID card found */ CK_ULONG slotIdx; for (slotIdx = 0; slotIdx < slot_count; slotIdx++) { CK_SESSION_HANDLE session_handle; //open a session retVal = (pFunctions->C_OpenSession)(slotIds[slotIdx], CKF_SERIAL_SESSION, NULL_PTR, NULL_PTR, &session_handle); if (retVal != CKR_OK) { continue; } /* Prepare some labels for particular bits of data that we want * to read from the card. See sdk/pdf/beid_card_data.pdf (or * .../beid_card_data.odt) for other options (e.g., address, * given name, issuing municipality, ...). */ CK_CHAR_PTR pFilename = (CK_CHAR_PTR)TEXT("carddata_glob_os_version"); CK_CHAR_PTR pSignatureFilename = (CK_CHAR_PTR)TEXT("CARD_DATA"); CK_CHAR_PTR pLastname = (CK_CHAR_PTR)TEXT("surname"); /* * Variables in which to store the actual data */ CK_VOID_PTR pFileValue = NULL; CK_VOID_PTR pSignatureValue = NULL; CK_VOID_PTR pLastnameValue = NULL; CK_ULONG FileValueLen = 0; CK_ULONG SignatureValueLen = 0; CK_ULONG LastnameValueLen = 0; //retrieve the card OS version retVal = Beidsdk_GetObjectValue(pFunctions, session_handle, pFilename, &pFileValue, &FileValueLen); if(retVal == CKR_OK) Beidsdk_PrintValue(pFilename,(CK_BYTE_PTR)pFileValue, FileValueLen); else printf("error 0x%.8x Beidsdk_GetObjectValue\n",retVal); //retrieve the data of the signature file retVal = Beidsdk_GetObjectValue(pFunctions, session_handle, pSignatureFilename, &pSignatureValue, &SignatureValueLen); if(retVal == CKR_OK) Beidsdk_PrintValue(pSignatureFilename,(CK_BYTE_PTR)pSignatureValue, SignatureValueLen); else printf("error 0x%.8x Beidsdk_GetObjectValue\n",retVal); //retrieve the lastname retVal = Beidsdk_GetObjectValue(pFunctions, session_handle, pLastname, &pLastnameValue, &LastnameValueLen); if(retVal == CKR_OK) Beidsdk_PrintValue(pLastname,(CK_BYTE_PTR)pLastnameValue, LastnameValueLen); else printf("error 0x%.8x Beidsdk_GetObjectValue\n",retVal); if (pFileValue != NULL) free (pFileValue); if (pSignatureValue != NULL) free (pSignatureValue); if (pLastnameValue != NULL) free (pLastnameValue); //close the session if (retVal == CKR_OK) retVal = (pFunctions->C_CloseSession) (session_handle); else (pFunctions->C_CloseSession) (session_handle); } freeslots: free(slotIds); finalize: if (retVal == CKR_OK) retVal = (pFunctions->C_Finalize) (NULL_PTR); else (pFunctions->C_Finalize) (NULL_PTR); out: dlclose(pkcs11Handle); return retVal; }