uint32_t ScomRegister::ForceRead() const { #define PRDF_FUNC "[ScomRegister::ForceRead] " uint32_t o_rc = FAIL; do { // No read allowed if register access attribute is write-only or no // access. if ( ( ACCESS_NONE == iv_operationType ) && ( ACCESS_WO == iv_operationType ) ) { PRDF_ERR( PRDF_FUNC"Write-only register: 0x%08x 0x%016llx", getChip()->GetId(), iv_scomAddress ); break; } // Read hardware. o_rc = Access( readCache(), MopRegisterAccess::READ ); if ( SUCCESS != o_rc ) { // The read failed. Remove the entry from the cache so a subsequent // Read() will attempt to read from hardware again. flushCache( getChip() ); } } while (0); return o_rc; #undef PRDF_FUNC }
bool DESFireStorageCardService::readData(boost::shared_ptr<Location> location, boost::shared_ptr<AccessInfo> aiToUse, void* data, size_t dataLength, CardBehavior /*behaviorFlags*/) { bool ret = false; EXCEPTION_ASSERT_WITH_LOG(location, std::invalid_argument, "location cannot be null."); EXCEPTION_ASSERT_WITH_LOG(data, std::invalid_argument, "location cannot be null."); boost::shared_ptr<DESFireLocation> dfLocation = boost::dynamic_pointer_cast<DESFireLocation>(location); boost::shared_ptr<DESFireAccessInfo> dfAiToUse = boost::dynamic_pointer_cast<DESFireAccessInfo>(aiToUse); EXCEPTION_ASSERT_WITH_LOG(dfLocation, std::invalid_argument, "location must be a DESFireLocation."); if (aiToUse) { EXCEPTION_ASSERT_WITH_LOG(dfAiToUse, std::invalid_argument, "aiToUse must be a DESFireAccessInfo."); } else { dfAiToUse = boost::dynamic_pointer_cast<DESFireAccessInfo>(getChip()->getProfile()->createAccessInfo()); } getChip()->getProfile()->clearKeys(); getDESFireChip()->getDESFireCommands()->selectApplication(dfLocation); bool needLoadKey = true; EncryptionMode encMode = dfLocation->securityLevel; if (encMode == CM_UNKNOWN) { encMode = getDESFireChip()->getDESFireCommands()->getEncryptionMode(aiToUse, static_cast<unsigned char>(dfLocation->file), true, &needLoadKey); } if (needLoadKey) { if (!dfAiToUse->readKey->isEmpty()) { getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, dfAiToUse->readKeyno, dfAiToUse->readKey); } if (!getDESFireChip()->getDESFireCommands()->authenticate(dfAiToUse->readKeyno)) { THROW_EXCEPTION_WITH_LOG(CardException, EXCEPTION_MSG_AUTHENTICATE); } } size_t reallen = getDESFireChip()->getDESFireCommands()->readData(dfLocation->file, dfLocation->byte, dataLength, data, encMode); if (dataLength <= static_cast<size_t>(reallen)) { ret = true; } return ret; }
uint32_t ScomRegister::Access( BIT_STRING_CLASS & bs, MopRegisterAccess::Operation op ) const { int32_t l_rc = SCR_ACCESS_FAILED; TARGETING::TargetHandle_t i_pchipTarget = getChip()->GetChipHandle(); l_rc = getScomService().Access( i_pchipTarget,bs,iv_scomAddress,op ); return(l_rc); }
void System::printConfig(ostream& out) const { out << "\n================ Begin System Configuration Print ================\n\n"; RubyConfig::printConfiguration(out); out << endl; getChip(0)->printConfig(out); m_network_ptr->printConfig(out); m_driver_ptr->printConfig(out); m_profiler_ptr->printConfig(out); out << "\n================ End System Configuration Print ================\n\n"; }
void LiquidCrystalNew_SSPI::write4bits(byte value) { //still used during init register byte v = value; byte en = _en1; // 4x40 LCD with 2 controller chips with separate enable lines if we called w 2 enable pins and are on lines 2 or 3 enable chip 2 if (_multipleChip && getChip()) en = _en2; bitWrite(_theData,LCDPIN_D4,v & 01); bitWrite(_theData,LCDPIN_D5,(v >>= 1) & 01); bitWrite(_theData,LCDPIN_D6,(v >>= 1) & 01); bitWrite(_theData,LCDPIN_D7,(v >>= 1) & 01); pulseEnable(en); }
bool ScomRegisterAccess::operator < ( const ScomRegisterAccess & i_rightRegister ) const { if( GetAddress() == i_rightRegister.GetAddress() ) { return ( getChip() < i_rightRegister.getChip() ); } else { return ( GetAddress() < i_rightRegister.GetAddress() ); } }
void LiquidCrystalNew_T3TWI::write4bits(byte value) { //still used during init register byte v = value; byte en = _en1; if (_multipleChip){ if (getChip()) en = _en2; } bitWrite(_theData,LCDPIN_D4,v & 01); bitWrite(_theData,LCDPIN_D5,(v >>= 1) & 01); bitWrite(_theData,LCDPIN_D6,(v >>= 1) & 01); bitWrite(_theData,LCDPIN_D7,(v >>= 1) & 01); pulseEnable(en); }
void ISO15693StorageCardService::erase() { std::shared_ptr<LocationNode> rootNode = getChip()->getRootLocationNode(); std::vector<std::shared_ptr<LocationNode> > childs = rootNode->getChildrens(); for (size_t i = 0; i < childs.size(); ++i) { std::shared_ptr<LocationNode> blockNode = childs.at(i); std::shared_ptr<AccessInfo> ai; erase(blockNode->getLocation(), ai); } }
std::vector<unsigned char> DESFireStorageCardService::readData(std::shared_ptr<Location> location, std::shared_ptr<AccessInfo> aiToUse, size_t dataLength, CardBehavior /*behaviorFlags*/) { EXCEPTION_ASSERT_WITH_LOG(location, std::invalid_argument, "location cannot be null."); std::shared_ptr<DESFireLocation> dfLocation = std::dynamic_pointer_cast<DESFireLocation>(location); std::shared_ptr<DESFireAccessInfo> dfAiToUse = std::dynamic_pointer_cast<DESFireAccessInfo>(aiToUse); EXCEPTION_ASSERT_WITH_LOG(dfLocation, std::invalid_argument, "location must be a DESFireLocation."); if (aiToUse) { EXCEPTION_ASSERT_WITH_LOG(dfAiToUse, std::invalid_argument, "aiToUse must be a DESFireAccessInfo."); } else { dfAiToUse = std::dynamic_pointer_cast<DESFireAccessInfo>(getChip()->getProfile()->createAccessInfo()); } getChip()->getProfile()->setDefaultKeysAt(dfLocation); getDESFireChip()->getDESFireCommands()->selectApplication(dfLocation); bool needLoadKey = true; EncryptionMode encMode = dfLocation->securityLevel; if (encMode == CM_UNKNOWN || encMode == CM_PLAIN) { encMode = getDESFireChip()->getDESFireCommands()->getEncryptionMode(static_cast<unsigned char>(dfLocation->file), true, &needLoadKey); } if (needLoadKey) { if (!dfAiToUse->readKey->isEmpty()) { getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, dfAiToUse->readKeyno, dfAiToUse->readKey); } getDESFireChip()->getDESFireCommands()->authenticate(dfAiToUse->readKeyno); } return getDESFireChip()->getDESFireCommands()->readData(dfLocation->file, dfLocation->byte, dataLength, encMode); }
unsigned int MifareCommands::getSectorFromMAD(long aid, boost::shared_ptr<MifareKey> madKeyA) { unsigned int sector = static_cast<unsigned int>(-1); MifareAccessInfo::SectorAccessBits sab; if (!madKeyA->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(0, KT_KEY_A, madKeyA); } unsigned char madbuf[32]; memset(madbuf, 0x00, sizeof(madbuf)); if (!readSector(0, 1, madbuf, sizeof(madbuf), sab)) { THROW_EXCEPTION_WITH_LOG(LibLogicalAccessException, "Can't read the MAD."); } unsigned char madcrc = calculateMADCrc(madbuf, sizeof(madbuf)); if (madcrc != madbuf[0]) { THROW_EXCEPTION_WITH_LOG(LibLogicalAccessException, "Bad MAD CRC."); } sector = findReferencedSector(aid, madbuf, sizeof(madbuf)); if ((sector == static_cast<unsigned int>(-1)) && getChip()->getCardType() == "Mifare4K") { unsigned char madbuf2[48]; memset(madbuf2, 0x00, sizeof(madbuf2)); if (readSector(16, 0, madbuf2, sizeof(madbuf2), sab) != sizeof(madbuf2)) { THROW_EXCEPTION_WITH_LOG(LibLogicalAccessException, "Can't read the MAD2."); } unsigned char mad2crc = calculateMADCrc(madbuf2, sizeof(madbuf2)); if (mad2crc != madbuf2[0]) { THROW_EXCEPTION_WITH_LOG(LibLogicalAccessException, "Bad MAD2 CRC."); } sector = findReferencedSector(aid, madbuf2, sizeof(madbuf2)); if (sector != static_cast<unsigned int>(-1)) { sector += 16; } } return sector; }
uint32_t ScomRegister::Write() { #define PRDF_FUNC "[ScomRegister::Write] " uint32_t o_rc = FAIL; do { // No write allowed if register access attribute is read-only or no // access. if ( ( ACCESS_NONE == iv_operationType ) && ( ACCESS_RO == iv_operationType ) ) { PRDF_ERR( PRDF_FUNC"Read-only register: 0x%08x 0x%016llx", getChip()->GetId(), iv_scomAddress ); break; } // Query the cache for an existing entry. if ( !queryCache() ) { // Something bad happened and there was nothing in the cache to // write to hardware. PRDF_ERR( PRDF_FUNC"No entry found in cache: 0x%08x 0x%016llx", getChip()->GetId(), iv_scomAddress ); break; } // Write hardware. o_rc = Access( readCache(), MopRegisterAccess::WRITE ); } while (0); return o_rc; #undef PRDF_FUNC }
// write either command or data, with automatic 4/8-bit selection void LiquidCrystalNew_SSPI::send(uint8_t value, byte mode) { byte en = _en1; if (_multipleChip && getChip()) en = _en2; //delayMicroseconds(DELAYPERCHAR); setDataMode(mode); // I2C & SPI bitWrite(_theData,LCDPIN_D4,value & 0x10); bitWrite(_theData,LCDPIN_D5,value & 0x20); bitWrite(_theData,LCDPIN_D6,value & 0x40); bitWrite(_theData,LCDPIN_D7,value & 0x80); pulseEnable(en); bitWrite(_theData,LCDPIN_D4,value & 0x01); bitWrite(_theData,LCDPIN_D5,value & 0x02); bitWrite(_theData,LCDPIN_D6,value & 0x04); bitWrite(_theData,LCDPIN_D7,value & 0x08); bitWrite(_theData,LCDPIN_LD,_backLight);//Background led pulseEnable(en); }
// write either command or data, with automatic 4/8-bit selection void LiquidCrystalNew_T3TWI::send(uint8_t value, byte mode) { byte en = _en1; if (_multipleChip){ if (getChip()) en = _en2; } setDataMode(mode); // I2C & SPI bitWrite(_theData,LCDPIN_D4,value & 0x10); bitWrite(_theData,LCDPIN_D5,value & 0x20); bitWrite(_theData,LCDPIN_D6,value & 0x40); bitWrite(_theData,LCDPIN_D7,value & 0x80); pulseEnable(en); bitWrite(_theData,LCDPIN_D4,value & 0x01); bitWrite(_theData,LCDPIN_D5,value & 0x02); bitWrite(_theData,LCDPIN_D6,value & 0x04); bitWrite(_theData,LCDPIN_D7,value & 0x08); bitWrite(_theData,LCDPIN_LD,_backLight);//Background led pulseEnable(en); }
bool MifarePlusSL3Commands::authenticate(boost::shared_ptr<Location> location, boost::shared_ptr<AccessInfo> ai) { boost::shared_ptr<MifarePlusAccessInfo> mAiToUse = boost::dynamic_pointer_cast<MifarePlusAccessInfo>(ai); if (ai) { EXCEPTION_ASSERT(mAiToUse, std::invalid_argument, "ai must be a MifarePlusAccessInfo."); } else { mAiToUse = boost::dynamic_pointer_cast<MifarePlusAccessInfo>(getChip()->getProfile()->createAccessInfo()); } if (mAiToUse->keyConfiguration && !mAiToUse->keyConfiguration->isEmpty()) authenticate(0, mAiToUse->keyConfiguration, KT_KEY_CONFIGURATION); else if (mAiToUse->keyMastercard && !mAiToUse->keyMastercard->isEmpty()) authenticate(0, mAiToUse->keyMastercard, KT_KEY_MASTERCARD); if (mAiToUse->keyOriginality && !mAiToUse->keyOriginality->isEmpty()) authenticate(0, mAiToUse->keyOriginality, KT_KEY_ORIGINALITY); if (mAiToUse->keyA && !mAiToUse->keyA->isEmpty()) { EXCEPTION_ASSERT(location, std::invalid_argument, "location cannot be null."); boost::shared_ptr<MifarePlusLocation> mLocation = boost::dynamic_pointer_cast<MifarePlusLocation>(location); EXCEPTION_ASSERT(mLocation, std::invalid_argument, "location must be a MifarePlusLocation."); authenticate(mLocation->sector, mAiToUse->keyA, KT_KEY_AES_A); } if (mAiToUse->keyB && !mAiToUse->keyB->isEmpty()) { EXCEPTION_ASSERT(location, std::invalid_argument, "location cannot be null."); boost::shared_ptr<MifarePlusLocation> mLocation = boost::dynamic_pointer_cast<MifarePlusLocation>(location); EXCEPTION_ASSERT(mLocation, std::invalid_argument, "location must be a MifarePlusLocation."); authenticate(mLocation->sector, mAiToUse->keyB, KT_KEY_AES_B); } return true; }
void DESFireEV1NFCTag4CardService::deleteNFCApplication(unsigned int aid, std::shared_ptr<logicalaccess::DESFireKey> masterPICCKey) { std::shared_ptr<logicalaccess::DESFireCommands> desfirecommand(std::dynamic_pointer_cast<logicalaccess::DESFireCommands>(getChip()->getCommands())); desfirecommand->selectApplication(0x00); if (masterPICCKey) { desfirecommand->authenticate(0x00, masterPICCKey); } desfirecommand->deleteApplication(aid); }
void DESFireEV1NFCTag4CardService::createNFCApplication(unsigned int aid, std::shared_ptr<logicalaccess::DESFireKey> masterPICCKey, unsigned short isoFIDApplication, unsigned short isoFIDCapabilityContainer, unsigned short isoFIDNDEFFile, unsigned short NDEFFileSize) { std::shared_ptr<logicalaccess::DESFireCommands> desfirecommand(std::dynamic_pointer_cast<logicalaccess::DESFireCommands>(getChip()->getCommands())); std::shared_ptr<logicalaccess::DESFireEV1Commands> desfireev1command(std::dynamic_pointer_cast<logicalaccess::DESFireEV1Commands>(getChip()->getCommands())); desfirecommand->selectApplication(0x00); if (masterPICCKey) { desfirecommand->authenticate(0x00, masterPICCKey); } unsigned char dfName[] = { 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x01 }; std::vector<unsigned char> dfNameVector(dfName, dfName + 7); desfireev1command->createApplication(aid, logicalaccess::DESFireKeySettings::KS_DEFAULT, 1, logicalaccess::DESFireKeyType::DF_KEY_DES, logicalaccess::FidSupport::FIDS_ISO_FID, isoFIDApplication, dfNameVector); std::shared_ptr<logicalaccess::DESFireKey> key(new logicalaccess::DESFireKey("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")); key->setKeyType(logicalaccess::DESFireKeyType::DF_KEY_DES); desfirecommand->selectApplication(aid); desfirecommand->authenticate(0x00, key); logicalaccess::DESFireAccessRights dar; dar.changeAccess = logicalaccess::TaskAccessRights::AR_KEY0; dar.readAccess = logicalaccess::TaskAccessRights::AR_FREE; dar.readAndWriteAccess = logicalaccess::TaskAccessRights::AR_KEY0; dar.writeAccess = logicalaccess::TaskAccessRights::AR_KEY0; desfireev1command->createStdDataFile(0x01, logicalaccess::EncryptionMode::CM_PLAIN, dar, 0x0f, isoFIDCapabilityContainer); writeCapabilityContainer(isoFIDCapabilityContainer, isoFIDNDEFFile, NDEFFileSize); dar.changeAccess = logicalaccess::TaskAccessRights::AR_KEY0; dar.readAccess = logicalaccess::TaskAccessRights::AR_FREE; dar.readAndWriteAccess = logicalaccess::TaskAccessRights::AR_FREE; dar.writeAccess = logicalaccess::TaskAccessRights::AR_FREE; desfireev1command->createStdDataFile(0x02, logicalaccess::EncryptionMode::CM_PLAIN, dar, 0xff, isoFIDNDEFFile); }
std::shared_ptr<MifareUltralightChip> getMifareUltralightChip() { return std::dynamic_pointer_cast<MifareUltralightChip>(getChip()); };
std::shared_ptr<ISO7816Chip> getISO7816Chip() { return std::dynamic_pointer_cast<ISO7816Chip>(getChip()); };
std::shared_ptr<FeliCaChip> getFeliCaChip() { return std::dynamic_pointer_cast<FeliCaChip>(getChip()); };
bool ScomRegister::queryCache() const { RegDataCache & cache = RegDataCache::getCachedRegisters(); BIT_STRING_CLASS * bs = cache.queryCache( getChip(), this ); return ( NULL != bs ); }
std::shared_ptr<TopazChip> TopazCommands::getTopazChip() { return std::dynamic_pointer_cast<TopazChip>(getChip()); }
std::vector<unsigned char> MifareStorageCardService::readData(std::shared_ptr<Location> location, std::shared_ptr<AccessInfo> aiToUse, size_t length, CardBehavior behaviorFlags) { std::vector<unsigned char> ret; EXCEPTION_ASSERT_WITH_LOG(location, std::invalid_argument, "location cannot be null."); std::shared_ptr<MifareLocation> mLocation = std::dynamic_pointer_cast<MifareLocation>(location); std::shared_ptr<MifareAccessInfo> mAiToUse = std::dynamic_pointer_cast<MifareAccessInfo>(aiToUse); EXCEPTION_ASSERT_WITH_LOG(mLocation, std::invalid_argument, "location must be a MifareLocation."); if (aiToUse) { EXCEPTION_ASSERT_WITH_LOG(mAiToUse, std::invalid_argument, "aiToUse must be a MifareAccessInfo."); } else { mAiToUse = std::dynamic_pointer_cast<MifareAccessInfo>(getChip()->getProfile()->createAccessInfo()); } getChip()->getProfile()->clearKeys(); if (mLocation->useMAD) { mLocation->sector = getMifareChip()->getMifareCommands()->getSectorFromMAD(mLocation->aid, mAiToUse->madKeyA); } size_t totaldatalen = length + (mLocation->block * 16) + mLocation->byte; int nbSectors = 0; size_t totalbuflen = 0; while (totalbuflen < totaldatalen) { totalbuflen += getMifareChip()->getMifareCommands()->getNbBlocks(mLocation->sector + nbSectors) * 16; nbSectors++; } for (int i = 0; i < nbSectors; ++i) { if (!mAiToUse->keyA->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(mLocation->sector + i, KT_KEY_A, mAiToUse->keyA); } if (!mAiToUse->keyB->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(mLocation->sector + i, KT_KEY_B, mAiToUse->keyB); } } if (nbSectors >= 1) { std::vector<unsigned char> dataSectors; if (behaviorFlags & CB_AUTOSWITCHAREA) { dataSectors = getMifareChip()->getMifareCommands()->readSectors(mLocation->sector, mLocation->sector + nbSectors - 1, mLocation->block, mAiToUse->sab); } else { dataSectors = getMifareChip()->getMifareCommands()->readSector(mLocation->sector, mLocation->block, mAiToUse->sab); } if (length <= (dataSectors.size() - mLocation->byte)) { ret.insert(ret.end(), dataSectors.begin() + mLocation->byte, dataSectors.begin() + mLocation->byte + length); } } return ret; }
std::shared_ptr<DESFireEV1Chip> getDESFireChip() { return std::dynamic_pointer_cast<DESFireEV1Chip>(getChip()); };
std::shared_ptr<logicalaccess::NdefMessage> DESFireEV1NFCTag4CardService::readNDEFFile(unsigned short isoFIDApplication, unsigned short isoFIDNDEFFile) { std::shared_ptr<logicalaccess::DESFireCommands> desfirecommand(std::dynamic_pointer_cast<logicalaccess::DESFireCommands>(getChip()->getCommands())); desfirecommand->selectApplication(0x00); return ISO7816NFCTag4CardService::readNDEFFile(isoFIDApplication, isoFIDNDEFFile); }
void DESFireStorageCardService::writeData(std::shared_ptr<Location> location, std::shared_ptr<AccessInfo> aiToUse, std::shared_ptr<AccessInfo> aiToWrite, const std::vector<unsigned char>& data, CardBehavior /*behaviorFlags*/) { LOG(LogLevel::INFOS) << "Starting write data..."; EXCEPTION_ASSERT_WITH_LOG(location, std::invalid_argument, "location cannot be null."); std::shared_ptr<DESFireLocation> dfLocation = std::dynamic_pointer_cast<DESFireLocation>(location); std::shared_ptr<DESFireAccessInfo> dfAiToUse = std::dynamic_pointer_cast<DESFireAccessInfo>(aiToUse); std::shared_ptr<DESFireAccessInfo> dfAiToWrite = std::dynamic_pointer_cast<DESFireAccessInfo>(aiToWrite); EXCEPTION_ASSERT_WITH_LOG(dfLocation, std::invalid_argument, "location must be a DESFireLocation."); if (aiToUse != NULL) { EXCEPTION_ASSERT_WITH_LOG(dfAiToUse, std::invalid_argument, "aiToUse must be a DESFireAccessInfo."); } else { dfAiToUse = std::dynamic_pointer_cast<DESFireAccessInfo>(getChip()->getProfile()->createAccessInfo()); } if (aiToWrite) { EXCEPTION_ASSERT_WITH_LOG(dfAiToWrite, std::invalid_argument, "aiToWrite must be a DESFireAccessInfo."); } getDESFireChip()->getProfile()->clearKeys(); if (!dfAiToUse->masterCardKey->isEmpty()) { getDESFireChip()->getDESFireProfile()->setKey(0, 0, dfAiToUse->masterCardKey); } else { getDESFireChip()->getDESFireProfile()->setKey(0, 0, std::shared_ptr<DESFireKey>(new DESFireKey(string("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00")))); } getDESFireChip()->getDESFireCommands()->selectApplication(0); bool createArbo = true; try { getDESFireChip()->getDESFireCommands()->authenticate(0); // Create application if doesn't exist std::vector<unsigned int> aids = getDESFireChip()->getDESFireCommands()->getApplicationIDs(); if (aiToWrite) { for (std::vector<unsigned int>::const_iterator aid = aids.cbegin(); (aid != aids.cend()) && createArbo; ++aid) { createArbo = (*aid != dfLocation->aid); } } } catch (std::exception&) { } try { if (createArbo && dfAiToWrite) { int maxNbKeys = 0; if (dfAiToWrite->readKeyno > dfAiToWrite->writeKeyno) { if (dfAiToWrite->readKeyno < 0xe) { maxNbKeys = dfAiToWrite->readKeyno + 1; } } if (maxNbKeys == 0) { if (dfAiToWrite->writeKeyno < 0xe) { maxNbKeys = dfAiToWrite->writeKeyno + 1; } } if (maxNbKeys == 0) { maxNbKeys = 1; } getDESFireChip()->getDESFireCommands()->createApplication(dfLocation, KS_DEFAULT, maxNbKeys); } } catch (std::exception&) { createArbo = false; } getDESFireChip()->getDESFireCommands()->selectApplication(dfLocation); DESFireKeyType cryptoMethod = DF_KEY_DES; if (std::dynamic_pointer_cast<DESFireEV1Location>(dfLocation)) { cryptoMethod = std::dynamic_pointer_cast<DESFireEV1Location>(dfLocation)->cryptoMethod; } if (!dfAiToUse->masterApplicationKey->isEmpty()) { dfAiToUse->masterApplicationKey->setKeyType(cryptoMethod); getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, 0, dfAiToUse->masterApplicationKey); } else { if (!createArbo) { getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, 0, getDESFireChip()->getDESFireProfile()->getDefaultKey(cryptoMethod)); } } if (!dfAiToUse->writeKey->isEmpty() && dfAiToUse->writeKeyno != 0x00) { dfAiToUse->writeKey->setKeyType(cryptoMethod); getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, dfAiToUse->writeKeyno, dfAiToUse->writeKey); } if (!dfAiToUse->readKey->isEmpty() && dfAiToUse->readKeyno != 0x00) { dfAiToUse->readKey->setKeyType(cryptoMethod); getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, dfAiToUse->readKeyno, dfAiToUse->readKey); } DESFireKeySettings appKeySettings = KS_DEFAULT; bool needLoadKey = true; createArbo = true; try { getDESFireChip()->getDESFireCommands()->authenticate(0); unsigned char appMaxNbKeys = 3; getDESFireChip()->getDESFireCommands()->getKeySettings(appKeySettings, appMaxNbKeys); std::vector<unsigned char> files = getDESFireChip()->getDESFireCommands()->getFileIDs(); if (aiToWrite) { for (std::vector<unsigned char>::const_iterator file = files.cbegin(); (file != files.cend()) && createArbo; ++file) { createArbo = (*file != dfLocation->file); } } } catch (std::exception&) { } try { if (createArbo && dfAiToWrite) { DESFireAccessRights rights; rights.readAccess = (TaskAccessRights)dfAiToWrite->readKeyno; // AR_KEY1 rights.writeAccess = (TaskAccessRights)dfAiToWrite->writeKeyno; // AR_KEY2; rights.readAndWriteAccess = (TaskAccessRights)dfAiToWrite->writeKeyno; //AR_KEY2 rights.changeAccess = (TaskAccessRights)dfAiToWrite->writeKeyno; dfAiToUse->writeKeyno = dfAiToWrite->writeKeyno; if (dfLocation->securityLevel == CM_UNKNOWN) { dfLocation->securityLevel = CM_ENCRYPT; } getDESFireChip()->getDESFireCommands()->createStdDataFile(dfLocation, rights, data.size() + dfLocation->byte); needLoadKey = false; } } catch (std::exception&) { } if (needLoadKey && !dfAiToUse->writeKey->isEmpty() && dfAiToUse->writeKeyno != 0) { getDESFireChip()->getDESFireProfile()->setKey(dfLocation->aid, dfAiToUse->writeKeyno, dfAiToUse->writeKey); } needLoadKey = true; EncryptionMode encMode = dfLocation->securityLevel; if (encMode == CM_UNKNOWN) { encMode = getDESFireChip()->getDESFireCommands()->getEncryptionMode(static_cast<unsigned char>(dfLocation->file), false, &needLoadKey); } if (needLoadKey) { getDESFireChip()->getDESFireCommands()->authenticate(dfAiToUse->writeKeyno); } getDESFireChip()->getDESFireCommands()->writeData(dfLocation->file, dfLocation->byte, data, encMode); // Write access informations too if (aiToWrite) { LOG(LogLevel::INFOS) << "Starting to change keys..."; bool changeKeys = ((appKeySettings & KS_CHANGE_KEY_WITH_TARGETED_KEYNO) == KS_CHANGE_KEY_WITH_TARGETED_KEYNO); if (!changeKeys) { try { getDESFireChip()->getDESFireCommands()->authenticate(0); changeKeys = true; } catch (std::exception&) { changeKeys = false; } } if (changeKeys) { if (!dfAiToWrite->writeKey->isEmpty() && dfAiToUse->writeKey != dfAiToWrite->writeKey && dfAiToWrite->writeKeyno != 0x00) { try { if (appKeySettings & KS_CHANGE_KEY_WITH_TARGETED_KEYNO) { getDESFireChip()->getDESFireCommands()->authenticate(dfAiToWrite->writeKeyno); } getDESFireChip()->getDESFireCommands()->changeKey(dfAiToWrite->writeKeyno, dfAiToWrite->writeKey); } catch (std::exception& ex) { THROW_EXCEPTION_WITH_LOG(CardException, std::string("Write key: ") + ex.what()); } } if (!dfAiToWrite->readKey->isEmpty() && dfAiToUse->readKey != dfAiToWrite->readKey && dfAiToWrite->readKeyno != 0x00) { try { if (appKeySettings & KS_CHANGE_KEY_WITH_TARGETED_KEYNO) { getDESFireChip()->getDESFireCommands()->authenticate(dfAiToWrite->readKeyno); } LOG(LogLevel::INFOS) << "Changing readKey."; getDESFireChip()->getDESFireCommands()->changeKey(dfAiToWrite->readKeyno, dfAiToWrite->readKey); } catch (std::exception& ex) { THROW_EXCEPTION_WITH_LOG(CardException, std::string("Read key: ") + ex.what()); } } if (!dfAiToWrite->masterApplicationKey->isEmpty() && dfAiToUse->masterApplicationKey != dfAiToWrite->masterApplicationKey) { try { if (appKeySettings & KS_CHANGE_KEY_WITH_TARGETED_KEYNO) { getDESFireChip()->getDESFireCommands()->authenticate(0); } LOG(LogLevel::INFOS) << "Changing masterApplicationKey."; getDESFireChip()->getDESFireCommands()->changeKey(0, dfAiToWrite->masterApplicationKey); } catch (std::exception& ex) { THROW_EXCEPTION_WITH_LOG(CardException, std::string("Master application key: ") + ex.what()); } } if (!dfAiToWrite->masterCardKey->isEmpty() && dfAiToUse->masterCardKey != dfAiToWrite->masterCardKey) { try { getDESFireChip()->getDESFireCommands()->selectApplication(0x00); getDESFireChip()->getDESFireCommands()->authenticate(0); LOG(LogLevel::INFOS) << "Changing masterCardKey. div? " << (dfAiToWrite->masterCardKey->getKeyDiversification() == NULL); getDESFireChip()->getDESFireCommands()->changeKey(0, dfAiToWrite->masterCardKey); } catch (std::exception& ex) { THROW_EXCEPTION_WITH_LOG(CardException, std::string("Master card key: ") + ex.what()); } } } } }
void DESFireEV1NFCTag4CardService::eraseNDEF() { std::shared_ptr<logicalaccess::DESFireCommands> desfirecommand(std::dynamic_pointer_cast<logicalaccess::DESFireCommands>(getChip()->getCommands())); desfirecommand->erase(); }
boost::shared_ptr<DESFireEV1Chip> DESFireEV1Commands::getDESFireEV1Chip() const { return boost::dynamic_pointer_cast<DESFireEV1Chip>(getChip()); }
void MifareStorageCardService::writeData(std::shared_ptr<Location> location, std::shared_ptr<AccessInfo> aiToUse, std::shared_ptr<AccessInfo> aiToWrite, const std::vector<unsigned char>& data, CardBehavior behaviorFlags) { EXCEPTION_ASSERT_WITH_LOG(location, std::invalid_argument, "location cannot be null."); std::shared_ptr<MifareLocation> mLocation = std::dynamic_pointer_cast<MifareLocation>(location); std::shared_ptr<MifareAccessInfo> mAiToUse = std::dynamic_pointer_cast<MifareAccessInfo>(aiToUse); EXCEPTION_ASSERT_WITH_LOG(mLocation, std::invalid_argument, "location must be a MifareLocation."); if (aiToUse) { EXCEPTION_ASSERT_WITH_LOG(mAiToUse, std::invalid_argument, "aiToUse must be a MifareAccessInfo."); } else { mAiToUse = std::dynamic_pointer_cast<MifareAccessInfo>(getChip()->getProfile()->createAccessInfo()); } bool writeAidToMad = false; getChip()->getProfile()->clearKeys(); if (mLocation->useMAD) { if (mLocation->sector == 0) { mLocation->sector = getMifareChip()->getMifareCommands()->getSectorFromMAD(mLocation->aid, mAiToUse->madKeyA); } else { writeAidToMad = true; } } size_t totaldatalen = data.size() + (mLocation->block * 16) + mLocation->byte; int nbSectors = 0; size_t totalbuflen = 0; while (totalbuflen < totaldatalen) { totalbuflen += getMifareChip()->getMifareCommands()->getNbBlocks(mLocation->sector + nbSectors) * 16; nbSectors++; // user data on sector trailer ? if (nbSectors == 1 && (totaldatalen - totalbuflen) == 1) { totalbuflen++; } } if (nbSectors >= 1) { size_t buflen = totalbuflen - (mLocation->block * 16); std::vector<unsigned char> dataSectors; dataSectors.resize(buflen, 0x00); std::copy(data.begin(), data.end(), dataSectors.begin() + mLocation->byte); if (writeAidToMad) { for (int i = mLocation->sector; i < (mLocation->sector + nbSectors); ++i) { getMifareChip()->getMifareCommands()->setSectorToMAD(mLocation->aid, i, mAiToUse->madKeyA, mAiToUse->madKeyB); } } for (int i = 0; i < nbSectors; ++i) { if (!mAiToUse->keyA->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(mLocation->sector + i, KT_KEY_A, mAiToUse->keyA); } if (!mAiToUse->keyB->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(mLocation->sector + i, KT_KEY_B, mAiToUse->keyB); } } std::shared_ptr<MifareAccessInfo> mAiToWrite; // Write access informations too if (aiToWrite) { // Sector keys will be changed after writing mAiToWrite = std::dynamic_pointer_cast<MifareAccessInfo>(aiToWrite); EXCEPTION_ASSERT_WITH_LOG(mAiToWrite, std::invalid_argument, "mAiToWrite must be a MifareAccessInfo."); // MAD keys are changed now if (writeAidToMad && mAiToWrite->useMAD) { unsigned int madsector = (mLocation->sector <= 16) ? 0 : 16; MifareAccessInfo::SectorAccessBits sab; getMifareChip()->getMifareProfile()->setKey(madsector, KT_KEY_A, mAiToUse->madKeyA); if (mAiToUse->madKeyB->isEmpty()) { sab.setTransportConfiguration(); } else { sab.setAReadBWriteConfiguration(); getMifareChip()->getMifareProfile()->setKey(madsector, KT_KEY_B, mAiToUse->madKeyB); } MifareAccessInfo::SectorAccessBits newsab; if (mAiToWrite->madKeyB->isEmpty()) { newsab.setTransportConfiguration(); } else { newsab.setAReadBWriteConfiguration(); } if (!mAiToWrite->madKeyA->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(getMifareChip()->getNbSectors(), KT_KEY_A, mAiToWrite->madKeyA); } if (!mAiToWrite->madKeyB->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(getMifareChip()->getNbSectors(), KT_KEY_B, mAiToWrite->madKeyB); } getMifareChip()->getMifareCommands()->changeKey(mAiToWrite->madKeyA, mAiToWrite->madKeyB, madsector, sab, &newsab, mAiToWrite->madGPB); } if (!mAiToWrite->keyA->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(getMifareChip()->getNbSectors(), KT_KEY_A, mAiToWrite->keyA); } if (!mAiToWrite->keyB->isEmpty()) { getMifareChip()->getMifareProfile()->setKey(getMifareChip()->getNbSectors(), KT_KEY_B, mAiToWrite->keyB); } // No key change, set to null (to not change sab, ...) if (mAiToWrite->keyA->isEmpty() && mAiToWrite->keyB->isEmpty()) { mAiToWrite.reset(); } } if (behaviorFlags & CB_AUTOSWITCHAREA) { getMifareChip()->getMifareCommands()->writeSectors(mLocation->sector, mLocation->sector + nbSectors - 1, mLocation->block, dataSectors, mAiToUse->sab, mAiToUse->gpb, (mAiToWrite) ? &(mAiToWrite->sab) : NULL); } else { getMifareChip()->getMifareCommands()->writeSector(mLocation->sector, mLocation->block, dataSectors, mAiToUse->sab, mAiToUse->gpb, (mAiToWrite) ? &(mAiToWrite->sab) : NULL); } getMifareChip()->getMifareProfile()->setKeyUsage(getMifareChip()->getNbSectors(), KT_KEY_A, false); getMifareChip()->getMifareProfile()->setKeyUsage(getMifareChip()->getNbSectors(), KT_KEY_B, false); } }
BIT_STRING_CLASS & ScomRegister::readCache() const { RegDataCache & cache = RegDataCache::getCachedRegisters(); return cache.read( getChip(), this ); }