static int testStatus(SCARDCONTEXT ctx, const char *readerName, const char *readerList) { SCARDHANDLE hCard; DWORD pcchReaderLen = 2048; //can this be calculated beforehand!? DWORD pdwState; DWORD pdwProtocol; BYTE pbAtr[32]; DWORD pcbAtrLen = 32; char readerName2[2048]; int returnValue = 0; long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &pdwProtocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { delayMS(200); // Normal case: fail position ----1 ret = SCardStatusA(hCard, readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 1; // ReaderLen = 0: fail position ---1- pcchReaderLen = NULL; readerName2[0] = '\0'; //make sure refreshed with new values ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 2; // ReaderLen = 1: fail position ---1-- pcchReaderLen = 1; readerName2[0] = '\0'; //make sure refreshed with new values ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); CHECK_PCSC_RET_PASS(0x4) //AtrLen = 0: fail position --1--- pcchReaderLen = 2048; pcbAtrLen = 0; ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 8; //AtrLen = 1: fail position -1---- pcbAtrLen = 1; ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 16; //AtrLen = 33 (bigger than returned): fail position 1----- pcbAtrLen = 33; ret = SCardStatusA(hCard, (LPSTR) readerName2, &pcchReaderLen, &pdwState, &pdwProtocol, (LPBYTE) &pbAtr, &pcbAtrLen); if (ret != SCARD_S_SUCCESS) returnValue |= 32; } return returnValue; }
static long sendAPDUS(SCARDCONTEXT ctx, const char *readerName, int apduCount, const char **apdus) { SCARDHANDLE hCard; DWORD protocol; char debugString[2048]; printf("Using reader \"%s\"\n\n", readerName); sprintf_s(debugString, 2047, "Using reader \"%s\"\n\n", readerName); DebugMessage(debugString); long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { unsigned char recvBuf[258]; DWORD recvBufLen; for (int i = 0; i < apduCount; i++) { recvBufLen = (DWORD) sizeof(recvBuf); sendAPDU(hCard, apdus[i], recvBuf, &recvBufLen, NULL, 0, true); } ret = SCardDisconnect(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardDisconnect", ret); } return 0; }
static UINT32 smartcard_ConnectA_Call(SMARTCARD_DEVICE* smartcard, SMARTCARD_OPERATION* operation, ConnectA_Call* call) { LONG status; SCARDHANDLE hCard; Connect_Return ret; IRP* irp = operation->irp; if ((call->Common.dwPreferredProtocols == SCARD_PROTOCOL_UNDEFINED) && (call->Common.dwShareMode != SCARD_SHARE_DIRECT)) { call->Common.dwPreferredProtocols = SCARD_PROTOCOL_Tx; } status = ret.ReturnCode = SCardConnectA(operation->hContext, (char*) call->szReader, call->Common.dwShareMode, call->Common.dwPreferredProtocols, &hCard, &ret.dwActiveProtocol); smartcard_scard_context_native_to_redir(smartcard, &(ret.hContext), operation->hContext); smartcard_scard_handle_native_to_redir(smartcard, &(ret.hCard), hCard); smartcard_trace_connect_return(smartcard, &ret); if (status) return status; status = smartcard_pack_connect_return(smartcard, irp->output, &ret); if (status) return status; if (call->szReader) free(call->szReader); return ret.ReturnCode; }
static int testCardFunctionality(SCARDCONTEXT ctx, const char *readerName) { SCARDHANDLE hCard; DWORD protocol; int errCount = 0; printf("Using reader \"%s\"\n\n", readerName); long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { delayMS(200); printf("--- Get Response tests ---\n"); errCount += testGetResponse(hCard); printf("\n--- Get Response tests within a transaction ---\n"); ret = SCardBeginTransaction(hCard); CHECK_PCSC_RET("SCardBeginTransaction", ret); errCount += testGetResponse(hCard); ret = SCardEndTransaction(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardBeginTransaction", ret); printf("\n--- Test reading a long file ---\n"); errCount += testLongFileRead(hCard); printf("\n--- Test reading a long file within a transaction ---\n"); ret = SCardBeginTransaction(hCard); CHECK_PCSC_RET("SCardBeginTransaction", ret); errCount += testLongFileRead(hCard); ret = SCardEndTransaction(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardBeginTransaction", ret); printf("\n--- Misc tests ---\n"); errCount += testMisc(hCard); ret = SCardDisconnect(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardDisconnect", ret); } if (errCount == 0) printf("\nFunctional tests done, no errors\n"); else printf("\nFunctional tests done, %d errors\n", errCount); return 0; }
static int testTransaction(SCARDCONTEXT ctx, const char *readerName) { int returnValue = 0; SCARDHANDLE hCard; DWORD protocol; long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET_PASS(0x1) //-------1 if (SCARD_S_SUCCESS == ret) { delayMS(200); ret = SCardBeginTransaction(hCard); CHECK_PCSC_RET_PASS(0x2) //------1- ret = SCardEndTransaction(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET_PASS(0x2) //-----1-- ret = SCardEndTransaction(1111, SCARD_LEAVE_CARD); CHECK_PCSC_RET_FAIL(0x2) //-----1-- }
void SmartCardReader::connect( ) { DWORD Protocol = 1; char buffer1[100]; char buffer2[100]; const char * tempstr = readerLabel.c_str() ; char * cReaderName = new char[ 10 + 1 ]; strcpy( cReaderName , tempstr ) ; //Connect to selected reader retCode = SCardConnectA( context, cReaderName , SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0|SCARD_PROTOCOL_T1, &card, &dwActProtocol ); if( retCode != SCARD_S_SUCCESS ) { //Failed to connect to reader ofLogError ( "SmartCardReader::connect() SMART CARD CONNECT FAILURE" ) << getSCardErrorMessage( retCode ) ; //DisplayOut( GetScardErrMsg( retCode ), RED ); return; } else { ofLogNotice( " SmartCardReader::connect() Connection to " + readerLabel + " successful." ) ; } //Successful connection to reader // IO_REQ.dwProtocol = Protocol; // IO_REQ.cbPciLength = sizeof( SCARD_IO_REQUEST ); //cbReader.GetLBText( cbReader.GetCurSel(), buffer2 ); //sprintf( buffer1, "%s %s \n", "Successful connection to ", buffer2 ); }
static int testConnect(SCARDCONTEXT ctx, const char *readerName) { int returnValue = 0; SCARDHANDLE hCard; DWORD protocol; long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); if (SCARD_S_SUCCESS != ret) returnValue |= 0x1; //-------1 if (SCARD_S_SUCCESS == ret) { delayMS(200); unsigned char recvBuf[258]; DWORD recvBufLen; recvBufLen = (DWORD) sizeof(recvBuf); sendAPDU(hCard, "00:A4:04:0C:0C:A0:00:00:01:77:50:4B:43:53:2D:31:35", recvBuf, &recvBufLen, NULL, 0, true); if (!(recvBufLen == 2)) returnValue |= 0x2; //------1- if (!(recvBuf[0] == 0x90 && recvBuf[1] == 0x00)) returnValue |= 0x2; //------1- ret = SCardDisconnect(hCard, SCARD_LEAVE_CARD); if (SCARD_S_SUCCESS != ret) returnValue |= 0x4; //-----1-- ret = SCardDisconnect(1111, SCARD_LEAVE_CARD); if (SCARD_S_SUCCESS == ret) returnValue |= 0x4; //-----1-- } return returnValue; }
bool CPCSCClass::Connect() { char ReaderName[100]; int ReaderSelected = 0; if(m_cbListObj == NULL) { mylog.AddToLogs(m_logobj,"error: List readers first"); return false; } ReaderSelected = m_cbListObj->GetCurSel(); m_cbListObj->GetLBText (ReaderSelected, ReaderName); Protocol = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1; RetCode = SCardConnectA( hContext, ReaderName, SCARD_SHARE_EXCLUSIVE, Protocol, &hCard, &Protocol); if (RetCode != SCARD_S_SUCCESS) { mylog.AddToLogs(m_logobj,"error: SCardConnectA failed"); mylog.AddToLogs(m_logobj,TranslatePCSCError()); return false; } IO_REQ.dwProtocol = Protocol; IO_REQ.cbPciLength = sizeof(SCARD_IO_REQUEST); mylog.AddToLogs(m_logobj,"SCardConnectA Success"); return true; }
static std:: vector<BYTE> sendAPDU(const char *apdu) { SCARDHANDLE hCard; unsigned char *recvBuf; //char debugString[2048]; unsigned char sendBuf[280]; size_t sendBufLen = sizeof(sendBuf); std:: vector<BYTE> result; SCARDCONTEXT ctx; const char *readerName; long ret = SCardEstablishContext(SCARD_SCOPE_USER, NULL, NULL, &ctx); CHECK_PCSC_RET("SCardEstablishContext", ret); if (SCARD_S_SUCCESS == ret) { char readerList[2000]; readerName = listReaders(ctx, readerList, sizeof(readerList), true, -1); DWORD protocol; char *chipnrForCompare = NULL; long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { DWORD recvBufLen = sizeof(recvBuf); unsigned char recvBuf[258]; // If chipnrForCompare == NULL then we retrieve the chip number // of this card to store all APDUs const char *thisChipNr = NULL; char chipNrBuf[2 * CHIP_NR_LEN + 1]; if (NULL == chipnrForCompare) { thisChipNr = GetChipNrFromCard(hCard, chipNrBuf); if (NULL == thisChipNr) { SCardDisconnect(hCard, SCARD_LEAVE_CARD); //return -1; } } //if (NULL == chipnrForCompare) recvBufLen = (DWORD) sizeof(recvBuf); //0x00000102 if (0 == hex2bin(apdu, sendBuf, &sendBufLen)) { // Check the APDU if (sendBufLen < 4) { printf("ERR: APDU should be at least 4 bytes\n"); DebugMessage("ERR: APDU should be at least 4 bytes\n"); } else if (sendBufLen > 5 && (size_t) (5 + sendBuf[4]) != sendBufLen) { printf("ERR: wrong P3 byte in case 3 APDU\n"); DebugMessage("ERR: wrong P3 byte in case 3 APDU\n"); } else { long ret = SCardTransmit2(hCard, &g_rgSCardT0Pci, sendBuf, (DWORD) sendBufLen, NULL, recvBuf, &recvBufLen); if (SCARD_S_SUCCESS == ret) { for (int i = 0; i < (int) recvBufLen; i++) { result.push_back(recvBuf[i]); } return result; // success } } } } } return result; // failed }
static int testAPDUS(SCARDCONTEXT ctx, const char *readerName, const char *chipnrForCompare) { SCARDHANDLE hCard; DWORD protocol; int err = 0; int errCount = 0; printf("Using reader \"%s\"\n\n", readerName); long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { delayMS(200); unsigned char recvBuf[258]; DWORD recvBufLen = sizeof(recvBuf); // If chipnrForCompare == NULL then we retrieve the chip number // of this card to store all APDUs const char *thisChipNr = NULL; char chipNrBuf[2 * CHIP_NR_LEN + 1]; if (NULL == chipnrForCompare) { thisChipNr = GetChipNrFromCard(hCard, chipNrBuf); if (NULL == thisChipNr) { SCardDisconnect(hCard, SCARD_LEAVE_CARD); return -1; } } // Send all APDUs for (int i = 0; fixedTestAPDUS[i] != NULL; i++) { recvBufLen = (DWORD) sizeof(recvBuf); ret = sendAPDU(hCard, fixedTestAPDUS[i], recvBuf, &recvBufLen, thisChipNr, i, NULL == chipnrForCompare); if (0 == ret && NULL != chipnrForCompare) { err = compareAPDUS(fixedTestAPDUS[i], recvBuf, recvBufLen, chipnrForCompare, i); if (err < 0) break; errCount += err; } } ret = SCardDisconnect(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardDisconnect", ret); if (NULL == chipnrForCompare) printf("Done, stored APDUs to files who's names start with %s\n", thisChipNr); else if (err >= 0) printf("APDU Tests done, %d differences with chip %s)\n", errCount, chipnrForCompare); } return 0; }
static int testCardFunctionality(SCARDCONTEXT ctx, const char *readerName) { SCARDHANDLE hCard; DWORD protocol; int errCount = 0; printf("Using reader \"%s\"\n\n", readerName); printf("NOTE: make sure no-one else is accessing the card!\n\n"); long ret = SCardConnectA(ctx, readerName, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0, &hCard, &protocol); CHECK_PCSC_RET("SCardConnect", ret); if (SCARD_S_SUCCESS == ret) { delayMS(200); printf("--- SCardReconnect tests ---\n"); errCount += testScardReconnect(hCard, SCARD_SHARE_SHARED, SCARD_PROTOCOL_T0); #ifdef _WIN32 printf("\n--- SCardState tests ---\n"); errCount += testSCardState(hCard); #endif printf("\n--- SCardStatus tests ---\n"); errCount += testSCardStatus(hCard); #ifndef MAC_OS_X printf("\n--- SCardGetAttrib tests ---\n"); errCount += testSCardGetAttrib(ctx, hCard); #endif printf("\n--- Get Response tests ---\n"); errCount += testGetResponse(hCard); printf("\n--- Get Response tests within a transaction ---\n"); ret = SCardBeginTransaction(hCard); CHECK_PCSC_RET("SCardBeginTransaction", ret); errCount += testGetResponse(hCard); ret = SCardEndTransaction(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardBeginTransaction", ret); printf("\n--- Test reading a long file ---\n"); errCount += testLongFileRead(hCard); printf("\n--- Test reading a long file within a transaction ---\n"); ret = SCardBeginTransaction(hCard); CHECK_PCSC_RET("SCardBeginTransaction", ret); errCount += testLongFileRead(hCard); ret = SCardEndTransaction(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardBeginTransaction", ret); printf("\n--- Misc tests ---\n"); errCount += testMisc(hCard); printf("\n--- Test ListReaders ---\n"); errCount += testListReaders(ctx); ret = SCardDisconnect(hCard, SCARD_LEAVE_CARD); CHECK_PCSC_RET("SCardDisconnect", ret); } if (errCount == 0) printf("\nFunctional tests done, no errors\n"); else printf("\nFunctional tests done, %d errors\n", errCount); return 0; }