文件名称:最新PCSC CCID 测试源码
文件大小:1.19MB
文件格式:RAR
更新时间:2014-07-13 03:24:43
PCSC 读卡器 智能卡 CCID USB
最新的PCSC、CCID以及测试源码。 贴一段源码: LONG SCardEstablishContext(DWORD dwScope, /*@unused@*/ LPCVOID pvReserved1, /*@unused@*/ LPCVOID pvReserved2, LPSCARDCONTEXT phContext) { (void)pvReserved1; (void)pvReserved2; if (dwScope != SCARD_SCOPE_USER && dwScope != SCARD_SCOPE_TERMINAL && dwScope != SCARD_SCOPE_SYSTEM && dwScope != SCARD_SCOPE_GLOBAL) { *phContext = 0; return SCARD_E_INVALID_VALUE; } /* * Unique identifier for this server so that it can uniquely be * identified by clients and distinguished from others */ *phContext = (PCSCLITE_SVC_IDENTITY + SYS_RandomInt(1, 65535)); Log2(PCSC_LOG_DEBUG, "Establishing Context: 0x%X", *phContext); return SCARD_S_SUCCESS; } LONG SCardReleaseContext(SCARDCONTEXT hContext) { /* * Nothing to do here RPC layer will handle this */ Log2(PCSC_LOG_DEBUG, "Releasing Context: 0x%X", hContext); return SCARD_S_SUCCESS; } LONG SCardConnect(/*@unused@*/ SCARDCONTEXT hContext, LPCSTR szReader, DWORD dwShareMode, DWORD dwPreferredProtocols, LPSCARDHANDLE phCard, LPDWORD pdwActiveProtocol) { LONG rv; READER_CONTEXT * rContext = NULL; uint32_t readerState; (void)hContext; PROFILE_START *phCard = 0; if ((dwShareMode != SCARD_SHARE_DIRECT) && !(dwPreferredProtocols & SCARD_PROTOCOL_T0) && !(dwPreferredProtocols & SCARD_PROTOCOL_T1) && !(dwPreferredProtocols & SCARD_PROTOCOL_RAW) && !(dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD)) return SCARD_E_PROTO_MISMATCH; if (dwShareMode != SCARD_SHARE_EXCLUSIVE && dwShareMode != SCARD_SHARE_SHARED && dwShareMode != SCARD_SHARE_DIRECT) return SCARD_E_INVALID_VALUE; Log3(PCSC_LOG_DEBUG, "Attempting Connect to %s using protocol: %d", szReader, dwPreferredProtocols); rv = RFReaderInfo((LPSTR) szReader, &rContext); if (rv != SCARD_S_SUCCESS) { Log2(PCSC_LOG_ERROR, "Reader %s Not Found", szReader); return rv; } /* * Make sure the reader is working properly */ rv = RFCheckReaderStatus(rContext); if (rv != SCARD_S_SUCCESS) return rv; /******************************************* * * This section checks for simple errors * *******************************************/ /* * Connect if not exclusive mode */ if (rContext->contexts == PCSCLITE_SHARING_EXCLUSIVE_CONTEXT) { Log1(PCSC_LOG_ERROR, "Error Reader Exclusive"); return SCARD_E_SHARING_VIOLATION; } /* * wait until a possible transaction is finished */ if (rContext->hLockId != 0) { Log1(PCSC_LOG_INFO, "Waiting for release of lock"); while (rContext->hLockId != 0) (void)SYS_USleep(PCSCLITE_LOCK_POLL_RATE); Log1(PCSC_LOG_INFO, "Lock released"); } /******************************************* * * This section tries to determine the * presence of a card or not * *******************************************/ readerState = rContext->readerState->readerState; if (dwShareMode != SCARD_SHARE_DIRECT) { if (!(readerState & SCARD_PRESENT)) { Log1(PCSC_LOG_ERROR, "Card Not Inserted"); return SCARD_E_NO_SMARTCARD; } /* Power on (again) the card if needed */ (void)pthread_mutex_lock(&rContext->powerState_lock); if (POWER_STATE_UNPOWERED == rContext->powerState) { DWORD dwAtrLen; dwAtrLen = sizeof(rContext->readerState->cardAtr); rv = IFDPowerICC(rContext, IFD_POWER_UP, rContext->readerState->cardAtr, &dwAtrLen); rContext->readerState->cardAtrLength = dwAtrLen; if (rv == IFD_SUCCESS) { readerState = SCARD_PRESENT | SCARD_POWERED | SCARD_NEGOTIABLE; Log1(PCSC_LOG_DEBUG, "power up complete."); LogXxd(PCSC_LOG_DEBUG, "Card ATR: ", rContext->readerState->cardAtr, rContext->readerState->cardAtrLength); } else Log3(PCSC_LOG_ERROR, "Error powering up card: %d 0x%04X", rv, rv); } if (! (readerState & SCARD_POWERED)) { Log1(PCSC_LOG_ERROR, "Card Not Powered"); (void)pthread_mutex_unlock(&rContext->powerState_lock); return SCARD_W_UNPOWERED_CARD; } /* the card is now in use */ rContext->powerState = POWER_STATE_INUSE; Log1(PCSC_LOG_DEBUG, "powerState: POWER_STATE_INUSE"); (void)pthread_mutex_unlock(&rContext->powerState_lock); } /******************************************* * * This section tries to decode the ATR * and set up which protocol to use * *******************************************/ if (dwPreferredProtocols & SCARD_PROTOCOL_RAW) rContext->readerState->cardProtocol = SCARD_PROTOCOL_RAW; else { if (dwShareMode != SCARD_SHARE_DIRECT) { /* lock here instead in IFDSetPTS() to lock up to * setting rContext->readerState->cardProtocol */ (void)pthread_mutex_lock(rContext->mMutex); /* the protocol is not yet set (no PPS yet) */ if (SCARD_PROTOCOL_UNDEFINED == rContext->readerState->cardProtocol) { UCHAR ucAvailable, ucDefault; int ret; ucDefault = PHGetDefaultProtocol(rContext->readerState->cardAtr, rContext->readerState->cardAtrLength); ucAvailable = PHGetAvailableProtocols(rContext->readerState->cardAtr, rContext->readerState->cardAtrLength); /* * If it is set to ANY let it do any of the protocols */ if (dwPreferredProtocols & SCARD_PROTOCOL_ANY_OLD) dwPreferredProtocols = SCARD_PROTOCOL_T0 | SCARD_PROTOCOL_T1; ret = PHSetProtocol(rContext, dwPreferredProtocols, ucAvailable, ucDefault); /* keep cardProtocol = SCARD_PROTOCOL_UNDEFINED in case of error */ if (SET_PROTOCOL_PPS_FAILED == ret) { (void)pthread_mutex_unlock(rContext->mMutex); return SCARD_W_UNRESPONSIVE_CARD; } if (SET_PROTOCOL_WRONG_ARGUMENT == ret) { (void)pthread_mutex_unlock(rContext->mMutex); return SCARD_E_PROTO_MISMATCH; } /* use negotiated protocol */ rContext->readerState->cardProtocol = ret; (void)pthread_mutex_unlock(rContext->mMutex); } else { (void)pthread_mutex_unlock(rContext->mMutex); if (! (dwPreferredProtocols & rContext->readerState->cardProtocol)) return SCARD_E_PROTO_MISMATCH; } } } *pdwActiveProtocol = rContext->readerState->cardProtocol; if (dwShareMode != SCARD_SHARE_DIRECT) { switch (*pdwActiveProtocol) { case SCARD_PROTOCOL_T0: case SCARD_PROTOCOL_T1: Log2(PCSC_LOG_DEBUG, "Active Protocol: T=%d", (*pdwActiveProtocol == SCARD_PROTOCOL_T0) ? 0 : 1); break; case SCARD_PROTOCOL_RAW: Log1(PCSC_LOG_DEBUG, "Active Protocol: RAW"); break; default: Log2(PCSC_LOG_ERROR, "Active Protocol: unknown %d", *pdwActiveProtocol); } } else Log1(PCSC_LOG_DEBUG, "Direct access: no protocol selected"); /* * Prepare the SCARDHANDLE identity */ *phCard = RFCreateReaderHandle(rContext); Log2(PCSC_LOG_DEBUG, "hCard Identity: %x", *phCard); /******************************************* * * This section tries to set up the * exclusivity modes. -1 is exclusive * *******************************************/ if (dwShareMode == SCARD_SHARE_EXCLUSIVE) { if (rContext->contexts == PCSCLITE_SHARING_NO_CONTEXT) { rContext->contexts = PCSCLITE_SHARING_EXCLUSIVE_CONTEXT; (void)RFLockSharing(*phCard, rContext); } else { (void)RFDestroyReaderHandle(*phCard); *phCard = 0; return SCARD_E_SHARING_VIOLATION; } } else { /* * Add a connection to the context stack */ rContext->contexts += 1; } /* * Add this handle to the handle list */ rv = RFAddReaderHandle(rContext, *phCard); if (rv != SCARD_S_SUCCESS) { /* * Clean up - there is no more room */ (void)RFDestroyReaderHandle(*phCard); if (rContext->contexts == PCSCLITE_SHARING_EXCLUSIVE_CONTEXT) rContext->contexts = PCSCLITE_SHARING_NO_CONTEXT; else if (rContext->contexts > PCSCLITE_SHARING_NO_CONTEXT) rContext->contexts -= 1; *phCard = 0; PROFILE_END return SCARD_F_INTERNAL_ERROR; } /* * Propagate new state to reader state */ rContext->readerState->readerSharing = rContext->contexts; PROFILE_END return SCARD_S_SUCCESS; }