int main(int ac, char **av)
{
    using namespace logicalaccess;

    prologue(ac, av);
    introduction();
    ReaderProviderPtr provider;
    ReaderUnitPtr readerUnit;
    ChipPtr chip;
    std::tie(provider, readerUnit, chip) = lla_test_init();

    PRINT_TIME(
        "Chip identifier: " << BufferHelper::getHex(chip->getChipIdentifier()));

    std::shared_ptr<AccessControlCardService> acs =
        std::dynamic_pointer_cast<AccessControlCardService>(
            chip->getService(CST_ACCESS_CONTROL));

    std::array<uint8_t, 64> buffer = {0};

    auto ret = acs->readFormat(std::make_shared<Wiegand26Format>(),
                               std::make_shared<ProxLocation>(), nullptr);
    ret->getLinearData(&buffer[0], buffer.size());

    std::cout << "Format: " << std::vector<uint8_t>(buffer.begin(), buffer.end())
              << std::endl;

    pcsc_test_shutdown(readerUnit);
    return 0;
}
int main(int ac, char **av)
{
    prologue(ac, av);
    introduction();
    ReaderProviderPtr provider;
    ReaderUnitPtr readerUnit;
    ChipPtr chip;
    std::tie(provider, readerUnit, chip) = lla_test_init("EPass");

    PRINT_TIME("Chip identifier: " <<
                   logicalaccess::BufferHelper::getHex(chip->getChipIdentifier()));

    LLA_ASSERT(chip->getCardType() == "EPass",
               "Chip is not a EPass, but is " + chip->getCardType() + " instead.");

    auto srv = std::dynamic_pointer_cast<IdentityCardService>(chip->getService(CST_IDENTITY));
    LLA_ASSERT(srv, "Cannot retrieve identity service from the chip");
    // Prepare the service.
    auto ai = std::make_shared<EPassAccessInfo>();
    ai->mrz_ = "W7GCH9ZY24UTO7904107F2006187<<<<<<<<<<<<<<<2";
    srv->setAccessInfo(ai);


    std::string name;
    LLA_ASSERT(srv->get(IdentityCardService::MetaData::NAME, name), "Failed to fetch name");
    LLA_ASSERT("ANDERSON  JANE" == name, "Name doesn't match.");
    LLA_SUBTEST_PASSED("GetName");

    ByteVector picture_data;
    LLA_ASSERT(srv->get(IdentityCardService::MetaData::PICTURE, picture_data), "Failed to"
        "get picture bytes");

    // We check the hash of the picture rather than the full picture bytes. Easier for tests.
    LLA_ASSERT(openssl::SHA1Hash(picture_data) == BufferHelper::fromHexString("9cb474bfb578a9c8defa8eb6fe9ea2cd643be308"),
               "Retrieved image picture doesn't match expected picture.");
    LLA_SUBTEST_PASSED("GetPicture");

    std::string nationality;
    LLA_ASSERT(srv->get(IdentityCardService::MetaData::NATIONALITY, nationality),
               "Failed to fetch nationality");
    LLA_ASSERT("UTO" == nationality, "Nationality doesn't match.");
    LLA_SUBTEST_PASSED("GetNationality");


    std::string docno;
    LLA_ASSERT(srv->get(IdentityCardService::MetaData::DOC_NO, docno),
               "Failed to fetch document number.");
    LLA_ASSERT(docno == "W7GCH9ZY2", "Document number doesn't match.");
    LLA_SUBTEST_PASSED("GetDocNo");


    std::chrono::system_clock::time_point tp;
    LLA_ASSERT(srv->get(IdentityCardService::MetaData::BIRTHDATE, tp), "Failed to "
        "fetch birthdate");
    std::time_t tp_t = std::chrono::system_clock::to_time_t(tp);
    std::tm tm = *std::localtime(&tp_t);

    char buff[512];
    std::strftime(buff, sizeof(buff), "%c", &tm);
    PRINT_TIME("Birthdate: " << buff);

    pcsc_test_shutdown(readerUnit);

    return EXIT_SUCCESS;
}
int main(int ac, char **av)
{
    using namespace logicalaccess;

    prologue(ac, av);
    introduction();
    ReaderProviderPtr provider;
    ReaderUnitPtr readerUnit;
    ChipPtr chip;
    std::tie(provider, readerUnit, chip) = lla_test_init();

    PRINT_TIME("Chip identifier: " <<
               logicalaccess::BufferHelper::getHex(chip->getChipIdentifier()));

   // LLA_ASSERT(chip->getCardType() == "Mifare1K",
//               "Chip is not a Mifare1K, but is " + chip->getCardType() +
//               " instead.");

    auto storage = std::dynamic_pointer_cast<logicalaccess::StorageCardService>(
            chip->getService(logicalaccess::CST_STORAGE));

    std::shared_ptr<logicalaccess::Location> location;
    std::shared_ptr<logicalaccess::AccessInfo> aiToUse;
    std::shared_ptr<logicalaccess::AccessInfo> aiToWrite;

    // We want to write data on sector 1.
    std::shared_ptr<logicalaccess::MifareLocation> mlocation(
            new logicalaccess::MifareLocation());
    mlocation->sector = 4;
    mlocation->block = 0;
    location = mlocation;

    // Key to use for sector authentication
    std::shared_ptr<logicalaccess::MifareAccessInfo> maiToUse(
            new logicalaccess::MifareAccessInfo());
    maiToUse->keyA->fromString("00 00 00 00 00 00");      // Default key
    maiToUse->keyB->fromString("00 00 00 00 00 00");
    maiToUse->sab.setAReadBWriteConfiguration();
    aiToUse = maiToUse;

    // Change the sector key with the following key
    std::shared_ptr<logicalaccess::MifareAccessInfo> maiToWrite(
            new logicalaccess::MifareAccessInfo());
    maiToWrite->keyA->fromString("00 00 00 00 00 00");
    maiToWrite->keyB->fromString("00 00 00 00 00 00");
    maiToWrite->sab.setAReadBWriteConfiguration(); // Configure the sector access bits
    aiToWrite = maiToWrite;

    // Data to write
    std::vector<uint8_t> writedata(16, 'e');

    // Data read
    std::vector<uint8_t> readdata;

    // Write data on the specified location with the specified key
    storage->writeData(location, aiToUse, aiToWrite, writedata, logicalaccess::CB_DEFAULT);
    using namespace logicalaccess; // required for overload of std::ostrean(vector &)
    PRINT_TIME("Wrote: " << writedata);
    LLA_SUBTEST_PASSED("WriteService")


    const int mifare_block_size = 16;
    // We read the data on the same location. Remember, the key is now changed.
    readdata = storage
            ->readData(location, aiToWrite, mifare_block_size, logicalaccess::CB_DEFAULT);
    PRINT_TIME("Read: " << readdata);
    LLA_SUBTEST_PASSED("ReadService")

    LLA_ASSERT(std::equal(writedata.begin(), writedata.end(), readdata.begin()),
               "Data read is not what we wrote.");
    LLA_SUBTEST_PASSED("CorrectWriteRead");

    pcsc_test_shutdown(readerUnit);
    return 0;
}
Beispiel #4
0
int main(int, char **)
{
    introduction();
    ReaderProviderPtr provider;
    ReaderUnitPtr readerUnit;
    ChipPtr chip;
    std::tie(provider, readerUnit, chip) = pcsc_test_init();

    PRINT_TIME("CHip identifier: " <<
               logicalaccess::BufferHelper::getHex(chip->getChipIdentifier()));

    LLA_ASSERT(chip->getCardType() == "DESFireEV1",
               "Chip is not an DESFireEV1, but is " + chip->getCardType() +
               " instead.");

    auto location_root_node = chip->getRootLocationNode();

    auto cmd = std::dynamic_pointer_cast<logicalaccess::DESFireISO7816Commands>(
            chip->getCommands());
    auto cmdev1 = std::dynamic_pointer_cast<logicalaccess::DESFireEV1ISO7816Commands>(
            chip->getCommands());
    LLA_ASSERT(cmd && cmdev1, "Cannot get correct command object from chip.");

    cmd->selectApplication(0x00);
    cmd->authenticate(0);

    cmd->erase();
    cmdev1->createApplication(0x521, logicalaccess::DESFireKeySettings::KS_DEFAULT, 3,
                              logicalaccess::DESFireKeyType::DF_KEY_AES,
                              logicalaccess::FIDS_NO_ISO_FID, 0, std::vector<unsigned char>());

    cmd->selectApplication(0x521);
    std::shared_ptr<logicalaccess::DESFireKey> key(new logicalaccess::DESFireKey());
    key->setKeyType(logicalaccess::DESFireKeyType::DF_KEY_AES);
    cmd->authenticate(0, key);
    LLA_SUBTEST_PASSED("Authenticate");

    logicalaccess::DESFireAccessRights ar;
    ar.readAccess = logicalaccess::TaskAccessRights::AR_KEY2;
    ar.writeAccess = logicalaccess::TaskAccessRights::AR_KEY1;
    ar.readAndWriteAccess = logicalaccess::TaskAccessRights::AR_KEY1;
    ar.changeAccess = logicalaccess::TaskAccessRights::AR_KEY1;
    cmdev1->createStdDataFile(0x00, logicalaccess::EncryptionMode::CM_ENCRYPT, ar, 4, 0);


    cmd->authenticate(1, key);
    std::vector<unsigned char> data = {0x01, 0x02, 0x03, 0x04}, tmp;
    cmdev1->writeData(0, 0, data, logicalaccess::EncryptionMode::CM_ENCRYPT);

    cmd->authenticate(2, key);
    tmp = cmdev1->readData(0, 0, 4, logicalaccess::EncryptionMode::CM_ENCRYPT);
    LLA_ASSERT(std::equal(data.begin(), data.end(), tmp.begin()),
               "read and write data are different!");
    LLA_SUBTEST_PASSED("WriteRead");

    cmd->authenticate(0x00, key);
    cmd->deleteFile(0x00);

    cmd->authenticate(0x00, key);
    std::shared_ptr<logicalaccess::DESFireKey> newkey(
            new logicalaccess::DESFireKey("00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 03"));
    cmd->changeKey(0x00, newkey);
    LLA_SUBTEST_PASSED("ChangeKey");

    cmd->selectApplication(0x00);
    cmd->authenticate(0);
    cmd->deleteApplication(0x521);

    auto service = std::dynamic_pointer_cast<logicalaccess::AccessControlCardService>(
            chip->getService(logicalaccess::CardServiceType::CST_ACCESS_CONTROL));
    LLA_ASSERT(service, "Cannot retrieve access control service from chip.");

    auto location = std::make_shared<logicalaccess::DESFireLocation>();
    location->aid = 0x522;
    location->file = 0;
    auto ai = std::make_shared<logicalaccess::DESFireAccessInfo>();
    auto format = std::make_shared<logicalaccess::Wiegand26Format>();
    format->setUid(1000);
    format->setFacilityCode(67);

    service->writeFormat(format, location, ai, ai);
    auto formattmp = std::make_shared<logicalaccess::Wiegand26Format>();
    auto rformat = std::dynamic_pointer_cast<logicalaccess::Wiegand26Format>(
            service->readFormat(formattmp, location, ai));

    if (!rformat || rformat->getUid() != 1000 || rformat->getFacilityCode() != 67)
    THROW_EXCEPTION_WITH_LOG(std::runtime_error, "Bad format");
    LLA_SUBTEST_PASSED("ReadFormat");

    pcsc_test_shutdown(readerUnit);

    return EXIT_SUCCESS;
}