static long TestPteidSendApdu(CReader & oReader) { long lErrors = 0; printf("\nTesting SendAPDU()\n"); // Send a Get Card Data APDU unsigned char tucGetCardData[] = {0x80, 0xE4, 0x00, 0x00, 0x1C}; CByteArray oGetCardData(tucGetCardData, sizeof(tucGetCardData)); CByteArray oData = oReader.SendAPDU(oGetCardData); // Serial nr. are the first 16 bytes of the Get Card Data APDU std::string csSerialNr = oReader.GetSerialNr(); if (csSerialNr != oData.ToString(false, true, 0, 16)) ERR_LOG("SendAPDU(GetCardData) returns wrong data (bad serial nr.)\n", lErrors); // Send a Select MF APDU unsigned char tucSelectMF[] = {0x00, 0xa4, 0x02, 0x0C, 0x02, 0x3f, 0x00}; CByteArray oSelectMF(tucSelectMF, sizeof(tucSelectMF)); oData = oReader.SendAPDU(oSelectMF); // Result should be 90 00 if (oData.Size() != 2 || oData.GetByte(0) != 0x90 || oData.GetByte(1) != 0x00) { ERR_LOG("SendAPDU(GetCardData) returns wrong data (bad serial nr.)\n", lErrors); printf(" (result = %s\n", oData.ToString().c_str()); } return lErrors; }
static long SendAPDUs(std::vector <std::string> & vcsAPDUs) { long lRet = 0; CReader &oReader = g_oCardLayer.getReader(g_csReaderName); if (oReader.Connect()) { for (unsigned int i = 0; i < vcsAPDUs.size(); i++) { CByteArray oRequestAPDU(vcsAPDUs[i], true); printf("\nIN: %s\n", oRequestAPDU.ToString().c_str()); CByteArray oResponseAPDU = oReader.SendAPDU(oRequestAPDU); printf("OUT: %s\n", oResponseAPDU.Size() < 14 ? oResponseAPDU.ToString().c_str(): oResponseAPDU.ToString(true, false).c_str()); } oReader.Disconnect(); } else printf(" No card found in the reader, exiting\n"); return lRet; }
static long GetFile(const std::vector <std::string> & vcsPaths) { long lRet = 0; CReader &oReader = g_oCardLayer.getReader(g_csReaderName); if (oReader.Connect()) { printf("Reader \"%s\":\n", oReader.GetReaderName().c_str()); // Verify PIN if specified lRet = VerifyPIN(oReader, 0); if (lRet == 0) { for (unsigned int i = 0; i < vcsPaths.size(); i++) { const std::string & csPath = vcsPaths[i].c_str(); CByteArray oData = oReader.ReadFile(csPath, 0, FULL_FILE); if (i == 0) printf("\n%s:%s\n", csPath.c_str(), oData.ToString(true, false).c_str()); WriteFile(oReader.GetSerialNr(), csPath, oData); } } oReader.Disconnect(); } else printf(" No card found in the reader, exiting\n"); return lRet; }
static long CardInfo() { CReader &oReader = g_oCardLayer.getReader(g_csReaderName); if (oReader.Connect()) { printf("Reader \"%s\":\n", oReader.GetReaderName().c_str()); CByteArray atr = oReader.GetATR(); printf(" ATR: %s\n", atr.ToString().c_str()); printf(" Type: %s\n", cardtype2string(oReader.GetCardType())); printf(" Serial Nr.: %s\n", oReader.GetSerialNr().c_str()); printf(" Label: %s\n", oReader.GetCardLabel().c_str()); printf("\nRemove/insert card and press ENTER or q to quit\n"); char c = getchar(); while (('q' != c) && (-1 != c)) // -1 means Ctrl-C { tCardStatus status = oReader.Status(true); printf(" - %s\n", Status2String(status)); c = getchar(); } oReader.Disconnect(); } else printf(" No card found in the reader, exiting\n"); return 0; }
static long Hash(tHashAlgo hashAlgo, const CByteArray & oData) { CHash oHash; CByteArray oHashData = oHash.Hash(hashAlgo, oData); printf("Hash: %s\n", oHashData.ToString(true, false).c_str()); WriteFile("", "hash.bin", oHashData); return 0; }
static long Sign(unsigned long signAlgo, unsigned long ulKeyIdx, const CByteArray & oData) { long lRet = 0; CReader &oReader = g_oCardLayer.getReader(g_csReaderName); if (oReader.Connect()) { if (ulKeyIdx >= oReader.PrivKeyCount()) { printf("ERR: pin index (%d) should be less then %d\n", ulKeyIdx, oReader.PrivKeyCount()); lRet = -1; } else { tPrivKey key = oReader.GetPrivKey(ulKeyIdx); printf("Using key \"%s\"\n", key.csLabel.c_str()); // Verify PIN if specified lRet = VerifyPIN(oReader, key.ulAuthID); if (lRet == 0) { CByteArray oSignature; if (signAlgo == SIGN_ALGO_RSA_RAW || signAlgo == SIGN_ALGO_RSA_PKCS) oSignature = oReader.Sign(key, signAlgo, oData); else { oSignature = HashAndSign(oReader, key, signAlgo, oData); oSignature = HashAndSign(oReader, key, signAlgo, oData); } printf("Signature: %s\n", oSignature.ToString(true, false).c_str()); WriteFile("", "signature.bin", oSignature); } } oReader.Disconnect(); } else printf(" No card found in the reader, exiting\n"); return lRet; }
bool APL_CardFile_Info::MapFields() { /* V1 cards ======== Serial Number (16 bytes) //0 Component code (1 byte) //16 OS number (1 byte) //17 OS version (1 byte) //18 Softmask number (1 byte) //19 Softmask version (1 byte) //20 Applet version (2 bytes) //21 Interface version (2 bytes) //23 PKCS#15 version (2 bytes) //25 Applet Life cycle (1 byte) //27 V2 cards ========= Serial Number (16 bytes) //0 Component code (1 byte) //16 OS number (1 byte) //17 OS version (1 byte) //18 Softmask number (1 byte) //19 Softmask version (1 byte) //20 Applet version (1 byte) //21 Global OS version (2 byte) //22 Applet interface version (1 byte) //24 PKCS#1 support (1 byte) //25 Key exchange version (1 byte) //26 Application Life cycle (1 byte) //27 */ CByteArray baBuffer; std::string sTemp; char buffer[50]; if(m_data.Size() < 27) return false; //SerialNumber baBuffer=m_data.GetBytes(0,16); sTemp=baBuffer.ToString(false); m_SerialNumber=sTemp; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(16)); m_ComponentCode = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(17)); m_OsNumber = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(18)); m_OsVersion = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(19)); m_SoftmaskNumber = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(20)); m_SoftmaskVersion = buffer; int iAppletVersion; iAppletVersion = m_data.GetByte(21); if (iAppletVersion == 0x01) iAppletVersion = 0x10; sprintf_s(buffer,sizeof(buffer),"%X.%X", iAppletVersion / 16, iAppletVersion % 16); m_AppletVersion = buffer; baBuffer=m_data.GetBytes(22,2); sTemp=baBuffer.ToString(false); m_GlobalOsVersion = sTemp; //int iAppletInterfaceVersion; //iAppletInterfaceVersion = (iAppletVersion < 0x20 ? 256 * m_data.GetByte(23) + m_data.GetByte(24) : m_data.GetByte(24)); //sprintf_s(buffer,sizeof(buffer),"%02X", iAppletInterfaceVersion); sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(24)); m_AppletInterfaceVersion = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(25)); m_PKCS1Support = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(26)); m_KeyExchangeVersion = buffer; sprintf_s(buffer,sizeof(buffer),"%02X", m_data.GetByte(27)); m_AppletLifeCicle = buffer; return true; }